|
| 1 | +#!/bin/bash |
| 2 | +# Headless Arduino IDE 1.x / arduino-nightly builder for CI (upload, board install). |
| 3 | + |
| 4 | +[ -n "$ARDUINO_HEADLESS_SOURCED" ] && return 0 |
| 5 | +ARDUINO_HEADLESS_SOURCED=1 |
| 6 | + |
| 7 | +_HEADLESS_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | +SCRIPTS_DIR="${SCRIPTS_DIR:-$_HEADLESS_LIB_DIR}" |
| 9 | + |
| 10 | +function run_with_timeout { |
| 11 | + local seconds="$1" pid elapsed rc |
| 12 | + shift |
| 13 | + "$@" & |
| 14 | + pid=$! |
| 15 | + elapsed=0 |
| 16 | + while kill -0 "$pid" 2>/dev/null; do |
| 17 | + if [ "$elapsed" -ge "$seconds" ]; then |
| 18 | + echo "ERROR: timed out after ${seconds}s" >&2 |
| 19 | + kill "$pid" 2>/dev/null || true |
| 20 | + wait "$pid" 2>/dev/null || true |
| 21 | + return 124 |
| 22 | + fi |
| 23 | + sleep 1 |
| 24 | + elapsed=$((elapsed + 1)) |
| 25 | + done |
| 26 | + wait "$pid" |
| 27 | + rc=$? |
| 28 | + return "$rc" |
| 29 | +} |
| 30 | + |
| 31 | +function _arduino_headless_log_filter_enabled { |
| 32 | + case "${ARDUINO_HEADLESS_LOG_FILTER:-1}" in |
| 33 | + 0 | n | N | no | NO | false | FALSE | off | OFF) return 1 ;; |
| 34 | + *) return 0 ;; |
| 35 | + esac |
| 36 | +} |
| 37 | + |
| 38 | +function _filter_arduino_headless_log_noise { |
| 39 | + # arduino-nightly mDNS (jmdns) spams WARN + hex dumps on cloud VM hostnames; not actionable in CI. |
| 40 | + LC_ALL=C grep -aEv \ |
| 41 | + -e '^(DEBUG|TRACE|INFO) StatusLogger' \ |
| 42 | + -e '^Picked up JAVA_TOOL_OPTIONS:' \ |
| 43 | + -e 'Arduino\[[0-9]+:[0-9]+\] ' \ |
| 44 | + -e '\[JRSAppKitAWT markAppIsDaemon\]' \ |
| 45 | + -e '^[[:space:]]+"-' \ |
| 46 | + -e '^[[:space:]]*[{}][[:space:]]*$' \ |
| 47 | + -e '^[[:space:]]*\)' \ |
| 48 | + -e '^JVM(Runtime|Options|Arguments|Classpath|DefaultOptions|MainClassName)' \ |
| 49 | + -e '^(CFBundleName|SearchSystemJVM|Command line passed|WorkingDirectory)=' \ |
| 50 | + -e 'javax\.jmdns' \ |
| 51 | + -e 'dns\[(query|response),' \ |
| 52 | + -e '^questions:$' \ |
| 53 | + -e 'questions=' \ |
| 54 | + -e '[[:space:]]*question:' \ |
| 55 | + -e 'DNSQuestion@' \ |
| 56 | + -e 'TYPE_IGNORE' \ |
| 57 | + -e 'CLASS_UNKNOWN' \ |
| 58 | + -e 'flags=0x[0-9a-fA-F]+:' \ |
| 59 | + -e 'bx-internal-cloud' \ |
| 60 | + -e '\.local' \ |
| 61 | + -e '^[[:space:]]*\[[[:space:]]*DNSQuestion@' \ |
| 62 | + -e '^[[:space:]]*[0-9a-fA-F]+: [0-9a-fA-F ]' \ |
| 63 | + -e '\]\]' \ |
| 64 | + | python3 -u -c 'import sys |
| 65 | +for raw in sys.stdin.buffer: |
| 66 | + try: |
| 67 | + sys.stdout.write(raw.decode("ascii")) |
| 68 | + except UnicodeDecodeError: |
| 69 | + pass' |
| 70 | +} |
| 71 | + |
| 72 | +function _run_arduino_headless_filtered { |
| 73 | + local rc |
| 74 | + if _arduino_headless_log_filter_enabled; then |
| 75 | + set -o pipefail |
| 76 | + "$@" 2>&1 | _filter_arduino_headless_log_noise |
| 77 | + rc=${PIPESTATUS[0]} |
| 78 | + else |
| 79 | + "$@" |
| 80 | + rc=$? |
| 81 | + fi |
| 82 | + return "$rc" |
| 83 | +} |
| 84 | + |
| 85 | +function _arduino_headless_java_bin { |
| 86 | + local appdir="$1" |
| 87 | + if [ -x "$appdir/java/bin/java" ]; then |
| 88 | + echo "$appdir/java/bin/java" |
| 89 | + return 0 |
| 90 | + fi |
| 91 | + command -v java |
| 92 | +} |
| 93 | + |
| 94 | +function _arduino_headless_classpath { |
| 95 | + local appdir="$1" cp |
| 96 | + cp=$(printf '%s:' "$appdir"/lib/*.jar) |
| 97 | + echo "${cp%:}" |
| 98 | +} |
| 99 | + |
| 100 | +function _arduino_headless_jul_config { |
| 101 | + local props="$_HEADLESS_LIB_DIR/arduino-headless-logging.properties" |
| 102 | + [ -f "$props" ] || return 1 |
| 103 | + printf '%s' "$props" |
| 104 | +} |
| 105 | + |
| 106 | +function _run_arduino_headless { |
| 107 | + local appdir="$1" |
| 108 | + shift |
| 109 | + local java_bin classpath jul_config |
| 110 | + local -a java_props=() java_cmd=() |
| 111 | + [ -d "$appdir" ] || { echo "ERROR: Arduino tree not found at $appdir" >&2; return 1; } |
| 112 | + |
| 113 | + jul_config="$(_arduino_headless_jul_config || true)" |
| 114 | + if [ -n "$jul_config" ]; then |
| 115 | + export JAVA_TOOL_OPTIONS="-Djava.awt.headless=true -Djava.util.logging.config.file=$jul_config" |
| 116 | + java_props=(-Djava.awt.headless=true "-Djava.util.logging.config.file=$jul_config") |
| 117 | + else |
| 118 | + export JAVA_TOOL_OPTIONS="-Djava.awt.headless=true" |
| 119 | + java_props=(-Djava.awt.headless=true) |
| 120 | + fi |
| 121 | + |
| 122 | + if [[ "${OS_IS_WINDOWS:-0}" == "1" ]]; then |
| 123 | + MSYS2_ARG_CONV_EXCL="*" MSYS_NO_PATHCONV=1 \ |
| 124 | + _run_arduino_headless_filtered "$appdir/arduino_debug.exe" "$@" |
| 125 | + return |
| 126 | + fi |
| 127 | + |
| 128 | + if [[ "${OS_IS_MACOS:-0}" == "1" ]]; then |
| 129 | + local app_bundle launcher |
| 130 | + app_bundle="$(cd "$appdir/../.." && pwd)" |
| 131 | + launcher="$app_bundle/Contents/MacOS/Arduino" |
| 132 | + [ -x "$launcher" ] || { echo "ERROR: Arduino launcher not found at $launcher" >&2; return 1; } |
| 133 | + if [ -n "$jul_config" ]; then |
| 134 | + export JAVA_TOOL_OPTIONS="-Djava.awt.headless=true -Dapple.awt.UIElement=true -Djava.util.logging.config.file=$jul_config" |
| 135 | + else |
| 136 | + export JAVA_TOOL_OPTIONS="-Djava.awt.headless=true -Dapple.awt.UIElement=true" |
| 137 | + fi |
| 138 | + if [[ "$(uname -m)" == "arm64" ]]; then |
| 139 | + _run_arduino_headless_filtered arch -x86_64 "$launcher" "$@" |
| 140 | + else |
| 141 | + _run_arduino_headless_filtered "$launcher" "$@" |
| 142 | + fi |
| 143 | + return |
| 144 | + fi |
| 145 | + |
| 146 | + java_bin="$(_arduino_headless_java_bin "$appdir")" |
| 147 | + [ -n "$java_bin" ] || { echo "ERROR: java not found for $appdir" >&2; return 1; } |
| 148 | + classpath="$(_arduino_headless_classpath "$appdir")" |
| 149 | + java_cmd=("$java_bin" "${java_props[@]}" -DAPP_DIR="$appdir" -cp "$classpath" processing.app.Base "$@") |
| 150 | + if [[ "${OS_IS_LINUX:-0}" == "1" ]] && command -v xvfb-run >/dev/null 2>&1; then |
| 151 | + _run_arduino_headless_filtered xvfb-run -a "${java_cmd[@]}" |
| 152 | + else |
| 153 | + _run_arduino_headless_filtered "${java_cmd[@]}" |
| 154 | + fi |
| 155 | +} |
| 156 | + |
| 157 | +function run_arduino_ide_v1 { |
| 158 | + _run_arduino_headless "${ARDUINO_IDE_PATH:?ARDUINO_IDE_PATH required — source install-arduino-ide.sh first}" "$@" |
| 159 | +} |
| 160 | + |
| 161 | +function run_arduino_builder { |
| 162 | + _run_arduino_headless "${ARDUINO_BUILDER_PATH:?ARDUINO_BUILDER_PATH required — source install-arduino-builder.sh first}" "$@" |
| 163 | +} |
0 commit comments