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 {
414414 self.h_dir = self.pathJoin(&h_list);
415415 }
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
432417 pub fn addOptions(self: *Builder) *OptionsStep {
433418 return OptionsStep.create(self);
434419 }
435420
436 pub fn addObject(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
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,
421 pub const ExecutableOptions = struct {
446422 name: []const u8,
447 root_src: ?[]const u8,
448 kind: LibExeObjStep.SharedLibKind,
449 ) *LibExeObjStep {
450 return addSharedLibrarySource(self, name, convertOptionalPathToFileSource(root_src), kind);
423 root_source_file: ?FileSource,
424 version: ?std.builtin.Version = null,
425 target: CrossTarget,
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 });
451441 }
452442
453 pub fn addSharedLibrarySource(
454 self: *Builder,
443 pub const ObjectOptions = struct {
455444 name: []const u8,
456 root_src: ?FileSource,
457 kind: LibExeObjStep.SharedLibKind,
458 ) *LibExeObjStep {
459 return LibExeObjStep.createSharedLibrary(self, name, root_src, kind);
460 }
445 root_source_file: ?FileSource,
446 target: CrossTarget,
447 optimize: std.builtin.Mode,
448 };
461449
462 pub fn addStaticLibrary(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
463 return addStaticLibrarySource(self, name, convertOptionalPathToFileSource(root_src));
450 pub fn addObject(b: *Builder, options: ObjectOptions) *LibExeObjStep {
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 });
464458 }
465459
466 pub fn addStaticLibrarySource(self: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
467 return LibExeObjStep.createStaticLibrary(self, name, root_src);
468 }
460 pub const SharedLibraryOptions = struct {
461 name: []const u8,
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 {
471 return LibExeObjStep.createTest(self, "test", .{ .path = root_src });
468 pub fn addSharedLibrary(b: *Builder, options: SharedLibraryOptions) *LibExeObjStep {
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 });
472478 }
473479
474 pub fn addTestSource(self: *Builder, root_src: FileSource) *LibExeObjStep {
475 return LibExeObjStep.createTest(self, "test", root_src.dupe(self));
476 }
480 pub const StaticLibraryOptions = struct {
481 name: []const u8,
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 {
479 return LibExeObjStep.createTestExe(self, name, .{ .path = root_src });
480 }
488 pub fn addStaticLibrary(b: *Builder, options: StaticLibraryOptions) *LibExeObjStep {
489 return LibExeObjStep.create(b, .{
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 {
483 return LibExeObjStep.createTestExe(self, name, root_src.dupe(self));
509 pub fn addTest(b: *Builder, options: TestOptions) *LibExeObjStep {
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 });
484517 }
485518
486 pub fn addAssemble(self: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
487 return addAssembleSource(self, name, .{ .path = src });
488 }
519 pub const AssemblyOptions = struct {
520 name: []const u8,
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 {
491 const obj_step = LibExeObjStep.createObject(self, name, null);
492 obj_step.addAssemblyFileSource(src.dupe(self));
526 pub fn addAssembly(b: *Builder, options: AssemblyOptions) *LibExeObjStep {
527 const obj_step = LibExeObjStep.create(b, .{
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));
493534 return obj_step;
494535 }
495536
......@@ -593,17 +634,6 @@ pub const Builder = struct {
593634 return TranslateCStep.create(self, source.dupe(self));
594635 }
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
607637 pub fn make(self: *Builder, step_names: []const []const u8) !void {
608638 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;
3636step: Step,
3737builder: *Builder,
3838name: []const u8,
39target: CrossTarget = CrossTarget{},
39target: CrossTarget,
4040target_info: NativeTargetInfo,
41optimize: std.builtin.Mode,
4142linker_script: ?FileSource = null,
4243version_script: ?[]const u8 = null,
4344out_filename: []const u8,
4445linkage: ?Linkage = null,
4546version: ?std.builtin.Version,
46build_mode: std.builtin.Mode,
4747kind: Kind,
4848major_only_filename: ?[]const u8,
4949name_only_filename: ?[]const u8,
......@@ -271,6 +271,16 @@ pub const IncludeDir = union(enum) {
271271 config_header_step: *ConfigHeaderStep,
272272};
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
274284pub const Kind = enum {
275285 exe,
276286 lib,
......@@ -279,11 +289,6 @@ pub const Kind = enum {
279289 test_exe,
280290};
281291
282pub const SharedLibKind = union(enum) {
283 versioned: std.builtin.Version,
284 unversioned: void,
285};
286
287292pub const Linkage = enum { dynamic, static };
288293
289294pub const EmitOption = union(enum) {
......@@ -302,43 +307,9 @@ pub const EmitOption = union(enum) {
302307 }
303308};
304309
305pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource, kind: SharedLibKind) *LibExeObjStep {
306 return initExtraArgs(builder, name, root_src, .lib, .dynamic, switch (kind) {
307 .versioned => |ver| ver,
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;
310pub fn create(builder: *Builder, options: Options) *LibExeObjStep {
311 const name = builder.dupe(options.name);
312 const root_src: ?FileSource = if (options.root_source_file) |rsrc| rsrc.dupe(builder) else null;
342313 if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) {
343314 panic("invalid name: '{s}'. It looks like a file path, but it is supposed to be the library or application name.", .{name});
344315 }
......@@ -350,14 +321,15 @@ fn initExtraArgs(
350321 .builder = builder,
351322 .verbose_link = false,
352323 .verbose_cc = false,
353 .build_mode = std.builtin.Mode.Debug,
354 .linkage = linkage,
355 .kind = kind,
324 .optimize = options.optimize,
325 .target = options.target,
326 .linkage = options.linkage,
327 .kind = options.kind,
356328 .root_src = root_src,
357329 .name = name,
358330 .frameworks = StringHashMap(FrameworkLinkInfo).init(builder.allocator),
359331 .step = Step.init(base_id, name, builder.allocator, make),
360 .version = ver,
332 .version = options.version,
361333 .out_filename = undefined,
362334 .out_h_filename = builder.fmt("{s}.h", .{name}),
363335 .out_lib_filename = undefined,
......@@ -457,11 +429,6 @@ fn computeOutFileNames(self: *LibExeObjStep) void {
457429 }
458430}
459431
460pub fn setTarget(self: *LibExeObjStep, target: CrossTarget) void {
461 self.target = target;
462 self.computeOutFileNames();
463}
464
465432pub fn setOutputDir(self: *LibExeObjStep, dir: []const u8) void {
466433 self.output_dir = self.builder.dupePath(dir);
467434}
......@@ -889,10 +856,6 @@ pub fn setVerboseCC(self: *LibExeObjStep, value: bool) void {
889856 self.verbose_cc = value;
890857}
891858
892pub fn setBuildMode(self: *LibExeObjStep, mode: std.builtin.Mode) void {
893 self.build_mode = mode;
894}
895
896859pub fn overrideZigLibDir(self: *LibExeObjStep, dir_path: []const u8) void {
897860 self.override_lib_dir = self.builder.dupePath(dir_path);
898861}
......@@ -1376,9 +1339,9 @@ fn make(step: *Step) !void {
13761339 try zig_args.append(libc_file);
13771340 }
13781341
1379 switch (self.build_mode) {
1342 switch (self.optimize) {
13801343 .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,
13821345 }
13831346
13841347 try zig_args.append("--cache-dir");