mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 07:03:11 +00:00
This change is seemingly insignificant but I actually agonized over this
for three days. Some other things I considered:
* (status quo in master branch) make Compile step creation functions
accept a Target.Query and delete the ResolvedTarget struct.
- downside: redundantly resolve target queries many times
* same as before but additionally add a hash map to cache target query
resolutions.
- downside: now there is a hash map that doesn't actually need to
exist, just to make the API more ergonomic.
* add is_native_os and is_native_abi fields to std.Target and use it
directly as the result of resolving a target query.
- downside: they really don't belong there. They would be available
as comptime booleans via `@import("builtin")` but they should not
be exposed that way.
With this change the downsides are:
* the option name of addExecutable and friends is `target` instead of
`resolved_target` matching the type name.
- upside: this does not break compatibility with existing build
scripts
* you likely end up seeing `target.result.cpu.arch` rather than
`target.cpu.arch`.
- upside: this is an improvement over `target.target.cpu.arch` which
it was before this commit.
- downside: `b.host.target` is now `b.host.result`.
205 lines
6.9 KiB
Zig
205 lines
6.9 KiB
Zig
pub fn build(b: *Build) void {
|
|
const test_step = b.step("test-link", "Run link tests");
|
|
b.default_step = test_step;
|
|
|
|
test_step.dependOn(@import("elf.zig").testAll(b));
|
|
test_step.dependOn(@import("macho.zig").testAll(b));
|
|
}
|
|
|
|
pub const Options = struct {
|
|
target: std.Build.ResolvedTarget,
|
|
optimize: std.builtin.OptimizeMode = .Debug,
|
|
use_llvm: bool = true,
|
|
use_lld: bool = false,
|
|
};
|
|
|
|
pub fn addTestStep(b: *Build, prefix: []const u8, opts: Options) *Step {
|
|
const target = opts.target.result.zigTriple(b.allocator) catch @panic("OOM");
|
|
const optimize = @tagName(opts.optimize);
|
|
const use_llvm = if (opts.use_llvm) "llvm" else "no-llvm";
|
|
const name = std.fmt.allocPrint(b.allocator, "test-{s}-{s}-{s}-{s}", .{
|
|
prefix, target, optimize, use_llvm,
|
|
}) catch @panic("OOM");
|
|
return b.step(name, "");
|
|
}
|
|
|
|
const OverlayOptions = struct {
|
|
name: []const u8,
|
|
asm_source_bytes: ?[]const u8 = null,
|
|
c_source_bytes: ?[]const u8 = null,
|
|
c_source_flags: []const []const u8 = &.{},
|
|
cpp_source_bytes: ?[]const u8 = null,
|
|
cpp_source_flags: []const []const u8 = &.{},
|
|
zig_source_bytes: ?[]const u8 = null,
|
|
pic: ?bool = null,
|
|
strip: ?bool = null,
|
|
};
|
|
|
|
pub fn addExecutable(b: *std.Build, base: Options, overlay: OverlayOptions) *Step.Compile {
|
|
const compile_step = b.addExecutable(.{
|
|
.name = overlay.name,
|
|
.root_source_file = rsf: {
|
|
const bytes = overlay.zig_source_bytes orelse break :rsf null;
|
|
break :rsf b.addWriteFiles().add("a.zig", bytes);
|
|
},
|
|
.target = base.target,
|
|
.optimize = base.optimize,
|
|
.use_llvm = base.use_llvm,
|
|
.use_lld = base.use_lld,
|
|
.pic = overlay.pic,
|
|
.strip = overlay.strip,
|
|
});
|
|
if (overlay.cpp_source_bytes) |bytes| {
|
|
compile_step.addCSourceFile(.{
|
|
.file = b.addWriteFiles().add("a.cpp", bytes),
|
|
.flags = overlay.cpp_source_flags,
|
|
});
|
|
}
|
|
if (overlay.c_source_bytes) |bytes| {
|
|
compile_step.addCSourceFile(.{
|
|
.file = b.addWriteFiles().add("a.c", bytes),
|
|
.flags = overlay.c_source_flags,
|
|
});
|
|
}
|
|
if (overlay.asm_source_bytes) |bytes| {
|
|
compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
|
|
}
|
|
return compile_step;
|
|
}
|
|
|
|
pub fn addObject(b: *Build, base: Options, overlay: OverlayOptions) *Step.Compile {
|
|
const compile_step = b.addObject(.{
|
|
.name = overlay.name,
|
|
.root_source_file = rsf: {
|
|
const bytes = overlay.zig_source_bytes orelse break :rsf null;
|
|
break :rsf b.addWriteFiles().add("a.zig", bytes);
|
|
},
|
|
.target = base.target,
|
|
.optimize = base.optimize,
|
|
.use_llvm = base.use_llvm,
|
|
.use_lld = base.use_lld,
|
|
.pic = overlay.pic,
|
|
.strip = overlay.strip,
|
|
});
|
|
if (overlay.cpp_source_bytes) |bytes| {
|
|
compile_step.addCSourceFile(.{
|
|
.file = b.addWriteFiles().add("a.cpp", bytes),
|
|
.flags = overlay.cpp_source_flags,
|
|
});
|
|
}
|
|
if (overlay.c_source_bytes) |bytes| {
|
|
compile_step.addCSourceFile(.{
|
|
.file = b.addWriteFiles().add("a.c", bytes),
|
|
.flags = overlay.c_source_flags,
|
|
});
|
|
}
|
|
if (overlay.asm_source_bytes) |bytes| {
|
|
compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
|
|
}
|
|
return compile_step;
|
|
}
|
|
|
|
pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
|
|
const compile_step = b.addStaticLibrary(.{
|
|
.name = overlay.name,
|
|
.root_source_file = rsf: {
|
|
const bytes = overlay.zig_source_bytes orelse break :rsf null;
|
|
break :rsf b.addWriteFiles().add("a.zig", bytes);
|
|
},
|
|
.target = base.target,
|
|
.optimize = base.optimize,
|
|
.use_llvm = base.use_llvm,
|
|
.use_lld = base.use_lld,
|
|
.pic = overlay.pic,
|
|
.strip = overlay.strip,
|
|
});
|
|
if (overlay.cpp_source_bytes) |bytes| {
|
|
compile_step.addCSourceFile(.{
|
|
.file = b.addWriteFiles().add("a.cpp", bytes),
|
|
.flags = overlay.cpp_source_flags,
|
|
});
|
|
}
|
|
if (overlay.c_source_bytes) |bytes| {
|
|
compile_step.addCSourceFile(.{
|
|
.file = b.addWriteFiles().add("a.c", bytes),
|
|
.flags = overlay.c_source_flags,
|
|
});
|
|
}
|
|
if (overlay.asm_source_bytes) |bytes| {
|
|
compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
|
|
}
|
|
return compile_step;
|
|
}
|
|
|
|
pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
|
|
const compile_step = b.addSharedLibrary(.{
|
|
.name = overlay.name,
|
|
.root_source_file = rsf: {
|
|
const bytes = overlay.zig_source_bytes orelse break :rsf null;
|
|
break :rsf b.addWriteFiles().add("a.zig", bytes);
|
|
},
|
|
.target = base.target,
|
|
.optimize = base.optimize,
|
|
.use_llvm = base.use_llvm,
|
|
.use_lld = base.use_lld,
|
|
.pic = overlay.pic,
|
|
.strip = overlay.strip,
|
|
});
|
|
if (overlay.cpp_source_bytes) |bytes| {
|
|
compile_step.addCSourceFile(.{
|
|
.file = b.addWriteFiles().add("a.cpp", bytes),
|
|
.flags = overlay.cpp_source_flags,
|
|
});
|
|
}
|
|
if (overlay.c_source_bytes) |bytes| {
|
|
compile_step.addCSourceFile(.{
|
|
.file = b.addWriteFiles().add("a.c", bytes),
|
|
.flags = overlay.c_source_flags,
|
|
});
|
|
}
|
|
if (overlay.asm_source_bytes) |bytes| {
|
|
compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
|
|
}
|
|
return compile_step;
|
|
}
|
|
|
|
pub fn addRunArtifact(comp: *Compile) *Run {
|
|
const b = comp.step.owner;
|
|
const run = b.addRunArtifact(comp);
|
|
run.skip_foreign_checks = true;
|
|
return run;
|
|
}
|
|
|
|
pub fn addCSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
|
|
const b = comp.step.owner;
|
|
const file = WriteFile.create(b).add("a.c", bytes);
|
|
comp.addCSourceFile(.{ .file = file, .flags = flags });
|
|
}
|
|
|
|
pub fn addCppSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
|
|
const b = comp.step.owner;
|
|
const file = WriteFile.create(b).add("a.cpp", bytes);
|
|
comp.addCSourceFile(.{ .file = file, .flags = flags });
|
|
}
|
|
|
|
pub fn addAsmSourceBytes(comp: *Compile, bytes: []const u8) void {
|
|
const b = comp.step.owner;
|
|
const actual_bytes = std.fmt.allocPrint(b.allocator, "{s}\n", .{bytes}) catch @panic("OOM");
|
|
const file = WriteFile.create(b).add("a.s", actual_bytes);
|
|
comp.addAssemblyFile(file);
|
|
}
|
|
|
|
pub fn expectLinkErrors(comp: *Compile, test_step: *Step, expected_errors: Compile.ExpectedCompileErrors) void {
|
|
comp.expect_errors = expected_errors;
|
|
const bin_file = comp.getEmittedBin();
|
|
bin_file.addStepDependencies(test_step);
|
|
}
|
|
|
|
const std = @import("std");
|
|
|
|
const Build = std.Build;
|
|
const Compile = Step.Compile;
|
|
const Run = Step.Run;
|
|
const Step = Build.Step;
|
|
const WriteFile = Step.WriteFile;
|