ci_verify_configure.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env bash
  2. set -o errexit -o pipefail -o posix
  3. # Copyright (c) 2019-2025 Cosmin Truta.
  4. #
  5. # Use, modification and distribution are subject to the MIT License.
  6. # Please see the accompanying file LICENSE_MIT.txt
  7. #
  8. # SPDX-License-Identifier: MIT
  9. # shellcheck source=ci/lib/ci.lib.sh
  10. source "$(dirname "$0")/lib/ci.lib.sh"
  11. cd "$CI_TOPLEVEL_DIR"
  12. CI_SRC_DIR="$CI_TOPLEVEL_DIR"
  13. CI_OUT_DIR="$CI_TOPLEVEL_DIR/out"
  14. CI_BUILD_DIR="$CI_OUT_DIR/ci_verify_configure.$CI_TARGET_SYSTEM.$CI_TARGET_ARCH.build"
  15. CI_INSTALL_DIR="$CI_OUT_DIR/ci_verify_configure.$CI_TARGET_SYSTEM.$CI_TARGET_ARCH.install"
  16. function ci_init_build {
  17. # Ensure that the mandatory variables are initialized.
  18. CI_MAKE="${CI_MAKE:-make}"
  19. [[ "$CI_TARGET_SYSTEM.$CI_TARGET_ARCH" != "$CI_BUILD_SYSTEM.$CI_BUILD_ARCH" ]] || {
  20. # For native builds, set CI_CC to "cc" by default if the cc command is available.
  21. # The configure script defaults CC to "gcc", which is not always a good idea.
  22. [[ -x $(command -v cc) ]] && CI_CC="${CI_CC:-cc}"
  23. }
  24. # Ensure that the CI_ variables that cannot be customized reliably are not initialized.
  25. [[ ! $CI_CONFIGURE_VARS ]] || {
  26. ci_err "unsupported: \$CI_CONFIGURE_VARS='$CI_CONFIGURE_VARS'"
  27. }
  28. [[ ! $CI_MAKE_VARS ]] || {
  29. ci_err "unsupported: \$CI_MAKE_VARS='$CI_MAKE_VARS'"
  30. }
  31. }
  32. function ci_trace_build {
  33. ci_info "## START OF CONFIGURATION ##"
  34. ci_info "build arch: $CI_BUILD_ARCH"
  35. ci_info "build system: $CI_BUILD_SYSTEM"
  36. [[ "$CI_TARGET_SYSTEM.$CI_TARGET_ARCH" != "$CI_BUILD_SYSTEM.$CI_BUILD_ARCH" ]] && {
  37. ci_info "target arch: $CI_TARGET_ARCH"
  38. ci_info "target system: $CI_TARGET_SYSTEM"
  39. }
  40. ci_info "source directory: $CI_SRC_DIR"
  41. ci_info "build directory: $CI_BUILD_DIR"
  42. ci_info "install directory: $CI_INSTALL_DIR"
  43. ci_info "environment option: \$CI_CONFIGURE_FLAGS: '$CI_CONFIGURE_FLAGS'"
  44. ci_info "environment option: \$CI_MAKE: '$CI_MAKE'"
  45. ci_info "environment option: \$CI_MAKE_FLAGS: '$CI_MAKE_FLAGS'"
  46. ci_info "environment option: \$CI_CC: '$CI_CC'"
  47. ci_info "environment option: \$CI_CC_FLAGS: '$CI_CC_FLAGS'"
  48. ci_info "environment option: \$CI_CPP: '$CI_CPP'"
  49. ci_info "environment option: \$CI_CPP_FLAGS: '$CI_CPP_FLAGS'"
  50. ci_info "environment option: \$CI_AR: '$CI_AR'"
  51. ci_info "environment option: \$CI_RANLIB: '$CI_RANLIB'"
  52. ci_info "environment option: \$CI_LD: '$CI_LD'"
  53. ci_info "environment option: \$CI_LD_FLAGS: '$CI_LD_FLAGS'"
  54. ci_info "environment option: \$CI_SANITIZERS: '$CI_SANITIZERS'"
  55. ci_info "environment option: \$CI_FORCE: '$CI_FORCE'"
  56. ci_info "environment option: \$CI_NO_BUILD: '$CI_NO_BUILD'"
  57. ci_info "environment option: \$CI_NO_TEST: '$CI_NO_TEST'"
  58. ci_info "environment option: \$CI_NO_INSTALL: '$CI_NO_INSTALL'"
  59. ci_info "environment option: \$CI_NO_CLEAN: '$CI_NO_CLEAN'"
  60. ci_info "executable: \$CI_MAKE: $(command -V "$CI_MAKE")"
  61. [[ $CI_CC ]] && {
  62. ci_info "executable: \$CI_CC: $(command -V "$CI_CC")"
  63. }
  64. [[ $CI_CPP ]] && {
  65. ci_info "executable: \$CI_CPP: $(command -V "$CI_CPP")"
  66. }
  67. [[ $CI_AR ]] && {
  68. ci_info "executable: \$CI_AR: $(command -V "$CI_AR")"
  69. }
  70. [[ $CI_RANLIB ]] && {
  71. ci_info "executable: \$CI_RANLIB: $(command -V "$CI_RANLIB")"
  72. }
  73. [[ $CI_LD ]] && {
  74. ci_info "executable: \$CI_LD: $(command -V "$CI_LD")"
  75. }
  76. ci_info "## END OF CONFIGURATION ##"
  77. }
  78. function ci_cleanup_old_build {
  79. ci_info "## START OF PRE-BUILD CLEANUP ##"
  80. [[ ! -e $CI_BUILD_DIR && ! -e $CI_INSTALL_DIR ]] || {
  81. ci_spawn rm -fr "$CI_BUILD_DIR"
  82. ci_spawn rm -fr "$CI_INSTALL_DIR"
  83. }
  84. [[ ! -e "$CI_SRC_DIR/config.status" ]] || {
  85. ci_warn "unexpected build configuration file: '$CI_SRC_DIR/config.status'"
  86. if ci_expr $((CI_FORCE))
  87. then
  88. # Delete the old config and (possibly) the old build.
  89. ci_info "note: forcing an in-tree build cleanup"
  90. if [[ -f $CI_SRC_DIR/Makefile ]]
  91. then
  92. ci_spawn make -C "$CI_SRC_DIR" distclean
  93. else
  94. ci_spawn rm -fr "$CI_SRC_DIR"/config.{log,status}
  95. fi
  96. else
  97. # Alert the user, but do not delete their files.
  98. ci_warn "the configure script might fail"
  99. ci_info "hint: consider using the option \$CI_FORCE=1"
  100. fi
  101. }
  102. ci_info "## END OF PRE-BUILD CLEANUP ##"
  103. }
  104. function ci_build {
  105. ci_info "## START OF BUILD ##"
  106. # Export the configure build environment.
  107. [[ $CI_CC ]] && ci_spawn export CC="$CI_CC"
  108. [[ $CI_CC_FLAGS ]] && ci_spawn export CFLAGS="$CI_CC_FLAGS"
  109. [[ $CI_CPP ]] && ci_spawn export CPP="$CI_CPP"
  110. [[ $CI_CPP_FLAGS ]] && ci_spawn export CPPFLAGS="$CI_CPP_FLAGS"
  111. [[ $CI_AR ]] && ci_spawn export AR="$CI_AR"
  112. [[ $CI_RANLIB ]] && ci_spawn export RANLIB="$CI_RANLIB"
  113. [[ $CI_LD ]] && ci_spawn export LD="$CI_LD"
  114. [[ $CI_LD_FLAGS ]] && ci_spawn export LDFLAGS="$CI_LD_FLAGS"
  115. [[ $CI_SANITIZERS ]] && {
  116. ci_spawn export CFLAGS="${CFLAGS:-"-O2"} -fsanitize=$CI_SANITIZERS"
  117. ci_spawn export LDFLAGS="${LDFLAGS}${LDFLAGS:+" "}-fsanitize=$CI_SANITIZERS"
  118. }
  119. # Spawn "autogen.sh" if the configure script is not available.
  120. [[ -x "$CI_SRC_DIR/configure" ]] || {
  121. ci_spawn "$CI_SRC_DIR/autogen.sh" --maintainer
  122. }
  123. # And... build!
  124. ci_spawn mkdir -p "$CI_BUILD_DIR"
  125. ci_spawn cd "$CI_BUILD_DIR"
  126. # Spawn "configure".
  127. ci_spawn "$CI_SRC_DIR/configure" --prefix="$CI_INSTALL_DIR" $CI_CONFIGURE_FLAGS
  128. ci_expr $((CI_NO_BUILD)) || {
  129. # Spawn "make".
  130. ci_spawn "$CI_MAKE" $CI_MAKE_FLAGS
  131. }
  132. ci_expr $((CI_NO_TEST)) || {
  133. # Spawn "make test" if testing is not disabled.
  134. ci_spawn "$CI_MAKE" $CI_MAKE_FLAGS test
  135. }
  136. ci_expr $((CI_NO_INSTALL)) || {
  137. # Spawn "make install" if installation is not disabled.
  138. ci_spawn "$CI_MAKE" $CI_MAKE_FLAGS install
  139. }
  140. ci_expr $((CI_NO_CLEAN)) || {
  141. # Spawn "make clean" and "make distclean" if cleaning is not disabled.
  142. ci_spawn "$CI_MAKE" $CI_MAKE_FLAGS clean
  143. ci_spawn "$CI_MAKE" $CI_MAKE_FLAGS distclean
  144. }
  145. ci_info "## END OF BUILD ##"
  146. }
  147. function usage {
  148. echo "usage: $CI_SCRIPT_NAME [<options>]"
  149. echo "options: -?|-h|--help"
  150. exit "${@:-0}"
  151. }
  152. function main {
  153. local opt
  154. while getopts ":" opt
  155. do
  156. # This ain't a while-loop. It only pretends to be.
  157. [[ $1 == -[?h]* || $1 == --help || $1 == --help=* ]] && usage 0
  158. ci_err "unknown option: '$1'"
  159. done
  160. shift $((OPTIND - 1))
  161. # And... go!
  162. ci_init_build
  163. ci_trace_build
  164. [[ $# -eq 0 ]] || {
  165. echo >&2 "error: unexpected argument: '$1'"
  166. echo >&2 "note: this program accepts environment options only"
  167. usage 2
  168. }
  169. ci_cleanup_old_build
  170. ci_build
  171. }
  172. main "$@"