Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn build(b: *std.Build) !void {

const docgen_exe = b.addExecutable(.{
.name = "docgen",
.root_source_file = .{ .path = "tools/docgen.zig" },
.root_source_file = b.path("tools/docgen.zig"),
.target = b.host,
.optimize = .Debug,
.single_threaded = single_threaded,
Expand All @@ -46,7 +46,7 @@ pub fn build(b: *std.Build) !void {
docgen_cmd.addArg("--zig-lib-dir");
docgen_cmd.addDirectoryArg(p);
}
docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" });
docgen_cmd.addFileArg(b.path("doc/langref.html.in"));
const langref_file = docgen_cmd.addOutputFileArg("langref.html");
const install_langref = b.addInstallFileWithDir(langref_file, .prefix, "doc/langref.html");
if (!skip_install_langref) {
Expand All @@ -55,9 +55,9 @@ pub fn build(b: *std.Build) !void {

const autodoc_test = b.addObject(.{
.name = "std",
.root_source_file = .{ .path = "lib/std/std.zig" },
.root_source_file = b.path("lib/std/std.zig"),
.target = target,
.zig_lib_dir = .{ .path = "lib" },
.zig_lib_dir = b.path("lib"),
.optimize = .Debug,
});
const install_std_docs = b.addInstallDirectory(.{
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn build(b: *std.Build) !void {

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

if (!skip_install_lib_files) {
b.installDirectory(.{
.source_dir = .{ .path = "lib" },
.source_dir = b.path("lib"),
.install_dir = if (flat) .prefix else .lib,
.install_subdir = if (flat) "lib" else "zig",
.exclude_extensions = &[_][]const u8{
Expand Down Expand Up @@ -552,10 +552,10 @@ pub fn build(b: *std.Build) !void {
const update_mingw_exe = b.addExecutable(.{
.name = "update_mingw",
.target = b.host,
.root_source_file = .{ .path = "tools/update_mingw.zig" },
.root_source_file = b.path("tools/update_mingw.zig"),
});
const update_mingw_run = b.addRunArtifact(update_mingw_exe);
update_mingw_run.addDirectoryArg(.{ .path = "lib" });
update_mingw_run.addDirectoryArg(b.path("lib"));
if (opt_mingw_src_path) |mingw_src_path| {
update_mingw_run.addDirectoryArg(.{ .cwd_relative = mingw_src_path });
} else {
Expand Down Expand Up @@ -606,10 +606,10 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
});
run_opt.addArtifactArg(exe);
run_opt.addArg("-o");
run_opt.addFileArg(.{ .path = "stage1/zig1.wasm" });
run_opt.addFileArg(b.path("stage1/zig1.wasm"));

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

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

const aro_module = b.createModule(.{
.root_source_file = .{ .path = "lib/compiler/aro/aro.zig" },
.root_source_file = b.path("lib/compiler/aro/aro.zig"),
});

const aro_translate_c_module = b.createModule(.{
.root_source_file = .{ .path = "lib/compiler/aro_translate_c.zig" },
.root_source_file = b.path("lib/compiler/aro_translate_c.zig"),
.imports = &.{
.{
.name = "aro",
Expand Down
29 changes: 18 additions & 11 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1546,22 +1546,22 @@ pub fn addInstallArtifact(
}

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

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

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

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

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

/// References a file or directory relative to the source root.
pub fn path(b: *Build, sub_path: []const u8) LazyPath {
assert(!fs.path.isAbsolute(sub_path));
if (fs.path.isAbsolute(sub_path)) {
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", .{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to*

sub_path,
});
}
return .{ .src_path = .{
.owner = b,
.sub_path = sub_path,
Expand Down Expand Up @@ -2315,12 +2319,15 @@ pub const LazyPath = union(enum) {
}
}

/// Duplicates the file source for a given builder.
/// Copies the internal strings.
///
/// The `b` parameter is only used for its allocator. All *Build instances
/// share the same allocator.
pub fn dupe(self: LazyPath, b: *Build) LazyPath {
return switch (self) {
.src_path => |sp| .{ .src_path = .{
.owner = sp.owner,
.sub_path = b.dupePath(sp.sub_path),
.sub_path = sp.owner.dupePath(sp.sub_path),
} },
.path => |p| .{ .path = b.dupePath(p) },
.cwd_relative => |p| .{ .cwd_relative = b.dupePath(p) },
Expand Down
15 changes: 2 additions & 13 deletions lib/std/Build/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ pub fn linkFramework(m: *Module, name: []const u8, options: LinkFrameworkOptions
pub const AddCSourceFilesOptions = struct {
/// When provided, `files` are relative to `root` rather than the
/// package that owns the `Compile` step.
root: LazyPath = .{ .path = "" },
root: ?LazyPath = null,
files: []const []const u8,
flags: []const []const u8 = &.{},
};
Expand All @@ -472,7 +472,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {

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

pub fn addRPath(m: *Module, directory_path: LazyPath) void {
const b = m.owner;
switch (directory_path) {
.path, .cwd_relative => |path| {
// TODO: remove this check after people upgrade and stop expecting it to work
if (std.mem.startsWith(u8, path, "@executable_path") or
std.mem.startsWith(u8, path, "@loader_path"))
{
@panic("this function is for adding directory paths. It does not support special rpaths. use addRPathSpecial for that.");
}
},
else => {},
}
m.rpaths.append(b.allocator, .{ .lazy_path = directory_path.dupe(b) }) catch @panic("OOM");
addLazyPathDependenciesOnly(m, directory_path);
}
Expand Down
28 changes: 2 additions & 26 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,8 @@ pub const HeaderInstallation = union(enum) {
dest_rel_path: []const u8,

pub fn dupe(self: File, b: *std.Build) File {
// 'path' lazy paths are relative to the build root of some step, inferred from the step
// in which they are used. This means that we can't dupe such paths, because they may
// come from dependencies with their own build roots and duping the paths as is might
// cause the build script to search for the file relative to the wrong root.
// As a temporary workaround, we convert build root-relative paths to absolute paths.
// If/when the build-root relative paths are updated to encode which build root they are
// relative to, this workaround should be removed.
const duped_source: LazyPath = switch (self.source) {
.path => |root_rel| .{ .cwd_relative = b.pathFromRoot(root_rel) },
else => self.source.dupe(b),
};

return .{
.source = duped_source,
.source = self.source.dupe(b),
.dest_rel_path = b.dupePath(self.dest_rel_path),
};
}
Expand Down Expand Up @@ -305,20 +293,8 @@ pub const HeaderInstallation = union(enum) {
};

pub fn dupe(self: Directory, b: *std.Build) Directory {
// 'path' lazy paths are relative to the build root of some step, inferred from the step
// in which they are used. This means that we can't dupe such paths, because they may
// come from dependencies with their own build roots and duping the paths as is might
// cause the build script to search for the file relative to the wrong root.
// As a temporary workaround, we convert build root-relative paths to absolute paths.
// If/when the build-root relative paths are updated to encode which build root they are
// relative to, this workaround should be removed.
const duped_source: LazyPath = switch (self.source) {
.path => |root_rel| .{ .cwd_relative = b.pathFromRoot(root_rel) },
else => self.source.dupe(b),
};

return .{
.source = duped_source,
.source = self.source.dupe(b),
.dest_rel_path = b.dupePath(self.dest_rel_path),
.options = self.options.dupe(b),
};
Expand Down
2 changes: 1 addition & 1 deletion test/link/bss/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {

const exe = b.addExecutable(.{
.name = "bss",
.root_source_file = .{ .path = "main.zig" },
.root_source_file = b.path("main.zig"),
.target = b.host,
.optimize = .Debug,
});
Expand Down
2 changes: 1 addition & 1 deletion test/link/common_symbols/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
});

const test_exe = b.addTest(.{
.root_source_file = .{ .path = "main.zig" },
.root_source_file = b.path("main.zig"),
.optimize = optimize,
});
test_exe.linkLibrary(lib_a);
Expand Down
2 changes: 1 addition & 1 deletion test/link/common_symbols_alignment/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
});

const test_exe = b.addTest(.{
.root_source_file = .{ .path = "main.zig" },
.root_source_file = b.path("main.zig"),
.optimize = optimize,
});
test_exe.linkLibrary(lib_a);
Expand Down
4 changes: 2 additions & 2 deletions test/link/glibc_compat/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn build(b: *std.Build) void {
.{ .arch_os_abi = t },
) catch unreachable),
});
exe.addCSourceFile(.{ .file = .{ .path = "main.c" } });
exe.addCSourceFile(.{ .file = b.path("main.c") });
exe.linkLibC();
// TODO: actually test the output
_ = exe.getEmittedBin();
Expand All @@ -45,7 +45,7 @@ pub fn build(b: *std.Build) void {

const exe = b.addExecutable(.{
.name = t,
.root_source_file = .{ .path = "glibc_runtime_check.zig" },
.root_source_file = b.path("glibc_runtime_check.zig"),
.target = target,
});
exe.linkLibC();
Expand Down
12 changes: 6 additions & 6 deletions test/link/interdependent_static_c_libs/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
.optimize = optimize,
.target = b.host,
});
lib_a.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &[_][]const u8{} });
lib_a.addIncludePath(.{ .path = "." });
lib_a.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} });
lib_a.addIncludePath(b.path("."));

const lib_b = b.addStaticLibrary(.{
.name = "b",
.optimize = optimize,
.target = b.host,
});
lib_b.addCSourceFile(.{ .file = .{ .path = "b.c" }, .flags = &[_][]const u8{} });
lib_b.addIncludePath(.{ .path = "." });
lib_b.addCSourceFile(.{ .file = b.path("b.c"), .flags = &[_][]const u8{} });
lib_b.addIncludePath(b.path("."));

const test_exe = b.addTest(.{
.root_source_file = .{ .path = "main.zig" },
.root_source_file = b.path("main.zig"),
.optimize = optimize,
});
test_exe.linkLibrary(lib_a);
test_exe.linkLibrary(lib_b);
test_exe.addIncludePath(.{ .path = "." });
test_exe.addIncludePath(b.path("."));

test_step.dependOn(&b.addRunArtifact(test_exe).step);
}
6 changes: 3 additions & 3 deletions test/link/macho.zig
Original file line number Diff line number Diff line change
Expand Up @@ -836,9 +836,9 @@ fn testLinkDirectlyCppTbd(b: *Build, opts: Options) *Step {
,
.cpp_source_flags = &.{ "-nostdlib++", "-nostdinc++" },
});
exe.root_module.addSystemIncludePath(.{ .path = b.pathJoin(&.{ sdk, "/usr/include" }) });
exe.root_module.addIncludePath(.{ .path = b.pathJoin(&.{ sdk, "/usr/include/c++/v1" }) });
exe.root_module.addObjectFile(.{ .path = b.pathJoin(&.{ sdk, "/usr/lib/libc++.tbd" }) });
exe.root_module.addSystemIncludePath(b.path(b.pathJoin(&.{ sdk, "/usr/include" })));
exe.root_module.addIncludePath(b.path(b.pathJoin(&.{ sdk, "/usr/include/c++/v1" })));
exe.root_module.addObjectFile(b.path(b.pathJoin(&.{ sdk, "/usr/lib/libc++.tbd" })));

const check = exe.checkObject();
check.checkInSymtab();
Expand Down
6 changes: 3 additions & 3 deletions test/link/static_libs_from_object_files/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
{
const exe = b.addExecutable(.{
.name = "test1",
.root_source_file = .{ .path = "main.zig" },
.root_source_file = b.path("main.zig"),
.optimize = optimize,
.target = b.host,
});
Expand Down Expand Up @@ -93,7 +93,7 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built

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

const exe = b.addExecutable(.{
.name = "test3",
.root_source_file = .{ .path = "main.zig" },
.root_source_file = b.path("main.zig"),
.target = b.host,
.optimize = optimize,
});
Expand Down
2 changes: 1 addition & 1 deletion test/link/wasm/archive/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
// and therefore link with its archive file.
const lib = b.addExecutable(.{
.name = "main",
.root_source_file = .{ .path = "main.zig" },
.root_source_file = b.path("main.zig"),
.optimize = optimize,
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
.strip = false,
Expand Down
2 changes: 1 addition & 1 deletion test/link/wasm/basic-features/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn build(b: *std.Build) void {
// Library with explicitly set cpu features
const lib = b.addExecutable(.{
.name = "lib",
.root_source_file = .{ .path = "main.zig" },
.root_source_file = b.path("main.zig"),
.optimize = .Debug,
.target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
Expand Down
4 changes: 2 additions & 2 deletions test/link/wasm/bss/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
{
const lib = b.addExecutable(.{
.name = "lib",
.root_source_file = .{ .path = "lib.zig" },
.root_source_file = b.path("lib.zig"),
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
.optimize = optimize_mode,
.strip = false,
Expand Down Expand Up @@ -64,7 +64,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
{
const lib = b.addExecutable(.{
.name = "lib",
.root_source_file = .{ .path = "lib2.zig" },
.root_source_file = b.path("lib2.zig"),
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
.optimize = optimize_mode,
.strip = false,
Expand Down
2 changes: 1 addition & 1 deletion test/link/wasm/export-data/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void {

const lib = b.addExecutable(.{
.name = "lib",
.root_source_file = .{ .path = "lib.zig" },
.root_source_file = b.path("lib.zig"),
.optimize = .ReleaseSafe, // to make the output deterministic in address positions
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
});
Expand Down
Loading