1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
#!/usr/bin/env zsh
builtin emulate -L zsh
setopt EXTENDED_GLOB
setopt PUSHD_SILENT
setopt ERR_EXIT
setopt ERR_RETURN
setopt NO_UNSET
setopt PIPE_FAIL
setopt NO_AUTO_PUSHD
setopt NO_PUSHD_IGNORE_DUPS
setopt FUNCTION_ARGZERO
## Enable for script debugging
# setopt WARN_CREATE_GLOBAL
# setopt WARN_NESTED_VAR
# setopt XTRACE
autoload -Uz is-at-least && if ! is-at-least 5.2; then
print -u2 -PR "${CI:+::error::}%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."
exit 1
fi
TRAPEXIT() {
local return_value=$?
if (( ${+CI} )) unset NSUnbufferedIO
return ${return_value}
}
TRAPZERR() {
if (( ${_loglevel:-3} > 2 )) {
print -u2 -PR "${CI:+::error::}%F{1} ✖︎ script execution error%f"
print -PR -e "
Callstack:
${(j:\n :)funcfiletrace}
"
}
exit 2
}
build() {
if (( ! ${+SCRIPT_HOME} )) typeset -g SCRIPT_HOME=${ZSH_ARGZERO:A:h}
local host_os=${${(s:-:)ZSH_ARGZERO:t:r}[2]}
local project_root=${SCRIPT_HOME:A:h:h}
local buildspec_file=${project_root}/buildspec.json
local dep_dir=""
fpath=("${SCRIPT_HOME}/utils.zsh" ${fpath})
autoload -Uz log_group log_info log_error log_output set_loglevel check_${host_os} setup_ccache
if [[ ! -r ${buildspec_file} ]] {
log_error \
'No buildspec.json found. Please create a build specification for your project.'
return 2
}
typeset -g -a skips=()
local -i verbosity=1
local -r _version='2.0.0'
local -r -a _valid_targets=(
macos-universal
linux-x86_64
linux-aarch64
)
local target
local config='RelWithDebInfo'
local -r -a _valid_configs=(Debug RelWithDebInfo Release MinSizeRel)
local -i codesign=0
if [[ ${host_os} == linux ]] {
local -r -a _valid_generators=(Ninja 'Unix Makefiles')
local generator='Ninja'
local -r _usage_host="
%F{yellow} Additional options for Linux builds%f
-----------------------------------------------------------------------------
%B--generator%b Specify build system to generate
Available generators:
- Ninja
- Unix Makefiles"
} elif [[ ${host_os} == macos ]] {
local -r _usage_host="
%F{yellow} Additional options for macOS builds%f
-----------------------------------------------------------------------------
%B-s | --codesign%b Enable codesigning (macOS only)"
}
local -i print_config=0
local -r _usage="
Usage: %B${functrace[1]%:*}%b <option> [<options>]
%BOptions%b:
%F{yellow} Build configuration options%f
-----------------------------------------------------------------------------
%B-t | --target%b Specify target
%B-c | --config%b Build configuration
%B-p | --portable%b Enable portable mode (Linux only)
%B-d | --dep%b Dependency directory name - default: %B%F{green}advss-build-dependencies%f%b
%B--skip-[all|build|deps]%b Skip all|building|checking for dependencies
%F{yellow} Output options%f
-----------------------------------------------------------------------------
%B-q | --quiet%b Quiet (error output only)
%B-v | --verbose%b Verbose (more detailed output)
%B--debug%b Debug (very detailed and added output)
%F{yellow} General options%f
-----------------------------------------------------------------------------
%B-h | --help%b Print this usage help
%B-V | --version%b Print script version information
${_usage_host:-}"
local -a args
while (( # )) {
case ${1} {
-t|--target|-c|--config|--generator)
if (( # == 1 )) || [[ ${2:0:1} == '-' ]] {
log_error "Missing value for option %B${1}%b"
log_output ${_usage}
exit 2
}
;;
}
case ${1} {
--)
shift
args+=($@)
break
;;
-t|--target)
if (( ! ${_valid_targets[(Ie)${2}]} )) {
log_error "Invalid value %B${2}%b for option %B${1}%b"
log_output ${_usage}
exit 2
}
target=${2}
shift 2
;;
-c|--config)
if (( ! ${_valid_configs[(Ie)${2}]} )) {
log_error "Invalid value %B${2}%b for option %B${1}%b"
log_output ${_usage}
exit 2
}
config=${2}
shift 2
;;
-s|--codesign) codesign=1; shift ;;
-d|--dep)
dep_dir="${2}"
shift 2
;;
-p|--portable) typeset -g PORTABLE=1; shift ;;
-q|--quiet) (( verbosity -= 1 )) || true; shift ;;
-v|--verbose) (( verbosity += 1 )); shift ;;
-h|--help) log_output ${_usage}; exit 0 ;;
-V|--version) print -Pr "${_version}"; exit 0 ;;
--debug) verbosity=3; shift ;;
--generator)
if [[ ${host_os} == linux ]] {
if (( ! ${_valid_generators[(Ie)${2}]} )) {
log_error "Invalid value %B${2}%b for option %B${1}%b"
log_output ${_usage}
exit 2
}
generator=${2}
}
shift 2
;;
--print-config) print_config=1; skips+=(deps); shift ;;
--skip-*)
local -r _skip="${${(s:-:)1}[-1]}"
local -r -a _check=(all build deps)
(( ${_check[(Ie)${_skip}]} )) || log_warning "Invalid skip mode %B${_skip}%b supplied"
typeset -g -a skips=(${skips} ${_skip})
shift
;;
*) log_error "Unknown option: %B${1}%b"; log_output ${_usage}; exit 2 ;;
}
}
: "${target:="${host_os}-${CPUTYPE}"}"
set -- ${(@)args}
set_loglevel ${verbosity}
if (( ! (${skips[(Ie)all]} + ${skips[(Ie)deps]}) )) {
check_${host_os}
setup_ccache
}
if [[ ${host_os} == linux ]] {
autoload -Uz setup_linux && setup_linux
}
local advss_deps_path
if [[ -z "${dep_dir}" ]] {
log_info "Building advss deps ..."
dep_dir=".deps/advss-build-dependencies"
${SCRIPT_HOME}/build-deps-${host_os}.zsh -c "${BUILD_CONFIG:-RelWithDebInfo}" -t "${target}" --generator "${generator}" -o "${dep_dir}"
}
advss_deps_path=$(realpath ${project_root}/${dep_dir})
log_info "Using advss deps at $advss_deps_path ..."
local product_name
local product_version
local git_tag="$(git describe --tags)"
read -r product_name product_version <<< \
"$(jq -r '. | {name, version} | join(" ")' ${buildspec_file})"
if [[ "${git_tag}" =~ '^([0-9]+\.){0,2}(\*|[0-9]+)$' ]] {
log_info "Using git tag as version identifier '${git_tag}'"
product_version="${git_tag}"
} else {
log_info "Using buildspec.json version identifier '${product_version}'"
}
case ${host_os} {
macos)
sed -i '' \
"s/project(\(.*\) VERSION \(.*\))/project(${product_name} VERSION ${product_version})/" \
"${project_root}/CMakeLists.txt"
;;
linux)
sed -i'' \
"s/project(\(.*\) VERSION \(.*\))/project(${product_name} VERSION ${product_version})/"\
"${project_root}/CMakeLists.txt"
;;
}
pushd ${project_root}
if (( ! (${skips[(Ie)all]} + ${skips[(Ie)build]}) )) {
log_group "Configuring ${product_name}..."
local -a cmake_args=(
-DCMAKE_PREFIX_PATH="${advss_deps_path}"
)
local -a cmake_build_args=(--build)
local -a cmake_install_args=(--install)
case ${_loglevel} {
0) cmake_args+=(-Wno_deprecated -Wno-dev --log-level=ERROR) ;;
1) ;;
2) cmake_build_args+=(--verbose) ;;
*) cmake_args+=(--debug-output) ;;
}
local -r _preset="${target%%-*}${CI:+-ci}"
case ${target} {
macos-*)
if (( ${+CI} )) typeset -gx NSUnbufferedIO=YES
cmake_args+=(
-DCMAKE_PREFIX_PATH="${advss_deps_path}"
--preset ${_preset}
)
if (( codesign )) {
autoload -Uz read_codesign_team && read_codesign_team
if [[ -z ${CODESIGN_TEAM} ]] {
autoload -Uz read_codesign && read_codesign
}
}
cmake_args+=(
-DCODESIGN_TEAM=${CODESIGN_TEAM:-}
-DCODESIGN_IDENTITY=${CODESIGN_IDENT:--}
)
cmake_build_args+=(--preset ${_preset} --parallel --config ${config} -- ONLY_ACTIVE_ARCH=NO -arch arm64 -arch x86_64)
cmake_install_args+=(build_macos --config ${config} --prefix "${project_root}/release/${config}")
local -a xcbeautify_opts=()
if (( _loglevel == 0 )) xcbeautify_opts+=(--quiet)
;;
linux-*)
cmake_args+=(
--preset ${_preset}-${target##*-}
-G "${generator}"
-DQT_VERSION=${QT_VERSION:-6}
-DCMAKE_BUILD_TYPE=${config}
-DCMAKE_INSTALL_PREFIX=/usr
)
local cmake_version
read -r _ _ cmake_version <<< "$(cmake --version)"
if [[ ${CPUTYPE} != ${target##*-} ]] {
if is-at-least 3.21.0 ${cmake_version}; then
cmake_args+=(--toolchain "${project_root}/cmake/linux/toolchains/${target##*-}-linux-gcc.cmake")
else
cmake_args+=(-D"CMAKE_TOOLCHAIN_FILE=${project_root}/cmake/linux/toolchains/${target##*-}-linux-gcc.cmake")
fi
}
cmake_build_args+=(--preset ${_preset}-${target##*-} --config ${config})
if [[ ${generator} == 'Unix Makefiles' ]] {
cmake_build_args+=(--parallel $(( $(nproc) + 1 )))
} else {
cmake_build_args+=(--parallel)
}
cmake_install_args+=(build_${target##*-} --prefix ${project_root}/release/${config})
;;
}
log_debug "Attempting to configure with CMake arguments: ${cmake_args}"
cmake ${cmake_args}
log_group "Building ${product_name}..."
if [[ ${host_os} == macos ]] {
if (( _loglevel > 1 )) {
cmake ${cmake_build_args}
} else {
cmake ${cmake_build_args} 2>&1 | xcbeautify ${xcbeautify_opts}
}
} else {
cmake ${cmake_build_args}
}
}
log_group "Installing ${product_name}..."
if (( _loglevel > 1 )) cmake_install_args+=(--verbose)
cmake ${cmake_install_args}
popd
log_group
}
build ${@}
|