diff --git a/source/dub/commandline.d b/source/dub/commandline.d index 6571982..6599651 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -20,7 +20,6 @@ import dub.platform : determineCompiler; import dub.project; import dub.internal.utils : getDUBVersion, getClosestMatch; -import dub.version_; import std.algorithm; import std.array; @@ -357,7 +356,7 @@ string m_buildType; BuildMode m_buildMode; string m_buildConfig; - string m_compilerName = initialCompilerBinary; + string m_compilerName; string m_arch; string[] m_debugVersions; Compiler m_compiler; @@ -368,6 +367,11 @@ bool m_forceRemove = false; } + this() + { + m_compilerName = defaultCompiler(); + } + override void prepare(scope CommandArgs args) { args.getopt("b|build", &m_buildType, [ @@ -382,7 +386,7 @@ "Specifies the compiler binary to use (can be a path).", "Arbitrary pre- and suffixes to the identifiers below are recognized (e.g. ldc2 or dmd-2.063) and matched to the proper compiler type:", " "~["dmd", "gdc", "ldc", "gdmd", "ldmd"].join(", "), - "Default value: "~initialCompilerBinary, + "Default value: "~m_compilerName, ]); args.getopt("a|arch", &m_arch, [ "Force a different architecture (e.g. x86 or x86_64)" diff --git a/source/dub/compilers/compiler.d b/source/dub/compilers/compiler.d index dafa331..7cab029 100644 --- a/source/dub/compilers/compiler.d +++ b/source/dub/compilers/compiler.d @@ -12,6 +12,7 @@ import dub.compilers.dmd; import dub.compilers.gdc; import dub.compilers.ldc; +import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.path; @@ -30,7 +31,6 @@ registerCompiler(new LdcCompiler); } - Compiler getCompiler(string name) { foreach (c; s_compilers) @@ -45,6 +45,26 @@ throw new Exception("Unknown compiler: "~name); } +string defaultCompiler() +{ + static string name; + if (!name.length) name = findCompiler(); + return name; +} + +private string findCompiler() +{ + import std.process : env=environment; + import dub.version_ : initialCompilerBinary; + version (Windows) enum sep = ";", exe = ".exe"; + version (Posix) enum sep = ":", exe = ""; + + auto paths = env.get("PATH", "").splitter(sep).map!Path; + auto res = [initialCompilerBinary, "dmd", "gdc", "gdmd", "ldc2", "ldmd2"] + .find!(bin => paths.canFind!(p => existsFile(p ~ (bin~exe)))); + return res.empty ? initialCompilerBinary : res.front; +} + void registerCompiler(Compiler c) { s_compilers ~= c; @@ -388,7 +408,7 @@ pragma(msg, ` ` ~ determineArchitecture()); pragma(msg, ` ],`); pragma(msg, `}`); - + string determinePlatform() { string ret;