Skip to content

Commit

Permalink
Merge pull request atilaneves#237 from atilaneves/fix-phony-exe
Browse files Browse the repository at this point in the history
Compile-time phony rule to run created binary
  • Loading branch information
atilaneves authored Nov 10, 2023
2 parents 7b0c170 + cddfd03 commit d56272d
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"fileVersion": 1,
"versions": {
"dub": "1.34.0",
"unit-threaded": "2.1.2"
"unit-threaded": "2.1.7"
}
}
4 changes: 1 addition & 3 deletions payload/reggae/dub/interop/dublib.d
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,10 @@ struct Dub {
import dub.internal.vibecompat.inet.path: NativePath;
import std.path: baseName, stripExtension;

const compilerBinName = options.dCompiler.baseName.stripExtension;

GeneratorSettings ret;

ret.cache = NativePath(options.workingDir) ~ "__dub_cache__";
ret.compiler = () @trusted { return getCompiler(compilerBinName); }();
ret.compiler = () @trusted { return getCompiler(options.compilerBinName); }();
ret.platform = () @trusted {
return ret.compiler.determinePlatform(ret.buildSettings,
options.dCompiler, options.dubArchOverride);
Expand Down
7 changes: 3 additions & 4 deletions payload/reggae/dub/interop/fetch.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module reggae.dub.interop.fetch;


package void dubFetch(O)(
void dubFetch(O)(
auto ref O output,
in imported!"reggae.options".Options options,
in string dubSelectionsJson)
Expand All @@ -14,12 +14,11 @@ package void dubFetch(O)(
import dub.packagemanager: PlacementLocation;
import std.array: replace;
import std.json: parseJSON, JSONType;
import std.file: readText;
import std.parallelism: parallel;

const(VersionedPackage)[] pkgsToFetch;

const json = parseJSON(readText(dubSelectionsJson));
const json = parseJSON(dubSelectionsJson);

auto dublib = reggae.dub.interop.dublib.Dub(options);
foreach(dubPackageName, versionJson; json["versions"].object) {
Expand All @@ -42,7 +41,7 @@ package void dubFetch(O)(
dubObj.fetch(
pkg.name,
Version(pkg.version_),
PlacementLocation.user,
dubObj.defaultPlacementLocation,
FetchOptions.none,
);
catch (Exception exc)
Expand Down
4 changes: 3 additions & 1 deletion payload/reggae/dub/interop/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ auto dubInfos(O)(ref O output,
import reggae.io: log;
import reggae.dub.interop.fetch: dubFetch;
import reggae.dub.interop.dublib: Dub;
import std.file: readText;

// must check for dub.selections.json before creating dub instance
const dubSelectionsJson = ensureDubSelectionsJson(output, options);
const dubSelectionsJsonPath = ensureDubSelectionsJson(output, options);
const dubSelectionsJson = readText(dubSelectionsJsonPath);

dubFetch(output, options, dubSelectionsJson);

Expand Down
8 changes: 7 additions & 1 deletion payload/reggae/options.d
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ struct Options {
//this will usually be empty, but won't be if the reggaefile imports other D files
string[] getReggaeFileDependenciesDlang() @safe const {
import reggae.dependencies: parseDepFile;
return parseDepFile(reggaeFileDepFile);
import std.algorithm: filter, canFind;
import std.array: array;
import std.path: buildPath, dirSeparator;

return parseDepFile(reggaeFileDepFile)
.filter!(a => !a.canFind(dirSeparator ~ buildPath("dub", "source")))
.array;
}

string reggaeFileDepFile() @safe pure const {
Expand Down
32 changes: 32 additions & 0 deletions payload/reggae/rules/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,38 @@ Target phony(string name,
return Target.phony(name, shellCommand, arrayify!dependenciesFunc, arrayify!implicitsFunc);
}

/**
Compile-time version of phony targeted towards running binaries
that are dependencies of the phony rule.
*/
Target phony(
string name,
alias dependenciesFunc,
string[] args = [],
alias implicitsFunc = () { Target[] ts; return ts; },
)
()
if(isTargets!dependenciesFunc && isTargets!implicitsFunc)
{
import reggae.build: objDirOf;
import std.array: join;
import std.path: buildPath;

auto deps = arrayify!dependenciesFunc;
if(deps.length > 1)
throw new Exception("Only one output allowed for this phony rule");
if(deps[0].rawOutputs.length > 1)
throw new Exception("Only one output allowed for this phony rule");
auto target = deps[0];

return Target.phony(
name,
(buildPath(objDirOf(Target(name)), target.rawOutputs[0]) ~ args).join(" "),
deps,
arrayify!implicitsFunc
);
}

private template isTargets(alias T) {
import std.traits: Unqual, ReturnType, isCallable;
import std.range: isInputRange, ElementType;
Expand Down
41 changes: 37 additions & 4 deletions src/reggae/reggae.d
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ struct Binary {
}


// FIXME: use the same dub version as in dub.selections.json for reggae itself
private enum dubSdl =
`
name "buildgen"
Expand All @@ -288,6 +287,7 @@ private string compileBuildGenerator(T)(auto ref T output, in Options options) {
import std.algorithm: map, joiner;
import std.range: chain, only;
import std.string: replace;
import std.array: array;

immutable buildGenName = getBuildGenName(options) ~ exeExt;
if(options.isScriptBuild) return buildGenName;
Expand All @@ -296,9 +296,16 @@ private string compileBuildGenerator(T)(auto ref T output, in Options options) {
// `options.reggaeFileDepFile` existing, which means we need to
// compile the reggaefile separately to get those dependencies
// *then* add any extra files to the dummy dub.sdl.
const dubVersions = ["Have_dub", "DubUseCurl"];
const versionFlag = options.isLdc ? "-d-version" : "-version";
const dubVersionFlags = dubVersions.map!(a => versionFlag ~ "=" ~ a).array;
auto reggaefileObj = Binary(
"reggaefile" ~ objExt,
[options.dCompiler, options.reggaeFilePath, "-o-", "-makedeps=" ~ options.reggaeFileDepFile] ~ importPaths(options),
[
options.dCompiler,
options.reggaeFilePath, "-o-", "-makedeps=" ~ options.reggaeFileDepFile,
]
~ dubVersionFlags ~ importPaths(options) ~ dubImportFlags(options),
);
buildBinary(output, options, reggaefileObj);

Expand All @@ -310,7 +317,10 @@ private string compileBuildGenerator(T)(auto ref T output, in Options options) {
.joiner(" ");
}

auto userFiles = chain(only(options.reggaeFilePath), options.getReggaeFileDependenciesDlang);
auto userFiles = chain(
options.reggaeFilePath.only,
options.getReggaeFileDependenciesDlang
);
auto userSourceFilesForDubSdl = stringsToSdlList(userFiles);
// [2..$] gets rid of `-I`
auto importPathsForDubSdl = stringsToSdlList(importPaths(options).map!(i => i[2..$]));
Expand All @@ -329,14 +339,37 @@ private string compileBuildGenerator(T)(auto ref T output, in Options options) {

auto binary = Binary(
buildGenName,
// FIXME use dcompiler
// FIXME - use --compiler
// The reason it doesn't work now is due to a test using
// a custom compiler
["dub", "build"], // since we now depend on dub at buildgen runtime
);
buildBinary(output, options, binary);

return buildGenName;
}

private string[] dubImportFlags(in imported!"reggae.options".Options options) {
import std.json: parseJSON;
import dub.dub: Dub, FetchOptions;
import dub.dependency: Version;
import std.file: exists;
import std.path: buildPath;
import reggae.path: dubPackagesDir;

const dubSelectionsJson = import("dub.selections.json");
const dubVersion = dubSelectionsJson
.parseJSON
["versions"]
["dub"]
.str;
auto dubObj = new Dub(options.projectPath);
dubObj.fetch("dub", Version(dubVersion), dubObj.defaultPlacementLocation, FetchOptions.none);
const dubSourcePath = buildPath(dubPackagesDir, "dub", dubVersion, "dub", "source");
assert(dubSourcePath.exists, "dub fetch failed: no path '" ~ dubSourcePath ~ "'");
return ["-I" ~ dubSourcePath];
}

private void buildBinary(T)(auto ref T output, in Options options, in Binary bin) {
import reggae.io: log;
import std.process;
Expand Down
9 changes: 3 additions & 6 deletions tests/it/runtime/dub/dependencies.d
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,9 @@ unittest {
}
}

@ShouldFail
@("dubDependency.exe.phony")
@Tags("dub", "ninja")
unittest {
import std.format;
with(immutable ReggaeSandbox()) {
writeFile(
"over/there/dub.sdl",
Expand All @@ -471,17 +469,16 @@ unittest {
}
}
);
const foo = inSandboxPath("over/there/foo");
writeFile(
"reggaefile.d",
q{
import reggae;

alias dubDep = dubDependency!(DubPath("over/there"));
alias yay = phony!("yay", "%s 0", dubDep);
alias nay = phony!("nay", "%s 1", dubDep);
alias yay = phony!("yay", dubDep, ["0"]);
alias nay = phony!("nay", dubDep, ["1"]);
mixin build!(yay, nay);
}.format(foo, foo)
}
);

runReggae("-b", "ninja");
Expand Down

0 comments on commit d56272d

Please sign in to comment.