Skip to content

Commit 2329a72

Browse files
committed
* Add raw parameter to stage1 build-exe --emit
* Add --custom-ext to stage1 build-exe, allow to customize the exectuable extension for --emit bin or --emit raw * Add setEmit to Zig build system * Add addExecutableCustomExtension to Zig build system to customize the executable extension, useful for emit raw * Implement emit_raw in stage1/stage2 Zig that does a similar job to llvm-objcopy * Add custom_extension and emit_raw / no_emit_raw to cache digest Part of #2826
1 parent aaa2f9a commit 2329a72

File tree

11 files changed

+476
-77
lines changed

11 files changed

+476
-77
lines changed

lib/std/build.zig

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ pub const Builder = struct {
214214
name,
215215
if (root_src) |p| FileSource{ .path = p } else null,
216216
false,
217+
null,
218+
);
219+
}
220+
221+
pub fn addExecutableCustomExtension(self: *Builder, name: []const u8, root_src: ?[]const u8, custom_extension: ?[] const u8) *LibExeObjStep {
222+
return LibExeObjStep.createExecutable(
223+
self,
224+
name,
225+
if (root_src) |p| FileSource{ .path = p } else null,
226+
false,
227+
custom_extension,
217228
);
218229
}
219230

@@ -228,15 +239,15 @@ pub const Builder = struct {
228239
.step = wfs,
229240
.basename = basename,
230241
},
231-
}), false);
242+
}), false, null);
232243
}
233244

234245
pub fn addExecutableSource(
235246
self: *Builder,
236247
name: []const u8,
237248
root_src: ?FileSource,
238249
) *LibExeObjStep {
239-
return LibExeObjStep.createExecutable(self, name, root_src, false);
250+
return LibExeObjStep.createExecutable(self, name, root_src, false, null);
240251
}
241252

242253
pub fn addObject(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
@@ -1129,6 +1140,7 @@ pub const LibExeObjStep = struct {
11291140
version: Version,
11301141
build_mode: builtin.Mode,
11311142
kind: Kind,
1143+
emit: ?Emit,
11321144
major_only_filename: []const u8,
11331145
name_only_filename: []const u8,
11341146
strip: bool,
@@ -1199,6 +1211,8 @@ pub const LibExeObjStep = struct {
11991211

12001212
subsystem: ?builtin.SubSystem = null,
12011213

1214+
custom_extension: ?[]const u8 = null,
1215+
12021216
const LinkObject = union(enum) {
12031217
StaticPath: []const u8,
12041218
OtherStep: *LibExeObjStep,
@@ -1220,33 +1234,40 @@ pub const LibExeObjStep = struct {
12201234
Test,
12211235
};
12221236

1237+
const Emit = enum {
1238+
Asm,
1239+
Bin,
1240+
LLVMIr,
1241+
Raw,
1242+
};
1243+
12231244
pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource, ver: Version) *LibExeObjStep {
12241245
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1225-
self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, ver);
1246+
self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, ver, null);
12261247
return self;
12271248
}
12281249

12291250
pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
12301251
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1231-
self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, builder.version(0, 0, 0));
1252+
self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, builder.version(0, 0, 0), null);
12321253
return self;
12331254
}
12341255

12351256
pub fn createObject(builder: *Builder, name: []const u8, root_src: ?FileSource) *LibExeObjStep {
12361257
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1237-
self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
1258+
self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0), null);
12381259
return self;
12391260
}
12401261

1241-
pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?FileSource, is_dynamic: bool) *LibExeObjStep {
1262+
pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?FileSource, is_dynamic: bool, custom_extension: ?[]const u8,) *LibExeObjStep {
12421263
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1243-
self.* = initExtraArgs(builder, name, root_src, Kind.Exe, is_dynamic, builder.version(0, 0, 0));
1264+
self.* = initExtraArgs(builder, name, root_src, Kind.Exe, is_dynamic, builder.version(0, 0, 0), custom_extension);
12441265
return self;
12451266
}
12461267

12471268
pub fn createTest(builder: *Builder, name: []const u8, root_src: FileSource) *LibExeObjStep {
12481269
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
1249-
self.* = initExtraArgs(builder, name, root_src, Kind.Test, false, builder.version(0, 0, 0));
1270+
self.* = initExtraArgs(builder, name, root_src, Kind.Test, false, builder.version(0, 0, 0), null);
12501271
return self;
12511272
}
12521273

