build_support.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. step "Check CCache..."
  61. if ccache -V >/dev/null 2>&1; then
  62. info "CCache available"
  63. CMAKE_CCACHE_OPTIONS="-DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_C_COMPILER_LAUNCHER=ccache"
  64. if [ "${CI}" ]; then
  65. ccache --set-config=cache_dir=${GITHUB_WORKSPACE:-${HOME}}/.ccache
  66. ccache --set-config=max_size=${CCACHE_SIZE:-500M}
  67. ccache --set-config=compression=true
  68. ccache -z
  69. fi
  70. else
  71. info "CCache not available"
  72. fi
  73. }
  74. safe_fetch() {
  75. if [ $# -lt 2 ]; then
  76. error "Usage: safe_fetch URL HASH"
  77. return 1
  78. fi
  79. while true; do
  80. case "${1}" in
  81. -n | --nocontinue ) NOCONTINUE=TRUE; shift ;;
  82. -- ) shift; break ;;
  83. * ) break ;;
  84. esac
  85. done
  86. DOWNLOAD_URL="${1}"
  87. DOWNLOAD_HASH="${2}"
  88. DOWNLOAD_FILE="$(basename ${DOWNLOAD_URL})"
  89. CURLCMD=${CURLCMD:-curl}
  90. if [ "${NOCONTINUE}" ]; then
  91. ${CURLCMD/--continue-at -/} "${DOWNLOAD_URL}"
  92. else
  93. ${CURLCMD} "${DOWNLOAD_URL}"
  94. fi
  95. if [ "${DOWNLOAD_HASH}" = "$(sha256sum "${DOWNLOAD_FILE}" | cut -d " " -f 1)" ]; then
  96. info "${DOWNLOAD_FILE} downloaded successfully and passed hash check"
  97. return 0
  98. else
  99. error "${DOWNLOAD_FILE} downloaded successfully and failed hash check"
  100. return 1
  101. fi
  102. }
  103. check_and_fetch() {
  104. if [ $# -lt 2 ]; then
  105. caught_error "Usage: check_and_fetch URL HASH"
  106. fi
  107. while true; do
  108. case "${1}" in
  109. -n | --nocontinue ) NOCONTINUE=TRUE; shift ;;
  110. -- ) shift; break ;;
  111. * ) break ;;
  112. esac
  113. done
  114. DOWNLOAD_URL="${1}"
  115. DOWNLOAD_HASH="${2}"
  116. DOWNLOAD_FILE="$(basename "${DOWNLOAD_URL}")"
  117. if [ -f "${DOWNLOAD_FILE}" ] && [ "${DOWNLOAD_HASH}" = "$(sha256sum "${DOWNLOAD_FILE}" | cut -d " " -f 1)" ]; then
  118. info "${DOWNLOAD_FILE} exists and passed hash check"
  119. return 0
  120. else
  121. safe_fetch "${DOWNLOAD_URL}" "${DOWNLOAD_HASH}"
  122. fi
  123. }
  124. github_fetch() {
  125. if [ $# -ne 3 ]; then
  126. error "Usage: github_fetch GITHUB_USER GITHUB_REPOSITORY GITHUB_COMMIT_HASH"
  127. return 1
  128. fi
  129. GH_USER="${1}"
  130. GH_REPO="${2}"
  131. GH_REF="${3}"
  132. if [ -d "./.git" ]; then
  133. info "Repository ${GH_USER}/${GH_REPO} already exists, updating..."
  134. git config advice.detachedHead false
  135. git config remote.origin.url "https://github.com/${GH_USER}/${GH_REPO}.git"
  136. git config remote.origin.fetch "+refs/heads/master:refs/remotes/origin/master"
  137. git config remote.origin.tapOpt --no-tags
  138. if ! git rev-parse -q --verify "${GH_COMMIT}^{commit}"; then
  139. git fetch origin
  140. fi
  141. git checkout -f "${GH_REF}" --
  142. git reset --hard "${GH_REF}" --
  143. if [ -d "./.gitmodules" ]; then
  144. git submodule foreach --recursive git submodule sync
  145. git submodule update --init --recursive
  146. fi
  147. else
  148. git clone "https://github.com/${GH_USER}/${GH_REPO}.git" "$(pwd)"
  149. git config advice.detachedHead false
  150. info "Checking out commit ${GH_REF}..."
  151. git checkout -f "${GH_REF}" --
  152. if [ -d "./.gitmodules" ]; then
  153. git submodule foreach --recursive git submodule sync
  154. git submodule update --init --recursive
  155. fi
  156. fi
  157. }
  158. apply_patch() {
  159. if [ $# -ne 2 ]; then
  160. error "Usage: apply_patch PATCH_URL PATCH_HASH"
  161. return 1
  162. fi
  163. COMMIT_URL="${1}"
  164. COMMIT_HASH="${2}"
  165. PATCH_FILE="$(basename ${COMMIT_URL})"
  166. if [ "${COMMIT_URL:0:5}" = "https" ]; then
  167. ${CURLCMD:-curl} "${COMMIT_URL}"
  168. if [ "${COMMIT_HASH}" = "$(sha256sum ${PATCH_FILE} | cut -d " " -f 1)" ]; then
  169. info "${PATCH_FILE} downloaded successfully and passed hash check"
  170. else
  171. error "${PATCH_FILE} downloaded successfully and failed hash check"
  172. return 1
  173. fi
  174. info "Applying patch ${COMMIT_URL}"
  175. else
  176. PATCH_FILE="${COMMIT_URL}"
  177. fi
  178. patch -g 0 -f -p1 -i "${PATCH_FILE}"
  179. }