|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Build hidapi library locally for development. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# ./dev/macos_dev_setup.sh |
| 8 | +# MACOS_UNIVERSAL2=0 ./dev/build_hidapi.sh # macOS: don't build universal2 |
| 9 | +# |
| 10 | +# Resulting libs: |
| 11 | +# macOS: build/local-hidapi/macos/lib/libhidapi*.dylib |
| 12 | +# Linux: build/local-hidapi/linux/lib/libhidapi-hidraw.so |
| 13 | +# Windows: build/local-hidapi/windows/bin/hidapi.dll |
| 14 | + |
| 15 | +. ./plover_build_utils/deps.sh |
| 16 | +. ./plover_build_utils/functions.sh |
| 17 | + |
| 18 | +python='python' |
| 19 | + |
| 20 | +MACOS_UNIVERSAL2="${MACOS_UNIVERSAL2:-1}" |
| 21 | + |
| 22 | +# Resolve repo root relative to this script |
| 23 | +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" |
| 24 | +REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)" |
| 25 | + |
| 26 | +WORK_DIR="${REPO_ROOT}/build/local-hidapi" |
| 27 | +SRC_DIR="${WORK_DIR}/src" |
| 28 | +BUILD_DIR="${WORK_DIR}/build" |
| 29 | +OUT_LIB_DIR_MAC="${WORK_DIR}/macos/lib" |
| 30 | +OUT_LIB_DIR_LNX="${WORK_DIR}/linux/lib" |
| 31 | +OUT_BIN_DIR_WIN="${WORK_DIR}/windows/bin" |
| 32 | + |
| 33 | +# Clean and prep |
| 34 | +# Prepare directories |
| 35 | +mkdir -p "${SRC_DIR}" "${BUILD_DIR}" |
| 36 | + |
| 37 | +OS="$(uname -s || true)" |
| 38 | +# If outputs already exist for the current platform, skip rebuilding. |
| 39 | +if [[ "${OS}" == "Darwin" ]]; then |
| 40 | + if ls "${OUT_LIB_DIR_MAC}"/libhidapi*.dylib >/dev/null 2>&1; then |
| 41 | + echo "hidapi found at ${OUT_LIB_DIR_MAC}; skipping build." |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +elif [[ "${OS}" == "Linux" ]]; then |
| 45 | + if ls "${OUT_LIB_DIR_LNX}"/libhidapi-hidraw.so* >/dev/null 2>&1; then |
| 46 | + echo "hidapi found at ${OUT_LIB_DIR_LNX}; skipping build." |
| 47 | + exit 0 |
| 48 | + fi |
| 49 | +elif [[ "${OS}" == MINGW* || "${OS}" == MSYS* || "${OS}" == CYGWIN* || "${OS}" == "Windows_NT" ]]; then |
| 50 | + if [ -f "${OUT_BIN_DIR_WIN}/hidapi.dll" ]; then |
| 51 | + echo "hidapi found at ${OUT_BIN_DIR_WIN}/hidapi.dll; skipping build." |
| 52 | + exit 0 |
| 53 | + fi |
| 54 | +fi |
| 55 | + |
| 56 | +# Clean and prep for a fresh build |
| 57 | +rm -rf "${SRC_DIR}" "${BUILD_DIR}" |
| 58 | +mkdir -p "${SRC_DIR}" "${BUILD_DIR}" |
| 59 | + |
| 60 | +echo "==> Downloading & unpacking hidapi ${hidapi_version}" |
| 61 | +fetch_hidapi "${SRC_DIR}" "${WORK_DIR}" |
| 62 | + |
| 63 | + |
| 64 | +if [[ "${OS}" == "Darwin" ]]; then |
| 65 | + echo "==> Configuring (macOS, IOHIDManager backend)" |
| 66 | + |
| 67 | + # Shared fetch/build helpers (macOS) |
| 68 | + # shellcheck disable=SC1091 |
| 69 | + . "${REPO_ROOT}/osx/build_hidapi.sh" |
| 70 | + |
| 71 | + mkdir -p "${OUT_LIB_DIR_MAC}" |
| 72 | + # Architectures |
| 73 | + if [[ "${MACOS_UNIVERSAL2}" == "1" ]]; then |
| 74 | + OSX_ARCHES="x86_64;arm64" |
| 75 | + else |
| 76 | + ARCH="$(uname -m || true)" |
| 77 | + if [[ "${ARCH}" == "arm64" || "${ARCH}" == "aarch64" ]]; then |
| 78 | + OSX_ARCHES="arm64" |
| 79 | + else |
| 80 | + OSX_ARCHES="x86_64" |
| 81 | + fi |
| 82 | + fi |
| 83 | + |
| 84 | + cmake_build_macos "${SRC_DIR}" "${BUILD_DIR}" "${OSX_ARCHES}" "RelWithDebInfo" |
| 85 | + |
| 86 | + # Find produced dylib |
| 87 | + DYLIB="$(/usr/bin/find "${BUILD_DIR}" -type f -name 'libhidapi*.dylib' -print -quit || true)" |
| 88 | + if [[ -z "${DYLIB}" ]]; then |
| 89 | + echo "Error: libhidapi*.dylib not found in build output." >&2 |
| 90 | + exit 3 |
| 91 | + fi |
| 92 | + |
| 93 | + BASENAME="$(basename "${DYLIB}")" |
| 94 | + echo "==> Staging ${BASENAME}" |
| 95 | + cp -f "${DYLIB}" "${OUT_LIB_DIR_MAC}/${BASENAME}" |
| 96 | + |
| 97 | + pushd "${OUT_LIB_DIR_MAC}" >/dev/null |
| 98 | + ln -sf "${BASENAME}" libhidapi.dylib || true |
| 99 | + popd >/dev/null |
| 100 | + |
| 101 | + echo |
| 102 | + echo "✅ Built macOS hidapi at: ${OUT_LIB_DIR_MAC}/${BASENAME}" |
| 103 | + |
| 104 | +elif [[ "${OS}" == "Linux" ]]; then |
| 105 | + echo "==> Configuring (Linux, hidraw backend)" |
| 106 | + mkdir -p "${OUT_LIB_DIR_LNX}" |
| 107 | + |
| 108 | + # Shared fetch/build helpers (Linux) |
| 109 | + # shellcheck disable=SC1091 |
| 110 | + . "${REPO_ROOT}/linux/build_hidapi.sh" |
| 111 | + |
| 112 | + cmake_build_linux "${SRC_DIR}" "${BUILD_DIR}" "RelWithDebInfo" |
| 113 | + |
| 114 | + SO="$(/usr/bin/find "${BUILD_DIR}" -type f -name 'libhidapi-hidraw.so*' -print -quit || true)" |
| 115 | + if [[ -z "${SO}" ]]; then |
| 116 | + echo "Error: libhidapi-hidraw.so not found in build output." >&2 |
| 117 | + exit 3 |
| 118 | + fi |
| 119 | + |
| 120 | + BASENAME="$(basename "${SO}")" |
| 121 | + echo "==> Staging ${BASENAME}" |
| 122 | + cp -f "${SO}" "${OUT_LIB_DIR_LNX}/${BASENAME}" |
| 123 | + |
| 124 | + pushd "${OUT_LIB_DIR_LNX}" >/dev/null |
| 125 | + [[ -e libhidapi-hidraw.so ]] || ln -sf "${BASENAME}" libhidapi-hidraw.so || true |
| 126 | + popd >/dev/null |
| 127 | + |
| 128 | + echo |
| 129 | + echo "✅ Built Linux hidapi at: ${OUT_LIB_DIR_LNX}/${BASENAME}" |
| 130 | + |
| 131 | +elif [[ "${OS}" == MINGW* || "${OS}" == MSYS* || "${OS}" == CYGWIN* || "${OS}" == "Windows_NT" ]]; then |
| 132 | + echo "==> Configuring (Windows, WinAPI backend)" |
| 133 | + mkdir -p "${OUT_BIN_DIR_WIN}" |
| 134 | + |
| 135 | + # Shared fetch/build helpers (Windows) |
| 136 | + # shellcheck disable=SC1091 |
| 137 | + . "${REPO_ROOT}/windows/build_hidapi.sh" |
| 138 | + |
| 139 | + # Build shared DLL |
| 140 | + cmake_build_windows "${SRC_DIR}" "${BUILD_DIR}" "Release" |
| 141 | + |
| 142 | + # Find the produced DLL (varies by generator) |
| 143 | + DLL="$(/usr/bin/find "${BUILD_DIR}" -type f \( -iname 'hidapi*.dll' -o -iname 'libhidapi*.dll' \) -print -quit 2>/dev/null || true)" |
| 144 | + if [[ -z "${DLL}" ]]; then |
| 145 | + echo "Error: hidapi DLL not found in build output." >&2 |
| 146 | + exit 3 |
| 147 | + fi |
| 148 | + |
| 149 | + BASENAME="$(basename "${DLL}")" |
| 150 | + echo "==> Staging ${BASENAME}" |
| 151 | + cp -f "${DLL}" "${OUT_BIN_DIR_WIN}/hidapi.dll" |
| 152 | + |
| 153 | + echo |
| 154 | + echo "✅ Built Windows hidapi at: ${OUT_BIN_DIR_WIN}/hidapi.dll" |
| 155 | +else |
| 156 | + echo "Unsupported OS: ${OS}. This helper currently supports macOS, Linux, and Windows." >&2 |
| 157 | + exit 4 |
| 158 | +fi |
0 commit comments