diff --git a/src/dmd/json.d b/src/dmd/json.d index c1327d0275fc..ccc296f0f9e4 100644 --- a/src/dmd/json.d +++ b/src/dmd/json.d @@ -806,3 +806,12 @@ extern (C++) void json_generate(OutBuffer* buf, Modules* modules) json.arrayEnd(); json.removeComma(); } + +extern (C++) void json_info(OutBuffer* buf) +{ + scope ToJsonVisitor json = new ToJsonVisitor(buf); + json.objectStart(); + json.property("compilerVendor", global.compiler.vendor); + json.objectEnd(); + json.removeComma(); +} diff --git a/src/dmd/mars.d b/src/dmd/mars.d index a946590aee82..efdebf511d75 100644 --- a/src/dmd/mars.d +++ b/src/dmd/mars.d @@ -360,8 +360,33 @@ private int tryMain(size_t argc, const(char)** argv) } if (files.dim == 0) { - usage(); - return EXIT_FAILURE; + // -X without arguments is the JSON analog of --version + if (global.params.doJsonGeneration) + { + OutBuffer buf; + json_info(&buf); + buf.writestring("\n"); + // write to stdout for -X and -Xf=- + if (!global.params.jsonfilename || strcmp(global.params.jsonfilename, "-") == 0) + { + fputs(buf.peekString(), stdout); + fflush(stdout); + } + else + { + ensurePathToNameExists(Loc.initial, global.params.jsonfilename); + auto jsonfile = new File(global.params.jsonfilename); + jsonfile.setbuffer(buf.data, buf.offset); + jsonfile._ref = 1; + writeFile(Loc.initial, jsonfile); + } + return EXIT_SUCCESS; + } + else + { + usage(); + return EXIT_FAILURE; + } } static if (TARGET.OSX) { diff --git a/test/compilable/extra-files/testjsonstdout.json b/test/compilable/extra-files/testjsonstdout.json new file mode 100644 index 000000000000..76faeac31d42 --- /dev/null +++ b/test/compilable/extra-files/testjsonstdout.json @@ -0,0 +1,3 @@ +{ + "compilerVendor" : "Digital Mars D" +} diff --git a/test/compilable/testjsonstdout.sh b/test/compilable/testjsonstdout.sh new file mode 100755 index 000000000000..3b06586ea60e --- /dev/null +++ b/test/compilable/testjsonstdout.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -ueo pipefail + +name="$(basename "$0" .sh)" +dir="${RESULTS_DIR}/compilable/" +out="$dir/${name}.json.out" + +"$DMD" -X > "$out" +diff "$out" compilable/extra-files/$name.json +rm "$out" + +"$DMD" -Xf=- > "$out" +diff "$out" compilable/extra-files/$name.json +rm "$out" + +"$DMD" -Xf="$out" +diff "$out" compilable/extra-files/$name.json +rm "$out" + +echo "OK" > "${dir}/${name}.sh.out"