Skip to content
Open
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
2 changes: 2 additions & 0 deletions zig/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ zig_validation_exec_platform(name = "zig_validation_exec_platform")

exports_files(
[
"compiler_rt_root.zig",
"versions.json",
"versions.bzl.tpl",
"versions.bzl",
Expand Down Expand Up @@ -179,6 +180,7 @@ filegroup(
":BUILD.bazel",
":cc_helper.bzl",
":cc_linkopts.bzl",
":compiler_rt_root.zig",
":platforms.bzl",
":resolved_target_toolchain.bzl",
":resolved_toolchain.bzl",
Expand Down
135 changes: 63 additions & 72 deletions zig/private/common/zig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ load(
)

ATTRS = {
"_compiler_rt_src": attr.label(
default = Label("//zig/private:compiler_rt_root.zig"),
allow_single_file = True,
),
"_incremental_worker": attr.label(
default = "//zig/private/worker:tool",
cfg = config.exec("zig"),
Expand Down Expand Up @@ -388,12 +392,6 @@ def zig_build_impl(ctx, *, kind):
global_args = ctx.actions.args()
global_args.use_param_file("@%s")

cc_link_compile_flags = []
if use_cc_common_link:
# Zig compiler-rt provides __zig_probe_stack, which regular compiler-rt
# does not provide. The compiler_runtime attribute can override this.
cc_link_compile_flags = ["-fcompiler-rt"]
global_args.add_all(cc_link_compile_flags)
target_flags = settings.args + zigtargetinfo.args

# Direct actions also include the SDK and cache options in this group.
Expand Down Expand Up @@ -451,16 +449,9 @@ def zig_build_impl(ctx, *, kind):
should_emit_bin = bin_output_is_executable or kind == "zig_shared_library" or ctx.attr.emit_bin

compile_bin_output = bin_output
cc_static_lib = None
test_obj = None
if use_cc_common_link and kind in ["zig_binary", "zig_shared_library"]:
# Zig produces this archive; the separate C/C++ link produces bin_output.
cc_static_lib = ctx.actions.declare_file(ctx.label.name + _static_lib_extension(zigtargetinfo.triple.os))
compile_bin_output = cc_static_lib
elif use_cc_common_link and kind == "zig_test":
# Test compilation produces an object; a separate build-lib packages it.
test_obj = ctx.actions.declare_file(ctx.label.name + _object_extension(zigtargetinfo.triple.os))
compile_bin_output = test_obj
if use_cc_common_link:
# Zig produces the object; the separate C/C++ link produces bin_output.
compile_bin_output = ctx.actions.declare_file(ctx.label.name + _object_extension(zigtargetinfo.triple.os))

primary_emission = None
if should_emit_bin:
Expand Down Expand Up @@ -540,18 +531,17 @@ def zig_build_impl(ctx, *, kind):
import_names = import_names,
)

if cc_static_lib or kind == "zig_static_library":
command, output_kind = "build-lib", "static_library"
mnemonic = "ZigBuildStaticLib" if kind == "zig_static_library" else "ZigBuildLib"
elif kind == "zig_test":
command, output_kind = ("test-obj", "object") if test_obj else ("test", "executable")
mnemonic = "ZigBuildTest"
elif kind == "zig_binary":
command, output_kind = "build-exe", "executable"
mnemonic = "ZigBuildExe"
else: # zig_shared_library
command, output_kind = "build-lib", "shared_library"
mnemonic = "ZigBuildSharedLib"
if use_cc_common_link:
command, output_kind, mnemonic = (
("test-obj", "object", "ZigBuildTest") if kind == "zig_test" else ("build-obj", "object", "ZigBuildObj")
)
else:
command, output_kind, mnemonic = {
"zig_binary": ("build-exe", "executable", "ZigBuildExe"),
"zig_test": ("test", "executable", "ZigBuildTest"),
"zig_static_library": ("build-lib", "static_library", "ZigBuildStaticLib"),
"zig_shared_library": ("build-lib", "shared_library", "ZigBuildSharedLib"),
}[kind]

args = ctx.actions.args()
args.use_param_file("@%s")
Expand All @@ -564,12 +554,12 @@ def zig_build_impl(ctx, *, kind):
"outputs": [emission.kind for emission in emissions],
}))
args.add_all(compile_outputs)
args.add_all(cc_link_compile_flags + target_flags)
args.add_all(target_flags)

if ctx.attr.compiler_runtime == "include":
args.add("-fcompiler-rt")
elif ctx.attr.compiler_runtime == "exclude":
if use_cc_common_link or ctx.attr.compiler_runtime == "exclude":
args.add("-fno-compiler-rt")
elif ctx.attr.compiler_runtime == "include":
args.add("-fcompiler-rt")

if ctx.attr.strip_debug_symbols:
if generate_dsym_file:
Expand Down Expand Up @@ -647,7 +637,11 @@ def zig_build_impl(ctx, *, kind):

transitive_inputs.append(depset(cdeps_inputs))

if kind == "zig_test" and use_cc_common_link:
if use_cc_common_link:
# The C/C++ toolchain can select PIC inputs even for an executable.
args.add("-fPIC")

if command == "test-obj":
# Zig 0.16's LLVM StackProtector pass crashes for test-obj when
# compiler-rt provides __stack_chk_fail as an alias.
# See: https://codeberg.org/ziglang/zig/issues/31702
Expand Down Expand Up @@ -695,10 +689,10 @@ def zig_build_impl(ctx, *, kind):
if not incremental_worker:
for emission in auxiliary_emissions:
args.add(emission.file, format = emission.flag)
if test_obj:
if command == "test-obj":
# Keep the explicit test-object path in its own argument group.
test_output_args = ctx.actions.args()
primary_args = test_output_args if test_obj else args
primary_args = test_output_args if test_output_args else args
if primary_emission:
primary_args.add(primary_emission.file, format = primary_emission.flag)
else:
Expand All @@ -725,66 +719,63 @@ def zig_build_impl(ctx, *, kind):
)

