From d696b36ec59a77450787ce8de91c87758fa34c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20IRMAK?= Date: Mon, 30 Nov 2020 13:03:45 +0300 Subject: [PATCH] Use DC env variable as the default compiler --- changelog/env-d-compiler.dd | 6 ++++++ source/dub/dub.d | 6 +++++- test/dc-env.sh | 6 ++++++ test/issue2012-dc-env/.no_build | 0 test/issue2012-dc-env/app.d | 18 ++++++++++++++++++ test/issue895-local-configuration.sh | 2 ++ 6 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 changelog/env-d-compiler.dd create mode 100755 test/dc-env.sh create mode 100644 test/issue2012-dc-env/.no_build create mode 100644 test/issue2012-dc-env/app.d diff --git a/changelog/env-d-compiler.dd b/changelog/env-d-compiler.dd new file mode 100644 index 000000000..8769ceabf --- /dev/null +++ b/changelog/env-d-compiler.dd @@ -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. diff --git a/source/dub/dub.d b/source/dub/dub.d index 3d05126a7..70951feaf 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -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; diff --git a/test/dc-env.sh b/test/dc-env.sh new file mode 100755 index 000000000..e5ea3bc43 --- /dev/null +++ b/test/dc-env.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +. $(dirname "${BASH_SOURCE[0]}")/common.sh +cd ${CURR_DIR}/issue2012-dc-env + +$DUB app.d ${DC} diff --git a/test/issue2012-dc-env/.no_build b/test/issue2012-dc-env/.no_build new file mode 100644 index 000000000..e69de29bb diff --git a/test/issue2012-dc-env/app.d b/test/issue2012-dc-env/app.d new file mode 100644 index 000000000..4b1f4d106 --- /dev/null +++ b/test/issue2012-dc-env/app.d @@ -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])); +} diff --git a/test/issue895-local-configuration.sh b/test/issue895-local-configuration.sh index 9799288ed..8593c0082 100755 --- a/test/issue895-local-configuration.sh +++ b/test/issue895-local-configuration.sh @@ -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.'