Skip to content

Commit

Permalink
Inject a source identifer into moc
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-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
dfinity/sdk#383 (comment))

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.
  • Loading branch information
nomeata committed Jun 20, 2020
1 parent 63d7b5d commit 0c12f55
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
39 changes: 26 additions & 13 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)\"" > $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


0 comments on commit 0c12f55

Please sign in to comment.