|
| 1 | +# This is a generically useful Makefile for F* that is self-contained |
| 2 | +# |
| 3 | +# It is tempting to factor this out into multiple Makefiles but that |
| 4 | +# makes it less portable, so resist temptation, or move to a more |
| 5 | +# sophisticated build system. |
| 6 | +# |
| 7 | +# We expect: |
| 8 | +# 1. `fstar.exe` to be in PATH (alternatively, you can also set |
| 9 | +# $FSTAR_HOME to be set to your F* repo/install directory) |
| 10 | +# |
| 11 | +# 2. `cargo`, `rustup`, `hax` and `jq` to be installed and in PATH. |
| 12 | +# |
| 13 | +# 3. the extracted Cargo crate to have "hax-lib" as a dependency: |
| 14 | +# `hax-lib = { version = "0.1.0-pre.1", git = "https://github.com/hacspec/hax"}` |
| 15 | +# |
| 16 | +# Optionally, you can set `HACL_HOME`. |
| 17 | +# |
| 18 | +# ROOTS contains all the top-level F* files you wish to verify |
| 19 | +# The default target `verify` verified ROOTS and its dependencies |
| 20 | +# To lax-check instead, set `OTHERFLAGS="--lax"` on the command-line |
| 21 | +# |
| 22 | +# To make F* emacs mode use the settings in this file, you need to |
| 23 | +# add the following lines to your .emacs |
| 24 | +# |
| 25 | +# (setq-default fstar-executable "<YOUR_FSTAR_HOME>/bin/fstar.exe") |
| 26 | +# (setq-default fstar-smt-executable "<YOUR_Z3_HOME>/bin/z3") |
| 27 | +# |
| 28 | +# (defun my-fstar-compute-prover-args-using-make () |
| 29 | +# "Construct arguments to pass to F* by calling make." |
| 30 | +# (with-demoted-errors "Error when constructing arg string: %S" |
| 31 | +# (let* ((fname (file-name-nondirectory buffer-file-name)) |
| 32 | +# (target (concat fname "-in")) |
| 33 | +# (argstr (car (process-lines "make" "--quiet" target)))) |
| 34 | +# (split-string argstr)))) |
| 35 | +# (setq fstar-subp-prover-args #'my-fstar-compute-prover-args-using-make) |
| 36 | +# |
| 37 | + |
| 38 | +HACL_HOME ?= $(HOME)/.hax/hacl_home |
| 39 | +FSTAR_BIN ?= $(shell command -v fstar.exe 1>&2 2> /dev/null && echo "fstar.exe" || echo "$(FSTAR_HOME)/bin/fstar.exe") |
| 40 | + |
| 41 | +CACHE_DIR ?= .cache |
| 42 | +HINT_DIR ?= .hints |
| 43 | + |
| 44 | +SHELL ?= /usr/bin/env bash |
| 45 | + |
| 46 | +EXECUTABLES = cargo cargo-hax jq |
| 47 | +K := $(foreach bin,$(EXECUTABLES),\ |
| 48 | + $(if $(shell command -v $(bin) 2> /dev/null),,$(error "No $(bin) in PATH"))) |
| 49 | + |
| 50 | +.PHONY: all verify clean |
| 51 | + |
| 52 | +all: |
| 53 | + rm -f .depend && $(MAKE) .depend |
| 54 | + $(MAKE) verify |
| 55 | + |
| 56 | +HAX_CLI = "cargo hax into fstar --z3rlimit 100" |
| 57 | + |
| 58 | +# If $HACL_HOME doesn't exist, clone it |
| 59 | +${HACL_HOME}: |
| 60 | + mkdir -p "${HACL_HOME}" |
| 61 | + git clone --depth 1 https://github.com/hacl-star/hacl-star.git "${HACL_HOME}" |
| 62 | + |
| 63 | +# If no any F* file is detected, we run hax |
| 64 | +ifeq "$(wildcard *.fst *fsti)" "" |
| 65 | +$(shell $(SHELL) -c $(HAX_CLI)) |
| 66 | +endif |
| 67 | + |
| 68 | +# By default, we process all the files in the current directory |
| 69 | +ROOTS = $(wildcard *.fst *fsti) |
| 70 | + |
| 71 | +# Regenerate F* files via hax when Rust sources change |
| 72 | +$(ROOTS): $(shell find ../../../src -type f -name '*.rs') |
| 73 | + $(shell $(SHELL) -c $(HAX_CLI)) |
| 74 | + |
| 75 | +# The following is a bash script that discovers F* libraries |
| 76 | +define FINDLIBS |
| 77 | + # Prints a path if and only if it exists. Takes one argument: the |
| 78 | + # path. |
| 79 | + function print_if_exists() { |
| 80 | + if [ -d "$$1" ]; then |
| 81 | + echo "$$1" |
| 82 | + fi |
| 83 | + } |
| 84 | + # Asks Cargo all the dependencies for the current crate or workspace, |
| 85 | + # and extract all "root" directories for each. Takes zero argument. |
| 86 | + function dependencies() { |
| 87 | + cargo metadata --format-version 1 | |
| 88 | + jq -r '.packages | .[] | .manifest_path | split("/") | .[:-1] | join("/")' |
| 89 | + } |
| 90 | + # Find hax libraries *around* a given path. Takes one argument: the |
| 91 | + # path. |
| 92 | + function find_hax_libraries_at_path() { |
| 93 | + path="$$1" |
| 94 | + # if there is a `proofs/fstar/extraction` subfolder, then that's a |
| 95 | + # F* library |
| 96 | + print_if_exists "$$path/proofs/fstar/extraction" |
| 97 | + # Maybe the `proof-libs` folder of hax is around? |
| 98 | + MAYBE_PROOF_LIBS=$$(realpath -q "$$path/../proof-libs/fstar") |
| 99 | + if [ $$? -eq 0 ]; then |
| 100 | + print_if_exists "$$MAYBE_PROOF_LIBS/core" |
| 101 | + print_if_exists "$$MAYBE_PROOF_LIBS/rust_primitives" |
| 102 | + fi |
| 103 | + } |
| 104 | + { while IFS= read path; do |
| 105 | + find_hax_libraries_at_path "$$path" |
| 106 | + done < <(dependencies) |
| 107 | + } | sort -u |
| 108 | +endef |
| 109 | +export FINDLIBS |
| 110 | + |
| 111 | +FSTAR_INCLUDE_DIRS = $(HACL_HOME)/lib $(shell bash -c "$$FINDLIBS") |
| 112 | + |
| 113 | +FSTAR_FLAGS = --cmi \ |
| 114 | + --warn_error -331 \ |
| 115 | + --cache_checked_modules --cache_dir $(CACHE_DIR) \ |
| 116 | + --already_cached "+Prims+FStar+LowStar+C+Spec.Loops+TestLib" \ |
| 117 | + $(addprefix --include ,$(FSTAR_INCLUDE_DIRS)) |
| 118 | + |
| 119 | +FSTAR = $(FSTAR_BIN) $(FSTAR_FLAGS) |
| 120 | + |
| 121 | +.depend: $(HINT_DIR) $(CACHE_DIR) $(ROOTS) |
| 122 | + $(info $(ROOTS)) |
| 123 | + $(FSTAR) --cmi --dep full $(ROOTS) --extract '* -Prims -LowStar -FStar' > $@ |
| 124 | + |
| 125 | +include .depend |
| 126 | + |
| 127 | +$(HINT_DIR): |
| 128 | + mkdir -p $@ |
| 129 | + |
| 130 | +$(CACHE_DIR): |
| 131 | + mkdir -p $@ |
| 132 | + |
| 133 | +$(CACHE_DIR)/%.checked: | .depend $(HINT_DIR) $(CACHE_DIR) |
| 134 | + $(FSTAR) $(OTHERFLAGS) $< $(ENABLE_HINTS) --hint_file $(HINT_DIR)/$(notdir $*).hints |
| 135 | + |
| 136 | +verify: $(addsuffix .checked, $(addprefix $(CACHE_DIR)/,$(ROOTS))) |
| 137 | + |
| 138 | +# Targets for interactive mode |
| 139 | + |
| 140 | +%.fst-in: |
| 141 | + $(info $(FSTAR_FLAGS) \ |
| 142 | + $(ENABLE_HINTS) --hint_file $(HINT_DIR)/$(basename $@).fst.hints) |
| 143 | + |
| 144 | +%.fsti-in: |
| 145 | + $(info $(FSTAR_FLAGS) \ |
| 146 | + $(ENABLE_HINTS) --hint_file $(HINT_DIR)/$(basename $@).fsti.hints) |
| 147 | + |
| 148 | + |
| 149 | +# Clean targets |
| 150 | + |
| 151 | +clean: |
| 152 | + rm -rf $(CACHE_DIR)/* |
| 153 | + rm *.fst |
0 commit comments