From 21eb3962e2b77441945bdc412b0f3f8d22cdd3fb Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Thu, 19 Dec 2024 17:11:11 -0800 Subject: [PATCH 1/2] [drake_bazel_external] Port to use MODULE.bazel --- drake_bazel_external/.bazelrc | 4 -- drake_bazel_external/.gitignore | 1 + drake_bazel_external/MODULE.bazel | 42 ++++++++++++++++++++ drake_bazel_external/README.md | 9 +++++ drake_bazel_external/WORKSPACE | 55 --------------------------- drake_bazel_external/apps/BUILD.bazel | 5 ++- drake_bazel_external/environ.bzl | 45 ---------------------- private/test/file_sync_test.py | 5 --- 8 files changed, 56 insertions(+), 110 deletions(-) create mode 100644 drake_bazel_external/MODULE.bazel delete mode 100644 drake_bazel_external/WORKSPACE delete mode 100644 drake_bazel_external/environ.bzl diff --git a/drake_bazel_external/.bazelrc b/drake_bazel_external/.bazelrc index 30f6a273..6bbaf487 100644 --- a/drake_bazel_external/.bazelrc +++ b/drake_bazel_external/.bazelrc @@ -1,9 +1,5 @@ # SPDX-License-Identifier: MIT-0 -# Don't use bzlmod yet. -common --enable_workspace=true -common --enable_bzlmod=false - # Default to an optimized build. build --compilation_mode=opt diff --git a/drake_bazel_external/.gitignore b/drake_bazel_external/.gitignore index ad8d446d..d2409e07 100644 --- a/drake_bazel_external/.gitignore +++ b/drake_bazel_external/.gitignore @@ -2,3 +2,4 @@ /bazel-* /user.bazelrc +/MODULE.bazel.lock diff --git a/drake_bazel_external/MODULE.bazel b/drake_bazel_external/MODULE.bazel new file mode 100644 index 00000000..35fabddf --- /dev/null +++ b/drake_bazel_external/MODULE.bazel @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: MIT-0 + +module(name = "drake_external_examples") + +# Add the Bazel rules we need. +bazel_dep(name = "rules_cc", version = "0.0.17") +bazel_dep(name = "rules_python", version = "0.40.0") + +# Use the host system python. +register_toolchains("@bazel_tools//tools/python:autodetecting_toolchain") + +# Here we introduce Drake as a module dependency, but note that Drake is not +# published to any Bazel registry. Below, we'll override it with a github +# source archive. +bazel_dep(name = "drake") + +# By default, this example always uses the latest Drake master branch. +DRAKE_COMMIT = "master" +DRAKE_CHECKSUM = "" + +# You can choose a specific revision of Drake to use, e.g.: +# DRAKE_COMMIT = "be4f658487f739ba04ec079de46f9459b719636d" +# DRAKE_CHECKSUM = "31ec8f87df3ceb6516de3c33a14c5d59ac5c003b4faf93ac526877d2e150b394" +# +# You can also use DRAKE_COMMIT to choose a Drake release; e.g.: +# DRAKE_COMMIT = "v0.15.0" +# DRAKE_CHECKSUM = "... TBD ..." +# +# Before changing the COMMIT, temporarily uncomment the next line so that Bazel +# displays the suggested new value for the CHECKSUM. +# DRAKE_CHECKSUM = "0" * 64 + +# This declares the `@drake` module as a source code archive from github. +# See README.md for instructions to use a local path, instead. +archive_override( + module_name = "drake", + urls = [x.format(DRAKE_COMMIT) for x in [ + "https://github.com/RobotLocomotion/drake/archive/{}.tar.gz", + ]], + sha256 = DRAKE_CHECKSUM, + strip_prefix = "drake-{}".format(DRAKE_COMMIT.lstrip("v")), +) diff --git a/drake_bazel_external/README.md b/drake_bazel_external/README.md index 86757ab8..68bb6eca 100644 --- a/drake_bazel_external/README.md +++ b/drake_bazel_external/README.md @@ -33,6 +33,15 @@ cause differences. This is important when using tools like `drake::FindResource` / `pydrake.common.FindResource`. You may generally want to stick to using `bazel run` when able. +### Using a local checkout of Drake + +To use Drake sources on disk instead of downloaded from github, pass the flag +``--override_module=drake=/home/user/stuff/drake`` to bazel on the command line +or add a line such as the following to ``user.bazelrc`` in the current directory: +``` +build --override_module=drake=/home/user/stuff/drake +``` + ## Python Versions By default, Python 3 is the Python interpreter that Drake will use when built diff --git a/drake_bazel_external/WORKSPACE b/drake_bazel_external/WORKSPACE deleted file mode 100644 index fd35af05..00000000 --- a/drake_bazel_external/WORKSPACE +++ /dev/null @@ -1,55 +0,0 @@ -# SPDX-License-Identifier: MIT-0 - -workspace(name = "drake_external_examples") - -DRAKE_COMMIT = "master" -DRAKE_CHECKSUM = "" - -# Or choose a specific revision of Drake to use. -# DRAKE_COMMIT = "be4f658487f739ba04ec079de46f9459b719636d" -# DRAKE_CHECKSUM = "31ec8f87df3ceb6516de3c33a14c5d59ac5c003b4faf93ac526877d2e150b394" -# -# You can also use DRAKE_COMMIT to choose a Drake release; eg: -# DRAKE_COMMIT = "v0.15.0" -# -# Before changing the COMMIT, temporarily uncomment the next line so that Bazel -# displays the suggested new value for the CHECKSUM. -# DRAKE_CHECKSUM = "0" * 64 - -# Or to temporarily build against a local checkout of Drake, at the bash prompt -# set an environment variable before building: -# export EXAMPLES_LOCAL_DRAKE_PATH=/home/user/stuff/drake - -# Load an environment variable. -load("//:environ.bzl", "environ_repository") -environ_repository(name = "environ", vars = ["EXAMPLES_LOCAL_DRAKE_PATH"]) -load("@environ//:environ.bzl", EXAMPLES_LOCAL_DRAKE_PATH = "EXAMPLES_LOCAL_DRAKE_PATH") - -# This declares the `@drake` repository as an http_archive from github, -# iff EXAMPLES_LOCAL_DRAKE_PATH is unset. When it is set, this declares a -# `@drake_ignored` package which is never referenced, and thus is ignored. -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -http_archive( - name = "drake" if not EXAMPLES_LOCAL_DRAKE_PATH else "drake_ignored", - urls = [x.format(DRAKE_COMMIT) for x in [ - "https://github.com/RobotLocomotion/drake/archive/{}.tar.gz", - ]], - sha256 = DRAKE_CHECKSUM, - strip_prefix = "drake-{}".format(DRAKE_COMMIT.lstrip("v")), -) - -# This declares the `@drake` repository as a local directory, -# iff EXAMPLES_LOCAL_DRAKE_PATH is set. When it is unset, this declares a -# `@drake_ignored` package which is never referenced, and thus is ignored. -local_repository( - name = "drake" if EXAMPLES_LOCAL_DRAKE_PATH else "drake_ignored", - path = EXAMPLES_LOCAL_DRAKE_PATH, -) -print("Using EXAMPLES_LOCAL_DRAKE_PATH={}".format(EXAMPLES_LOCAL_DRAKE_PATH)) if EXAMPLES_LOCAL_DRAKE_PATH else None # noqa - -# Reference external software libraries, tools, and toolchains per Drake's -# defaults. Some software will come from the host system (Ubuntu or macOS); -# other software will be downloaded in source or binary form from GitHub or -# other sites. -load("@drake//tools/workspace:default.bzl", "add_default_workspace") -add_default_workspace() diff --git a/drake_bazel_external/apps/BUILD.bazel b/drake_bazel_external/apps/BUILD.bazel index 6bdbed24..80df5da1 100644 --- a/drake_bazel_external/apps/BUILD.bazel +++ b/drake_bazel_external/apps/BUILD.bazel @@ -1,6 +1,7 @@ # SPDX-License-Identifier: MIT-0 -load("@drake//tools/skylark:py.bzl", "py_binary", "py_test") +load("@rules_cc//cc:defs.bzl", "cc_binary") +load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") load("@drake//tools/skylark:pybind.bzl", "pybind_py_library") # Compile a sample application. @@ -79,6 +80,7 @@ cc_test( pybind_py_library( name = "simple_adder_py", + cc_binary_rule = cc_binary, cc_so_name = "simple_adder", cc_srcs = ["simple_adder_py.cc"], cc_deps = [ @@ -88,6 +90,7 @@ pybind_py_library( ], py_deps = ["@drake//bindings/pydrake"], py_imports = ["."], + py_library_rule = py_library, ) # Mimic the C++ test in Python to show the bindings. diff --git a/drake_bazel_external/environ.bzl b/drake_bazel_external/environ.bzl deleted file mode 100644 index 1403d12b..00000000 --- a/drake_bazel_external/environ.bzl +++ /dev/null @@ -1,45 +0,0 @@ -# SPDX-License-Identifier: MIT-0 - -# Write out a repository that contains: -# - An empty BUILD file, to define a package. -# - An environ.bzl file with variable assignments for each ENV_NAMES item. -def _impl(repository_ctx): - vars = repository_ctx.attr.vars - bzl_content = [] - for key in vars: - value = repository_ctx.os.environ.get(key, "") - bzl_content.append("{}='{}'\n".format(key, value)) - repository_ctx.file( - "BUILD.bazel", - content = "\n", - executable = False, - ) - repository_ctx.file( - "environ.bzl", - content = "".join(bzl_content), - executable = False, - ) - -_string_list = attr.string_list() - -def environ_repository(name = None, vars = []): - """Provide specific environment variables for use in a WORKSPACE file. - The `vars` are the environment variables to provide. - - Example: - environ_repository(name = "foo", vars = ["BAR", "BAZ"]) - load("@foo//:environ.bzl", "BAR", "BAZ") - print(BAR) - """ - rule = repository_rule( - implementation = _impl, - attrs = { - "vars": _string_list, - }, - local = True, - environ = vars, - ) - rule( - name = name, - vars = vars, - ) diff --git a/private/test/file_sync_test.py b/private/test/file_sync_test.py index 343bcf74..b5e18260 100755 --- a/private/test/file_sync_test.py +++ b/private/test/file_sync_test.py @@ -73,17 +73,13 @@ for path in [ ".bazelignore", ".bazelproject", - ".bazelrc", ".clang-format", ".github/ci_build_test", ".github/setup", ".github/ubuntu_setup", - ".gitignore", "BUILD.bazel", "CPPLINT.cfg", "LICENSE", - "WORKSPACE", - "apps/BUILD.bazel", "apps/exec.sh", "apps/find_resource_test.py", "apps/import_all_test.py", @@ -95,7 +91,6 @@ "apps/simple_adder_test.cc", "apps/simple_continuous_time_system.cc", "apps/simple_logging_example.py", - "environ.bzl", "setup/install_prereqs", ] ]) From 0d842b266ac6867ec6c0f3dd37422f9b27e9dfba Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Thu, 19 Dec 2024 17:12:03 -0800 Subject: [PATCH 2/2] DNM Use Drake PR --- drake_bazel_external/MODULE.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drake_bazel_external/MODULE.bazel b/drake_bazel_external/MODULE.bazel index 35fabddf..7a4a126b 100644 --- a/drake_bazel_external/MODULE.bazel +++ b/drake_bazel_external/MODULE.bazel @@ -15,7 +15,7 @@ register_toolchains("@bazel_tools//tools/python:autodetecting_toolchain") bazel_dep(name = "drake") # By default, this example always uses the latest Drake master branch. -DRAKE_COMMIT = "master" +DRAKE_COMMIT = "bzlmod-extension" DRAKE_CHECKSUM = "" # You can choose a specific revision of Drake to use, e.g.: @@ -35,7 +35,7 @@ DRAKE_CHECKSUM = "" archive_override( module_name = "drake", urls = [x.format(DRAKE_COMMIT) for x in [ - "https://github.com/RobotLocomotion/drake/archive/{}.tar.gz", + "https://github.com/jwnimmer-tri/drake/archive/{}.tar.gz", ]], sha256 = DRAKE_CHECKSUM, strip_prefix = "drake-{}".format(DRAKE_COMMIT.lstrip("v")),