@@ -364,12 +364,35 @@ int useMain(string[] args)
364364 {
365365 error(" redub use requires 1 additional argument: " ,
366366 " \n\t opend <dmd|ldc>: uses the wanted opend compiler as the default" ,
367- " \n\t ldc <version?>: uses the latest ldc latest if version is unspecified." ,
368- " \n\t dmd <version?>: uses the " ~ DefaultDMDVersion~ " dmd if the version is unspecified ." ,
369- " \n\t reset: removes the default compiler and redub will set it again by the first one found in the PATH environment variable" ,
367+ " \n\t ldc <version|path ?>: looks in PATH if version|path is unspecified. Downloads latest if not found ." ,
368+ " \n\t dmd <version|path ?>: looks in PATH if version|path is unspecified. Downloads " ~ DefaultDMDVersion~ " if not found ." ,
369+ " \n\t reset: 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" ))
0 commit comments