Skip to content

Commit

Permalink
Use DC env variable as the default compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Nov 30, 2020
1 parent 57f89af commit d696b36
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions changelog/env-d-compiler.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Use DC environment variable as default D compiler

dub now respects the `DC` environment variable, meaning that `DC=ldc2 dub build` will behave as `dub build --compiler=ldc2`.
In case both are supplied, the `--compiler` switch still has priority.
Note that when DUB recursively invokes itself, for example in `preGenerateCommands`,
it sets the `DC` variable to the compiler it is using, meaning that nested dub invocation will now use the same compiler.
6 changes: 5 additions & 1 deletion source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,11 @@ class Dub {
import std.process : environment;
import std.range : front;

m_defaultCompiler = m_config.defaultCompiler.expandTilde;
// Env takes precedence
if (auto envCompiler = environment.get("DC"))
m_defaultCompiler = envCompiler;
else
m_defaultCompiler = m_config.defaultCompiler.expandTilde;
if (m_defaultCompiler.length && m_defaultCompiler.isAbsolute)
return;

Expand Down
6 changes: 6 additions & 0 deletions test/dc-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

. $(dirname "${BASH_SOURCE[0]}")/common.sh
cd ${CURR_DIR}/issue2012-dc-env

$DUB app.d ${DC}
Empty file added test/issue2012-dc-env/.no_build
Empty file.
18 changes: 18 additions & 0 deletions test/issue2012-dc-env/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env dub
/+ dub.sdl:
name "app"
+/

import std.format;

void main(string[] args)
{
version (LDC)
immutable expected = "ldc2";
version (DigitalMars)
immutable expected = "dmd";
version (GNU)
immutable expected = "gdc";

assert(expected == args[1], format!"Expected '%s' but got '%s'"(expected, args[1]));
}
2 changes: 2 additions & 0 deletions test/issue895-local-configuration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function cleanup {

trap cleanup EXIT

unset DC

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF "Unknown compiler: $(dirname $CURR_DIR)/bin/foo"; then
rm ../bin/foo
die $LINENO 'DUB did not find the local configuration with an adjacent compiler.'
Expand Down

0 comments on commit d696b36

Please sign in to comment.