build_support.sh 5.6 KB

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