diff --git a/zig/private/BUILD.bazel b/zig/private/BUILD.bazel index 65b0b99e..99246070 100644 --- a/zig/private/BUILD.bazel +++ b/zig/private/BUILD.bazel @@ -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", @@ -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", diff --git a/zig/private/common/zig_build.bzl b/zig/private/common/zig_build.bzl index f96f9040..224c755f 100644 --- a/zig/private/common/zig_build.bzl +++ b/zig/private/common/zig_build.bzl @@ -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"), @@ -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. @@ -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: @@ -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") @@ -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: @@ -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 @@ -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: @@ -725,58 +719,51 @@ 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, @@ -784,7 +771,11 @@ def zig_build_impl(ctx, *, kind): 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 ) diff --git a/zig/private/compiler_rt_root.zig b/zig/private/compiler_rt_root.zig new file mode 100644 index 00000000..41fdc48f --- /dev/null +++ b/zig/private/compiler_rt_root.zig @@ -0,0 +1,2 @@ +// Zig imports its own compiler-rt when compiling this empty root with +// -fcompiler-rt. diff --git a/zig/private/worker/worker.zig b/zig/private/worker/worker.zig index dc2bcf7b..48a71798 100644 --- a/zig/private/worker/worker.zig +++ b/zig/private/worker/worker.zig @@ -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"), }; @@ -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; diff --git a/zig/tests/dsym_test.bzl b/zig/tests/dsym_test.bzl index ffce021b..e070ea8f 100644 --- a/zig/tests/dsym_test.bzl +++ b/zig/tests/dsym_test.bzl @@ -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) @@ -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) diff --git a/zig/tests/emit_attr_test.bzl b/zig/tests/emit_attr_test.bzl index 451a6d1d..978ecdd7 100644 --- a/zig/tests/emit_attr_test.bzl +++ b/zig/tests/emit_attr_test.bzl @@ -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) @@ -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) diff --git a/zig/tests/incremental/BUILD.bazel b/zig/tests/incremental/BUILD.bazel index 6b4d835b..51ed907b 100644 --- a/zig/tests/incremental/BUILD.bazel +++ b/zig/tests/incremental/BUILD.bazel @@ -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", diff --git a/zig/tests/incremental_test.bzl b/zig/tests/incremental_test.bzl index 9698611d..c899a623 100644 --- a/zig/tests/incremental_test.bzl +++ b/zig/tests/incremental_test.bzl @@ -101,11 +101,15 @@ def _cc_binary_handoff_test_impl(ctx): env = analysistest.begin(ctx) target = analysistest.target_under_test(env) compile = assert_find_action(env, "ZigIncrementalCompile") + runtime = assert_find_action(env, "ZigCompilerRuntime") link = assert_find_action(env, "CppLink") - archives = compile.outputs.to_list() - asserts.equals(env, 1, len(archives)) - asserts.true(env, archives[0] in link.inputs.to_list(), "The separate link must consume the worker's archive.") + objects = compile.outputs.to_list() + asserts.equals(env, 1, len(objects)) + asserts.true(env, objects[0] in link.inputs.to_list(), "The separate link must consume the worker's object.") + asserts.true(env, "-fno-compiler-rt" in compile.argv) + if runtime: + asserts.true(env, runtime.outputs.to_list()[0] in link.inputs.to_list()) executable = target[DefaultInfo].files_to_run.executable asserts.equals(env, [executable], target[DefaultInfo].files.to_list()) @@ -126,20 +130,22 @@ def _cc_test_handoff_test_impl(ctx): env = analysistest.begin(ctx) target = analysistest.target_under_test(env) compile = assert_find_action(env, "ZigIncrementalCompile") - archive = assert_find_action(env, "ZigBuildLib") + runtime = assert_find_action(env, "ZigCompilerRuntime") link = assert_find_action(env, "CppLink") for flag in ["--zig-lib-dir", "--cache-dir", "--global-cache-dir"]: - asserts.equals(env, 1, len([arg for arg in archive.argv if arg == flag]), "Ordinary packaging needs exactly one {}.".format(flag)) + asserts.equals(env, 1, len([arg for arg in runtime.argv if arg == flag]), "The runtime build needs exactly one {}.".format(flag)) asserts.false(env, flag in compile.argv, "The worker owns its SDK/cache paths; {} must not leak into the request.".format(flag)) objects = compile.outputs.to_list() asserts.equals(env, 1, len(objects)) - asserts.true(env, objects[0] in archive.inputs.to_list(), "Ordinary archive packaging must consume the worker's test object.") + asserts.true(env, "-fno-compiler-rt" in compile.argv) + asserts.false(env, objects[0] in runtime.inputs.to_list(), "The runtime build can run independently of test compilation.") + asserts.true(env, objects[0] in link.inputs.to_list(), "The separate link must consume the worker's test object.") - archives = archive.outputs.to_list() - asserts.equals(env, 1, len(archives)) - asserts.true(env, archives[0] in link.inputs.to_list(), "The separate link must consume the packaged test archive.") + runtime_outputs = runtime.outputs.to_list() + asserts.equals(env, 1, len(runtime_outputs)) + asserts.true(env, runtime_outputs[0] in link.inputs.to_list(), "The separate link must consume the runtime object.") executable = target[DefaultInfo].files_to_run.executable asserts.equals(env, [executable], target[DefaultInfo].files.to_list()) @@ -149,15 +155,28 @@ def _cc_test_handoff_test_impl(ctx): _cc_test_handoff_test = analysistest.make(_cc_test_handoff_test_impl, config_settings = _CC_SETTINGS) +def _cc_test_without_runtime_test_impl(ctx): + env = analysistest.begin(ctx) + compile = assert_find_action(env, "ZigIncrementalCompile") + link = assert_find_action(env, "CppLink") + + asserts.true(env, "-fno-compiler-rt" in compile.argv) + asserts.equals(env, [], [action for action in analysistest.target_actions(env) if action.mnemonic == "ZigCompilerRuntime"]) + asserts.true(env, compile.outputs.to_list()[0] in link.inputs.to_list()) + + return analysistest.end(env) + +_cc_test_without_runtime_test = analysistest.make(_cc_test_without_runtime_test_impl, config_settings = _CC_SETTINGS) + def _cc_shared_handoff_test_impl(ctx): env = analysistest.begin(ctx) target = analysistest.target_under_test(env) compile = assert_find_action(env, "ZigIncrementalCompile") link = assert_find_action(env, "CppLink") - archives = compile.outputs.to_list() - asserts.equals(env, 1, len(archives)) - asserts.true(env, archives[0] in link.inputs.to_list(), "The separate link must consume the worker's archive.") + objects = compile.outputs.to_list() + asserts.equals(env, 1, len(objects)) + asserts.true(env, objects[0] in link.inputs.to_list(), "The separate link must consume the worker's object.") libraries = target[DefaultInfo].files.to_list() asserts.equals(env, 1, len(libraries)) @@ -186,9 +205,9 @@ def _auxiliary_outputs_test_impl(ctx): asserts.true(env, file in compile.outputs.to_list(), "Auxiliary outputs belong to compilation, not linking.") asserts.false(env, file in link.outputs.to_list()) - archives = [file for file in compile.outputs.to_list() if file not in auxiliary] - asserts.equals(env, 1, len(archives)) - asserts.true(env, archives[0] in link.inputs.to_list()) + objects = [file for file in compile.outputs.to_list() if file not in auxiliary] + asserts.equals(env, 1, len(objects)) + asserts.true(env, objects[0] in link.inputs.to_list()) executable = target[DefaultInfo].files_to_run.executable asserts.equals(env, [executable], target[DefaultInfo].files.to_list()) @@ -285,7 +304,7 @@ def incremental_test_suite(name): partial.make(_test_binary_test, target_under_test = "//zig/tests/incremental:test_binary", size = "small"), partial.make(_static_library_test, target_under_test = "//zig/tests/incremental:static_library", size = "small"), partial.make(_shared_library_test, target_under_test = "//zig/tests/incremental:shared_library", size = "small"), - # Test the archive and test-object handoffs to cc_common.link. + # Test the object handoffs to cc_common.link. partial.make(_cc_binary_handoff_test, target_under_test = "//zig/tests/incremental:native_binary", size = "small"), partial.make(_cc_test_handoff_test, target_under_test = "//zig/tests/incremental:test_binary", size = "small"), partial.make(_cc_shared_handoff_test, target_under_test = "//zig/tests/incremental:shared_library", size = "small"), @@ -298,4 +317,5 @@ def incremental_test_suite(name): partial.make(_unsupported_os_test, target_under_test = "//zig/tests/incremental:binary", size = "small"), partial.make(_unsupported_execution_toolchain_test, target_under_test = "//zig/tests/incremental:binary", size = "small"), partial.make(_unsupported_c_sources_test, target_under_test = "//zig/tests/incremental:c_sources", size = "small"), + partial.make(_cc_test_without_runtime_test, target_under_test = "//zig/tests/incremental:test_without_compiler_runtime", size = "small"), ) diff --git a/zig/tests/rules_test.bzl b/zig/tests/rules_test.bzl index a8bbc91a..f9bed10a 100644 --- a/zig/tests/rules_test.bzl +++ b/zig/tests/rules_test.bzl @@ -653,9 +653,9 @@ def _use_cc_common_link_simple_binary_test_impl(ctx): build = [ action for action in analysistest.target_actions(env) - if action.mnemonic == "ZigBuildLib" + if action.mnemonic == "ZigBuildObj" ] - asserts.equals(env, 1, len(build), "zig_binary should generate one ZigBuildLib action.") + asserts.equals(env, 1, len(build), "zig_binary should generate one ZigBuildObj action.") build = [ action diff --git a/zig/tests/use_cc_common_link_test.bzl b/zig/tests/use_cc_common_link_test.bzl index dd135370..c9ca0ee2 100644 --- a/zig/tests/use_cc_common_link_test.bzl +++ b/zig/tests/use_cc_common_link_test.bzl @@ -63,7 +63,7 @@ def _define_build_use_cc_common_link_test(mnemonics, use_cc_common_link): config_settings = {_SETTINGS_USE_CC_COMMON_LINK: use_cc_common_link}, ) -_build_exe_use_cc_common_link_cc_test = _define_build_use_cc_common_link_test(["ZigBuildLib", "CppLink"], use_cc_common_link = True) +_build_exe_use_cc_common_link_cc_test = _define_build_use_cc_common_link_test(["ZigBuildObj", "CppLink"], use_cc_common_link = True) _build_exe_use_cc_common_link_zig_test = _define_build_use_cc_common_link_test(["ZigBuildExe"], use_cc_common_link = False) def _static_library_link_setting_test_impl(ctx): @@ -86,10 +86,10 @@ _build_static_lib_use_cc_common_link_zig_test = analysistest.make( config_settings = {_SETTINGS_USE_CC_COMMON_LINK: False}, ) -_build_shared_lib_use_cc_common_link_cc_test = _define_build_use_cc_common_link_test(["ZigBuildLib", "CppLink"], use_cc_common_link = True) +_build_shared_lib_use_cc_common_link_cc_test = _define_build_use_cc_common_link_test(["ZigBuildObj", "CppLink"], use_cc_common_link = True) _build_shared_lib_use_cc_common_link_zig_test = _define_build_use_cc_common_link_test(["ZigBuildSharedLib"], use_cc_common_link = False) -_build_test_use_cc_common_link_cc_test = _define_build_use_cc_common_link_test(["ZigBuildLib", "CppLink"], use_cc_common_link = True) +_build_test_use_cc_common_link_cc_test = _define_build_use_cc_common_link_test(["ZigBuildTest", "ZigCompilerRuntime", "CppLink"], use_cc_common_link = True) _build_test_use_cc_common_link_zig_test = _define_build_use_cc_common_link_test(["ZigBuildTest"], use_cc_common_link = False) def use_cc_common_link_test_suite(name):