Imported Upstream version 1.6.40
[platform/upstream/libpng.git] / ci / ci_verify_makefiles.sh
1 #!/usr/bin/env bash
2 set -e
3
4 # ci_verify_makefiles.sh
5 # Continuously integrate libpng using the legacy makefiles.
6 #
7 # Copyright (c) 2019-2023 Cosmin Truta.
8 #
9 # This software is released under the libpng license.
10 # For conditions of distribution and use, see the disclaimer
11 # and license in png.h.
12
13 CI_SCRIPTNAME="$(basename "$0")"
14 CI_SCRIPTDIR="$(cd "$(dirname "$0")" && pwd)"
15 CI_SRCDIR="$(dirname "$CI_SCRIPTDIR")"
16
17 function ci_info {
18     printf >&2 "%s: %s\\n" "$CI_SCRIPTNAME" "$*"
19 }
20
21 function ci_err {
22     printf >&2 "%s: error: %s\\n" "$CI_SCRIPTNAME" "$*"
23     exit 2
24 }
25
26 function ci_spawn {
27     printf >&2 "%s: executing:" "$CI_SCRIPTNAME"
28     printf >&2 " %q" "$@"
29     printf >&2 "\\n"
30     "$@"
31 }
32
33 function ci_init_makefiles_build {
34     CI_SYSTEM_NAME="$(uname -s)"
35     CI_MACHINE_NAME="$(uname -m)"
36     CI_MAKE="${CI_MAKE:-make}"
37     case "$CI_CC" in
38     ( *clang* )
39         CI_MAKEFILES="${CI_MAKEFILES:-"scripts/makefile.clang"}" ;;
40     ( *gcc* )
41         CI_MAKEFILES="${CI_MAKEFILES:-"scripts/makefile.gcc"}" ;;
42     ( * )
43         CI_MAKEFILES="${CI_MAKEFILES:-"scripts/makefile.std"}" ;;
44     esac
45 }
46
47 function ci_trace_makefiles_build {
48     ci_info "## START OF CONFIGURATION ##"
49     ci_info "system name: $CI_SYSTEM_NAME"
50     ci_info "machine hardware name: $CI_MACHINE_NAME"
51     ci_info "source directory: $CI_SRCDIR"
52     ci_info "environment option: \$CI_MAKEFILES: '$CI_MAKEFILES'"
53     ci_info "environment option: \$CI_MAKE: '$CI_MAKE'"
54     ci_info "environment option: \$CI_MAKE_FLAGS: '$CI_MAKE_FLAGS'"
55     ci_info "environment option: \$CI_MAKE_VARS: '$CI_MAKE_VARS'"
56     ci_info "environment option: \$CI_CC: '$CI_CC'"
57     ci_info "environment option: \$CI_CC_FLAGS: '$CI_CC_FLAGS'"
58     ci_info "environment option: \$CI_CPP: '$CI_CPP'"
59     ci_info "environment option: \$CI_CPP_FLAGS: '$CI_CPP_FLAGS'"
60     ci_info "environment option: \$CI_AR: '$CI_AR'"
61     ci_info "environment option: \$CI_RANLIB: '$CI_RANLIB'"
62     ci_info "environment option: \$CI_LD: '$CI_LD'"
63     ci_info "environment option: \$CI_LD_FLAGS: '$CI_LD_FLAGS'"
64     ci_info "environment option: \$CI_LIBS: '$CI_LIBS'"
65     ci_info "environment option: \$CI_SANITIZERS: '$CI_SANITIZERS'"
66     ci_info "environment option: \$CI_NO_TEST: '$CI_NO_TEST'"
67     ci_info "environment option: \$CI_NO_CLEAN: '$CI_NO_CLEAN'"
68     ci_info "executable: \$CI_MAKE: $(command -V "$CI_MAKE")"
69     [[ $CI_CC ]] &&
70         ci_info "executable: \$CI_CC: $(command -V "$CI_CC")"
71     [[ $CI_CPP ]] &&
72         ci_info "executable: \$CI_CPP: $(command -V "$CI_CPP")"
73     [[ $CI_AR ]] &&
74         ci_info "executable: \$CI_AR: $(command -V "$CI_AR")"
75     [[ $CI_RANLIB ]] &&
76         ci_info "executable: \$CI_RANLIB: $(command -V "$CI_RANLIB")"
77     [[ $CI_LD ]] &&
78         ci_info "executable: \$CI_LD: $(command -V "$CI_LD")"
79     ci_info "## END OF CONFIGURATION ##"
80 }
81
82 function ci_cleanup_old_makefiles_build {
83     # Any old makefile-based build will most likely leave a mess
84     # of object files behind if interrupted, e.g., via Ctrl+C.
85     # There may be other files behind, depending on what makefile
86     # had been used. We cannot easily enumerate all of those.
87     # Fortunately, for a clean makefiles-based build, it should be
88     # sufficient to remove the old object files only.
89     [[ -z $(find "$CI_SRCDIR" -maxdepth 1 -name "*.o") ]] ||
90         ci_spawn rm -f "$CI_SRCDIR"/*.o
91     [[ -z $(find "$CI_SRCDIR" -maxdepth 1 -name "*.obj") ]] ||
92         ci_spawn rm -f "$CI_SRCDIR"/*.obj
93 }
94
95 function ci_build_makefiles {
96     ci_info "## START OF BUILD ##"
97     # Initialize ALL_CC_FLAGS and ALL_LD_FLAGS as strings.
98     local ALL_CC_FLAGS="$CI_CC_FLAGS"
99     local ALL_LD_FLAGS="$CI_LD_FLAGS"
100     [[ $CI_SANITIZERS ]] && {
101         ALL_CC_FLAGS="-fsanitize=$CI_SANITIZERS ${ALL_CC_FLAGS:-"-O2"}"
102         ALL_LD_FLAGS="-fsanitize=$CI_SANITIZERS $ALL_LD_FLAGS"
103     }
104     # Initialize ALL_MAKE_FLAGS and ALL_MAKE_VARS as arrays.
105     local ALL_MAKE_FLAGS=($CI_MAKE_FLAGS)
106     local ALL_MAKE_VARS=()
107     [[ $CI_CC ]] && ALL_MAKE_VARS+=(CC="$CI_CC")
108     [[ $ALL_CC_FLAGS ]] && ALL_MAKE_VARS+=(CFLAGS="$ALL_CC_FLAGS")
109     [[ $CI_CPP ]] && ALL_MAKE_VARS+=(CPP="$CI_CPP")
110     [[ $CI_CPP_FLAGS ]] && ALL_MAKE_VARS+=(CPPFLAGS="$CI_CPP_FLAGS")
111     [[ $CI_AR ]] && ALL_MAKE_VARS+=(
112         AR="${CI_AR:-ar}"
113         AR_RC="${CI_AR:-ar} rc"
114     )
115     [[ $CI_RANLIB ]] && ALL_MAKE_VARS+=(RANLIB="$CI_RANLIB")
116     [[ $CI_LD ]] && ALL_MAKE_VARS+=(LD="$CI_LD")
117     [[ $ALL_LD_FLAGS ]] && ALL_MAKE_VARS+=(LDFLAGS="$ALL_LD_FLAGS")
118     [[ $CI_LIBS ]] && ALL_MAKE_VARS+=(LIBS="$CI_LIBS")
119     ALL_MAKE_VARS+=($CI_MAKE_VARS)
120     # Build!
121     cd "$CI_SRCDIR"
122     local MY_MAKEFILE
123     for MY_MAKEFILE in $CI_MAKEFILES
124     do
125         ci_info "using makefile: $MY_MAKEFILE"
126         ci_spawn "$CI_MAKE" -f "$MY_MAKEFILE" \
127                             "${ALL_MAKE_FLAGS[@]}" \
128                             "${ALL_MAKE_VARS[@]}"
129         [[ $CI_NO_TEST ]] ||
130             ci_spawn "$CI_MAKE" -f "$MY_MAKEFILE" \
131                                 "${ALL_MAKE_FLAGS[@]}" \
132                                 "${ALL_MAKE_VARS[@]}" \
133                                 test
134         [[ $CI_NO_CLEAN ]] ||
135             ci_spawn "$CI_MAKE" -f "$MY_MAKEFILE" \
136                                 "${ALL_MAKE_FLAGS[@]}" \
137                                 "${ALL_MAKE_VARS[@]}" \
138                                 clean
139     done
140     ci_info "## END OF BUILD ##"
141 }
142
143 function main {
144     [[ $# -eq 0 ]] || {
145         ci_info "note: this program accepts environment options only"
146         ci_err "unexpected command arguments: '$*'"
147     }
148     ci_init_makefiles_build
149     ci_trace_makefiles_build
150     ci_cleanup_old_makefiles_build
151     ci_build_makefiles
152 }
153
154 main "$@"