| ... | ... | @@ -1,123 +1,169 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | const build_options = @import("build_options"); |
| 3 | 4 | const Allocator = std.mem.Allocator; |
| 5 | const assert = std.debug.assert; |
| 4 | 6 | const mem = std.mem; |
| 5 | 7 | const tracy = @import("tracy.zig"); |
| 6 | 8 | const trace = tracy.trace; |
| 7 | 9 | |
| 10 | const Cache = @import("Cache.zig"); |
| 8 | 11 | const Compilation = @import("Compilation.zig"); |
| 9 | 12 | const CRTFile = Compilation.CRTFile; |
| 10 | 13 | const LinkObject = Compilation.LinkObject; |
| 11 | 14 | const Package = @import("Package.zig"); |
| 12 | 15 | |
| 13 | | pub const CompilerRtLib = struct { |
| 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 { |
| 16 | pub fn buildCompilerRtLib(comp: *Compilation, compiler_rt_lib: *?CRTFile) !void { |
| 30 | 17 | const tracy_trace = trace(@src()); |
| 31 | 18 | defer tracy_trace.end(); |
| 32 | 19 | |
| 33 | | var progress: std.Progress = .{ .dont_print_on_dumb = true }; |
| 34 | | var progress_node = progress.start("Compile Compiler-RT", sources.len + 1); |
| 35 | | defer progress_node.end(); |
| 36 | | if (comp.color == .off) progress.terminal = null; |
| 20 | var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa); |
| 21 | defer arena_allocator.deinit(); |
| 22 | const arena = arena_allocator.allocator(); |
| 37 | 23 | |
| 38 | | progress_node.activate(); |
| 24 | const target = comp.getTarget(); |
| 39 | 25 | |
| 40 | | var link_objects: [sources.len]LinkObject = undefined; |
| 41 | | for (sources) |source, i| { |
| 42 | | var obj_progress_node = progress_node.start(source, 0); |
| 43 | | obj_progress_node.activate(); |
| 44 | | defer obj_progress_node.end(); |
| 26 | // Use the global cache directory. |
| 27 | var cache_parent: Cache = .{ |
| 28 | .gpa = comp.gpa, |
| 29 | .manifest_dir = try comp.global_cache_directory.handle.makeOpenPath("h", .{}), |
| 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); |
| 47 | | link_objects[i] = .{ |
| 48 | | .path = compiler_rt_lib.crt_object_files[i].?.full_object_path, |
| 49 | | .must_link = true, |
| 50 | | }; |
| 33 | var cache = cache_parent.obtain(); |
| 34 | defer cache.deinit(); |
| 35 | |
| 36 | cache.hash.add(sources.len); |
| 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); |
| 56 | | lib_progress_node.activate(); |
| 57 | | defer lib_progress_node.end(); |
| 48 | const hit = try cache.hit(); |
| 49 | const digest = cache.final(); |
| 50 | const o_sub_path = try std.fs.path.join(arena, &[_][]const u8{ "o", &digest }); |
| 58 | 51 | |
| 59 | | const target = comp.getTarget(); |
| 60 | | const basename = try std.zig.binNameAlloc(comp.gpa, .{ |
| 52 | var o_directory: Compilation.Directory = .{ |
| 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 | 69 | .root_name = root_name, |
| 62 | 70 | .target = target, |
| 63 | 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. |
| 68 | | const emit_bin = Compilation.EmitLoc{ |
| 69 | | .directory = null, // Put it in the cache directory. |
| 70 | | .basename = basename, |
| 71 | | }; |
| 72 | | const sub_compilation = try Compilation.create(comp.gpa, .{ |
| 73 | | .local_cache_directory = comp.global_cache_directory, |
| 74 | | .global_cache_directory = comp.global_cache_directory, |
| 75 | | .zig_lib_directory = comp.zig_lib_directory, |
| 76 | | .cache_mode = .whole, |
| 77 | | .target = target, |
| 78 | | .root_name = root_name, |
| 79 | | .main_pkg = null, |
| 80 | | .output_mode = .Lib, |
| 81 | | .link_mode = .Static, |
| 82 | | .function_sections = true, |
| 83 | | .thread_pool = comp.thread_pool, |
| 84 | | .libc_installation = comp.bin_file.options.libc_installation, |
| 85 | | .emit_bin = emit_bin, |
| 86 | | .optimize_mode = comp.compilerRtOptMode(), |
| 87 | | .want_sanitize_c = false, |
| 88 | | .want_stack_check = false, |
| 89 | | .want_red_zone = comp.bin_file.options.red_zone, |
| 90 | | .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer, |
| 91 | | .want_valgrind = false, |
| 92 | | .want_tsan = false, |
| 93 | | .want_pic = comp.bin_file.options.pic, |
| 94 | | .want_pie = comp.bin_file.options.pie, |
| 95 | | .want_lto = comp.bin_file.options.lto, |
| 96 | | .emit_h = null, |
| 97 | | .strip = comp.compilerRtStrip(), |
| 98 | | .is_native_os = comp.bin_file.options.is_native_os, |
| 99 | | .is_native_abi = comp.bin_file.options.is_native_abi, |
| 100 | | .self_exe_path = comp.self_exe_path, |
| 101 | | .link_objects = &link_objects, |
| 102 | | .verbose_cc = comp.verbose_cc, |
| 103 | | .verbose_link = comp.bin_file.options.verbose_link, |
| 104 | | .verbose_air = comp.verbose_air, |
| 105 | | .verbose_llvm_ir = comp.verbose_llvm_ir, |
| 106 | | .verbose_cimport = comp.verbose_cimport, |
| 107 | | .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features, |
| 108 | | .clang_passthrough_mode = comp.clang_passthrough_mode, |
| 109 | | .skip_linker_dependencies = true, |
| 110 | | .parent_compilation_link_libc = comp.bin_file.options.link_libc, |
| 111 | | }); |
| 112 | | defer sub_compilation.destroy(); |
| 74 | if (!actual_hit) { |
| 75 | var progress: std.Progress = .{ .dont_print_on_dumb = true }; |
| 76 | var progress_node = progress.start("Compile Compiler-RT", sources.len + 1); |
| 77 | defer progress_node.end(); |
| 78 | if (comp.color == .off) progress.terminal = null; |
| 79 | |
| 80 | progress_node.activate(); |
| 81 | |
| 82 | var link_objects: [sources.len]LinkObject = undefined; |
| 83 | for (sources) |source, i| { |
| 84 | var obj_progress_node = progress_node.start(source, 0); |
| 85 | obj_progress_node.activate(); |
| 86 | defer obj_progress_node.end(); |
| 87 | |
| 88 | var tmp_crt_file: ?CRTFile = null; |
| 89 | defer if (tmp_crt_file) |*crt| crt.deinit(comp.gpa); |
| 90 | try comp.buildOutputFromZig(source, .Obj, &tmp_crt_file, .compiler_rt); |
| 91 | link_objects[i] = .{ |
| 92 | .path = try arena.dupe(u8, tmp_crt_file.?.full_object_path), |
| 93 | .must_link = true, |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | var lib_progress_node = progress_node.start(root_name, 0); |
| 98 | lib_progress_node.activate(); |
| 99 | defer lib_progress_node.end(); |
| 100 | |
| 101 | // TODO: This is extracted into a local variable to work around a stage1 miscompilation. |
| 102 | const emit_bin = Compilation.EmitLoc{ |
| 103 | .directory = o_directory, // Put it in the cache directory. |
| 104 | .basename = basename, |
| 105 | }; |
| 106 | const sub_compilation = try Compilation.create(comp.gpa, .{ |
| 107 | .local_cache_directory = comp.global_cache_directory, |
| 108 | .global_cache_directory = comp.global_cache_directory, |
| 109 | .zig_lib_directory = comp.zig_lib_directory, |
| 110 | .cache_mode = .whole, |
| 111 | .target = target, |
| 112 | .root_name = root_name, |
| 113 | .main_pkg = null, |
| 114 | .output_mode = .Lib, |
| 115 | .link_mode = .Static, |
| 116 | .function_sections = true, |
| 117 | .thread_pool = comp.thread_pool, |
| 118 | .libc_installation = comp.bin_file.options.libc_installation, |
| 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 = .{ |
| 117 | | .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{ |
| 118 | | sub_compilation.bin_file.options.emit.?.sub_path, |
| 159 | assert(compiler_rt_lib.* == null); |
| 160 | compiler_rt_lib.* = .{ |
| 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 | |