Skip to content

Commit 10ff81c

Browse files
authored
Merge pull request #19623 from ziglang/LazyPath
std.Build.LazyPath: upgrade API usages of source-relative path
2 parents 3bafc44 + 499a202 commit 10ff81c

File tree

68 files changed

+173
-203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+173
-203
lines changed

build.zig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn build(b: *std.Build) !void {
3434

3535
const docgen_exe = b.addExecutable(.{
3636
.name = "docgen",
37-
.root_source_file = .{ .path = "tools/docgen.zig" },
37+
.root_source_file = b.path("tools/docgen.zig"),
3838
.target = b.host,
3939
.optimize = .Debug,
4040
.single_threaded = single_threaded,
@@ -46,7 +46,7 @@ pub fn build(b: *std.Build) !void {
4646
docgen_cmd.addArg("--zig-lib-dir");
4747
docgen_cmd.addDirectoryArg(p);
4848
}
49-
docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" });
49+
docgen_cmd.addFileArg(b.path("doc/langref.html.in"));
5050
const langref_file = docgen_cmd.addOutputFileArg("langref.html");
5151
const install_langref = b.addInstallFileWithDir(langref_file, .prefix, "doc/langref.html");
5252
if (!skip_install_langref) {
@@ -55,9 +55,9 @@ pub fn build(b: *std.Build) !void {
5555

5656
const autodoc_test = b.addObject(.{
5757
.name = "std",
58-
.root_source_file = .{ .path = "lib/std/std.zig" },
58+
.root_source_file = b.path("lib/std/std.zig"),
5959
.target = target,
60-
.zig_lib_dir = .{ .path = "lib" },
60+
.zig_lib_dir = b.path("lib"),
6161
.optimize = .Debug,
6262
});
6363
const install_std_docs = b.addInstallDirectory(.{
@@ -86,7 +86,7 @@ pub fn build(b: *std.Build) !void {
8686

8787
const check_case_exe = b.addExecutable(.{
8888
.name = "check-case",
89-
.root_source_file = .{ .path = "test/src/Cases.zig" },
89+
.root_source_file = b.path("test/src/Cases.zig"),
9090
.target = b.host,
9191
.optimize = optimize,
9292
.single_threaded = single_threaded,
@@ -135,7 +135,7 @@ pub fn build(b: *std.Build) !void {
135135

136136
if (!skip_install_lib_files) {
137137
b.installDirectory(.{
138-
.source_dir = .{ .path = "lib" },
138+
.source_dir = b.path("lib"),
139139
.install_dir = if (flat) .prefix else .lib,
140140
.install_subdir = if (flat) "lib" else "zig",
141141
.exclude_extensions = &[_][]const u8{
@@ -552,10 +552,10 @@ pub fn build(b: *std.Build) !void {
552552
const update_mingw_exe = b.addExecutable(.{
553553
.name = "update_mingw",
554554
.target = b.host,
555-
.root_source_file = .{ .path = "tools/update_mingw.zig" },
555+
.root_source_file = b.path("tools/update_mingw.zig"),
556556
});
557557
const update_mingw_run = b.addRunArtifact(update_mingw_exe);
558-
update_mingw_run.addDirectoryArg(.{ .path = "lib" });
558+
update_mingw_run.addDirectoryArg(b.path("lib"));
559559
if (opt_mingw_src_path) |mingw_src_path| {
560560
update_mingw_run.addDirectoryArg(.{ .cwd_relative = mingw_src_path });
561561
} else {
@@ -606,10 +606,10 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
606606
});
607607
run_opt.addArtifactArg(exe);
608608
run_opt.addArg("-o");
609-
run_opt.addFileArg(.{ .path = "stage1/zig1.wasm" });
609+
run_opt.addFileArg(b.path("stage1/zig1.wasm"));
610610

611611
const copy_zig_h = b.addWriteFiles();
612-
copy_zig_h.addCopyFileToSource(.{ .path = "lib/zig.h" }, "stage1/zig.h");
612+
copy_zig_h.addCopyFileToSource(b.path("lib/zig.h"), "stage1/zig.h");
613613

614614
const update_zig1_step = b.step("update-zig1", "Update stage1/zig1.wasm");
615615
update_zig1_step.dependOn(&run_opt.step);
@@ -627,7 +627,7 @@ const AddCompilerStepOptions = struct {
627627
fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.Step.Compile {
628628
const exe = b.addExecutable(.{
629629
.name = "zig",
630-
.root_source_file = .{ .path = "src/main.zig" },
630+
.root_source_file = b.path("src/main.zig"),
631631
.target = options.target,
632632
.optimize = options.optimize,
633633
.max_rss = 7_000_000_000,
@@ -638,11 +638,11 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.St
638638
exe.stack_size = stack_size;
639639

640640
const aro_module = b.createModule(.{
641-
.root_source_file = .{ .path = "lib/compiler/aro/aro.zig" },
641+
.root_source_file = b.path("lib/compiler/aro/aro.zig"),
642642
});
643643

644644
const aro_translate_c_module = b.createModule(.{
645-
.root_source_file = .{ .path = "lib/compiler/aro_translate_c.zig" },
645+
.root_source_file = b.path("lib/compiler/aro_translate_c.zig"),
646646
.imports = &.{
647647
.{
648648
.name = "aro",

lib/std/Build.zig

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,22 +1546,22 @@ pub fn addInstallArtifact(
15461546
}
15471547

15481548
///`dest_rel_path` is relative to prefix path
1549-
pub fn installFile(self: *Build, src_path: []const u8, dest_rel_path: []const u8) void {
1550-
self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .prefix, dest_rel_path).step);
1549+
pub fn installFile(b: *Build, src_path: []const u8, dest_rel_path: []const u8) void {
1550+
b.getInstallStep().dependOn(&b.addInstallFileWithDir(b.path(src_path), .prefix, dest_rel_path).step);
15511551
}
15521552

1553-
pub fn installDirectory(self: *Build, options: Step.InstallDir.Options) void {
1554-
self.getInstallStep().dependOn(&self.addInstallDirectory(options).step);
1553+
pub fn installDirectory(b: *Build, options: Step.InstallDir.Options) void {
1554+
b.getInstallStep().dependOn(&b.addInstallDirectory(options).step);
15551555
}
15561556

15571557
///`dest_rel_path` is relative to bin path
1558-
pub fn installBinFile(self: *Build, src_path: []const u8, dest_rel_path: []const u8) void {
1559-
self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .bin, dest_rel_path).step);
1558+
pub fn installBinFile(b: *Build, src_path: []const u8, dest_rel_path: []const u8) void {
1559+
b.getInstallStep().dependOn(&b.addInstallFileWithDir(b.path(src_path), .bin, dest_rel_path).step);
15601560
}
15611561

15621562
///`dest_rel_path` is relative to lib path
1563-
pub fn installLibFile(self: *Build, src_path: []const u8, dest_rel_path: []const u8) void {
1564-
self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .lib, dest_rel_path).step);
1563+
pub fn installLibFile(b: *Build, src_path: []const u8, dest_rel_path: []const u8) void {
1564+
b.getInstallStep().dependOn(&b.addInstallFileWithDir(b.path(src_path), .lib, dest_rel_path).step);
15651565
}
15661566

15671567
pub fn addObjCopy(b: *Build, source: LazyPath, options: Step.ObjCopy.Options) *Step.ObjCopy {
@@ -1637,7 +1637,11 @@ pub fn truncateFile(self: *Build, dest_path: []const u8) !void {
16371637

16381638
/// References a file or directory relative to the source root.
16391639
pub fn path(b: *Build, sub_path: []const u8) LazyPath {
1640-
assert(!fs.path.isAbsolute(sub_path));
1640+
if (fs.path.isAbsolute(sub_path)) {
1641+
std.debug.panic("sub_path is expected to be relative to the build root, but was this absolute path: '{s}'. It is best avoid absolute paths, but if you must, it is supported by LazyPath.cwd_relative", .{
1642+
sub_path,
1643+
});
1644+
}
16411645
return .{ .src_path = .{
16421646
.owner = b,
16431647
.sub_path = sub_path,
@@ -2315,12 +2319,15 @@ pub const LazyPath = union(enum) {
23152319
}
23162320
}
23172321

2318-
/// Duplicates the file source for a given builder.
2322+
/// Copies the internal strings.
2323+
///
2324+
/// The `b` parameter is only used for its allocator. All *Build instances
2325+
/// share the same allocator.
23192326
pub fn dupe(self: LazyPath, b: *Build) LazyPath {
23202327
return switch (self) {
23212328
.src_path => |sp| .{ .src_path = .{
23222329
.owner = sp.owner,
2323-
.sub_path = b.dupePath(sp.sub_path),
2330+
.sub_path = sp.owner.dupePath(sp.sub_path),
23242331
} },
23252332
.path => |p| .{ .path = b.dupePath(p) },
23262333
.cwd_relative => |p| .{ .cwd_relative = b.dupePath(p) },

lib/std/Build/Module.zig

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub fn linkFramework(m: *Module, name: []const u8, options: LinkFrameworkOptions
451451
pub const AddCSourceFilesOptions = struct {
452452
/// When provided, `files` are relative to `root` rather than the
453453
/// package that owns the `Compile` step.
454-
root: LazyPath = .{ .path = "" },
454+
root: ?LazyPath = null,
455455
files: []const []const u8,
456456
flags: []const []const u8 = &.{},
457457
};
@@ -472,7 +472,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
472472

473473
const c_source_files = allocator.create(CSourceFiles) catch @panic("OOM");
474474
c_source_files.* = .{
475-
.root = options.root,
475+
.root = options.root orelse b.path(""),
476476
.files = b.dupeStrings(options.files),
477477
.flags = b.dupeStrings(options.flags),
478478
};
@@ -573,17 +573,6 @@ pub fn addLibraryPath(m: *Module, directory_path: LazyPath) void {
573573

574574
pub fn addRPath(m: *Module, directory_path: LazyPath) void {
575575
const b = m.owner;
576-
switch (directory_path) {
577-
.path, .cwd_relative => |path| {
578-
// TODO: remove this check after people upgrade and stop expecting it to work
579-
if (std.mem.startsWith(u8, path, "@executable_path") or
580-
std.mem.startsWith(u8, path, "@loader_path"))
581-
{
582-
@panic("this function is for adding directory paths. It does not support special rpaths. use addRPathSpecial for that.");
583-
}
584-
},
585-
else => {},
586-
}
587576
m.rpaths.append(b.allocator, .{ .lazy_path = directory_path.dupe(b) }) catch @panic("OOM");
588577
addLazyPathDependenciesOnly(m, directory_path);
589578
}

lib/std/Build/Step/Compile.zig

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -264,20 +264,8 @@ pub const HeaderInstallation = union(enum) {
264264
dest_rel_path: []const u8,
265265

266266
pub fn dupe(self: File, b: *std.Build) File {
267-
// 'path' lazy paths are relative to the build root of some step, inferred from the step
268-
// in which they are used. This means that we can't dupe such paths, because they may
269-
// come from dependencies with their own build roots and duping the paths as is might
270-
// cause the build script to search for the file relative to the wrong root.
271-
// As a temporary workaround, we convert build root-relative paths to absolute paths.
272-
// If/when the build-root relative paths are updated to encode which build root they are
273-
// relative to, this workaround should be removed.
274-
const duped_source: LazyPath = switch (self.source) {
275-
.path => |root_rel| .{ .cwd_relative = b.pathFromRoot(root_rel) },
276-
else => self.source.dupe(b),
277-
};
278-
279267
return .{
280-
.source = duped_source,
268+
.source = self.source.dupe(b),
281269
.dest_rel_path = b.dupePath(self.dest_rel_path),
282270
};
283271
}
@@ -305,20 +293,8 @@ pub const HeaderInstallation = union(enum) {
305293
};
306294

307295
pub fn dupe(self: Directory, b: *std.Build) Directory {
308-
// 'path' lazy paths are relative to the build root of some step, inferred from the step
309-
// in which they are used. This means that we can't dupe such paths, because they may
310-
// come from dependencies with their own build roots and duping the paths as is might
311-
// cause the build script to search for the file relative to the wrong root.
312-
// As a temporary workaround, we convert build root-relative paths to absolute paths.
313-
// If/when the build-root relative paths are updated to encode which build root they are
314-
// relative to, this workaround should be removed.
315-
const duped_source: LazyPath = switch (self.source) {
316-
.path => |root_rel| .{ .cwd_relative = b.pathFromRoot(root_rel) },
317-
else => self.source.dupe(b),
318-
};
319-
320296
return .{
321-
.source = duped_source,
297+
.source = self.source.dupe(b),
322298
.dest_rel_path = b.dupePath(self.dest_rel_path),
323299
.options = self.options.dupe(b),
324300
};

test/link/bss/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
66

77
const exe = b.addExecutable(.{
88
.name = "bss",
9-
.root_source_file = .{ .path = "main.zig" },
9+
.root_source_file = b.path("main.zig"),
1010
.target = b.host,
1111
.optimize = .Debug,
1212
});

test/link/common_symbols/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2222
});
2323

2424
const test_exe = b.addTest(.{
25-
.root_source_file = .{ .path = "main.zig" },
25+
.root_source_file = b.path("main.zig"),
2626
.optimize = optimize,
2727
});
2828
test_exe.linkLibrary(lib_a);

test/link/common_symbols_alignment/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2222
});
2323

2424
const test_exe = b.addTest(.{
25-
.root_source_file = .{ .path = "main.zig" },
25+
.root_source_file = b.path("main.zig"),
2626
.optimize = optimize,
2727
});
2828
test_exe.linkLibrary(lib_a);

test/link/glibc_compat/build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn build(b: *std.Build) void {
2121
.{ .arch_os_abi = t },
2222
) catch unreachable),
2323
});
24-
exe.addCSourceFile(.{ .file = .{ .path = "main.c" } });
24+
exe.addCSourceFile(.{ .file = b.path("main.c") });
2525
exe.linkLibC();
2626
// TODO: actually test the output
2727
_ = exe.getEmittedBin();
@@ -45,7 +45,7 @@ pub fn build(b: *std.Build) void {
4545

4646
const exe = b.addExecutable(.{
4747
.name = t,
48-
.root_source_file = .{ .path = "glibc_runtime_check.zig" },
48+
.root_source_file = b.path("glibc_runtime_check.zig"),
4949
.target = target,
5050
});
5151
exe.linkLibC();

test/link/interdependent_static_c_libs/build.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616
.optimize = optimize,
1717
.target = b.host,
1818
});
19-
lib_a.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &[_][]const u8{} });
20-
lib_a.addIncludePath(.{ .path = "." });
19+
lib_a.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} });
20+
lib_a.addIncludePath(b.path("."));
2121

2222
const lib_b = b.addStaticLibrary(.{
2323
.name = "b",
2424
.optimize = optimize,
2525
.target = b.host,
2626
});
27-
lib_b.addCSourceFile(.{ .file = .{ .path = "b.c" }, .flags = &[_][]const u8{} });
28-
lib_b.addIncludePath(.{ .path = "." });
27+
lib_b.addCSourceFile(.{ .file = b.path("b.c"), .flags = &[_][]const u8{} });
28+
lib_b.addIncludePath(b.path("."));
2929

3030
const test_exe = b.addTest(.{
31-
.root_source_file = .{ .path = "main.zig" },
31+
.root_source_file = b.path("main.zig"),
3232
.optimize = optimize,
3333
});
3434
test_exe.linkLibrary(lib_a);
3535
test_exe.linkLibrary(lib_b);
36-
test_exe.addIncludePath(.{ .path = "." });
36+
test_exe.addIncludePath(b.path("."));
3737

3838
test_step.dependOn(&b.addRunArtifact(test_exe).step);
3939
}

test/link/macho.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,9 +836,9 @@ fn testLinkDirectlyCppTbd(b: *Build, opts: Options) *Step {
836836
,
837837
.cpp_source_flags = &.{ "-nostdlib++", "-nostdinc++" },
838838
});
839-
exe.root_module.addSystemIncludePath(.{ .path = b.pathJoin(&.{ sdk, "/usr/include" }) });
840-
exe.root_module.addIncludePath(.{ .path = b.pathJoin(&.{ sdk, "/usr/include/c++/v1" }) });
841-
exe.root_module.addObjectFile(.{ .path = b.pathJoin(&.{ sdk, "/usr/lib/libc++.tbd" }) });
839+
exe.root_module.addSystemIncludePath(b.path(b.pathJoin(&.{ sdk, "/usr/include" })));
840+
exe.root_module.addIncludePath(b.path(b.pathJoin(&.{ sdk, "/usr/include/c++/v1" })));
841+
exe.root_module.addObjectFile(b.path(b.pathJoin(&.{ sdk, "/usr/lib/libc++.tbd" })));
842842

843843
const check = exe.checkObject();
844844
check.checkInSymtab();

test/link/static_libs_from_object_files/build.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
5757
{
5858
const exe = b.addExecutable(.{
5959
.name = "test1",
60-
.root_source_file = .{ .path = "main.zig" },
60+
.root_source_file = b.path("main.zig"),
6161
.optimize = optimize,
6262
.target = b.host,
6363
});
@@ -93,7 +93,7 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
9393

9494
const exe = b.addExecutable(.{
9595
.name = "test2",
96-
.root_source_file = .{ .path = "main.zig" },
96+
.root_source_file = b.path("main.zig"),
9797
.target = b.host,
9898
.optimize = optimize,
9999
});
@@ -134,7 +134,7 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
134134

135135
const exe = b.addExecutable(.{
136136
.name = "test3",
137-
.root_source_file = .{ .path = "main.zig" },
137+
.root_source_file = b.path("main.zig"),
138138
.target = b.host,
139139
.optimize = optimize,
140140
});

test/link/wasm/archive/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717
// and therefore link with its archive file.
1818
const lib = b.addExecutable(.{
1919
.name = "main",
20-
.root_source_file = .{ .path = "main.zig" },
20+
.root_source_file = b.path("main.zig"),
2121
.optimize = optimize,
2222
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2323
.strip = false,

test/link/wasm/basic-features/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
66
// Library with explicitly set cpu features
77
const lib = b.addExecutable(.{
88
.name = "lib",
9-
.root_source_file = .{ .path = "main.zig" },
9+
.root_source_file = b.path("main.zig"),
1010
.optimize = .Debug,
1111
.target = b.resolveTargetQuery(.{
1212
.cpu_arch = .wasm32,

test/link/wasm/bss/build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
1616
{
1717
const lib = b.addExecutable(.{
1818
.name = "lib",
19-
.root_source_file = .{ .path = "lib.zig" },
19+
.root_source_file = b.path("lib.zig"),
2020
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
2121
.optimize = optimize_mode,
2222
.strip = false,
@@ -64,7 +64,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
6464
{
6565
const lib = b.addExecutable(.{
6666
.name = "lib",
67-
.root_source_file = .{ .path = "lib2.zig" },
67+
.root_source_file = b.path("lib2.zig"),
6868
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
6969
.optimize = optimize_mode,
7070
.strip = false,

test/link/wasm/export-data/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void {
1111

1212
const lib = b.addExecutable(.{
1313
.name = "lib",
14-
.root_source_file = .{ .path = "lib.zig" },
14+
.root_source_file = b.path("lib.zig"),
1515
.optimize = .ReleaseSafe, // to make the output deterministic in address positions
1616
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
1717
});

0 commit comments

Comments
 (0)