1- #! /bin/bash
1+ #! /usr/ bin/env bash
22set -e
33
4- # Requires (nix-shell with) cabal
4+ # Requires (nix-shell with) git, rsync
55
6- if [ -z " $1 " ]; then
7- echo " \$ 1 path to libsimplicity repository root"
6+ # # 0. Parse command-line options
7+ if [ -z " $1 " ] || [ -z " $2 " ]; then
8+ echo " Usage: $0 <path to depend directory> <path to libsimplicity repository root>"
89 exit 1
910fi
1011
11- C_DIR= " $1 "
12- RUST_DIR= " $( pwd ) "
13- VENDORED_HEAD= $( sed -n ' 2p ' " $RUST_DIR " /simplicity-sys/depend/simplicity-HEAD-revision.txt )
12+ DEPEND_PATH= $( readlink -f " $1 " )
13+ LIBSIM_PATH= $( readlink -f " $2 " )
14+ VENDORED_SIM_DIR= " $DEPEND_PATH /simplicity"
1415
15- cd " $C_DIR "
16- REV=$( git rev-parse HEAD)
16+ DEFAULT_VERSION_CODE=$( grep " ^version" " $DEPEND_PATH /../Cargo.toml" | sed ' s/\./_/g' | sed ' s/.*"\(.*\)".*/\1/' | cut -d_ -f1-2)
1717
18- if [ " $REV " != " $VENDORED_HEAD " ]; then
19- echo " WARNING: Haskell and Rust have different libsimplicity version"
20- echo " Haskell: $REV "
21- echo " Rust: $VENDORED_HEAD "
18+ : " ${SIMPLICITY_ALLOC_VERSION_CODE:= $DEFAULT_VERSION_CODE } "
19+
20+ # # 1. Sanity check environment.
21+ if ! command -v git > /dev/null; then
22+ echo " Missing 'git' executable in evironment."
23+ exit 1
24+ fi
25+ if ! command -v rsync > /dev/null; then
26+ echo " Missing 'rsync' executable in evironment."
27+ exit 1
28+ fi
29+
30+ if [ ! -d " $DEPEND_PATH " ]; then
31+ echo " Depend path '$DEPEND_PATH ' does not appear to be a directory."
32+ exit 1
33+ fi
34+
35+ if [ ! -d " $LIBSIM_PATH " ]; then
36+ echo " libsimplicity path '$LIBSIM_PATH ' does not appear to be a directory."
37+ exit 1
2238fi
2339
40+ if [ -d " $VENDORED_SIM_DIR " ]; then
41+ while true ; do
42+ read -r -p " $VENDORED_SIM_DIR will be deleted [yn]: " yn
43+ case $yn in
44+ [Yy]* ) break ;;
45+ [Nn]* ) exit ;;
46+ * ) echo " Please answer y or n." ;;
47+ esac
48+ done
49+
50+ rm -rf " $VENDORED_SIM_DIR "
51+ elif [ -e " $VENDORED_SIM_DIR " ]; then
52+ echo " 'simplicity' inside depend directory exists but appears not to be a directory."
53+ echo " Please move or delete this file."
54+ exit 1
55+ fi
56+
57+ # # 2. Copy files from libsimplicity
58+ pushd " $LIBSIM_PATH /C"
59+
2460if test -n " $( git status --porcelain) " ; then
2561 echo " WARNING: libsimplicity repo is not clean"
2662fi
2763
28- cabal build -j8
29- cabal exec GenRustJets
64+ HEAD=$( git rev-parse HEAD)
65+ echo " # This file has been automatically generated." > " $DEPEND_PATH /simplicity-HEAD-revision.txt"
66+ echo " $HEAD " >> " $DEPEND_PATH /simplicity-HEAD-revision.txt"
3067
31- cd " $RUST_DIR "
68+ # Copy C folder to simplicity-sys/depend/simplicity
69+ # Use rsync to copy only files tracked by git
70+ git ls-files | rsync -av --files-from=- . " $VENDORED_SIM_DIR "
3271
33- DEFAULT_VERSION_CODE=$( grep " ^version" " ./simplicity-sys/Cargo.toml" | sed ' s/\./_/g' | sed ' s/.*"\(.*\)".*/\1/' | cut -d_ -f1-2)
34- : " ${SIMPLICITY_ALLOC_VERSION_CODE:= $DEFAULT_VERSION_CODE } "
72+ popd
73+
74+ # # 3. Patch things to include versions
3575
36- mv " ${C_DIR} /jets_ffi.rs" " ./simplicity-sys/src/c_jets/jets_ffi.rs"
37- mv " ${C_DIR} /jets_wrapper.rs" " ./simplicity-sys/src/c_jets/jets_wrapper.rs"
38- mv " ${C_DIR} /jets_wrapper.c" " ./simplicity-sys/depend/jets_wrapper.c"
76+ # a. patch our own drop/new functions for C structures.
77+ find " $DEPEND_PATH /.." -name " *.rs" -type f -exec sed -i " s/rust_[0-9_]*_free/rust_${SIMPLICITY_ALLOC_VERSION_CODE} _free/" {} \;
78+ find " $DEPEND_PATH /.." -name " *.rs" -type f -exec sed -i " s/rust_[0-9_]*_malloc/rust_${SIMPLICITY_ALLOC_VERSION_CODE} _malloc/" {} \;
79+ find " $DEPEND_PATH /.." -name " *.rs" -type f -exec sed -i " s/rust_[0-9_]*_calloc/rust_${SIMPLICITY_ALLOC_VERSION_CODE} _calloc/" {} \;
3980
40- # Tweak the c_ prefixes in the wrappers
41- sed -i -r " s/\" c_/\" rustsimplicity_${SIMPLICITY_ALLOC_VERSION_CODE} _c_/" \
42- " ./simplicity-sys/src/c_jets/jets_ffi.rs"
81+ # b. patch the rust_{malloc,calloc,free} functions in simplicity_alloc.h
82+ sed " s/rust_/rust_${SIMPLICITY_ALLOC_VERSION_CODE} _/" \
83+ < " $DEPEND_PATH /simplicity_alloc.h.patch" \
84+ | patch " $VENDORED_SIM_DIR /simplicity_alloc.h"
4385
44- # Update version prefix in wrapper.h if the version changed
45- sed -i -r " s/ rustsimplicity_[0-9]+_[0-9]+_/ rustsimplicity_${SIMPLICITY_ALLOC_VERSION_CODE} _/" \
46- " ./simplicity-sys/depend/wrapper.h"
86+ # c. patch every single simplicity_* symbol in the library (every instance except
87+ # those in #includes, which is overkill but doesn't hurt anything)
88+ find " $DEPEND_PATH /simplicity" \( -name " *.[ch]" -o -name ' *.inc' \) -type f -print0 | xargs -0 \
89+ sed -i " /^#include/! s/simplicity_/rustsimplicity_${SIMPLICITY_ALLOC_VERSION_CODE} _/g"
90+ # ...ok, actually we didn't want to replace simplicity_err
91+ find " $DEPEND_PATH /simplicity" \( -name " *.[ch]" -o -name ' *.inc' \) -type f -print0 | xargs -0 \
92+ sed -i " s/rustsimplicity_${SIMPLICITY_ALLOC_VERSION_CODE} _err/simplicity_err/g"
93+ # Special-case calls in depend/env.c and depend/warpper.h
94+ sed -i -r " s/rustsimplicity_[0-9]+_[0-9]+_/rustsimplicity_${SIMPLICITY_ALLOC_VERSION_CODE} _/" \
95+ " $DEPEND_PATH /env.c" \
96+ " $DEPEND_PATH /wrapper.h"
4797
48- mv " ${C_DIR} /core.rs" " ./src/jet/init/"
49- mv " ${C_DIR} /bitcoin.rs" " ./src/jet/init/"
50- mv " ${C_DIR} /elements.rs" " ./src/jet/init/"
98+ # d. ...also update the corresponding link_name= entries in the Rust source code
99+ find " ./src/" -name " *.rs" -type f -print0 | xargs -0 \
100+ sed -i -r " s/rustsimplicity_[0-9]+_[0-9]+_(.*)([\" \(])/rustsimplicity_${SIMPLICITY_ALLOC_VERSION_CODE} _\1\2/g"
101+ # e. ...and the links= field in the manifest file
102+ sed -i -r " s/^links = \" .*\" $/links = \" rustsimplicity_${SIMPLICITY_ALLOC_VERSION_CODE} \" /" Cargo.toml
0 commit comments