Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bazel: Build pybind module and test #661

Merged
merged 14 commits into from
Feb 24, 2025
51 changes: 37 additions & 14 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@buildifier_prebuilt//:rules.bzl", "buildifier", "buildifier_test")
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
load("@rules_gazebo//gazebo:headers.bzl", "gz_configure_header", "gz_export_header")
load("@rules_license//rules:license.bzl", "license")
load("@rules_python//python:py_test.bzl", "py_test")

package(
default_applicable_licenses = [":license"],
Expand Down Expand Up @@ -76,6 +77,28 @@ cc_library(
],
)

pybind_extension(
name = "gz/math8",
srcs = glob(
include = [
"src/python_pybind11/src/*.cc",
"src/python_pybind11/src/*.hh",
],
),
defines = [
"BINDINGS_MODULE_NAME=math8",
],
linkstatic = True,
visibility = ["//visibility:private"],
deps = [":gz-math"],
)

py_library(
name = "python",
data = [":gz/math8"],
visibility = ["//visibility:public"],
)

test_sources = glob(
include = [
"src/*_TEST.cc",
Expand All @@ -95,18 +118,18 @@ test_sources = glob(
for src in test_sources
]

buildifier(
name = "buildifier.fix",
exclude_patterns = ["./.git/*"],
lint_mode = "fix",
mode = "fix",
py_test_sources = glob(
include = [
"src/python_pybind11/test/*_TEST.py",
],
)

buildifier_test(
name = "buildifier.test",
exclude_patterns = ["./.git/*"],
lint_mode = "warn",
mode = "diff",
no_sandbox = True,
workspace = "//:MODULE.bazel",
)
[
py_test(
name = src.replace("/", "_").replace(".py", "_py").replace("src_python_pybind11_test_", ""),
srcs = [src],
main = src,
deps = [":python"],
)
for src in py_test_sources
]
20 changes: 20 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ bazel_dep(name = "buildifier_prebuilt", version = "7.3.1")
bazel_dep(name = "googletest", version = "1.14.0")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "eigen", version = "3.4.0")
bazel_dep(name = "pybind11_bazel", version = "2.13.6")
bazel_dep(name = "rules_python", version = "0.36.0")
bazel_dep(name = "platforms", version = "0.0.11")

# Gazebo Dependencies
bazel_dep(name = "rules_gazebo", version = "0.0.2")
Expand All @@ -17,3 +20,20 @@ archive_override(
strip_prefix = "gz-utils-gz-utils3",
urls = ["https://github.com/gazebosim/gz-utils/archive/refs/heads/gz-utils3.tar.gz"],
)

PYTHON_VERSIONS = [
"3.9",
"3.10",
"3.11",
"3.12",
]

python = use_extension("@rules_python//python/extensions:python.bzl", "python")

[
python.toolchain(
is_default = python_version == PYTHON_VERSIONS[-1],
python_version = python_version,
)
for python_version in PYTHON_VERSIONS
]
13 changes: 13 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ example_sources = glob(
"//:gz-math",
],
) for src in example_sources]

example_sources_py = glob(
include = [
"*.py",
],
)

[py_binary(
name = src.replace(".py", "_py"),
srcs = [src],
main = src,
deps = ["//:python"],
) for src in example_sources_py]