Skip to content

Commit 9b74389

Browse files
committed
Fixed: #67 - Now redub will look in system PATH before trying to download/use redub managed compilers. use now also accepts a path to a compiler
1 parent 16fe786 commit 9b74389

File tree

3 files changed

+74
-25
lines changed

3 files changed

+74
-25
lines changed

source/app.d

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,35 @@ int useMain(string[] args)
364364
{
365365
error("redub use requires 1 additional argument: ",
366366
"\n\topend <dmd|ldc>: uses the wanted opend compiler as the default",
367-
"\n\tldc <version?>: uses the latest ldc latest if version is unspecified.",
368-
"\n\tdmd <version?>: uses the "~DefaultDMDVersion~" dmd if the version is unspecified.",
369-
"\n\treset: removes the default compiler and redub will set it again by the first one found in the PATH environment variable",
367+
"\n\tldc <version|path?>: looks in PATH if version|path is unspecified. Downloads latest if not found.",
368+
"\n\tdmd <version|path?>: looks in PATH if version|path is unspecified. Downloads "~DefaultDMDVersion~" if not found.",
369+
"\n\treset: removes the default compiler and redub will set it again by the first one found in the PATH environment variable.",
370370
);
371371
return 1;
372372
}
373+
374+
static string tryGetCompiler(AcceptedCompiler comp, string versionOrPath, out bool useInternalCompilers)
375+
{
376+
bool useDefaultBehavior = versionOrPath.length == 0;
377+
string compiler = comp == AcceptedCompiler.dmd ? "dmd" : "ldc2";
378+
if(useDefaultBehavior)
379+
{
380+
import redub.tooling.compiler_identification;
381+
CompilerBinary bin = searchCompiler(compiler, false);
382+
if(bin.compiler == comp)
383+
return bin.bin;
384+
}
385+
else if(exists(versionOrPath)) //Path
386+
{
387+
CompilerBinary bin = searchCompiler(versionOrPath, false);
388+
if(bin.compiler != comp)
389+
throw new RedubException("Attempt to use path "~versionOrPath~" as a "~compiler~" compiler, but it is a "~bin.getCompilerString);
390+
return bin.bin;
391+
}
392+
useInternalCompilers = true;
393+
return null;
394+
}
395+
373396
string compiler = args[1];
374397
if(compiler.startsWith("opend"))
375398
{
@@ -392,35 +415,45 @@ int useMain(string[] args)
392415
else if(compiler == "ldc" || compiler == "ldc2")
393416
{
394417
import redub.api;
395-
import redub.misc.ldc_install;
396-
import redub.misc.github_tag_check;
397-
enum ldcRepo = "ldc-developers/ldc";
398418
string ldcVer = args.length > 2 ? args[2] : null;
399-
if(!ldcVer)
419+
bool useInternal;
420+
string ldcBin = tryGetCompiler(AcceptedCompiler.ldc2, ldcVer, useInternal);
421+
if(useInternal)
422+
{
423+
import redub.misc.ldc_install;
424+
import redub.misc.github_tag_check;
425+
enum ldcRepo = "ldc-developers/ldc";
426+
400427
ldcVer = getLatestGitRepositoryTag(ldcRepo);
401-
string ldcFolder = getLdcFolder(ldcVer);
402-
if(!exists(ldcFolder))
403-
installLdc(ldcVer);
404-
string ldcBin = "ldc2";
405-
version(Windows)
406-
ldcBin~= ".exe";
407-
ldcBin = buildNormalizedPath(ldcFolder, ldcBin);
428+
string ldcFolder = getLdcFolder(ldcVer);
429+
if(!exists(ldcFolder))
430+
installLdc(ldcVer);
431+
version(Windows)
432+
ldcBin = buildNormalizedPath(ldcFolder, "ldc2.exe");
433+
else
434+
ldcBin = buildNormalizedPath(ldcFolder, "ldc2");
435+
}
408436
saveGlobalCompiler(ldcBin, meta, true, false);
409437
}
410438
else if(compiler == "dmd")
411439
{
412440
import redub.misc.dmd_install;
413441
string dmdVer = args.length > 2 ? args[2] : DefaultDMDVersion;
414-
string dmdFolder = getDmdFolder(dmdVer);
415-
if(!exists(dmdFolder) && !installDmd(dmdVer))
442+
bool useInternal;
443+
string dmdBin = tryGetCompiler(AcceptedCompiler.dmd, dmdVer, useInternal);
444+
if(useInternal)
416445
{
417-
error("Could not install DMD for using it.");
418-
return 1;
446+
string dmdFolder = getDmdFolder(dmdVer);
447+
if(!exists(dmdFolder) && !installDmd(dmdVer))
448+
{
449+
error("Could not install DMD for using it.");
450+
return 1;
451+
}
452+
version(Windows)
453+
dmdBin = buildNormalizedPath(dmdFolder, "dmd.exe");
454+
else
455+
dmdBin = buildNormalizedPath(dmdFolder, "dmd");
419456
}
420-
string dmdBin = "dmd";
421-
version(Windows)
422-
dmdBin~= ".exe";
423-
dmdBin = buildNormalizedPath(dmdFolder, dmdBin);
424457
saveGlobalCompiler(dmdBin, meta, true, false);
425458
}
426459
else if(compiler.startsWith("reset"))

source/redub/misc/dmd_install.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module redub.misc.dmd_install;
22
import std.system;
33

4-
enum DefaultDMDVersion = "2.111.0";
4+
enum DefaultDMDVersion = "2.112.0";
55

66

77
string getDmdFolder(string ver, OS os = std.system.os, ISA isa = instructionSetArchitecture)

source/redub/tooling/compiler_identification.d

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,22 @@ private string getCompilerVersion(ref string compiler)
199199
return null;
200200
}
201201

202-
private CompilerBinary searchCompiler(string compilerOrPath, JSONValue compilersInfo, bool isDefault, bool isC, string compilerAssumption = null)
202+
/**
203+
* Utility to get compiler info given the arguments. Used with useMain to verify for existing compilers.
204+
* Params:
205+
* compilerOrPath = The compiler identifier (such as dmd|ldc etc) to find
206+
* isC = Are you looking for C compilers?
207+
* Returns: Extensive information about the compiler
208+
*/
209+
CompilerBinary searchCompiler(string compilerOrPath, bool isC)
210+
{
211+
import redub.meta;
212+
JSONValue compilersInfo = getRedubMeta();
213+
bool isDefault = compilerOrPath.length == 0;
214+
return searchCompiler(compilerOrPath, compilersInfo, isDefault, isC, null, false);
215+
}
216+
217+
private CompilerBinary searchCompiler(string compilerOrPath, JSONValue compilersInfo, bool isDefault, bool isC, string compilerAssumption = null, bool useGlobalPath = true)
203218
{
204219
import redub.misc.find_executable;
205220
import redub.tooling.compilers_inference;
@@ -218,7 +233,8 @@ private CompilerBinary searchCompiler(string compilerOrPath, JSONValue compilers
218233
compilerOrPath = locCompiler;
219234
else
220235
{
221-
ret = getCompilerFromGlobalPath(compilerOrPath, compilersInfo, compilerVersion);
236+
if(useGlobalPath)
237+
ret = getCompilerFromGlobalPath(compilerOrPath, compilersInfo, compilerVersion);
222238
isGlobal = true;
223239
}
224240
}

0 commit comments

Comments
 (0)