nicholasjng / nanobind-bazel

Bazel defs and rules for building Python projects with nanobind extensions.
Apache License 2.0
6 stars 0 forks source link

Add nanobind domain setting to `nanobind_extension` #17

Closed nicholasjng closed 3 months ago

nicholasjng commented 3 months ago

Mirrors the CMake NB_DOMAIN setting, resulting in an extension-scoped -DNB_DOMAIN=$DOMAIN define value. Hence, it should appear in local_defines.

nicholasjng commented 3 months ago

Capturing the compile commands with NB_DOMAIN mydomain in nanobind_example's CMakeLists.txt:

{
  "directory": "/Users/nicholasjunge/Workspaces/c++/nanobind_example/build/cp312-abi3-macosx_14_0_arm64",
  "command": "/Library/Developer/CommandLineTools/usr/bin/c++ -DNB_DOMAIN=mydomain -DPy_LIMITED_API=0x030C0000 -Dnanobind_example_ext_EXPORTS -I/opt/homebrew/opt/python@3.12/Frameworks/Python.framework/Versions/3.12/include/python3.12 -I/private/var/folders/rw/mj164vm16k10x_byww6my1s00000gn/T/build-env-adxpqkkr/lib/python3.12/site-packages/nanobind/include -O3 -DNDEBUG -std=gnu++17 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk -fPIC -fvisibility=hidden -fno-stack-protector -Os -o CMakeFiles/nanobind_example_ext.dir/src/nanobind_example_ext.cpp.o -c /Users/nicholasjunge/Workspaces/c++/nanobind_example/src/nanobind_example_ext.cpp",
  "file": "/Users/nicholasjunge/Workspaces/c++/nanobind_example/src/nanobind_example_ext.cpp",
  "output": "CMakeFiles/nanobind_example_ext.dir/src/nanobind_example_ext.cpp.o"
},

Setting the domain in the extension directly means that we can build multiple extensions, each with a different domain, in Bazel directly, which amounts to the same functionality known from CMake.

nicholasjng commented 3 months ago

Bazel's compile command for nanobind_example_ext.o:

  {
    "file": "src/nanobind_example_ext.cpp",
    "arguments": [
      "external/bazel_tools~cc_configure_extension~local_config_cc/cc_wrapper.sh",
      "-xc++",
      "-U_FORTIFY_SOURCE",
      "-fstack-protector",
      "-Wall",
      "-Wthread-safety",
      "-Wself-assign",
      "-Wunused-but-set-parameter",
      "-Wno-free-nonheap-object",
      "-fcolor-diagnostics",
      "-fno-omit-frame-pointer",
      "-g0",
      "-O2",
      "-D_FORTIFY_SOURCE=1",
      "-DNDEBUG",
      "-ffunction-sections",
      "-fdata-sections",
      "-std=c++14",
      "-MD",
      "-MF",
      "bazel-out/darwin_arm64-opt/bin/src/_objs/nanobind_example_ext.so/nanobind_example_ext.d",
      "-frandom-seed=bazel-out/darwin_arm64-opt/bin/src/_objs/nanobind_example_ext.so/nanobind_example_ext.o",
      "-DPy_LIMITED_API=0x030C0000",
      "-DNB_DOMAIN=mydomain",
      "-iquote",
      ".",
      "-iquote",
      "bazel-out/darwin_arm64-opt/bin",
      "-iquote",
      "external/nanobind_bazel~~internal_configure_extension~nanobind",
      "-iquote",
      "bazel-out/darwin_arm64-opt/bin/external/nanobind_bazel~~internal_configure_extension~nanobind",
      "-iquote",
      "external/nanobind_bazel~~internal_configure_extension~robin_map",
      "-iquote",
      "bazel-out/darwin_arm64-opt/bin/external/nanobind_bazel~~internal_configure_extension~robin_map",
      "-iquote",
      "external/rules_python~~python~python_3_12_aarch64-apple-darwin",
      "-iquote",
      "bazel-out/darwin_arm64-opt/bin/external/rules_python~~python~python_3_12_aarch64-apple-darwin",
      "-Ibazel-out/darwin_arm64-opt/bin/external/nanobind_bazel~~internal_configure_extension~robin_map/_virtual_includes/robin_map",
      "-isystem",
      "external/nanobind_bazel~~internal_configure_extension~nanobind/include",
      "-isystem",
      "bazel-out/darwin_arm64-opt/bin/external/nanobind_bazel~~internal_configure_extension~nanobind/include",
      "-isystem",
      "external/rules_python~~python~python_3_12_aarch64-apple-darwin/include",
      "-isystem",
      "bazel-out/darwin_arm64-opt/bin/external/rules_python~~python~python_3_12_aarch64-apple-darwin/include",
      "-isystem",
      "external/rules_python~~python~python_3_12_aarch64-apple-darwin/include/python3.12",
      "-isystem",
      "bazel-out/darwin_arm64-opt/bin/external/rules_python~~python~python_3_12_aarch64-apple-darwin/include/python3.12",
      "-isystem",
      "external/rules_python~~python~python_3_12_aarch64-apple-darwin/include/python3.12m",
      "-isystem",
      "bazel-out/darwin_arm64-opt/bin/external/rules_python~~python~python_3_12_aarch64-apple-darwin/include/python3.12m",
      "-mmacosx-version-min=14.4",
      "-std=c++17",
      "-fexceptions",
      "-fno-strict-aliasing",
      "-no-canonical-prefixes",
      "-Wno-builtin-macro-redefined",
      "-D__DATE__=\"redacted\"",
      "-D__TIMESTAMP__=\"redacted\"",
      "-D__TIME__=\"redacted\"",
      "-c",
      "src/nanobind_example_ext.cpp",
      "-o",
      "bazel-out/darwin_arm64-opt/bin/src/_objs/nanobind_example_ext.so/nanobind_example_ext.o"
    ],
    "directory": "/Users/nicholasjunge/Workspaces/c++/nanobind_example"
  },

Domain looks good, so I think we have what we want. (The include path monstrosity is another story.)