Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/rules.md

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

1 change: 1 addition & 0 deletions zig/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ filegroup(
":BUILD.bazel",
"//zig/config/bootstrapped:all_files",
"//zig/config/mode:all_files",
"//zig/config/strip:all_files",
"//zig/config/threaded:all_files",
"//zig/config/use_standalone_translate_c:all_files",
],
Expand Down
27 changes: 27 additions & 0 deletions zig/config/strip/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
config_setting(
name = "always",
values = {
"strip": "always",
},
)

config_setting(
name = "never",
values = {
"strip": "never",
},
)

config_setting(
name = "sometimes",
values = {
"strip": "sometimes",
},
)

# Execute `bazel run //util:update_filegroups` to update this target.
filegroup(
name = "all_files",
srcs = [":BUILD.bazel"],
visibility = ["//zig/config:__pkg__"],
)
9 changes: 5 additions & 4 deletions zig/private/common/zig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ enabled, see https://github.com/ziglang/zig/issues/25069. Set
mandatory = False,
),
"strip_debug_symbols": attr.bool(
doc = "Whether to pass '-fstrip' to the zig compiler to remove debug symbols.",
doc = "Whether to force passing '-fstrip' to the zig compiler to remove debug symbols. Bazel's `--strip` flag is also supported globally.",
mandatory = False,
default = False,
),
Expand Down Expand Up @@ -262,7 +262,8 @@ def zig_build_impl(ctx, *, kind):
translate_c_toolchain = translate_c_exec_group_toolchain(ctx)
translatectoolchaininfo = translate_c_toolchain.translatectoolchaininfo if translate_c_toolchain else None

use_cc_common_link = ctx.attr._settings[ZigSettingsInfo].use_cc_common_link
settings = ctx.attr._settings[ZigSettingsInfo]
use_cc_common_link = settings.use_cc_common_link
use_test_obj = kind == "zig_test" and use_cc_common_link and semver.gte(zigtoolchaininfo.zig_version, "0.16.0")

providers = []
Expand Down Expand Up @@ -305,7 +306,7 @@ def zig_build_impl(ctx, *, kind):
elif ctx.attr.compiler_runtime == "exclude":
args.add("-fno-compiler-rt")

if ctx.attr.strip_debug_symbols:
if ctx.attr.strip_debug_symbols and not settings.strip:
args.add("-fstrip")

zig_lib_dir(
Expand Down Expand Up @@ -411,7 +412,7 @@ def zig_build_impl(ctx, *, kind):
)

zig_settings(
settings = ctx.attr._settings[ZigSettingsInfo],
settings = settings,
args = global_args,
)

Expand Down
1 change: 1 addition & 0 deletions zig/private/providers/zig_settings_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ FIELDS = {
"mode": "The Zig build mode.",
"use_cc_common_link": "Whether to use cc_common.link to link zig binaries, tests and shared libraries.",
"threaded": "The Zig multi- or single-threaded setting.",
"strip": "Whether Zig compile actions should remove debug symbols.",
"args": "The collected compiler arguments for all active settings.",
}

Expand Down
83 changes: 62 additions & 21 deletions zig/private/settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ You can build the settings target to obtain a JSON file
capturing all configured Zig build settings.
"""

MODE_ARGS = {
"debug": ["-O", "Debug"],
"release_safe": ["-O", "ReleaseSafe"],
"release_small": ["-O", "ReleaseSmall"],
"release_fast": ["-O", "ReleaseFast"],
}

MODE_VALUES = ["auto", "debug", "release_safe", "release_small", "release_fast"]

COMPILATION_MODE_TO_MODE = {
"dbg": "debug",
"fastbuild": "debug",
"opt": "release_fast",
}

THREADED_ARGS = {
"multi": ["-fno-single-threaded"],
"single": ["-fsingle-threaded"],
}

THREADED_VALUES = ["multi", "single"]

STRIP_VALUES = ["always", "never", "sometimes"]

ATTRS = {
"mode": attr.label(
doc = "The build mode setting.",
Expand Down Expand Up @@ -64,29 +88,16 @@ Use this at your own risk of hitting undefined behaviors.
""",
mandatory = True,
),
"_bazel_strip": attr.label(
doc = "The selected Bazel --strip option value.",
default = "//zig/settings:strip",
),
}

MODE_ARGS = {
"debug": ["-O", "Debug"],
"release_safe": ["-O", "ReleaseSafe"],
"release_small": ["-O", "ReleaseSmall"],
"release_fast": ["-O", "ReleaseFast"],
}

MODE_VALUES = ["auto", "debug", "release_safe", "release_small", "release_fast"]

COMPILATION_MODE_TO_MODE = {
"dbg": "debug",
"fastbuild": "debug",
"opt": "release_fast",
}

THREADED_ARGS = {
"multi": ["-fno-single-threaded"],
"single": ["-fsingle-threaded"],
}

THREADED_VALUES = ["multi", "single"]
BazelStripInfo = provider(
doc = "The selected Bazel --strip option value.",
fields = ["strip"],
)

def _is_exec_configuration(ctx):
return ctx.genfiles_dir.path.find("-exec") != -1
Expand All @@ -101,6 +112,16 @@ def _resolve_mode(ctx, mode):

return COMPILATION_MODE_TO_MODE[compilation_mode]

def _resolve_strip(ctx, strip):
if strip == "always":
return True
if strip == "never":
return False
if strip == "sometimes":
return ctx.var["COMPILATION_MODE"] == "fastbuild"

fail("Unrecognized Bazel strip setting {}.".format(strip))

def _settings_impl(ctx):
args = []

Expand All @@ -115,12 +136,17 @@ def _settings_impl(ctx):

use_cc_common_link = ctx.attr.host_use_cc_common_link[BuildSettingInfo].value if is_exec_configuration else ctx.attr.use_cc_common_link[BuildSettingInfo].value

strip = _resolve_strip(ctx, ctx.attr._bazel_strip[BazelStripInfo].strip)
if strip:
args.append("-fstrip")

args.extend(ctx.attr.host_zigopt[BuildSettingInfo].value if is_exec_configuration else ctx.attr.zigopt[BuildSettingInfo].value)

settings_info = ZigSettingsInfo(
mode = mode,
threaded = threaded,
use_cc_common_link = use_cc_common_link,
strip = strip,
args = args,
)

Expand All @@ -141,3 +167,18 @@ settings = rule(
attrs = ATTRS,
doc = DOC,
)

def _bazel_strip_impl(ctx):
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it here because it's a private attribute to this rule but we can put it somewhere else

return [BazelStripInfo(strip = ctx.attr.strip)]

bazel_strip = rule(
_bazel_strip_impl,
attrs = {
"strip": attr.string(
doc = "The Bazel --strip option value.",
mandatory = True,
values = STRIP_VALUES,
),
},
doc = "Captures the selected Bazel --strip option value.",
)
10 changes: 10 additions & 0 deletions zig/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load(
"//zig/private:settings.bzl",
"MODE_VALUES",
"THREADED_VALUES",
"bazel_strip",
"settings",
)

Expand All @@ -19,6 +20,15 @@ settings(
zigopt = ":zigopt",
)

bazel_strip(
name = "strip",
strip = select({
"//zig/config/strip:always": "always",
"//zig/config/strip:never": "never",
"//zig/config/strip:sometimes": "sometimes",
}),
)

bool_flag(
name = "use_cc_common_link",
build_setting_default = False,
Expand Down
Loading
Loading