authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-28 17:14:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-31 15:09:35-07:00
log71ff60f1265da83e619b1b6b2488ecb448fdfd36
treec38e652543d15bae6eebb03a2ec4711e56611d42
parent063888afff75f9d91fd221d84e1b74b111304ac3

std.build: eliminate setTarget and setBuildMode

This is a breaking change that makes the API for creating build artifacts no longer have any period of time where the target and optimization mode are not set.

2 files changed, 120 insertions(+), 127 deletions(-)

lib/std/build.zig+98-68
...@@ -414,82 +414,123 @@ pub const Builder = struct {...@@ -414,82 +414,123 @@ pub const Builder = struct {
414 self.h_dir = self.pathJoin(&h_list);414 self.h_dir = self.pathJoin(&h_list);
415 }415 }
416416
417 fn convertOptionalPathToFileSource(path: ?[]const u8) ?FileSource {
418 return if (path) |p|
419 FileSource{ .path = p }
420 else
421 null;
422 }
423
424 pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
425 return addExecutableSource(self, name, convertOptionalPathToFileSource(root_src));
426 }
427
428 pub fn addExecutableSource(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
429 return LibExeObjStep.createExecutable(builder, name, root_src);
430 }
431
432 pub fn addOptions(self: *Builder) *OptionsStep {417 pub fn addOptions(self: *Builder) *OptionsStep {
433 return OptionsStep.create(self);418 return OptionsStep.create(self);
434 }419 }
435420
436 pub fn addObject(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {421 pub const ExecutableOptions = struct {
437 return addObjectSource(self, name, convertOptionalPathToFileSource(root_src));
438 }
439
440 pub fn addObjectSource(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
441 return LibExeObjStep.createObject(builder, name, root_src);
442 }
443
444 pub fn addSharedLibrary(
445 self: *Builder,
446 name: []const u8,422 name: []const u8,
447 root_src: ?[]const u8,423 root_source_file: ?FileSource,
448 kind: LibExeObjStep.SharedLibKind,424 version: ?std.builtin.Version = null,
449 ) *LibExeObjStep {425 target: CrossTarget,
450 return addSharedLibrarySource(self, name, convertOptionalPathToFileSource(root_src), kind);426 optimize: std.builtin.Mode,
427 linkage: ?LibExeObjStep.Linkage = null,
428 };
429
430 pub fn addExecutable(b: *Builder, options: ExecutableOptions) *LibExeObjStep {
431 return LibExeObjStep.create(b, .{
432 .name = options.name,
433 .root_source_file = options.root_source_file,
434 .version = options.version,
435 .target = options.target,
436 .optimize = options.optimize,
437 .kind = .exe,
438 .linkage = options.linkage,
439 .version = options.version,
440 });
451 }441 }
452442
453 pub fn addSharedLibrarySource(443 pub const ObjectOptions = struct {
454 self: *Builder,
455 name: []const u8,444 name: []const u8,
456 root_src: ?FileSource,445 root_source_file: ?FileSource,
457 kind: LibExeObjStep.SharedLibKind,446 target: CrossTarget,
458 ) *LibExeObjStep {447 optimize: std.builtin.Mode,
459 return LibExeObjStep.createSharedLibrary(self, name, root_src, kind);448 };
460 }
461449
462 pub fn addStaticLibrary(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {450 pub fn addObject(b: *Builder, options: ObjectOptions) *LibExeObjStep {
463 return addStaticLibrarySource(self, name, convertOptionalPathToFileSource(root_src));451 return LibExeObjStep.create(b, .{
452 .name = options.name,
453 .root_source_file = options.root_source_file,
454 .target = options.target,
455 .optimize = options.optimize,
456 .kind = .obj,
457 });
464 }458 }
465459
466 pub fn addStaticLibrarySource(self: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {460 pub const SharedLibraryOptions = struct {
467 return LibExeObjStep.createStaticLibrary(self, name, root_src);461 name: []const u8,
468 }462 root_source_file: ?FileSource,
463 version: ?std.builtin.Version = null,
464 target: CrossTarget,
465 optimize: std.builtin.Mode,
466 };
469467
470 pub fn addTest(self: *Builder, root_src: []const u8) *LibExeObjStep {468 pub fn addSharedLibrary(b: *Builder, options: SharedLibraryOptions) *LibExeObjStep {
471 return LibExeObjStep.createTest(self, "test", .{ .path = root_src });469 return LibExeObjStep.create(b, .{
470 .name = options.name,
471 .root_source_file = options.root_source_file,
472 .kind = .lib,
473 .linkage = .dynamic,
474 .version = options.version,
475 .target = options.target,
476 .optimize = options.optimize,
477 });
472 }478 }
473479
474 pub fn addTestSource(self: *Builder, root_src: FileSource) *LibExeObjStep {480 pub const StaticLibraryOptions = struct {
475 return LibExeObjStep.createTest(self, "test", root_src.dupe(self));481 name: []const u8,
476 }482 root_source_file: ?FileSource = null,
483 target: CrossTarget,
484 optimize: std.builtin.Mode,
485 version: ?std.builtin.Version = null,
486 };
477487
478 pub fn addTestExe(self: *Builder, name: []const u8, root_src: []const u8) *LibExeObjStep {488 pub fn addStaticLibrary(b: *Builder, options: StaticLibraryOptions) *LibExeObjStep {
479 return LibExeObjStep.createTestExe(self, name, .{ .path = root_src });489 return LibExeObjStep.create(b, .{
480 }490 .name = options.name,
491 .root_source_file = options.root_source_file,
492 .kind = .lib,
493 .linkage = .static,
494 .version = options.version,
495 .target = options.target,
496 .optimize = options.optimize,
497 });
498 }
499
500 pub const TestOptions = struct {
501 name: []const u8 = "test",
502 kind: LibExeObjStep.Kind = .@"test",
503 root_source_file: FileSource,
504 target: CrossTarget,
505 optimize: std.builtin.Mode,
506 version: ?std.builtin.Version = null,
507 };
481508
482 pub fn addTestExeSource(self: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {509 pub fn addTest(b: *Builder, options: TestOptions) *LibExeObjStep {
483 return LibExeObjStep.createTestExe(self, name, root_src.dupe(self));510 return LibExeObjStep.create(b, .{
511 .name = options.name,
512 .kind = options.kind,
513 .root_source_file = options.root_source_file,
514 .target = options.target,
515 .optimize = options.optimize,
516 });
484 }517 }
485518
486 pub fn addAssemble(self: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {519 pub const AssemblyOptions = struct {
487 return addAssembleSource(self, name, .{ .path = src });520 name: []const u8,
488 }521 source_file: FileSource,
522 target: CrossTarget,
523 optimize: std.builtin.Mode,
524 };
489525
490 pub fn addAssembleSource(self: *Builder, name: []const u8, src: FileSource) *LibExeObjStep {526 pub fn addAssembly(b: *Builder, options: AssemblyOptions) *LibExeObjStep {
491 const obj_step = LibExeObjStep.createObject(self, name, null);527 const obj_step = LibExeObjStep.create(b, .{
492 obj_step.addAssemblyFileSource(src.dupe(self));528 .name = options.name,
529 .root_source_file = null,
530 .target = options.target,
531 .optimize = options.optimize,
532 });
533 obj_step.addAssemblyFileSource(options.source_file.dupe(b));
493 return obj_step;534 return obj_step;
494 }535 }
495536
...@@ -593,17 +634,6 @@ pub const Builder = struct {...@@ -593,17 +634,6 @@ pub const Builder = struct {
593 return TranslateCStep.create(self, source.dupe(self));634 return TranslateCStep.create(self, source.dupe(self));
594 }635 }
595636
596 pub fn version(self: *const Builder, major: u32, minor: u32, patch: u32) LibExeObjStep.SharedLibKind {
597 _ = self;
598 return .{
599 .versioned = .{
600 .major = major,
601 .minor = minor,
602 .patch = patch,
603 },
604 };
605 }
606
607 pub fn make(self: *Builder, step_names: []const []const u8) !void {637 pub fn make(self: *Builder, step_names: []const []const u8) !void {
608 try self.makePath(self.cache_root);638 try self.makePath(self.cache_root);
609639
lib/std/build/LibExeObjStep.zig+22-59
...@@ -36,14 +36,14 @@ pub const base_id = .lib_exe_obj;...@@ -36,14 +36,14 @@ pub const base_id = .lib_exe_obj;
36step: Step,36step: Step,
37builder: *Builder,37builder: *Builder,
38name: []const u8,38name: []const u8,
39target: CrossTarget = CrossTarget{},39target: CrossTarget,
40target_info: NativeTargetInfo,40target_info: NativeTargetInfo,
41optimize: std.builtin.Mode,
41linker_script: ?FileSource = null,42linker_script: ?FileSource = null,
42version_script: ?[]const u8 = null,43version_script: ?[]const u8 = null,
43out_filename: []const u8,44out_filename: []const u8,
44linkage: ?Linkage = null,45linkage: ?Linkage = null,
45version: ?std.builtin.Version,46version: ?std.builtin.Version,
46build_mode: std.builtin.Mode,
47kind: Kind,47kind: Kind,
48major_only_filename: ?[]const u8,48major_only_filename: ?[]const u8,
49name_only_filename: ?[]const u8,49name_only_filename: ?[]const u8,
...@@ -271,6 +271,16 @@ pub const IncludeDir = union(enum) {...@@ -271,6 +271,16 @@ pub const IncludeDir = union(enum) {
271 config_header_step: *ConfigHeaderStep,271 config_header_step: *ConfigHeaderStep,
272};272};
273273
274pub const Options = struct {
275 name: []const u8,
276 root_source_file: ?FileSource = null,
277 target: CrossTarget,
278 optimize: std.builtin.Mode,
279 kind: Kind,
280 linkage: ?Linkage = null,
281 version: ?std.builtin.Version = null,
282};
283
274pub const Kind = enum {284pub const Kind = enum {
275 exe,285 exe,
276 lib,286 lib,
...@@ -279,11 +289,6 @@ pub const Kind = enum {...@@ -279,11 +289,6 @@ pub const Kind = enum {
279 test_exe,289 test_exe,
280};290};
281291
282pub const SharedLibKind = union(enum) {
283 versioned: std.builtin.Version,
284 unversioned: void,
285};
286
287pub const Linkage = enum { dynamic, static };292pub const Linkage = enum { dynamic, static };
288293
289pub const EmitOption = union(enum) {294pub const EmitOption = union(enum) {
...@@ -302,43 +307,9 @@ pub const EmitOption = union(enum) {...@@ -302,43 +307,9 @@ pub const EmitOption = union(enum) {
302 }307 }
303};308};
304309
305pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource, kind: SharedLibKind) *LibExeObjStep {310pub fn create(builder: *Builder, options: Options) *LibExeObjStep {
306 return initExtraArgs(builder, name, root_src, .lib, .dynamic, switch (kind) {311 const name = builder.dupe(options.name);
307 .versioned => |ver| ver,312 const root_src: ?FileSource = if (options.root_source_file) |rsrc| rsrc.dupe(builder) else null;
308 .unversioned => null,
309 });
310}
311
312pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
313 return initExtraArgs(builder, name, root_src, .lib, .static, null);
314}
315
316pub fn createObject(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
317 return initExtraArgs(builder, name, root_src, .obj, null, null);
318}
319
320pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
321 return initExtraArgs(builder, name, root_src, .exe, null, null);
322}
323
324pub fn createTest(builder: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {
325 return initExtraArgs(builder, name, root_src, .@"test", null, null);
326}
327
328pub fn createTestExe(builder: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {
329 return initExtraArgs(builder, name, root_src, .test_exe, null, null);
330}
331
332fn initExtraArgs(
333 builder: *Builder,
334 name_raw: []const u8,
335 root_src_raw: ?FileSource,
336 kind: Kind,
337 linkage: ?Linkage,
338 ver: ?std.builtin.Version,
339) *LibExeObjStep {
340 const name = builder.dupe(name_raw);
341 const root_src: ?FileSource = if (root_src_raw) |rsrc| rsrc.dupe(builder) else null;
342 if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) {313 if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) {
343 panic("invalid name: '{s}'. It looks like a file path, but it is supposed to be the library or application name.", .{name});314 panic("invalid name: '{s}'. It looks like a file path, but it is supposed to be the library or application name.", .{name});
344 }315 }
...@@ -350,14 +321,15 @@ fn initExtraArgs(...@@ -350,14 +321,15 @@ fn initExtraArgs(
350 .builder = builder,321 .builder = builder,
351 .verbose_link = false,322 .verbose_link = false,
352 .verbose_cc = false,323 .verbose_cc = false,
353 .build_mode = std.builtin.Mode.Debug,324 .optimize = options.optimize,
354 .linkage = linkage,325 .target = options.target,
355 .kind = kind,326 .linkage = options.linkage,
327 .kind = options.kind,
356 .root_src = root_src,328 .root_src = root_src,
357 .name = name,329 .name = name,
358 .frameworks = StringHashMap(FrameworkLinkInfo).init(builder.allocator),330 .frameworks = StringHashMap(FrameworkLinkInfo).init(builder.allocator),
359 .step = Step.init(base_id, name, builder.allocator, make),331 .step = Step.init(base_id, name, builder.allocator, make),
360 .version = ver,332 .version = options.version,
361 .out_filename = undefined,333 .out_filename = undefined,
362 .out_h_filename = builder.fmt("{s}.h", .{name}),334 .out_h_filename = builder.fmt("{s}.h", .{name}),
363 .out_lib_filename = undefined,335 .out_lib_filename = undefined,
...@@ -457,11 +429,6 @@ fn computeOutFileNames(self: *LibExeObjStep) void {...@@ -457,11 +429,6 @@ fn computeOutFileNames(self: *LibExeObjStep) void {
457 }429 }
458}430}
459431
460pub fn setTarget(self: *LibExeObjStep, target: CrossTarget) void {
461 self.target = target;
462 self.computeOutFileNames();
463}
464
465pub fn setOutputDir(self: *LibExeObjStep, dir: []const u8) void {432pub fn setOutputDir(self: *LibExeObjStep, dir: []const u8) void {
466 self.output_dir = self.builder.dupePath(dir);433 self.output_dir = self.builder.dupePath(dir);
467}434}
...@@ -889,10 +856,6 @@ pub fn setVerboseCC(self: *LibExeObjStep, value: bool) void {...@@ -889,10 +856,6 @@ pub fn setVerboseCC(self: *LibExeObjStep, value: bool) void {
889 self.verbose_cc = value;856 self.verbose_cc = value;
890}857}
891858
892pub fn setBuildMode(self: *LibExeObjStep, mode: std.builtin.Mode) void {
893 self.build_mode = mode;
894}
895
896pub fn overrideZigLibDir(self: *LibExeObjStep, dir_path: []const u8) void {859pub fn overrideZigLibDir(self: *LibExeObjStep, dir_path: []const u8) void {
897 self.override_lib_dir = self.builder.dupePath(dir_path);860 self.override_lib_dir = self.builder.dupePath(dir_path);
898}861}
...@@ -1376,9 +1339,9 @@ fn make(step: *Step) !void {...@@ -1376,9 +1339,9 @@ fn make(step: *Step) !void {
1376 try zig_args.append(libc_file);1339 try zig_args.append(libc_file);
1377 }1340 }
13781341
1379 switch (self.build_mode) {1342 switch (self.optimize) {
1380 .Debug => {}, // Skip since it's the default.1343 .Debug => {}, // Skip since it's the default.
1381 else => zig_args.append(builder.fmt("-O{s}", .{@tagName(self.build_mode)})) catch unreachable,1344 else => zig_args.append(builder.fmt("-O{s}", .{@tagName(self.optimize)})) catch unreachable,
1382 }1345 }
13831346
1384 try zig_args.append("--cache-dir");1347 try zig_args.append("--cache-dir");