xref: /freebsd/crypto/libecc/scripts/crossbuild.sh (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1#/*
2# *  Copyright (C) 2017 - This file is part of libecc project
3# *
4# *  Authors:
5# *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6# *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7# *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8# *
9# *  Contributors:
10# *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11# *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12# *
13# *  This software is licensed under a dual BSD and GPL v2 license.
14# *  See LICENSE file at the root folder of the project.
15# */
16#!/bin/sh
17
18# Check if a file $1 exists. Copy it in $2 if
19# it exists, or else log and error in $3.
20check_and_copy(){
21	if [ -e $1 ]
22	then
23		cp $1 $2
24	else
25		echo "$2 did not compile ..." >> $3
26	fi
27}
28
29copy_compiled_examples(){
30	ROOT_DIR=$1
31	CROSSBUILD_OUTPUT=$2
32	triplet=$3
33	wordsize=$4
34	ERROR_LOG_FILE=$5
35	suffix=$6
36	# Basic
37	check_and_copy $ROOT_DIR/src/examples/basic/nn_pollard_rho $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/nn_pollard_rho_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
38	check_and_copy $ROOT_DIR/src/examples/basic/fp_square_residue $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/fp_square_residue_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
39	check_and_copy $ROOT_DIR/src/examples/basic/curve_basic_examples $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/curve_basic_examples_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
40	check_and_copy $ROOT_DIR/src/examples/basic/curve_ecdh $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/curve_ecdh_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
41	# Hash
42	check_and_copy $ROOT_DIR/src/examples/"hash"/"hash" $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/hash_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
43	# SSS
44	check_and_copy $ROOT_DIR/src/examples/sss/sss $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/sss_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
45	# Signatures
46	check_and_copy $ROOT_DIR/src/examples/sig/rsa/rsa $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/rsa_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
47	check_and_copy $ROOT_DIR/src/examples/sig/dsa/dsa $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/dsa_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
48	check_and_copy $ROOT_DIR/src/examples/sig/sdsa/sdsa $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/sdsa_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
49	check_and_copy $ROOT_DIR/src/examples/sig/kcdsa/kcdsa $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/kcdsa_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
50	check_and_copy $ROOT_DIR/src/examples/sig/gostr34_10_94/gostr34_10_94 $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/gostr34_10_94_"$triplet"_word"$wordsize""$suffix" $ERROR_LOG_FILE
51}
52
53check_triplet_wordsize(){
54	triplet=$1
55	wordsize=$2
56	# Create a temporary workspace and copy the files to compile
57	ROOT_DIR=$CROSSBUILD_OUTPUT/tmp/ecc_root_"$triplet"_"$wordsize"
58	mkdir -p $ROOT_DIR
59	# Copy necessary source files from the root project
60	cp -r $SRC_DIR/src/ $ROOT_DIR/
61	cp -r $SRC_DIR/include/ $ROOT_DIR/
62	cp $SRC_DIR/common.mk $ROOT_DIR/
63	cp $SRC_DIR/Makefile $ROOT_DIR/
64	mkdir -p $ROOT_DIR/build
65	mkdir -p $CROSSBUILD_OUTPUT/compilation_log
66	mkdir -p $CROSSBUILD_OUTPUT/error_log
67	COMPILATION_LOG_FILE=$CROSSBUILD_OUTPUT/compilation_log/compilation_log_"$triplet"_"$wordsize"
68	ERROR_LOG_FILE=$CROSSBUILD_OUTPUT/error_log/error_log_"$triplet"_"$wordsize"
69	# NOTE: for 64 bit triplets, multiarch/crossbuild docker's gcc 4.9 has a bug handling loop unrolling in -O3 and
70	# is mistaken in detecting arrays overflows at compilation time
71	# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64277
72	# Also, add the "-Wno-pedantic-ms-format" for specific quikrs of mingw with "%lld" (...)
73	if [ "$triplet" = "x86_64-w64-mingw32" ] || [ "$triplet" = "aarch64-linux-gnu" ]; then
74		extra_lib_cflags="-O2"
75		extra_bin_cflags=""
76		if [ "$triplet" = "x86_64-w64-mingw32" ] && [ "$wordsize" = "64" ]; then
77			extra_lib_cflags=$extra_lib_cflags" -Wno-pedantic-ms-format"
78		fi
79	# There is also a misbehavior for mingw improperly finding unintialized variables
80	# Also, add the "-Wno-pedantic-ms-format" for specific quikrs of mingw with "%lld" (...)
81	elif [ "$triplet" = "i686-w64-mingw32" ]; then
82		extra_lib_cflags="-Wno-maybe-uninitialized"
83		extra_bin_cflags=""
84		if [ "$wordsize" = "64" ]; then
85			extra_lib_cflags=$extra_lib_cflags" -Wno-pedantic-ms-format"
86		fi
87        # NOTE: on darwin based clang, some of our options are too recent for the installed
88        # llvm ... Hence we remove warnings as errors here
89	elif [ "$triplet" = "i386-apple-darwin" ] || [ "$triplet" = "x86_64-apple-darwin" ] || [ "$triplet" = "x86_64h-apple-darwin" ]; then
90		extra_lib_cflags="-Wno-error"
91		extra_bin_cflags="-Wno-error"
92        else
93		extra_lib_cflags=""
94		extra_bin_cflags=""
95	fi
96	############## Release compilation
97	echo "======== COMPILING RELEASE FOR $triplet / $wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
98	# Library, self tests and utils
99	docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR -e EXTRA_LIB_CFLAGS="$extra_lib_cflags" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags" multiarch/crossbuild make "$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
100	mkdir -p $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"
101	check_and_copy $ROOT_DIR/build/ec_self_tests $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_self_tests_"$triplet"_word"$wordsize" $ERROR_LOG_FILE
102	check_and_copy $ROOT_DIR/build/ec_utils $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_utils_"$triplet"_word"$wordsize" $ERROR_LOG_FILE
103	# Examples
104	docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/ -w $ROOT_DIR/src/examples -e EXTRA_LIB_CFLAGS="$extra_lib_cflags" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags" multiarch/crossbuild make "$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
105	copy_compiled_examples "$ROOT_DIR" "$CROSSBUILD_OUTPUT" "$triplet" "$wordsize" "$ERROR_LOG_FILE" ""
106	# Clean
107	docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR multiarch/crossbuild make clean
108	docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/src/examples -w $ROOT_DIR/src/examples multiarch/crossbuild make clean
109	############## Debug compilation
110	echo "======== COMPILING DEBUG FOR $triplet / $wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
111	############## Release compilation
112	# Library, self tests and utils
113	docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR -e EXTRA_LIB_CFLAGS="$extra_lib_cflags" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags" multiarch/crossbuild make debug"$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
114	mkdir -p $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"
115	check_and_copy $ROOT_DIR/build/ec_self_tests $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_self_tests_"$triplet"_word"$wordsize"_debug $ERROR_LOG_FILE
116	check_and_copy $ROOT_DIR/build/ec_utils $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_utils_"$triplet"_word"$wordsize"_debug $ERROR_LOG_FILE
117	# Examples
118	docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/ -w $ROOT_DIR/src/examples -e EXTRA_LIB_CFLAGS="$extra_lib_cflags" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags" multiarch/crossbuild make debug"$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
119	copy_compiled_examples "$ROOT_DIR" "$CROSSBUILD_OUTPUT" "$triplet" "$wordsize" "$ERROR_LOG_FILE" "_debug"
120	# Clean
121	docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR multiarch/crossbuild make clean
122	docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/src/examples -w $ROOT_DIR/src/examples multiarch/crossbuild make clean
123	echo "===========================================" 2>&1 | tee -a $COMPILATION_LOG_FILE
124	# Compile static binaries for everyone except Mac OS (gcc on it does not support -static)
125	if [ "$triplet" != "i386-apple-darwin" ] && [ "$triplet" != "x86_64-apple-darwin" ] && [ "$triplet" != "x86_64h-apple-darwin" ]; then
126		############## Release compilation with static binaries (for emulation)
127		echo "======== COMPILING STATIC RELEASE FOR $triplet / $wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
128		# Library, self tests and utils
129		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR -e EXTRA_LIB_CFLAGS="$extra_lib_cflags" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags" -e BIN_LDFLAGS="-static" multiarch/crossbuild make "$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
130		mkdir -p $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"
131		check_and_copy $ROOT_DIR/build/ec_self_tests $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_self_tests_"$triplet"_word"$wordsize"_static $ERROR_LOG_FILE
132		check_and_copy $ROOT_DIR/build/ec_utils $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_utils_"$triplet"_word"$wordsize"_static $ERROR_LOG_FILE
133		# Examples
134		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/ -w $ROOT_DIR/src/examples -e EXTRA_LIB_CFLAGS="$extra_lib_cflags" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags" -e BIN_LDFLAGS="-static" multiarch/crossbuild make "$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
135		copy_compiled_examples "$ROOT_DIR" "$CROSSBUILD_OUTPUT" "$triplet" "$wordsize" "$ERROR_LOG_FILE" "_static"
136		# Clean
137		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR multiarch/crossbuild make clean
138		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/src/examples -w $ROOT_DIR/src/examples multiarch/crossbuild make clean
139		##### 4096 bits case for 64 bit word size only
140		###############################################
141		if [ "$wordsize" = "64" ]; then
142			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR multiarch/crossbuild make clean
143			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/src/examples -w $ROOT_DIR/src/examples multiarch/crossbuild make clean
144			# Self tests and utils
145			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR -e EXTRA_LIB_CFLAGS="$extra_lib_cflags -DUSER_NN_BIT_LEN=4096" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags -DUSER_NN_BIT_LEN=4096" -e BIN_LDFLAGS="-static" multiarch/crossbuild make "$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
146			mkdir -p $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"
147			check_and_copy $ROOT_DIR/build/ec_self_tests $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_self_tests_"$triplet"_word"$wordsize"_static_4096 $ERROR_LOG_FILE
148			check_and_copy $ROOT_DIR/build/ec_utils $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_utils_"$triplet"_word"$wordsize"_static_4096 $ERROR_LOG_FILE
149			# Examples
150			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/ -w $ROOT_DIR/src/examples -e EXTRA_LIB_CFLAGS="$extra_lib_cflags -DUSER_NN_BIT_LEN=4096" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags -DUSER_NN_BIT_LEN=4096" -e BIN_LDFLAGS="-static" multiarch/crossbuild make "$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
151			copy_compiled_examples "$ROOT_DIR" "$CROSSBUILD_OUTPUT" "$triplet" "$wordsize" "$ERROR_LOG_FILE" "_static_4096"
152			# Clean
153			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR multiarch/crossbuild make clean
154			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/src/examples -w $ROOT_DIR/src/examples multiarch/crossbuild make clean
155		fi
156		############## Debug compilation with static binaries (for emulation)
157		echo "======== COMPILING STATIC DEBUG FOR $triplet / $wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
158		############## Release compilation with static binaries (for emulation)
159		# Self tests and utils
160		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR -e EXTRA_LIB_CFLAGS="$extra_lib_cflags" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags" -e BIN_LDFLAGS="-static" multiarch/crossbuild make debug"$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
161		mkdir -p $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"
162		check_and_copy $ROOT_DIR/build/ec_self_tests $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_self_tests_"$triplet"_word"$wordsize"_debug_static $ERROR_LOG_FILE
163		check_and_copy $ROOT_DIR/build/ec_utils $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_utils_"$triplet"_word"$wordsize"_debug_static $ERROR_LOG_FILE
164		# Examples
165		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/ -w $ROOT_DIR/src/examples -e EXTRA_LIB_CFLAGS="$extra_lib_cflags" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags" -e BIN_LDFLAGS="-static" multiarch/crossbuild make debug"$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
166		copy_compiled_examples "$ROOT_DIR" "$CROSSBUILD_OUTPUT" "$triplet" "$wordsize" "$ERROR_LOG_FILE" "_debug_static"
167		# Clean
168		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR multiarch/crossbuild make clean
169		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/src/examples -w $ROOT_DIR/src/examples multiarch/crossbuild make clean
170		##### 4096 bits case for 64 bit word size only
171		###############################################
172		if [ "$wordsize" = "64" ]; then
173			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR multiarch/crossbuild make clean
174			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/src/examples -w $ROOT_DIR/src/examples multiarch/crossbuild make clean
175			# Self tests and utils
176			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR -e EXTRA_LIB_CFLAGS="$extra_lib_cflags -DUSER_NN_BIT_LEN=4096" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags -DUSER_NN_BIT_LEN=4096" -e BIN_LDFLAGS="-static" multiarch/crossbuild make debug"$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
177			mkdir -p $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"
178			check_and_copy $ROOT_DIR/build/ec_self_tests $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_self_tests_"$triplet"_word"$wordsize"_debug_static_4096 $ERROR_LOG_FILE
179			check_and_copy $ROOT_DIR/build/ec_utils $CROSSBUILD_OUTPUT/"$triplet"/word"$wordsize"/ec_utils_"$triplet"_word"$wordsize"_debug_static_4096 $ERROR_LOG_FILE
180			# Examples
181			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/ -w $ROOT_DIR/src/examples -e EXTRA_LIB_CFLAGS="$extra_lib_cflags -DUSER_NN_BIT_LEN=4096" -e EXTRA_BIN_CFLAGS="$extra_bin_cflags -DUSER_NN_BIT_LEN=4096" -e BIN_LDFLAGS="-static" multiarch/crossbuild make debug"$wordsize" 2>&1 | tee -a $COMPILATION_LOG_FILE
182			copy_compiled_examples "$ROOT_DIR" "$CROSSBUILD_OUTPUT" "$triplet" "$wordsize" "$ERROR_LOG_FILE" "_debug_static_4096"
183			# Clean
184			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR -w $ROOT_DIR multiarch/crossbuild make clean
185			docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -e CROSS_TRIPLE=$triplet -v $ROOT_DIR:$ROOT_DIR/src/examples -w $ROOT_DIR/src/examples multiarch/crossbuild make clean
186		fi
187		echo "===========================================" 2>&1 | tee -a $COMPILATION_LOG_FILE
188	fi
189	# Cleanup compilation stuff
190	rm -rf $ROOT_DIR
191}
192
193
194print_help(){
195	echo "$0 uses the docker multiarch/crossbuild image to compile libecc on multiple platforms"
196	echo "with multiple word sizes (16, 32 and 64). The compilation logs and errors as well as"
197	echo "the produced binaries are kept int the 'crossbuild_out' folder."
198	echo "Supported platform triplets are:"
199	echo "arm-linux-gnueabi / arm-linux-gnueabihf / powerpc64le-linux-gnu / aarch64-linux-gnu /"
200	echo "mipsel-linux-gnu / i386-apple-darwin / x86_64-apple-darwin / i686-w64-mingw32 / x86_64-w64-mingw32."
201	echo ""
202	echo "$0 with no argument will test the compilation for all the triplets and the word sizes."
203	echo "  -h: print this help"
204	echo "  -triplet: execute the crossbuild only for a given triplet:"
205	echo "      $ sh $0 -triplet arm-linux-gnueabi"
206	echo "      => This will execute cross-compilation for arm-linux-gnueabi for all the word sizes."
207	echo "      $ sh $0 -triplet arm-linux-gnueabi 64"
208	echo "      => This will execute cross-compilation for arm-linux-gnueabi only for 64-bit word size."
209	echo "  -cpu: will specify the number of tasks used for parallel compilation. The default behaviour is to"
210	echo "        use the maximum available CPUs on the machine, but one can reduce th enumber of parallel"
211	echo "        tasks with this toggle. Warning: this toggle is not compatible with the -triplet toggle."
212}
213
214
215######### Script main
216
217# Adapt our sources directory depending on the calling
218# directory
219SRC_DIR=`dirname "$(readlink -f "$0")"`/..
220CROSSBUILD_OUTPUT=$SRC_DIR/scripts/crossbuild_out/
221
222# Check for the docker command line
223CHECK_DOCKER=$(docker -v)
224if [ $? -ne 0 ]; then
225	echo "docker is not installed ... Please install it!"
226	exit
227fi
228
229# Check for docker image multiarch/crossbuild
230if [ -z $(docker images -q multiarch/crossbuild) ]
231then
232	echo "Please install the multiarch/crossbuild docker image:"
233	echo "$ docker pull multiarch/crossbuild"
234	exit
235fi
236
237# Print help if asked
238if [ "$1" = "-h" ]
239then
240	print_help $0
241	exit
242fi
243
244# If we have arguments, just execute subcommand
245if [ "$1" = "-triplet" ]
246then
247	# Clean stuff if this is an explicit call from command line
248	if ! [ "$4" = "-automate" ]
249	then
250		echo "Cleaning before running ..."
251		rm -rf $CROSSBUILD_OUTPUT/*
252		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -v $SRC_DIR:/ecc -w /ecc multiarch/crossbuild make clean
253		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -v $SRC_DIR:/ecc -w /ecc/src/examples multiarch/crossbuild make clean
254		docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -v $SRC_DIR:/ecc -w /ecc/src/arithmetic_tests multiarch/crossbuild make clean
255	fi
256	# If no specific word size has been given, do all the sizes
257	if [ "$3" = "" ]
258	then
259		for wordsize in 16 32 64;
260		do
261			check_triplet_wordsize $2 $wordsize
262		done
263	else
264		check_triplet_wordsize $2 $3
265	fi
266	exit
267fi
268
269# Clean
270echo "Cleaning before running ..."
271rm -rf $CROSSBUILD_OUTPUT/*
272docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -v $SRC_DIR:/ecc -w /ecc multiarch/crossbuild make clean
273docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -v $SRC_DIR:/ecc -w /ecc/src/examples multiarch/crossbuild make clean
274docker run -e VERBOSE=1 -e ASSERT_PRINT="1" -e COMPLETE="$COMPLETE" -e BLINDING="$BLINDING" -e LADDER="$LADDER" -e CRYPTOFUZZ="$CRYPTOFUZZ" --rm -v $SRC_DIR:/ecc -w /ecc/src/arithmetic_tests multiarch/crossbuild make clean
275
276ALL_CHECKS=""
277for wordsize in 16 32 64;
278do
279	for triplet in arm-linux-gnueabi arm-linux-gnueabihf powerpc64le-linux-gnu aarch64-linux-gnu mipsel-linux-gnu i386-apple-darwin x86_64-apple-darwin x86_64h-apple-darwin i686-w64-mingw32 x86_64-w64-mingw32;
280	do
281		ALL_CHECKS="$ALL_CHECKS\n-triplet $triplet $wordsize -automate"
282	done
283done
284
285if [ "$1" = "-cpu" ]
286then
287	if [ "$3" = "-triplet" ]
288	then
289		echo "-cpu and -triplet are not compatible ..."
290		exit
291	else
292		# User defined number of CPUs
293		NCPU=$2
294	fi
295else
296	# Get number of CPUs for parallel processing
297	NCPU=`getconf _NPROCESSORS_ONLN`
298fi
299echo "Parallelizing on $NCPU processors"
300# Unleash the kraken
301echo $ALL_CHECKS | xargs -n 4 -P $NCPU sh `readlink -f "$0"`
302
303# Check if we had an error, and if yes exit with error
304for wordsize in 16 32 64;
305do
306	for triplet in arm-linux-gnueabi arm-linux-gnueabihf powerpc64le-linux-gnu aarch64-linux-gnu mipsel-linux-gnu i386-apple-darwin x86_64-apple-darwin x86_64h-apple-darwin i686-w64-mingw32 x86_64-w64-mingw32;
307	do
308		ERROR_LOG_FILE=$CROSSBUILD_OUTPUT/error_log/error_log_"$triplet"_"$wordsize"
309		if [ -f "$ERROR_LOG_FILE" ]; then
310			echo "!!!!!!! There have been compilation errors for $triplet $wordsize ..."
311			exit 255
312		fi
313	done
314done
315echo "All compilations went OK!"
316