authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2021-02-23 21:14:50+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-11 10:38:57+03:00
log07acb1ccc97329cdcc0df37234325af9b00420a3
tree26e56446411725be53958dfef4331cd6ad598e73
parent56cb0b5ca0ffc4fc2c54b9dfb0f15fb7c50dc840

Changes createExecutable parameter is_dynamic to a enum to make code more readable .


7 files changed, 77 insertions(+), 66 deletions(-)

lib/std/build.zig+61-43
......@@ -174,7 +174,7 @@ pub const Builder = struct {
174174 .is_release = false,
175175 .override_lib_dir = null,
176176 .install_path = undefined,
177 .vcpkg_root = VcpkgRoot{ .Unattempted = {} },
177 .vcpkg_root = VcpkgRoot{ .unattempted = {} },
178178 .args = null,
179179 };
180180 try self.top_level_steps.append(&self.install_tls);
......@@ -212,7 +212,7 @@ pub const Builder = struct {
212212 }
213213
214214 pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
215 return addExecutableSource(self, name, convertOptionalPathToFileSource(root_src), false);
215 return addExecutableSource(self, name, convertOptionalPathToFileSource(root_src), .static);
216216 }
217217
218218 pub const addExecutableSource = LibExeObjStep.createExecutable;
......@@ -250,7 +250,7 @@ pub const Builder = struct {
250250 }
251251
252252 pub fn addAssemble(self: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
253 return addAssembleSource(self, name, FileSource.relative(src));
253 return addAssembleSource(self, name, .{ .path = src });
254254 }
255255
256256 pub fn addAssembleSource(self: *Builder, name: []const u8, src: FileSource) *LibExeObjStep {
......@@ -876,7 +876,7 @@ pub const Builder = struct {
876876
877877 ///`dest_rel_path` is relative to prefix path
878878 pub fn installFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) void {
879 self.getInstallStep().dependOn(&self.addInstallFileWithDir(FileSource.relative(src_path), .Prefix, dest_rel_path).step);
879 self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .Prefix, dest_rel_path).step);
880880 }
881881
882882 pub fn installDirectory(self: *Builder, options: InstallDirectoryOptions) void {
......@@ -885,12 +885,12 @@ pub const Builder = struct {
885885
886886 ///`dest_rel_path` is relative to bin path
887887 pub fn installBinFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) void {
888 self.getInstallStep().dependOn(&self.addInstallFileWithDir(FileSource.relative(src_path), .Bin, dest_rel_path).step);
888 self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .Bin, dest_rel_path).step);
889889 }
890890
891891 ///`dest_rel_path` is relative to lib path
892892 pub fn installLibFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) void {
893 self.getInstallStep().dependOn(&self.addInstallFileWithDir(FileSource.relative(src_path), .Lib, dest_rel_path).step);
893 self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .Lib, dest_rel_path).step);
894894 }
895895
896896 pub fn installRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) void {
......@@ -1239,7 +1239,7 @@ pub const GeneratedFile = struct {
12391239/// A file source is a reference to an existing or future file.
12401240///
12411241pub const FileSource = union(enum) {
1242 /// A plain file path, relative to build root.
1242 /// A plain file path, relative to build root or absolute.
12431243 path: []const u8,
12441244
12451245 /// A file that is generated by an interface. Those files usually are
......@@ -1270,13 +1270,12 @@ pub const FileSource = union(enum) {
12701270 }
12711271 }
12721272
1273 /// Should only be called during make(), returns an absolute path to the file.
1273 /// Should only be called during make(), returns a path relative to the build root or absolute.
12741274 pub fn getPath(self: FileSource, builder: *Builder) []const u8 {
12751275 const path = switch (self) {
12761276 .path => |p| builder.pathFromRoot(p),
12771277 .generated => |gen| gen.getPath(),
12781278 };
1279 std.debug.assert(std.fs.path.isAbsolute(path));
12801279 return path;
12811280 }
12821281
......@@ -1307,7 +1306,7 @@ pub const LibExeObjStep = struct {
13071306 linker_script: ?FileSource = null,
13081307 version_script: ?[]const u8 = null,
13091308 out_filename: []const u8,
1310 is_dynamic: bool,
1309 linkage: Linkage,
13111310 version: ?Version,
13121311 build_mode: builtin.Mode,
13131312 kind: Kind,
......@@ -1442,9 +1441,11 @@ pub const LibExeObjStep = struct {
14421441 unversioned: void,
14431442 };
14441443
1444 pub const Linkage = enum { dynamic, static };
1445
14451446 pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource, kind: SharedLibKind) *LibExeObjStep {
14461447 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1447 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, switch (kind) {
1448 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, .dynamic, switch (kind) {
14481449 .versioned => |ver| ver,
14491450 .unversioned => null,
14501451 });
......@@ -1453,25 +1454,25 @@ pub const LibExeObjStep = struct {
14531454
14541455 pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
14551456 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1456 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, null);
1457 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, .static, null);
14571458 return self;
14581459 }
14591460
14601461 pub fn createObject(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
14611462 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1462 self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, null);
1463 self.* = initExtraArgs(builder, name, root_src, Kind.Obj, .static, null);
14631464 return self;
14641465 }
14651466
1466 pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?FileSource, is_dynamic: bool) *LibExeObjStep {
1467 pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?FileSource, linkage: Linkage) *LibExeObjStep {
14671468 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1468 self.* = initExtraArgs(builder, name, root_src, Kind.Exe, is_dynamic, null);
1469 self.* = initExtraArgs(builder, name, root_src, Kind.Exe, linkage, null);
14691470 return self;
14701471 }
14711472
14721473 pub fn createTest(builder: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {
14731474 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1474 self.* = initExtraArgs(builder, name, root_src, Kind.Test, false, null);
1475 self.* = initExtraArgs(builder, name, root_src, Kind.Test, .static, null);
14751476 return self;
14761477 }
14771478
......@@ -1480,7 +1481,7 @@ pub const LibExeObjStep = struct {
14801481 name_raw: []const u8,
14811482 root_src_raw: ?FileSource,
14821483 kind: Kind,
1483 is_dynamic: bool,
1484 linkage: Linkage,
14841485 ver: ?Version,
14851486 ) LibExeObjStep {
14861487 const name = builder.dupe(name_raw);
......@@ -1494,7 +1495,7 @@ pub const LibExeObjStep = struct {
14941495 .verbose_link = false,
14951496 .verbose_cc = false,
14961497 .build_mode = builtin.Mode.Debug,
1497 .is_dynamic = is_dynamic,
1498 .linkage = linkage,
14981499 .kind = kind,
14991500 .root_src = root_src,
15001501 .name = name,
......@@ -1553,12 +1554,15 @@ pub const LibExeObjStep = struct {
15531554 .Obj => .Obj,
15541555 .Exe, .Test => .Exe,
15551556 },
1556 .link_mode = if (self.is_dynamic) .Dynamic else .Static,
1557 .link_mode = switch (self.linkage) {
1558 .dynamic => std.builtin.LinkMode.Dynamic,
1559 .static => std.builtin.LinkMode.Static,
1560 },
15571561 .version = self.version,
15581562 }) catch unreachable;
15591563
15601564 if (self.kind == .Lib) {
1561 if (!self.is_dynamic) {
1565 if (self.linkage == .static) {
15621566 self.out_lib_filename = self.out_filename;
15631567 } else if (self.version) |version| {
15641568 if (target.isDarwin()) {
......@@ -1655,7 +1659,7 @@ pub const LibExeObjStep = struct {
16551659 }
16561660
16571661 pub fn isDynamicLibrary(self: *LibExeObjStep) bool {
1658 return self.kind == Kind.Lib and self.is_dynamic;
1662 return self.kind == Kind.Lib and self.linkage == .dynamic;
16591663 }
16601664
16611665 pub fn producesPdbFile(self: *LibExeObjStep) bool {
......@@ -1920,8 +1924,13 @@ pub const LibExeObjStep = struct {
19201924 source_duped.addStepDependencies(&self.step);
19211925 }
19221926
1923 pub fn addObjectFile(self: *LibExeObjStep, source: FileSource) void {
1927 pub fn addObjectFile(self: *LibExeObjStep, source_file: []const u8) void {
1928 self.addObjectFileSource(.{ .path = source_file });
1929 }
1930
1931 pub fn addObjectFileSource(self: *LibExeObjStep, source: FileSource) void {
19241932 self.link_objects.append(LinkObject{ .static_path = source.dupe(self.builder) }) catch unreachable;
1933 source.addStepDependencies(&self.step);
19251934 }
19261935
19271936 pub fn addObject(self: *LibExeObjStep, obj: *LibExeObjStep) void {
......@@ -2063,15 +2072,24 @@ pub const LibExeObjStep = struct {
20632072 }
20642073
20652074 pub fn addPackage(self: *LibExeObjStep, package: Pkg) void {
2066 package.path.addStepDependencies(&self.step);
20672075 self.packages.append(self.builder.dupePkg(package)) catch unreachable;
2076 self.addRecursiveBuildDeps(package);
2077 }
2078
2079 fn addRecursiveBuildDeps(self: *LibExeObjStep, package: Pkg) void {
2080 package.path.addStepDependencies(&self.step);
2081 if (package.dependencies) |deps| {
2082 for (deps) |dep| {
2083 self.addRecursiveBuildDeps(dep);
2084 }
2085 }
20682086 }
20692087
20702088 pub fn addPackagePath(self: *LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void {
2071 self.packages.append(Pkg{
2089 self.addPackage(Pkg{
20722090 .name = self.builder.dupe(name),
20732091 .path = .{ .path = self.builder.dupe(pkg_index_path) },
2074 }) catch unreachable;
2092 });
20752093 }
20762094
20772095 /// If Vcpkg was found on the system, it will be added to include and lib
......@@ -2081,20 +2099,20 @@ pub const LibExeObjStep = struct {
20812099 // after findVcpkgRoot and have only one switch statement, but the compiler
20822100 // cannot resolve the error set.
20832101 switch (self.builder.vcpkg_root) {
2084 .Unattempted => {
2102 .unattempted => {
20852103 self.builder.vcpkg_root = if (try findVcpkgRoot(self.builder.allocator)) |root|
2086 VcpkgRoot{ .Found = root }
2104 VcpkgRoot{ .found = root }
20872105 else
2088 .NotFound;
2106 .not_found;
20892107 },
2090 .NotFound => return error.VcpkgNotFound,
2091 .Found => {},
2108 .not_found => return error.VcpkgNotFound,
2109 .found => {},
20922110 }
20932111
20942112 switch (self.builder.vcpkg_root) {
2095 .Unattempted => unreachable,
2096 .NotFound => return error.VcpkgNotFound,
2097 .Found => |root| {
2113 .unattempted => unreachable,
2114 .not_found => return error.VcpkgNotFound,
2115 .found => |root| {
20982116 const allocator = self.builder.allocator;
20992117 const triplet = try self.target.vcpkgTriplet(allocator, linkage);
21002118 defer self.builder.allocator.free(triplet);
......@@ -2207,7 +2225,7 @@ pub const LibExeObjStep = struct {
22072225 const full_path_lib = other.getOutputLibPath();
22082226 try zig_args.append(full_path_lib);
22092227
2210 if (other.is_dynamic and !self.target.isWindows()) {
2228 if (other.linkage == .dynamic and !self.target.isWindows()) {
22112229 if (fs.path.dirname(full_path_lib)) |dirname| {
22122230 try zig_args.append("-rpath");
22132231 try zig_args.append(dirname);
......@@ -2373,13 +2391,13 @@ pub const LibExeObjStep = struct {
23732391 zig_args.append("--name") catch unreachable;
23742392 zig_args.append(self.name) catch unreachable;
23752393
2376 if (self.kind == Kind.Lib and self.is_dynamic) {
2394 if (self.kind == Kind.Lib and self.linkage == .dynamic) {
23772395 if (self.version) |version| {
23782396 zig_args.append("--version") catch unreachable;
23792397 zig_args.append(builder.fmt("{}", .{version})) catch unreachable;
23802398 }
23812399 }
2382 if (self.is_dynamic) {
2400 if (self.linkage == .dynamic) {
23832401 try zig_args.append("-dynamic");
23842402 }
23852403 if (self.bundle_compiler_rt) |x| {
......@@ -2682,7 +2700,7 @@ pub const LibExeObjStep = struct {
26822700 }
26832701 }
26842702
2685 if (self.kind == Kind.Lib and self.is_dynamic and self.version != null and self.target.wantSharedLibSymLinks()) {
2703 if (self.kind == .Lib and self.linkage == .dynamic and self.version != null and self.target.wantSharedLibSymLinks()) {
26862704 try doAtomicSymLinks(builder.allocator, self.getOutputPath(), self.major_only_filename.?, self.name_only_filename.?);
26872705 }
26882706 }
......@@ -3033,15 +3051,15 @@ fn findVcpkgRoot(allocator: *Allocator) !?[]const u8 {
30333051}
30343052
30353053const VcpkgRoot = union(VcpkgRootStatus) {
3036 Unattempted: void,
3037 NotFound: void,
3038 Found: []const u8,
3054 unattempted: void,
3055 not_found: void,
3056 found: []const u8,
30393057};
30403058
30413059const VcpkgRootStatus = enum {
3042 Unattempted,
3043 NotFound,
3044 Found,
3060 unattempted,
3061 not_found,
3062 found,
30453063};
30463064
30473065pub const VcpkgLinkage = std.builtin.LinkMode;
lib/std/build/TranslateCStep.zig+1-1
......@@ -63,7 +63,7 @@ pub fn setTarget(self: *TranslateCStep, target: CrossTarget) void {
6363
6464/// Creates a step to build an executable from the translated source.
6565pub fn addExecutable(self: *TranslateCStep) *LibExeObjStep {
66 return self.builder.addExecutableSource("translated_c", build.FileSource{ .generated = &self.output_file }, false);
66 return self.builder.addExecutableSource("translated_c", build.FileSource{ .generated = &self.output_file }, .static);
6767}
6868
6969pub fn addIncludeDir(self: *TranslateCStep, include_dir: []const u8) void {
lib/std/build/WriteFileStep.zig+1-4
......@@ -48,10 +48,7 @@ pub fn add(self: *WriteFileStep, basename: []const u8, bytes: []const u8) void {
4848
4949 self.files.append(node);
5050}
51/// Unless setOutputDir was called, this function must be called only in
52/// the make step, from a step that has declared a dependency on this one.
53/// To run an executable built with zig build, use `run`, or create an install step and invoke it.
54//pub const getOutputPath = @compileError("WriteFileStep.getOutputPath is deprecated! Use getFileSource to retrieve a ");
51
5552/// Gets a file source for the given basename. If the file does not exist, returns `null`.
5653pub fn getFileSource(step: *WriteFileStep, basename: []const u8) ?build.FileSource {
5754 var it = step.files.first;
test/src/compare_output.zig+3-3
......@@ -105,7 +105,7 @@ pub const CompareOutputContext = struct {
105105 }
106106
107107 const exe = b.addExecutable("test", null);
108 exe.addAssemblyFileFromWriteFileStep(write_src, case.sources.items[0].filename);
108 exe.addAssemblyFileSource(write_src.getFileSource(case.sources.items[0].filename).?);
109109
110110 const run = exe.run();
111111 run.addArgs(case.cli_args);
......@@ -126,7 +126,7 @@ pub const CompareOutputContext = struct {
126126 }
127127
128128 const basename = case.sources.items[0].filename;
129 const exe = b.addExecutableFromWriteFileStep("test", write_src, basename);
129 const exe = b.addExecutableSource("test", write_src.getFileSource(basename).?, false);
130130 exe.setBuildMode(mode);
131131 if (case.link_libc) {
132132 exe.linkSystemLibrary("c");
......@@ -147,7 +147,7 @@ pub const CompareOutputContext = struct {
147147 }
148148
149149 const basename = case.sources.items[0].filename;
150 const exe = b.addExecutableFromWriteFileStep("test", write_src, basename);
150 const exe = b.addExecutableSource("test", write_src.getFileSource(basename).?, false);
151151 if (case.link_libc) {
152152 exe.linkSystemLibrary("c");
153153 }
test/src/run_translated_c.zig+2-6
......@@ -86,12 +86,8 @@ pub const RunTranslatedCContext = struct {
8686 for (case.sources.items) |src_file| {
8787 write_src.add(src_file.filename, src_file.source);
8888 }
89 const translate_c = b.addTranslateC(.{
90 .write_file = .{
91 .step = write_src,
92 .basename = case.sources.items[0].filename,
93 },
94 });
89 const translate_c = b.addTranslateC(write_src.getFileSource(case.sources.items[0].filename).?);
90
9591 translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name});
9692 const exe = translate_c.addExecutable();
9793 exe.setTarget(self.target);
test/src/translate_c.zig+2-6
......@@ -109,12 +109,8 @@ pub const TranslateCContext = struct {
109109 write_src.add(src_file.filename, src_file.source);
110110 }
111111
112 const translate_c = b.addTranslateC(.{
113 .write_file = .{
114 .step = write_src,
115 .basename = case.sources.items[0].filename,
116 },
117 });
112 const translate_c = b.addTranslateC(write_src.getFileSource(case.sources.items[0].filename).?);
113
118114 translate_c.step.name = annotated_case_name;
119115 translate_c.setTarget(case.target);
120116
test/tests.zig+7-3
......@@ -107,6 +107,7 @@ const test_targets = blk: {
107107 .link_libc = true,
108108 },
109109
110
110111 TestTarget{
111112 .target = .{
112113 .cpu_arch = .aarch64,
......@@ -227,6 +228,7 @@ const test_targets = blk: {
227228 .link_libc = true,
228229 },
229230
231
230232 TestTarget{
231233 .target = .{
232234 .cpu_arch = .riscv64,
......@@ -654,7 +656,7 @@ pub const StackTracesContext = struct {
654656 const b = self.b;
655657 const src_basename = "source.zig";
656658 const write_src = b.addWriteFile(src_basename, source);
657 const exe = b.addExecutableFromWriteFileStep("test", write_src, src_basename);
659 const exe = b.addExecutableSource("test", write_src.getFileSource(src_basename).?, false);
658660 exe.setBuildMode(mode);
659661
660662 const run_and_compare = RunAndCompareStep.create(
......@@ -668,6 +670,7 @@ pub const StackTracesContext = struct {
668670 self.step.dependOn(&run_and_compare.step);
669671 }
670672
673
671674 const RunAndCompareStep = struct {
672675 step: build.Step,
673676 context: *StackTracesContext,
......@@ -776,6 +779,7 @@ pub const StackTracesContext = struct {
776779 var it = mem.split(stderr, "\n");
777780 process_lines: while (it.next()) |line| {
778781 if (line.len == 0) continue;
782
779783 // offset search past `[drive]:` on windows
780784 var pos: usize = if (std.Target.current.os.tag == .windows) 2 else 0;
781785 // locate delims/anchor
......@@ -935,7 +939,7 @@ pub const CompileErrorContext = struct {
935939 try zig_args.append("build-obj");
936940 }
937941 const root_src_basename = self.case.sources.items[0].filename;
938 try zig_args.append(self.write_src.getOutputPath(root_src_basename));
942 try zig_args.append(self.write_src.getFileSource(root_src_basename).?.getPath(b));
939943
940944 zig_args.append("--name") catch unreachable;
941945 zig_args.append("test") catch unreachable;
......@@ -1368,4 +1372,4 @@ fn printInvocation(args: []const []const u8) void {
13681372 warn("{s} ", .{arg});
13691373 }
13701374 warn("\n", .{});
1371}
1375}
\ No newline at end of file