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

add --deep flag to dub build #2647

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ class GenerateCommand : PackageBuildCommand {
protected {
string m_generator;
bool m_printPlatform, m_printBuilds, m_printConfigs;
bool m_deep; // only set in BuildCommand
}

this() @safe pure nothrow
Expand Down Expand Up @@ -1274,6 +1275,7 @@ class GenerateCommand : PackageBuildCommand {
gensettings.runArgs = app_args;
// legacy compatibility, default working directory is always CWD
gensettings.overrideToolWorkingDirectory = getWorkingDirectory();
gensettings.buildDeep = m_deep;

logDiagnostic("Generating using %s", m_generator);
dub.generateProject(m_generator, gensettings);
Expand Down Expand Up @@ -1316,6 +1318,9 @@ class BuildCommand : GenerateCommand {
args.getopt("n|non-interactive", &m_nonInteractive, [
"Don't enter interactive mode."
]);
args.getopt("d|deep", &m_deep, [
"Build all dependencies, even when main target is a static library."
]);
super.prepare(args);
m_generator = "build";
}
Expand Down
20 changes: 11 additions & 9 deletions source/dub/generators/build.d
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ class BuildGenerator : ProjectGenerator {
settings.buildType.color(Color.magenta), settings.platform.compilerBinary,
settings.platform.architecture);

if (settings.rdmd || (rootTT == TargetType.staticLibrary && !settings.buildDeep)) {
// Only build the main target.
// RDMD always builds everything at once and static libraries don't need their
// dependencies to be built, unless --deep flag is specified
NativePath tpath;
buildTarget(settings, root_ti.buildSettings.dup, m_project.rootPackage, root_ti.config, root_ti.packages, null, tpath);
return;
}

// Recursive build starts here

bool any_cached = false;

NativePath[string] target_paths;
Expand Down Expand Up @@ -158,15 +169,6 @@ class BuildGenerator : ProjectGenerator {
target_paths[target] = tpath;
}

// build all targets
if (settings.rdmd || rootTT == TargetType.staticLibrary) {
// RDMD always builds everything at once and static libraries don't need their
// dependencies to be built
NativePath tpath;
buildTarget(settings, root_ti.buildSettings.dup, m_project.rootPackage, root_ti.config, root_ti.packages, null, tpath);
return;
}

buildTargetRec(m_project.rootPackage.name);

if (dynamicLibDepsFilesToCopy.length) {
Expand Down
3 changes: 3 additions & 0 deletions source/dub/generators/generator.d
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,9 @@ struct GeneratorSettings {
/// single file dub package
bool single;

/// build all dependencies for static libraries
bool buildDeep;

string[] runArgs;
void delegate(int status, string output) compileCallback;
void delegate(int status, string output) linkCallback;
Expand Down
2 changes: 2 additions & 0 deletions test/pr2647-build-deep/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dubhome/
pr2647-build-deep
Empty file added test/pr2647-build-deep/.no_test
Empty file.
2 changes: 2 additions & 0 deletions test/pr2647-build-deep/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name "pr2647-build-deep";
targetType "executable";
3 changes: 3 additions & 0 deletions test/pr2647-build-deep/pack/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name "pack"
targetType "staticLibrary"
dependency "urld" version="==2.1.1"
14 changes: 14 additions & 0 deletions test/pr2647-build-deep/pack/source/lib.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module lib;

import url;

string getDlangUrl()
{
URL url;
with(url)
{
scheme = "https";
host = "dlang.org";
}
return url.toString();
}
53 changes: 53 additions & 0 deletions test/pr2647-build-deep/source/test_build_deep.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module test_build_deep;

import std.array;
import std.file;
import std.path;
import std.process;
import std.stdio;

void main()
{
const dubhome = __FILE_FULL_PATH__.dirName().dirName().buildNormalizedPath("dubhome");
const packdir = __FILE_FULL_PATH__.dirName().dirName().buildNormalizedPath("pack");
const dub = absolutePath(environment["DUB"]);

if (exists(dubhome))
{
rmdirRecurse(dubhome);
}

scope (success)
{
// leave dubhome in the tree for analysis in case of failure
rmdirRecurse(dubhome);
}

const string[string] env = [
"DUB_HOME": dubhome,
];

// testing the regular way first: `dub build` only builds what is needed
// (urld is downloaded but not built)
const dubBuildProg = [dub, "build"];
writefln("running %s ...", dubBuildProg.join(" "));
auto dubBuild = spawnProcess(dubBuildProg, stdin, stdout, stderr, env, Config.none, packdir);
wait(dubBuild);
assert(exists(buildPath(dubhome, "cache", "pack")));
assert(isDir(buildPath(dubhome, "cache", "pack")));
assert(exists(buildPath(dubhome, "packages", "urld")));
assert(isDir(buildPath(dubhome, "packages", "urld")));
assert(!exists(buildPath(dubhome, "cache", "urld")));

// now testing the --deep switch: `dub build --deep` will build urld
const dubBuildDeepProg = [dub, "build", "--deep"];
writefln("running %s ...", dubBuildDeepProg.join(" "));
auto dubBuildDeep = spawnProcess(dubBuildDeepProg, stdin, stdout, stderr, env, Config.none, packdir);
wait(dubBuildDeep);
assert(exists(buildPath(dubhome, "cache", "pack")));
assert(isDir(buildPath(dubhome, "cache", "pack")));
assert(exists(buildPath(dubhome, "packages", "urld")));
assert(isDir(buildPath(dubhome, "packages", "urld")));
assert(exists(buildPath(dubhome, "cache", "urld")));
assert(isDir(buildPath(dubhome, "cache", "urld")));
}