From ef93e9aad18cfef4ea6d80e519b728f9badee18d Mon Sep 17 00:00:00 2001 From: Joachim Breitner Date: Sun, 21 Jun 2020 23:09:23 +0200 Subject: [PATCH] Inject a source identifer into `moc` (#1631) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have been preaching this for so long to other teams, I really better clean up my own backyard: Make sure that you can run moc --version and get a somewhat useful indication what version that is. This is important for when random people use `moc` from random versions of SDK or – later – build it themselves and then provide bug reports. When building locally, with `.git` around, this now injects the git revision: $ ./moc --version Motoko compiler (revision 9abae15e-dirty) (the `-dirty` indicates that my work-tree is not clean). When `.git` is _not_ available, as typically (and rightfully) when building with nix, it looks if Nix’s `$out` variable is set, and includes that: $ $(nix-build -A moc)/bin/moc --version Motoko compiler (revision 3yrjbw6g-aqppc05d-596yiq8k-bsznw5jw) This is less useful for end-users, but it still allows us to (hopefully) identify the version, in the worst case by running this command on all revisions to find the right one: $ nix-store --query --outputs $(nix-instantiate -A moc-bin) /nix/store/3yrjbw6gaqppc05d596yiq8kbsznw5jw-moc-bin Note that I injected dashes into the id so that it does not trip up `common.lib.standaloneRust` which otherwise would take it for an unwanted dependency. @basvandijk suggests in https://github.com/dfinity-lab/sdk/pull/383#issuecomment-588515223 that one may query Hydra’s database to map from id to revision. Once we do versioned releases, we can of course extend this to inject the version number there, and use `git describe --tag` to have `v1.0-200-9abae15e` like descriptions. I _hope_ this will not cause extra recompilations with `dune`, and will work out-of-the box for everyone, but we’ll see. --- default.nix | 2 ++ src/Makefile | 37 ++++++++++++++++-------- src/exes/deser.ml | 3 +- src/exes/didc.ml | 3 +- src/exes/dune | 6 ++-- src/exes/moc.ml | 3 +- src/source_id/.gitignore | 1 + src/source_id/dune | 1 + src/source_id/gen.sh | 18 ++++++++++++ test/repl/ok/double-import.stdout.ok | 2 +- test/repl/ok/file-and-repl.stdout.ok | 2 +- test/repl/ok/outrange-int-nat.stdout.ok | 2 +- test/repl/ok/stateful.stdout.ok | 2 +- test/repl/ok/triangle-import.stdout.ok | 2 +- test/repl/ok/type-lub-repl.stdout.ok | 2 +- test/repl/ok/variant-shorthand.stdout.ok | 2 +- test/run.sh | 1 + 17 files changed, 61 insertions(+), 28 deletions(-) create mode 100644 src/source_id/.gitignore create mode 100644 src/source_id/dune create mode 100755 src/source_id/gen.sh diff --git a/default.nix b/default.nix index d434b094cdc..c4836695ab0 100644 --- a/default.nix +++ b/default.nix @@ -89,6 +89,7 @@ let ocaml_exe = name: bin: buildInputs = commonBuildInputs staticpkgs; buildPhase = '' + patchShebangs . make DUNE_OPTS="--display=short --profile ${profile}" ${bin} ''; @@ -312,6 +313,7 @@ rec { nixpkgs.nodejs-10_x ]; buildPhase = '' + patchShebangs . make ${n}.js ''; installPhase = '' diff --git a/src/Makefile b/src/Makefile index 104fecc71be..32841640a8d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -17,9 +17,12 @@ ALL_TARGETS = $(MO_IDE_TARGET) $(MO_LD_TARGET) $(MO_DOC_TARGET) $(MOC_TARGET) $( .PHONY: all clean moc mo-doc mo-ide mo-ld moc.js didc deser unit-tests didc.js +# let make update check this file every time +SOURCE_ID = source_id/source_id.ml + # This targets is spelled out so that `make` # only invokes `dune` once, much faster -all: grammar +all: $(SOURCE_ID) grammar dune build $(DUNE_OPTS) $(ALL_TARGETS) @ln -fs $(MOC_TARGET) moc @ln -fs $(MO_IDE_TARGET) mo-ide @@ -28,47 +31,58 @@ all: grammar @ln -fs $(MOC_JS_TARGET) moc.js @ln -fs $(DIDC_TARGET) didc @ln -fs $(DESER_TARGET) deser + @ln -fs $(DIDC_JS_TARGET) didc.js -moc: +moc: $(SOURCE_ID) dune build $(DUNE_OPTS) $(MOC_TARGET) @ln -fs $(MOC_TARGET) moc -mo-ide: +mo-ide: $(SOURCE_ID) dune build $(DUNE_OPTS) $(MO_IDE_TARGET) @ln -fs $(MO_IDE_TARGET) mo-ide -mo-ld: +mo-ld: $(SOURCE_ID) dune build $(DUNE_OPTS) $(MO_LD_TARGET) @ln -fs $(MO_LD_TARGET) mo-ld -mo-doc: +mo-doc: $(SOURCE_ID) dune build $(DUNE_OPTS) $(MO_DOC_TARGET) @ln -fs $(MO_DOC_TARGET) mo-doc -moc.js: +moc.js: $(SOURCE_ID) dune build $(DUNE_OPTS) $(MOC_JS_TARGET) @ln -fs $(MOC_JS_TARGET) moc.js -didc.js: +didc.js: $(SOURCE_ID) dune build $(DUNE_OPTS) $(DIDC_JS_TARGET) @ln -fs $(DIDC_JS_TARGET) didc.js -didc: +didc: $(SOURCE_ID) dune build $(DUNE_OPTS) $(DIDC_TARGET) @ln -fs $(DIDC_TARGET) didc -deser: +deser: $(SOURCE_ID) dune build $(DUNE_OPTS) $(DESER_TARGET) @ln -fs $(DESER_TARGET) deser +.PHONY: $(SOURCE_ID) + +# only actually touch the file if changed +$(SOURCE_ID): + source_id/gen.sh + unit-tests: dune runtest $(DUNE_OPTS) format: - ocamlformat --inplace docs/*.mli docs/*.ml languageServer/*.ml languageServer/*.mli + ocamlformat --inplace \ + docs/*.mli \ + docs/*.ml \ + languageServer/*.ml \ + languageServer/*.mli clean: - rm -f moc mo-ide mo-ld moc.js didc deser + rm -f moc mo-ide mo-ld moc.js didc deser $(SOURCE_ID) dune clean test: $(NAME) @@ -78,7 +92,6 @@ test: $(NAME) test-quick: $(NAME) make -C ../test MOC=$(MOC) quick - grammar: ../doc/modules/language-guide/examples/grammar.txt ../doc/modules/language-guide/examples/grammar.txt: mo_frontend/parser.mly gen-grammar/gen-grammar.sh gen-grammar/grammar.sed diff --git a/src/exes/deser.ml b/src/exes/deser.ml index 90c807a0410..9e11b6bac11 100644 --- a/src/exes/deser.ml +++ b/src/exes/deser.ml @@ -491,8 +491,7 @@ end (* CLI *) let name = "deser" -let version = "0.1" -let banner = "Interface Description Language (IDL) " ^ version ^ " message dumper" +let banner = "Candid toolkit (revision " ^ Source_id.id ^ ")" let usage = "Usage: " ^ name ^ " [option] [file ...]" let mode = ref Nary diff --git a/src/exes/didc.ml b/src/exes/didc.ml index b6550fa209c..c0eba3e6fe9 100644 --- a/src/exes/didc.ml +++ b/src/exes/didc.ml @@ -2,8 +2,7 @@ open Idllib open Printf let name = "didc" -let version = "0.1" -let banner = "Interface Description Language (IDL) " ^ version ^ " interpreter" +let banner = "Candid compiler (revision " ^ Source_id.id ^ ")" let usage = "Usage: " ^ name ^ " [option] [file ...]" diff --git a/src/exes/dune b/src/exes/dune index 1089574b32c..0d37f553045 100644 --- a/src/exes/dune +++ b/src/exes/dune @@ -1,7 +1,7 @@ (executable (name moc) (modules moc) - (libraries profiler pipeline) + (libraries profiler pipeline source_id) ) (executable (name mo_ide) @@ -21,10 +21,10 @@ (executable (name didc) (modules didc) - (libraries idllib) + (libraries idllib source_id) ) (executable (name deser) (modules deser) - (libraries stdio num) + (libraries stdio num source_id) ) diff --git a/src/exes/moc.ml b/src/exes/moc.ml index 154d6ec7c39..3bdd76bb49d 100644 --- a/src/exes/moc.ml +++ b/src/exes/moc.ml @@ -4,8 +4,7 @@ open Mo_config open Printf let name = "moc" -let version = "0.1" -let banner = "Motoko " ^ version ^ " interpreter" +let banner = "Motoko compiler (revision " ^ Source_id.id ^ ")" let usage = "Usage: " ^ name ^ " [option] [file ...]" diff --git a/src/source_id/.gitignore b/src/source_id/.gitignore new file mode 100644 index 00000000000..0dba3891d38 --- /dev/null +++ b/src/source_id/.gitignore @@ -0,0 +1 @@ +source_id.ml diff --git a/src/source_id/dune b/src/source_id/dune new file mode 100644 index 00000000000..eb9dfedbccb --- /dev/null +++ b/src/source_id/dune @@ -0,0 +1 @@ +(library (name source_id)) diff --git a/src/source_id/gen.sh b/src/source_id/gen.sh new file mode 100755 index 00000000000..0aee80bf06d --- /dev/null +++ b/src/source_id/gen.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +cd "$(dirname "$BASH_SOURCE")" + +file="source_id.ml" + +if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ] +then echo "let id = \"$(git describe --always --dirty)\"" > $file.tmp +elif [ -n "$out" ] +then echo "let id = \"$(echo $out|cut -d/ -f4|cut -d- -f1|fold -w8 | paste -sd'-' -)\"" > $file.tmp +else echo "let id = \"unidentified version\"" > $file.tmp +fi +if [ ! -e $file ] || ! cmp -s $file.tmp $file +then mv $file.tmp $file +else rm -f $file.tmp +fi + + diff --git a/test/repl/ok/double-import.stdout.ok b/test/repl/ok/double-import.stdout.ok index 1c8fbb037f3..00e1ccdb3f7 100644 --- a/test/repl/ok/double-import.stdout.ok +++ b/test/repl/ok/double-import.stdout.ok @@ -1,4 +1,4 @@ -Motoko 0.1 interpreter +Motoko compiler (revision XXX) -- Parsing stdin: > -- Parsing lib/empty.mo: -- Checking empty.mo: diff --git a/test/repl/ok/file-and-repl.stdout.ok b/test/repl/ok/file-and-repl.stdout.ok index 52b8ed87e51..407a3419fe5 100644 --- a/test/repl/ok/file-and-repl.stdout.ok +++ b/test/repl/ok/file-and-repl.stdout.ok @@ -1,4 +1,4 @@ -Motoko 0.1 interpreter +Motoko compiler (revision XXX) -- Parsing /dev/fd/63: -- Checking /dev/fd/63: -- Definedness /dev/fd/63: diff --git a/test/repl/ok/outrange-int-nat.stdout.ok b/test/repl/ok/outrange-int-nat.stdout.ok index 4991e7fc60c..8c22a277283 100644 --- a/test/repl/ok/outrange-int-nat.stdout.ok +++ b/test/repl/ok/outrange-int-nat.stdout.ok @@ -1,4 +1,4 @@ -Motoko 0.1 interpreter +Motoko compiler (revision XXX) > let Prim : module {...} > +127 : Int8 > prim:___: execution error, numeric overflow diff --git a/test/repl/ok/stateful.stdout.ok b/test/repl/ok/stateful.stdout.ok index de1ae251ba1..cec7e92bb4a 100644 --- a/test/repl/ok/stateful.stdout.ok +++ b/test/repl/ok/stateful.stdout.ok @@ -1,3 +1,3 @@ -Motoko 0.1 interpreter +Motoko compiler (revision XXX) > let x : Nat = 42 > > > > diff --git a/test/repl/ok/triangle-import.stdout.ok b/test/repl/ok/triangle-import.stdout.ok index c3c1e0e1472..e5745f3b962 100644 --- a/test/repl/ok/triangle-import.stdout.ok +++ b/test/repl/ok/triangle-import.stdout.ok @@ -1,4 +1,4 @@ -Motoko 0.1 interpreter +Motoko compiler (revision XXX) -- Parsing stdin: > -- Parsing lib/b.mo: -- Parsing lib/c.mo: diff --git a/test/repl/ok/type-lub-repl.stdout.ok b/test/repl/ok/type-lub-repl.stdout.ok index d43988727e7..adf962550c7 100644 --- a/test/repl/ok/type-lub-repl.stdout.ok +++ b/test/repl/ok/type-lub-repl.stdout.ok @@ -1,4 +1,4 @@ -Motoko 0.1 interpreter +Motoko compiler (revision XXX) > [null, ?42, ?(-25)] : [(?Int)] > [null, null] : [Null] > [{a = 42}, {b = 42}] : [{}] diff --git a/test/repl/ok/variant-shorthand.stdout.ok b/test/repl/ok/variant-shorthand.stdout.ok index 98c2894b838..f8b02c43219 100644 --- a/test/repl/ok/variant-shorthand.stdout.ok +++ b/test/repl/ok/variant-shorthand.stdout.ok @@ -1,4 +1,4 @@ -Motoko 0.1 interpreter +Motoko compiler (revision XXX) > #bar : {#bar} > #foo(#bar) : {#foo : {#bar}} > [#Monday, #Tuesday, #Wednesday, #Thursday, #Friday, #Saturday, #Sunday] : [{#Friday; #Monday; #Saturday; #Sunday; #Thursday; #Tuesday; #Wednesday}] diff --git a/test/run.sh b/test/run.sh index d6c30bd5be1..a2780d8b4ab 100755 --- a/test/run.sh +++ b/test/run.sh @@ -88,6 +88,7 @@ function normalize () { sed 's/trap at 0x[a-f0-9]*/trap at 0x___:/g' | sed 's/source location: @[a-f0-9]*/source location: @___:/g' | sed 's/Ignore Diff:.*/Ignore Diff: (ignored)/ig' | + sed 's/compiler (revision .*)/compiler (revision XXX)/ig' | cat > $1.norm mv $1.norm $1 fi