From e88c44d1818996b852bd0dfa0406f60e391f0687 Mon Sep 17 00:00:00 2001 From: Superstar64 Date: Mon, 19 Mar 2018 06:53:39 -0400 Subject: [PATCH] rdmd: support --eval to make it Makefile friendly --- changelog/rdmdMakefiles.dd | 12 ++++++++++++ rdmd.d | 8 +++++--- rdmd_test.d | 23 +++++++++++++++++++++++ travis.sh | 11 +++++++++-- 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 changelog/rdmdMakefiles.dd diff --git a/changelog/rdmdMakefiles.dd b/changelog/rdmdMakefiles.dd new file mode 100644 index 0000000..a66d89e --- /dev/null +++ b/changelog/rdmdMakefiles.dd @@ -0,0 +1,12 @@ +rdmd can now be used as a shell in makefiles + +With gnu make(3.82 or higher), rdmd can now be used in makefiles. +This is accomplished by setting the SHELL and .SHELLFLAGS to /usr/bin/rdmd and --eval respectively. +--- +.ONESHELL: +SHELL = /usr/bin/rdmd +.SHELLFLAGS = --eval +hello.txt: +$(TAB)import std.file; +$(TAB)write("$@","hello world\n"); +--- diff --git a/rdmd.d b/rdmd.d index b8ce054..fda6304 100755 --- a/rdmd.d +++ b/rdmd.d @@ -329,12 +329,14 @@ int main(string[] args) size_t indexOfProgram(string[] args) { - foreach(i, arg; args[1 .. $]) + foreach(i; 1 .. args.length) { + auto arg = args[i]; if (!arg.startsWith('-', '@') && - !arg.endsWith(".obj", ".o", ".lib", ".a", ".def", ".map", ".res")) + !arg.endsWith(".obj", ".o", ".lib", ".a", ".def", ".map", ".res") && + args[i - 1] != "--eval") { - return i + 1; + return i; } } diff --git a/rdmd_test.d b/rdmd_test.d index 790d29d..1ee42d8 100755 --- a/rdmd_test.d +++ b/rdmd_test.d @@ -614,6 +614,29 @@ void runTests(string rdmdApp, string compiler, string model) res = execute(rdmdArgs ~ [src1]); assert(res.status == 1, res.output); } + + { + import std.format : format; + + auto textOutput = tempDir().buildPath("rdmd_makefile_test.txt"); + if (exists(textOutput)) + { + remove(textOutput); + } + enum makefileFormatter = `.ONESHELL: +SHELL = %s +.SHELLFLAGS = %-(%s %) --eval +%s: + import std.file; + write("$@","hello world\n");`; + string makefileString = format!makefileFormatter(rdmdArgs[0], rdmdArgs[1 .. $], textOutput); + auto makefilePath = tempDir().buildPath("rdmd_makefile_test.mak"); + std.file.write(makefilePath, makefileString); + auto make = environment.get("MAKE") is null ? "make" : environment.get("MAKE"); + res = execute([make, "-f", makefilePath]); + assert(res.status == 0, res.output); + assert(std.file.read(textOutput) == "hello world\n"); + } } void runConcurrencyTest(string rdmdApp, string compiler, string model) diff --git a/travis.sh b/travis.sh index bbb5464..df6428d 100755 --- a/travis.sh +++ b/travis.sh @@ -10,8 +10,15 @@ set -uexo pipefail GDMD=$(find ~/dlang -type f -name "gdmd") LDMD2=$(find ~/dlang -type f -name "ldmd2") -make -f posix.mak all DMD="$(which dmd)" -make -f posix.mak test DMD="$(which dmd)" \ +curl https://ftp.gnu.org/gnu/make/make-4.2.tar.gz | tar -xz +cd make-4.2 +./configure +./build.sh +cd .. +export MAKE=$(pwd)/make-4.2/make + +$MAKE -f posix.mak all DMD="$(which dmd)" +$MAKE -f posix.mak test DMD="$(which dmd)" \ RDMD_TEST_COMPILERS=dmd,"$GDMD","$LDMD2" \ VERBOSE_RDMD_TEST=1