Skip to content

Commit

Permalink
Inject a source identifer into moc (#1631)
Browse files Browse the repository at this point in the history
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
dfinity/sdk#383 (comment) 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.
  • Loading branch information
nomeata authored Jun 21, 2020
1 parent ae4abf0 commit ef93e9a
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 28 deletions.
2 changes: 2 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ let ocaml_exe = name: bin:
buildInputs = commonBuildInputs staticpkgs;

buildPhase = ''
patchShebangs .
make DUNE_OPTS="--display=short --profile ${profile}" ${bin}
'';

Expand Down Expand Up @@ -312,6 +313,7 @@ rec {
nixpkgs.nodejs-10_x
];
buildPhase = ''
patchShebangs .
make ${n}.js
'';
installPhase = ''
Expand Down
37 changes: 25 additions & 12 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/exes/deser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/exes/didc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...]"


Expand Down
6 changes: 3 additions & 3 deletions src/exes/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(executable
(name moc)
(modules moc)
(libraries profiler pipeline)
(libraries profiler pipeline source_id)
)
(executable
(name mo_ide)
Expand All @@ -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)
)
3 changes: 1 addition & 2 deletions src/exes/moc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...]"


Expand Down
1 change: 1 addition & 0 deletions src/source_id/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source_id.ml
1 change: 1 addition & 0 deletions src/source_id/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(library (name source_id))
18 changes: 18 additions & 0 deletions src/source_id/gen.sh
Original file line number Diff line number Diff line change
@@ -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


2 changes: 1 addition & 1 deletion test/repl/ok/double-import.stdout.ok
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Motoko 0.1 interpreter
Motoko compiler (revision XXX)
-- Parsing stdin:
> -- Parsing lib/empty.mo:
-- Checking empty.mo:
Expand Down
2 changes: 1 addition & 1 deletion test/repl/ok/file-and-repl.stdout.ok
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Motoko 0.1 interpreter
Motoko compiler (revision XXX)
-- Parsing /dev/fd/63:
-- Checking /dev/fd/63:
-- Definedness /dev/fd/63:
Expand Down
2 changes: 1 addition & 1 deletion test/repl/ok/outrange-int-nat.stdout.ok
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Motoko 0.1 interpreter
Motoko compiler (revision XXX)
> let Prim : module {...}
> +127 : Int8
> prim:___: execution error, numeric overflow
Expand Down
2 changes: 1 addition & 1 deletion test/repl/ok/stateful.stdout.ok
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Motoko 0.1 interpreter
Motoko compiler (revision XXX)
> let x : Nat = 42
> > > >
2 changes: 1 addition & 1 deletion test/repl/ok/triangle-import.stdout.ok
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Motoko 0.1 interpreter
Motoko compiler (revision XXX)
-- Parsing stdin:
> -- Parsing lib/b.mo:
-- Parsing lib/c.mo:
Expand Down
2 changes: 1 addition & 1 deletion test/repl/ok/type-lub-repl.stdout.ok
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Motoko 0.1 interpreter
Motoko compiler (revision XXX)
> [null, ?42, ?(-25)] : [(?Int)]
> [null, null] : [Null]
> [{a = 42}, {b = 42}] : [{}]
Expand Down
2 changes: 1 addition & 1 deletion test/repl/ok/variant-shorthand.stdout.ok
Original file line number Diff line number Diff line change
@@ -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}]
Expand Down
1 change: 1 addition & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ef93e9a

Please sign in to comment.