Skip to content

Commit dd8e833

Browse files
committed
feat: Start migration to bzlmod
1 parent d1f264f commit dd8e833

8 files changed

Lines changed: 1010 additions & 50 deletions

File tree

.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
common --noenable_bzlmod
1919

20+
common:bzlmod --enable_bzlmod
21+
common:bzlmod --noenable_workspace
22+
2023
build --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
2124
build --incompatible_strict_action_env=true
2225
build --incompatible_enable_cc_toolchain_resolution

MODULE.bazel

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
module(name = "gcc_toolchain", version="0.8.0")
2+
3+
# Dependencies
4+
# ============
5+
bazel_dep(name = "bazel_skylib", version = "1.8.1")
6+
bazel_dep(name = "aspect_bazel_lib", version = "2.20.0")
7+
bazel_dep(name = "platforms", version = "1.0.0")
8+
bazel_dep(name = "rules_cc", version = "0.2.0")
9+
10+
# Local Toolchains
11+
# ================
12+
gcc_toolchains = use_extension("//toolchain:module_extensions.bzl", "gcc_toolchains")
13+
14+
# Unfortunately, we can't load `ARCHS` directly here.
15+
# But the attributes in `gcc_register_toolchain.register_toolchain` are gated to only contain values from ARCHS
16+
all_architectures = [
17+
"aarch64",
18+
"armv7",
19+
"x86_64",
20+
]
21+
[gcc_toolchains.toolchain(
22+
name = "gcc_toolchain_{}".format(arch),
23+
target_arch = arch,
24+
)
25+
for arch in all_architectures]
26+
27+
[use_repo(gcc_toolchains, "gcc_toolchain_{}".format(arch))
28+
for arch in all_architectures]
29+
30+
[register_toolchains(
31+
"@gcc_toolchain_{}//:cc_toolchain".format(arch),
32+
"@gcc_toolchain_{}//:fortran_toolchain".format(arch),
33+
)
34+
for arch in all_architectures]
35+
36+
# Dev Dependencies (for examples/)
37+
# ===============================
38+
bazel_dep(name = "rules_foreign_cc", version = "0.15.0", dev_dependency=True)
39+
bazel_dep(name = "rules_proto", version = "7.1.0", dev_dependency=True)
40+
bazel_dep(name = "protobuf", version = "29.3", dev_dependency=True, repo_name="com_google_protobuf")
41+
single_version_override(
42+
module_name = "protobuf",
43+
patch_strip = 1,
44+
patches = ["//third_party/patches:com_google_protobuf.bzlmod.patch"],
45+
version = "29.3",
46+
)
47+
48+
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
49+
http_archive(
50+
name = "avl",
51+
url = "https://web.mit.edu/drela/Public/web/avl/avl3.40b.tgz",
52+
sha256 = "6d62e563578b79795a84958cfe4e221a4c9847fbeb4a821d45bc049934fc6a90",
53+
strip_prefix = "Avl",
54+
build_file = "//:examples/avl/avl.BUILD.bazel",
55+
)
56+
57+
_ALL_SRCS = """\
58+
filegroup(
59+
name = "srcs",
60+
srcs = glob(
61+
include = ["**"],
62+
exclude = ["**/* *"],
63+
),
64+
visibility = ["//visibility:public"],
65+
)
66+
"""
67+
68+
_LAPAK_PATCH = """\
69+
cat > make.inc <<EOF
70+
####################################################################
71+
# LAPACK make include file. #
72+
####################################################################
73+
74+
SHELL = $0
75+
76+
CC ?=
77+
78+
FC ?=
79+
FFLAGS ?=
80+
FFLAGS_DRV ?=
81+
FFLAGS_NOOPT ?=
82+
83+
LDFLAGS ?=
84+
85+
AR ?=
86+
ARFLAGS = cr
87+
RANLIB = echo
88+
89+
BLASLIB ?=
90+
CBLASLIB ?=
91+
LAPACKLIB ?=
92+
TMGLIB ?=
93+
LAPACKELIB ?=
94+
95+
DOCSDIR ?=
96+
97+
TIMER ?=
98+
EOF
99+
"""
100+
101+
http_archive(
102+
name = "lapack",
103+
url = "https://github.com/Reference-LAPACK/lapack/archive/refs/tags/v3.12.0.tar.gz",
104+
sha256 = "eac9570f8e0ad6f30ce4b963f4f033f0f643e7c3912fc9ee6cd99120675ad48b",
105+
strip_prefix = "lapack-3.12.0",
106+
patch_cmds = [_LAPAK_PATCH],
107+
build_file_content = _ALL_SRCS,
108+
)
109+
110+
http_archive(
111+
name = "openssl",
112+
sha256 = "40dceb51a4f6a5275bde0e6bf20ef4b91bfc32ed57c0552e2e8e15463372b17a",
113+
strip_prefix = "openssl-1.1.1n",
114+
url = "https://www.openssl.org/source/openssl-1.1.1n.tar.gz",
115+
build_file_content = _ALL_SRCS,
116+
)
117+

MODULE.bazel.lock

Lines changed: 688 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/defs.md

