Skip to content

ability to use zig build-obj -fcompiler-rt or zig build-lib -fcompiler-rt to produce a compiler-rt build artifact #7264

@andrewrk

Description

@andrewrk

This critical build artifact is already built automatically when it is needed by other build artifacts, but it can often be useful alone, especially when integrating with a third party build system.

I started trying to implement it, thinking it would be a quick fix, but I think it will be a little bit more involved because we probably want to de-duplicate the code from the buildOutputFromZig function.

Here's what I tried so far:

diff --git a/src/Compilation.zig b/src/Compilation.zig
index b7660718d..9d681f1b5 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -432,6 +432,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
 
         // Make a decision on whether to use LLVM or our own backend.
         const use_llvm = if (options.use_llvm) |explicit| explicit else blk: {
+            // Self-hosted is not yet capable of building compiler-rt.
+            if (options.is_compiler_rt_or_libc)
+                break :blk true;
+
             // If we have no zig code to compile, no need for LLVM.
             if (options.root_pkg == null)
                 break :blk false;
diff --git a/src/main.zig b/src/main.zig
index 5e406152d..396c713a1 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1314,6 +1314,8 @@ fn buildOutputType(
         fatal("`zig test` expects a zig source file argument", .{});
     }
 
+    var is_compiler_rt = false;
+
     const root_name = if (provided_name) |n| n else blk: {
         if (arg_mode == .zig_test) {
             break :blk "test";
@@ -1336,6 +1338,10 @@ fn buildOutputType(
             // TODO once the attempt to unwrap error: LinkingWithoutZigSourceUnimplemented
             // is solved, remove the above fatal() and uncomment the `break` below.
             //break :blk "run";
+        } else if (want_compiler_rt == true) {
+            is_compiler_rt = true;
+            function_sections = true;
+            break :blk "compiler_rt";
         } else {
             fatal("expected a positional argument, -femit-bin=[path], --show-builtin, or --name [name]", .{});
         }
@@ -1630,7 +1636,7 @@ fn buildOutputType(
                 .path = local_cache_dir_path,
             };
         }
-        if (arg_mode == .run) {
+        if (arg_mode == .run or is_compiler_rt) {
             break :l global_cache_directory;
         }
         const cache_dir_path = blk: {
@@ -1741,6 +1747,7 @@ fn buildOutputType(
         .test_name_prefix = test_name_prefix,
         .disable_lld_caching = !have_enable_cache,
         .subsystem = subsystem,
+        .is_compiler_rt_or_libc = is_compiler_rt,
     }) catch |err| {
         fatal("unable to create compilation: {}", .{@errorName(err)});
     };

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions