forked from libjxl/libjxl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci.sh
executable file
·1555 lines (1393 loc) · 50 KB
/
ci.sh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Copyright (c) the JPEG XL Project Authors. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# Continuous integration helper module. This module is meant to be called from
# workflows during the continuous integration build, as well as from the
# command line for developers.
set -eu
OS=`uname -s`
SELF=$(realpath "$0")
MYDIR=$(dirname "${SELF}")
### Environment parameters:
TEST_STACK_LIMIT="${TEST_STACK_LIMIT:-256}"
BENCHMARK_NUM_THREADS="${BENCHMARK_NUM_THREADS:-0}"
BUILD_CONFIG=${BUILD_CONFIG:-}
CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-RelWithDebInfo}
CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH:-}
CMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER:-}
CMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER:-}
CMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM:-}
SKIP_BUILD="${SKIP_BUILD:-0}"
SKIP_TEST="${SKIP_TEST:-0}"
FASTER_MSAN_BUILD="${FASTER_MSAN_BUILD:-0}"
TARGETS="${TARGETS:-all doc}"
TEST_SELECTOR="${TEST_SELECTOR:-}"
BUILD_TARGET="${BUILD_TARGET:-}"
ENABLE_WASM_SIMD="${ENABLE_WASM_SIMD:-0}"
if [[ -n "${BUILD_TARGET}" ]]; then
BUILD_DIR="${BUILD_DIR:-${MYDIR}/build-${BUILD_TARGET%%-*}}"
else
BUILD_DIR="${BUILD_DIR:-${MYDIR}/build}"
fi
# Whether we should post a message in the MR when the build fails.
POST_MESSAGE_ON_ERROR="${POST_MESSAGE_ON_ERROR:-1}"
# By default, do a lightweight debian HWY package build.
HWY_PKG_OPTIONS="${HWY_PKG_OPTIONS:---set-envvar=HWY_EXTRA_CONFIG=-DBUILD_TESTING=OFF -DHWY_ENABLE_EXAMPLES=OFF -DHWY_ENABLE_CONTRIB=OFF}"
# Set default compilers to clang if not already set
export CC=${CC:-clang}
export CXX=${CXX:-clang++}
# Time limit for the "fuzz" command in seconds (0 means no limit).
FUZZER_MAX_TIME="${FUZZER_MAX_TIME:-0}"
SANITIZER="none"
if [[ "${BUILD_TARGET%%-*}" == "x86_64" ||
"${BUILD_TARGET%%-*}" == "i686" ]]; then
# Default to building all targets, even if compiler baseline is SSE4
HWY_BASELINE_TARGETS=${HWY_BASELINE_TARGETS:-HWY_EMU128}
else
HWY_BASELINE_TARGETS=${HWY_BASELINE_TARGETS:-}
fi
# Convenience flag to pass both CMAKE_C_FLAGS and CMAKE_CXX_FLAGS
CMAKE_FLAGS=${CMAKE_FLAGS:-}
CMAKE_C_FLAGS="${CMAKE_C_FLAGS:-} ${CMAKE_FLAGS}"
CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS:-} ${CMAKE_FLAGS}"
CMAKE_CROSSCOMPILING_EMULATOR=${CMAKE_CROSSCOMPILING_EMULATOR:-}
CMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS:-}
CMAKE_FIND_ROOT_PATH=${CMAKE_FIND_ROOT_PATH:-}
CMAKE_MODULE_LINKER_FLAGS=${CMAKE_MODULE_LINKER_FLAGS:-}
CMAKE_SHARED_LINKER_FLAGS=${CMAKE_SHARED_LINKER_FLAGS:-}
CMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE:-}
if [[ "${ENABLE_WASM_SIMD}" -ne "0" ]]; then
CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -msimd128"
CMAKE_C_FLAGS="${CMAKE_C_FLAGS} -msimd128"
CMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS} -msimd128"
fi
if [[ "${ENABLE_WASM_SIMD}" -eq "2" ]]; then
CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -DHWY_WANT_WASM2"
CMAKE_C_FLAGS="${CMAKE_C_FLAGS} -DHWY_WANT_WASM2"
fi
if [[ -z "${BUILD_CONFIG}" ]]; then
TOOLS_DIR="${BUILD_DIR}/tools"
else
TOOLS_DIR="${BUILD_DIR}/tools/${BUILD_CONFIG}"
fi
if [[ ! -z "${HWY_BASELINE_TARGETS}" ]]; then
CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -DHWY_BASELINE_TARGETS=${HWY_BASELINE_TARGETS}"
fi
# Version inferred from the CI variables.
CI_COMMIT_SHA=${GITHUB_SHA:-}
JPEGXL_VERSION=${JPEGXL_VERSION:-}
# Benchmark parameters
STORE_IMAGES=${STORE_IMAGES:-1}
BENCHMARK_CORPORA="${MYDIR}/third_party/corpora"
# Local flags passed to sanitizers.
UBSAN_FLAGS=(
-fsanitize=alignment
-fsanitize=bool
-fsanitize=bounds
-fsanitize=builtin
-fsanitize=enum
-fsanitize=float-cast-overflow
-fsanitize=float-divide-by-zero
-fsanitize=integer-divide-by-zero
-fsanitize=null
-fsanitize=object-size
-fsanitize=pointer-overflow
-fsanitize=return
-fsanitize=returns-nonnull-attribute
-fsanitize=shift-base
-fsanitize=shift-exponent
-fsanitize=unreachable
-fsanitize=vla-bound
-fno-sanitize-recover=undefined
# Brunsli uses unaligned accesses to uint32_t, so alignment is just a warning.
-fsanitize-recover=alignment
)
# -fsanitize=function doesn't work on aarch64 and arm.
if [[ "${BUILD_TARGET%%-*}" != "aarch64" &&
"${BUILD_TARGET%%-*}" != "arm" ]]; then
UBSAN_FLAGS+=(
-fsanitize=function
)
fi
if [[ "${BUILD_TARGET%%-*}" != "arm" ]]; then
UBSAN_FLAGS+=(
-fsanitize=signed-integer-overflow
)
fi
CLANG_TIDY_BIN_CANDIDATES=(
clang-tidy
clang-tidy-6.0
clang-tidy-7
clang-tidy-8
clang-tidy-9
clang-tidy-10
clang-tidy-11
clang-tidy-12
clang-tidy-13
clang-tidy-14
clang-tidy-15
clang-tidy-16
clang-tidy-17
clang-tidy-18
)
CLANG_TIDY_BIN=${CLANG_TIDY_BIN:-$(which ${CLANG_TIDY_BIN_CANDIDATES[@]} 2>/dev/null | tail -n 1)}
# Default to "cat" if "colordiff" is not installed or if stdout is not a tty.
if [[ -t 1 ]]; then
COLORDIFF_BIN=$(which colordiff cat 2>/dev/null | head -n 1)
else
COLORDIFF_BIN="cat"
fi
FIND_BIN=$(which gfind find 2>/dev/null | head -n 1)
# "false" will disable wine64 when not installed. This won't allow
# cross-compiling.
WINE_BIN=$(which wine64 false 2>/dev/null | head -n 1)
CLANG_VERSION="${CLANG_VERSION:-}"
# Detect the clang version suffix and store it in CLANG_VERSION. For example,
# "6.0" for clang 6 or "7" for clang 7.
detect_clang_version() {
if [[ -n "${CLANG_VERSION}" ]]; then
return 0
fi
local clang_version=$("${CC:-clang}" --version | head -n1)
clang_version=${clang_version#"Debian "}
clang_version=${clang_version#"Ubuntu "}
local llvm_tag
case "${clang_version}" in
"clang version 6."*)
CLANG_VERSION="6.0"
;;
"clang version "*)
# Any other clang version uses just the major version number.
local suffix="${clang_version#clang version }"
CLANG_VERSION="${suffix%%.*}"
;;
"emcc"*)
# We can't use asan or msan in the emcc case.
;;
*)
echo "Unknown clang version: ${clang_version}" >&2
return 1
esac
}
# Temporary files cleanup hooks.
CLEANUP_FILES=()
cleanup() {
if [[ ${#CLEANUP_FILES[@]} -ne 0 ]]; then
rm -fr "${CLEANUP_FILES[@]}"
fi
}
# Executed on exit.
on_exit() {
local retcode="$1"
# Always cleanup the CLEANUP_FILES.
cleanup
}
trap 'retcode=$?; { set +x; } 2>/dev/null; on_exit ${retcode}' INT TERM EXIT
# These variables are populated when calling merge_request_commits().
# The current hash at the top of the current branch or merge request branch (if
# running from a merge request pipeline).
MR_HEAD_SHA=""
# The common ancestor between the current commit and the tracked branch, such
# as main. This includes a list
MR_ANCESTOR_SHA=""
# Populate MR_HEAD_SHA and MR_ANCESTOR_SHA.
merge_request_commits() {
{ set +x; } 2>/dev/null
# GITHUB_SHA is the current reference being build in GitHub Actions.
if [[ -n "${GITHUB_SHA:-}" ]]; then
# GitHub normally does a checkout of a merge commit on a shallow repository
# by default. We want to get a bit more of the history to be able to diff
# changes on the Pull Request if needed. This fetches 10 more commits which
# should be enough given that PR normally should have 1 commit.
git -C "${MYDIR}" fetch -q origin "${GITHUB_SHA}" --depth 10
if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then
MR_HEAD_SHA="$(git rev-parse "FETCH_HEAD^2" 2>/dev/null ||
echo "${GITHUB_SHA}")"
else
MR_HEAD_SHA="${GITHUB_SHA}"
fi
else
MR_HEAD_SHA=$(git -C "${MYDIR}" rev-parse -q "HEAD")
fi
if [[ -n "${GITHUB_BASE_REF:-}" ]]; then
# Pull request workflow in GitHub Actions. GitHub checkout action uses
# "origin" as the remote for the git checkout.
git -C "${MYDIR}" fetch -q origin "${GITHUB_BASE_REF}"
MR_ANCESTOR_SHA=$(git -C "${MYDIR}" rev-parse -q FETCH_HEAD)
else
# We are in a local branch, not a pull request workflow.
MR_ANCESTOR_SHA=$(git -C "${MYDIR}" rev-parse -q HEAD@{upstream} || true)
fi
if [[ -z "${MR_ANCESTOR_SHA}" ]]; then
echo "Warning, not tracking any branch, using the last commit in HEAD.">&2
# This prints the return value with just HEAD.
MR_ANCESTOR_SHA=$(git -C "${MYDIR}" rev-parse -q "${MR_HEAD_SHA}^")
else
# GitHub runs the pipeline on a merge commit, no need to look for the common
# ancestor in that case.
if [[ -z "${GITHUB_BASE_REF:-}" ]]; then
MR_ANCESTOR_SHA=$(git -C "${MYDIR}" merge-base \
"${MR_ANCESTOR_SHA}" "${MR_HEAD_SHA}")
fi
fi
set -x
}
# Set up and export the environment variables needed by the child processes.
export_env() {
if [[ "${BUILD_TARGET}" == *mingw32 ]]; then
# Wine needs to know the paths to the mingw dlls. These should be
# separated by ';'.
WINEPATH=$("${CC:-clang}" -print-search-dirs --target="${BUILD_TARGET}" \
| grep -F 'libraries: =' | cut -f 2- -d '=' | tr ':' ';')
# We also need our own libraries in the wine path.
local real_build_dir=$(realpath "${BUILD_DIR}")
# Some library .dll dependencies are installed in /bin:
export WINEPATH="${WINEPATH};${real_build_dir};${real_build_dir}/third_party/brotli;/usr/${BUILD_TARGET}/bin"
local prefix="${BUILD_DIR}/wineprefix"
mkdir -p "${prefix}"
export WINEPREFIX=$(realpath "${prefix}")
fi
# Sanitizers need these variables to print and properly format the stack
# traces:
LLVM_SYMBOLIZER=$("${CC:-clang}" -print-prog-name=llvm-symbolizer || true)
if [[ -n "${LLVM_SYMBOLIZER}" ]]; then
export ASAN_SYMBOLIZER_PATH="${LLVM_SYMBOLIZER}"
export MSAN_SYMBOLIZER_PATH="${LLVM_SYMBOLIZER}"
export UBSAN_SYMBOLIZER_PATH="${LLVM_SYMBOLIZER}"
fi
}
cmake_configure() {
export_env
if [[ "${STACK_SIZE:-0}" == 1 ]]; then
# Dump the stack size of each function in the .stack_sizes section for
# analysis.
CMAKE_C_FLAGS+=" -fstack-size-section"
CMAKE_CXX_FLAGS+=" -fstack-size-section"
fi
local args=(
-B"${BUILD_DIR}" -H"${MYDIR}"
-DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
-G Ninja
-DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS}"
-DCMAKE_C_FLAGS="${CMAKE_C_FLAGS}"
-DCMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS}"
-DCMAKE_MODULE_LINKER_FLAGS="${CMAKE_MODULE_LINKER_FLAGS}"
-DCMAKE_SHARED_LINKER_FLAGS="${CMAKE_SHARED_LINKER_FLAGS}"
-DJPEGXL_VERSION="${JPEGXL_VERSION}"
-DSANITIZER="${SANITIZER}"
# These are not enabled by default in cmake.
-DJPEGXL_ENABLE_VIEWERS=ON
-DJPEGXL_ENABLE_PLUGINS=ON
-DJPEGXL_ENABLE_DEVTOOLS=ON
# We always use libfuzzer in the ci.sh wrapper.
-DJPEGXL_FUZZER_LINK_FLAGS="-fsanitize=fuzzer"
)
if [[ "${BUILD_TARGET}" != *mingw32 ]]; then
args+=(
-DJPEGXL_WARNINGS_AS_ERRORS=ON
)
fi
if [[ -n "${BUILD_TARGET}" ]]; then
local system_name="Linux"
if [[ "${BUILD_TARGET}" == *mingw32 ]]; then
# When cross-compiling with mingw the target must be set to Windows and
# run programs with wine.
system_name="Windows"
args+=(
-DCMAKE_CROSSCOMPILING_EMULATOR="${WINE_BIN}"
# Normally CMake automatically defines MINGW=1 when building with the
# mingw compiler (x86_64-w64-mingw32-gcc) but we are normally compiling
# with clang.
-DMINGW=1
)
fi
# EMSCRIPTEN toolchain sets the right values itself
if [[ "${BUILD_TARGET}" != wasm* ]]; then
# If set, BUILD_TARGET must be the target triplet such as
# x86_64-unknown-linux-gnu.
args+=(
-DCMAKE_C_COMPILER_TARGET="${BUILD_TARGET}"
-DCMAKE_CXX_COMPILER_TARGET="${BUILD_TARGET}"
# Only the first element of the target triplet.
-DCMAKE_SYSTEM_PROCESSOR="${BUILD_TARGET%%-*}"
-DCMAKE_SYSTEM_NAME="${system_name}"
-DCMAKE_TOOLCHAIN_FILE="${CMAKE_TOOLCHAIN_FILE}"
)
else
args+=(
# sjpeg confuses WASM SIMD with SSE.
-DSJPEG_ENABLE_SIMD=OFF
# Building shared libs is not very useful for WASM.
-DBUILD_SHARED_LIBS=OFF
)
fi
args+=(
# These are needed to make googletest work when cross-compiling.
-DCMAKE_CROSSCOMPILING=1
-DHAVE_STD_REGEX=0
-DHAVE_POSIX_REGEX=0
-DHAVE_GNU_POSIX_REGEX=0
-DHAVE_STEADY_CLOCK=0
-DHAVE_THREAD_SAFETY_ATTRIBUTES=0
)
if [[ -z "${CMAKE_FIND_ROOT_PATH}" ]]; then
# find_package() will look in this prefix for libraries.
CMAKE_FIND_ROOT_PATH="/usr/${BUILD_TARGET}"
fi
if [[ -z "${CMAKE_PREFIX_PATH}" ]]; then
CMAKE_PREFIX_PATH="/usr/${BUILD_TARGET}"
fi
# Use pkg-config for the target. If there's no pkg-config available for the
# target we can set the PKG_CONFIG_PATH to the appropriate path in most
# linux distributions.
local pkg_config=$(which "${BUILD_TARGET}-pkg-config" || true)
if [[ -z "${pkg_config}" ]]; then
pkg_config=$(which pkg-config)
export PKG_CONFIG_LIBDIR="/usr/${BUILD_TARGET}/lib/pkgconfig"
fi
if [[ -n "${pkg_config}" ]]; then
args+=(-DPKG_CONFIG_EXECUTABLE="${pkg_config}")
fi
fi
if [[ -n "${CMAKE_CROSSCOMPILING_EMULATOR}" ]]; then
args+=(
-DCMAKE_CROSSCOMPILING_EMULATOR="${CMAKE_CROSSCOMPILING_EMULATOR}"
)
fi
if [[ -n "${CMAKE_FIND_ROOT_PATH}" ]]; then
args+=(
-DCMAKE_FIND_ROOT_PATH="${CMAKE_FIND_ROOT_PATH}"
)
fi
if [[ -n "${CMAKE_PREFIX_PATH}" ]]; then
args+=(
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}"
)
fi
if [[ -n "${CMAKE_C_COMPILER_LAUNCHER}" ]]; then
args+=(
-DCMAKE_C_COMPILER_LAUNCHER="${CMAKE_C_COMPILER_LAUNCHER}"
)
fi
if [[ -n "${CMAKE_CXX_COMPILER_LAUNCHER}" ]]; then
args+=(
-DCMAKE_CXX_COMPILER_LAUNCHER="${CMAKE_CXX_COMPILER_LAUNCHER}"
)
fi
if [[ -n "${CMAKE_MAKE_PROGRAM}" ]]; then
args+=(
-DCMAKE_MAKE_PROGRAM="${CMAKE_MAKE_PROGRAM}"
)
fi
if [[ "${BUILD_TARGET}" == wasm* ]]; then
emcmake cmake "${args[@]}" "$@"
else
cmake "${args[@]}" "$@"
fi
}
cmake_build_and_test() {
if [[ "${SKIP_BUILD}" -eq "1" ]]; then
return 0
fi
# gtest_discover_tests() runs the test binaries to discover the list of tests
# at build time, which fails under qemu.
ASAN_OPTIONS=detect_leaks=0 cmake --build "${BUILD_DIR}" -- $TARGETS
# Pack test binaries if requested.
if [[ "${PACK_TEST:-}" == "1" ]]; then
(cd "${BUILD_DIR}"
${FIND_BIN} -name '*.cmake' -a '!' -path '*CMakeFiles*'
# gtest / gtest_main shared libs
${FIND_BIN} lib/ -name 'libg*.so*'
${FIND_BIN} -type d -name tests -a '!' -path '*CMakeFiles*'
) | tar -C "${BUILD_DIR}" -cf "${BUILD_DIR}/tests.tar.xz" -T - \
--use-compress-program="xz --threads=$(nproc --all || echo 1) -6"
du -h "${BUILD_DIR}/tests.tar.xz"
# Pack coverage data if also available.
touch "${BUILD_DIR}/gcno.sentinel"
(cd "${BUILD_DIR}"; echo gcno.sentinel; ${FIND_BIN} -name '*gcno') | \
tar -C "${BUILD_DIR}" -cvf "${BUILD_DIR}/gcno.tar.xz" -T - \
--use-compress-program="xz --threads=$(nproc --all || echo 1) -6"
fi
if [[ "${SKIP_TEST}" -ne "1" ]]; then
(cd "${BUILD_DIR}"
export UBSAN_OPTIONS=print_stacktrace=1
[[ "${TEST_STACK_LIMIT}" == "none" ]] || ulimit -s "${TEST_STACK_LIMIT}"
ctest -j $(nproc --all || echo 1) ${TEST_SELECTOR} --output-on-failure)
fi
}
# Configure the build to strip unused functions. This considerably reduces the
# output size, specially for tests which only use a small part of the whole
# library.
strip_dead_code() {
# Emscripten does tree shaking without any extra flags.
if [[ "${BUILD_TARGET}" == wasm* ]]; then
return 0
fi
# -ffunction-sections, -fdata-sections and -Wl,--gc-sections effectively
# discard all unreachable code, reducing the code size. For this to work, we
# need to also pass --no-export-dynamic to prevent it from exporting all the
# internal symbols (like functions) making them all reachable and thus not a
# candidate for removal.
CMAKE_CXX_FLAGS+=" -ffunction-sections -fdata-sections"
CMAKE_C_FLAGS+=" -ffunction-sections -fdata-sections"
if [[ "${OS}" == "Darwin" ]]; then
CMAKE_EXE_LINKER_FLAGS+=" -dead_strip"
CMAKE_SHARED_LINKER_FLAGS+=" -dead_strip"
else
CMAKE_EXE_LINKER_FLAGS+=" -Wl,--gc-sections -Wl,--no-export-dynamic"
CMAKE_SHARED_LINKER_FLAGS+=" -Wl,--gc-sections -Wl,--no-export-dynamic"
fi
}
### Externally visible commands
cmd_debug() {
CMAKE_BUILD_TYPE="DebugOpt"
cmake_configure "$@"
cmake_build_and_test
}
cmd_release() {
CMAKE_BUILD_TYPE="Release"
strip_dead_code
cmake_configure "$@"
cmake_build_and_test
}
cmd_opt() {
CMAKE_BUILD_TYPE="RelWithDebInfo"
CMAKE_CXX_FLAGS+=" -DJXL_IS_DEBUG_BUILD"
cmake_configure "$@"
cmake_build_and_test
}
cmd_coverage() {
# -O0 prohibits stack space reuse -> causes stack-overflow on dozens of tests.
TEST_STACK_LIMIT="none"
cmd_release -DJPEGXL_ENABLE_COVERAGE=ON "$@"
if [[ "${SKIP_TEST}" -ne "1" ]]; then
# If we didn't run the test we also don't print a coverage report.
cmd_coverage_report
fi
}
cmd_coverage_report() {
LLVM_COV=$("${CC:-clang}" -print-prog-name=llvm-cov)
local real_build_dir=$(realpath "${BUILD_DIR}")
local gcovr_args=(
-r "${real_build_dir}"
--gcov-executable "${LLVM_COV} gcov"
# Only print coverage information for the libjxl directories. The rest
# is not part of the code under test.
--filter '.*jxl/.*'
--exclude '.*_gbench.cc'
--exclude '.*_test.cc'
--exclude '.*_testonly..*'
--exclude '.*_debug.*'
--exclude '.*test_utils..*'
--object-directory "${real_build_dir}"
)
(
cd "${real_build_dir}"
gcovr "${gcovr_args[@]}" --html --html-details \
--output="${real_build_dir}/coverage.html"
gcovr "${gcovr_args[@]}" --print-summary |
tee "${real_build_dir}/coverage.txt"
gcovr "${gcovr_args[@]}" --xml --output="${real_build_dir}/coverage.xml"
)
}
cmd_test() {
export_env
# Unpack tests if needed.
if [[ -e "${BUILD_DIR}/tests.tar.xz" && ! -d "${BUILD_DIR}/tests" ]]; then
tar -C "${BUILD_DIR}" -Jxvf "${BUILD_DIR}/tests.tar.xz"
fi
if [[ -e "${BUILD_DIR}/gcno.tar.xz" && ! -d "${BUILD_DIR}/gcno.sentinel" ]]; then
tar -C "${BUILD_DIR}" -Jxvf "${BUILD_DIR}/gcno.tar.xz"
fi
(cd "${BUILD_DIR}"
export UBSAN_OPTIONS=print_stacktrace=1
[[ "${TEST_STACK_LIMIT}" == "none" ]] || ulimit -s "${TEST_STACK_LIMIT}"
ctest -j $(nproc --all || echo 1) ${TEST_SELECTOR} --output-on-failure "$@")
}
cmd_gbench() {
export_env
(cd "${BUILD_DIR}"
export UBSAN_OPTIONS=print_stacktrace=1
lib/jxl_gbench \
--benchmark_counters_tabular=true \
--benchmark_out_format=json \
--benchmark_out=gbench.json "$@"
)
}
cmd_asanfuzz() {
CMAKE_CXX_FLAGS+=" -fsanitize=fuzzer-no-link -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1"
CMAKE_C_FLAGS+=" -fsanitize=fuzzer-no-link -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1"
cmd_asan -DJPEGXL_ENABLE_FUZZERS=ON "$@"
}
cmd_msanfuzz() {
# Install msan if needed before changing the flags.
detect_clang_version
local msan_prefix="${HOME}/.msan/${CLANG_VERSION}"
# TODO(eustas): why libc++abi.a is bad?
if [[ ! -d "${msan_prefix}" || -e "${msan_prefix}/lib/libc++abi.a" ]]; then
# Install msan libraries for this version if needed or if an older version
# with libc++abi was installed.
cmd_msan_install
fi
CMAKE_CXX_FLAGS+=" -fsanitize=fuzzer-no-link -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1"
CMAKE_C_FLAGS+=" -fsanitize=fuzzer-no-link -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1"
cmd_msan -DJPEGXL_ENABLE_FUZZERS=ON "$@"
}
cmd_asan() {
SANITIZER="asan"
CMAKE_C_FLAGS+=" -g -DADDRESS_SANITIZER \
-fsanitize=address ${UBSAN_FLAGS[@]}"
CMAKE_CXX_FLAGS+=" -g -DADDRESS_SANITIZER \
-fsanitize=address ${UBSAN_FLAGS[@]}"
strip_dead_code
cmake_configure "$@" -DJPEGXL_ENABLE_TCMALLOC=OFF
cmake_build_and_test
}
cmd_tsan() {
SANITIZER="tsan"
local tsan_args=(
-g
-DTHREAD_SANITIZER
-fsanitize=thread
)
CMAKE_C_FLAGS+=" ${tsan_args[@]}"
CMAKE_CXX_FLAGS+=" ${tsan_args[@]}"
cmake_configure "$@" -DJPEGXL_ENABLE_TCMALLOC=OFF
cmake_build_and_test
}
cmd_msan() {
SANITIZER="msan"
detect_clang_version
local msan_prefix="${HOME}/.msan/${CLANG_VERSION}"
if [[ ! -d "${msan_prefix}" || -e "${msan_prefix}/lib/libc++abi.a" ]]; then
# Install msan libraries for this version if needed or if an older version
# with libc++abi was installed.
cmd_msan_install
fi
local msan_c_flags=(
-fsanitize=memory
-fno-omit-frame-pointer
-g
-DMEMORY_SANITIZER
# Force gtest to not use the cxxbai.
-DGTEST_HAS_CXXABI_H_=0
)
if [[ "${FASTER_MSAN_BUILD}" -ne "1" ]]; then
msan_c_flags=(
"${msan_c_flags[@]}"
-fsanitize-memory-track-origins
)
fi
local msan_cxx_flags=(
"${msan_c_flags[@]}"
# Some C++ sources don't use the std at all, so the -stdlib=libc++ is unused
# in those cases. Ignore the warning.
-Wno-unused-command-line-argument
-stdlib=libc++
# We include the libc++ from the msan directory instead, so we don't want
# the std includes.
-nostdinc++
-cxx-isystem"${msan_prefix}/include/c++/v1"
)
local msan_linker_flags=(
-L"${msan_prefix}"/lib
-Wl,-rpath -Wl,"${msan_prefix}"/lib/
)
CMAKE_C_FLAGS+=" ${msan_c_flags[@]}"
CMAKE_CXX_FLAGS+=" ${msan_cxx_flags[@]}"
CMAKE_EXE_LINKER_FLAGS+=" ${msan_linker_flags[@]}"
CMAKE_MODULE_LINKER_FLAGS+=" ${msan_linker_flags[@]}"
CMAKE_SHARED_LINKER_FLAGS+=" ${msan_linker_flags[@]}"
strip_dead_code
# MSAN share of stack size is non-negligible.
TEST_STACK_LIMIT="none"
# TODO(eustas): investigate why fuzzers do not link when MSAN libc++ is used
cmake_configure "$@" \
-DCMAKE_CROSSCOMPILING=1 -DRUN_HAVE_STD_REGEX=0 -DRUN_HAVE_POSIX_REGEX=0 \
-DJPEGXL_ENABLE_TCMALLOC=OFF -DJPEGXL_WARNINGS_AS_ERRORS=OFF \
-DCMAKE_REQUIRED_LINK_OPTIONS="${msan_linker_flags[@]}" \
-DJPEGXL_ENABLE_FUZZERS=OFF
cmake_build_and_test
}
# Install libc++ libraries compiled with msan in the msan_prefix for the current
# compiler version.
cmd_msan_install() {
local tmpdir=$(mktemp -d)
CLEANUP_FILES+=("${tmpdir}")
local msan_root="${HOME}/.msan"
mkdir -p "${msan_root}"
# Detect the llvm to install:
export CC="${CC:-clang}"
export CXX="${CXX:-clang++}"
detect_clang_version
# Allow overriding the LLVM checkout.
local llvm_root="${LLVM_ROOT:-}"
if [ -z "${llvm_root}" ]; then
declare -A llvm_tag_by_version=(
["6.0"]="6.0.1"
["7"]="7.1.0"
["8"]="8.0.1"
["9"]="9.0.2"
["10"]="10.0.1"
["11"]="11.1.0"
["12"]="12.0.1"
["13"]="13.0.1"
["14"]="14.0.6"
["15"]="15.0.7"
["16"]="16.0.6"
["17"]="17.0.6"
["18"]="18.1.6"
)
local llvm_tag="${CLANG_VERSION}.0.0"
if [[ -n "${llvm_tag_by_version["${CLANG_VERSION}"]}" ]]; then
llvm_tag=${llvm_tag_by_version["${CLANG_VERSION}"]}
fi
llvm_tag="llvmorg-${llvm_tag}"
local llvm_targz="${msan_root}/${llvm_tag}.tar.gz"
if [ ! -f "${llvm_targz}" ]; then
curl -L --show-error -o "${llvm_targz}" \
"https://github.com/llvm/llvm-project/archive/${llvm_tag}.tar.gz"
fi
tar -C "${tmpdir}" -zxf "${llvm_targz}"
llvm_root="${tmpdir}/llvm-project-${llvm_tag}"
fi
local msan_prefix="${msan_root}/${CLANG_VERSION}"
rm -rf "${msan_prefix}"
local TARGET_OPTS=""
if [[ -n "${BUILD_TARGET}" ]]; then
TARGET_OPTS=" \
-DCMAKE_C_COMPILER_TARGET=\"${BUILD_TARGET}\" \
-DCMAKE_CXX_COMPILER_TARGET=\"${BUILD_TARGET}\" \
-DCMAKE_SYSTEM_PROCESSOR=\"${BUILD_TARGET%%-*}\" \
"
fi
local build_dir="${tmpdir}/build-llvm"
mkdir -p "${build_dir}"
cd ${llvm_root}
cmake -B"${build_dir}" \
-G Ninja \
-S runtimes \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_USE_SANITIZER=Memory \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" \
-DLIBCXXABI_ENABLE_SHARED=ON \
-DLIBCXXABI_ENABLE_STATIC=OFF \
-DLIBCXX_ENABLE_SHARED=ON \
-DLIBCXX_ENABLE_STATIC=OFF \
-DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS}" \
-DCMAKE_C_FLAGS="${CMAKE_C_FLAGS}" \
-DCMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS}" \
-DCMAKE_SHARED_LINKER_FLAGS="${CMAKE_SHARED_LINKER_FLAGS}" \
-DCMAKE_INSTALL_PREFIX="${msan_prefix}" \
-DLLVM_PATH="${llvm_root}/llvm" \
-DLLVM_CONFIG_PATH="$(which llvm-config-${CLANG_VERSION} llvm-config | head -n1)" \
${TARGET_OPTS}
cmake --build "${build_dir}"
ninja -C "${build_dir}" install
}
# Internal build step shared between all cmd_ossfuzz_* commands.
_cmd_ossfuzz() {
local sanitizer="$1"
shift
mkdir -p "${BUILD_DIR}"
local real_build_dir=$(realpath "${BUILD_DIR}")
# oss-fuzz defines three directories:
# * /work, with the working directory to do re-builds
# * /src, with the source code to build
# * /out, with the output directory where to copy over the built files.
# We use $BUILD_DIR as the /work and the script directory as the /src. The
# /out directory is ignored as developers are used to look for the fuzzers in
# $BUILD_DIR/tools/ directly.
if [[ "${sanitizer}" = "memory" && ! -d "${BUILD_DIR}/msan" ]]; then
sudo docker run --rm -i \
--user $(id -u):$(id -g) \
-v "${real_build_dir}":/work \
gcr.io/oss-fuzz-base/msan-libs-builder \
bash -c "cp -r /msan /work"
fi
# Args passed to ninja. These will be evaluated as a string separated by
# spaces.
local jpegxl_extra_args="$@"
sudo docker run --rm -i \
-e JPEGXL_UID=$(id -u) \
-e JPEGXL_GID=$(id -g) \
-e FUZZING_ENGINE="${FUZZING_ENGINE:-libfuzzer}" \
-e SANITIZER="${sanitizer}" \
-e ARCHITECTURE=x86_64 \
-e FUZZING_LANGUAGE=c++ \
-e MSAN_LIBS_PATH="/work/msan" \
-e JPEGXL_EXTRA_ARGS="${jpegxl_extra_args}" \
-v "${MYDIR}":/src/libjxl \
-v "${MYDIR}/tools/scripts/ossfuzz-build.sh":/src/build.sh \
-v "${real_build_dir}":/work \
gcr.io/oss-fuzz/libjxl
}
cmd_ossfuzz_asan() {
_cmd_ossfuzz address "$@"
}
cmd_ossfuzz_msan() {
_cmd_ossfuzz memory "$@"
}
cmd_ossfuzz_ubsan() {
_cmd_ossfuzz undefined "$@"
}
cmd_ossfuzz_ninja() {
[[ -e "${BUILD_DIR}/build.ninja" ]]
local real_build_dir=$(realpath "${BUILD_DIR}")
if [[ -e "${BUILD_DIR}/msan" ]]; then
echo "ossfuzz_ninja doesn't work with msan builds. Use ossfuzz_msan." >&2
exit 1
fi
sudo docker run --rm -i \
--user $(id -u):$(id -g) \
-v "${MYDIR}":/src/libjxl \
-v "${real_build_dir}":/work \
gcr.io/oss-fuzz/libjxl \
ninja -C /work "$@"
}
cmd_fast_benchmark() {
local small_corpus_tar="${BENCHMARK_CORPORA}/jyrki-full.tar"
local small_corpus_url="https://storage.googleapis.com/artifacts.jpegxl.appspot.com/corpora/jyrki-full.tar"
mkdir -p "${BENCHMARK_CORPORA}"
if [ -f "${small_corpus_tar}" ]; then
curl --show-error -o "${small_corpus_tar}" -z "${small_corpus_tar}" "${small_corpus_url}"
else
curl --show-error -o "${small_corpus_tar}" "${small_corpus_url}"
fi
local tmpdir=$(mktemp -d)
CLEANUP_FILES+=("${tmpdir}")
tar -xf "${small_corpus_tar}" -C "${tmpdir}"
run_benchmark "${tmpdir}" 1048576
}
cmd_benchmark() {
local nikon_corpus_tar="${BENCHMARK_CORPORA}/nikon-subset.tar"
mkdir -p "${BENCHMARK_CORPORA}"
curl --show-error -o "${nikon_corpus_tar}" -z "${nikon_corpus_tar}" \
"https://storage.googleapis.com/artifacts.jpegxl.appspot.com/corpora/nikon-subset.tar"
local tmpdir=$(mktemp -d)
CLEANUP_FILES+=("${tmpdir}")
tar -xvf "${nikon_corpus_tar}" -C "${tmpdir}"
local sem_id="jpegxl_benchmark-$$"
local nprocs=$(nproc --all || echo 1)
images=()
local filename
while IFS= read -r filename; do
# This removes the './'
filename="${filename:2}"
local mode
if [[ "${filename:0:4}" == "srgb" ]]; then
mode="RGB_D65_SRG_Rel_SRG"
elif [[ "${filename:0:5}" == "adobe" ]]; then
mode="RGB_D65_Ado_Rel_Ado"
else
echo "Unknown image colorspace: ${filename}" >&2
exit 1
fi
png_filename="${filename%.ppm}.png"
png_filename=$(echo "${png_filename}" | tr '/' '_')
sem --bg --id "${sem_id}" -j"${nprocs}" -- \
"${TOOLS_DIR}/decode_and_encode" \
"${tmpdir}/${filename}" "${mode}" "${tmpdir}/${png_filename}"
images+=( "${png_filename}" )
done < <(cd "${tmpdir}"; ${FIND_BIN} . -name '*.ppm' -type f)
sem --id "${sem_id}" --wait
# We need about 10 GiB per thread on these images.
run_benchmark "${tmpdir}" 10485760
}
get_mem_available() {
if [[ "${OS}" == "Darwin" ]]; then
echo $(vm_stat | grep -F 'Pages free:' | awk '{print $3 * 4}')
elif [[ "${OS}" == MINGW* ]]; then
echo $(vmstat | tail -n 1 | awk '{print $4 * 4}')
else
echo $(grep -F MemAvailable: /proc/meminfo | awk '{print $2}')
fi
}
run_benchmark() {
local src_img_dir="$1"
local mem_per_thread="${2:-10485760}"
local output_dir="${BUILD_DIR}/benchmark_results"
mkdir -p "${output_dir}"
if [[ "${OS}" == MINGW* ]]; then
src_img_dir=`cygpath -w "${src_img_dir}"`
fi
local num_threads=1
if [[ ${BENCHMARK_NUM_THREADS} -gt 0 ]]; then
num_threads=${BENCHMARK_NUM_THREADS}
else
# The memory available at the beginning of the benchmark run in kB. The number
# of threads depends on the available memory, and the passed memory per
# thread. We also add a 2 GiB of constant memory.
local mem_available="$(get_mem_available)"
# Check that we actually have a MemAvailable value.
[[ -n "${mem_available}" ]]
num_threads=$(( (${mem_available} - 1048576) / ${mem_per_thread} ))
if [[ ${num_threads} -le 0 ]]; then
num_threads=1
fi
fi
local benchmark_args=(
--input "${src_img_dir}/*.png"
--codec=jpeg:yuv420:q85,webp:q80,jxl:d1:6,jxl:d1:6:downsampling=8,jxl:d5:6,jxl:d5:6:downsampling=8,jxl:m:d0:2,jxl:m:d0:3,jxl:m:d2:2
--output_dir "${output_dir}"
--show_progress
--num_threads="${num_threads}"
--decode_reps=11
--encode_reps=11
)
if [[ "${STORE_IMAGES}" == "1" ]]; then
benchmark_args+=(--save_decompressed --save_compressed)
fi
(
[[ "${TEST_STACK_LIMIT}" == "none" ]] || ulimit -s "${TEST_STACK_LIMIT}"
"${TOOLS_DIR}/benchmark_xl" "${benchmark_args[@]}" | \
tee "${output_dir}/results.txt"
# Check error code for benchmark_xl command. This will exit if not.
return ${PIPESTATUS[0]}
)
}
# Helper function to wait for the CPU temperature to cool down on ARM.
wait_for_temp() {
{ set +x; } 2>/dev/null
local temp_limit=${1:-38000}
if [[ -z "${THERMAL_FILE:-}" ]]; then
echo "Must define the THERMAL_FILE with the thermal_zoneX/temp file" \
"to read the temperature from. This is normally set in the runner." >&2
exit 1
fi
local org_temp=$(cat "${THERMAL_FILE}")
if [[ "${org_temp}" -ge "${temp_limit}" ]]; then
echo -n "Waiting for temp to get down from ${org_temp}... "
fi
local temp="${org_temp}"
local secs=0
while [[ "${temp}" -ge "${temp_limit}" ]]; do
sleep 1
temp=$(cat "${THERMAL_FILE}")
echo -n "${temp} "
secs=$((secs + 1))
if [[ ${secs} -ge 5 ]]; then
break
fi
done
if [[ "${org_temp}" -ge "${temp_limit}" ]]; then
echo "Done, temp=${temp}"
fi
set -x
}
# Helper function to set the cpuset restriction of the current process.
cmd_cpuset() {
[[ "${SKIP_CPUSET:-}" != "1" ]] || return 0
local newset="$1"
local mycpuset=$(cat /proc/self/cpuset)
mycpuset="/dev/cpuset${mycpuset}"
# Check that the directory exists:
[[ -d "${mycpuset}" ]]
if [[ -e "${mycpuset}/cpuset.cpus" ]]; then
echo "${newset}" >"${mycpuset}/cpuset.cpus"
else
echo "${newset}" >"${mycpuset}/cpus"
fi
}
# Return the encoding/decoding speed from the Stats output.
_speed_from_output() {
local speed="$1"
local unit="${2:-MP/s}"
if [[ "${speed}" == *"${unit}"* ]]; then
speed="${speed%% ${unit}*}"
speed="${speed##* }"
echo "${speed}"