xzdec: Remove unused short option -M
[xz/debian.git] / build-aux / ci_build.bash
blobb471dd78916080ec130ece2d2e1d8f8721094275
1 #!/bin/bash
2 # SPDX-License-Identifier: 0BSD
4 #############################################################################
6 # Script meant to be used for Continuous Integration automation for POSIX
7 # systems. On GitHub, this is used by Ubuntu and MacOS builds.
9 #############################################################################
11 # Author: Jia Tan
13 #############################################################################
15 set -e
17 USAGE="Usage: $0
18 -a [autogen flags]
19 -b [autotools|cmake]
20 -c [crc32|crc64|sha256]
21 -d [encoders|decoders|bcj|delta|threads|shared|nls|small|clmul|sandbox]
22 -f [CFLAGS]
23 -l [destdir]
24 -m [compiler]
25 -n [ARTIFACTS_DIR_NAME]
26 -p [all|build|test]
27 -s [srcdir]"
29 # Absolute path of script directory
30 ABS_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
32 # Default CLI option values
33 AUTOGEN_FLAGS=""
34 BUILD_SYSTEM="autotools"
35 CHECK_TYPE="crc32,crc64,sha256"
36 BCJ="y"
37 DELTA="y"
38 ENCODERS="y"
39 DECODERS="y"
40 THREADS="y"
41 SHARED="y"
42 NATIVE_LANG_SUPPORT="y"
43 SMALL="n"
44 CLMUL="y"
45 SANDBOX="y"
46 SRC_DIR="$ABS_DIR/../"
47 DEST_DIR="$SRC_DIR/../xz_build"
48 PHASE="all"
49 ARTIFACTS_DIR_NAME="output"
51 [[ -z ${CPU_COUNT} ]] && { CPU_COUNT=$(nproc 2>/dev/null || sysctl -n hw.activecpu); }
52 [[ -z ${MAKEFLAGS} ]] && export MAKEFLAGS="-j${CPU_COUNT} -l${CPU_COUNT}"
53 [[ -z ${CFLAGS} ]] && export CFLAGS="-O2"
55 ###################
56 # Parse arguments #
57 ###################
59 while getopts a:b:c:d:l:m:n:s:p:f:w:h opt; do
60 # b option can have either value "autotools" OR "cmake"
61 case ${opt} in
63 echo "$USAGE"
64 exit 0
67 AUTOGEN_FLAGS="$OPTARG"
70 case "$OPTARG" in
71 autotools) ;;
72 cmake) ;;
73 *) echo "Invalid build system: $OPTARG"; exit 1;;
74 esac
75 BUILD_SYSTEM="$OPTARG"
77 c) CHECK_TYPE="$OPTARG"
79 # d options can be a comma separated list of things to disable at
80 # configure time
82 for disable_arg in $(echo "$OPTARG" | sed "s/,/ /g"); do
83 case "$disable_arg" in
84 encoders) ENCODERS="n" ;;
85 decoders) DECODERS="n" ;;
86 bcj) BCJ="n" ;;
87 delta) DELTA="n" ;;
88 threads) THREADS="n" ;;
89 shared) SHARED="n";;
90 nls) NATIVE_LANG_SUPPORT="n";;
91 small) SMALL="y";;
92 clmul) CLMUL="n";;
93 sandbox) SANDBOX="n";;
94 *) echo "Invalid disable value: $disable_arg"; exit 1 ;;
95 esac
96 done
98 l) DEST_DIR="$OPTARG"
101 CC="$OPTARG"
102 export CC
104 n) ARTIFACTS_DIR_NAME="$OPTARG"
106 s) SRC_DIR="$OPTARG"
108 p) PHASE="$OPTARG"
111 CFLAGS+=" $OPTARG"
112 export CFLAGS
114 w) WRAPPER="$OPTARG"
116 esac
117 done
120 ####################
121 # Helper Functions #
122 ####################
124 # These two functions essentially implement the ternary "?" operator.
125 add_extra_option() {
126 # First argument is option value ("y" or "n")
127 # Second argument is option to set if "y"
128 # Third argument is option to set if "n"
129 if [ "$1" = "y" ]
130 then
131 EXTRA_OPTIONS="$EXTRA_OPTIONS $2"
132 else
133 EXTRA_OPTIONS="$EXTRA_OPTIONS $3"
138 add_to_filter_list() {
139 # First argument is option value ("y" or "n")
140 # Second argument is option to set if "y"
141 if [ "$1" = "y" ]
142 then
143 FILTER_LIST="$FILTER_LIST$2"
148 ###############
149 # Build Phase #
150 ###############
152 if [ "$PHASE" = "all" ] || [ "$PHASE" = "build" ]
153 then
154 # Checksum options should be specified differently based on the
155 # build system. It must be calculated here since we won't know
156 # the build system used until all args have been parsed.
157 # Autotools - comma separated
158 # CMake - semi-colon separated
159 if [ "$BUILD_SYSTEM" = "autotools" ]
160 then
161 SEP=","
162 else
163 SEP=";"
166 CHECK_TYPE_TEMP=""
167 for crc in $(echo "$CHECK_TYPE" | sed "s/,/ /g"); do
168 case "$crc" in
169 # Remove "crc32" from cmake build, if specified.
170 crc32)
171 if [ "$BUILD_SYSTEM" = "cmake" ]
172 then
173 continue
176 crc64) ;;
177 sha256) ;;
178 *) echo "Invalid check type: $crc"; exit 1 ;;
179 esac
181 CHECK_TYPE_TEMP="$CHECK_TYPE_TEMP$SEP$crc"
182 done
184 # Remove the first character from $CHECK_TYPE_TEMP since it will
185 # always be the delimiter.
186 CHECK_TYPE="${CHECK_TYPE_TEMP:1}"
188 FILTER_LIST="lzma1$SEP"lzma2
190 # Build based on arguments
191 mkdir -p "$DEST_DIR"
193 # Generate configure option values
194 EXTRA_OPTIONS=""
196 case $BUILD_SYSTEM in
197 autotools)
198 cd "$SRC_DIR"
200 # Run autogen.sh script if not already run
201 if [ ! -f configure ]
202 then
203 ./autogen.sh "$AUTOGEN_FLAGS"
206 cd "$DEST_DIR"
208 add_to_filter_list "$BCJ" ",x86,powerpc,ia64,arm,armthumb,arm64,sparc,riscv"
209 add_to_filter_list "$DELTA" ",delta"
211 add_extra_option "$ENCODERS" "--enable-encoders=$FILTER_LIST" "--disable-encoders"
212 add_extra_option "$DECODERS" "--enable-decoders=$FILTER_LIST" "--disable-decoders"
213 add_extra_option "$THREADS" "" "--disable-threads"
214 add_extra_option "$SHARED" "" "--disable-shared"
215 add_extra_option "$NATIVE_LANG_SUPPORT" "" "--disable-nls"
216 add_extra_option "$SMALL" "--enable-small" ""
217 add_extra_option "$CLMUL" "" "--disable-clmul-crc"
218 add_extra_option "$SANDBOX" "" "--enable-sandbox=no"
220 # Workaround a bug in too old config.guess. Version with
221 # timestamp='2022-05-08' would be needed but the autotools-dev
222 # package has 2022-01-09 in Ubuntu 22.04LTS and 24.04LTS. The
223 # bug breaks i386 assembler usage autodetection.
224 if "$SRC_DIR/build-aux/config.guess" | grep -q x86_64-pc-linux-gnux32
225 then
226 EXTRA_OPTIONS="$EXTRA_OPTIONS --build=i686-pc-linux-gnu"
229 # Run configure script
230 "$SRC_DIR"/configure --enable-werror --enable-checks="$CHECK_TYPE" $EXTRA_OPTIONS --config-cache
232 # Build the project
233 make
235 cmake)
236 cd "$DEST_DIR"
238 add_to_filter_list "$BCJ" ";x86;powerpc;ia64;arm;armthumb;arm64;sparc;riscv"
239 add_to_filter_list "$DELTA" ";delta"
241 add_extra_option "$THREADS" "-DENABLE_THREADS=ON" "-DENABLE_THREADS=OFF"
243 # Disable MicroLZMA if encoders are not configured.
244 add_extra_option "$ENCODERS" "-DENCODERS=$FILTER_LIST" "-DENCODERS= -DMICROLZMA_ENCODER=OFF"
246 # Disable MicroLZMA and lzip decoders if decoders are not configured.
247 add_extra_option "$DECODERS" "-DDECODERS=$FILTER_LIST" "-DDECODERS= -DMICROLZMA_DECODER=OFF -DLZIP_DECODER=OFF"
249 # CMake disables the shared library by default.
250 add_extra_option "$SHARED" "-DBUILD_SHARED_LIBS=ON" ""
252 add_extra_option "$SMALL" "-DHAVE_SMALL=ON" ""
254 # Remove old cache file to clear previous settings.
255 rm -f "CMakeCache.txt"
256 cmake "$SRC_DIR/CMakeLists.txt" -B "$DEST_DIR" $EXTRA_OPTIONS -DADDITIONAL_CHECK_TYPES="$CHECK_TYPE" -G "Unix Makefiles"
257 cmake --build "$DEST_DIR"
259 esac
263 ##############
264 # Test Phase #
265 ##############
267 if [ "$PHASE" = "all" ] || [ "$PHASE" = "test" ]
268 then
269 case $BUILD_SYSTEM in
270 autotools)
271 cd "$DEST_DIR"
272 # If the tests fail, copy the test logs into the artifacts folder
273 if make check VERBOSE=1 LOG_COMPILER="$WRAPPER"
274 then
276 else
277 mkdir -p "$SRC_DIR/build-aux/artifacts/$ARTIFACTS_DIR_NAME"
278 cp ./tests/*.log "$SRC_DIR/build-aux/artifacts/$ARTIFACTS_DIR_NAME"
279 exit 1
282 cmake)
283 cd "$DEST_DIR"
284 if ${WRAPPER} make test
285 then
287 else
288 mkdir -p "$SRC_DIR/build-aux/artifacts/$ARTIFACTS_DIR_NAME"
289 cp ./Testing/Temporary/*.log "$SRC_DIR/build-aux/artifacts/$ARTIFACTS_DIR_NAME"
290 exit 1
293 esac