Skip to content

Commit

Permalink
Merge pull request #312 from atilaneves/dubDependant-runtime
Browse files Browse the repository at this point in the history
Runtime version of dubDependant
  • Loading branch information
atilaneves authored Jun 20, 2024
2 parents 9c1695b + a46c6f6 commit 6ba93e8
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 27 deletions.
64 changes: 49 additions & 15 deletions payload/reggae/rules/dub/external.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,60 @@ imported!"reggae.build".Target dubDependant(
)
()
{
import reggae.rules.dub: oneOptionalOf, isOfType;
import reggae.rules.d: dlink;
import reggae.config: reggaeOptions = options; // the ones used to run reggae
return dubDependant!sourcesFunc(reggaeOptions, targetName, targetType, A);
}

// mostly runtime version
imported!"reggae.build".Target dubDependant
(alias sourcesFunc, A...)
(
in imported!"reggae.options".Options options,
in imported!"reggae.types".TargetName targetName,
DubPackageTargetType targetType,
// the other arguments can be:
// * DubPath
// * CompilerFlags
// * LinkerFlags
// * ImportPaths
// * StringImportPaths
auto ref A args,
)
{
import reggae.rules.common: objectFiles;
import reggae.rules.d: dlink;
import reggae.types: TargetName, CompilerFlags, LinkerFlags, ImportPaths, StringImportPaths;
import reggae.config: reggaeOptions = options; // the ones used to run reggae
import std.meta: Filter;
import std.algorithm: map, joiner;
import std.array: array;
import std.range: chain;
import std.traits: Unqual;

alias DubPaths = Filter!(isOfType!DubPath, A);
static assert(DubPaths.length > 0, "At least one `DubPath` needed");
DubPath[] dubPaths;
static foreach(arg; args) {
static if(is(Unqual!(typeof(arg)) == DubPath))
dubPaths ~= arg;
}

enum compilerFlags = oneOptionalOf!(CompilerFlags, A);
enum linkerFlags = oneOptionalOf!(LinkerFlags, A);
enum importPaths = oneOptionalOf!(ImportPaths, A);
enum stringImportPaths = oneOptionalOf!(StringImportPaths, A);
template oneOptionalOf(T) {
import std.meta: staticIndexOf;
enum index = staticIndexOf!(T, A);
static if(index == -1) {
T oneOptionalOf() {
return T();
}
} else {
T oneOptionalOf() {
return args[index];
}
}
}
const compilerFlags = oneOptionalOf!CompilerFlags;
const linkerFlags = oneOptionalOf!LinkerFlags;
const importPaths = oneOptionalOf!ImportPaths;
const stringImportPaths = oneOptionalOf!StringImportPaths;

auto dubPathDependencies = [DubPaths]
.map!(p => DubPathDependency(reggaeOptions, p))
auto dubPathDependencies = dubPaths
.map!(p => DubPathDependency(options, p))
.array
;

Expand All @@ -87,8 +121,8 @@ imported!"reggae.build".Target dubDependant(

auto objs = objectFiles!sourcesFunc(
compilerFlags,
ImportPaths(allImportPaths),
StringImportPaths(allStringImportPaths),
const ImportPaths(allImportPaths),
const StringImportPaths(allStringImportPaths),
);

auto dubDepsObjs = dubPathDependencies
Expand All @@ -97,14 +131,14 @@ imported!"reggae.build".Target dubDependant(
;

const targetNameWithExt = withExtension(targetName, targetType);

return dlink(
TargetName(targetNameWithExt),
objs ~ dubDepsObjs,
linkerFlags,
);
}


private struct DubPathDependency {
import reggae.options: Options;
import reggae.build: Target;
Expand Down
8 changes: 4 additions & 4 deletions payload/reggae/types.d
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ struct ImportPaths {
this.value = value;
}

this(R)(R range) @trusted /*array*/ if(isInputRange!R) {
this(R)(R range) inout @trusted /*array*/ if(isInputRange!R) {
import std.array: array;
this.value = range.array;
this.value = cast(typeof(this.value)) range.array;
}

this(inout(string) value) inout pure {
Expand All @@ -99,9 +99,9 @@ struct StringImportPaths {
this.value = value;
}

this(R)(R range) @trusted /*array*/ if(isInputRange!R) {
this(R)(R range) inout @trusted /*array*/ if(isInputRange!R) {
import std.array: array;
this.value = range.array;
this.value = cast(typeof(this.value)) range.array;
}

this(inout(string) value) inout pure {
Expand Down
72 changes: 64 additions & 8 deletions tests/it/runtime/dub/dependencies.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ else


// A dub package that isn't at the root of the project directory
@("dubDependant.path.exe.default")
@("dubDependant.ct.path.exe.default")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
Expand Down Expand Up @@ -66,8 +66,64 @@ unittest {
}
}

@("dubDependant.rt.path.exe.default")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
import reggae.rules.common: exeExt;
with(immutable ReggaeSandbox()) {
// a dub package we're going to depend on by path
writeFile(
"over/there/dub.sdl",
[
`name "foo"`,
`targetType "library"`
]
);
// src code for the dub dependency
writeFile(
"over/there/source/foo.d",
q{int twice(int i) { return i * 2; }}
);
// our main program, which will depend on a dub package by path
writeFile(
"src/app.d",
q{
import foo;
void main() {
assert(5.twice == 10);
}
}
);
writeFile(
"reggaefile.d",
q{
import reggae;
import reggae.config: options;
auto app() {
return dubDependant!(
Sources!(Files("src/app.d")),
)(
options,
TargetName("myapp"),
DubPackageTargetType.executable,
DubPath("over/there"),
);
}
mixin build!app;
}
);

runReggae("-b", "ninja");
ninja.shouldExecuteOk;
shouldExist("myapp" ~ exeExt);
shouldSucceed("myapp");
}
}


// A dub package that isn't at the root of the project directory
@("dubDependant.path.exe.config")
@("dubDependant.ct.path.exe.config")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
Expand Down Expand Up @@ -132,7 +188,7 @@ unittest {


// A dub package that isn't at the root of the project directory
@("dubDependant.path.lib")
@("dubDependant.ct.path.lib")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
Expand Down Expand Up @@ -186,7 +242,7 @@ unittest {
}

// A dub package that isn't at the root of the project directory
@("dubDependant.path.dll")
@("dubDependant.ct.path.dll")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
Expand Down Expand Up @@ -239,7 +295,7 @@ unittest {
}
}

@("dubDependant.flags.compiler")
@("dubDependant.ct.flags.compiler")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
Expand Down Expand Up @@ -283,7 +339,7 @@ unittest {
}


@("dubDependant.flags.linker")
@("dubDependant.ct.flags.linker")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
Expand Down Expand Up @@ -328,7 +384,7 @@ unittest {
}


@("dubDependant.flags.imports")
@("dubDependant.ct.flags.imports")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
Expand Down Expand Up @@ -372,7 +428,7 @@ unittest {
}


@("dubDependant.flags.stringImports")
@("dubDependant.ct.flags.stringImports")
@MaybeFlaky
@Tags("dub", "ninja")
unittest {
Expand Down

0 comments on commit 6ba93e8

Please sign in to comment.