if use_cc_common_link:
static_lib = cc_static_lib
if kind == "zig_test":
archive_emission = struct(
kind = "bin",
file = ctx.actions.declare_file(ctx.label.name + _static_lib_extension(zigtargetinfo.triple.os)),
flag = "-femit-bin=%s",
)
static_lib = archive_emission.file
lib_args = ctx.actions.args()
lib_args.add_all(
is_shared_library = kind == "zig_shared_library"
objects = [compile_bin_output]
if ctx.attr.compiler_runtime != "exclude":
# Zig 0.17 omits compiler-rt exports from test-obj. Zig 0.16 can
# crash when LLVM emits stack protection and compiler-rt into the
# same object. Build the runtime separately with the same SDK and
# target, including Zig-specific symbols such as __zig_probe_stack.
compiler_rt = ctx.actions.declare_file(ctx.label.name + ".compiler_rt" + _object_extension(zigtargetinfo.triple.os))
runtime_args = ctx.actions.args()
runtime_args.add_all(
root_module.cc_info.linking_context.linker_inputs,
map_each = zig_cdeps_libc_flags,
uniquify = True,
)
lib_args.add_all([
runtime_args.add_all([
"-fPIC",
"-fcompiler-rt",
"-OReleaseFast",
"-fno-stack-protector",
])
lib_args.add(archive_emission.file, format = archive_emission.flag)
lib_args.add(test_obj)
runtime_args.add(compiler_rt, format = "-femit-bin=%s")
runtime_args.add(ctx.file._compiler_rt_src)
runtime_inputs = [ctx.file._compiler_rt_src, zigtoolchaininfo.validation]
if zigtoolchaininfo.zig_lib.file:
runtime_inputs.append(zigtoolchaininfo.zig_lib.file)

_compile(
ctx,
zigtoolchaininfo = zigtoolchaininfo,
command = "build-lib",
output_kind = "static_library",
command = "build-obj",
output_kind = "object",
global_args = global_args,
compiler_args = lib_args,
compiler_args = runtime_args,
test_output_args = None,
worker = None,
outputs = [archive_emission.file],
inputs = [test_obj, zigtoolchaininfo.validation] + ([zigtoolchaininfo.zig_lib.file] if zigtoolchaininfo.zig_lib.file else []),
mnemonic = "ZigBuildLib",
progress_message = "zig build-lib %{label}",
outputs = [compiler_rt],
inputs = runtime_inputs,
mnemonic = "ZigCompilerRuntime",
progress_message = "zig build-obj compiler-rt for %{label}",
**zig_build_kwargs
)
objects.append(compiler_rt)

is_shared_library = kind == "zig_shared_library"
library_to_link = cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = static_lib,
alwayslink = is_shared_library,
)
linking_context = cc_common.create_linking_context(
linker_inputs = depset([
cc_common.create_linker_input(
owner = ctx.label,
libraries = depset([library_to_link]),
),
]),
)
# The same PIC objects can be used for either C/C++ link mode.
objects = depset(objects)
link_outputs = cc_common_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
name = ctx.label.name,
output_type = "dynamic_library" if is_shared_library else "executable",
main_output = bin_output,
linking_contexts = [linking_context, root_module.cc_info.linking_context],
compilation_outputs = cc_common.create_compilation_outputs(
objects = objects,
pic_objects = objects,
),
linking_contexts = [root_module.cc_info.linking_context],
user_link_flags = linkopts,
**cc_link_kwargs
)
Expand Down
2 changes: 2 additions & 0 deletions zig/private/compiler_rt_root.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Zig imports its own compiler-rt when compiling this empty root with
// -fcompiler-rt.
8 changes: 5 additions & 3 deletions zig/private/worker/worker.zig
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const Compilation = struct {

const valid_command = switch (result.output_kind) {
.executable => std.mem.eql(u8, result.command, "build-exe") or std.mem.eql(u8, result.command, "test"),
.object => std.mem.eql(u8, result.command, "test-obj"),
.object => std.mem.eql(u8, result.command, "build-obj") or std.mem.eql(u8, result.command, "test-obj"),
.static_library, .shared_library => std.mem.eql(u8, result.command, "build-lib"),
};

Expand Down Expand Up @@ -443,8 +443,10 @@ const Worker = struct {
switch (compilation.output_kind) {
.static_library => try argv.append("-static"),
.shared_library => try argv.append("-dynamic"),
.executable => if (std.mem.eql(u8, compilation.command, "test")) try argv.append("--test-no-exec"),
.object => try argv.append("--test-no-exec"),
.executable, .object => {},
}
if (std.mem.eql(u8, compilation.command, "test") or std.mem.eql(u8, compilation.command, "test-obj")) {
try argv.append("--test-no-exec");
}

var emit_bin = false;
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/dsym_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _dsyms_enabled_test_impl(ctx):
)

for action in analysistest.target_actions(env):
if action.mnemonic in ["ZigBuildLib", "ZigBuildTest"]:
if action.mnemonic in ["ZigBuildObj", "ZigBuildTest"]:
assert_flag_unset(env, "-fstrip", action.argv)

return analysistest.end(env)
Expand Down Expand Up @@ -81,7 +81,7 @@ def _dsyms_disabled_test_impl(ctx):

asserts.false(env, hasattr(output_groups, "dsyms"), "dSYMs should not be exposed when generate_dsym_file is disabled.")
assert_find_action(env, "CppLink")
assert_flag_set(env, "-fstrip", assert_find_action(env, "ZigBuildLib").argv)
assert_flag_set(env, "-fstrip", assert_find_action(env, "ZigBuildObj").argv)

return analysistest.end(env)

Expand Down
8 changes: 4 additions & 4 deletions zig/tests/emit_attr_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def _use_cc_common_link_binary_emit_asm_test_impl(ctx):
asserts.equals(env, 1, len(asm_outputs), "zig_binary should expose exactly one asm output.")
asm_output = asm_outputs[0]

build = assert_find_action(env, "ZigBuildLib")
build = assert_find_action(env, "ZigBuildObj")

_assert_prefixed_arg_unique(env, "-femit-asm=", build.argv)
asserts.true(env, sets.contains(sets.make(build.outputs.to_list()), asm_output), "zig_binary ZigBuildLib action should declare the asm output.")
asserts.true(env, sets.contains(sets.make(build.outputs.to_list()), asm_output), "zig_binary ZigBuildObj action should declare the asm output.")

return analysistest.end(env)

Expand Down Expand Up @@ -123,10 +123,10 @@ def _use_cc_common_link_shared_emit_asm_test_impl(ctx):
asserts.equals(env, 1, len(asm_outputs), "zig_shared_library should expose exactly one asm output.")
asm_output = asm_outputs[0]

build = assert_find_action(env, "ZigBuildLib")
build = assert_find_action(env, "ZigBuildObj")

_assert_prefixed_arg_unique(env, "-femit-asm=", build.argv)
asserts.true(env, sets.contains(sets.make(build.outputs.to_list()), asm_output), "zig_shared_library ZigBuildLib action should declare the asm output.")
asserts.true(env, sets.contains(sets.make(build.outputs.to_list()), asm_output), "zig_shared_library ZigBuildObj action should declare the asm output.")

return analysistest.end(env)

Expand Down
8 changes: 8 additions & 0 deletions zig/tests/incremental/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ zig_test(
visibility = ["//zig/tests:__pkg__"],
)

zig_test(
name = "test_without_compiler_runtime",
compiler_runtime = "exclude",
main = "test.zig",
tags = ["manual"],
visibility = ["//zig/tests:__pkg__"],
)

zig_static_library(
name = "static_library",
main = "library.zig",
Expand Down
Loading
Loading