Turn `@pybind11` into an internal dependency. Fixes #71.
diff --git a/MODULE.bazel b/MODULE.bazel index 08caa0f..66b5e0b 100644 --- a/MODULE.bazel +++ b/MODULE.bazel
@@ -6,4 +6,7 @@ bazel_dep(name = "platforms", version = "0.0.8") bazel_dep(name = "rules_cc", version = "0.0.9") -bazel_dep(name = "rules_python", version = "0.30.0") +bazel_dep(name = "rules_python", version = "0.31.0") + +internal_configure = use_extension("//:internal_configure.bzl", "internal_configure_extension") +use_repo(internal_configure, "pybind11")
diff --git a/README.md b/README.md index db18abb..3dfe5f3 100644 --- a/README.md +++ b/README.md
@@ -12,23 +12,10 @@ arguments match a `cc_test`. To test a `pybind_extension`, the most common approach is to write the test in -python and use the standard `py_test` build rule. +Python and use the standard `py_test` build rule. -Provided targets: - -- `@pybind11//:pybind11_embed`: Automatically adds required build flags to - embed Python. Add as a dependency to your `cc_binary`. - - `@pybind11//:pybind11_embed` currently supports Python 3 MacOS/Ubuntu/Debian - environments: - - - pyenv - - pipenv - - virtualenv - - If `pybind11_embed` doesn't work with your embedded Python project, add - `@pybind11` as a dependency to your `cc_binary` and - [follow the instructions for manually retrieving the build flags](https://docs.python.org/3/extending/embedding.html#embedding-python-in-c). +To embed Python, add `@rules_python//python/cc:current_py_cc_libs` as a +dependency to your `cc_binary`. ## Installation @@ -37,15 +24,15 @@ ```starlark http_archive( name = "pybind11_bazel", - strip_prefix = "pybind11_bazel-<stable-commit>", - urls = ["https://github.com/pybind/pybind11_bazel/archive/<stable-commit>.zip"], + strip_prefix = "pybind11_bazel-<version>", + urls = ["https://github.com/pybind/pybind11_bazel/archive/v<version>.zip"], ) # We still require the pybind library. http_archive( name = "pybind11", build_file = "@pybind11_bazel//:pybind11.BUILD", - strip_prefix = "pybind11-<stable-version>", - urls = ["https://github.com/pybind/pybind11/archive/v<stable-version>.zip"], + strip_prefix = "pybind11-<version>", + urls = ["https://github.com/pybind/pybind11/archive/v<version>.zip"], ) ``` @@ -60,8 +47,7 @@ In your `MODULE.bazel` file: ```starlark -pybind11_configure = use_extension("@pybind11_bazel//:pybind11_configure.bzl", "pybind11_configure_extension") -use_repo(pybind11_configure, "pybind11") +bazel_dep(name = "pybind11_bazel", version = "<version>") ``` Usage in your `BUILD` file is as described previously.
diff --git a/build_defs.bzl b/build_defs.bzl index 59063bf..ee34502 100644 --- a/build_defs.bzl +++ b/build_defs.bzl
@@ -9,10 +9,8 @@ pass PYBIND_COPTS = select({ - "@pybind11//:msvc_compiler": [], - "//conditions:default": [ - "-fexceptions", - ], + Label("@pybind11//:msvc_compiler"): [], + "//conditions:default": ["-fexceptions"], }) PYBIND_FEATURES = [ @@ -21,7 +19,7 @@ ] PYBIND_DEPS = [ - "@pybind11", + Label("@pybind11//:pybind11"), "@rules_python//python/cc:current_py_cc_headers", ] @@ -42,15 +40,13 @@ native.cc_binary( name = name + ".so", copts = copts + PYBIND_COPTS + select({ - "@pybind11//:msvc_compiler": [], - "//conditions:default": [ - "-fvisibility=hidden", - ], + Label("@pybind11//:msvc_compiler"): [], + "//conditions:default": ["-fvisibility=hidden"], }), features = features + PYBIND_FEATURES, linkopts = linkopts + select({ - "@pybind11//:msvc_compiler": [], - "@pybind11//:osx": ["-undefined", "dynamic_lookup"], + "@platforms//os:osx": ["-undefined", "dynamic_lookup"], + Label("@pybind11//:msvc_compiler"): [], "//conditions:default": ["-Wl,-Bsymbolic"], }), linkshared = 1,
diff --git a/pybind11_configure.bzl b/internal_configure.bzl similarity index 81% rename from pybind11_configure.bzl rename to internal_configure.bzl index 931f30b..a6b15e1 100644 --- a/pybind11_configure.bzl +++ b/internal_configure.bzl
@@ -1,4 +1,4 @@ -"""Module extension for "configuring" pybind11.""" +"""Module extension for "configuring" pybind11_bazel.""" load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") @@ -11,7 +11,7 @@ fail("Failed to parse my own version from `MODULE.bazel`! " + "This should never happen!") -def _pybind11_configure_extension_impl(module_ctx): +def _internal_configure_extension_impl(module_ctx): version = _parse_my_own_version_from_module_dot_bazel(module_ctx) # The pybind11_bazel version should typically just be the pybind11 version, @@ -24,4 +24,4 @@ urls = ["https://github.com/pybind/pybind11/archive/v%s.zip" % version], ) -pybind11_configure_extension = module_extension(implementation = _pybind11_configure_extension_impl) +internal_configure_extension = module_extension(implementation = _internal_configure_extension_impl)
diff --git a/pybind11.BUILD b/pybind11.BUILD index 35dd4a0..5908ce1 100644 --- a/pybind11.BUILD +++ b/pybind11.BUILD
@@ -1,60 +1,38 @@ # pybind11 - Seamless operability between C++11 and Python. load("@rules_cc//cc:defs.bzl", "cc_library") -package(default_visibility = ["//visibility:public"]) - licenses(["notice"]) exports_files(["LICENSE"]) -OPTIONS = select({ - ":msvc_compiler": [], - "//conditions:default": [ - "-fexceptions", - # Useless warnings - "-Xclang-only=-Wno-undefined-inline", - "-Xclang-only=-Wno-pragma-once-outside-header", - "-Xgcc-only=-Wno-error", # no way to just disable the pragma-once warning in gcc - ], -}) - -INCLUDES = [ - "include/pybind11/**/*.h", -] - -EXCLUDES = [ - # Deprecated file that just emits a warning - "include/pybind11/common.h", -] - cc_library( name = "pybind11", hdrs = glob( - INCLUDES, - exclude = EXCLUDES, + include = [ + "include/pybind11/**/*.h", + ], + exclude = [ + # Deprecated file that just emits a warning + "include/pybind11/common.h", + ], ), - copts = OPTIONS, + copts = select({ + ":msvc_compiler": [], + "//conditions:default": [ + "-fexceptions", + # Useless warnings + "-Xclang-only=-Wno-undefined-inline", + "-Xclang-only=-Wno-pragma-once-outside-header", + "-Xgcc-only=-Wno-error", # no way to just disable the pragma-once warning in gcc + ], + }), includes = ["include"], + visibility = ["//visibility:public"], deps = ["@rules_python//python/cc:current_py_cc_headers"], ) -cc_library( - name = "pybind11_embed", - hdrs = glob( - INCLUDES, - exclude = EXCLUDES, - ), - copts = OPTIONS, - includes = ["include"], - deps = ["@rules_python//python/cc:current_py_cc_libs"], -) - config_setting( name = "msvc_compiler", flag_values = {"@bazel_tools//tools/cpp:compiler": "msvc-cl"}, -) - -config_setting( - name = "osx", - constraint_values = ["@platforms//os:osx"], + visibility = ["//visibility:public"], )