Skip to content

Commit 085cc54

Browse files
papercloverandrewrk
authored andcommitted
replace TranslateC.addIncludeDir with variants with LazyPath/library names
1 parent 0e876a6 commit 085cc54

File tree

2 files changed

+82
-36
lines changed

2 files changed

+82
-36
lines changed

lib/std/Build/Module.zig

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,44 @@ pub const IncludeDir = union(enum) {
135135
framework_path_system: LazyPath,
136136
other_step: *Step.Compile,
137137
config_header_step: *Step.ConfigHeader,
138+
139+
pub fn appendZigProcessFlags(
140+
include_dir: IncludeDir,
141+
b: *std.Build,
142+
zig_args: *std.ArrayList([]const u8),
143+
asking_step: ?*Step,
144+
) !void {
145+
switch (include_dir) {
146+
.path => |include_path| {
147+
try zig_args.appendSlice(&.{ "-I", include_path.getPath2(b, asking_step) });
148+
},
149+
.path_system => |include_path| {
150+
try zig_args.appendSlice(&.{ "-isystem", include_path.getPath2(b, asking_step) });
151+
},
152+
.path_after => |include_path| {
153+
try zig_args.appendSlice(&.{ "-idirafter", include_path.getPath2(b, asking_step) });
154+
},
155+
.framework_path => |include_path| {
156+
try zig_args.appendSlice(&.{ "-F", include_path.getPath2(b, asking_step) });
157+
},
158+
.framework_path_system => |include_path| {
159+
try zig_args.appendSlice(&.{ "-iframework", include_path.getPath2(b, asking_step) });
160+
},
161+
.other_step => |other| {
162+
if (other.generated_h) |header| {
163+
try zig_args.appendSlice(&.{ "-isystem", std.fs.path.dirname(header.getPath()).? });
164+
}
165+
if (other.installed_headers_include_tree) |include_tree| {
166+
try zig_args.appendSlice(&.{ "-I", include_tree.generated_directory.getPath() });
167+
}
168+
},
169+
.config_header_step => |config_header| {
170+
const full_file_path = config_header.output_file.getPath();
171+
const header_dir_path = full_file_path[0 .. full_file_path.len - config_header.include_path.len];
172+
try zig_args.appendSlice(&.{ "-I", header_dir_path });
173+
},
174+
}
175+
}
138176
};
139177

140178
pub const LinkFrameworkOptions = struct {
@@ -690,36 +728,7 @@ pub fn appendZigProcessFlags(
690728
}
691729

692730
for (m.include_dirs.items) |include_dir| {
693-
switch (include_dir) {
694-
.path => |include_path| {
695-
try zig_args.appendSlice(&.{ "-I", include_path.getPath2(b, asking_step) });
696-
},
697-
.path_system => |include_path| {
698-
try zig_args.appendSlice(&.{ "-isystem", include_path.getPath2(b, asking_step) });
699-
},
700-
.path_after => |include_path| {
701-
try zig_args.appendSlice(&.{ "-idirafter", include_path.getPath2(b, asking_step) });
702-
},
703-
.framework_path => |include_path| {
704-
try zig_args.appendSlice(&.{ "-F", include_path.getPath2(b, asking_step) });
705-
},
706-
.framework_path_system => |include_path| {
707-
try zig_args.appendSlice(&.{ "-iframework", include_path.getPath2(b, asking_step) });
708-
},
709-
.other_step => |other| {
710-
if (other.generated_h) |header| {
711-
try zig_args.appendSlice(&.{ "-isystem", std.fs.path.dirname(header.getPath()).? });
712-
}
713-
if (other.installed_headers_include_tree) |include_tree| {
714-
try zig_args.appendSlice(&.{ "-I", include_tree.generated_directory.getPath() });
715-
}
716-
},
717-
.config_header_step => |config_header| {
718-
const full_file_path = config_header.output_file.getPath();
719-
const header_dir_path = full_file_path[0 .. full_file_path.len - config_header.include_path.len];
720-
try zig_args.appendSlice(&.{ "-I", header_dir_path });
721-
},
722-
}
731+
try include_dir.appendZigProcessFlags(b, zig_args, asking_step);
723732
}
724733

725734
try zig_args.appendSlice(m.c_macros.items);

lib/std/Build/Step/TranslateC.zig

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const std = @import("std");
22
const Step = std.Build.Step;
3+
const LazyPath = std.Build.LazyPath;
34
const fs = std.fs;
45
const mem = std.mem;
56

@@ -9,7 +10,7 @@ pub const base_id: Step.Id = .translate_c;
910

1011
step: Step,
1112
source: std.Build.LazyPath,
12-
include_dirs: std.ArrayList([]const u8),
13+
include_dirs: std.ArrayList(std.Build.Module.IncludeDir),
1314
c_macros: std.ArrayList([]const u8),
1415
out_basename: []const u8,
1516
target: std.Build.ResolvedTarget,
@@ -37,7 +38,7 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {
3738
.makeFn = make,
3839
}),
3940
.source = source,
40-
.include_dirs = std.ArrayList([]const u8).init(owner.allocator),
41+
.include_dirs = std.ArrayList(std.Build.Module.IncludeDir).init(owner.allocator),
4142
.c_macros = std.ArrayList([]const u8).init(owner.allocator),
4243
.out_basename = undefined,
4344
.target = options.target,
@@ -95,8 +96,45 @@ pub fn createModule(translate_c: *TranslateC) *std.Build.Module {
9596
});
9697
}
9798

