diff options
Diffstat (limited to 'src/Compilation.zig')
| -rw-r--r-- | src/Compilation.zig | 161 |
1 files changed, 4 insertions, 157 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index bf7246b8d8..6e27d339f8 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -5657,149 +5657,10 @@ pub const CImportResult = struct { /// Caller owns returned memory. pub fn cImport(comp: *Compilation, c_src: []const u8, owner_mod: *Package.Module) !CImportResult { dev.check(.translate_c_command); - - const tracy_trace = trace(@src()); - defer tracy_trace.end(); - - const cimport_zig_basename = "cimport.zig"; - - var man = comp.obtainCObjectCacheManifest(owner_mod); - defer man.deinit(); - - man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects - man.hash.addBytes(c_src); - man.hash.add(comp.config.c_frontend); - - // If the previous invocation resulted in clang errors, we will see a hit - // here with 0 files in the manifest, in which case it is actually a miss. - // We need to "unhit" in this case, to keep the digests matching. - const prev_hash_state = man.hash.peekBin(); - const actual_hit = hit: { - _ = try man.hit(); - if (man.files.entries.len == 0) { - man.unhit(prev_hash_state, 0); - break :hit false; - } - break :hit true; - }; - const digest = if (!actual_hit) digest: { - var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa); - defer arena_allocator.deinit(); - const arena = arena_allocator.allocator(); - - const tmp_digest = man.hash.peek(); - const tmp_dir_sub_path = try fs.path.join(arena, &[_][]const u8{ "o", &tmp_digest }); - var zig_cache_tmp_dir = try comp.dirs.local_cache.handle.makeOpenPath(tmp_dir_sub_path, .{}); - defer zig_cache_tmp_dir.close(); - const cimport_basename = "cimport.h"; - const out_h_path = try comp.dirs.local_cache.join(arena, &[_][]const u8{ - tmp_dir_sub_path, cimport_basename, - }); - const out_dep_path = try std.fmt.allocPrint(arena, "{s}.d", .{out_h_path}); - - try zig_cache_tmp_dir.writeFile(.{ .sub_path = cimport_basename, .data = c_src }); - if (comp.verbose_cimport) { - log.info("C import source: {s}", .{out_h_path}); - } - - var argv = std.array_list.Managed([]const u8).init(comp.gpa); - defer argv.deinit(); - - try argv.append(@tagName(comp.config.c_frontend)); // argv[0] is program name, actual args start at [1] - try comp.addTranslateCCArgs(arena, &argv, .c, out_dep_path, owner_mod); - - try argv.append(out_h_path); - - if (comp.verbose_cc) { - dump_argv(argv.items); - } - var tree = switch (comp.config.c_frontend) { - .aro => tree: { - if (true) @panic("TODO"); - break :tree undefined; - }, - .clang => tree: { - if (!build_options.have_llvm) unreachable; - const translate_c = @import("translate_c.zig"); - - // Convert to null terminated args. - const new_argv_with_sentinel = try arena.alloc(?[*:0]const u8, argv.items.len + 1); - new_argv_with_sentinel[argv.items.len] = null; - const new_argv = new_argv_with_sentinel[0..argv.items.len :null]; - for (argv.items, 0..) |arg, i| { - new_argv[i] = try arena.dupeZ(u8, arg); - } - - const c_headers_dir_path_z = try comp.dirs.zig_lib.joinZ(arena, &.{"include"}); - var errors = std.zig.ErrorBundle.empty; - errdefer errors.deinit(comp.gpa); - break :tree translate_c.translate( - comp.gpa, - new_argv.ptr, - new_argv.ptr + new_argv.len, - &errors, - c_headers_dir_path_z, - ) catch |err| switch (err) { - error.OutOfMemory => return error.OutOfMemory, - error.SemanticAnalyzeFail => { - return CImportResult{ - .digest = undefined, - .cache_hit = actual_hit, - .errors = errors, - }; - }, - }; - }, - }; - defer tree.deinit(comp.gpa); - - if (comp.verbose_cimport) { - log.info("C import .d file: {s}", .{out_dep_path}); - } - - const dep_basename = fs.path.basename(out_dep_path); - try man.addDepFilePost(zig_cache_tmp_dir, dep_basename); - switch (comp.cache_use) { - .whole => |whole| if (whole.cache_manifest) |whole_cache_manifest| { - whole.cache_manifest_mutex.lock(); - defer whole.cache_manifest_mutex.unlock(); - try whole_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename); - }, - .incremental, .none => {}, - } - - const bin_digest = man.finalBin(); - const hex_digest = Cache.binToHex(bin_digest); - const o_sub_path = "o" ++ fs.path.sep_str ++ hex_digest; - var o_dir = try comp.dirs.local_cache.handle.makeOpenPath(o_sub_path, .{}); - defer o_dir.close(); - - var out_zig_file = try o_dir.createFile(cimport_zig_basename, .{}); - defer out_zig_file.close(); - - const formatted = try tree.renderAlloc(comp.gpa); - defer comp.gpa.free(formatted); - - try out_zig_file.writeAll(formatted); - - break :digest bin_digest; - } else man.finalBin(); - - if (man.have_exclusive_lock) { - // Write the updated manifest. This is a no-op if the manifest is not dirty. Note that it is - // possible we had a hit and the manifest is dirty, for example if the file mtime changed but - // the contents were the same, we hit the cache but the manifest is dirty and we need to update - // it to prevent doing a full file content comparison the next time around. - man.writeManifest() catch |err| { - log.warn("failed to write cache manifest for C import: {s}", .{@errorName(err)}); - }; - } - - return CImportResult{ - .digest = digest, - .cache_hit = actual_hit, - .errors = std.zig.ErrorBundle.empty, - }; + _ = comp; + _ = c_src; + _ = owner_mod; + @panic("TODO execute 'zig translate-c' as a sub process and use the results"); } fn workerUpdateCObject( @@ -6739,20 +6600,6 @@ pub fn tmpFilePath(comp: Compilation, ally: Allocator, suffix: []const u8) error } } -pub fn addTranslateCCArgs( - comp: *Compilation, - arena: Allocator, - argv: *std.array_list.Managed([]const u8), - ext: FileExt, - out_dep_path: ?[]const u8, - owner_mod: *Package.Module, -) !void { - try argv.appendSlice(&.{ "-x", "c" }); - try comp.addCCArgs(arena, argv, ext, out_dep_path, owner_mod); - // This gives us access to preprocessing entities, presumably at the cost of performance. - try argv.appendSlice(&.{ "-Xclang", "-detailed-preprocessing-record" }); -} - /// Add common C compiler args between translate-c and C object compilation. pub fn addCCArgs( comp: *const Compilation, |
