Skip to content

Commit

Permalink
[bazel] Enable bzlmod in hybrid mode
Browse files Browse the repository at this point in the history
This commit adds a `MODULE.bazel` file and enables the bzlmod feature,
but actually continues to use the existing `WORKSPACE` dependency
management by moving to `WORKSPACE.bzlmod`.

Dependencies can be gradually migrated from `WORKSPACE` to
`MODULE.bazel`.

Signed-off-by: James Wainwright <[email protected]>
  • Loading branch information
jwnrt committed Nov 18, 2024
1 parent e88353f commit 19ac6a9
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

# Opt-out of bzlmod for now and continue to use `WORKSPACE`.
common --noenable_bzlmod
# Enable bzlmod using both `MODULE.bazel` and `WORKSPACE.bzlmod`.
common --enable_bzlmod

# https://docs.opentitan.org/doc/rm/c_cpp_coding_style/#cxx-version specifies
build --action_env=BAZEL_CXXOPTS="-std=gnu++14"
Expand Down
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ sw/**/*.md doc
sw/**/*.svg doc
sw/**/*.jpg doc
sw/**/*.png doc

# Generated lock files
MODULE.bazel.lock linguist-generated=true
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package(default_visibility = ["//visibility:public"])

exports_files([
"WORKSPACE",
"WORKSPACE.bzlmod",
"python-requirements.txt",
"tool_requirements.py",
])
Expand Down
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

module(name = "lowrisc_opentitan")
110 changes: 110 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
12 changes: 6 additions & 6 deletions quality/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ licence_test(
Licensed under the Apache License, Version 2.0, see LICENSE for details.
SPDX-License-Identifier: Apache-2.0
""",
workspace = "//:WORKSPACE",
workspace = "//:WORKSPACE.bzlmod",
)

format_exclude = [
Expand All @@ -99,7 +99,7 @@ clang_format_test(
name = "clang_format_check",
exclude_patterns = format_exclude,
mode = "diff",
workspace = "//:WORKSPACE",
workspace = "//:WORKSPACE.bzlmod",
)

clang_format_check(
Expand Down Expand Up @@ -141,7 +141,7 @@ test_suite(
)

buildifier_exclude = [
"./WORKSPACE", # Prevent Buildifier from inserting unnecessary newlines.
"./WORKSPACE.bzlmod", # Prevent Buildifier from inserting unnecessary newlines.
"./**/vendor/**",
"./build/**",
"./third_party/rust/crates/crates.bzl", # Autogenerated by cargo-raze
Expand All @@ -159,7 +159,7 @@ buildifier_test(
mode = "diff",
no_sandbox = True,
verbose = True,
workspace = "//:WORKSPACE",
workspace = "//:WORKSPACE.bzlmod",
)

# TODO(cfrantz): Find a way to keep this list of rust targets synchronized
Expand Down Expand Up @@ -194,7 +194,7 @@ RUST_TARGETS = [
rustfmt_test(
name = "rustfmt_check",
exclude_patterns = format_exclude,
workspace = "//:WORKSPACE",
workspace = "//:WORKSPACE.bzlmod",
)

rustfmt_fix(
Expand All @@ -210,7 +210,7 @@ sh_test(
name = "shellcheck_check",
srcs = ["//util/sh/scripts:run-shellcheck.sh"],
data = [
"//:WORKSPACE",
"//:WORKSPACE.bzlmod",
"@shellcheck",
],
env = {
Expand Down
2 changes: 1 addition & 1 deletion rules/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bare_repository = repository_rule(
doc = "Script to create symlink trees for the `local` case.",
),
"workspace": attr.label(
default = Label("//:WORKSPACE"),
default = Label("//:WORKSPACE.bzlmod"),
allow_single_file = True,
doc = "Root workspace.",
),
Expand Down
2 changes: 1 addition & 1 deletion rules/scripts/clang_format.template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ elif [[ ! -z "${BUILD_WORKSPACE_DIRECTORY+is_set}" ]]; then
cd ${BUILD_WORKSPACE_DIRECTORY} || exit 1
else
echo "Neither WORKSPACE nor BUILD_WORKSPACE_DIRECTORY were set."
echo "If this is a test rule, add 'workspace = \"//:WORKSPACE\"' to your rule."
echo "If this is a test rule, add 'workspace = \"//:WORKSPACE.bzlmod\"' to your rule."
exit 1
fi

Expand Down
2 changes: 1 addition & 1 deletion rules/scripts/rustfmt.template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ elif [[ -n "${BUILD_WORKSPACE_DIRECTORY+is_set}" ]]; then
cd "${BUILD_WORKSPACE_DIRECTORY}" || exit 1
else
echo "Neither WORKSPACE nor BUILD_WORKSPACE_DIRECTORY were set."
echo "If this is a test rule, add 'workspace = \"//:WORKSPACE\"' to your rule."
echo "If this is a test rule, add 'workspace = \"//:WORKSPACE.bzlmod\"' to your rule."
exit 1
fi

Expand Down
8 changes: 4 additions & 4 deletions util/sh/scripts/run-shellcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
set -e

# Escape the sandbox when running under `bazel test`. This script assumes that
# when it's invoked by Bazel, the Bazel target will depend on `//:WORKSPACE`.
# when it's invoked by Bazel, the Bazel target will depend on `//:WORKSPACE.bzlmod`.
# This assumption enables us to infer that we're running in the sandbox when we
# see a symlink named "WORKSPACE".
if [[ -L WORKSPACE ]]; then
# see a symlink named "WORKSPACE.bzlmod".
if [[ -L WORKSPACE.bzlmod ]]; then
SHELLCHECK="$(realpath "${SHELLCHECK}")"
REPO_TOP="$(dirname "$(realpath WORKSPACE)")"
REPO_TOP="$(dirname "$(realpath WORKSPACE.bzlmod)")"
cd "${REPO_TOP}"
else
REPO_TOP="$(git rev-parse --show-toplevel)"
Expand Down

0 comments on commit 19ac6a9

Please sign in to comment.