Skip to content

Commit 9110f78

Browse files
committed
Add a sample reproducible build solution using docker
1 parent 1394be8 commit 9110f78

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@ make test
171171

172172
The templates provided here, use the same conventions as `ckb-native-build-sample` project, so feel free to refer to the more detailed [usage](https://github.com/xxuejie/ckb-native-build-sample?tab=readme-ov-file#usage) doc in the sample project.
173173

174+
### Reproducible Build
175+
176+
When using this set of templates, we always recommend to use locally installed native versions of LLVM & Rust to build and test your scripts. However, reproducible build is an important part of CKB scripts, which would require locked versions of LLVM & Rust to work, which might not be an easy task when using locally installed versions of compilers.
177+
178+
For the time being, we have prepared a script that does reproducible build via [a docker container image](https://github.com/cryptape/llvm-n-rust). We do want to mention that docker is not necessarily THE way to do reproducible build, nor is it the best way to do reproducible build. There might well be other ways that are better, such as chroot or Nix. It's just that historically, docker has been used in CKB script's build process, and adding a script leveraging docker here, provides an easy solution into the issue.
179+
180+
To do reproducible build, you can use the included script with varying commands:
181+
182+
```
183+
$ ./scripts/reproducible_build_docker # Clean current repository, used locked LLVM & Rust from a docker container
184+
# to build all contracts, then test the binaries against a checksum file.
185+
186+
$ ./scripts/reproducible_build_docker --update # Update the checksum file with new binaries, could be handy when you have
187+
# made changes to the binaries.
188+
189+
$ ./scripts/reproducible_build_docker --no-clean # Do not clean intermediate files before building, it is not recommended to
190+
# use this but when you really know what you are doing, it could help you save
191+
# some time.
192+
193+
$ ./scripts/reproducible_build_docker --proxy "..." # Setup docker container so it pulls Rust crates using a proxy server
194+
```
195+
196+
By default, the checksum file is stored in `checksums.txt` in the root of the repository. It is strongly recommended that this file is checked into version control, and a CI is setup so reproducible build is always checked in new commits.
197+
174198
### Standalone Contract Crate
175199

176200
In rare cases if you want to simply use a standalone contract crate without a workspace. The [standalone-contract](https://github.com/cryptape/ckb-script-templates/tree/main/standalone-contract) template is prepared for you:
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
#
3+
# An utility script helping with reproducible script builds via docker.
4+
# Note that this utility serves only as one example, docker is not
5+
# necessarily THE way to do reproducible build, nor is it the best way
6+
# to do reproducible build.
7+
set -ex
8+
9+
DOCKER_IMAGE="docker.io/cryptape/llvm-n-rust:20240630"
10+
CHECKSUM_FILE_PATH="checksums.txt"
11+
12+
# We are parsing command line arguments based on tips from:
13+
# https://stackoverflow.com/a/14203146
14+
15+
while [[ $# -gt 0 ]]; do
16+
case $1 in
17+
-p|--proxy)
18+
PROXY="$2"
19+
shift # past argument
20+
shift # past value
21+
;;
22+
-u|--update)
23+
UPDATE="yes"
24+
shift # past argument
25+
;;
26+
--no-clean)
27+
NOCLEAN="yes"
28+
shift # past argument
29+
;;
30+
-*|--*)
31+
echo "Unknown option $1"
32+
exit 1
33+
;;
34+
*)
35+
echo "Unknown argument $1"
36+
exit 1
37+
;;
38+
esac
39+
done
40+
41+
if [[ -n "${PROXY}" ]]; then
42+
PROXY_ARGS="-e ALL_PROXY=${PROXY} -e HTTPS_PROXY=${PROXY} -e HTTP_PROXY=${PROXY}"
43+
fi
44+
45+
TASKS=""
46+
if [[ "${NOCLEAN}" != "yes" ]]; then
47+
TASKS+=" clean "
48+
fi
49+
50+
if [[ "${UPDATE}" = "yes" ]]; then
51+
TASKS+=" checksum CHECKSUM_FILE=${CHECKSUM_FILE_PATH} "
52+
else
53+
TASKS+=" build "
54+
fi
55+
56+
docker run --rm $PROXY_ARGS -v `pwd`:/code $DOCKER_IMAGE make $TASKS
57+
58+
if [[ "${UPDATE}" = "yes" ]]; then
59+
echo "${CHECKSUM_FILE_PATH} file is updated with latest binary hashes!"
60+
else
61+
sha256sum -c ${CHECKSUM_FILE_PATH}
62+
fi
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
#
3+
# An utility script helping with reproducible script builds via docker.
4+
# Note that this utility serves only as one example, docker is not
5+
# necessarily THE way to do reproducible build, nor is it the best way
6+
# to do reproducible build.
7+
set -ex
8+
9+
DOCKER_IMAGE="docker.io/cryptape/llvm-n-rust:20240630"
10+
CHECKSUM_FILE_PATH="checksums.txt"
11+
12+
# We are parsing command line arguments based on tips from:
13+
# https://stackoverflow.com/a/14203146
14+
15+
while [[ $# -gt 0 ]]; do
16+
case $1 in
17+
-p|--proxy)
18+
PROXY="$2"
19+
shift # past argument
20+
shift # past value
21+
;;
22+
-u|--update)
23+
UPDATE="yes"
24+
shift # past argument
25+
;;
26+
--no-clean)
27+
NOCLEAN="yes"
28+
shift # past argument
29+
;;
30+
-*|--*)
31+
echo "Unknown option $1"
32+
exit 1
33+
;;
34+
*)
35+
echo "Unknown argument $1"
36+
exit 1
37+
;;
38+
esac
39+
done
40+
41+
if [[ -n "${PROXY}" ]]; then
42+
PROXY_ARGS="-e ALL_PROXY=${PROXY} -e HTTPS_PROXY=${PROXY} -e HTTP_PROXY=${PROXY}"
43+
fi
44+
45+
TASKS=""
46+
if [[ "${NOCLEAN}" != "yes" ]]; then
47+
TASKS+=" clean "
48+
fi
49+
50+
if [[ "${UPDATE}" = "yes" ]]; then
51+
TASKS+=" checksum CHECKSUM_FILE=${CHECKSUM_FILE_PATH} "
52+
else
53+
TASKS+=" build "
54+
fi
55+
56+
docker run --rm $PROXY_ARGS -v `pwd`:/code $DOCKER_IMAGE make $TASKS
57+
58+
if [[ "${UPDATE}" = "yes" ]]; then
59+
echo "${CHECKSUM_FILE_PATH} file is updated with latest binary hashes!"
60+
else
61+
sha256sum -c ${CHECKSUM_FILE_PATH}
62+
fi

0 commit comments

Comments
 (0)