@@ -1257,6 +1278,7 @@ pub const LibExeObjStep = struct {
12571278
kind: Kind,
12581279
is_dynamic: bool,
12591280
ver: Version,
1281+
custom_extension: ?[]const u8,
12601282
) LibExeObjStep {
12611283
if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) {
12621284
panic("invalid name: '{}'. It looks like a file path, but it is supposed to be the library or application name.", .{name});
@@ -1269,6 +1291,7 @@ pub const LibExeObjStep = struct {
12691291
.build_mode = builtin.Mode.Debug,
12701292
.is_dynamic = is_dynamic,
12711293
.kind = kind,
1294+
.emit = null,
12721295
.root_src = root_src,
12731296
.name = name,
12741297
.target = Target.Native,
@@ -1305,6 +1328,7 @@ pub const LibExeObjStep = struct {
13051328
.single_threaded = false,
13061329
.installed_path = null,
13071330
.install_step = null,
1331+
.custom_extension = custom_extension,
13081332
};
13091333
self.computeOutFileNames();
13101334
if (root_src) |rs| rs.addStepDependencies(&self.step);
@@ -1317,7 +1341,13 @@ pub const LibExeObjStep = struct {
13171341
self.out_filename = self.builder.fmt("{}{}", .{ self.name, self.target.oFileExt() });
13181342
},
13191343
.Exe => {
1320-
self.out_filename = self.builder.fmt("{}{}", .{ self.name, self.target.exeFileExt() });
1344+
const extension = blk: {
1345+
if (self.custom_extension) |ext| {
1346+
break :blk ext;
1347+
}
1348+
break :blk self.target.exeFileExt();
1349+
};
1350+
self.out_filename = self.builder.fmt("{}{}", .{ self.name, extension });
13211351
},
13221352
.Test => {
13231353
self.out_filename = self.builder.fmt("test{}", .{self.target.exeFileExt()});
@@ -1384,6 +1414,10 @@ pub const LibExeObjStep = struct {
13841414
self.computeOutFileNames();
13851415
}
13861416

1417+
pub fn setEmit(self: *LibExeObjStep, emit: Emit) void {
1418+
self.emit = emit;
1419+
}
1420+
13871421
pub fn setTargetGLibC(self: *LibExeObjStep, major: u32, minor: u32, patch: u32) void {
13881422
self.target_glibc = Version{
13891423
.major = major,
@@ -1835,6 +1869,18 @@ pub const LibExeObjStep = struct {
18351869

18361870
if (self.root_src) |root_src| try zig_args.append(root_src.getPath(builder));
18371871

1872+
if (self.emit) |emit| {
1873+
try zig_args.append("--emit");
1874+
1875+
const emitArg = switch (emit) {
1876+
.Asm => "asm",
1877+
.Bin => "bin",
1878+
.LLVMIr => "llvm-ir",
1879+
.Raw => "raw",
1880+
};
1881+
try zig_args.append(emitArg);
1882+
}
1883+
18381884
for (self.link_objects.toSlice()) |link_object| {
18391885
switch (link_object) {
18401886
.StaticPath => |static_path| {
@@ -1942,6 +1988,11 @@ pub const LibExeObjStep = struct {
19421988
zig_args.append("--name") catch unreachable;
19431989
zig_args.append(self.name) catch unreachable;
19441990

1991+
if (self.custom_extension) |ext| {
1992+
try zig_args.append("--custom-ext");
1993+
try zig_args.append(ext);
1994+
}
1995+
19451996
if (self.kind == Kind.Lib and self.is_dynamic) {
19461997
zig_args.append("--ver-major") catch unreachable;
19471998
zig_args.append(builder.fmt("{}", .{self.version.major})) catch unreachable;

lib/std/debug.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,8 @@ fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
928928
fn findDwarfSectionFromElf(elf_file: *elf.Elf, name: []const u8) !?DwarfInfo.Section {
929929
const elf_header = (try elf_file.findSection(name)) orelse return null;
930930
return DwarfInfo.Section{
931-
.offset = elf_header.offset,
932-
.size = elf_header.size,
931+
.offset = elf_header.sh_offset,
932+
.size = elf_header.sh_size,
933933
};
934934
}
935935

0 commit comments

Comments
 (0)