build_support.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/bash
  2. ##############################################################################
  3. # Unix support functions
  4. ##############################################################################
  5. #
  6. # This script file can be included in build scripts for UNIX-compatible
  7. # shells to compose build scripts.
  8. #
  9. ##############################################################################
  10. ## DEFINE UTILITIES ##
  11. if [ -z "${QUIET}" ]; then
  12. status() {
  13. echo -e "${COLOR_BLUE}[${PRODUCT_NAME}] ${1}${COLOR_RESET}"
  14. }
  15. step() {
  16. echo -e "${COLOR_GREEN} + ${1}${COLOR_RESET}"
  17. }
  18. info() {
  19. echo -e "${COLOR_ORANGE} + ${1}${COLOR_RESET}"
  20. }
  21. error() {
  22. echo -e "${COLOR_RED} + ${1}${COLOR_RESET}"
  23. }
  24. else
  25. status() {
  26. :
  27. }
  28. step() {
  29. :
  30. }
  31. info() {
  32. :
  33. }
  34. error() {
  35. echo -e "${COLOR_RED} + ${1}${COLOR_RESET}"
  36. }
  37. fi
  38. exists() {
  39. /usr/bin/command -v "$1" >/dev/null 2>&1
  40. }
  41. ensure_dir() {
  42. [[ -n "${1}" ]] && /bin/mkdir -p "${1}" && builtin cd "${1}"
  43. }
  44. cleanup() {
  45. :
  46. }
  47. caught_error() {
  48. error "ERROR during build step: ${1}"
  49. cleanup
  50. exit 1
  51. }
  52. # Setup build environment
  53. BUILD_DIR="${BUILD_DIR:-build}"
  54. BUILD_CONFIG="${BUILD_CONFIG:-RelWithDebInfo}"
  55. CI_WORKFLOW="${CHECKOUT_DIR}/.github/workflows/main.yml"
  56. CURRENT_ARCH=$(uname -m)
  57. CURRENT_DATE="$(date +"%Y-%m-%d")"
  58. ## Utility functions ##
  59. check_ccache() {
  60. if ccache -V >/dev/null 2>&1; then
  61. info "CCache available"
  62. CMAKE_CCACHE_OPTIONS="-DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_C_COMPILER_LAUNCHER=ccache"
  63. if [ "${CI}" ]; then
  64. ccache --set-config=cache_dir=${GITHUB_WORKSPACE:-${HOME}}/.ccache
  65. ccache --set-config=max_size=${CCACHE_SIZE:-500M}
  66. ccache --set-config=compression=true
  67. ccache -z
  68. fi
  69. else
  70. info "CCache not available"
  71. fi
  72. }
  73. _add_ccache_to_path() {
  74. if [ "${CMAKE_CCACHE_OPTIONS}" ]; then
  75. PATH="/usr/local/opt/ccache/libexec:${PATH}"
  76. status "Compiler Info:"
  77. local IFS=$'\n'
  78. for COMPILER_INFO in $(type cc c++ gcc g++ clang clang++ || true); do
  79. info "${COMPILER_INFO}"
  80. done
  81. fi
  82. }
  83. safe_fetch() {
  84. if [ $# -lt 2 ]; then
  85. error "Usage: safe_fetch URL HASH"
  86. return 1
  87. fi
  88. while true; do
  89. case "${1}" in
  90. -n | --nocontinue ) NOCONTINUE=TRUE; shift ;;
  91. -- ) shift; break ;;
  92. * ) break ;;
  93. esac
  94. done
  95. DOWNLOAD_URL="${1}"
  96. DOWNLOAD_HASH="${2}"
  97. DOWNLOAD_FILE="$(basename ${DOWNLOAD_URL})"
  98. CURLCMD=${CURLCMD:-curl}
  99. if [ "${NOCONTINUE}" ]; then
  100. ${CURLCMD/--continue-at -/} "${DOWNLOAD_URL}"
  101. else
  102. ${CURLCMD} "${DOWNLOAD_URL}"
  103. fi
  104. if [ "${DOWNLOAD_HASH}" = "$(sha256sum "${DOWNLOAD_FILE}" | cut -d " " -f 1)" ]; then
  105. info "${DOWNLOAD_FILE} downloaded successfully and passed hash check"
  106. return 0
  107. else
  108. error "${DOWNLOAD_FILE} downloaded successfully and failed hash check"
  109. return 1
  110. fi
  111. }
  112. check_and_fetch() {
  113. if [ $# -lt 2 ]; then
  114. caught_error "Usage: check_and_fetch URL HASH"
  115. fi
  116. while true; do
  117. case "${1}" in
  118. -n | --nocontinue ) NOCONTINUE=TRUE; shift ;;
  119. -- ) shift; break ;;
  120. * ) break ;;
  121. esac
  122. done
  123. DOWNLOAD_URL="${1}"
  124. DOWNLOAD_HASH="${2}"
  125. DOWNLOAD_FILE="$(basename "${DOWNLOAD_URL}")"
  126. if [ -f "${DOWNLOAD_FILE}" ] && [ "${DOWNLOAD_HASH}" = "$(sha256sum "${DOWNLOAD_FILE}" | cut -d " " -f 1)" ]; then
  127. info "${DOWNLOAD_FILE} exists and passed hash check"
  128. return 0
  129. else
  130. safe_fetch "${DOWNLOAD_URL}" "${DOWNLOAD_HASH}"
  131. fi
  132. }
  133. github_fetch() {
  134. if [ $# -ne 3 ]; then
  135. error "Usage: github_fetch GITHUB_USER GITHUB_REPOSITORY GITHUB_COMMIT_HASH"
  136. return 1
  137. fi
  138. GH_USER="${1}"
  139. GH_REPO="${2}"
  140. GH_REF="${3}"
  141. if [ -d "./.git" ]; then
  142. info "Repository ${GH_USER}/${GH_REPO} already exists, updating..."
  143. git config advice.detachedHead false
  144. git config remote.origin.url "https://github.com/${GH_USER}/${GH_REPO}.git"
  145. git config remote.origin.fetch "+refs/heads/master:refs/remotes/origin/master"
  146. git config remote.origin.tapOpt --no-tags
  147. if ! git rev-parse -q --verify "${GH_COMMIT}^{commit}"; then
  148. git fetch origin
  149. fi
  150. git checkout -f "${GH_REF}" --
  151. git reset --hard "${GH_REF}" --
  152. if [ -d "./.gitmodules" ]; then
  153. git submodule foreach --recursive git submodule sync
  154. git submodule update --init --recursive
  155. fi
  156. else
  157. git clone "https://github.com/${GH_USER}/${GH_REPO}.git" "$(pwd)"
  158. git config advice.detachedHead false
  159. info "Checking out commit ${GH_REF}.."
  160. git checkout -f "${GH_REF}" --
  161. if [ -d "./.gitmodules" ]; then
  162. git submodule foreach --recursive git submodule sync
  163. git submodule update --init --recursive
  164. fi
  165. fi
  166. }
  167. apply_patch() {
  168. if [ $# -ne 2 ]; then
  169. error "Usage: apply_patch PATCH_URL PATCH_HASH"
  170. return 1
  171. fi
  172. COMMIT_URL="${1}"
  173. COMMIT_HASH="${2}"
  174. PATCH_FILE="$(basename ${COMMIT_URL})"
  175. if [ "${COMMIT_URL:0:5}" = "https" ]; then
  176. ${CURLCMD:-curl} "${COMMIT_URL}"
  177. if [ "${COMMIT_HASH}" = "$(sha256sum ${PATCH_FILE} | cut -d " " -f 1)" ]; then
  178. info "${PATCH_FILE} downloaded successfully and passed hash check"
  179. else
  180. error "${PATCH_FILE} downloaded successfully and failed hash check"
  181. return 1
  182. fi
  183. info "Applying patch ${COMMIT_URL}"
  184. else
  185. PATCH_FILE="${COMMIT_URL}"
  186. fi
  187. patch -g 0 -f -p1 -i "${PATCH_FILE}"
  188. }