CMake: Bump maximum policy version to 3.27.
[xz.git] / build-aux / ci_build.sh
blob3cc014bef01b12d223370428d6acb7cd761ac06d
1 #!/bin/bash
3 #############################################################################
5 # Script meant to be used for Continuous Integration automation for POSIX
6 # systems. On GitHub, this is used by Ubuntu and MacOS builds.
8 #############################################################################
10 # Author: Jia Tan
12 # This file has been put into the public domain.
13 # You can do whatever you want with this file.
15 #############################################################################
17 set -e
19 USAGE="Usage: $0
20 -a [autogen flags]
21 -b [autotools|cmake]
22 -c [crc32|crc64|sha256]
23 -d [encoders|decoders|bcj|delta|threads|shared|nls|small|ifunc|clmul]
24 -f [CFLAGS]
25 -l [destdir]
26 -n [ARTIFACTS_DIR_NAME]
27 -p [all|build|test]
28 -s [srcdir]"
30 # Absolute path of script directory
31 ABS_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
33 # Default CLI option values
34 AUTOGEN_FLAGS=""
35 BUILD_SYSTEM="autotools"
36 CHECK_TYPE="crc32,crc64,sha256"
37 BCJ="y"
38 DELTA="y"
39 ENCODERS="y"
40 DECODERS="y"
41 THREADS="y"
42 SHARED="y"
43 NATIVE_LANG_SUPPORT="y"
44 SMALL="n"
45 IFUNC="y"
46 CLMUL="y"
47 SRC_DIR="$ABS_DIR/../"
48 DEST_DIR="$SRC_DIR/../xz_build"
49 PHASE="all"
50 ARTIFACTS_DIR_NAME="output"
53 ###################
54 # Parse arguments #
55 ###################
57 while getopts a:b:c:d:l:n:s:p:f:h opt; do
58 # b option can have either value "autotools" OR "cmake"
59 case ${opt} in
61 echo "$USAGE"
62 exit 0
65 AUTOGEN_FLAGS="$OPTARG"
68 case "$OPTARG" in
69 autotools) ;;
70 cmake) ;;
71 *) echo "Invalid build system: $OPTARG"; exit 1;;
72 esac
73 BUILD_SYSTEM="$OPTARG"
75 c) CHECK_TYPE="$OPTARG"
77 # d options can be a comma separated list of things to disable at
78 # configure time
80 for disable_arg in $(echo "$OPTARG" | sed "s/,/ /g"); do
81 case "$disable_arg" in
82 encoders) ENCODERS="n" ;;
83 decoders) DECODERS="n" ;;
84 bcj) BCJ="n" ;;
85 delta) DELTA="n" ;;
86 threads) THREADS="n" ;;
87 shared) SHARED="n";;
88 nls) NATIVE_LANG_SUPPORT="n";;
89 small) SMALL="y";;
90 ifunc) IFUNC="n";;
91 clmul) CLMUL="n";;
92 *) echo "Invalid disable value: $disable_arg"; exit 1 ;;
93 esac
94 done
96 l) DEST_DIR="$OPTARG"
98 n) ARTIFACTS_DIR_NAME="$OPTARG"
100 s) SRC_DIR="$OPTARG"
102 p) PHASE="$OPTARG"
105 CFLAGS="$OPTARG"
106 export CFLAGS
108 esac
109 done
112 ####################
113 # Helper Functions #
114 ####################
116 # These two functions essentially implement the ternary "?" operator.
117 add_extra_option() {
118 # First argument is option value ("y" or "n")
119 # Second argument is option to set if "y"
120 # Third argument is option to set if "n"
121 if [ "$1" = "y" ]
122 then
123 EXTRA_OPTIONS="$EXTRA_OPTIONS $2"
124 else
125 EXTRA_OPTIONS="$EXTRA_OPTIONS $3"
130 add_to_filter_list() {
131 # First argument is option value ("y" or "n")
132 # Second argument is option to set if "y"
133 if [ "$1" = "y" ]
134 then
135 FILTER_LIST="$FILTER_LIST$2"
140 ###############
141 # Build Phase #
142 ###############
144 if [ "$PHASE" = "all" ] || [ "$PHASE" = "build" ]
145 then
146 # Checksum options should be specified differently based on the
147 # build system. It must be calculated here since we won't know
148 # the build system used until all args have been parsed.
149 # Autotools - comma separated
150 # CMake - semi-colon separated
151 if [ "$BUILD_SYSTEM" = "autotools" ]
152 then
153 SEP=","
154 else
155 SEP=";"
158 CHECK_TYPE_TEMP=""
159 for crc in $(echo "$CHECK_TYPE" | sed "s/,/ /g"); do
160 case "$crc" in
161 # Remove "crc32" from cmake build, if specified.
162 crc32)
163 if [ "$BUILD_SYSTEM" = "cmake" ]
164 then
165 continue
168 crc64) ;;
169 sha256) ;;
170 *) echo "Invalid check type: $crc"; exit 1 ;;
171 esac
173 CHECK_TYPE_TEMP="$CHECK_TYPE_TEMP$SEP$crc"
174 done
176 # Remove the first character from $CHECK_TYPE_TEMP since it will
177 # always be the delimiter.
178 CHECK_TYPE="${CHECK_TYPE_TEMP:1}"
180 FILTER_LIST="lzma1$SEP"lzma2
182 # Build based on arguments
183 mkdir -p "$DEST_DIR"
185 # Generate configure option values
186 EXTRA_OPTIONS=""
188 case $BUILD_SYSTEM in
189 autotools)
190 cd "$SRC_DIR"
192 # Run autogen.sh script if not already run
193 if [ ! -f configure ]
194 then
195 ./autogen.sh "$AUTOGEN_FLAGS"
198 cd "$DEST_DIR"
200 add_to_filter_list "$BCJ" ",x86,powerpc,ia64,arm,armthumb,arm64,sparc"
201 add_to_filter_list "$DELTA" ",delta"
203 add_extra_option "$ENCODERS" "--enable-encoders=$FILTER_LIST" "--disable-encoders"
204 add_extra_option "$DECODERS" "--enable-decoders=$FILTER_LIST" "--disable-decoders"
205 add_extra_option "$THREADS" "" "--disable-threads"
206 add_extra_option "$SHARED" "" "--disable-shared"
207 add_extra_option "$NATIVE_LANG_SUPPORT" "" "--disable-nls"
208 add_extra_option "$SMALL" "--enable-small" ""
209 add_extra_option "$IFUNC" "" "--disable-ifunc"
210 add_extra_option "$CLMUL" "" "--disable-clmul-crc"
212 # Run configure script
213 "$SRC_DIR"/configure --enable-werror --enable-checks="$CHECK_TYPE" $EXTRA_OPTIONS --config-cache
215 # Build the project
216 make
218 cmake)
219 cd "$DEST_DIR"
221 add_to_filter_list "$BCJ" ";x86;powerpc;ia64;arm;armthumb;arm64;sparc"
222 add_to_filter_list "$DELTA" ";delta"
224 add_extra_option "$THREADS" "-DENABLE_THREADS=ON" "-DENABLE_THREADS=OFF"
226 # Disable MicroLZMA if encoders are not configured.
227 add_extra_option "$ENCODERS" "-DENCODERS=$FILTER_LIST" "-DENCODERS= -DMICROLZMA_ENCODER=OFF"
229 # Disable MicroLZMA and lzip decoders if decoders are not configured.
230 add_extra_option "$DECODERS" "-DDECODERS=$FILTER_LIST" "-DDECODERS= -DMICROLZMA_DECODER=OFF -DLZIP_DECODER=OFF"
232 # CMake disables the shared library by default.
233 add_extra_option "$SHARED" "-DBUILD_SHARED_LIBS=ON" ""
235 add_extra_option "$SMALL" "-DHAVE_SMALL=ON" ""
237 # Remove old cache file to clear previous settings.
238 rm -f "CMakeCache.txt"
239 cmake "$SRC_DIR/CMakeLists.txt" -B "$DEST_DIR" $EXTRA_OPTIONS -DADDITIONAL_CHECK_TYPES="$CHECK_TYPE" -G "Unix Makefiles"
240 cmake --build "$DEST_DIR"
242 esac
246 ##############
247 # Test Phase #
248 ##############
250 if [ "$PHASE" = "all" ] || [ "$PHASE" = "test" ]
251 then
252 case $BUILD_SYSTEM in
253 autotools)
254 cd "$DEST_DIR"
255 # If the tests fail, copy the test logs into the artifacts folder
256 if make check
257 then
259 else
260 mkdir -p "$SRC_DIR/build-aux/artifacts/$ARTIFACTS_DIR_NAME"
261 cp ./tests/*.log "$SRC_DIR/build-aux/artifacts/$ARTIFACTS_DIR_NAME"
262 exit 1
265 cmake)
266 cd "$DEST_DIR"
267 if make test
268 then
270 else
271 mkdir -p "$SRC_DIR/build-aux/artifacts/$ARTIFACTS_DIR_NAME"
272 cp ./Testing/Temporary/*.log "$SRC_DIR/build-aux/artifacts/$ARTIFACTS_DIR_NAME"
273 exit 1
276 esac