Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LazyPath improvements #28

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 50 additions & 24 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub fn build(b: *Build) !void {
}

// Use LDC2 (https://github.com/ldc-developers/ldc) to compile the D examples
pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDir {
// ldmd2: ldc2 wrapped w/ dmd flags
const ldc = try b.findProgram(&.{"ldmd2"}, &.{});

Expand Down Expand Up @@ -326,11 +326,11 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
}

if (options.includePaths) |includePath| {
for (includePath) |path| {
if (path[0] == '-') {
for (includePath) |dir| {
if (dir[0] == '-') {
@panic("add includepath only!");
}
ldc_exec.addArg(b.fmt("-I{s}", .{path}));
ldc_exec.addArg(b.fmt("-I{s}", .{dir}));
}
}

Expand Down Expand Up @@ -374,14 +374,20 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
// Print character (column) numbers in diagnostics
ldc_exec.addArg("-vcolumns");

const extFile = switch (options.kind) {
.exe, .@"test" => std.Target.exeFileExt(options.target.result),
.lib => if (options.linkage == .static) std.Target.staticLibSuffix(options.target.result) else std.Target.dynamicLibSuffix(options.target.result),
.obj => if (options.target.result.os.tag == .windows) ".obj" else ".o",
};
// object file output (zig-cache/o/{hash_id}/*.o)
var objpath: []const u8 = undefined; // needed for wasm build
if (b.cache_root.path) |path| {
// immutable state hash
objpath = b.pathJoin(&.{ path, "o", &b.graph.cache.hash.peek() });
ldc_exec.addArg(b.fmt("-od={s}", .{objpath}));
const objpath = ldc_exec.addPrefixedOutputFileArg("-of=", try std.mem.concat(b.allocator, u8, &.{ options.name, extFile }));
if (b.cache_root.path) |dir| {
// mutable state hash (ldc2 cache - llvm-ir2obj)
ldc_exec.addArg(b.fmt("-cache={s}", .{b.pathJoin(&.{ path, "o", &b.graph.cache.hash.final() })}));
ldc_exec.addArg(b.fmt("-cache={s}", .{b.pathJoin(&.{
dir,
"o",
&b.graph.cache.hash.final(),
})}));
}

// disable LLVM-IR verifier
Expand All @@ -406,7 +412,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {

// D Source files
for (options.sources) |src| {
ldc_exec.addArg(src);
ldc_exec.addFileArg(path(b, src));
}

// linker flags
Expand Down Expand Up @@ -439,13 +445,13 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
// C include path
for (lib_sokol.root_module.include_dirs.items) |include_dir| {
if (include_dir == .other_step) continue;
const path = if (include_dir == .path)
const dir = if (include_dir == .path)
include_dir.path.getPath(b)
else if (include_dir == .path_system)
include_dir.path_system.getPath(b)
else
include_dir.path_after.getPath(b);
ldc_exec.addArg(b.fmt("-P-I{s}", .{path}));
ldc_exec.addArg(b.fmt("-P-I{s}", .{dir}));
}

// library paths
Expand Down Expand Up @@ -534,8 +540,16 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
};

// output file
if (options.kind != .obj)
ldc_exec.addArg(b.fmt("-of={s}", .{b.pathJoin(&.{ b.install_prefix, outputDir, options.name })}));
const installdir = b.addInstallDirectory(.{
.install_dir = .prefix,
.source_dir = objpath.dirname(),
.install_subdir = outputDir,
.exclude_extensions = &.{
"o",
"obj",
},
});
installdir.step.dependOn(&ldc_exec.step);

if (options.zig_cc) {
const zcc = buildZigCC(b);
Expand All @@ -549,7 +563,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
}

const example_run = b.addSystemCommand(&.{b.pathJoin(&.{ b.install_path, outputDir, options.name })});
example_run.step.dependOn(&ldc_exec.step);
example_run.step.dependOn(&installdir.step);

const run = if (options.kind != .@"test")
b.step(b.fmt("run-{s}", .{options.name}), b.fmt("Run {s} example", .{options.name}))
Expand All @@ -559,12 +573,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
if (options.target.result.isWasm()) {
// get D object file and put it in the wasm artifact
const artifact = addArtifact(b, options);
artifact.addObjectFile(.{
.src_path = .{
.sub_path = b.fmt("{s}/{s}.o", .{ objpath, options.name }),
.owner = b,
},
});
artifact.addObjectFile(objpath);
artifact.linkLibrary(options.artifact.?);
artifact.step.dependOn(&ldc_exec.step);
const backend = resolveSokolBackend(options.backend, options.target.result);
Expand Down Expand Up @@ -593,7 +602,11 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
} else {
if (options.artifact) |lib_sokol| {
if (lib_sokol.rootModuleTarget().os.tag == .windows and lib_sokol.isDynamicLibrary()) {
ldc_exec.addArg(b.pathJoin(&.{ b.install_path, "lib", b.fmt("{s}.lib", .{lib_sokol.name}) }));
ldc_exec.addArg(b.pathJoin(&.{
b.install_path,
"lib",
b.fmt("{s}.lib", .{lib_sokol.name}),
}));
} else {
ldc_exec.addArtifactArg(lib_sokol);
var it = lib_sokol.root_module.iterateDependencies(lib_sokol, false);
Expand All @@ -616,7 +629,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
}
run.dependOn(&example_run.step);
}
return ldc_exec;
return installdir;
}

pub const DCompileStep = struct {
Expand Down Expand Up @@ -648,6 +661,19 @@ pub fn addArtifact(b: *Build, options: DCompileStep) *Build.Step.Compile {
});
}

pub fn path(b: *std.Build, sub_path: []const u8) std.Build.LazyPath {
if (std.fs.path.isAbsolute(sub_path)) {
return .{
.cwd_relative = sub_path,
};
} else return .{
.src_path = .{
.owner = b,
.sub_path = sub_path,
},
};
}

// -------------------------- Others Configuration --------------------------

// zig-cc wrapper for ldc2
Expand Down