Skip to content

Commit

Permalink
Merge pull request #299 from kinke/win_lib.exe
Browse files Browse the repository at this point in the history
Windows: Resolve path to lib.exe (for static libs) at reggae-time
  • Loading branch information
atilaneves authored May 10, 2024
2 parents 23f1f71 + 11121d9 commit d720c75
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
27 changes: 19 additions & 8 deletions payload/reggae/options.d
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,21 @@ string banner() @safe pure nothrow {
}

private void setExePath(ref string executable, in string envVar, const string default_) @safe {
import std.process : environment, executeShell;
import std.string : splitLines;
import std.process : environment;

if (executable == "")
executable = environment.get(envVar, default_);

executable = resolveExecutable(executable);
}

string resolveExecutable(const string executable) @safe {
import std.process : executeShell;
import std.string : splitLines;

if (executable == "")
return executable;

static finder(in string exe) {
version(Windows)
return executeShell("where " ~ exe);
Expand All @@ -395,11 +404,13 @@ private void setExePath(ref string executable, in string envVar, const string de
}

const finderResult = finder(executable);
if (finderResult.status != 0) return;
if (finderResult.status == 0) {
// splitting lines here instead of chomp because `where` can (and
// does for dmd on Windows) return more than one line
const lines = finderResult.output.splitLines;
if (lines.length)
return lines[0];
}

// splitting lines here instead of chomp because `where` can (and
// does for dmd on Windows) return more than one line
const lines = finderResult.output.splitLines;
if (lines.length)
executable = lines[0];
return executable;
}
21 changes: 15 additions & 6 deletions payload/reggae/rules/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ Target staticLibrary(
);
}

Target staticLibraryTarget(in string name, Target[] objects) @safe pure {
Target staticLibraryTarget(in string name, Target[] objects) @safe {
import std.path: defaultExtension;
return Target(
[buildPath("$builddir", defaultExtension(name, libExt))],
Expand All @@ -496,7 +496,7 @@ Target staticLibraryTarget(in string name, Target[] objects) @safe pure {
);
}

Target staticLibraryTarget(in string name, Target[] objects, Target[] implicits) @safe pure {
Target staticLibraryTarget(in string name, Target[] objects, Target[] implicits) @safe {
import std.path: defaultExtension;
return Target(
[buildPath("$builddir", defaultExtension(name, libExt))],
Expand All @@ -506,10 +506,19 @@ Target staticLibraryTarget(in string name, Target[] objects, Target[] implicits)
);
}

version(Windows)
private enum staticLibraryShellCommand = "lib.exe /OUT:$out $in";
else
private enum staticLibraryShellCommand = "ar rcs $out $in";
private string staticLibraryShellCommand() @safe {
version(Windows) {
import reggae.options: resolveExecutable;

static string libExe;
if (!libExe.length)
libExe = resolveExecutable("lib.exe");

return `"` ~ libExe ~ `" /OUT:$out $in`;
} else {
return "ar rcs $out $in";
}
}

private Target[] srcFilesToObjectTargets(
in imported!"reggae.options".Options options,
Expand Down
26 changes: 26 additions & 0 deletions tests/it/rules/static_lib.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module tests.it.rules.static_lib;

import tests.it.runtime;

@("C archiver command in ninja rule")
@Tags("ninja")
unittest {
with(immutable ReggaeSandbox()) {
writeFile(
"reggaefile.d",
q{
import reggae;
alias lib = staticLibrary!("mylib", Sources!("src"));
mixin build!lib;
}
);

writeFile("src/foo.c", "void foo() {}");
runReggae("-b", "ninja");

version(Windows) // should have resolved path to lib.exe
fileShouldContain("build.ninja", `\lib.exe" /OUT:`);
else
fileShouldContain("build.ninja", "before = ar rcs ");
}
}
1 change: 1 addition & 0 deletions tests/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int main(string[] args) {
"tests.it.rules.json_build",
"tests.it.rules.cmake",
"tests.it.rules.d_cmake_interop",
"tests.it.rules.static_lib",
"tests.it.runtime.lua",
"tests.it.runtime.javascript",
"tests.it.runtime.user_vars",
Expand Down

0 comments on commit d720c75

Please sign in to comment.