summaryrefslogtreecommitdiff
path: root/maint/reproducible_build
blob: 75e31122716b24261d5e72c72ed81bc41922de35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
#
# This script is run inside a docker container as part of our
# reproducible build process.
#
set -xeuo pipefail
if [ ! -f /.dockerenv ]; then
    echo Not running inside Docker, build will probably not be reproducible
    echo Use docker_reproducible_build instead to get the right environment
fi
if [ $# -eq 0 ]; then
	echo usage : "$0" '<linux|windows|macos|android...>'
	exit 1
fi

: "${CARGO:=cargo}" # quotes are just to placate shellcheck

linux=""
windows=""
macos=""
android=""
while [ "$#" -ne 0 ]; do
	case "$1" in
	linux)   linux=1;;
	windows) windows=1;;
	macos)   macos=1;;
	android) android=1;;
	*)
		echo "unknown target : $1" >&2
		exit 1;;
	esac
	shift
done

here=$(pwd)

## fix the target architecture to get reproducible builds
## the architecture was chosen as old enough that it should cover most usage
## while still supporting useful features like AES-NI. Older architectures
## won't be able to execute the resulting binary.
if [ -z $android ]; then
  export CFLAGS="-march=westmere"
  export RUSTFLAGS="-C target-cpu=westmere"
fi
export SOURCE_DATE_EPOCH="0"

## force build to run in a fixed location. Necessary because the build path
## is somehow captured when compiling.
cp -a "$here" /arti
cd /arti

cargo_build () {
	$CARGO build --locked "$@"
}
cargo_build_arti () {
	cargo_build -p arti --release --features full,static "$@"
}

echo "android: '$android'"
if [ -z $android ]; then
  ## add missing dependencies
  apk add perl make git musl-dev
fi

if [ -n "$linux" ]; then
	## no additional dependencies specifically for Linux

	## Build targeting x86_64-unknown-linux-musl to get a static binary
	## feature "static" enable compiling some C dependencies instead of linking
	## to system libraries. It is required to get a well behaving result.
	cargo_build_arti --target x86_64-unknown-linux-musl
	mv /arti/target/x86_64-unknown-linux-musl/release/arti "$here"/arti-linux
fi
if [ -n "$android" ]; then
  cargo ndk --target aarch64-linux-android build --locked -p arti --release --features full,static
  mv target/aarch64-linux-android/release/arti "$here"/arti-android-aarch64
  "$ANDROID_NDK_ROOT"/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip "$here"/arti-android-aarch64
  cargo ndk --target armv7-linux-androideabi build --locked -p arti --release --features full,static
  mv target/armv7-linux-androideabi/release/arti "$here"/arti-android-armv7
  "$ANDROID_NDK_ROOT"/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip "$here"/arti-android-armv7
fi
if [ -n "$windows" ]; then
	apk add mingw-w64-gcc
	rustup target add x86_64-pc-windows-gnu

	## Same tweaks as for Linux, plus don't insert compilation timestamp into PE headers
	RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,--no-insert-timestamp" \
		cargo_build_arti --target x86_64-pc-windows-gnu
	mv /arti/target/x86_64-pc-windows-gnu/release/arti.exe "$here"/arti-windows.exe
fi
if [ -n "$macos" ]; then
	apk add bash cmake patch clang libc-dev libxml2-dev openssl-dev musl-fts-dev build-base python3 bsd-compat-headers xz
	rustup target add x86_64-apple-darwin

	mkdir -p .cargo
	# (note: "ar" seems to be unused here. We could probably remove it?)
	cat > .cargo/config << EOF
[target.x86_64-apple-darwin]
linker = "x86_64-apple-darwin16-clang++-stdc++"
ar = "x86_64-apple-darwin16-ar"
EOF
	OSX_SDK_URL=https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX10.12.sdk.tar.xz
	OSX_SDK_VERSION=10.12
	OSX_SDK_SHA256=b314704d85934481c9927a0450db1768baf9af9efe649562fcb1a503bb44512f
	OSX_SDK="MacOSX${OSX_SDK_VERSION}.sdk.tar.xz"

	## don't compile clang if it's already here (CI cache?)
	if [ ! -x "/arti/osxcross/target/bin/o64-clang" ]; then
		git clone https://github.com/tpoechtrager/osxcross
		cd osxcross
		wget -nc "${OSX_SDK_URL}" -O tarballs/${OSX_SDK}
		echo "${OSX_SDK_SHA256}  tarballs/${OSX_SDK}" > ./sdk-checksum
		sha256sum -c ./sdk-checksum
		# Lingering mystery: If I change this 10.7
		# (and the occurrence of 10.7 below) to 10.12, the build fails with
		# "cannot find libc++ headers" again.
		UNATTENDED=yes OSX_VERSION_MIN=10.7 ./build.sh
		# copy it to gitlab build-dir so it may get cached
		cp -r /arti/osxcross "$here"
		cd ..
	fi

	PATH="/arti/osxcross/target/bin:$PATH" \
		MACOSX_DEPLOYMENT_TARGET="10.7" \
		CC=o64-clang \
		CXX=o64-clang++ \
		cargo_build_arti --target x86_64-apple-darwin
	mv /arti/target/x86_64-apple-darwin/release/arti "$here"/arti-macos
fi

git config --global --add safe.directory /arti

set +x
echo "branch       :" "$(git rev-parse --abbrev-ref HEAD)"
echo "commit       :" "$(git rev-parse HEAD)"
[ -z "$linux" ]   || echo "Linux hash   :" "$(sha256sum "$here"/arti-linux       | cut -d " " -f 1)"
[ -z "$android" ]   || echo -e "Android hash   :" "\n$(sha256sum "$here"/arti-android* | cut -d " " -f 1)"
[ -z "$windows" ] || echo "Windows hash :" "$(sha256sum "$here"/arti-windows.exe | cut -d " " -f 1)"
[ -z "$macos" ]   || echo "MacOS hash   :" "$(sha256sum "$here"/arti-macos       | cut -d " " -f 1)"