.build.zsh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env zsh
  2. builtin emulate -L zsh
  3. setopt EXTENDED_GLOB
  4. setopt PUSHD_SILENT
  5. setopt ERR_EXIT
  6. setopt ERR_RETURN
  7. setopt NO_UNSET
  8. setopt PIPE_FAIL
  9. setopt NO_AUTO_PUSHD
  10. setopt NO_PUSHD_IGNORE_DUPS
  11. setopt FUNCTION_ARGZERO
  12. ## Enable for script debugging
  13. #setopt WARN_CREATE_GLOBAL
  14. #setopt WARN_NESTED_VAR
  15. #setopt XTRACE
  16. autoload -Uz is-at-least && if ! is-at-least 5.2; then
  17. print -u2 -PR "%F{1}${funcstack[1]##*/}:%f Running on Zsh version %B${ZSH_VERSION}%b, but Zsh %B5.2%b is the minimum supported version. Upgrade Zsh to fix this issue."
  18. exit 1
  19. fi
  20. _trap_error() {
  21. print -u2 -PR '%F{1} ✖︎ script execution error%f'
  22. print -PR -e "
  23. Callstack:
  24. ${(j:\n :)funcfiletrace}
  25. "
  26. exit 2
  27. }
  28. build() {
  29. if (( ! ${+SCRIPT_HOME} )) typeset -g SCRIPT_HOME=${ZSH_ARGZERO:A:h}
  30. local host_os=${${(s:-:)ZSH_ARGZERO:t:r}[2]}
  31. local target="${host_os}-${CPUTYPE}"
  32. local project_root=${SCRIPT_HOME:A:h:h}
  33. local buildspec_file="${project_root}/buildspec.json"
  34. trap '_trap_error' ZERR
  35. fpath=("${SCRIPT_HOME}/utils.zsh" ${fpath})
  36. autoload -Uz log_info log_error log_output set_loglevel check_${host_os} setup_${host_os} setup_obs setup_ccache
  37. local -i _verbosity=1
  38. local -r _version='0.0.1'
  39. local -r -a _valid_targets=(
  40. macos-x86_64
  41. macos-arm64
  42. macos-universal
  43. linux-x86_64
  44. )
  45. local -r -a _valid_configs=(Debug RelWithDebInfo Release MinSizeRel)
  46. local -r _usage="
  47. Usage: %B${functrace[1]%:*}%b <option> [<options>]
  48. %BOptions%b:
  49. %F{yellow} Build configuration options%f
  50. -----------------------------------------------------------------------------
  51. %B-t | --target%b Specify target - default: %B%F{green}${host_os}-${CPUTYPE}%f%b
  52. %B-c | --config%b Build configuration - default: %B%F{green}RelWithDebInfo%f%b
  53. %B-s | --codesign%b Enable codesigning (macOS only)
  54. %F{yellow} Output options%f
  55. -----------------------------------------------------------------------------
  56. %B-q | --quiet%b Quiet (error output only)
  57. %B-v | --verbose%b Verbose (more detailed output)
  58. %F{yellow} General options%f
  59. -----------------------------------------------------------------------------
  60. %B-h | --help%b Print this usage help
  61. %B-V | --version%b Print script version information"
  62. local -a args
  63. while (( # )) {
  64. case ${1} {
  65. -t|--target|-c|--config)
  66. if (( # == 1 )) || [[ ${2:0:1} == '-' ]] {
  67. log_error "Missing value for option %B${1}%b"
  68. log_output ${_usage}
  69. exit 2
  70. }
  71. ;;
  72. }
  73. case ${1} {
  74. --)
  75. shift
  76. args+=($@)
  77. break
  78. ;;
  79. -t|--target)
  80. if (( ! ${_valid_targets[(Ie)${2}]} )) {
  81. log_error "Invalid value %B${2}%b for option %B${1}%b"
  82. log_output ${_usage}
  83. exit 2
  84. }
  85. target=${2}
  86. shift 2
  87. ;;
  88. -c|--config)
  89. if (( ! ${_valid_configs[(Ie)${2}]} )) {
  90. log_error "Invalid value %B${2}%b for option %B${1}%b"
  91. log_output ${_usage}
  92. exit 2
  93. }
  94. BUILD_CONFIG=${2}
  95. shift 2
  96. ;;
  97. -s|--codesign) CODESIGN=1; shift ;;
  98. -q|--quiet) (( _verbosity -= 1 )) || true; shift ;;
  99. -v|--verbose) (( _verbosity += 1 )); shift ;;
  100. -h|--help) log_output ${_usage}; exit 0 ;;
  101. -V|--version) print -Pr "${_version}"; exit 0 ;;
  102. --debug) _verbosity=3; shift ;;
  103. *) log_error "Unknown option: %B${1}%b"; log_output ${_usage}; exit 2 ;;
  104. }
  105. }
  106. set -- ${(@)args}
  107. set_loglevel ${_verbosity}
  108. check_${host_os}
  109. setup_${host_os}
  110. read -r product_name product_version <<< \
  111. "$(jq -r '. | {name, version} | join(" ")' ${project_root}/buildspec.json)"
  112. case ${host_os} {
  113. macos-*)
  114. sed -i '' \
  115. "s/project(\(.*\) VERSION \(.*\))/project(${product_name} VERSION ${product_version})/" \
  116. "${project_root}"/CMakeLists.txt
  117. ;;
  118. linux-*)
  119. sed -i'' \
  120. "s/project(\(.*\) VERSION \(.*\))/project(${product_name} VERSION ${product_version})/"\
  121. "${project_root}"/CMakeLists.txt
  122. ;;
  123. }
  124. setup_ccache
  125. setup_obs
  126. pushd ${project_root}
  127. log_info "Configuring ${product_name}..."
  128. local -a cmake_args=(
  129. -DCMAKE_BUILD_TYPE=${BUILD_CONFIG:-RelWithDebInfo}
  130. -DCMAKE_PREFIX_PATH="${project_root:h}/obs-build-dependencies/obs-plugin-deps"
  131. )
  132. if (( _loglevel == 0 )) cmake_args+=(-Wno_deprecated -Wno-dev --log-level=ERROR)
  133. case ${target} {
  134. macos-*)
  135. autoload -Uz read_codesign
  136. if (( ${+CODESIGN} )) {
  137. read_codesign
  138. }
  139. cmake_args+=(
  140. -DCMAKE_OSX_ARCHITECTURES=${${target##*-}//universal/x86_64;arm64}
  141. -DCMAKE_OSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET:-10.15}
  142. -DOBS_BUNDLE_CODESIGN_IDENTITY="${CODESIGN_IDENT:--}"
  143. -DCMAKE_FRAMEWORK_PATH="${project_root:h}/obs-build-dependencies/obs-plugin-deps/Frameworks"
  144. )
  145. ;;
  146. linux-*)
  147. if (( ${+CI} )) {
  148. cmake_args+=(-DCMAKE_INSTALL_PREFIX=/usr)
  149. }
  150. ;;
  151. }
  152. cmake -S . -B build_${target} -G Ninja ${cmake_args}
  153. local -a cmake_args=()
  154. if (( _loglevel > 1 )) cmake_args+=(--verbose)
  155. log_info "Building ${product_name}"
  156. cmake --build build_${target} --config ${BUILD_CONFIG:-RelWithDebInfo} ${cmake_args}
  157. log_info "Install ${product_name}"
  158. cmake --install build_${target} --config ${BUILD_CONFIG:-RelWithDebInfo} --prefix "${project_root}"/release ${cmake_args}
  159. popd
  160. }
  161. build ${@}