github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/tini/ci/run_build.sh (about) 1 #!/bin/bash 2 # Should be run from the root dir, or SOURCE_DIR should be set. 3 set -o errexit 4 set -o nounset 5 set -o pipefail 6 7 # Default compiler 8 : ${CC:="gcc"} 9 10 echo "CC=${CC}" 11 12 # Paths 13 : ${SOURCE_DIR:="."} 14 : ${DIST_DIR:="${SOURCE_DIR}/dist"} 15 : ${BUILD_DIR:="/tmp/build"} 16 17 # GPG Configuration 18 : ${GPG_PASSPHRASE:=""} 19 20 21 # Make those paths absolute, and export them for the Python tests to consume. 22 export SOURCE_DIR="$(readlink -f "${SOURCE_DIR}")" 23 export DIST_DIR="$(readlink -f "${DIST_DIR}")" 24 export BUILD_DIR="$(readlink -f "${BUILD_DIR}")" 25 26 # Configuration 27 : ${FORCE_SUBREAPER:="1"} 28 export FORCE_SUBREAPER 29 30 31 # Our build platform doesn't have those newer Linux flags, but we want Tini to have subreaper support 32 # We also use those in our tests 33 CFLAGS="-DPR_SET_CHILD_SUBREAPER=36 -DPR_GET_CHILD_SUBREAPER=37" 34 if [[ "${FORCE_SUBREAPER}" -eq 1 ]]; then 35 # If FORCE_SUBREAPER is requested, then we set those CFLAGS for the Tini build 36 export CFLAGS 37 fi 38 39 # Ensure Python output is not buffered (to make tests output clearer) 40 export PYTHONUNBUFFERED=1 41 42 # Set path to prioritize our utils 43 export REAL_PATH="${PATH}" 44 export PATH="${SOURCE_DIR}/ci/util:${PATH}" 45 46 # Build 47 CMAKE_ARGS=(-B"${BUILD_DIR}" -H"${SOURCE_DIR}") 48 if [[ -n "${MINIMAL:-}" ]]; then 49 CMAKE_ARGS+=(-DMINIMAL=ON) 50 fi 51 cmake "${CMAKE_ARGS[@]}" 52 53 pushd "${BUILD_DIR}" 54 make clean 55 make 56 if [[ -n "${ARCH_NATIVE:=}" ]]; then 57 make package 58 fi 59 popd 60 61 pkg_version="$(cat "${BUILD_DIR}/VERSION")" 62 63 64 if [[ -n "${ARCH_NATIVE:=}" ]]; then 65 echo "Built native package (ARCH_NATIVE=${ARCH_NATIVE})" 66 echo "Running smoke and internal tests" 67 68 BIN_TEST_DIR="${BUILD_DIR}/bin-test" 69 mkdir -p "$BIN_TEST_DIR" 70 export PATH="${BIN_TEST_DIR}:${PATH}" 71 72 # Smoke tests (actual tests need Docker to run; they don't run within the CI environment) 73 for tini in "${BUILD_DIR}/tini" "${BUILD_DIR}/tini-static"; do 74 echo "Smoke test for ${tini}" 75 "$tini" --version 76 77 echo "Testing ${tini} --version" 78 "$tini" --version | grep -q "tini version" 79 80 echo "Testing ${tini} without arguments exits with 1" 81 ! "$tini" 2>/dev/null 82 83 echo "Testing ${tini} shows help message" 84 { 85 ! "$tini" 2>&1 86 } | grep -q "supervision of a valid init process" 87 88 if [[ -n "${MINIMAL:-}" ]]; then 89 echo "Testing $tini with: true" 90 "${tini}" true 91 92 echo "Testing $tini with: false" 93 if "${tini}" false; then 94 exit 1 95 fi 96 97 echo "Testing ${tini} does not reference options that don't exist" 98 ! { 99 ! "$tini" 2>&1 100 } | grep -q "more verbose" 101 102 # We try running binaries named after flags (both valid and invalid 103 # flags) and test that they run. 104 for flag in h s x; do 105 bin="-${flag}" 106 echo "Testing $tini can run binary: ${bin}" 107 cp "$(which true)" "${BIN_TEST_DIR}/${bin}" 108 "$tini" "$bin" 109 done 110 111 echo "Testing $tini can run binary --version if args are given" 112 cp "$(which true)" "${BIN_TEST_DIR}/--version" 113 if "$tini" "--version" --foo | grep -q "tini version"; then 114 exit 1 115 fi 116 else 117 echo "Testing ${tini} -h" 118 "${tini}" -h 119 120 echo "Testing $tini for license" 121 "$tini" -l | diff - "${SOURCE_DIR}/LICENSE" 122 123 echo "Testing $tini with: true" 124 "${tini}" -vvv true 125 126 echo "Testing $tini with: false" 127 if "${tini}" -vvv false; then 128 exit 1 129 fi 130 131 echo "Testing ${tini} references options that exist" 132 { 133 ! "$tini" 2>&1 134 } | grep -q "more verbose" 135 fi 136 137 echo "Testing ${tini} supports TINI_VERBOSITY" 138 TINI_VERBOSITY=3 "$tini" true 2>&1 | grep -q 'Received SIGCHLD' 139 140 echo "Testing ${tini} exits with 127 if the command does not exist" 141 "$tini" foobar123 && rc="$?" || rc="$?" 142 if [[ "$rc" != 127 ]]; then 143 echo "Exit code was: ${rc}" 144 exit 1 145 fi 146 147 echo "Testing ${tini} exits with 126 if the command is not executable" 148 "$tini" /etc && rc="$?" || rc="$?" 149 if [[ "$rc" != 126 ]]; then 150 echo "Exit code was: ${rc}" 151 exit 1 152 fi 153 154 # Test stdin / stdout are handed over to child 155 echo "Testing ${tini} does not break pipes" 156 echo "exit 0" | "${tini}" sh 157 if [[ ! "$?" -eq "0" ]]; then 158 echo "Pipe test failed" 159 exit 1 160 fi 161 162 echo "Checking hardening on $tini" 163 hardening-check --nopie --nostackprotector --nobindnow "${tini}" 164 done 165 166 # Quick package audit 167 if which rpm >/dev/null; then 168 echo "Contents for RPM:" 169 rpm -qlp "${BUILD_DIR}/tini_${pkg_version}.rpm" 170 echo "--" 171 fi 172 173 if which dpkg >/dev/null; then 174 echo "Contents for DEB:" 175 dpkg --contents "${BUILD_DIR}/tini_${pkg_version}.deb" 176 echo "--" 177 fi 178 179 # Compile test code 180 "${CC}" -o "${BUILD_DIR}/sigconf-test" "${SOURCE_DIR}/test/sigconf/sigconf-test.c" 181 182 # Create virtual environment to run tests. 183 # Accept system site packages for faster local builds. 184 VENV="${BUILD_DIR}/venv" 185 virtualenv --system-site-packages "${VENV}" 186 187 # Don't use activate because it does not play nice with nounset 188 export PATH="${VENV}/bin:${PATH}" 189 export CFLAGS # We need them to build our test suite, regardless of FORCE_SUBREAPER 190 191 # Install test dependencies 192 pip install psutil python-prctl bitmap 193 194 # Run tests 195 python "${SOURCE_DIR}/test/run_inner_tests.py" 196 else 197 if [[ ! -n "${ARCH_SUFFIX:=}" ]]; then 198 echo "Built cross package, but $ARCH_SUFFIX is empty!" 199 exit 1 200 fi 201 echo "Built cross package (ARCH_SUFFIX=${ARCH_SUFFIX})" 202 echo "Skipping smoke and internal tests" 203 fi 204 205 # Now, copy over files to DIST_DIR, with appropriate names depending on the 206 # architecture. 207 # Handle the DEB / RPM 208 mkdir -p "${DIST_DIR}" 209 210 TINIS=() 211 212 for tini in tini tini-static; do 213 if [[ -n "${ARCH_SUFFIX:=}" ]]; then 214 to="${DIST_DIR}/${tini}-${ARCH_SUFFIX}" 215 TINIS+=("$to") 216 cp "${BUILD_DIR}/${tini}" "$to" 217 fi 218 219 if [[ -n "${ARCH_NATIVE:=}" ]]; then 220 to="${DIST_DIR}/${tini}" 221 TINIS+=("$to") 222 cp "${BUILD_DIR}/${tini}" "$to" 223 fi 224 done 225 226 if [[ -n "${ARCH_NATIVE:=}" ]]; then 227 for pkg_format in deb rpm; do 228 src="${BUILD_DIR}/tini_${pkg_version}.${pkg_format}" 229 230 if [[ -n "${ARCH_SUFFIX:=}" ]]; then 231 to="${DIST_DIR}/tini_${pkg_version}-${ARCH_SUFFIX}.${pkg_format}" 232 TINIS+=("$to") 233 cp "$src" "$to" 234 fi 235 236 if [[ -n "${ARCH_NATIVE:=}" ]]; then 237 to="${DIST_DIR}/tini_${pkg_version}.${pkg_format}" 238 TINIS+=("$to") 239 cp "$src" "$to" 240 fi 241 done 242 fi 243 244 echo "Tinis: ${TINIS[*]}" 245 246 for tini in "${TINIS[@]}"; do 247 echo "${tini}:" 248 sha1sum "$tini" 249 file "$tini" 250 echo "--" 251 done 252 253 # If a signing key and passphrase are made available, then use it to sign the 254 # binaries 255 if [[ -n "$GPG_PASSPHRASE" ]] && [[ -f "${SOURCE_DIR}/sign.key" ]]; then 256 echo "Signing tinis" 257 GPG_SIGN_HOMEDIR="${BUILD_DIR}/gpg-sign" 258 GPG_VERIFY_HOMEDIR="${BUILD_DIR}/gpg-verify" 259 PGP_KEY_FINGERPRINT="595E85A6B1B4779EA4DAAEC70B588DFF0527A9B7" 260 PGP_KEYSERVER="ha.pool.sks-keyservers.net" 261 262 mkdir "${GPG_SIGN_HOMEDIR}" "${GPG_VERIFY_HOMEDIR}" 263 chmod 700 "${GPG_SIGN_HOMEDIR}" "${GPG_VERIFY_HOMEDIR}" 264 265 gpg --homedir "${GPG_SIGN_HOMEDIR}" --import "${SOURCE_DIR}/sign.key" 266 gpg --homedir "${GPG_VERIFY_HOMEDIR}" --keyserver "$PGP_KEYSERVER" --recv-keys "$PGP_KEY_FINGERPRINT" 267 268 for tini in "${TINIS[@]}"; do 269 echo "${GPG_PASSPHRASE}" | gpg --homedir "${GPG_SIGN_HOMEDIR}" --passphrase-fd 0 --armor --detach-sign "${tini}" 270 gpg --homedir "${GPG_VERIFY_HOMEDIR}" --verify "${tini}.asc" 271 done 272 fi