Skip to content

Commit 41c4a75

Browse files
authored
Merge pull request #561 from cerisier/settings-zigopts
feat: add support for global zigopt setting
2 parents 9ee67f9 + e9a2e6f commit 41c4a75

File tree

6 files changed

+119
-5
lines changed

6 files changed

+119
-5
lines changed

docs/rules.md

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

zig/private/settings.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ ATTRS = {
2727
doc = "The Zig multi- or single-threaded setting.",
2828
mandatory = True,
2929
),
30+
"zigopt": attr.label(
31+
doc = """
32+
Additional list of flags passed to the zig compiler for all Zig compile actions.
33+
34+
The flags specified by this setting do not override those specified via the `zigopts` attribute of `zig_*` rules.
35+
Instead, they are prepended to the command line before module specific flags.
36+
37+
This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself.
38+
Use this at your own risk of hitting undefined behaviors.
39+
""",
40+
mandatory = True,
41+
),
3042
}
3143

3244
MODE_ARGS = {
@@ -56,6 +68,9 @@ def _settings_impl(ctx):
5668
threaded = ctx.attr.threaded[BuildSettingInfo].value
5769
args.extend(THREADED_ARGS[threaded])
5870

71+
zigopts = ctx.attr.zigopt[BuildSettingInfo].value
72+
args.extend(zigopts)
73+
5974
settings_info = ZigSettingsInfo(
6075
mode = mode,
6176
threaded = threaded,

zig/private/zig_configure.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def _zig_transition_impl(settings, attr):
113113
result["//zig/settings:mode"] = attr.mode
114114
if attr.threaded:
115115
result["//zig/settings:threaded"] = attr.threaded
116+
if attr.zigopt:
117+
result["//zig/settings:zigopt"] = attr.zigopt
116118
return result
117119

118120
_zig_transition = transition(
@@ -124,6 +126,7 @@ _zig_transition = transition(
124126
"//zig/settings:use_cc_common_link",
125127
"//zig/settings:mode",
126128
"//zig/settings:threaded",
129+
"//zig/settings:zigopt",
127130
],
128131
outputs = [
129132
"//command_line_option:extra_toolchains",
@@ -132,6 +135,7 @@ _zig_transition = transition(
132135
"//zig/settings:use_cc_common_link",
133136
"//zig/settings:mode",
134137
"//zig/settings:threaded",
138+
"//zig/settings:zigopt",
135139
],
136140
)
137141

@@ -169,6 +173,18 @@ def _make_attrs(*, executable):
169173
mandatory = False,
170174
values = THREADED_VALUES,
171175
),
176+
"zigopt": attr.string_list(
177+
doc = """
178+
Additional list of flags passed to the zig compiler for all Zig compile actions.
179+
180+
The flags specified by this setting do not override those specified via the `zigopts` attribute of `zig_*` rules.
181+
Instead, they are prepended to the command line before module specific flags.
182+
183+
This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself.
184+
Use this at your own risk of hitting undefined behaviors.
185+
""",
186+
mandatory = False,
187+
),
172188
"_allowlist_function_transition": attr.label(
173189
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
174190
),

zig/settings/BUILD.bazel

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
1+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "repeatable_string_flag", "string_flag")
22
load(
33
"//zig/private:settings.bzl",
44
"MODE_VALUES",
@@ -12,12 +12,13 @@ settings(
1212
threaded = ":threaded",
1313
use_cc_common_link = ":use_cc_common_link",
1414
visibility = ["//visibility:public"],
15+
zigopt = ":zigopt",
1516
)
1617

1718
bool_flag(
1819
name = "use_cc_common_link",
1920
build_setting_default = False,
20-
visibility = ["//zig/config/mode:__pkg__"],
21+
visibility = ["//visibility:private"],
2122
)
2223

2324
string_flag(
@@ -34,6 +35,12 @@ string_flag(
3435
visibility = ["//zig/config/threaded:__pkg__"],
3536
)
3637

38+
repeatable_string_flag(
39+
name = "zigopt",
40+
build_setting_default = [],
41+
visibility = ["//visibility:private"],
42+
)
43+
3744
# Execute `bazel run //util:update_filegroups` to update this target.
3845
filegroup(
3946
name = "all_files",

zig/tests/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ load(":threaded_test.bzl", "threaded_test_suite")
1212
load(":toolchain_header_test.bzl", "toolchain_header_test_suite")
1313
load(":use_cc_common_link_test.bzl", "use_cc_common_link_test_suite")
1414
load(":versions_test.bzl", "versions_test_suite")
15+
load(":zigopt_test.bzl", "zigopt_test_suite")
1516

1617
bzlmod_zig_test_suite(name = "bzlmod_zig_test")
1718

@@ -39,6 +40,8 @@ toolchain_header_test_suite(name = "toolchain_header_test")
3940

4041
versions_test_suite(name = "versions_test")
4142

43+
zigopt_test_suite(name = "zigopt_test")
44+
4245
bzl_library(
4346
name = "util",
4447
srcs = ["util.bzl"],

zig/tests/zigopt_test.bzl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""Analysis tests for Zig multi- or single-zigopt settings."""
2+
3+
load("@bazel_skylib//lib:partial.bzl", "partial")
4+
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "unittest")
5+
load("//zig/private/providers:zig_settings_info.bzl", "ZigSettingsInfo")
6+
load(
7+
":util.bzl",
8+
"assert_find_action",
9+
"assert_flag_set",
10+
"canonical_label",
11+
)
12+
13+
_SETTINGS_ZIGOPT = canonical_label("@//zig/settings:zigopt")
14+
15+
def _define_settings_zigopt_test(zigopts):
16+
def _test_impl(ctx):
17+
env = analysistest.begin(ctx)
18+
19+
settings = analysistest.target_under_test(env)[ZigSettingsInfo]
20+
21+
for zigopt in zigopts:
22+
assert_flag_set(env, zigopt, settings.args)
23+
24+
return analysistest.end(env)
25+
26+
return analysistest.make(
27+
_test_impl,
28+
config_settings = {_SETTINGS_ZIGOPT: zigopts},
29+
)
30+
31+
_settings_zigopt_test = _define_settings_zigopt_test(["-mcpu=native", "-flto"])
32+
33+
def _define_build_zigopt_test(mnemonic, zigopts):
34+
def _test_impl(ctx):
35+
env = analysistest.begin(ctx)
36+
37+
action = assert_find_action(env, mnemonic)
38+
39+
for zigopt in zigopts:
40+
assert_flag_set(env, zigopt, action.argv)
41+
42+
return analysistest.end(env)
43+
44+
return analysistest.make(
45+
_test_impl,
46+
config_settings = {_SETTINGS_ZIGOPT: zigopts},
47+
)
48+
49+
_build_exe_zigopt_test = _define_build_zigopt_test("ZigBuildExe", ["-mcpu-native", "-flto"])
50+
51+
_build_static_lib_zigopt_test = _define_build_zigopt_test("ZigBuildStaticLib", ["-mcpu-native", "-flto"])
52+
53+
_build_shared_lib_zigopt_test = _define_build_zigopt_test("ZigBuildSharedLib", ["-mcpu-native", "-flto"])
54+
55+
_build_test_zigopt_test = _define_build_zigopt_test("ZigBuildTest", ["-mcpu-native", "-flto"])
56+
57+
def zigopt_test_suite(name):
58+
unittest.suite(
59+
name,
60+
# Test Zig zigopt setting on the settings target
61+
partial.make(_settings_zigopt_test, target_under_test = "//zig/settings", size = "small"),
62+
# Test Zig zigopt setting on a binary target
63+
partial.make(_build_exe_zigopt_test, target_under_test = "//zig/tests/simple-binary:binary", size = "small"),
64+
# Test Zig zigopt setting on a library target
65+
partial.make(_build_static_lib_zigopt_test, target_under_test = "//zig/tests/simple-library:library", size = "small"),
66+
# Test Zig zigopt setting on a shared library target
67+
partial.make(_build_shared_lib_zigopt_test, target_under_test = "//zig/tests/simple-shared-library:shared", size = "small"),
68+
# Test Zig zigopt setting on a test target
69+
partial.make(_build_test_zigopt_test, target_under_test = "//zig/tests/simple-test:test", size = "small"),
70+
)

0 commit comments

Comments
 (0)