@@ -23,6 +23,7 @@ pub const WriteFileStep = @import("build/WriteFileStep.zig");
23
23
pub const RunStep = @import ("build/RunStep.zig" );
24
24
pub const CheckFileStep = @import ("build/CheckFileStep.zig" );
25
25
pub const InstallRawStep = @import ("build/InstallRawStep.zig" );
26
+ pub const OptionsStep = @import ("build/OptionsStep.zig" );
26
27
27
28
pub const Builder = struct {
28
29
install_tls : TopLevelStep ,
@@ -247,6 +248,10 @@ pub const Builder = struct {
247
248
return LibExeObjStep .createExecutable (builder , name , root_src );
248
249
}
249
250
251
+ pub fn addOptions (self : * Builder ) * OptionsStep {
252
+ return OptionsStep .create (self );
253
+ }
254
+
250
255
pub fn addObject (self : * Builder , name : []const u8 , root_src : ? []const u8 ) * LibExeObjStep {
251
256
return addObjectSource (self , name , convertOptionalPathToFileSource (root_src ));
252
257
}
@@ -1375,16 +1380,6 @@ pub const FileSource = union(enum) {
1375
1380
}
1376
1381
};
1377
1382
1378
- const BuildOptionArtifactArg = struct {
1379
- name : []const u8 ,
1380
- artifact : * LibExeObjStep ,
1381
- };
1382
-
1383
- const BuildOptionFileSourceArg = struct {
1384
- name : []const u8 ,
1385
- source : FileSource ,
1386
- };
1387
-
1388
1383
pub const LibExeObjStep = struct {
1389
1384
pub const base_id = .lib_exe_obj ;
1390
1385
@@ -1434,9 +1429,6 @@ pub const LibExeObjStep = struct {
1434
1429
out_lib_filename : []const u8 ,
1435
1430
out_pdb_filename : []const u8 ,
1436
1431
packages : ArrayList (Pkg ),
1437
- build_options_contents : std .ArrayList (u8 ),
1438
- build_options_artifact_args : std .ArrayList (BuildOptionArtifactArg ),
1439
- build_options_file_source_args : std .ArrayList (BuildOptionFileSourceArg ),
1440
1432
1441
1433
object_src : []const u8 ,
1442
1434
@@ -1603,9 +1595,6 @@ pub const LibExeObjStep = struct {
1603
1595
.rpaths = ArrayList ([]const u8 ).init (builder .allocator ),
1604
1596
.framework_dirs = ArrayList ([]const u8 ).init (builder .allocator ),
1605
1597
.object_src = undefined ,
1606
- .build_options_contents = std .ArrayList (u8 ).init (builder .allocator ),
1607
- .build_options_artifact_args = std .ArrayList (BuildOptionArtifactArg ).init (builder .allocator ),
1608
- .build_options_file_source_args = std .ArrayList (BuildOptionFileSourceArg ).init (builder .allocator ),
1609
1598
.c_std = Builder .CStd .C99 ,
1610
1599
.override_lib_dir = null ,
1611
1600
.main_pkg_path = null ,
@@ -2038,119 +2027,6 @@ pub const LibExeObjStep = struct {
2038
2027
self .linkLibraryOrObject (obj );
2039
2028
}
2040
2029
2041
- pub fn addBuildOption (self : * LibExeObjStep , comptime T : type , name : []const u8 , value : T ) void {
2042
- const out = self .build_options_contents .writer ();
2043
- switch (T ) {
2044
- []const []const u8 = > {
2045
- out .print ("pub const {}: []const []const u8 = &[_][]const u8{{\n " , .{std .zig .fmtId (name )}) catch unreachable ;
2046
- for (value ) | slice | {
2047
- out .print (" \" {}\" ,\n " , .{std .zig .fmtEscapes (slice )}) catch unreachable ;
2048
- }
2049
- out .writeAll ("};\n " ) catch unreachable ;
2050
- return ;
2051
- },
2052
- [:0 ]const u8 = > {
2053
- out .print ("pub const {}: [:0]const u8 = \" {}\" ;\n " , .{ std .zig .fmtId (name ), std .zig .fmtEscapes (value ) }) catch unreachable ;
2054
- return ;
2055
- },
2056
- []const u8 = > {
2057
- out .print ("pub const {}: []const u8 = \" {}\" ;\n " , .{ std .zig .fmtId (name ), std .zig .fmtEscapes (value ) }) catch unreachable ;
2058
- return ;
2059
- },
2060
- ? [:0 ]const u8 = > {
2061
- out .print ("pub const {}: ?[:0]const u8 = " , .{std .zig .fmtId (name )}) catch unreachable ;
2062
- if (value ) | payload | {
2063
- out .print ("\" {}\" ;\n " , .{std .zig .fmtEscapes (payload )}) catch unreachable ;
2064
- } else {
2065
- out .writeAll ("null;\n " ) catch unreachable ;
2066
- }
2067
- return ;
2068
- },
2069
- ? []const u8 = > {
2070
- out .print ("pub const {}: ?[]const u8 = " , .{std .zig .fmtId (name )}) catch unreachable ;
2071
- if (value ) | payload | {
2072
- out .print ("\" {}\" ;\n " , .{std .zig .fmtEscapes (payload )}) catch unreachable ;
2073
- } else {
2074
- out .writeAll ("null;\n " ) catch unreachable ;
2075
- }
2076
- return ;
2077
- },
2078
- std .builtin .Version = > {
2079
- out .print (
2080
- \\pub const {}: @import("std").builtin.Version = .{{
2081
- \\ .major = {d},
2082
- \\ .minor = {d},
2083
- \\ .patch = {d},
2084
- \\}};
2085
- \\
2086
- , .{
2087
- std .zig .fmtId (name ),
2088
-
2089
- value .major ,
2090
- value .minor ,
2091
- value .patch ,
2092
- }) catch unreachable ;
2093
- },
2094
- std .SemanticVersion = > {
2095
- out .print (
2096
- \\pub const {}: @import("std").SemanticVersion = .{{
2097
- \\ .major = {d},
2098
- \\ .minor = {d},
2099
- \\ .patch = {d},
2100
- \\
2101
- , .{
2102
- std .zig .fmtId (name ),
2103
-
2104
- value .major ,
2105
- value .minor ,
2106
- value .patch ,
2107
- }) catch unreachable ;
2108
- if (value .pre ) | some | {
2109
- out .print (" .pre = \" {}\" ,\n " , .{std .zig .fmtEscapes (some )}) catch unreachable ;
2110
- }
2111
- if (value .build ) | some | {
2112
- out .print (" .build = \" {}\" ,\n " , .{std .zig .fmtEscapes (some )}) catch unreachable ;
2113
- }
2114
- out .writeAll ("};\n " ) catch unreachable ;
2115
- return ;
2116
- },
2117
- else = > {},
2118
- }
2119
- switch (@typeInfo (T )) {
2120
- .Enum = > | enum_info | {
2121
- out .print ("pub const {} = enum {{\n " , .{std .zig .fmtId (@typeName (T ))}) catch unreachable ;
2122
- inline for (enum_info .fields ) | field | {
2123
- out .print (" {},\n " , .{std .zig .fmtId (field .name )}) catch unreachable ;
2124
- }
2125
- out .writeAll ("};\n " ) catch unreachable ;
2126
- },
2127
- else = > {},
2128
- }
2129
- out .print ("pub const {}: {s} = {};\n " , .{ std .zig .fmtId (name ), @typeName (T ), value }) catch unreachable ;
2130
- }
2131
-
2132
- /// The value is the path in the cache dir.
2133
- /// Adds a dependency automatically.
2134
- pub fn addBuildOptionArtifact (self : * LibExeObjStep , name : []const u8 , artifact : * LibExeObjStep ) void {
2135
- self .build_options_artifact_args .append (.{ .name = self .builder .dupe (name ), .artifact = artifact }) catch unreachable ;
2136
- self .step .dependOn (& artifact .step );
2137
- }
2138
-
2139
- /// The value is the path in the cache dir.
2140
- /// Adds a dependency automatically.
2141
- /// basename refers to the basename of the WriteFileStep
2142
- pub fn addBuildOptionFileSource (
2143
- self : * LibExeObjStep ,
2144
- name : []const u8 ,
2145
- source : FileSource ,
2146
- ) void {
2147
- self .build_options_file_source_args .append (.{
2148
- .name = name ,
2149
- .source = source .dupe (self .builder ),
2150
- }) catch unreachable ;
2151
- source .addStepDependencies (& self .step );
2152
- }
2153
-
2154
2030
pub fn addSystemIncludeDir (self : * LibExeObjStep , path : []const u8 ) void {
2155
2031
self .include_dirs .append (IncludeDir { .raw_path_system = self .builder .dupe (path ) }) catch unreachable ;
2156
2032
}
@@ -2176,6 +2052,10 @@ pub const LibExeObjStep = struct {
2176
2052
self .addRecursiveBuildDeps (package );
2177
2053
}
2178
2054
2055
+ pub fn addOptions (self : * LibExeObjStep , package_name : []const u8 , options : * OptionsStep ) void {
2056
+ self .addPackage (options .getPackage (package_name ));
2057
+ }
2058
+
2179
2059
fn addRecursiveBuildDeps (self : * LibExeObjStep , package : Pkg ) void {
2180
2060
package .path .addStepDependencies (& self .step );
2181
2061
if (package .dependencies ) | deps | {
@@ -2393,41 +2273,6 @@ pub const LibExeObjStep = struct {
2393
2273
}
2394
2274
}
2395
2275
2396
- if (self .build_options_contents .items .len > 0 or
2397
- self .build_options_artifact_args .items .len > 0 or
2398
- self .build_options_file_source_args .items .len > 0 )
2399
- {
2400
- // Render build artifact and write file options at the last minute, now that the path is known.
2401
- //
2402
- // Note that pathFromRoot uses resolve path, so this will have
2403
- // correct behavior even if getOutputPath is already absolute.
2404
- for (self .build_options_artifact_args .items ) | item | {
2405
- self .addBuildOption (
2406
- []const u8 ,
2407
- item .name ,
2408
- self .builder .pathFromRoot (item .artifact .getOutputSource ().getPath (self .builder )),
2409
- );
2410
- }
2411
- for (self .build_options_file_source_args .items ) | item | {
2412
- self .addBuildOption (
2413
- []const u8 ,
2414
- item .name ,
2415
- item .source .getPath (self .builder ),
2416
- );
2417
- }
2418
-
2419
- const build_options_file = try fs .path .join (
2420
- builder .allocator ,
2421
- &[_ ][]const u8 { builder .cache_root , builder .fmt ("{s}_build_options.zig" , .{self .name }) },
2422
- );
2423
- const path_from_root = builder .pathFromRoot (build_options_file );
2424
- try fs .cwd ().writeFile (path_from_root , self .build_options_contents .items );
2425
- try zig_args .append ("--pkg-begin" );
2426
- try zig_args .append ("build_options" );
2427
- try zig_args .append (path_from_root );
2428
- try zig_args .append ("--pkg-end" );
2429
- }
2430
-
2431
2276
if (self .image_base ) | image_base | {
2432
2277
try zig_args .append ("--image-base" );
2433
2278
try zig_args .append (builder .fmt ("0x{x}" , .{image_base }));
@@ -3141,6 +2986,7 @@ pub const Step = struct {
3141
2986
run ,
3142
2987
check_file ,
3143
2988
install_raw ,
2989
+ options ,
3144
2990
custom ,
3145
2991
};
3146
2992
@@ -3312,43 +3158,6 @@ test "Builder.dupePkg()" {
3312
3158
try std .testing .expect (dupe_deps [0 ].path .path .ptr != pkg_dep .path .path .ptr );
3313
3159
}
3314
3160
3315
- test "LibExeObjStep.addBuildOption" {
3316
- if (builtin .os .tag == .wasi ) return error .SkipZigTest ;
3317
-
3318
- var arena = std .heap .ArenaAllocator .init (std .testing .allocator );
3319
- defer arena .deinit ();
3320
- var builder = try Builder .create (
3321
- & arena .allocator ,
3322
- "test" ,
3323
- "test" ,
3324
- "test" ,
3325
- "test" ,
3326
- );
3327
- defer builder .destroy ();
3328
-
3329
- var exe = builder .addExecutable ("not_an_executable" , "/not/an/executable.zig" );
3330
- exe .addBuildOption (usize , "option1" , 1 );
3331
- exe .addBuildOption (? usize , "option2" , null );
3332
- exe .addBuildOption ([]const u8 , "string" , "zigisthebest" );
3333
- exe .addBuildOption (? []const u8 , "optional_string" , null );
3334
- exe .addBuildOption (std .SemanticVersion , "semantic_version" , try std .SemanticVersion .parse ("0.1.2-foo+bar" ));
3335
-
3336
- try std .testing .expectEqualStrings (
3337
- \\pub const option1: usize = 1;
3338
- \\pub const option2: ?usize = null;
3339
- \\pub const string: []const u8 = "zigisthebest";
3340
- \\pub const optional_string: ?[]const u8 = null;
3341
- \\pub const semantic_version: @import("std").SemanticVersion = .{
3342
- \\ .major = 0,
3343
- \\ .minor = 1,
3344
- \\ .patch = 2,
3345
- \\ .pre = "foo",
3346
- \\ .build = "bar",
3347
- \\};
3348
- \\
3349
- , exe .build_options_contents .items );
3350
- }
3351
-
3352
3161
test "LibExeObjStep.addPackage" {
3353
3162
if (builtin .os .tag == .wasi ) return error .SkipZigTest ;
3354
3163
0 commit comments