authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-04-30 23:07:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-12 18:17:45-04:00
log19aab5302c71edccae7959065062fd1695a396aa
tree942792e93fde35bbb7c0db611578b690f29e3b6a
parent43da1029200f8f8fead6bfe3995d4fb9549293fc

translate-c: Ensure extra_cflags are passed to clang

Additionally ensure that the Zig cache incorporates any extra cflags when using translate-c. Fixes the issue identified in #8662

3 files changed, 29 insertions(+), 22 deletions(-)

src/Cache.zig+20-1
...@@ -11,6 +11,7 @@ const testing = std.testing;...@@ -11,6 +11,7 @@ const testing = std.testing;
11const mem = std.mem;11const mem = std.mem;
12const fmt = std.fmt;12const fmt = std.fmt;
13const Allocator = std.mem.Allocator;13const Allocator = std.mem.Allocator;
14const Compilation = @import("Compilation.zig");
1415
15/// Be sure to call `Manifest.deinit` after successful initialization.16/// Be sure to call `Manifest.deinit` after successful initialization.
16pub fn obtain(cache: *const Cache) Manifest {17pub fn obtain(cache: *const Cache) Manifest {
...@@ -61,7 +62,7 @@ pub const File = struct {...@@ -61,7 +62,7 @@ pub const File = struct {
61pub const HashHelper = struct {62pub const HashHelper = struct {
62 hasher: Hasher = hasher_init,63 hasher: Hasher = hasher_init,
6364
64 const EmitLoc = @import("Compilation.zig").EmitLoc;65 const EmitLoc = Compilation.EmitLoc;
6566
66 /// Record a slice of bytes as an dependency of the process being cached67 /// Record a slice of bytes as an dependency of the process being cached
67 pub fn addBytes(hh: *HashHelper, bytes: []const u8) void {68 pub fn addBytes(hh: *HashHelper, bytes: []const u8) void {
...@@ -220,6 +221,24 @@ pub const Manifest = struct {...@@ -220,6 +221,24 @@ pub const Manifest = struct {
220 return idx;221 return idx;
221 }222 }
222223
224 pub fn hashCSource(self: *Manifest, c_source: Compilation.CSourceFile) !void {
225 _ = try self.addFile(c_source.src_path, null);
226 // Hash the extra flags, with special care to call addFile for file parameters.
227 // TODO this logic can likely be improved by utilizing clang_options_data.zig.
228 const file_args = [_][]const u8{"-include"};
229 var arg_i: usize = 0;
230 while (arg_i < c_source.extra_flags.len) : (arg_i += 1) {
231 const arg = c_source.extra_flags[arg_i];
232 self.hash.addBytes(arg);
233 for (file_args) |file_arg| {
234 if (mem.eql(u8, file_arg, arg) and arg_i + 1 < c_source.extra_flags.len) {
235 arg_i += 1;
236 _ = try self.addFile(c_source.extra_flags[arg_i], null);
237 }
238 }
239 }
240 }
241
223 pub fn addOptionalFile(self: *Manifest, optional_file_path: ?[]const u8) !void {242 pub fn addOptionalFile(self: *Manifest, optional_file_path: ?[]const u8) !void {
224 self.hash.add(optional_file_path != null);243 self.hash.add(optional_file_path != null);
225 const file_path = optional_file_path orelse return;244 const file_path = optional_file_path orelse return;
src/Compilation.zig+1-17
...@@ -2260,23 +2260,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_comp_progress_node: *...@@ -2260,23 +2260,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_comp_progress_node: *
22602260
2261 man.hash.add(comp.clang_preprocessor_mode);2261 man.hash.add(comp.clang_preprocessor_mode);
22622262
2263 _ = try man.addFile(c_object.src.src_path, null);2263 try man.hashCSource(c_object.src);
2264 {
2265 // Hash the extra flags, with special care to call addFile for file parameters.
2266 // TODO this logic can likely be improved by utilizing clang_options_data.zig.
2267 const file_args = [_][]const u8{"-include"};
2268 var arg_i: usize = 0;
2269 while (arg_i < c_object.src.extra_flags.len) : (arg_i += 1) {
2270 const arg = c_object.src.extra_flags[arg_i];
2271 man.hash.addBytes(arg);
2272 for (file_args) |file_arg| {
2273 if (mem.eql(u8, file_arg, arg) and arg_i + 1 < c_object.src.extra_flags.len) {
2274 arg_i += 1;
2275 _ = try man.addFile(c_object.src.extra_flags[arg_i], null);
2276 }
2277 }
2278 }
2279 }
22802264
2281 {2265 {
2282 const is_collision = blk: {2266 const is_collision = blk: {
src/main.zig+8-4
...@@ -2172,7 +2172,7 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi...@@ -2172,7 +2172,7 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi
2172 defer if (enable_cache) man.deinit();2172 defer if (enable_cache) man.deinit();
21732173
2174 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects2174 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
2175 _ = man.addFile(c_source_file.src_path, null) catch |err| {2175 man.hashCSource(c_source_file) catch |err| {
2176 fatal("unable to process '{s}': {s}", .{ c_source_file.src_path, @errorName(err) });2176 fatal("unable to process '{s}': {s}", .{ c_source_file.src_path, @errorName(err) });
2177 };2177 };
21782178
...@@ -2202,12 +2202,16 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi...@@ -2202,12 +2202,16 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi
2202 }2202 }
22032203
2204 // Convert to null terminated args.2204 // Convert to null terminated args.
2205 const new_argv_with_sentinel = try arena.alloc(?[*:0]const u8, argv.items.len + 1);2205 const clang_args_len = argv.items.len + c_source_file.extra_flags.len;
2206 new_argv_with_sentinel[argv.items.len] = null;2206 const new_argv_with_sentinel = try arena.alloc(?[*:0]const u8, clang_args_len + 1);
2207 const new_argv = new_argv_with_sentinel[0..argv.items.len :null];2207 new_argv_with_sentinel[clang_args_len] = null;
2208 const new_argv = new_argv_with_sentinel[0..clang_args_len :null];
2208 for (argv.items) |arg, i| {2209 for (argv.items) |arg, i| {
2209 new_argv[i] = try arena.dupeZ(u8, arg);2210 new_argv[i] = try arena.dupeZ(u8, arg);
2210 }2211 }
2212 for (c_source_file.extra_flags) |arg, i| {
2213 new_argv[argv.items.len + i] = try arena.dupeZ(u8, arg);
2214 }
22112215
2212 const c_headers_dir_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{"include"});2216 const c_headers_dir_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{"include"});
2213 const c_headers_dir_path_z = try arena.dupeZ(u8, c_headers_dir_path);2217 const c_headers_dir_path_z = try arena.dupeZ(u8, c_headers_dir_path);