Skip to content

Commit 4570fdc

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

12 files changed

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

MODULE.bazel.lock

Lines changed: 700 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.

examples/lapack/BUILD.bazel

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# limitations under the License.
1717

1818
load("@rules_foreign_cc//foreign_cc:make.bzl", "make")
19+
load(":patches.bzl", "lapack_repo_name")
1920

2021
_libs = [
2122
"liblapack.a",
@@ -35,15 +36,18 @@ make(
3536
],
3637
env = {
3738
"FC": "$$EXT_BUILD_ROOT$$/$(FC)",
38-
"FFLAGS": "$(FFLAGS) -frecursive",
39+
"FFLAGS": "$(FFLAGS_RULES_FOREIGN_CC) -frecursive",
3940
"LD": "$$EXT_BUILD_ROOT$$/$(LD)",
40-
"LDFLAGS": "$(FLDFLAGS)",
41+
"LDFLAGS": "$(FLDFLAGS_RULES_FOREIGN_CC)",
4142
},
4243
lib_name = "lapack",
4344
lib_source = "@lapack//:srcs",
4445
out_static_libs = _libs,
4546
postfix_script = "\n".join([
46-
"mv $$EXT_BUILD_ROOT/external/lapack/{0} $$INSTALLDIR/lib/{0}".format(lib)
47+
"mv $$EXT_BUILD_ROOT/external/{0}/{1} $$INSTALLDIR/lib/{1}".format(
48+
lapack_repo_name,
49+
lib,
50+
)
4751
for lib in _libs
4852
]),
4953
targets = [

examples/lapack/patches.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ EOF
5555
"""
5656

5757
LAPACK_PATCHES = [_create_make_inc]
58+
59+
# Canonical repository name of the @lapack dependency.
60+
# Documentation: https://bazel.build/external/module#repository_names_and_strict_deps
61+
lapack_repo_name = Label("@lapack").repo_name

rules_cc/defs.bzl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,41 @@
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, 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": ["@gcc_toolchain_aarch64//:{}".format(target_name)],
20+
"@platforms//cpu:armv7": ["@gcc_toolchain_armv7//:{}".format(target_name)],
21+
"@platforms//cpu:x86_64": ["@gcc_toolchain_x86_64//:{}".format(target_name)],
2122
})
22-
native.cc_library(
23+
24+
rule(
2325
name = name,
2426
deps = deps,
2527
**kwargs
2628
)
2729

30+
def cc_library(name, static_libstdcxx = False, **kwargs):
31+
_with_libstdcxx(
32+
rule = native.cc_library,
33+
name = name,
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+
static_libstdcxx = static_libstdcxx,
43+
**kwargs,
3944
)
4045

4146
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(
47+
_with_libstdcxx(
48+
rule = native.cc_test,
4949
name = name,
50-
deps = deps,
51-
**kwargs
50+
static_libstdcxx = static_libstdcxx,
51+
**kwargs,
5252
)

rules_fortran/defs.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,17 @@ def _current_fortran_toolchain_impl(ctx):
487487
(fortran_toolchain, feature_configuration) = _get_configuration(ctx)
488488
(compiler, compile_flags) = _get_compiler(fortran_toolchain, feature_configuration)
489489
(_, link_flags) = _get_linker(fortran_toolchain, feature_configuration)
490+
491+
# Because rules_foreign_cc runs in its own directory, we need to prepend the special variable $$EXT_BUILD_ROOT
492+
# to make sure we're linking against the right targets.
493+
link_flags_foreign_cc = [f.replace("external/", "$$EXT_BUILD_ROOT/external/") for f in link_flags]
494+
compile_flags_foreign_cc = [f.replace("external/", "$$EXT_BUILD_ROOT/external/") for f in compile_flags]
490495
vars = {
491496
"FC": compiler,
492497
"FFLAGS": " ".join(compile_flags),
493498
"FLDFLAGS": " ".join(link_flags),
499+
"FFLAGS_RULES_FOREIGN_CC": " ".join(compile_flags_foreign_cc),
500+
"FLDFLAGS_RULES_FOREIGN_CC": " ".join(link_flags_foreign_cc),
494501
}
495502
files = depset([], transitive = [fortran_toolchain.all_files])
496503
return [

0 commit comments

Comments
 (0)