| ... | @@ -1,123 +1,169 @@ | ... | @@ -1,123 +1,169 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| | 3 | const build_options = @import("build_options"); |
| 3 | const Allocator = std.mem.Allocator; | 4 | const Allocator = std.mem.Allocator; |
| | 5 | const assert = std.debug.assert; |
| 4 | const mem = std.mem; | 6 | const mem = std.mem; |
| 5 | const tracy = @import("tracy.zig"); | 7 | const tracy = @import("tracy.zig"); |
| 6 | const trace = tracy.trace; | 8 | const trace = tracy.trace; |
| 7 | | 9 | |
| | 10 | const Cache = @import("Cache.zig"); |
| 8 | const Compilation = @import("Compilation.zig"); | 11 | const Compilation = @import("Compilation.zig"); |
| 9 | const CRTFile = Compilation.CRTFile; | 12 | const CRTFile = Compilation.CRTFile; |
| 10 | const LinkObject = Compilation.LinkObject; | 13 | const LinkObject = Compilation.LinkObject; |
| 11 | const Package = @import("Package.zig"); | 14 | const Package = @import("Package.zig"); |
| 12 | | 15 | |
| 13 | pub const CompilerRtLib = struct { | 16 | pub fn buildCompilerRtLib(comp: *Compilation, compiler_rt_lib: *?CRTFile) !void { |
| 14 | crt_object_files: [sources.len]?CRTFile = undefined, | | |
| 15 | crt_lib_file: ?CRTFile = null, | | |
| 16 | | | |
| 17 | pub fn deinit(crt_lib: *CompilerRtLib, gpa: Allocator) void { | | |
| 18 | for (crt_lib.crt_object_files) |*crt_file| { | | |
| 19 | if (crt_file.*) |*cf| { | | |
| 20 | cf.deinit(gpa); | | |
| 21 | } | | |
| 22 | } | | |
| 23 | if (crt_lib.crt_lib_file) |*crt_file| { | | |
| 24 | crt_file.deinit(gpa); | | |
| 25 | } | | |
| 26 | } | | |
| 27 | }; | | |
| 28 | | | |
| 29 | pub fn buildCompilerRtLib(comp: *Compilation, compiler_rt_lib: *CompilerRtLib) !void { | | |
| 30 | const tracy_trace = trace(@src()); | 17 | const tracy_trace = trace(@src()); |
| 31 | defer tracy_trace.end(); | 18 | defer tracy_trace.end(); |
| 32 | | 19 | |
| 33 | var progress: std.Progress = .{ .dont_print_on_dumb = true }; | 20 | var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa); |
| 34 | var progress_node = progress.start("Compile Compiler-RT", sources.len + 1); | 21 | defer arena_allocator.deinit(); |
| 35 | defer progress_node.end(); | 22 | const arena = arena_allocator.allocator(); |
| 36 | if (comp.color == .off) progress.terminal = null; | | |
| 37 | | 23 | |
| 38 | progress_node.activate(); | 24 | const target = comp.getTarget(); |
| 39 | | 25 | |
| 40 | var link_objects: [sources.len]LinkObject = undefined; | 26 | // Use the global cache directory. |
| 41 | for (sources) |source, i| { | 27 | var cache_parent: Cache = .{ |
| 42 | var obj_progress_node = progress_node.start(source, 0); | 28 | .gpa = comp.gpa, |
| 43 | obj_progress_node.activate(); | 29 | .manifest_dir = try comp.global_cache_directory.handle.makeOpenPath("h", .{}), |
| 44 | defer obj_progress_node.end(); | 30 | }; |
| | 31 | defer cache_parent.manifest_dir.close(); |
| 45 | | 32 | |
| 46 | try comp.buildOutputFromZig(source, .Obj, &compiler_rt_lib.crt_object_files[i], .compiler_rt); | 33 | var cache = cache_parent.obtain(); |
| 47 | link_objects[i] = .{ | 34 | defer cache.deinit(); |
| 48 | .path = compiler_rt_lib.crt_object_files[i].?.full_object_path, | 35 | |
| 49 | .must_link = true, | 36 | cache.hash.add(sources.len); |
| 50 | }; | 37 | for (sources) |source| { |
| | 38 | const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{source}); |
| | 39 | _ = try cache.addFile(full_path, null); |
| 51 | } | 40 | } |
| 52 | | 41 | |
| 53 | const root_name = "compiler_rt"; | 42 | cache.hash.addBytes(build_options.version); |
| | 43 | cache.hash.addBytes(comp.zig_lib_directory.path orelse "."); |
| | 44 | cache.hash.add(target.cpu.arch); |
| | 45 | cache.hash.add(target.os.tag); |
| | 46 | cache.hash.add(target.abi); |
| 54 | | 47 | |
| 55 | var lib_progress_node = progress_node.start(root_name, 0); | 48 | const hit = try cache.hit(); |
| 56 | lib_progress_node.activate(); | 49 | const digest = cache.final(); |
| 57 | defer lib_progress_node.end(); | 50 | const o_sub_path = try std.fs.path.join(arena, &[_][]const u8{ "o", &digest }); |
| 58 | | 51 | |
| 59 | const target = comp.getTarget(); | 52 | var o_directory: Compilation.Directory = .{ |
| 60 | const basename = try std.zig.binNameAlloc(comp.gpa, .{ | 53 | .handle = try comp.global_cache_directory.handle.makeOpenPath(o_sub_path, .{}), |
| | 54 | .path = try std.fs.path.join(arena, &[_][]const u8{ comp.global_cache_directory.path.?, o_sub_path }), |
| | 55 | }; |
| | 56 | defer o_directory.handle.close(); |
| | 57 | |
| | 58 | const ok_basename = "ok"; |
| | 59 | const actual_hit = if (hit) blk: { |
| | 60 | o_directory.handle.access(ok_basename, .{}) catch |err| switch (err) { |
| | 61 | error.FileNotFound => break :blk false, |
| | 62 | else => |e| return e, |
| | 63 | }; |
| | 64 | break :blk true; |
| | 65 | } else false; |
| | 66 | |
| | 67 | const root_name = "compiler_rt"; |
| | 68 | const basename = try std.zig.binNameAlloc(arena, .{ |
| 61 | .root_name = root_name, | 69 | .root_name = root_name, |
| 62 | .target = target, | 70 | .target = target, |
| 63 | .output_mode = .Lib, | 71 | .output_mode = .Lib, |
| 64 | }); | 72 | }); |
| 65 | errdefer comp.gpa.free(basename); | | |
| 66 | | 73 | |
| 67 | // TODO: This is extracted into a local variable to work around a stage1 miscompilation. | 74 | if (!actual_hit) { |
| 68 | const emit_bin = Compilation.EmitLoc{ | 75 | var progress: std.Progress = .{ .dont_print_on_dumb = true }; |
| 69 | .directory = null, // Put it in the cache directory. | 76 | var progress_node = progress.start("Compile Compiler-RT", sources.len + 1); |
| 70 | .basename = basename, | 77 | defer progress_node.end(); |
| 71 | }; | 78 | if (comp.color == .off) progress.terminal = null; |
| 72 | const sub_compilation = try Compilation.create(comp.gpa, .{ | 79 | |
| 73 | .local_cache_directory = comp.global_cache_directory, | 80 | progress_node.activate(); |
| 74 | .global_cache_directory = comp.global_cache_directory, | 81 | |
| 75 | .zig_lib_directory = comp.zig_lib_directory, | 82 | var link_objects: [sources.len]LinkObject = undefined; |
| 76 | .cache_mode = .whole, | 83 | for (sources) |source, i| { |
| 77 | .target = target, | 84 | var obj_progress_node = progress_node.start(source, 0); |
| 78 | .root_name = root_name, | 85 | obj_progress_node.activate(); |
| 79 | .main_pkg = null, | 86 | defer obj_progress_node.end(); |
| 80 | .output_mode = .Lib, | 87 | |
| 81 | .link_mode = .Static, | 88 | var tmp_crt_file: ?CRTFile = null; |
| 82 | .function_sections = true, | 89 | defer if (tmp_crt_file) |*crt| crt.deinit(comp.gpa); |
| 83 | .thread_pool = comp.thread_pool, | 90 | try comp.buildOutputFromZig(source, .Obj, &tmp_crt_file, .compiler_rt); |
| 84 | .libc_installation = comp.bin_file.options.libc_installation, | 91 | link_objects[i] = .{ |
| 85 | .emit_bin = emit_bin, | 92 | .path = try arena.dupe(u8, tmp_crt_file.?.full_object_path), |
| 86 | .optimize_mode = comp.compilerRtOptMode(), | 93 | .must_link = true, |
| 87 | .want_sanitize_c = false, | 94 | }; |
| 88 | .want_stack_check = false, | 95 | } |
| 89 | .want_red_zone = comp.bin_file.options.red_zone, | 96 | |
| 90 | .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer, | 97 | var lib_progress_node = progress_node.start(root_name, 0); |
| 91 | .want_valgrind = false, | 98 | lib_progress_node.activate(); |
| 92 | .want_tsan = false, | 99 | defer lib_progress_node.end(); |
| 93 | .want_pic = comp.bin_file.options.pic, | 100 | |
| 94 | .want_pie = comp.bin_file.options.pie, | 101 | // TODO: This is extracted into a local variable to work around a stage1 miscompilation. |
| 95 | .want_lto = comp.bin_file.options.lto, | 102 | const emit_bin = Compilation.EmitLoc{ |
| 96 | .emit_h = null, | 103 | .directory = o_directory, // Put it in the cache directory. |
| 97 | .strip = comp.compilerRtStrip(), | 104 | .basename = basename, |
| 98 | .is_native_os = comp.bin_file.options.is_native_os, | 105 | }; |
| 99 | .is_native_abi = comp.bin_file.options.is_native_abi, | 106 | const sub_compilation = try Compilation.create(comp.gpa, .{ |
| 100 | .self_exe_path = comp.self_exe_path, | 107 | .local_cache_directory = comp.global_cache_directory, |
| 101 | .link_objects = &link_objects, | 108 | .global_cache_directory = comp.global_cache_directory, |
| 102 | .verbose_cc = comp.verbose_cc, | 109 | .zig_lib_directory = comp.zig_lib_directory, |
| 103 | .verbose_link = comp.bin_file.options.verbose_link, | 110 | .cache_mode = .whole, |
| 104 | .verbose_air = comp.verbose_air, | 111 | .target = target, |
| 105 | .verbose_llvm_ir = comp.verbose_llvm_ir, | 112 | .root_name = root_name, |
| 106 | .verbose_cimport = comp.verbose_cimport, | 113 | .main_pkg = null, |
| 107 | .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features, | 114 | .output_mode = .Lib, |
| 108 | .clang_passthrough_mode = comp.clang_passthrough_mode, | 115 | .link_mode = .Static, |
| 109 | .skip_linker_dependencies = true, | 116 | .function_sections = true, |
| 110 | .parent_compilation_link_libc = comp.bin_file.options.link_libc, | 117 | .thread_pool = comp.thread_pool, |
| 111 | }); | 118 | .libc_installation = comp.bin_file.options.libc_installation, |
| 112 | defer sub_compilation.destroy(); | 119 | .emit_bin = emit_bin, |
| | 120 | .optimize_mode = comp.compilerRtOptMode(), |
| | 121 | .want_sanitize_c = false, |
| | 122 | .want_stack_check = false, |
| | 123 | .want_red_zone = comp.bin_file.options.red_zone, |
| | 124 | .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer, |
| | 125 | .want_valgrind = false, |
| | 126 | .want_tsan = false, |
| | 127 | .want_pic = comp.bin_file.options.pic, |
| | 128 | .want_pie = comp.bin_file.options.pie, |
| | 129 | .want_lto = comp.bin_file.options.lto, |
| | 130 | .emit_h = null, |
| | 131 | .strip = comp.compilerRtStrip(), |
| | 132 | .is_native_os = comp.bin_file.options.is_native_os, |
| | 133 | .is_native_abi = comp.bin_file.options.is_native_abi, |
| | 134 | .self_exe_path = comp.self_exe_path, |
| | 135 | .link_objects = &link_objects, |
| | 136 | .verbose_cc = comp.verbose_cc, |
| | 137 | .verbose_link = comp.bin_file.options.verbose_link, |
| | 138 | .verbose_air = comp.verbose_air, |
| | 139 | .verbose_llvm_ir = comp.verbose_llvm_ir, |
| | 140 | .verbose_cimport = comp.verbose_cimport, |
| | 141 | .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features, |
| | 142 | .clang_passthrough_mode = comp.clang_passthrough_mode, |
| | 143 | .skip_linker_dependencies = true, |
| | 144 | .parent_compilation_link_libc = comp.bin_file.options.link_libc, |
| | 145 | }); |
| | 146 | defer sub_compilation.destroy(); |
| | 147 | |
| | 148 | try sub_compilation.updateSubCompilation(); |
| | 149 | |
| | 150 | if (o_directory.handle.createFile(ok_basename, .{})) |file| { |
| | 151 | file.close(); |
| | 152 | } else |err| { |
| | 153 | std.log.warn("compiler-rt lib: failed to mark completion: {s}", .{@errorName(err)}); |
| | 154 | } |
| | 155 | } |
| 113 | | 156 | |
| 114 | try sub_compilation.updateSubCompilation(); | 157 | try cache.writeManifest(); |
| 115 | | 158 | |
| 116 | compiler_rt_lib.crt_lib_file = .{ | 159 | assert(compiler_rt_lib.* == null); |
| 117 | .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{ | 160 | compiler_rt_lib.* = .{ |
| 118 | sub_compilation.bin_file.options.emit.?.sub_path, | 161 | .full_object_path = try std.fs.path.join(comp.gpa, &[_][]const u8{ |
| | 162 | comp.global_cache_directory.path.?, |
| | 163 | o_sub_path, |
| | 164 | basename, |
| 119 | }), | 165 | }), |
| 120 | .lock = sub_compilation.bin_file.toOwnedLock(), | 166 | .lock = cache.toOwnedLock(), |
| 121 | }; | 167 | }; |
| 122 | } | 168 | } |
| 123 | | 169 | |