maint(common): consolidate functions to determine OS

- add `builder_is_windows`, `builder_is_macos` and `builder_is_linux`
  functions
- add/move `BUILDER_OS` env variable to `resources/builder.inc.sh`
- use the new functions and env variable

Fixes: #14109
Test-bot: skip
This commit is contained in:
Eberhard Beilharz 2025-07-09 18:33:09 +02:00
parent 94242982fc
commit 3b136fcec9
No known key found for this signature in database
GPG key ID: E9140597606020D3
30 changed files with 137 additions and 137 deletions

View file

@ -18,7 +18,7 @@ builder_describe "Keyman common Linux modules" \
builder_parse "$@"
if [[ $BUILDER_OS != linux ]]; then
if ! builder_is_linux; then
builder_echo grey "Platform is not linux; skipping common/linux"
exit 0
fi

View file

@ -18,7 +18,7 @@ builder_describe "Keyman common mac modules" \
builder_parse "$@"
if [[ $BUILDER_OS != mac ]]; then
if ! builder_is_macos; then
builder_echo grey "Platform is not macOS; skipping common/mac"
exit 0
fi

View file

@ -12,7 +12,7 @@ THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")"
## END STANDARD BUILD SCRIPT INCLUDE
if [[ $BUILDER_OS == win ]]; then
if builder_is_windows; then
# BrowserStackLocal may not exist, so always pass
taskkill //f //im BrowserStackLocal.exe || true
fi

View file

@ -18,7 +18,7 @@ builder_describe "Keyman common Windows modules" \
builder_parse "$@"
if [[ $BUILDER_OS != win ]]; then
if ! builder_is_windows; then
builder_echo grey "Platform is not Windows; skipping common/windows"
exit 0
fi

View file

