Commit 71ff60f126

Andrew Kelley <andrew@ziglang.org>
2023-01-29 01:14:06
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.
1 parent 063888a
Changed files (2)
lib/std/build/LibExeObjStep.zig
@@ -36,14 +36,14 @@ pub const base_id = .lib_exe_obj;
 step: Step,
 builder: *Builder,
 name: []const u8,
-target: CrossTarget = CrossTarget{},
+target: CrossTarget,
 target_info: NativeTargetInfo,
+optimize: std.builtin.Mode,
 linker_script: ?FileSource = null,
 version_script: ?[]const u8 = null,
 out_filename: []const u8,
 linkage: ?Linkage = null,
 version: ?std.builtin.Version,
-build_mode: std.builtin.Mode,
 kind: Kind,
 major_only_filename: ?[]const u8,
 name_only_filename: ?[]const u8,
@@ -271,6 +271,16 @@ pub const IncludeDir = union(enum) {
     config_header_step: *ConfigHeaderStep,
 };
 
+pub const Options = struct {
+    name: []const u8,
+    root_source_file: ?FileSource = null,
+    target: CrossTarget,
+    optimize: std.builtin.Mode,
+    kind: Kind,
+    linkage: ?Linkage = null,
+    version: ?std.builtin.Version = null,
+};
+
 pub const Kind = enum {
     exe,
     lib,
@@ -279,11 +289,6 @@ pub const Kind = enum {
     test_exe,
 };
 
-pub const SharedLibKind = union(enum) {
-    versioned: std.builtin.Version,
-    unversioned: void,
-};
-
 pub const Linkage = enum { dynamic, static };
 
 pub const EmitOption = union(enum) {
@@ -302,43 +307,9 @@ pub const EmitOption = union(enum) {
     }
 };
 
-pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource, kind: SharedLibKind) *LibExeObjStep {
-    return initExtraArgs(builder, name, root_src, .lib, .dynamic, switch (kind) {
-        .versioned => |ver| ver,
-        .unversioned => null,
-    });
-}
-
-pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
-    return initExtraArgs(builder, name, root_src, .lib, .static, null);
-}
-
-pub fn createObject(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
-    return initExtraArgs(builder, name, root_src, .obj, null, null);
-}
-
-pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
-    return initExtraArgs(builder, name, root_src, .exe, null, null);
-}
-
-pub fn createTest(builder: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {
-    return initExtraArgs(builder, name, root_src, .@"test", null, null);
-}
-
-pub fn createTestExe(builder: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {
-    return initExtraArgs(builder, name, root_src, .test_exe, null, null);
-}
-
-fn initExtraArgs(
-    builder: *Builder,
-    name_raw: []const u8,
-    root_src_raw: ?FileSource,
-    kind: Kind,
-    linkage: ?Linkage,
-    ver: ?std.builtin.Version,
-) *LibExeObjStep {
-    const name = builder.dupe(name_raw);
-    const root_src: ?FileSource = if (root_src_raw) |rsrc| rsrc.dupe(builder) else null;
+pub fn create(builder: *Builder, options: Options) *LibExeObjStep {
+    const name = builder.dupe(options.name);
+    const root_src: ?FileSource = if (options.root_source_file) |rsrc| rsrc.dupe(builder) else null;
     if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) {
         panic("invalid name: '{s}'. It looks like a file path, but it is supposed to be the library or application name.", .{name});
     }
@@ -350,14 +321,15 @@ fn initExtraArgs(
         .builder = builder,
         .verbose_link = false,
         .verbose_cc = false,
-        .build_mode = std.builtin.Mode.Debug,
-        .linkage = linkage,
-        .kind = kind,
+        .optimize = options.optimize,
+        .target = options.target,
+        .linkage = options.linkage,
+        .kind = options.kind,
         .root_src = root_src,
         .name = name,
         .frameworks = StringHashMap(FrameworkLinkInfo).init(builder.allocator),
         .step = Step.init(base_id, name, builder.allocator, make),
-        .version = ver,
+        .version = options.version,
         .out_filename = undefined,
         .out_h_filename = builder.fmt("{s}.h", .{name}),
         .out_lib_filename = undefined,
@@ -457,11 +429,6 @@ fn computeOutFileNames(self: *LibExeObjStep) void {
     }
 }
 
-pub fn setTarget(self: *LibExeObjStep, target: CrossTarget) void {
-    self.target = target;
-    self.computeOutFileNames();
-}
-
 pub fn setOutputDir(self: *LibExeObjStep, dir: []const u8) void {
     self.output_dir = self.builder.dupePath(dir);
 }
@@ -889,10 +856,6 @@ pub fn setVerboseCC(self: *LibExeObjStep, value: bool) void {
     self.verbose_cc = value;
 }
 
-pub fn setBuildMode(self: *LibExeObjStep, mode: std.builtin.Mode) void {
-    self.build_mode = mode;
-}
-
 pub fn overrideZigLibDir(self: *LibExeObjStep, dir_path: []const u8) void {
     self.override_lib_dir = self.builder.dupePath(dir_path);
 }
@@ -1376,9 +1339,9 @@ fn make(step: *Step) !void {
         try zig_args.append(libc_file);
     }
 
-    switch (self.build_mode) {
+    switch (self.optimize) {
         .Debug => {}, // Skip since it's the default.
-        else => zig_args.append(builder.fmt("-O{s}", .{@tagName(self.build_mode)})) catch unreachable,
+        else => zig_args.append(builder.fmt("-O{s}", .{@tagName(self.optimize)})) catch unreachable,
     }
 
     try zig_args.append("--cache-dir");
lib/std/build.zig
@@ -414,82 +414,123 @@ pub const Builder = struct {
         self.h_dir = self.pathJoin(&h_list);
     }
 
-    fn convertOptionalPathToFileSource(path: ?[]const u8) ?FileSource {
-        return if (path) |p|
-            FileSource{ .path = p }
-        else
-            null;
-    }
-
-    pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
-        return addExecutableSource(self, name, convertOptionalPathToFileSource(root_src));
-    }
-
-    pub fn addExecutableSource(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
-        return LibExeObjStep.createExecutable(builder, name, root_src);
-    }
-
     pub fn addOptions(self: *Builder) *OptionsStep {
         return OptionsStep.create(self);
     }
 
-    pub fn addObject(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
-        return addObjectSource(self, name, convertOptionalPathToFileSource(root_src));
-    }
-
-    pub fn addObjectSource(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
-        return LibExeObjStep.createObject(builder, name, root_src);
-    }
-
-    pub fn addSharedLibrary(
-        self: *Builder,
+    pub const ExecutableOptions = struct {
         name: []const u8,
-        root_src: ?[]const u8,
-        kind: LibExeObjStep.SharedLibKind,
-    ) *LibExeObjStep {
-        return addSharedLibrarySource(self, name, convertOptionalPathToFileSource(root_src), kind);
+        root_source_file: ?FileSource,
+        version: ?std.builtin.Version = null,
+        target: CrossTarget,
+        optimize: std.builtin.Mode,
+        linkage: ?LibExeObjStep.Linkage = null,
+    };
+
+    pub fn addExecutable(b: *Builder, options: ExecutableOptions) *LibExeObjStep {
+        return LibExeObjStep.create(b, .{
+            .name = options.name,
+            .root_source_file = options.root_source_file,
+            .version = options.version,
+            .target = options.target,
+            .optimize = options.optimize,
+            .kind = .exe,
+            .linkage = options.linkage,
+            .version = options.version,
+        });
     }
 
-    pub fn addSharedLibrarySource(
-        self: *Builder,
+    pub const ObjectOptions = struct {
         name: []const u8,
-        root_src: ?FileSource,
-        kind: LibExeObjStep.SharedLibKind,
-    ) *LibExeObjStep {
-        return LibExeObjStep.createSharedLibrary(self, name, root_src, kind);
-    }
+        root_source_file: ?FileSource,
+        target: CrossTarget,
+        optimize: std.builtin.Mode,
+    };
 
-    pub fn addStaticLibrary(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
-        return addStaticLibrarySource(self, name, convertOptionalPathToFileSource(root_src));
+    pub fn addObject(b: *Builder, options: ObjectOptions) *LibExeObjStep {
+        return LibExeObjStep.create(b, .{
+            .name = options.name,
+            .root_source_file = options.root_source_file,
+            .target = options.target,
+            .optimize = options.optimize,
+            .kind = .obj,
+        });
     }
 
-    pub fn addStaticLibrarySource(self: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
-        return LibExeObjStep.createStaticLibrary(self, name, root_src);
-    }
+    pub const SharedLibraryOptions = struct {
+        name: []const u8,
+        root_source_file: ?FileSource,
+        version: ?std.builtin.Version = null,
+        target: CrossTarget,
+        optimize: std.builtin.Mode,
+    };
 
-    pub fn addTest(self: *Builder, root_src: []const u8) *LibExeObjStep {
-        return LibExeObjStep.createTest(self, "test", .{ .path = root_src });
+    pub fn addSharedLibrary(b: *Builder, options: SharedLibraryOptions) *LibExeObjStep {
+        return LibExeObjStep.create(b, .{
+            .name = options.name,
+            .root_source_file = options.root_source_file,
+            .kind = .lib,
+            .linkage = .dynamic,
+            .version = options.version,
+            .target = options.target,
+            .optimize = options.optimize,
+        });
     }
 
-    pub fn addTestSource(self: *Builder, root_src: FileSource) *LibExeObjStep {
-        return LibExeObjStep.createTest(self, "test", root_src.dupe(self));
-    }
+    pub const StaticLibraryOptions = struct {
+        name: []const u8,
+        root_source_file: ?FileSource = null,
+        target: CrossTarget,
+        optimize: std.builtin.Mode,
+        version: ?std.builtin.Version = null,
+    };
 
-    pub fn addTestExe(self: *Builder, name: []const u8, root_src: []const u8) *LibExeObjStep {
-        return LibExeObjStep.createTestExe(self, name, .{ .path = root_src });
-    }
+    pub fn addStaticLibrary(b: *Builder, options: StaticLibraryOptions) *LibExeObjStep {
+        return LibExeObjStep.create(b, .{
+            .name = options.name,
+            .root_source_file = options.root_source_file,
+            .kind = .lib,
+            .linkage = .static,
+            .version = options.version,
+            .target = options.target,
+            .optimize = options.optimize,
+        });
+    }
+
+    pub const TestOptions = struct {
+        name: []const u8 = "test",
+        kind: LibExeObjStep.Kind = .@"test",
+        root_source_file: FileSource,
+        target: CrossTarget,
+        optimize: std.builtin.Mode,
+        version: ?std.builtin.Version = null,
+    };
 
-    pub fn addTestExeSource(self: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {
-        return LibExeObjStep.createTestExe(self, name, root_src.dupe(self));
+    pub fn addTest(b: *Builder, options: TestOptions) *LibExeObjStep {
+        return LibExeObjStep.create(b, .{
+            .name = options.name,
+            .kind = options.kind,
+            .root_source_file = options.root_source_file,
+            .target = options.target,
+            .optimize = options.optimize,
+        });
     }
 
-    pub fn addAssemble(self: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
-        return addAssembleSource(self, name, .{ .path = src });
-    }
+    pub const AssemblyOptions = struct {
+        name: []const u8,
+        source_file: FileSource,
+        target: CrossTarget,
+        optimize: std.builtin.Mode,
+    };
 
-    pub fn addAssembleSource(self: *Builder, name: []const u8, src: FileSource) *LibExeObjStep {
-        const obj_step = LibExeObjStep.createObject(self, name, null);
-        obj_step.addAssemblyFileSource(src.dupe(self));
+    pub fn addAssembly(b: *Builder, options: AssemblyOptions) *LibExeObjStep {
+        const obj_step = LibExeObjStep.create(b, .{
+            .name = options.name,
+            .root_source_file = null,
+            .target = options.target,
+            .optimize = options.optimize,
+        });
+        obj_step.addAssemblyFileSource(options.source_file.dupe(b));
         return obj_step;
     }
 
@@ -593,17 +634,6 @@ pub const Builder = struct {
         return TranslateCStep.create(self, source.dupe(self));
     }
 
-    pub fn version(self: *const Builder, major: u32, minor: u32, patch: u32) LibExeObjStep.SharedLibKind {
-        _ = self;
-        return .{
-            .versioned = .{
-                .major = major,
-                .minor = minor,
-                .patch = patch,
-            },
-        };
-    }
-
     pub fn make(self: *Builder, step_names: []const []const u8) !void {
         try self.makePath(self.cache_root);