From c634c0ca36618a3c23bbfeaff0d46b86444ec4b6 Mon Sep 17 00:00:00 2001 From: Didzis Gosko Date: Wed, 1 Nov 2017 23:31:53 +0200 Subject: [PATCH] macOS support --- BUILD | 217 +++++++++++++++++- WORKSPACE | 23 ++ .../text_level/lua_bindings.cc | 7 +- docs/build_macos.md | 128 +++++++++++ .../deepmind/headless_native_macos_glimp.c | 80 +++++++ glib_system.BUILD | 28 +++ libxml_system.BUILD | 15 ++ lua.BUILD | 30 ++- numpy.BUILD | 11 + osmesa.BUILD | 76 ++++++ public/dmlab_so_loader.cc | 40 ++++ python.BUILD | 16 +- q3map2/BUILD | 21 +- sdl.BUILD | 20 +- 14 files changed, 691 insertions(+), 21 deletions(-) create mode 100644 docs/build_macos.md create mode 100644 engine/code/deepmind/headless_native_macos_glimp.c create mode 100644 glib_system.BUILD create mode 100644 libxml_system.BUILD create mode 100644 numpy.BUILD create mode 100644 osmesa.BUILD diff --git a/BUILD b/BUILD index 3c683b19..9d972bd9 100644 --- a/BUILD +++ b/BUILD @@ -545,6 +545,37 @@ cc_binary( deps = ["@zlib_archive//:zlib"], ) +objc_library( + name = "macos_dlg_lib", + srcs = [ + CODE_DIR + "/sys/sys_osx.m" + ], + hdrs = [ + "public/dmlab.h", + CODE_DIR + "/deepmind/context.h", + ] + glob( + [ + CODE_DIR + "/botlib/*.h", + CODE_DIR + "/client/*.h", + CODE_DIR + "/qcommon/*.h", + CODE_DIR + "/sys/*.h", + CODE_DIR + "/server/*.h", + CODE_DIR + "/renderercommon/*.h", + CODE_DIR + "/renderergl1/*.h", + ], + exclude = [ + CODE_DIR + "/client/fx_*.h", + CODE_DIR + "/qcommon/vm_powerpc_asm.h", + CODE_DIR + "/qcommon/vm_sparc.h", + CODE_DIR + "/renderercommon/tr_types.h", + CODE_DIR + "/renderercommon/tr_public.h", + CODE_DIR + "/renderergl1/tr_local.h", + ], + ), + sdk_frameworks = ["Cocoa", "Carbon"], + copts = ["-fno-objc-arc"], +) + # To link with the SDL frontend, depend on :game_lib_sdl # and add "-lGL" to the linker flags. cc_library( @@ -722,7 +753,11 @@ cc_library( "//third_party/rl_api:env_c_api", "@jpeg_archive//:jpeg", "@sdl_system//:sdl2", - ], + ] + select({ + ":darwin": ["@osmesa_system//:osmesa"], + ":darwin_x86_64": ["@osmesa_system//:osmesa"], + "//conditions:default": [], + }), ) # To link with the headless GLX frontend, depend on :game_lib_headless_glx @@ -815,6 +850,94 @@ cc_library( ], ) +cc_library( + name = "game_lib_headless_darwin", + srcs = [ + CODE_DIR + "/asm/ftola.c", + CODE_DIR + "/asm/qasm-inline.h", + CODE_DIR + "/asm/snapvector.c", + CODE_DIR + "/cgame/cg_public.h", + CODE_DIR + "/client/libmumblelink.c", + CODE_DIR + "/deepmind/dm_public.h", + CODE_DIR + "/deepmind/dmlab_connect.c", + CODE_DIR + "/game/bg_public.h", + CODE_DIR + "/game/g_public.h", + CODE_DIR + "/null/null_input.c", + CODE_DIR + "/null/null_snddma.c", + CODE_DIR + "/sys/con_log.c", + CODE_DIR + "/sys/con_passive.c", + CODE_DIR + "/sys/sys_main.c", + CODE_DIR + "/sys/sys_unix.c", + CODE_DIR + "/ui/ui_public.h", + + ## OpenGL rendering + CODE_DIR + "/deepmind/headless_native_macos_glimp.c", + CODE_DIR + "/deepmind/glimp_common.h", + CODE_DIR + "/deepmind/glimp_common.c", + ] + glob( + [ + CODE_DIR + "/botlib/*.c", + CODE_DIR + "/client/cl_*.c", + CODE_DIR + "/client/snd_*.c", + CODE_DIR + "/qcommon/*.c", + CODE_DIR + "/renderercommon/*.c", + CODE_DIR + "/renderergl1/*.c", + CODE_DIR + "/server/*.c", + ], + exclude = [ + CODE_DIR + "/renderergl1/tr_subs.c", + CODE_DIR + "/server/sv_rankings.c", + CODE_DIR + "/qcommon/vm_none.c", + CODE_DIR + "/qcommon/vm_powerpc*.c", + CODE_DIR + "/qcommon/vm_sparc.c", + ], + ), + hdrs = [ + "public/dmlab.h", + CODE_DIR + "/deepmind/context.h", + ] + glob( + [ + CODE_DIR + "/botlib/*.h", + CODE_DIR + "/client/*.h", + CODE_DIR + "/qcommon/*.h", + CODE_DIR + "/sys/*.h", + CODE_DIR + "/server/*.h", + CODE_DIR + "/renderercommon/*.h", + CODE_DIR + "/renderergl1/*.h", + ], + exclude = [ + CODE_DIR + "/client/fx_*.h", + CODE_DIR + "/qcommon/vm_powerpc_asm.h", + CODE_DIR + "/qcommon/vm_sparc.h", + CODE_DIR + "/renderercommon/tr_types.h", + CODE_DIR + "/renderercommon/tr_public.h", + CODE_DIR + "/renderergl1/tr_local.h", + ], + ), + copts = [ + "-std=c99", + "-fno-strict-aliasing", + ARCH_VAR, + STANDALONE_VAR, + ], + defines = [ + "BOTLIB", + "_GNU_SOURCE", + ], + textual_hdrs = [ + CODE_DIR + "/renderercommon/tr_types.h", + CODE_DIR + "/renderercommon/tr_public.h", + CODE_DIR + "/renderergl1/tr_local.h", + ], + deps = [ + ":qcommon_hdrs", + "//deepmind/include:context_headers", + "//third_party/rl_api:env_c_api", + "@jpeg_archive//:jpeg", + "@sdl_system//:sdl2", + ], +) + ASSETS = [ "assets/default.cfg", "assets/q3config.cfg", @@ -912,15 +1035,22 @@ cc_binary( ":vm_pk3", ], linkopts = [ - "-lGL", "-lm", - ], + ] + select({ + ":darwin": ["-framework OpenGL"], + ":darwin_x86_64": ["-framework OpenGL"], + "//conditions:default": ["-lGL"], + }), deps = [ ":game_lib_sdl", "//deepmind/engine:callbacks", "//deepmind/engine:context", "@zlib_archive//:zlib", - ], + ] + select({ + ":darwin": [":macos_dlg_lib"], + ":darwin_x86_64": [":macos_dlg_lib"], + "//conditions:default": [], + }), ) config_setting( @@ -928,45 +1058,101 @@ config_setting( values = {"define": "headless=false"}, ) +config_setting( + name = "dmlab_lib_sdl_darwin", + values = {"define": "headless=false", "cpu": "darwin"}, +) + +config_setting( + name = "dmlab_lib_sdl_darwin_x86_64", + values = {"define": "headless=false", "cpu": "darwin_x86_64"}, +) + config_setting( name = "dmlab_headless_hw", values = {"define": "headless=glx"}, ) +config_setting( + name = "dmlab_headless_hw_darwin", + values = {"define": "headless=macos", "cpu": "darwin"}, +) + +config_setting( + name = "dmlab_headless_hw_darwin_x86_64", + values = {"define": "headless=macos", "cpu": "darwin_x86_64"}, +) + config_setting( name = "dmlab_headless_sw", values = {"define": "headless=osmesa"}, ) +config_setting( + name = "dmlab_headless_sw_darwin", + values = {"define": "headless=osmesa", "cpu": "darwin"}, +) + +config_setting( + name = "dmlab_headless_sw_darwin_x86_64", + values = {"define": "headless=osmesa", "cpu": "darwin_x86_64"}, +) + cc_binary( name = "libdmlab.so", linkopts = select({ "//conditions:default": ["-lOSMesa"], + ":dmlab_headless_hw_darwin": [ + "-framework OpenGL", + ], + ":dmlab_headless_hw_darwin_x86_64": [ + "-framework OpenGL", + ], ":dmlab_headless_hw": [ "-lGL", "-lX11", ], + ":dmlab_lib_sdl_darwin": [ + "-framework OpenGL", + ], + ":dmlab_lib_sdl_darwin_x86_64": [ + "-framework OpenGL", + ], ":dmlab_lib_sdl": [ "-lGL", "-lX11", ], - }) + [ - "-Wl,--version-script", - ":dmlab.lds", - ], + ":dmlab_headless_sw_darwin": [], + ":dmlab_headless_sw_darwin_x86_64": [], + }) + select({ + "//conditions:default": [ + "-Wl,--version-script", + ":dmlab.lds", + ], + ":darwin": [], + ":darwin_x86_64": [], + }), linkshared = 1, linkstatic = 1, deps = select({ "//conditions:default": [":game_lib_headless_osmesa"], ":dmlab_lib_sdl": [":game_lib_sdl"], ":dmlab_headless_hw": [":game_lib_headless_glx"], + ":dmlab_headless_hw_darwin": [":game_lib_headless_darwin"], + ":dmlab_headless_hw_darwin_x86_64": [":game_lib_headless_darwin"], ":dmlab_headless_sw": [":game_lib_headless_osmesa"], + ":dmlab_headless_sw_darwin": [":game_lib_headless_osmesa"], + ":dmlab_headless_sw_darwin_x86_64": [":game_lib_headless_osmesa"], }) + [ ":dmlab.lds", "//deepmind/engine:callbacks", "//deepmind/engine:context", "@zlib_archive//:zlib", - ], + ] + select({ + ":darwin": [":macos_dlg_lib"], + ":darwin_x86_64": [":macos_dlg_lib"], + "//conditions:default": [], + }), ) cc_library( @@ -1007,6 +1193,7 @@ cc_binary( deps = [ ":dmlablib", "@python_system//:python", + # "@numpy_archive//:numpy_headers", ], ) @@ -1038,3 +1225,15 @@ py_test( main = "python/random_agent_test.py", deps = [":random_agent"], ) + +config_setting( + name = "darwin", + values = {"cpu": "darwin"}, + visibility = ["//visibility:public"], +) + +config_setting( + name = "darwin_x86_64", + values = {"cpu": "darwin_x86_64"}, + visibility = ["//visibility:public"], +) diff --git a/WORKSPACE b/WORKSPACE index a15bffdb..265f5c24 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -54,6 +54,14 @@ new_http_archive( url = "http://bazel-mirror.storage.googleapis.com/zlib.net/zlib-1.2.8.tar.gz", ) +new_http_archive( + name = "numpy_archive", + build_file = "numpy.BUILD", + sha256 = "4c6b4eef790528bebb7ec9590d74cc193868940fe68e4109a91c196df72d8094", + strip_prefix = "numpy-1.13.3", + url = "https://github.com/numpy/numpy/releases/download/v1.13.3/numpy-1.13.3.tar.gz", +) + # TODO: Replace with hermetic build new_local_repository( name = "lua_system", @@ -70,3 +78,18 @@ new_local_repository( build_file = "python.BUILD", path = "/usr", ) +new_local_repository( + name = "libxml_system", + build_file = "libxml_system.BUILD", + path = "/usr", +) +new_local_repository( + name = "glib_system", + build_file = "glib_system.BUILD", + path = "/usr", +) +new_local_repository( + name = "osmesa_system", + build_file = "osmesa.BUILD", + path = "/opt", +) diff --git a/deepmind/level_generation/text_level/lua_bindings.cc b/deepmind/level_generation/text_level/lua_bindings.cc index 4b6dc2ad..f33d0a7b 100644 --- a/deepmind/level_generation/text_level/lua_bindings.cc +++ b/deepmind/level_generation/text_level/lua_bindings.cc @@ -86,7 +86,12 @@ bool LuaCustomEntityCallback( lua_pop(L, res.n_results()); return false; } else { - auto is_empty = std::mem_fn(&std::string::empty); + // auto is_empty = std::mem_fn(&std::string::empty); + // NOTE: on macOS clang, the above definition of is_empty causes "ld: 32-bit RIP relative reference out of range" error + // See: + // http://blog.quasardb.net/weird-pie-related-linker-errors-on-os-x/ + // http://www.cocoabuilder.com/archive/xcode/317778-os-constant-cause-32-bit-rip-relative-reference-out-of-range-linker-error-on-x64.html#319148 + auto is_empty = [](std::string& s){ return s.empty(); }; data.erase(std::remove_if(data.begin(), data.end(), is_empty), data.end()); VLOG(1) << "User callback(" << i << ", " << j << ", '" << ent diff --git a/docs/build_macos.md b/docs/build_macos.md new file mode 100644 index 00000000..fb02c14a --- /dev/null +++ b/docs/build_macos.md @@ -0,0 +1,128 @@ + +# Build *DeepMind Lab* for macOS + +For more information refer to [How to build DeepMind Lab](build.md). + +### Prepare build environment + +The following steps has to be done only once per system. + +1. Make sure _Xcode Command Tools_ are installed: + + ```shell + $ xcode-select --install + ``` + +2. Install [Homebrew](https://brew.sh/): + + ```shell + $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" + ``` + +3. Install required packages via _Homebrew_: + + ```shell + $ brew install bazel coreutils sdl2 lua glib libxml2 numpy + ``` + +4. The `ln`, `cp` and `realpath` tools from Homebrew `coreutils` package must be made accessible by Bazel using one of the following approaches: + + 1. Copy symlinks removing `g` prefix in `/usr/local/bin`: + + ```shell + $ sudo cp /usr/local/bin/gln /usr/local/bin/ln + $ sudo cp /usr/local/bin/gcp /usr/local/bin/cp + $ sudo cp /usr/local/bin/grealpath /usr/local/bin/realpath + ``` + + Make sure that sure that `/usr/local/bin` is in `PATH` environment variable and is listed before `/bin`, i.e., + `$ which ln` should output `/usr/local/bin/ln`. If it does not, you may need to update your `~/.profile`: + + ```shell + $ echo 'export PATH=/usr/local/bin:$PATH' >> ~/.profile + ``` + + and re-open your terminal window. + + 2. Export `PATH` variable for each build and run session: + + ```shell + export PATH=/usr/local/opt/coreutils/libexec/gnubin:$PATH + ``` + + **NOTE:** if you choose this approach, remember to execute the above command once per each terminal session used to build and run this project + or you may add this line at the end of your `~/.profile` file for a permanent solution, + however, in this case all tools from `coreutils` package will override the native system tools, which is not recommended. + + **NOTE:** because of a _Bazel_ [bug](https://github.com/bazelbuild/bazel/issues/4008) the `--action_env PATH=...` argument will not work for this. + +5. For convenience you may want to make _macOS_ as the default target platform, + you can do this by putting following lines in your `~/.bazelrc` or `tools/bazel.rc` at the project level: + + ``` + build --apple_platform_type=macos + run --apple_platform_type=macos + ``` + + **NOTE:** if you decide not to put these lines in the `~/.bazelrc` or `tools/bazel.rc`, + you will need to add `--apple_platform_type=macos` argument + to each `run` and `build` invocation of _Bazel_ for this project. + + Additionally you could add `--cpu=darwin` or `--cpu=darwin_x86_64` argument, + but it should be autodetected when building under _macOS_. + +6. Optionally, for headless software rendering support you need to install _[OSMesa](https://www.mesa3d.org/)_, + unfortunately there are no binary packages available, therefore you will need to build it from source. + To facilitate building process of _OSMesa_, you can try the installation script from + [devernay/osmesa-install](https://github.com/devernay/osmesa-install), but first you need to prepare target directories: + + ```shell + $ sudo mkdir /opt/osmesa /opt/llvm + $ sudo chown $USER:staff /opt/osmesa /opt/llvm + ``` + + Then run the script to build and install _OSMesa_: + + ```shell + $ curl https://raw.githubusercontent.com/devernay/osmesa-install/master/osmesa-install.sh \ + | sed -e 's/mangled=1/mangled=0/' osmesa-install.sh | LLVM_BUILD=1 bash + ``` + +### Build and run _DeepMind Lab_ for macOS + +1. [Clone or download *DeepMind Lab*](https://github.com/deepmind/lab). + +2. Build *DeepMind Lab* and run a random agent: + + ```shell + $ cd lab + # Build the Python interface to DeepMind Lab with OpenGL + lab$ bazel build :deepmind_lab.so --define headless=macos + # Rebuild the Python interface in non-headless mode and run a random agent + lab$ bazel run :random_agent --define headless=false + ``` + + The Bazel target `:deepmind_lab.so` builds the Python module that interfaces + *DeepMind Lab*. It can be build in headless hardware rendering mode (`--define + headless=macos` on macOS), headless software rendering mode (`--define + headless=osmesa`) or non-headless mode (`--define headless=false`). + + For more information refer to [How to build DeepMind Lab](build.md). + +### Troubleshooting + +* If got error message about missing `Python.h` header, then probably + the _Xcode Command Line Tools_ are not installed, refer to step 1 of [Prepare build environment](#prepare-build-environment). + +* If got error message about missing `Carbon.h` header, + then you have to add `--apple_platform_type=macos` argument to invocation of _Bazel_ commands, + for convenience you can put it in the `~/.bazelrc` or `tools/bazel.rc`, + refer to step 5 of [Prepare build environment](#prepare-build-environment) for details. + +* If got error messages like `ln: illegal option -- L`, `cp: illegal option -- t` or `/bin/bash: realpath: command not found`, + then _Bazel_ cannot find the _GNU_ `ln`, `cp` or `realpath` tools, + refer to step 4 of [Prepare build environment](#prepare-build-environment) for details. + +* If got error message `ld: library not found for -lOSMesa`, then you probably did not specify `headless` parameter, + depending on what you need, you have to specify one of the following: `--define headless=false`, `--define headless=macos` or + `--define headless=osmesa`, refer to [Build and Run _DeepMind Lab_ for macOS](#build-and-run-deepmind-lab-for-macos) for details. diff --git a/engine/code/deepmind/headless_native_macos_glimp.c b/engine/code/deepmind/headless_native_macos_glimp.c new file mode 100644 index 00000000..b1da4aa1 --- /dev/null +++ b/engine/code/deepmind/headless_native_macos_glimp.c @@ -0,0 +1,80 @@ +#include +#include +#include + +static CGLContextObj context; +//static GLuint framebuffer; +//static GLuint colorRenderbuffer; +//static GLuint depthRenderbuffer; + +void GLimp_MakeCurrent(void) { +} + +void GLimp_Init(void) { + CGLPixelFormatObj pix; + GLint npix; + int attribs[] = { + kCGLPFAAccelerated, // no software rendering + kCGLPFAOpenGLProfile, + kCGLOGLPVersion_Legacy, + 0 + }; + + GLimp_CommonPreInit(); + + // NOTE: in headless mode there is no GUI, hence output to console instead of message boxes + + if (CGLChoosePixelFormat((CGLPixelFormatAttribute *)attribs, &pix, &npix) != kCGLNoError) { + // Sys_Error("GLimp_Init - choose pixel format error!\n"); + printf("GLimp_Init - choose pixel format error!\n"); + exit(1); + } + if (CGLCreateContext(pix, NULL, &context) != kCGLNoError) { + // Sys_Error("GLimp_Init - create context error!\n"); + printf("GLimp_Init - create context error!\n"); + exit(1); + } + if (CGLSetCurrentContext(context) != kCGLNoError) { + // Sys_Error("GLimp_Init - set current context error!"); + printf("GLimp_Init - set current context error!\n"); + exit(1); + } + CGLDestroyPixelFormat(pix); + + printf("Renderer: %s\nVersion: %s\n", glGetString(GL_RENDERER), glGetString(GL_VERSION)); + + /* + glGenFramebuffers(1, &framebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + + glGenRenderbuffers(1, &colorRenderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, glConfig.vidWidth, glConfig.vidHeight); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); + + glGenRenderbuffers(1, &depthRenderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, glConfig.vidWidth, glConfig.vidHeight); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + // Sys_Error("GLimp_Init - framebuffer not ready!"); + printf("GLimp_Init - framebuffer not ready!\n"); + exit(1); + } + */ + + GLimp_CommonPostInit(); +} + +void GLimp_Shutdown(void) { + /* + glBindRenderbuffer(GL_RENDERBUFFER, 0); + glDeleteRenderbuffers(1, &colorRenderbuffer); + glDeleteRenderbuffers(1, &depthRenderbuffer); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glDeleteFramebuffers(1, &framebuffer); + */ + CGLSetCurrentContext(NULL); + CGLDestroyContext(context); +} diff --git a/glib_system.BUILD b/glib_system.BUILD new file mode 100644 index 00000000..706c523e --- /dev/null +++ b/glib_system.BUILD @@ -0,0 +1,28 @@ + +cc_library( + name = "glib", + hdrs = glob([ + # "local/include/glib-2.0/*.h", + # "local/include/glib-2.0/*/*.h", + # "local/include/glib-2.0/*/*/*.h", + # "local/lib/glib-2.0/include/*.h", + # Homebrew + "local/opt/glib/include/glib-2.0/**/*.h", + "local/opt/glib/lib/glib-2.0/include/**/*.h", + ]), + defines = ["_REENTRANT"], + includes = [ + # "local/include/glib-2.0", + # "local/lib/glib-2.0/include", + # Homebrew + "local/opt/glib/include/glib-2.0", + "local/opt/glib/lib/glib-2.0/include", + ], + linkopts = [ + "-lglib-2.0", + # "-L/usr/local/lib", + # Homebrew + "-L/usr/local/opt/glib/lib", + ], + visibility = ["//visibility:public"], +) diff --git a/libxml_system.BUILD b/libxml_system.BUILD new file mode 100644 index 00000000..ad93806f --- /dev/null +++ b/libxml_system.BUILD @@ -0,0 +1,15 @@ + +cc_library( + name = "libxml", + hdrs = glob([ + # Homebrew + "local/opt/libxml2/include/libxml2/libxml/*.h" + ]), + defines = ["_REENTRANT"], + includes = [ + # Homebrew + "local/opt/libxml2/include/libxml2" + ], + linkopts = ["-lxml2"], + visibility = ["//visibility:public"], +) diff --git a/lua.BUILD b/lua.BUILD index e7aa4d25..f4c1a279 100644 --- a/lua.BUILD +++ b/lua.BUILD @@ -6,8 +6,32 @@ cc_library( name = "lua", - hdrs = glob(["include/lua5.1/*.h"]), - includes = ["include/lua5.1"], - linkopts = ["-llua5.1"], + hdrs = glob([ + "include/lua5.1/*.h", + # "local/include/lua5.2/*.h", + # Homebrew + "local/opt/lua/include/*.h", + ]), + includes = [ + "include/lua5.1", + # "local/include/lua5.2", + # Homebrew + "local/opt/lua/include", + ], + linkopts = select({ + "@//:darwin": [ + "-llua.5.2", + # Homebrew + "-L/usr/local/opt/lua/lib", + ], + "@//:darwin_x86_64": [ + "-llua.5.2", + # Homebrew + "-L/usr/local/opt/lua/lib", + ], + "//conditions:default": [ + "-llua5.1", + ], + }), visibility = ["//visibility:public"], ) diff --git a/numpy.BUILD b/numpy.BUILD new file mode 100644 index 00000000..c7e91615 --- /dev/null +++ b/numpy.BUILD @@ -0,0 +1,11 @@ + +cc_library( + name = "numpy_headers", + hdrs = glob([ + "numpy/core/include/**/*.h", + ]), + includes = [ + "numpy/core/include", + ], + visibility = ["//visibility:public"], +) diff --git a/osmesa.BUILD b/osmesa.BUILD new file mode 100644 index 00000000..2d3f7a7e --- /dev/null +++ b/osmesa.BUILD @@ -0,0 +1,76 @@ +# Install unmangled OSMesa using script from: https://github.com/devernay/osmesa-install +# curl https://raw.githubusercontent.com/devernay/osmesa-install/master/osmesa-install.sh \ +# | sed -e 's/mangled=1/mangled=0/' osmesa-install.sh | LLVM_BUILD=1 bash + +cc_library( + name = "osmesa", + hdrs = glob([ + "osmesa/include/**/*.h", + ]), + includes = [ + "osmesa/include", + ], + linkopts = [ + "-lOSMesa32", + "-lGLU", + + "-lLLVMAnalysis", + "-lLLVMAsmParser", + "-lLLVMAsmPrinter", + "-lLLVMBitReader", + "-lLLVMBitWriter", + "-lLLVMCodeGen", + "-lLLVMCore", + "-lLLVMCoroutines", + "-lLLVMCoverage", + "-lLLVMDebugInfoCodeView", + "-lLLVMDebugInfoDWARF", + "-lLLVMDebugInfoMSF", + "-lLLVMDebugInfoPDB", + "-lLLVMDemangle", + "-lLLVMExecutionEngine", + "-lLLVMGlobalISel", + "-lLLVMIRReader", + "-lLLVMInstCombine", + "-lLLVMInstrumentation", + "-lLLVMInterpreter", + "-lLLVMLTO", + "-lLLVMLibDriver", + "-lLLVMLineEditor", + "-lLLVMLinker", + "-lLLVMMC", + "-lLLVMMCDisassembler", + "-lLLVMMCJIT", + "-lLLVMMCParser", + "-lLLVMMIRParser", + "-lLLVMObjCARCOpts", + "-lLLVMObject", + "-lLLVMObjectYAML", + "-lLLVMOption", + "-lLLVMOrcJIT", + "-lLLVMPasses", + "-lLLVMProfileData", + "-lLLVMRuntimeDyld", + "-lLLVMScalarOpts", + "-lLLVMSelectionDAG", + "-lLLVMSupport", + "-lLLVMSymbolize", + "-lLLVMTableGen", + "-lLLVMTarget", + "-lLLVMTransformUtils", + "-lLLVMVectorize", + "-lLLVMX86AsmParser", + "-lLLVMX86AsmPrinter", + "-lLLVMX86CodeGen", + "-lLLVMX86Desc", + "-lLLVMX86Disassembler", + "-lLLVMX86Info", + "-lLLVMX86Utils", + "-lLLVMXRay", + "-lLLVMipo", + + "-L/opt/osmesa/lib", + "-L/opt/llvm/lib", + ], + visibility = ["//visibility:public"], +) diff --git a/public/dmlab_so_loader.cc b/public/dmlab_so_loader.cc index 3289b754..303523d9 100644 --- a/public/dmlab_so_loader.cc +++ b/public/dmlab_so_loader.cc @@ -24,7 +24,9 @@ #include #include +#ifndef __APPLE__ #include +#endif #include #include #include @@ -42,6 +44,44 @@ #error Must define DMLAB_SO_LOCATION dynamic library path. #endif +#ifdef __APPLE__ +ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count) +{ + const int buf_size = 8192; // 8KB + char buf[buf_size]; + size_t rd, wr, total = 0; + off_t ofs; + if (offset) { + ofs = lseek(in_fd, 0, SEEK_CUR); + if (ofs == -1 || lseek(in_fd, *offset, SEEK_SET) == -1) + return -1; + } + while (count > 0) { + rd = read(in_fd, buf, sizeof(buf) <= count ? sizeof(buf) : count); + if (rd == -1) + return -1; + if (rd == 0) + break; + char *p = buf; + while(rd > 0) { + wr = write(out_fd, p, rd); + if (wr == -1) + return -1; + rd -= wr; + p += wr; + count -= wr; + total += wr; + } + } + if (offset) { + *offset = lseek(in_fd, 0, SEEK_CUR); + if (*offset == -1 || lseek(in_fd, ofs, SEEK_SET) == -1) + return -1; + } + return total; +} +#endif + namespace { std::mutex connect_mutex; diff --git a/python.BUILD b/python.BUILD index f0b3f9a6..fd7c9798 100644 --- a/python.BUILD +++ b/python.BUILD @@ -5,7 +5,19 @@ cc_library( name = "python", - hdrs = glob(["include/python2.7/*.h"]), - includes = ["include/python2.7"], + hdrs = glob([ + "include/python2.7/*.h", + # numpy + "local/lib/python2.7/site-packages/numpy/core/include/**/*.h", + # Homebrew numpy + "local/opt/numpy/lib/python2.7/site-packages/numpy/core/include/**/*.h", + ]), + includes = [ + "include/python2.7", + # numpy + "local/lib/python2.7/site-packages/numpy/core/include", + # Homebrew numpy + "local/opt/numpy/lib/python2.7/site-packages/numpy/core/include", + ], visibility = ["//visibility:public"], ) diff --git a/q3map2/BUILD b/q3map2/BUILD index 15f9cf2b..fc608478 100644 --- a/q3map2/BUILD +++ b/q3map2/BUILD @@ -78,7 +78,10 @@ cc_binary( "-DMD4_Final=MD4Final", "-DMD4_Init=MD4Init", "-DMD4_Update=MD4Update", - "-DMD5(data,len,out)=\"{ MD5_CTX ctx; MD5Init(&ctx); MD5Update(&ctx, data, len); MD5Final(out, &ctx); }\"", + #"-DMD5(data,len,out)=\"{ MD5_CTX ctx; MD5Init(&ctx); MD5Update(&ctx, data, len); MD5Final(out, &ctx); }\"", + # NOTE: Bazel's clang_wrapper does not handle arguments with spaces correctly, + # see Bazel issue #3979: https://github.com/bazelbuild/bazel/issues/3979 + "-DMD5(data,len,out)={MD5_CTX(ctx);MD5Init(&ctx);MD5Update(&ctx,data,len);MD5Final(out,&ctx);}", # workaround ], visibility = [ "//:__pkg__", @@ -91,9 +94,21 @@ cc_binary( ":picomodel", "//third_party/md:md4", "//third_party/md:md5", - "@glib_archive//:glib", + ] + select({ + "@//:darwin": [ + "@glib_system//:glib", + "@libxml_system//:libxml", + ], + "@//:darwin_x86_64": [ + "@glib_system//:glib", + "@libxml_system//:libxml", + ], + "//conditions:default": [ + "@glib_archive//:glib", + "@libxml_archive//:libxml", + ], + }) + [ "@jpeg_archive//:jpeg", - "@libxml_archive//:libxml", "@png_archive//:png", ], ) diff --git a/sdl.BUILD b/sdl.BUILD index 89a70f34..f27c2b44 100644 --- a/sdl.BUILD +++ b/sdl.BUILD @@ -4,9 +4,23 @@ cc_library( name = "sdl2", - hdrs = glob(["include/SDL2/*.h"]), + hdrs = glob([ + "include/SDL2/*.h", + "local/include/SDL2/*.h", + # Homebrew + "local/opt/sdl2/include/SDL2/*.h", + ]), defines = ["_REENTRANT"], - includes = ["include/SDL2"], - linkopts = ["-lSDL2"], + includes = [ + "include/SDL2", + "local/include/SDL2", + # Homebrew + "local/opt/sdl2/include/SDL2", + ], + linkopts = [ + "-lSDL2", + # Homebrew + "-L/usr/local/opt/sdl2/lib", + ], visibility = ["//visibility:public"], )