@ -91,7 +91,7 @@ do_test() {
if [[ $target =~ ^(x86|x64)$ ]]; then
cmd //C build.bat $target $BUILDER_CONFIGURATION test $testparams
else
if [[ $target == wasm ]] && [[ $BUILDER_OS == mac ]]; then
if [[ $target == wasm ]] && builder_is_macos; then
# 11794 -- parallel tests failing on some mac build agents; temporary
# mitigation until we diagnose root cause
meson test -j 1 -C "$MESON_PATH" $testparams
@ -136,7 +136,7 @@ do_uninstall() {
# ----------------------------------------------------------------------------
build_meson_cross_file_for_wasm() {
if [ $BUILDER_OS == win ]; then
if builder_is_windows; then
local R=$(cygpath -w $(echo $EMSCRIPTEN_BASE) | sed 's_\\_\\\\_g')
else
local R=$(echo $EMSCRIPTEN_BASE | sed 's_/_\\/_g')

View file

@ -66,7 +66,7 @@ builder_parse "$@"
#-------------------------------------------------------------------------------------------------------------------
if [[ $BUILDER_OS == win ]]; then
if builder_is_windows; then
source "$KEYMAN_ROOT/resources/build/win/environment.inc.sh"
fi

View file

@ -126,7 +126,7 @@ do_command() {
# ----------------------------------------------------------------------------
build_meson_cross_file_for_wasm() {
if [ $BUILDER_OS == win ]; then
if builder_is_windows; then
local R=$(cygpath -w $(echo $EMSCRIPTEN_BASE) | sed 's_\\_\\\\_g')
else
local R=$(echo $EMSCRIPTEN_BASE | sed 's_/_\\/_g')
@ -140,4 +140,4 @@ should_do_full_test() {
fi
return 1
}
}

View file

@ -123,6 +123,13 @@ package.json (`npm run <script>`).
passed in, or `release` otherwise, which corresponds to the output folder
names for many projects.
* `BUILDER_OS` will be set to `linux`, `mac`, or `win`, depending on the platform
the builder script is currently running on.
**NOTE:** Only use `BUILDER_OS` in a `case` statement. Use one of the
`builder_is_macos`, `builder_is_windows`, or `builder_is_linux` functions
to check if the script is running on a particular platform.
Other environment variables and paths will probably be added over time.
## Split
@ -1241,6 +1248,20 @@ builder_run_action test builder_do_typescript_tests [coverage_threshold]
--------------------------------------------------------------------------------
## `builder_is_windows`, `builder_is_macos`, and `builder_is_linux` functions
Returns 0 (true) if building on the particular platform.
### Usage
```bash
if builder_is_windows; then
builder_echo "We're building on a Windows machine"
fi
```
--------------------------------------------------------------------------------
## Formatting variables
These helper variables define ANSI color escapes when running in color mode, and

View file

@ -7,7 +7,7 @@ THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")"
## END STANDARD BUILD SCRIPT INCLUDE
# Please note that this build script (understandably) assumes that it is running on Mac OS X.
if [[ "${OSTYPE}" != "darwin"* ]]; then
if ! builder_is_macos; then
echo "This build script will only run in a Mac environment."
exit 1
fi

View file

@ -7,7 +7,7 @@ THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")"
## END STANDARD BUILD SCRIPT INCLUDE
# Please note that this build script (understandably) assumes that it is running on Mac OS X.
if [[ "${OSTYPE}" != "darwin"* ]]; then
if ! builder_is_macos; then
echo "This build script will only run in a Mac environment."
exit 1
fi

View file

@ -179,7 +179,7 @@ function _builder_prepublish() {
builder_echo "Manually linking $link_source -> $link_target (see https://github.com/npm/cli/issues/3466)"
rm -rf $link_source
if [[ $BUILDER_OS == win ]]; then
if builder_is_windows; then
link_source="$(cygpath -w "$link_source")"
link_target="$(cygpath -w "$link_target")"
cmd //c mklink //j "$link_source" "$link_target"

View file

@ -205,26 +205,6 @@ printBuildNumberForTeamCity
findShouldSentryRelease
# Sets the BUILDER_OS environment variable to linux|mac|win
#
_builder_get_operating_system() {
declare -g BUILDER_OS
# Default value, since it's the most general case/configuration to detect.
BUILDER_OS=linux
# Subject to change with future improvements.
if [[ $OSTYPE == darwin* ]]; then
BUILDER_OS=mac
elif [[ $OSTYPE == msys ]]; then
BUILDER_OS=win
elif [[ $OSTYPE == cygwin ]]; then
BUILDER_OS=win
fi
readonly BUILDER_OS
}
_builder_get_operating_system
# Intended for use with macOS-based builds, as Xcode build phase "run script"s do not have access to important
# environment variables. Doesn't hurt to run it at other times as well. The output file is .gitignore'd.
function exportEnvironmentDefinitionScript() {
@ -259,7 +239,8 @@ function exportEnvironmentDefinitionScript() {
# someone else to intentionally use, so this check seems reasonable.
#
# https://gist.github.com/gdavis/6670468 has a representative copy of a standard Xcode environment variable setup.
if [ "$BUILDER_OS" == "mac" ] && [[ -z "${XCODE_VERSION_ACTUAL:-}" ]] && [[ -z "${XCODE_PRODUCT_BUILD_VERSION:-}" ]]; then
# shellcheck disable=2310
if builder_is_macos && [[ -z "${XCODE_VERSION_ACTUAL:-}" ]] && [[ -z "${XCODE_PRODUCT_BUILD_VERSION:-}" ]]; then
exportEnvironmentDefinitionScript
fi

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash
# shellcheck shell=bash
#
# Setup JQ environment variable according to the user's system
#
@ -13,22 +13,17 @@ if [[ -z "${JQ+x}" ]]; then
# . "${THIS_SCRIPT%/*}/build-utils.sh"
## END STANDARD BUILD SCRIPT INCLUDE
case "${OSTYPE}" in
"cygwin")
JQ=$(dirname "$JQ_THIS_SCRIPT")/jq-win64.exe
;;
"msys")
JQ=$(dirname "$JQ_THIS_SCRIPT")/jq-win64.exe
;;
*)
JQ=jq
;;
esac
if builder_is_windows; then
JQ=$(dirname "${JQ_THIS_SCRIPT}")/jq-win64.exe
else
JQ=jq
fi
readonly JQ
# JQ with inplace file replacement
function jqi() {
cat <<< "$($JQ -c "$1" < "$2")" > "$2"
# shellcheck disable=2312
cat <<< "$("${JQ}" -c "$1" < "$2")" > "$2"
}
fi
fi

View file

@ -51,14 +51,14 @@ function add_zip_files() {
shift
;;
-[0123456789])
# Compression level where
# Compression level where
# -0 indicates no compression
# -1 indicates low compression (fastest)
# -9 indicates ultra compression (slowest)
ZIP_FLAGS+=($1)
if [[ $1 =~ -([0-9]) ]]; then
SEVENZ_FLAGS+=("-mx${BASH_REMATCH[1]}")
fi
fi
shift;
;;
@ -80,14 +80,12 @@ function add_zip_files() {
if ! command -v zip 2>&1 > /dev/null; then
# Fallback to 7z
if [[ -z "${SEVENZ+x}" ]]; then
case "${OSTYPE}" in
"cygwin"|"msys")
SEVENZ="${SEVENZ_HOME}"/7z.exe
;;
*)
SEVENZ=7z
;;
esac
if builder_is_windows; then
# shellcheck disable=2154
SEVENZ="${SEVENZ_HOME}/7z.exe"
else
SEVENZ=7z
fi
fi
# 7z command to add files so clear zip flags

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash
# shellcheck shell=bash
# shellcheck disable=SC2310
# Note: these two lines can be uncommented for debugging and profiling build
@ -33,6 +33,7 @@ SHLVL=0
# _builder_init is called internally at the bottom of this file after we have
# all function declarations in place.
function _builder_init() {
_builder_get_operating_system
_builder_findRepoRoot
_builder_setBuildScriptIdentifiers
@ -2090,27 +2091,14 @@ builder_describe_platform() {
local builder_platforms=(linux mac win)
local builder_tools=(android-studio delphi)
# --- Detect platform ---
# Default value, since it's the most general case/configuration to detect.
local builder_platform=linux
# This is copied from build-utils.sh to avoid creating a dependency on it
if [[ $OSTYPE == darwin* ]]; then
builder_platform=mac
elif [[ $OSTYPE == msys ]]; then
builder_platform=win
elif [[ $OSTYPE == cygwin ]]; then
builder_platform=win
fi
local builder_platform="${BUILDER_OS}"
# --- Detect tools ---
local builder_installed_tools=()
# Detect delphi compiler (see also delphi_environment.inc.sh)
if [[ $builder_platform == win ]]; then
if builder_is_windows; then
local ProgramFilesx86="$(cygpath -w -F 42)"
if [[ -x "$(cygpath -u "$ProgramFilesx86\\Embarcadero\\Studio\\20.0\\bin\\dcc32.exe")" ]]; then
builder_installed_tools+=(delphi)
@ -2278,6 +2266,50 @@ builder_if_release_build_level() {
"$@"
}
# Returns 0 if we're running on Windows, i.e. if the environment variable
# `OSTYPE` is set to "msys" or "cygwin".
builder_is_windows() {
if [[ "${OSTYPE:-}" == "msys" ]] || [[ "${OSTYPE:-}" == "cygwin" ]]; then
return 0
else
return 1
fi
}
# Returns 0 if we're running on macOS.
builder_is_macos() {
if [[ "${OSTYPE:-}" == darwin* ]]; then
return 0
else
return 1
fi
}
# Returns 0 if we're running on Linux.
builder_is_linux() {
if [[ "${OSTYPE:-}" == "linux-gnu" ]]; then
return 0
else
return 1
fi
}
# Sets the BUILDER_OS environment variable to linux|mac|win
#
_builder_get_operating_system() {
declare -g BUILDER_OS
if builder_is_macos; then
BUILDER_OS=mac
elif builder_is_windows; then
BUILDER_OS=win
else
BUILDER_OS=linux
fi
readonly BUILDER_OS
}
################################################################################
# Final initialization
################################################################################

View file

@ -63,8 +63,8 @@ is_default_values() {
# Check that `docker buildx` is available
check_buildx_available() {
if ! docker --help | grep -q buildx; then
if [[ "${OSTYPE:-}" == "linux-gnu" ]]; then
builder_echo error "Docker buildx is not available. Please install Docker buildx to use this script. e.g. 'sudo apt install docker-buildx'"
if builder_is_linux; then
builder_echo error "Docker buildx is not available. Please install Docker buildx to use this script. E.g. 'sudo apt install docker-buildx'"
else
builder_echo error "Docker buildx is not available. Please install Docker buildx to use this script."
fi

View file

@ -20,7 +20,7 @@
#
locate_emscripten() {
local EMCC_EXECUTABLE
if [[ "${BUILDER_OS}" == "win" ]]; then
if builder_is_windows; then
EMCC_EXECUTABLE="emcc.py"
else
EMCC_EXECUTABLE="emcc"

View file

@ -6,7 +6,7 @@
# Allows for a quick macOS check for those scripts requiring a macOS environment.
verify_on_mac() {
if [[ "${OSTYPE}" != "darwin"* ]]; then
if ! builder_is_macos; then
builder_die "This build script will only run in a Mac environment."
exit 1
fi
@ -111,7 +111,7 @@ write_download_info() {
_verify_project "${PLATFORM}"
# shellcheck disable=SC2312
if [[ "${BUILDER_OS}" == "mac" ]] && [[ $(command -v stat) == /usr/bin/stat ]]; then
if builder_is_macos && [[ $(command -v stat) == /usr/bin/stat ]]; then
# /usr/bin/stat on mac is BSD
STAT_FLAGS="-f%z"
else
@ -271,24 +271,25 @@ verify_npm_setup() {
}
_print_expected_node_version() {
"$JQ" -r '.engines.node' "$KEYMAN_ROOT/package.json"
"$JQ" -r '.engines.node' "$KEYMAN_ROOT/package.json"
}
# Use nvm to select a node version according to package.json
# see /docs/build/node.md
_select_node_version_with_nvm() {
local REQUIRED_NODE_VERSION="$(_print_expected_node_version)"
local CURRENT_NODE_VERSION
local REQUIRED_NODE_VERSION CURRENT_NODE_VERSION
if [[ $BUILDER_OS != win ]]; then
# launch nvm in a sub process, see _builder_nvm.sh for details
"$KEYMAN_ROOT/resources/build/_builder_nvm.sh" "$REQUIRED_NODE_VERSION"
else
REQUIRED_NODE_VERSION="$(_print_expected_node_version)"
if builder_is_windows; then
CURRENT_NODE_VERSION="$(node --version)"
if [[ "$CURRENT_NODE_VERSION" != "v$REQUIRED_NODE_VERSION" ]]; then
start //wait //b nvm install "$REQUIRED_NODE_VERSION"
start //wait //b nvm use "$REQUIRED_NODE_VERSION"
if [[ "${CURRENT_NODE_VERSION}" != "v${REQUIRED_NODE_VERSION}" ]]; then
start //wait //b nvm install "${REQUIRED_NODE_VERSION}"
start //wait //b nvm use "${REQUIRED_NODE_VERSION}"
fi
else
# launch nvm in a sub process, see _builder_nvm.sh for details
"${KEYMAN_ROOT}/resources/build/_builder_nvm.sh" "${REQUIRED_NODE_VERSION}"
fi
# Now, check that the node version is correct, on all systems

View file

@ -92,7 +92,7 @@ function _publish_to_playstore() {
}
function do_publish() {
if ! is_windows; then
if ! builder_is_windows; then
# currently only tested on Windows, TODO: test cross-platform
builder_echo error "This script is intended to be run on Windows only."
return 1

View file

@ -22,11 +22,11 @@ builder_describe \
builder_parse "$@"
if is_ubuntu; then
if builder_is_linux; then
ARCH=arch
elif is_windows; then
elif builder_is_windows; then
ARCH=win
elif is_macos; then
elif builder_is_macos; then
ARCH=mac
else
builder_echo error "Unknown architecture"

View file

@ -2,7 +2,7 @@
# Keyman is copyright (C) SIL Global. MIT License.
developer_install_dependencies_on_linux_action() {
if ! is_ubuntu; then
if ! builder_is_linux; then
return 0
fi

View file

@ -142,7 +142,7 @@ function build_developer_action() {
}
function publish_action() {
if ! is_windows; then
if ! builder_is_windows; then
# requires Powershell, so currently only supported on Windows
builder_echo error "This script is intended to be run on Windows only."
return 1

View file

@ -38,7 +38,7 @@ function build_developer_action() {
builder_echo end "build developer" success "Finished building Keyman Developer"
}
if is_windows; then
if builder_is_windows; then
builder_echo error "This script is intended to be run on Linux or macOS only."
exit 1
fi

View file

@ -25,7 +25,7 @@ builder_describe \
builder_parse "$@"
if ! is_windows; then
if ! builder_is_windows; then
builder_echo error "This script is intended to be run on Windows only."
exit 1
fi

View file

@ -1,36 +1,8 @@
# shellcheck shell=bash
# Keyman is copyright (C) SIL Global. MIT License.
# Returns 0 if we're running on Ubuntu.
is_ubuntu() {
if [[ "${OSTYPE:-}" == "linux-gnu" ]]; then
return 0
else
return 1
fi
}
# Returns 0 if we're running on Windows, i.e. if the environment variable
# `OSTYPE` is set to "msys" or "cygwin".
is_windows() {
if [[ "${OSTYPE:-}" == "msys" ]] || [[ "${OSTYPE:-}" == "cygwin" ]]; then
return 0
else
return 1
fi
}
# Returns 0 if we're running on macOS.
is_macos() {
if [[ "${OSTYPE:-}" == darwin* ]]; then
return 0
else
return 1
fi
}
install_nvm() {
if ! is_ubuntu; then
if ! builder_is_linux; then
# on Windows and macOS build agents are configured manually
return 0
fi
@ -55,7 +27,7 @@ set_variables_for_nvm() {
}
install_emscripten() {
if ! is_ubuntu; then
if ! builder_is_linux; then
# on Windows and macOS build agents are configured manually
return 0
fi
@ -92,7 +64,7 @@ _tc_rsync() {
"--rsync-path=${RSYNC_PATH}" # path on remote server
)
if is_windows; then
if builder_is_windows; then
rsync_args+=(
'--chmod=Dug=rwx,Do=rx,Fug=rw,Fo=r' # map Windows security to host security
"--rsh=${RSYNC_HOME}\ssh -i ${USERPROFILE}\.ssh\id_rsa -o UserKnownHostsFile=${USERPROFILE}\.ssh\known_hosts" # use ssh
@ -108,13 +80,13 @@ _tc_rsync() {
"${DESTINATION}"
)
if is_windows; then
if builder_is_windows; then
local CYGPATH_RSYNC_HOME
CYGPATH_RSYNC_HOME=$(cygpath -w "${RSYNC_HOME}")
MSYS_NO_PATHCONV=1 "${CYGPATH_RSYNC_HOME}\\rsync.exe" "${rsync_args[@]}"
else
local RSYNC=rsync
if is_macos; then
if builder_is_macos; then
RSYNC=/usr/local/bin/rsync
[[ -f /opt/homebrew/bin/rsync ]] && RSYNC=/opt/homebrew/bin/rsync
fi

View file

@ -25,7 +25,7 @@ _is_package_installed() {
# Parameters:
# $* - List of package names to check and install (e.g., "lcov jq")
ba_linux_check_and_install_packages() {
if ! is_ubuntu; then
if ! builder_is_linux; then
return 0
fi

View file

@ -73,7 +73,7 @@ function publish_web_action() {
builder_echo start publish "Publishing KeymanWeb release"
# TODO: refactor to allow to run on Linux/macOS as well
if ! is_windows; then
if ! builder_is_windows; then
builder_echo end publish error "Publishing KeymanWeb is only supported on Windows"
return 1
fi

View file

@ -2,7 +2,7 @@
# Keyman is copyright (C) SIL Global. MIT License.
web_install_dependencies_on_linux_action() {
if ! is_ubuntu; then
if ! builder_is_linux; then
return 0
fi
@ -18,7 +18,7 @@ web_install_dependencies_on_linux_action() {
}
_install_playwright_dependencies() {
if ! is_ubuntu || ! is_os_version_or_higher 24.04; then
if ! builder_is_linux || ! is_os_version_or_higher 24.04; then
return 0
fi
@ -40,14 +40,14 @@ web_build_action() {
web_test_action() {
builder_echo start web_test "Running tests for native KeymanWeb"
if is_ubuntu; then
if builder_is_linux; then
ba_linux_start_xvfb
trap "ba_linux_stop_xvfb" ERR
fi
"${KEYMAN_ROOT}/web/ci.sh" test
if is_ubuntu; then
if builder_is_linux; then
ba_linux_stop_xvfb
trap ERR
fi

View file

@ -36,7 +36,7 @@ builder_parse "$@"
cd "${KEYMAN_ROOT}/windows/src"
if ! is_windows; then
if ! builder_is_windows; then
builder_echo error "This script is intended to be run on Windows only."
exit 1
fi

View file

@ -30,7 +30,7 @@ builder_parse "$@"
cd "${KEYMAN_ROOT}/windows/src"
if ! is_windows; then
if ! builder_is_windows; then
builder_echo error "This script is intended to be run on Windows only."
exit 1
fi