From d7feb1a8f56445e6ad421fbd9d2877989a3f8234 Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Wed, 8 Jan 2025 15:56:36 -0800 Subject: [PATCH] [drake_bazel_external] Port to use MODULE.bazel --- drake_bazel_external/.bazelrc | 4 --- drake_bazel_external/.gitignore | 1 + drake_bazel_external/MODULE.bazel | 53 +++++++++++++++++++++++++++++ drake_bazel_external/README.md | 9 +++++ drake_bazel_external/WORKSPACE | 55 ------------------------------- drake_bazel_external/environ.bzl | 45 ------------------------- private/test/file_sync_test.py | 4 --- 7 files changed, 63 insertions(+), 108 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..40eac740 --- /dev/null +++ b/drake_bazel_external/MODULE.bazel @@ -0,0 +1,53 @@ +# 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") + +# 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 = "19bb4703fdf4950d4b3530d496d8a0ff1ca5fc22" +# DRAKE_CHECKSUM = "489d255db3cb1c8ff7a91ae0f961d76b6083a1d4a3f58150371b4b359683adb6" +# +# 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")), +) + +# Use the host system /usr/bin/python3. +python_repository = use_repo_rule( + "@drake//tools/workspace/python:repository.bzl", + "python_repository", +) + +python_repository( + name = "python", + linux_interpreter_path = "/usr/bin/python3", + requirements_flavor = "build", +) + +register_toolchains("@python//:all") 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/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 d9a99954..85872401 100755 --- a/private/test/file_sync_test.py +++ b/private/test/file_sync_test.py @@ -62,16 +62,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", @@ -84,7 +81,6 @@ "apps/simple_adder_test.cc", "apps/simple_continuous_time_system.cc", "apps/simple_logging_example.py", - "environ.bzl", "setup/install_prereqs", ] ])