Skip to content

Commit 7b470ac

Browse files
committed
Move Candle specific files to a new subdirectory
As explained in a comment in build-instructions.sh, this leads to a bit of changing directories at the beginning. The following mental model may be useful: all (relative) paths should be relative to the project root. The main wrinkle here is that the cake binary currently expects some files at hardcoded locations - so if we don't want those files to be at the top-level directory, we need to start Candle in a different location, and change directory right after booting. To enable this, we also make use of the new "custom" FFI port - this will become handy in the future for getting the time and calling external binaries.
1 parent 64543e9 commit 7b470ac

20 files changed

Lines changed: 120 additions & 187 deletions

basics.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
(* (c) Copyright, Andrea Gabrielli, Marco Maggesi 2017-2018 *)
99
(* ========================================================================= *)
1010

11-
needs "candle_kernel.ml";;
11+
needs "candle/kernel.ml";;
1212

1313
(* ------------------------------------------------------------------------- *)
1414
(* Create probably-fresh variable *)

build-instructions.sh

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
#!/bin/bash
2+
# Set up "strict mode" for bash
23
set -euo pipefail
34

5+
# Create (if needed) + Change into the build directory
6+
mkdir -p candle/build
7+
cd candle/build
8+
49
# Get the 64-bit CakeML compiler from here:
5-
curl -OL https://cakeml.org/regression/artefacts/3276/cake-x64-64.tar.gz
6-
tar xvzf cake-x64-64.tar.gz
10+
curl -OL https://cakeml.org/regression/artefacts/3286/cake-x64-64.tar.gz
11+
tar xvzf cake-x64-64.tar.gz --strip-components=1
712

813
# By default, the CakeML compiler reserves a few kilobytes for constants and
914
# code produced by the dynamic compiler. Using Candle requires setting these
1015
# to some megabytes:
11-
patch cake-x64-64/cake.S cake.S.patch
16+
patch cake.S ../cake.S.patch
1217

13-
# Build the compiler binary
14-
cd cake-x64-64 && make && cd ..
18+
# Patching in useful FFI calls
19+
patch basis_ffi.c ../basis_ffi.c.patch
1520

16-
# Copy the compiler binary, the exported compiler state and candle_boot.ml into
17-
# this directory:
18-
cp cake-x64-64/cake cake-x64-64/config_enc_str.txt cake-x64-64/candle_boot.ml .
21+
# Build the compiler binary
22+
make
1923

2024
# Create the types.txt file necessary for candle_insulate.py
2125
./cake --types < /dev/null > types.txt 2>&1
2226

2327
# Generate candle_insulate.ml
24-
python candle_insulate.py types.txt candle_insulate.ml
28+
python ../insulate.py types.txt insulate.ml
29+
30+
# The working directory of the binary will be CANDLE_ROOT/candle/build,
31+
# so it needs to change directory to CANDLE_ROOT after booting..
32+
#
33+
# Q: Why do we not start the cake binary from CANDLE_ROOT?
34+
# A: The cake binary looks for config_enc_str.txt and candle_boot.ml relative
35+
# to the current working directoy. I find it neater if these files are not
36+
# copied to CANDLE_ROOT, but stay tucked away in the build folder.
37+
#
38+
cat ../chdir_to_root.ml >> candle_boot.ml
2539

2640
# You can now run Candle by writing:
27-
# $ ./cake --candle
28-
# or:
29-
# $ ./candle
41+
# $ ./candle.sh
3042
# (without the $) at your prompt. Load the HOL Light sources by writing:
3143
# > #use "hol.ml";;
3244
# (without > and with double semicolons) in the REPL.

candle

Lines changed: 0 additions & 2 deletions
This file was deleted.

candle.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
# Set up "strict mode" for bash
3+
set -euo pipefail
4+
5+
# Change into correct directory - note that Candle must take care to change back
6+
# to script_dir (see build-instructions.sh for more information)
7+
script_dir=$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")
8+
cd $script_dir/candle/build
9+
10+
# Start Candle
11+
./cake --candle

candle/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

candle/basis_ffi.c.patch

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--- basis_ffi.c
2+
+++ basis_ffi.c
3+
@@ -294,6 +294,13 @@
4+
}
5+
6+
void fficustom (unsigned char *c, long clen, unsigned char *a, long alen) {
7+
- assert(0 <= alen);
8+
- assert(0 <= clen);
9+
+ if (strcmp(c, "chdir") == 0) {
10+
+ assert(1 <= alen);
11+
+ a[0] = chdir(a) != 0;
12+
+ } else if (strcmp(c, "getcwd") == 0) {
13+
+ assert(1 <= alen);
14+
+ a[0] = getcwd(a + 1, alen - 1) == NULL;
15+
+ } else {
16+
+ return;
17+
+ }
18+
}
19+
20+
// ---------------------------------------------------------------------------
File renamed without changes.

candle/chdir_to_root.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
exception Sys_error of string;;
3+
let pp_exn e =
4+
match e with
5+
| Sys_error s ->
6+
Pretty_printer.app_block "Sys_error" [Pretty_printer.pp_string s]
7+
| _ -> pp_exn e;;
8+
9+
let chdir dir =
10+
let len = String.size dir in
11+
let _ = if len = 0 then raise (Sys_error "Sys.chdir: empty input") else () in
12+
let bytes = Word8_array.array (len + 1) (Word8.fromInt 0) in
13+
let _ = Word8_array.copyVec dir 0 len bytes 0 in
14+
let _ = Runtime.customFFI "chdir" bytes in
15+
let ret = Word8.toInt (Word8_array.sub bytes 0) in
16+
if ret <> 0 then raise (Sys_error "Sys.chdir: unsuccessful FFI") else ();;
17+
18+
let _ = chdir "../..";;
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)