#!/usr/bin/env bash set -Eeuo pipefail app_name="m9zCtrlTty" app_package="./apps/m9zCtrlTty" script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd -- "${script_dir}/../.." && pwd)" usage() { cat <<'EOF' Usage: ./apps/m9zCtrlTty/build.sh [goarm] Examples: ./apps/m9zCtrlTty/build.sh linux arm 7 ./apps/m9zCtrlTty/build.sh linux arm64 ./apps/m9zCtrlTty/build.sh windows amd64 ./apps/m9zCtrlTty/build.sh darwin arm64 Arguments: goos Target operating system accepted by "go tool dist list". goarch Target CPU architecture accepted by "go tool dist list". goarm ARM generation for 32-bit GOARCH=arm: 5, 6, or 7 (default: 7). Soft-float targets may use 5,softfloat, 6,softfloat, or 7,softfloat. Environment variables: OUTPUT_ROOT Output root directory (default: dist). CGO_ENABLED CGO mode (default: 0 for cross-compilation). BUILD_TAGS Optional Go build tags. BUILD_LDFLAGS Go linker flags (default: -s -w; set to an empty value to keep symbols). EOF } if [[ $# -lt 2 || $# -gt 3 ]]; then usage >&2 exit 2 fi if ! command -v go >/dev/null 2>&1; then printf 'Go toolchain was not found in PATH.\n' >&2 exit 127 fi target_os="$1" target_arch="$2" target_pair="${target_os}/${target_arch}" supported_target=false while IFS= read -r candidate; do if [[ "$candidate" == "$target_pair" ]]; then supported_target=true break fi done < <(go tool dist list) if [[ "$supported_target" != true ]]; then printf 'Unsupported Go build target: %s\n' "$target_pair" >&2 printf 'Run "go tool dist list" to see supported targets.\n' >&2 exit 2 fi arch_label="$target_arch" if [[ "$target_arch" == "arm" ]]; then target_goarm="${3:-${GOARM:-7}}" case "$target_goarm" in 5|6|7|5,softfloat|6,softfloat|7,softfloat) ;; *) printf 'Invalid GOARM value: %s\n' "$target_goarm" >&2 exit 2 ;; esac export GOARM="$target_goarm" arm_generation="${target_goarm%%,*}" arch_label="armv${arm_generation}" if [[ "$target_goarm" == *,softfloat ]]; then arch_label="${arch_label}-softfloat" fi elif [[ $# -eq 3 ]]; then printf 'The third argument (goarm) is only valid when GOARCH=arm.\n' >&2 exit 2 fi output_root="${OUTPUT_ROOT:-dist}" if [[ "$output_root" != /* ]]; then output_root="${repo_root}/${output_root}" fi output_dir="${output_root}/${target_os}-${arch_label}" extension="" case "$target_os" in windows) extension=".exe" ;; js|wasip1) if [[ "$target_arch" == "wasm" ]]; then extension=".wasm" fi ;; esac output_path="${output_dir}/${app_name}${extension}" mkdir -p "$output_dir" export GOOS="$target_os" export GOARCH="$target_arch" export CGO_ENABLED="${CGO_ENABLED:-0}" if [[ "${BUILD_LDFLAGS+x}" == "x" ]]; then build_ldflags="$BUILD_LDFLAGS" else build_ldflags="-s -w" fi build_args=(build -mod=readonly -trimpath -o "$output_path") if [[ -n "${BUILD_TAGS:-}" ]]; then build_args+=(-tags "$BUILD_TAGS") fi if [[ -n "$build_ldflags" ]]; then build_args+=(-ldflags "$build_ldflags") fi build_args+=("$app_package") printf 'Building %s for %s (CGO_ENABLED=%s)\n' "$app_name" "$target_pair" "$CGO_ENABLED" if [[ "$target_arch" == "arm" ]]; then printf 'Using GOARM=%s\n' "$GOARM" fi ( cd "$repo_root" go "${build_args[@]}" ) printf 'Build complete: %s\n' "$output_path" if command -v file >/dev/null 2>&1; then file "$output_path" fi