build_host.sh (3739B)
1 #!/usr/bin/env bash 2 set -euo pipefail 3 4 script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" 5 platform_root="$(cd "${script_dir}/.." && pwd -P)" 6 repo_root="$(git -C "${script_dir}" rev-parse --show-toplevel)" 7 requested_configuration="${CONFIGURATION:-Debug}" 8 bundle_name="Radroots.app" 9 plist_template="${platform_root}/App/Resources/Info.plist" 10 11 require_command() { 12 if command -v "$1" >/dev/null 2>&1; then 13 return 14 fi 15 echo "missing required command: $1" >&2 16 exit 1 17 } 18 19 append_env_value() { 20 local var_name="$1" 21 local value="$2" 22 local current="${!var_name:-}" 23 24 if [[ -n "${current}" ]]; then 25 export "${var_name}=${value} ${current}" 26 else 27 export "${var_name}=${value}" 28 fi 29 } 30 31 prepare_macos_build_env() { 32 local sdk_path 33 local bindgen_args 34 35 sdk_path="$(xcrun --sdk macosx --show-sdk-path)" 36 if [[ -z "${sdk_path}" || ! -d "${sdk_path}" ]]; then 37 echo "unable to resolve macos sdk path via xcrun" >&2 38 exit 1 39 fi 40 41 if [[ ! -f "${sdk_path}/usr/include/dispatch/dispatch.h" ]]; then 42 echo "missing macos dispatch header: ${sdk_path}/usr/include/dispatch/dispatch.h" >&2 43 exit 1 44 fi 45 46 export SDKROOT="${sdk_path}" 47 bindgen_args="--sysroot=${SDKROOT} -I${SDKROOT}/usr/include" 48 49 append_env_value BINDGEN_EXTRA_CLANG_ARGS "${bindgen_args}" 50 append_env_value BINDGEN_EXTRA_CLANG_ARGS_aarch64_apple_darwin "${bindgen_args}" 51 append_env_value BINDGEN_EXTRA_CLANG_ARGS_x86_64_apple_darwin "${bindgen_args}" 52 } 53 54 workspace_version() { 55 python3 - <<'PY' "${repo_root}/Cargo.toml" 56 import re 57 import sys 58 59 path = sys.argv[1] 60 with open(path, "r", encoding="utf-8") as handle: 61 cargo_toml = handle.read() 62 63 match = re.search(r'^\[workspace\.package\][\s\S]*?^version\s*=\s*"([^"]+)"', cargo_toml, re.MULTILINE) 64 if not match: 65 raise SystemExit("missing workspace.package.version") 66 67 print(match.group(1), end="") 68 PY 69 } 70 71 cargo_target_dir() { 72 ( 73 cd "${repo_root}" 74 cargo metadata --format-version 1 --no-deps | python3 -c 'import json, sys; print(json.load(sys.stdin)["target_directory"], end="")' 75 ) 76 } 77 78 configure_build_lane() { 79 case "${requested_configuration}" in 80 Debug|debug) 81 bundle_configuration="Debug" 82 cargo_profile="debug" 83 ;; 84 Release|release) 85 bundle_configuration="Release" 86 cargo_profile="release" 87 ;; 88 *) 89 echo "unsupported CONFIGURATION: ${requested_configuration}" >&2 90 exit 1 91 ;; 92 esac 93 } 94 95 require_command cargo 96 require_command git 97 require_command python3 98 require_command /usr/libexec/PlistBuddy 99 require_command xcrun 100 101 configure_build_lane 102 prepare_macos_build_env 103 104 bundle_root="${platform_root}/.derived-data/Build/Products/${bundle_configuration}/${bundle_name}" 105 contents_root="${bundle_root}/Contents" 106 executable_root="${contents_root}/MacOS" 107 resources_root="${contents_root}/Resources" 108 plist_path="${contents_root}/Info.plist" 109 binary_target="${executable_root}/Radroots" 110 app_icon_path="${resources_root}/AppIcon.icns" 111 112 ( 113 cd "${repo_root}" 114 if [[ "${cargo_profile}" == "release" ]]; then 115 cargo build -p radroots_app --release 116 else 117 cargo build -p radroots_app 118 fi 119 ) 120 121 binary_source="$(cargo_target_dir)/${cargo_profile}/radroots_app" 122 123 if [[ ! -x "${binary_source}" ]]; then 124 echo "missing desktop launcher binary: ${binary_source}" >&2 125 exit 1 126 fi 127 128 rm -rf "${bundle_root}" 129 mkdir -p "${executable_root}" "${resources_root}" 130 cp "${plist_template}" "${plist_path}" 131 cp "${binary_source}" "${binary_target}" 132 chmod +x "${binary_target}" 133 "${script_dir}/build_icon.sh" "${app_icon_path}" 134 135 /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $(workspace_version)" "${plist_path}" 136 /usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${RADROOTS_APP_BUILD:-1}" "${plist_path}" 137 138 printf '%s\n' "${bundle_root}"