| author | |
| committer | |
| log | d9f1a952b8b0e19aafcf568b35cc220adbb4a7b5 |
| tree | d7333d6953a0fe37ac11b12a2456efebffd2a4c0 |
| parent | b67caf72e31eefe08fe2dc8b17b4bca16e0e3cc6 |
Adds a missing call to addLazyPathDependenciesOnly in
std.Build.Module.addCSourceFiles. Also fixes an issue in
std.Build.Step.WriteFile where it wasn't updating all the GeneratedFile
instances for every directory. To fix the second issue, I removed
all the GeneratedFile instances and now all files/directories reference
the steps main GeneratedFile via sub paths.12 files changed, 96 insertions(+), 47 deletions(-)
lib/std/Build/Module.zig+1| ... | ... | @@ -484,6 +484,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void { |
| 484 | 484 | .flags = b.dupeStrings(options.flags), |
| 485 | 485 | }; |
| 486 | 486 | m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM"); |
| 487 | addLazyPathDependenciesOnly(m, c_source_files.root); | |
| 487 | 488 | } |
| 488 | 489 | |
| 489 | 490 | pub fn addCSourceFile(m: *Module, source: CSourceFile) void { |
lib/std/Build/Step/WriteFile.zig+23-33| ... | ... | @@ -17,8 +17,8 @@ const WriteFile = @This(); |
| 17 | 17 | step: Step, |
| 18 | 18 | |
| 19 | 19 | // The elements here are pointers because we need stable pointers for the GeneratedFile field. |
| 20 | files: std.ArrayListUnmanaged(*File), | |
| 21 | directories: std.ArrayListUnmanaged(*Directory), | |
| 20 | files: std.ArrayListUnmanaged(File), | |
| 21 | directories: std.ArrayListUnmanaged(Directory), | |
| 22 | 22 | |
| 23 | 23 | output_source_files: std.ArrayListUnmanaged(OutputSourceFile), |
| 24 | 24 | generated_directory: std.Build.GeneratedFile, |
| ... | ... | @@ -26,20 +26,14 @@ generated_directory: std.Build.GeneratedFile, |
| 26 | 26 | pub const base_id: Step.Id = .write_file; |
| 27 | 27 | |
| 28 | 28 | pub const File = struct { |
| 29 | generated_file: std.Build.GeneratedFile, | |
| 30 | 29 | sub_path: []const u8, |
| 31 | 30 | contents: Contents, |
| 32 | ||
| 33 | pub fn getPath(file: *File) std.Build.LazyPath { | |
| 34 | return .{ .generated = .{ .file = &file.generated_file } }; | |
| 35 | } | |
| 36 | 31 | }; |
| 37 | 32 | |
| 38 | 33 | pub const Directory = struct { |
| 39 | 34 | source: std.Build.LazyPath, |
| 40 | 35 | sub_path: []const u8, |
| 41 | 36 | options: Options, |
| 42 | generated_dir: std.Build.GeneratedFile, | |
| 43 | 37 | |
| 44 | 38 | pub const Options = struct { |
| 45 | 39 | /// File paths that end in any of these suffixes will be excluded from copying. |
| ... | ... | @@ -56,10 +50,6 @@ pub const Directory = struct { |
| 56 | 50 | }; |
| 57 | 51 | } |
| 58 | 52 | }; |
| 59 | ||
| 60 | pub fn getPath(dir: *Directory) std.Build.LazyPath { | |
| 61 | return .{ .generated = .{ .file = &dir.generated_dir } }; | |
| 62 | } | |
| 63 | 53 | }; |
| 64 | 54 | |
| 65 | 55 | pub const OutputSourceFile = struct { |
| ... | ... | @@ -92,15 +82,18 @@ pub fn create(owner: *std.Build) *WriteFile { |
| 92 | 82 | pub fn add(write_file: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.LazyPath { |
| 93 | 83 | const b = write_file.step.owner; |
| 94 | 84 | const gpa = b.allocator; |
| 95 | const file = gpa.create(File) catch @panic("OOM"); | |
| 96 | file.* = .{ | |
| 97 | .generated_file = .{ .step = &write_file.step }, | |
| 85 | const file = File{ | |
| 98 | 86 | .sub_path = b.dupePath(sub_path), |
| 99 | 87 | .contents = .{ .bytes = b.dupe(bytes) }, |
| 100 | 88 | }; |
| 101 | 89 | write_file.files.append(gpa, file) catch @panic("OOM"); |
| 102 | 90 | write_file.maybeUpdateName(); |
| 103 | return file.getPath(); | |
| 91 | return .{ | |
| 92 | .generated = .{ | |
| 93 | .file = &write_file.generated_directory, | |
| 94 | .sub_path = file.sub_path, | |
| 95 | }, | |
| 96 | }; | |
| 104 | 97 | } |
| 105 | 98 | |
| 106 | 99 | /// Place the file into the generated directory within the local cache, |
| ... | ... | @@ -113,9 +106,7 @@ pub fn add(write_file: *WriteFile, sub_path: []const u8, bytes: []const u8) std. |
| 113 | 106 | pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path: []const u8) std.Build.LazyPath { |
| 114 | 107 | const b = write_file.step.owner; |
| 115 | 108 | const gpa = b.allocator; |
| 116 | const file = gpa.create(File) catch @panic("OOM"); | |
| 117 | file.* = .{ | |
| 118 | .generated_file = .{ .step = &write_file.step }, | |
| 109 | const file = File{ | |
| 119 | 110 | .sub_path = b.dupePath(sub_path), |
| 120 | 111 | .contents = .{ .copy = source }, |
| 121 | 112 | }; |
| ... | ... | @@ -123,7 +114,12 @@ pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path: |
| 123 | 114 | |
| 124 | 115 | write_file.maybeUpdateName(); |
| 125 | 116 | source.addStepDependencies(&write_file.step); |
| 126 | return file.getPath(); | |
| 117 | return .{ | |
| 118 | .generated = .{ | |
| 119 | .file = &write_file.generated_directory, | |
| 120 | .sub_path = file.sub_path, | |
| 121 | }, | |
| 122 | }; | |
| 127 | 123 | } |
| 128 | 124 | |
| 129 | 125 | /// Copy files matching the specified exclude/include patterns to the specified subdirectory |
| ... | ... | @@ -137,18 +133,21 @@ pub fn addCopyDirectory( |
| 137 | 133 | ) std.Build.LazyPath { |
| 138 | 134 | const b = write_file.step.owner; |
| 139 | 135 | const gpa = b.allocator; |
| 140 | const dir = gpa.create(Directory) catch @panic("OOM"); | |
| 141 | dir.* = .{ | |
| 136 | const dir = Directory{ | |
| 142 | 137 | .source = source.dupe(b), |
| 143 | 138 | .sub_path = b.dupePath(sub_path), |
| 144 | 139 | .options = options.dupe(b), |
| 145 | .generated_dir = .{ .step = &write_file.step }, | |
| 146 | 140 | }; |
| 147 | 141 | write_file.directories.append(gpa, dir) catch @panic("OOM"); |
| 148 | 142 | |
| 149 | 143 | write_file.maybeUpdateName(); |
| 150 | 144 | source.addStepDependencies(&write_file.step); |
| 151 | return dir.getPath(); | |
| 145 | return .{ | |
| 146 | .generated = .{ | |
| 147 | .file = &write_file.generated_directory, | |
| 148 | .sub_path = dir.sub_path, | |
| 149 | }, | |
| 150 | }; | |
| 152 | 151 | } |
| 153 | 152 | |
| 154 | 153 | /// A path relative to the package root. |
| ... | ... | @@ -278,11 +277,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void { |
| 278 | 277 | |
| 279 | 278 | if (try step.cacheHit(&man)) { |
| 280 | 279 | const digest = man.final(); |
| 281 | for (write_file.files.items) |file| { | |
| 282 | file.generated_file.path = try b.cache_root.join(b.allocator, &.{ | |
| 283 | "o", &digest, file.sub_path, | |
| 284 | }); | |
| 285 | } | |
| 286 | 280 | write_file.generated_directory.path = try b.cache_root.join(b.allocator, &.{ "o", &digest }); |
| 287 | 281 | return; |
| 288 | 282 | } |
| ... | ... | @@ -342,10 +336,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void { |
| 342 | 336 | _ = prev_status; |
| 343 | 337 | }, |
| 344 | 338 | } |
| 345 | ||
| 346 | file.generated_file.path = try b.cache_root.join(b.allocator, &.{ | |
| 347 | cache_path, file.sub_path, | |
| 348 | }); | |
| 349 | 339 | } |
| 350 | 340 | for (write_file.directories.items) |dir| { |
| 351 | 341 | const full_src_dir_path = dir.source.getPath2(b, step); |
test/src/Cases.zig+4-2| ... | ... | @@ -665,10 +665,12 @@ pub fn lowerToBuildSteps( |
| 665 | 665 | const writefiles = b.addWriteFiles(); |
| 666 | 666 | var file_sources = std.StringHashMap(std.Build.LazyPath).init(b.allocator); |
| 667 | 667 | defer file_sources.deinit(); |
| 668 | for (update.files.items) |file| { | |
| 668 | const first_file = update.files.items[0]; | |
| 669 | const root_source_file = writefiles.add(first_file.path, first_file.src); | |
| 670 | file_sources.put(first_file.path, root_source_file) catch @panic("OOM"); | |
| 671 | for (update.files.items[1..]) |file| { | |
| 669 | 672 | file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM"); |
| 670 | 673 | } |
| 671 | const root_source_file = writefiles.files.items[0].getPath(); | |
| 672 | 674 | |
| 673 | 675 | const artifact = if (case.is_test) b.addTest(.{ |
| 674 | 676 | .root_source_file = root_source_file, |
test/src/CompareOutput.zig+6-4| ... | ... | @@ -81,7 +81,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { |
| 81 | 81 | const b = self.b; |
| 82 | 82 | |
| 83 | 83 | const write_src = b.addWriteFiles(); |
| 84 | for (case.sources.items) |src_file| { | |
| 84 | const first_src = case.sources.items[0]; | |
| 85 | const first_file = write_src.add(first_src.filename, first_src.source); | |
| 86 | for (case.sources.items[1..]) |src_file| { | |
| 85 | 87 | _ = write_src.add(src_file.filename, src_file.source); |
| 86 | 88 | } |
| 87 | 89 | |
| ... | ... | @@ -99,7 +101,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { |
| 99 | 101 | .target = b.graph.host, |
| 100 | 102 | .optimize = .Debug, |
| 101 | 103 | }); |
| 102 | exe.addAssemblyFile(write_src.files.items[0].getPath()); | |
| 104 | exe.addAssemblyFile(first_file); | |
| 103 | 105 | |
| 104 | 106 | const run = b.addRunArtifact(exe); |
| 105 | 107 | run.setName(annotated_case_name); |
| ... | ... | @@ -119,7 +121,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { |
| 119 | 121 | |
| 120 | 122 | const exe = b.addExecutable(.{ |
| 121 | 123 | .name = "test", |
| 122 | .root_source_file = write_src.files.items[0].getPath(), | |
| 124 | .root_source_file = first_file, | |
| 123 | 125 | .optimize = optimize, |
| 124 | 126 | .target = b.graph.host, |
| 125 | 127 | }); |
| ... | ... | @@ -145,7 +147,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { |
| 145 | 147 | |
| 146 | 148 | const exe = b.addExecutable(.{ |
| 147 | 149 | .name = "test", |
| 148 | .root_source_file = write_src.files.items[0].getPath(), | |
| 150 | .root_source_file = first_file, | |
| 149 | 151 | .target = b.graph.host, |
| 150 | 152 | .optimize = .Debug, |
| 151 | 153 | }); |
test/src/RunTranslatedC.zig+4-2| ... | ... | @@ -72,11 +72,13 @@ pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void { |
| 72 | 72 | } else if (self.test_filters.len > 0) return; |
| 73 | 73 | |
| 74 | 74 | const write_src = b.addWriteFiles(); |
| 75 | for (case.sources.items) |src_file| { | |
| 75 | const first_case = case.sources.items[0]; | |
| 76 | const root_source_file = write_src.add(first_case.filename, first_case.source); | |
| 77 | for (case.sources.items[1..]) |src_file| { | |
| 76 | 78 | _ = write_src.add(src_file.filename, src_file.source); |
| 77 | 79 | } |
| 78 | 80 | const translate_c = b.addTranslateC(.{ |
| 79 | .root_source_file = write_src.files.items[0].getPath(), | |
| 81 | .root_source_file = root_source_file, | |
| 80 | 82 | .target = b.graph.host, |
| 81 | 83 | .optimize = .Debug, |
| 82 | 84 | }); |
test/src/StackTrace.zig+3-2| ... | ... | @@ -51,10 +51,11 @@ fn addExpect( |
| 51 | 51 | if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break; |
| 52 | 52 | } else if (self.test_filters.len > 0) return; |
| 53 | 53 | |
| 54 | const write_src = b.addWriteFile("source.zig", source); | |
| 54 | const write_files = b.addWriteFiles(); | |
| 55 | const source_zig = write_files.add("source.zig", source); | |
| 55 | 56 | const exe = b.addExecutable(.{ |
| 56 | 57 | .name = "test", |
| 57 | .root_source_file = write_src.files.items[0].getPath(), | |
| 58 | .root_source_file = source_zig, | |
| 58 | 59 | .optimize = optimize_mode, |
| 59 | 60 | .target = b.graph.host, |
| 60 | 61 | .error_tracing = mode_config.error_tracing, |
test/src/TranslateC.zig+4-2| ... | ... | @@ -93,12 +93,14 @@ pub fn addCase(self: *TranslateCContext, case: *const TestCase) void { |
| 93 | 93 | } else if (self.test_filters.len > 0) return; |
| 94 | 94 | |
| 95 | 95 | const write_src = b.addWriteFiles(); |
| 96 | for (case.sources.items) |src_file| { | |
| 96 | const first_src = case.sources.items[0]; | |
| 97 | const root_source_file = write_src.add(first_src.filename, first_src.source); | |
| 98 | for (case.sources.items[1..]) |src_file| { | |
| 97 | 99 | _ = write_src.add(src_file.filename, src_file.source); |
| 98 | 100 | } |
| 99 | 101 | |
| 100 | 102 | const translate_c = b.addTranslateC(.{ |
| 101 | .root_source_file = write_src.files.items[0].getPath(), | |
| 103 | .root_source_file = root_source_file, | |
| 102 | 104 | .target = b.resolveTargetQuery(case.target), |
| 103 | 105 | .optimize = .Debug, |
| 104 | 106 | }); |
test/standalone/build.zig.zon+3| ... | ... | @@ -83,6 +83,9 @@ |
| 83 | 83 | .dep_shared_builtin = .{ |
| 84 | 84 | .path = "dep_shared_builtin", |
| 85 | 85 | }, |
| 86 | .dep_lazypath = .{ | |
| 87 | .path = "dep_lazypath", | |
| 88 | }, | |
| 86 | 89 | .dirname = .{ |
| 87 | 90 | .path = "dirname", |
| 88 | 91 | }, |
test/standalone/dep_lazypath/build.zig created+37| ... | ... | @@ -0,0 +1,37 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | pub fn build(b: *std.Build) void { | |
| 4 | const test_step = b.step("test", "Test it"); | |
| 5 | b.default_step = test_step; | |
| 6 | ||
| 7 | const optimize: std.builtin.OptimizeMode = .Debug; | |
| 8 | ||
| 9 | { | |
| 10 | const write_files = b.addWriteFiles(); | |
| 11 | const generated_main_c = write_files.add("main.c", ""); | |
| 12 | const exe = b.addExecutable(.{ | |
| 13 | .name = "test", | |
| 14 | .target = b.graph.host, | |
| 15 | .optimize = optimize, | |
| 16 | }); | |
| 17 | exe.addCSourceFiles(.{ | |
| 18 | .root = generated_main_c.dirname(), | |
| 19 | .files = &.{"main.c"}, | |
| 20 | }); | |
| 21 | b.step("csourcefiles", "").dependOn(&exe.step); | |
| 22 | test_step.dependOn(&exe.step); | |
| 23 | } | |
| 24 | { | |
| 25 | const write_files = b.addWriteFiles(); | |
| 26 | const dir = write_files.addCopyDirectory(b.path("inc"), "", .{}); | |
| 27 | const exe = b.addExecutable(.{ | |
| 28 | .name = "test", | |
| 29 | .root_source_file = b.path("inctest.zig"), | |
| 30 | .target = b.graph.host, | |
| 31 | .optimize = optimize, | |
| 32 | }); | |
| 33 | exe.addIncludePath(dir); | |
| 34 | b.step("copydir", "").dependOn(&exe.step); | |
| 35 | test_step.dependOn(&exe.step); | |
| 36 | } | |
| 37 | } |
test/standalone/dep_lazypath/inc/foo.h created+1| ... | ... | @@ -0,0 +1 @@ |
| 1 | #define foo_value 42 |
test/standalone/dep_lazypath/inctest.zig created+8| ... | ... | @@ -0,0 +1,8 @@ |
| 1 | const std = @import("std"); | |
| 2 | const c = @cImport({ | |
| 3 | @cInclude("foo.h"); | |
| 4 | }); | |
| 5 | comptime { | |
| 6 | std.debug.assert(c.foo_value == 42); | |
| 7 | } | |
| 8 | pub fn main() void {} |
test/tests.zig+2-2| ... | ... | @@ -783,7 +783,7 @@ pub fn addCliTests(b: *std.Build) *Step { |
| 783 | 783 | if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) { |
| 784 | 784 | const tmp_path = b.makeTempPath(); |
| 785 | 785 | |
| 786 | const writefile = b.addWriteFile("example.zig", | |
| 786 | const example_zig = b.addWriteFiles().add("example.zig", | |
| 787 | 787 | \\// Type your code here, or load an example. |
| 788 | 788 | \\export fn square(num: i32) i32 { |
| 789 | 789 | \\ return num * num; |
| ... | ... | @@ -804,7 +804,7 @@ pub fn addCliTests(b: *std.Build) *Step { |
| 804 | 804 | "-fno-emit-bin", "-fno-emit-h", |
| 805 | 805 | "-fstrip", "-OReleaseFast", |
| 806 | 806 | }); |
| 807 | run.addFileArg(writefile.files.items[0].getPath()); | |
| 807 | run.addFileArg(example_zig); | |
| 808 | 808 | const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s"); |
| 809 | 809 | |
| 810 | 810 | const checkfile = b.addCheckFile(example_s, .{ |