Skip to content

Commit 0e9eed4

Browse files
committed
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
1 parent e9e91b4 commit 0e9eed4

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/Cache.zig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const testing = std.testing;
1111
const mem = std.mem;
1212
const fmt = std.fmt;
1313
const Allocator = std.mem.Allocator;
14+
const Compilation = @import("Compilation.zig");
1415

1516
/// Be sure to call `Manifest.deinit` after successful initialization.
1617
pub fn obtain(cache: *const Cache) Manifest {
@@ -61,7 +62,7 @@ pub const File = struct {
6162
pub const HashHelper = struct {
6263
hasher: Hasher = hasher_init,
6364

64-
const EmitLoc = @import("Compilation.zig").EmitLoc;
65+
const EmitLoc = Compilation.EmitLoc;
6566

6667
/// Record a slice of bytes as an dependency of the process being cached
6768
pub fn addBytes(hh: *HashHelper, bytes: []const u8) void {
@@ -220,6 +221,24 @@ pub const Manifest = struct {
220221
return idx;
221222
}
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+
223242
pub fn addOptionalFile(self: *Manifest, optional_file_path: ?[]const u8) !void {
224243
self.hash.add(optional_file_path != null);
225244
const file_path = optional_file_path orelse return;

src/Compilation.zig

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,23 +2261,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_comp_progress_node: *
22612261

22622262
man.hash.add(comp.clang_preprocessor_mode);
22632263

2264-
_ = try man.addFile(c_object.src.src_path, null);
2265-
{
2266-
// Hash the extra flags, with special care to call addFile for file parameters.
2267-
// TODO this logic can likely be improved by utilizing clang_options_data.zig.
2268-
const file_args = [_][]const u8{"-include"};
2269-
var arg_i: usize = 0;
2270-
while (arg_i < c_object.src.extra_flags.len) : (arg_i += 1) {
2271-
const arg = c_object.src.extra_flags[arg_i];
2272-
man.hash.addBytes(arg);
2273-
for (file_args) |file_arg| {
2274-
if (mem.eql(u8, file_arg, arg) and arg_i + 1 < c_object.src.extra_flags.len) {
2275-
arg_i += 1;
2276-
_ = try man.addFile(c_object.src.extra_flags[arg_i], null);
2277-
}
2278-
}
2279-
}
2280-
}
2264+
try man.hashCSource(c_object.src);
22812265

22822266
{
22832267
const is_collision = blk: {

src/main.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi
21722172
defer if (enable_cache) man.deinit();
21732173

21742174
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| {
21762176
fatal("unable to process '{s}': {s}", .{ c_source_file.src_path, @errorName(err) });
21772177
};
21782178

@@ -2202,12 +2202,16 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi
22022202
}
22032203

22042204
// Convert to null terminated args.
2205-
const new_argv_with_sentinel = try arena.alloc(?[*:0]const u8, argv.items.len + 1);
2206-
new_argv_with_sentinel[argv.items.len] = null;
2207-
const new_argv = new_argv_with_sentinel[0..argv.items.len :null];
2205+
const clang_args_len = argv.items.len + c_source_file.extra_flags.len;
2206+
const new_argv_with_sentinel = try arena.alloc(?[*:0]const u8, clang_args_len + 1);
2207+
new_argv_with_sentinel[clang_args_len] = null;
2208+
const new_argv = new_argv_with_sentinel[0..clang_args_len :null];
22082209
for (argv.items) |arg, i| {
22092210
new_argv[i] = try arena.dupeZ(u8, arg);
22102211
}
2212+
for (c_source_file.extra_flags) |arg, i| {
2213+
new_argv[argv.items.len + i] = try arena.dupeZ(u8, arg);
2214+
}
22112215

22122216
const c_headers_dir_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{"include"});
22132217
const c_headers_dir_path_z = try arena.dupeZ(u8, c_headers_dir_path);

0 commit comments

Comments
 (0)