Skip to content

Commit 5dde366

Browse files
authored
Merge pull request #516 from mtzguido/no_fstar_home
No FSTAR_HOME
2 parents fc68c6d + aaff37e commit 5dde366

File tree

13 files changed

+65
-131
lines changed

13 files changed

+65
-131
lines changed

.nix/karamel.nix

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ in
3333

3434
outputs = ["out" "home"];
3535

36-
FSTAR_HOME = fstar;
3736
GIT_REV = version;
3837

3938
configurePhase = "export KRML_HOME=$(pwd)";
@@ -58,9 +57,6 @@ in
5857
passthru = {
5958
lib = ocamlPackages.buildDunePackage {
6059
GIT_REV = version;
61-
# the Makefile expects `FSTAR_HOME` to be or `fstar.exe` to be
62-
# in PATH, but this is not useful for buulding the library
63-
FSTAR_HOME = "dummy";
6460
inherit version propagatedBuildInputs;
6561
nativeBuildInputs = with ocamlPackages; [menhir];
6662
pname = "krml";

Makefile

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,14 @@ include $(visitors_root)/Makefile.preprocess
1414

1515
.PHONY: all minimal clean test pre krmllib install
1616

17-
ifeq ($(OS),Windows_NT)
18-
OCAMLPATH_SEP=;
19-
else
20-
OCAMLPATH_SEP=:
21-
endif
17+
FSTAR_EXE ?= fstar.exe
2218

2319
all: minimal krmllib
2420

25-
ifdef FSTAR_HOME
26-
OCAMLPATH:=$(OCAMLPATH)$(OCAMLPATH_SEP)$(FSTAR_HOME)/lib
27-
else
28-
FSTAR_EXE=$(shell which fstar.exe)
29-
ifneq ($(FSTAR_EXE),)
30-
FSTAR_HOME=$(dir $(FSTAR_EXE))/..
31-
OCAMLPATH:=$(OCAMLPATH)$(OCAMLPATH_SEP)$(FSTAR_HOME)/lib
32-
else
33-
# If we are just trying to do a minimal build, we don't need F*.
34-
ifneq ($(MAKECMDGOALS),minimal)
35-
$(error "fstar.exe not found, please install FStar")
36-
endif
37-
endif
38-
endif
39-
export FSTAR_HOME
40-
export OCAMLPATH
21+
# If we are just trying to do a minimal build, we don't need F*.
22+
# Note: lazy assignment so this does not warn if fstar.exe is not there
23+
# (e.g. on a minimal build or cleaning)
24+
FSTAR_OCAMLENV = $(shell $(FSTAR_EXE) --ocamlenv)
4125

4226
minimal: lib/AutoConfig.ml lib/Version.ml
4327
dune build
@@ -74,8 +58,9 @@ test: all
7458

7559
# Auto-detection
7660
pre:
77-
@ocamlfind query fstar.lib >/dev/null 2>&1 || \
78-
{ echo "Didn't find fstar.lib via ocamlfind or in FSTAR_HOME (which is: $(FSTAR_HOME)); run $(MAKE) -C $(FSTAR_HOME)"; exit 1; }
61+
@eval "$(FSTAR_OCAMLENV)" && \
62+
ocamlfind query fstar.lib >/dev/null 2>&1 || \
63+
{ echo "Didn't find fstar.lib via ocamlfind; is F* properly installed? (FSTAR_EXE = $(FSTAR_EXE))"; exit 1; }
7964

8065

8166
install: all

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ make via homebrew, and invoke `gmake` instead of `make`.
4848

4949
`$ opam install ppx_deriving_yojson zarith pprint "menhir>=20161115" sedlex process fix "wasm>=2.0.0" visitors ctypes-foreign ctypes uucp`
5050

51-
Next, make sure you have an up-to-date F\*, and that you ran `make` in the
52-
`ulib/ml` directory of F\*. The `fstar.exe` executable should be on your PATH
53-
and `FSTAR_HOME` should point to the directory where F\* is installed.
51+
Next, make sure you have an up-to-date F\*, either as a binary package
52+
or that you have built it by running `make`. The `fstar.exe` executable
53+
should be on your PATH, or you may set the variable `FSTAR_EXE` to its
54+
location.
5455

5556
To build just run `make` from this directory.
5657

book/Setup.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ For a nix flake based install use
4040
4141
$ nix shell
4242
43-
In any case, remember to export suitable values for the ``FSTAR_HOME`` and
43+
In any case, remember to export suitable values for the ``FSTAR_EXE`` and
4444
``KRML_HOME`` environment variables once you're done.
4545

4646
We strongly recommend using the `fstar-mode.el

book/tutorial/Makefile

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,7 @@ FSTAR_EXTRACT = --extract 'OCaml:-* +Spec'
5151
# - --cache_checked_modules to rely on a pre-built ulib and krmllib
5252
# - --cache_dir, to avoid polluting our generated build artifacts outside o
5353

54-
# Where is F* ?
55-
ifndef FSTAR_HOME
56-
FSTAR_EXE=$(shell which fstar.exe)
57-
ifneq ($(FSTAR_EXE),)
58-
# Assuming that fstar.exe is in some ..../bin directory
59-
FSTAR_HOME=$(dir $(FSTAR_EXE))/..
60-
else
61-
$(error "fstar.exe not found, please install FStar")
62-
endif
63-
endif
64-
export FSTAR_HOME
65-
66-
FSTAR_NO_FLAGS = $(FSTAR_HOME)/bin/fstar.exe $(FSTAR_HINTS) \
54+
FSTAR_NO_FLAGS = $(FSTAR_EXE) $(FSTAR_HINTS) \
6755
--odir obj --cache_checked_modules $(FSTAR_INCLUDES) --cmi \
6856
--already_cached 'Prims FStar LowStar C Spec.Loops TestLib WasmSupport' --warn_error '+241@247+285' \
6957
--cache_dir obj --hint_dir hints
@@ -234,9 +222,9 @@ dist/libbignum.a: dist/Makefile.basic
234222
# First complication... no comment.
235223
# NOTE: if F* was installed via opam, then this is redundant
236224
ifeq ($(OS),Windows_NT)
237-
export OCAMLPATH := $(FSTAR_HOME)/lib;$(OCAMLPATH)
225+
export OCAMLPATH := $(shell $(FSTAR_EXE) --locate_ocaml);$(OCAMLPATH)
238226
else
239-
export OCAMLPATH := $(FSTAR_HOME)/lib:$(OCAMLPATH)
227+
export OCAMLPATH := $(shell $(FSTAR_EXE) --locate_ocaml):$(OCAMLPATH)
240228
endif
241229

242230
# Second complication: F* generates a list of ML files in the reverse linking

book/tutorial/Makefile.include

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,12 @@
44

55
BIGNUM_HOME ?= .
66

7-
# Where is F* ?
8-
ifndef FSTAR_HOME
9-
FSTAR_EXE=$(shell which fstar.exe)
10-
ifneq ($(FSTAR_EXE),)
11-
# Assuming that fstar.exe is in some ..../bin directory
12-
FSTAR_HOME=$(dir $(FSTAR_EXE))/..
13-
else
14-
$(error "fstar.exe not found, please install FStar")
15-
endif
16-
endif
17-
export FSTAR_HOME
18-
197
ifeq (,$(KRML_HOME))
208
$(error KRML_HOME is not defined)
219
endif
2210

11+
FSTAR_EXE ?= fstar.exe
12+
2313
# I prefer to always check all fst files in the source directories; otherwise,
2414
# it's too easy to add a new file only to find out later that it's not being
2515
# checked. Note the usage of BIGNUM_HOME

krmllib/Makefile

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ all: verify-all compile-all
1313
# Verification & extraction #
1414
################################################################################
1515

16-
ifdef FSTAR_HOME
17-
# Assume there is a F* source tree
18-
FSTAR_EXE=$(FSTAR_HOME)/bin/fstar.exe
19-
else
20-
# Assume F* in the PATH
21-
FSTAR_EXE=fstar.exe
22-
endif
16+
FSTAR_EXE ?= fstar.exe
2317

2418
EXTRACT_DIR = .extract
2519

@@ -98,7 +92,7 @@ $(EXTRACT_DIR)/%.krml: | .depend
9892
# We don't extract LowStar.Lib as everything is generic data structures that
9993
# need to be specialized based on their usage from client code.
10094
KRML_ARGS += -fparentheses -fcurly-braces -fno-shadow -header copyright-header.txt \
101-
-minimal -skip-compilation -extract-uints \
95+
-fstar $(FSTAR_EXE) -minimal -skip-compilation -extract-uints \
10296
-bundle 'LowStar.Lib.\*'
10397

10498
MACHINE_INTS = \

lib/Driver.ml

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ module P = Process
4545
* understood as a cyclic dependency on our own [Output] module. *)
4646

4747
(** These three variables filled in by [detect_fstar] *)
48-
let fstar = ref ""
49-
let fstar_home = ref ""
48+
let fstar = Options.fstar
5049
let fstar_lib = ref ""
5150
let fstar_rev = ref "<unknown>"
5251
let fstar_options = ref []
@@ -202,63 +201,50 @@ let detect_karamel_if () =
202201
let expand_prefixes s =
203202
if KString.starts_with s "FSTAR_LIB" then
204203
!fstar_lib ^^ KString.chop s "FSTAR_LIB"
205-
else if KString.starts_with s "FSTAR_HOME" then
206-
!fstar_home ^^ KString.chop s "FSTAR_HOME"
207204
else
208205
s
209206

210-
(* Fills in fstar{,_home,_options} *)
207+
(* Fills in fstar{,_lib,_options}. Does NOT read any environment variables. *)
211208
let detect_fstar () =
212209
detect_karamel_if ();
213210

214211
if not !Options.silent then
215212
KPrint.bprintf "%s⚙ KaRaMeL will drive F*.%s Here's what we found:\n" Ansi.blue Ansi.reset;
216213

217-
begin try
218-
let r = Sys.getenv "FSTAR_HOME" in
219-
if not !Options.silent then
220-
KPrint.bprintf "read FSTAR_HOME via the environment\n";
221-
fstar_home := r;
222-
fstar := r ^^ "bin" ^^ "fstar.exe"
223-
with Not_found -> try
224-
fstar := read_one_line "which" [| "fstar.exe" |];
225-
fstar_home := d (d !fstar);
226-
if not !Options.silent then
227-
KPrint.bprintf "FSTAR_HOME is %s (via fstar.exe in PATH)\n" !fstar_home
228-
with _ ->
229-
fatal_error "Did not find fstar.exe in PATH and FSTAR_HOME is not set"
214+
(* Try to resolve fstar to an absolute path. This is just so the
215+
full path appears in logs. *)
216+
if not (KString.starts_with !fstar "/") then begin
217+
try fstar := read_one_line "which" [| !fstar |]
218+
with _ -> ()
230219
end;
231220

232-
let fstar_ulib = !fstar_home ^^ "ulib" in
233-
if not (Sys.file_exists fstar_ulib && Sys.is_directory fstar_ulib) ; then begin
234-
if not !Options.silent then
235-
KPrint.bprintf "F* library not found in ulib; falling back to lib/fstar\n";
236-
fstar_lib := !fstar_home ^^ "lib" ^^ "fstar"
237-
end else begin
238-
fstar_lib := fstar_ulib
221+
if not !Options.silent then
222+
KPrint.bprintf "Using fstar.exe = %s\n" !fstar;
223+
224+
(* Ask F* for the location of its library *)
225+
begin try fstar_lib := read_one_line !fstar [| "--locate_lib" |]
226+
with | _ ->
227+
fatal_error "Could not locate F* library: %s --locate_lib failed" !fstar
239228
end;
229+
if not !Options.silent then
230+
KPrint.bprintf "F* library root: %s\n" !fstar_lib;
240231

241232
if success "which" [| "cygpath" |] then begin
242233
fstar := read_one_line "cygpath" [| "-m"; !fstar |];
243234
if not !Options.silent then
244235
KPrint.bprintf "%sfstar converted to windows path:%s %s\n" Ansi.underline Ansi.reset !fstar;
245-
fstar_home := read_one_line "cygpath" [| "-m"; !fstar_home |];
246-
if not !Options.silent then
247-
KPrint.bprintf "%sfstar home converted to windows path:%s %s\n" Ansi.underline Ansi.reset !fstar_home;
248236
fstar_lib := read_one_line "cygpath" [| "-m"; !fstar_lib |];
249237
if not !Options.silent then
250238
KPrint.bprintf "%sfstar lib converted to windows path:%s %s\n" Ansi.underline Ansi.reset !fstar_lib
251239
end;
252240

253-
if try Sys.is_directory (!fstar_home ^^ ".git") with Sys_error _ -> false then begin
254-
let cwd = Sys.getcwd () in
255-
Sys.chdir !fstar_home;
256-
let branch = read_one_line "git" [| "rev-parse"; "--abbrev-ref"; "HEAD" |] in
257-
fstar_rev := String.sub (read_one_line "git" [| "rev-parse"; "HEAD" |]) 0 8;
258-
let color = if branch = "master" then Ansi.green else Ansi.orange in
241+
(* Record F* version, as output by the executable. *)
242+
begin try
243+
let lines = Process.read_stdout !fstar [| "--version" |] in
244+
fstar_rev := String.trim (String.concat " " lines);
259245
if not !Options.silent then
260-
KPrint.bprintf "fstar is on %sbranch %s%s\n" color branch Ansi.reset;
261-
Sys.chdir cwd
246+
KPrint.bprintf "%sfstar version:%s %s\n" Ansi.underline Ansi.reset !fstar_rev
247+
with | _ -> ()
262248
end;
263249

264250
let fstar_includes = List.map expand_prefixes !Options.includes in
@@ -268,7 +254,16 @@ let detect_fstar () =
268254
] @ List.flatten (List.rev_map (fun d -> ["--include"; d]) fstar_includes);
269255
(* This is a superset of the needed modules... some will be dropped very early
270256
* on in Karamel.ml *)
271-
fstar_options := (!fstar_lib ^^ "FStar.UInt128.fst") :: !fstar_options;
257+
258+
(* Locate and pass FStar.UInt128 *)
259+
let fstar_locate_file f =
260+
try read_one_line !fstar [| "--locate_file"; f |]
261+
with
262+
| _ ->
263+
Warn.fatal_error "Could not locate file %s, is F* properly installed?" f
264+
in
265+
fstar_options := fstar_locate_file "FStar.UInt128.fst" :: !fstar_options;
266+
272267
fstar_options := (!runtime_dir ^^ "WasmSupport.fst") :: !fstar_options;
273268
if not !Options.silent then
274269
KPrint.bprintf "%sfstar is:%s %s %s\n" Ansi.underline Ansi.reset !fstar (String.concat " " !fstar_options);

lib/Options.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let no_prefix: Bundle.pat list ref = ref Bundle.[
1616
Module [ "C"; "Compat"; "Endianness" ];
1717
Module [ "LowStar"; "Endianness" ]
1818
]
19+
let fstar = ref "fstar.exe" (* F* command to use *)
1920
(* krmllib.h now added directly in Output.ml so that it appears before the first
2021
* #ifdef *)
2122
let add_include: (include_ * string) list ref = ref [ ]

src/Karamel.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ The default arguments are: %s
136136
All include directories and paths supports special prefixes:
137137
- if a path starts with FSTAR_LIB, this will expand to wherever F*'s ulib
138138
directory is
139-
- if a path starts with FSTAR_HOME, this will expand to wherever the source
140-
checkout of F* is (this does not always exist, e.g. in the case of an OPAM
141-
setup)
142139

143140
The compiler switches turn on the following options.
144141
[-cc gcc] (default) adds [%s]
@@ -192,6 +189,8 @@ Supported options:|}
192189
in
193190
let spec = [
194191
(* KaRaMeL as a driver *)
192+
"-fstar", Arg.Set_string Options.fstar, " fstar.exe to use; defaults to \
193+
'fstar.exe'";
195194
"-cc", Arg.Set_string Options.cc, " compiler to use; one of gcc (default), \
196195
compcert, g++, clang, msvc";
197196
"-m32", Arg.Set Options.m32, " turn on 32-bit cross-compiling";

0 commit comments

Comments
 (0)