.build.zsh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. if (( ! ${+CI} )) {
  17. print -u2 -PR "%F{1} ✖︎ ${ZSH_ARGZERO:t:r} requires CI environment.%f"
  18. exit 1
  19. }
  20. autoload -Uz is-at-least && if ! is-at-least 5.2; then
  21. 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."
  22. exit 1
  23. fi
  24. TRAPZERR() {
  25. print -u2 -PR "::error::%F{1} ✖︎ script execution error.%f"
  26. print -PR -e "
  27. Callstack:
  28. ${(j:\n :)funcfiletrace}
  29. "
  30. exit 2
  31. }
  32. build() {
  33. if (( ! ${+SCRIPT_HOME} )) typeset -g SCRIPT_HOME=${ZSH_ARGZERO:A:h}
  34. local host_os=${${(s:-:)ZSH_ARGZERO:t:r}[2]}
  35. local project_root=${SCRIPT_HOME:A:h:h}
  36. local buildspec_file=${project_root}/buildspec.json
  37. fpath=(${SCRIPT_HOME}/utils.zsh ${fpath})
  38. autoload -Uz log_group log_error log_output check_${host_os} setup_ccache
  39. if [[ ! -r ${buildspec_file} ]] {
  40. log_error 'Missing buildspec.json in project checkout.'
  41. return 2
  42. }
  43. local -i debug=0
  44. local target
  45. local -r -a _valid_targets=(
  46. macos-x86_64
  47. macos-arm64
  48. linux-x86_64
  49. )
  50. local config='RelWithDebInfo'
  51. local -r -a _valid_configs=(Debug RelWithDebInfo Release MinSizeRel)
  52. local -i codesign=0
  53. local -a args
  54. while (( # )) {
  55. case ${1} {
  56. -t|--target|--generator|-c|--config)
  57. if (( # == 1 )) || [[ ${2:0:1} == '-' ]] {
  58. log_error "Missing value for option %B${1}%b"
  59. exit 2
  60. }
  61. ;;
  62. }
  63. case ${1} {
  64. --) shift; args+=($@); break ;;
  65. -t|--target)
  66. if (( ! ${_valid_targets[(Ie)${2}]} )) {
  67. log_error "Invalid value %B${2}%b for option %B${1}%b"
  68. exit 2
  69. }
  70. target=${2}
  71. shift 2
  72. ;;
  73. -c|--config)
  74. if (( ! ${_valid_configs[(Ie)${2}]} )) {
  75. log_error "Invalid value %B${2}%b for option %B${1}%b"
  76. exit 2
  77. }
  78. config=${2}
  79. shift 2
  80. ;;
  81. -s|--codesign) codesign=1; shift ;;
  82. --debug) debug=1; shift ;;
  83. *) log_error "Unknown option: %B${1}%b"; log_output ${_usage}; exit 2 ;;
  84. }
  85. }
  86. : "${target:="${host_os}-${CPUTYPE}"}"
  87. set -- ${(@)args}
  88. check_${host_os}
  89. setup_ccache
  90. if [[ ${host_os} == linux ]] {
  91. autoload -Uz setup_linux && setup_linux
  92. }
  93. local product_name
  94. read -r product_name <<< "$(jq -r '.name' ${buildspec_file})"
  95. pushd ${project_root}
  96. local -a cmake_args=()
  97. local -a cmake_build_args=(--build)
  98. local -a cmake_install_args=(--install)
  99. if (( debug )) cmake_args+=(--debug-output)
  100. case ${target} {
  101. macos-*)
  102. cmake_args+=(--preset 'macos-ci' -DCMAKE_OSX_ARCHITECTURES:STRING=${target##*-})
  103. typeset -gx NSUnbufferedIO=YES
  104. typeset -gx CODESIGN_IDENT="${CODESIGN_IDENT:--}"
  105. if (( codesign )) && [[ -z ${CODESIGN_TEAM} ]] {
  106. typeset -gx CODESIGN_TEAM="$(print "${CODESIGN_IDENT}" | /usr/bin/sed -En 's/.+\((.+)\)/\1/p')"
  107. }
  108. log_group "Configuring ${product_name}..."
  109. cmake -S ${project_root} ${cmake_args}
  110. log_group "Building ${product_name}..."
  111. run_xcodebuild() {
  112. if (( debug )) {
  113. xcodebuild ${@}
  114. } else {
  115. xcodebuild ${@} 2>&1 | xcbeautify
  116. }
  117. }
  118. local -a build_args=(
  119. ONLY_ACTIVE_ARCH=NO
  120. -project obs-studio.xcodeproj
  121. -target obs-studio
  122. -destination "generic/platform=macOS,name=Any Mac"
  123. -configuration ${config}
  124. -parallelizeTargets
  125. -hideShellScriptEnvironment
  126. build
  127. )
  128. local -a archive_args=(
  129. ONLY_ACTIVE_ARCH=NO
  130. -project obs-studio.xcodeproj
  131. -scheme obs-studio
  132. -destination "generic/platform=macOS,name=Any Mac"
  133. -archivePath obs-studio.xcarchive
  134. -parallelizeTargets
  135. -hideShellScriptEnvironment
  136. archive
  137. )
  138. local -a export_args=(
  139. -exportArchive
  140. -archivePath obs-studio.xcarchive
  141. -exportOptionsPlist exportOptions.plist
  142. -exportPath ${project_root}/build_macos
  143. )
  144. pushd build_macos
  145. if [[ ${GITHUB_EVENT_NAME} == push && ${GITHUB_REF_NAME} =~ [0-9]+.[0-9]+.[0-9]+(-(rc|beta).+)? ]] {
  146. run_xcodebuild ${archive_args}
  147. run_xcodebuild ${export_args}
  148. } else {
  149. run_xcodebuild ${build_args}
  150. rm -rf OBS.app
  151. mkdir OBS.app
  152. ditto UI/${config}/OBS.app OBS.app
  153. }
  154. popd
  155. ;;
  156. linux-*)
  157. cmake_args+=(
  158. -S ${PWD} -B build_${target##*-}
  159. -G Ninja
  160. -DCMAKE_BUILD_TYPE:STRING=${config}
  161. -DCEF_ROOT_DIR:PATH="${project_root}/.deps/cef_binary_${CEF_VERSION}_${target//-/_}"
  162. -DENABLE_AJA:BOOL=OFF
  163. -DENABLE_WEBRTC:BOOL=OFF
  164. )
  165. if (( ! UBUNTU_2210_OR_LATER )) cmake_args+=(-DENABLE_NEW_MPEGTS_OUTPUT:BOOL=OFF)
  166. cmake_build_args+=(build_${target##*-} --config ${config} --parallel)
  167. cmake_install_args+=(build_${target##*-} --prefix ${project_root}/build_${target##*-}/install/${config})
  168. log_group "Configuring ${product_name}..."
  169. cmake -S ${project_root} ${cmake_args}
  170. log_group "Building ${product_name}..."
  171. if (( debug )) cmake_build_args+=(--verbose)
  172. cmake ${cmake_build_args}
  173. log_group "Installing ${product_name}..."
  174. if (( debug )) cmake_install_args+=(--verbose)
  175. cmake ${cmake_install_args}
  176. ;;
  177. }
  178. popd
  179. log_group
  180. }
  181. build ${@}