Lines changed: 52 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rules_cc/defs.bzl

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,82 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
def cc_library(name, static_libstdcxx = False, **kwargs):
15+
def _with_libstdcxx(rule, name, gcc_toolchain_prefix = "gcc_toolchain", static_libstdcxx = False, **kwargs):
16+
target_name = "libstdcxx{}".format("_static" if static_libstdcxx else "")
1617
deps = kwargs.pop("deps", [])
1718
deps += select({
18-
"@platforms//cpu:aarch64": ["@gcc_toolchain_aarch64//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
19-
"@platforms//cpu:armv7": ["@gcc_toolchain_armv7//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
20-
"@platforms//cpu:x86_64": ["@gcc_toolchain_x86_64//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
19+
"@platforms//cpu:aarch64": ["{}_aarch64//:{}".format(gcc_toolchain_prefix, target_name)],
20+
"@platforms//cpu:armv7": ["{}_armv7//:{}".format(gcc_toolchain_prefix, target_name)],
21+
"@platforms//cpu:x86_64": ["{}_x86_64//:{}".format(gcc_toolchain_prefix, target_name)],
2122
})
22-
native.cc_library(
23+
rule(
2324
name = name,
2425
deps = deps,
2526
**kwargs
2627
)
2728

29+
def cc_library(name, static_libstdcxx = False, **kwargs):
30+
_with_libstdcxx(
31+
rule = native.cc_library,
32+
name = name,
33+
gcc_toolchain_prefix = "@gcc_toolchain",
34+
static_libstdcxx = static_libstdcxx,
35+
**kwargs,
36+
)
37+
2838
def cc_binary(name, static_libstdcxx = False, **kwargs):
29-
deps = kwargs.pop("deps", [])
30-
deps += select({
31-
"@platforms//cpu:aarch64": ["@gcc_toolchain_aarch64//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
32-
"@platforms//cpu:armv7": ["@gcc_toolchain_armv7//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
33-
"@platforms//cpu:x86_64": ["@gcc_toolchain_x86_64//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
34-
})
35-
native.cc_binary(
39+
_with_libstdcxx(
40+
rule = native.cc_binary,
3641
name = name,
37-
deps = deps,
38-
**kwargs
42+
gcc_toolchain_prefix = "@gcc_toolchain",
43+
static_libstdcxx = static_libstdcxx,
44+
**kwargs,
3945
)
4046

4147
def cc_test(name, static_libstdcxx = False, **kwargs):
42-
deps = kwargs.pop("deps", [])
43-
deps += select({
44-
"@platforms//cpu:aarch64": ["@gcc_toolchain_aarch64//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
45-
"@platforms//cpu:armv7": ["@gcc_toolchain_armv7//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
46-
"@platforms//cpu:x86_64": ["@gcc_toolchain_x86_64//:libstdcxx{}".format("_static" if static_libstdcxx else "")],
47-
})
48-
native.cc_test(
48+
_with_libstdcxx(
49+
rule = native.cc_test,
4950
name = name,
50-
deps = deps,
51-
**kwargs
51+
gcc_toolchain_prefix = "@gcc_toolchain",
52+
static_libstdcxx = static_libstdcxx,
53+
**kwargs,
54+
)
55+
56+
# These targets should only be used to patch dependencies of gcc-toolchain that should use our toolchain.
57+
# For instance, we depend on protobuf to run `examples/protobuf`, but we also want protobuf to link against libstdc++, so we patch it to use our version of the targets.
58+
# You may be wondering: Why can't we use the "public" versions of the targets?
59+
# In bzlmod, the repositories `@gcc_toolchain_<arch>` that we create in MODULE.bazel are not visible to our dependencies.
60+
# Furthermore, this repository, `@gcc_toolchain`, is _also_ not visible (probably because it would be a circular dependency),
61+
# so we can't even patch the `protobuf/MODULE.bazel` file to bring the `@gcc_toolchain_<arch>` repositories into scope.
62+
#
63+
# Therefore, we need to use the "absolute path" of these repositories to address them (main~extension~toolchain.
64+
# Documentation: https://bazel.build/external/extension#repository_names_and_visibility
65+
#
66+
# Yes, this is brittle and the docs warn us against it,
67+
# but I haven't found any other way to reference `@gcc_toolchain_<arch>` targets from `@protobuf`.
68+
def cc_library_internal(name, static_libstdcxx = False, **kwargs):
69+
_with_libstdcxx(
70+
rule = native.cc_library,
71+
name = name,
72+
gcc_toolchain_prefix = "@@_main~gcc_toolchains~gcc_toolchain",
73+
static_libstdcxx = static_libstdcxx,
74+
**kwargs,
75+
)
76+
77+
def cc_binary_internal(name, static_libstdcxx = False, **kwargs):
78+
_with_libstdcxx(
79+
rule = native.cc_binary,
80+
name = name,
81+
gcc_toolchain_prefix = "@@_main~gcc_toolchains~gcc_toolchain",
82+
static_libstdcxx = static_libstdcxx,
83+
**kwargs,
84+
)
85+
86+
def cc_test_internal(name, static_libstdcxx = False, **kwargs):
87+
_with_libstdcxx(
88+
rule = native.cc_test,
89+
name = name,
90+
gcc_toolchain_prefix = "@@_main~gcc_toolchains~gcc_toolchain",
91+
static_libstdcxx = static_libstdcxx,
92+
**kwargs,
5293
)

0 commit comments

Comments
 (0)