Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andrewrk opened this issue Dec 1, 2020 · 1 comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Dec 1, 2020

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)});
     };
@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Dec 1, 2020
@andrewrk andrewrk added this to the 0.9.0 milestone Dec 1, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@alloveras
Copy link

Hey @andrewrk,
I am not super familiar with Zig's on-demand build process of the libcompiler_rt.a and libcompiler_rt.a.o files. I am currently trying to do the exact same thing this ticket aims to provide (a way to build these files in a standalone way).

From my own experimentation, I think I managed to get the files built independently but I wonder if there are any flaws with the approach that I may have missed due to my ignorance of the topic itself and Zig's internals:

$ zig version
0.10.0

$ zig build-lib -target x86_64-linux-gnu "$(zig env | jq -r .lib_dir)/compiler_rt.zig"

$ ls lib*
libcompiler_rt.a   libcompiler_rt.a.o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

2 participants