From fedeb66295bf5fd869048dabaa8e353d543d4078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 4 Jul 2015 19:00:01 +0200 Subject: [PATCH] Move getTargetFileName to Compiler interface. See #618. --- source/dub/commandline.d | 6 ++--- source/dub/compilers/compiler.d | 32 +++------------------- source/dub/compilers/dmd.d | 27 +++++++++++++++++++ source/dub/compilers/gdc.d | 25 +++++++++++++++++ source/dub/compilers/ldc.d | 27 +++++++++++++++++++ source/dub/dub.d | 12 ++++----- source/dub/generators/build.d | 33 +++++++++++++---------- source/dub/generators/targetdescription.d | 2 +- source/dub/package_.d | 8 +++--- source/dub/project.d | 18 ++++++------- 10 files changed, 124 insertions(+), 66 deletions(-) diff --git a/source/dub/commandline.d b/source/dub/commandline.d index d933c865c..6de49e672 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -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()); } diff --git a/source/dub/compilers/compiler.d b/source/dub/compilers/compiler.d index bb34cde73..859b9e596 100644 --- a/source/dub/compilers/compiler.d +++ b/source/dub/compilers/compiler.d @@ -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; @@ -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; diff --git a/source/dub/compilers/dmd.d b/source/dub/compilers/dmd.d index 73d3994a9..d314534c3 100644 --- a/source/dub/compilers/dmd.d +++ b/source/dub/compilers/dmd.d @@ -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) { diff --git a/source/dub/compilers/gdc.d b/source/dub/compilers/gdc.d index 3c58cf193..5c5c4a683 100644 --- a/source/dub/compilers/gdc.d +++ b/source/dub/compilers/gdc.d @@ -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) { diff --git a/source/dub/compilers/ldc.d b/source/dub/compilers/ldc.d index 7cd75d8df..37a5fc619 100644 --- a/source/dub/compilers/ldc.d +++ b/source/dub/compilers/ldc.d @@ -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) { diff --git a/source/dub/dub.d b/source/dub/dub.d index 9e022dc1d..2499d5c0f 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -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); } } diff --git a/source/dub/generators/build.d b/source/dub/generators/build.d index 8db2c327c..caf4f82c9 100644 --- a/source/dub/generators/build.d +++ b/source/dub/generators/build.d @@ -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)) @@ -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); } } @@ -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; } @@ -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; } @@ -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"; @@ -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 ){ @@ -332,9 +332,9 @@ 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))) @@ -342,11 +342,11 @@ class BuildGenerator : ProjectGenerator { 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; @@ -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); } @@ -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")); diff --git a/source/dub/generators/targetdescription.d b/source/dub/generators/targetdescription.d index e5b0e02bb..028f7262d 100644 --- a/source/dub/generators/targetdescription.d +++ b/source/dub/generators/targetdescription.d @@ -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); } diff --git a/source/dub/package_.d b/source/dub/package_.d index 90b19bd10..9cac12341 100644 --- a/source/dub/package_.d +++ b/source/dub/package_.d @@ -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; @@ -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; @@ -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; } diff --git a/source/dub/project.d b/source/dub/project.d index fb25c0797..2ae1fb002 100644 --- a/source/dub/project.d +++ b/source/dub/project.d @@ -597,7 +597,7 @@ class Project { }*/ /// Outputs a build description of the project, including its dependencies. - ProjectDescription describe(BuildPlatform platform, string config, string build_type = null) + ProjectDescription describe(BuildPlatform platform, Compiler compiler, string config, string build_type = null) { import dub.generators.targetdescription; @@ -612,9 +612,9 @@ class Project { // collect high level information about projects (useful for IDE display) auto configs = getPackageConfigs(platform, config); - ret.packages ~= m_rootPackage.describe(platform, config); + ret.packages ~= m_rootPackage.describe(platform, compiler, config); foreach (dep; m_dependencies) - ret.packages ~= dep.describe(platform, configs[dep.name]); + ret.packages ~= dep.describe(platform, compiler, configs[dep.name]); foreach (p; getTopologicalPackageList(false, null, configs)) ret.packages[ret.packages.countUntil!(pp => pp.name == p.name)].active = true; @@ -642,7 +642,7 @@ class Project { /// ditto deprecated void describe(ref Json dst, BuildPlatform platform, string config) { - auto desc = describe(platform, config); + auto desc = describe(platform, null, config); foreach (string key, value; desc.serializeToJson()) dst[key] = value; } @@ -915,7 +915,7 @@ class Project { string[] listBuildSettings(BuildPlatform platform, string config, string buildType, string[] requestedData, Compiler formattingCompiler, bool nullDelim) { - auto projectDescription = describe(platform, config, buildType); + auto projectDescription = describe(platform, formattingCompiler, config, buildType); auto configs = getPackageConfigs(platform, config); PackageDescription packageDescription; foreach (pack; projectDescription.packages) { @@ -956,16 +956,16 @@ class Project { } /// Outputs the import paths for the project, including its dependencies. - string[] listImportPaths(BuildPlatform platform, string config, string buildType, bool nullDelim) + string[] listImportPaths(BuildPlatform platform, Compiler compiler, string config, string buildType, bool nullDelim) { - auto projectDescription = describe(platform, config, buildType); + auto projectDescription = describe(platform, compiler, config, buildType); return listBuildSetting!"importPaths"(platform, config, projectDescription, null, nullDelim); } /// Outputs the string import paths for the project, including its dependencies. - string[] listStringImportPaths(BuildPlatform platform, string config, string buildType, bool nullDelim) + string[] listStringImportPaths(BuildPlatform platform, Compiler compiler, string config, string buildType, bool nullDelim) { - auto projectDescription = describe(platform, config, buildType); + auto projectDescription = describe(platform, compiler, config, buildType); return listBuildSetting!"stringImportPaths"(platform, config, projectDescription, null, nullDelim); }