From 0c12f5509b7388b9dd09ae38dcb62d9551e4ebd3 Mon Sep 17 00:00:00 2001 From: Joachim Breitner Date: Sat, 20 Jun 2020 17:14:34 +0200 Subject: [PATCH] Inject a source identifer into `moc` 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-tee is not clean). When `.git` is _not_ available, as typially (and rightfully) when building with nix, it looks if Nix’s `$out` variable is set, and includes that: ~/dfinity/motoko $ $(nix-build -A moc)/bin/moc --version Motoko compiler (revision xx6fmamdh5bjph9law6x02d7f4hw8f84-moc-bin) 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/xx6fmamdh5bjph9law6x02d7f4hw8f84-moc-bin Or by querying hydra (as @basvandijk suggests in https://github.com/dfinity-lab/sdk/pull/383#issuecomment-588515223) 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 derscriptions. I _hope_ this will not cause extra recompilations with `dune`, and will work out-of-the box for everyone, but we’ll see. --- src/Makefile | 39 ++++++++++++++++++++++++++------------- 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 ++++++++++++++++++ 8 files changed, 52 insertions(+), 22 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/src/Makefile b/src/Makefile index b03a3afcbc8..32841640a8d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,13 +13,16 @@ DESER_TARGET = _build/default/exes/deser.exe DUNE_OPTS ?= -ALL_TARGETS = $(MO_IDE_TARGET) $(MO_LD_TARGET) $(MO_DOC_TARGET) $(MOC_TARGET) $(MOC_JS_TARGET) $(DIDC_TARGET) $(DESER_TARGET) ($DIDC_JS_TARGET) +ALL_TARGETS = $(MO_IDE_TARGET) $(MO_LD_TARGET) $(MO_DOC_TARGET) $(MOC_TARGET) $(MOC_JS_TARGET) $(DIDC_TARGET) $(DESER_TARGET) $(DIDC_JS_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..0b81e54feb6 --- /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)\"" > $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 + +