Skip to content

Commit

Permalink
Move getTargetFileName to Compiler interface.
Browse files Browse the repository at this point in the history
See #618.
  • Loading branch information
s-ludwig committed Apr 1, 2016
1 parent 7b9eba4 commit fedeb66
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 66 deletions.
6 changes: 3 additions & 3 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -908,14 +908,14 @@ class DescribeCommand : PackageBuildCommand {
auto config = m_buildConfig.length ? m_buildConfig : m_defaultConfig;

if (m_importPaths) {
dub.listImportPaths(m_buildPlatform, config, m_buildType, m_dataNullDelim);
dub.listImportPaths(m_buildPlatform, m_compiler, config, m_buildType, m_dataNullDelim);
} else if (m_stringImportPaths) {
dub.listStringImportPaths(m_buildPlatform, config, m_buildType, m_dataNullDelim);
dub.listStringImportPaths(m_buildPlatform, m_compiler, config, m_buildType, m_dataNullDelim);
} else if (m_data) {
dub.listProjectData(m_buildPlatform, config, m_buildType, m_data,
m_dataList? null : m_compiler, m_dataNullDelim);
} else {
auto desc = dub.project.describe(m_buildPlatform, config, m_buildType);
auto desc = dub.project.describe(m_buildPlatform, m_compiler, config, m_buildType);
writeln(desc.serializeToPrettyJson());
}

Expand Down
32 changes: 3 additions & 29 deletions source/dub/compilers/compiler.d
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ interface Compiler {
/// Removes any dflags that match one of the BuildOptions values and populates the BuildSettings.options field.
void extractBuildOptions(ref BuildSettings settings) const;

/// Computes the full file name of the generated binary.
string getTargetFileName(in BuildSettings settings, in BuildPlatform platform) const;

/// Adds the appropriate flag to set a target path
void setTarget(ref BuildSettings settings, in BuildPlatform platform, string targetPath = null) const;

Expand Down Expand Up @@ -355,35 +358,6 @@ struct BuildPlatform {
}
}


string getTargetFileName(in BuildSettings settings, in BuildPlatform platform)
{
assert(settings.targetName.length > 0, "No target name set.");
final switch (settings.targetType) {
case TargetType.autodetect: assert(false, "Configurations must have a concrete target type.");
case TargetType.none: return null;
case TargetType.sourceLibrary: return null;
case TargetType.executable:
if( platform.platform.canFind("windows") )
return settings.targetName ~ ".exe";
else return settings.targetName;
case TargetType.library:
case TargetType.staticLibrary:
if (platform.platform.canFind("windows") && platform.compiler == "dmd")
return settings.targetName ~ ".lib";
else return "lib" ~ settings.targetName ~ ".a";
case TargetType.dynamicLibrary:
if( platform.platform.canFind("windows") )
return settings.targetName ~ ".dll";
else return "lib" ~ settings.targetName ~ ".so";
case TargetType.object:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".obj";
else return settings.targetName ~ ".o";
}
}


bool isLinkerFile(string f)
{
import std.path;
Expand Down
27 changes: 27 additions & 0 deletions source/dub/compilers/dmd.d
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,33 @@ class DmdCompiler : Compiler {
settings.dflags = newflags.data;
}

string getTargetFileName(in BuildSettings settings, in BuildPlatform platform)
const {
assert(settings.targetName.length > 0, "No target name set.");
final switch (settings.targetType) {
case TargetType.autodetect: assert(false, "Configurations must have a concrete target type.");
case TargetType.none: return null;
case TargetType.sourceLibrary: return null;
case TargetType.executable:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".exe";
else return settings.targetName;
case TargetType.library:
case TargetType.staticLibrary:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".lib";
else return "lib" ~ settings.targetName ~ ".a";
case TargetType.dynamicLibrary:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".dll";
else return "lib" ~ settings.targetName ~ ".so";
case TargetType.object:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".obj";
else return settings.targetName ~ ".o";
}
}

void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const
{
final switch (settings.targetType) {
Expand Down
25 changes: 25 additions & 0 deletions source/dub/compilers/gdc.d
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,31 @@ class GdcCompiler : Compiler {
settings.dflags = newflags.data;
}

string getTargetFileName(in BuildSettings settings, in BuildPlatform platform)
const {
assert(settings.targetName.length > 0, "No target name set.");
final switch (settings.targetType) {
case TargetType.autodetect: assert(false, "Configurations must have a concrete target type.");
case TargetType.none: return null;
case TargetType.sourceLibrary: return null;
case TargetType.executable:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".exe";
else return settings.targetName;
case TargetType.library:
case TargetType.staticLibrary:
return "lib" ~ settings.targetName ~ ".a";
case TargetType.dynamicLibrary:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".dll";
else return "lib" ~ settings.targetName ~ ".so";
case TargetType.object:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".obj";
else return settings.targetName ~ ".o";
}
}

void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const
{
final switch (settings.targetType) {
Expand Down
27 changes: 27 additions & 0 deletions source/dub/compilers/ldc.d
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,33 @@ class LdcCompiler : Compiler {
settings.dflags = newflags.data;
}

string getTargetFileName(in BuildSettings settings, in BuildPlatform platform)
const {
assert(settings.targetName.length > 0, "No target name set.");
final switch (settings.targetType) {
case TargetType.autodetect: assert(false, "Configurations must have a concrete target type.");
case TargetType.none: return null;
case TargetType.sourceLibrary: return null;
case TargetType.executable:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".exe";
else return settings.targetName;
case TargetType.library:
case TargetType.staticLibrary:
/*if (m_generatesCOFF)
return settings.targetName ~ ".lib";
else*/ return "lib" ~ settings.targetName ~ ".a";
case TargetType.dynamicLibrary:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".dll";
else return "lib" ~ settings.targetName ~ ".so";
case TargetType.object:
if (platform.platform.canFind("windows"))
return settings.targetName ~ ".obj";
else return settings.targetName ~ ".o";
}
}

void setTarget(ref BuildSettings settings, in BuildPlatform platform, string tpath = null) const
{
final switch (settings.targetType) {
Expand Down
12 changes: 6 additions & 6 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -448,27 +448,27 @@ class Dub {
}

/// Outputs a JSON description of the project, including its dependencies.
deprecated void describeProject(BuildPlatform platform, string config)
deprecated void describeProject(BuildPlatform platform, Compiler compiler, string config)
{
import std.stdio;
auto desc = m_project.describe(platform, config);
auto desc = m_project.describe(platform, compiler, config);
writeln(desc.serializeToPrettyJson());
}

void listImportPaths(BuildPlatform platform, string config, string buildType, bool nullDelim)
void listImportPaths(BuildPlatform platform, Compiler compiler, string config, string buildType, bool nullDelim)
{
import std.stdio;

foreach(path; m_project.listImportPaths(platform, config, buildType, nullDelim)) {
foreach(path; m_project.listImportPaths(platform, compiler, config, buildType, nullDelim)) {
writeln(path);
}
}

void listStringImportPaths(BuildPlatform platform, string config, string buildType, bool nullDelim)
void listStringImportPaths(BuildPlatform platform, Compiler compiler, string config, string buildType, bool nullDelim)
{
import std.stdio;

foreach(path; m_project.listStringImportPaths(platform, config, buildType, nullDelim)) {
foreach(path; m_project.listStringImportPaths(platform, compiler, config, buildType, nullDelim)) {
writeln(path);
}
}
Expand Down
33 changes: 19 additions & 14 deletions source/dub/generators/build.d
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class BuildGenerator : ProjectGenerator {
foreach (ldep; ti.linkDependencies) {
auto dbs = targets[ldep].buildSettings;
if (bs.targetType != TargetType.staticLibrary) {
bs.addSourceFiles((Path(dbs.targetPath) ~ getTargetFileName(dbs, settings.platform)).toNativeString());
bs.addSourceFiles(getTargetPath(dbs, settings).toNativeString());
} else {
additional_dep_files ~= Path(dbs.targetPath) ~ getTargetFileName(dbs, settings.platform);
additional_dep_files ~= getTargetPath(dbs, settings);
}
}
if (buildTarget(settings, bs, ti.pack, ti.config, ti.packages, additional_dep_files))
Expand All @@ -95,7 +95,7 @@ class BuildGenerator : ProjectGenerator {
// run the generated executable
auto buildsettings = targets[m_project.rootPackage.name].buildSettings;
if (settings.run && !(buildsettings.options & BuildOption.syntaxOnly)) {
auto exe_file_path = Path(buildsettings.targetPath) ~ getTargetFileName(buildsettings, settings.platform);
auto exe_file_path = getTargetPath(buildsettings, settings);
runTarget(exe_file_path, buildsettings, settings.runArgs, settings);
}
}
Expand Down Expand Up @@ -147,10 +147,10 @@ class BuildGenerator : ProjectGenerator {
auto cwd = Path(getcwd());
auto target_path = pack.path ~ format(".dub/build/%s/", build_id);

if (!settings.force && isUpToDate(target_path, buildsettings, settings.platform, pack, packages, additional_dep_files)) {
if (!settings.force && isUpToDate(target_path, buildsettings, settings, pack, packages, additional_dep_files)) {
logInfo("%s %s: target for configuration \"%s\" is up to date.", pack.name, pack.vers, config);
logDiagnostic("Using existing build in %s.", target_path.toNativeString());
copyTargetFile(target_path, buildsettings, settings.platform);
copyTargetFile(target_path, buildsettings, settings);
return true;
}

Expand All @@ -176,7 +176,7 @@ class BuildGenerator : ProjectGenerator {
cbuildsettings.targetPath = target_path.relativeTo(cwd).toNativeString();
buildWithCompiler(settings, cbuildsettings);

copyTargetFile(target_path, buildsettings, settings.platform);
copyTargetFile(target_path, buildsettings, settings);

return false;
}
Expand Down Expand Up @@ -216,11 +216,11 @@ class BuildGenerator : ProjectGenerator {
m_temporaryFiles ~= tmpdir;
tmp_target = true;
}
exe_file_path = Path(buildsettings.targetPath) ~ getTargetFileName(buildsettings, settings.platform);
exe_file_path = getTargetPath(buildsettings, settings);
settings.compiler.setTarget(buildsettings, settings.platform);
}

logDiagnostic("Application output name is '%s'", getTargetFileName(buildsettings, settings.platform));
logDiagnostic("Application output name is '%s'", settings.compiler.getTargetFileName(buildsettings, settings.platform));

string[] flags = ["--build-only", "--compiler="~settings.platform.compilerBinary];
if (settings.force) flags ~= "--force";
Expand Down Expand Up @@ -285,7 +285,7 @@ class BuildGenerator : ProjectGenerator {
m_temporaryFiles ~= tmppath;
is_temp_target = true;
}
exe_file_path = Path(buildsettings.targetPath) ~ getTargetFileName(buildsettings, settings.platform);
exe_file_path = getTargetPath(buildsettings, settings);
}

if( buildsettings.preBuildCommands.length ){
Expand Down Expand Up @@ -332,21 +332,21 @@ class BuildGenerator : ProjectGenerator {
settings.platform.compiler, settings.platform.frontendVersion, hashstr);
}

private void copyTargetFile(Path build_path, BuildSettings buildsettings, BuildPlatform platform)
private void copyTargetFile(Path build_path, BuildSettings buildsettings, GeneratorSettings settings)
{
auto filename = getTargetFileName(buildsettings, platform);
auto filename = settings.compiler.getTargetFileName(buildsettings, settings.platform);
auto src = build_path ~ filename;
logDiagnostic("Copying target from %s to %s", src.toNativeString(), buildsettings.targetPath);
if (!existsFile(Path(buildsettings.targetPath)))
mkdirRecurse(buildsettings.targetPath);
hardLinkFile(src, Path(buildsettings.targetPath) ~ filename, true);
}

private bool isUpToDate(Path target_path, BuildSettings buildsettings, BuildPlatform platform, in Package main_pack, in Package[] packages, in Path[] additional_dep_files)
private bool isUpToDate(Path target_path, BuildSettings buildsettings, GeneratorSettings settings, in Package main_pack, in Package[] packages, in Path[] additional_dep_files)
{
import std.datetime;

auto targetfile = target_path ~ getTargetFileName(buildsettings, platform);
auto targetfile = target_path ~ settings.compiler.getTargetFileName(buildsettings, settings.platform);
if (!existsFile(targetfile)) {
logDiagnostic("Target '%s' doesn't exist, need rebuild.", targetfile.toNativeString());
return false;
Expand Down Expand Up @@ -412,7 +412,7 @@ class BuildGenerator : ProjectGenerator {
Path target_file;
scope (failure) {
logDiagnostic("FAIL %s %s %s" , buildsettings.targetPath, buildsettings.targetName, buildsettings.targetType);
auto tpath = Path(buildsettings.targetPath) ~ getTargetFileName(buildsettings, settings.platform);
auto tpath = getTargetPath(buildsettings, settings);
if (generate_binary && existsFile(tpath))
removeFile(tpath);
}
Expand Down Expand Up @@ -538,6 +538,11 @@ private Path getMainSourceFile(in Package prj)
return prj.path ~ "source/app.d";
}

private Path getTargetPath(in ref BuildSettings bs, in ref GeneratorSettings settings)
{
return Path(bs.targetPath) ~ settings.compiler.getTargetFileName(bs, settings.platform);
}

unittest {
version (Windows) {
assert(isLinkerFile("test.obj"));
Expand Down
2 changes: 1 addition & 1 deletion source/dub/generators/targetdescription.d
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TargetDescriptionGenerator : ProjectGenerator {
foreach (ld; ti.linkDependencies) {
auto ltarget = targets[ld];
auto ltbs = ltarget.buildSettings;
auto targetfil = (Path(ltbs.targetPath) ~ getTargetFileName(ltbs, settings.platform)).toNativeString();
auto targetfil = (Path(ltbs.targetPath) ~ settings.compiler.getTargetFileName(ltbs, settings.platform)).toNativeString();
d.buildSettings.addLinkerFiles(targetfil);
}

Expand Down
8 changes: 4 additions & 4 deletions source/dub/package_.d
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class Package {

/** Returns a description of the package for use in IDEs or build tools.
*/
PackageDescription describe(BuildPlatform platform, string config)
PackageDescription describe(BuildPlatform platform, Compiler compiler, string config)
const {
PackageDescription ret;
ret.configuration = config;
Expand All @@ -391,8 +391,8 @@ class Package {
ret.targetType = bs.targetType;
ret.targetPath = bs.targetPath;
ret.targetName = bs.targetName;
if (ret.targetType != TargetType.none)
ret.targetFileName = getTargetFileName(bs, platform);
if (ret.targetType != TargetType.none && compiler)
ret.targetFileName = compiler.getTargetFileName(bs, platform);
ret.workingDirectory = bs.workingDirectory;
ret.mainSourceFile = bs.mainSourceFile;
ret.dflags = bs.dflags;
Expand Down Expand Up @@ -438,7 +438,7 @@ class Package {
// ditto
deprecated void describe(ref Json dst, BuildPlatform platform, string config)
{
auto res = describe(platform, config);
auto res = describe(platform, null, config);
foreach (string key, value; res.serializeToJson())
dst[key] = value;
}
Expand Down
Loading

0 comments on commit fedeb66

Please sign in to comment.