98-
pub fn addIncludeDir(translate_c: *TranslateC, include_dir: []const u8) void {
99-
translate_c.include_dirs.append(translate_c.step.owner.dupePath(include_dir)) catch @panic("OOM");
99+
pub fn addAfterIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
100+
const b = translate_c.step.owner;
101+
translate_c.include_dirs.append(.{ .path_after = lazy_path.dupe(b) }) catch
102+
@panic("OOM");
103+
lazy_path.addStepDependencies(&translate_c.step);
104+
}
105+
106+
pub fn addSystemIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
107+
const b = translate_c.step.owner;
108+
translate_c.include_dirs.append(.{ .path_system = lazy_path.dupe(b) }) catch
109+
@panic("OOM");
110+
lazy_path.addStepDependencies(&translate_c.step);
111+
}
112+
113+
pub fn addIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
114+
const b = translate_c.step.owner;
115+
translate_c.include_dirs.append(.{ .path = lazy_path.dupe(b) }) catch
116+
@panic("OOM");
117+
lazy_path.addStepDependencies(&translate_c.step);
118+
}
119+
120+
pub fn addConfigHeader(translate_c: *TranslateC, config_header: *Step.ConfigHeader) void {
121+
translate_c.include_dirs.append(.{ .config_header_step = config_header }) catch
122+
@panic("OOM");
123+
translate_c.step.dependOn(&config_header.step);
124+
}
125+
126+
pub fn addSystemFrameworkPath(translate_c: *TranslateC, directory_path: LazyPath) void {
127+
const b = translate_c.step.owner;
128+
translate_c.include_dirs.append(.{ .framework_path_system = directory_path.dupe(b) }) catch
129+
@panic("OOM");
130+
directory_path.addStepDependencies(&translate_c.step);
131+
}
132+
133+
pub fn addFrameworkPath(translate_c: *TranslateC, directory_path: LazyPath) void {
134+
const b = translate_c.step.owner;
135+
translate_c.include_dirs.append(.{ .framework_path = directory_path.dupe(b) }) catch
136+
@panic("OOM");
137+
directory_path.addStepDependencies(&translate_c.step);
100138
}
101139

102140
pub fn addCheckFile(translate_c: *TranslateC, expected_matches: []const []const u8) *Step.CheckFile {
@@ -147,8 +185,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
147185
}
148186

149187
for (translate_c.include_dirs.items) |include_dir| {
150-
try argv_list.append("-I");
151-
try argv_list.append(include_dir);
188+
try include_dir.appendZigProcessFlags(b, &argv_list, step);
152189
}
153190

154191
for (translate_c.c_macros.items) |c_macro| {

0 commit comments

Comments
 (0)