summaryrefslogtreecommitdiff
path: root/.github/scripts/deploy-macos-app.sh
blob: f4eaaae71a59a193b42302b9af1a60066bf903a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env bash
set -Eeuo pipefail

usage() {
  cat <<'USAGE'
Usage: deploy-macos-app.sh --app PATH --workspace-prefix PATH --qt-prefix PATH --boost-prefix PATH --fftw-prefix PATH --libusb-prefix PATH --portaudio-prefix PATH --fortran-prefix PATH --gcc-lib PATH [--fortran-original-prefix PATH] [--homebrew-prefix PATH]
USAGE
}

APP=""
WORKSPACE_PREFIX="${GITHUB_WORKSPACE:-}"
QT_PREFIX=""
BOOST_PREFIX=""
FFTW_PREFIX=""
LIBUSB_PREFIX=""
PORTAUDIO_PREFIX=""
FORTRAN_PREFIX=""
FORTRAN_ORIGINAL_PREFIX=""
GCC_LIB=""
HOMEBREW_PREFIX="${HOMEBREW_PREFIX:-}"

while [ "$#" -gt 0 ]; do
  case "$1" in
    --app) APP="$2"; shift 2 ;;
    --workspace-prefix) WORKSPACE_PREFIX="$2"; shift 2 ;;
    --qt-prefix) QT_PREFIX="$2"; shift 2 ;;
    --boost-prefix) BOOST_PREFIX="$2"; shift 2 ;;
    --fftw-prefix) FFTW_PREFIX="$2"; shift 2 ;;
    --libusb-prefix) LIBUSB_PREFIX="$2"; shift 2 ;;
    --portaudio-prefix) PORTAUDIO_PREFIX="$2"; shift 2 ;;
    --fortran-prefix) FORTRAN_PREFIX="$2"; shift 2 ;;
    --fortran-original-prefix) FORTRAN_ORIGINAL_PREFIX="$2"; shift 2 ;;
    --gcc-lib) GCC_LIB="$2"; shift 2 ;;
    --homebrew-prefix) HOMEBREW_PREFIX="$2"; shift 2 ;;
    --help|-h) usage; exit 0 ;;
    *) echo "Unknown argument: $1" >&2; usage >&2; exit 2 ;;
  esac
done

for value_name in APP WORKSPACE_PREFIX QT_PREFIX BOOST_PREFIX FFTW_PREFIX LIBUSB_PREFIX PORTAUDIO_PREFIX FORTRAN_PREFIX GCC_LIB HOMEBREW_PREFIX; do
  if [ -z "${!value_name}" ]; then
    echo "Missing required argument or environment: ${value_name}" >&2
    usage >&2
    exit 2
  fi
done

FRAMEWORKS="${APP}/Contents/Frameworks"
mkdir -p "${FRAMEWORKS}"

dump_bundle_context() {
  local status="$1"
  local line="$2"
  local command="$3"
  echo "::error::deploy-macos-app failed at line ${line} with status ${status}: ${command}"
  echo "APP=${APP}"
  echo "FRAMEWORKS=${FRAMEWORKS}"
  echo "FORTRAN_PREFIX=${FORTRAN_PREFIX}"
  echo "FORTRAN_ORIGINAL_PREFIX=${FORTRAN_ORIGINAL_PREFIX}"
  echo "GCC_LIB=${GCC_LIB}"
  echo "HOMEBREW_PREFIX=${HOMEBREW_PREFIX}"
  echo "App executable contents:"
  ls -la "${APP}/Contents/MacOS" || true
  echo "Framework contents:"
  ls -la "${FRAMEWORKS}" || true
  echo "External references in app executables:"
  find "${APP}/Contents/MacOS" -type f -perm +111 -print -exec otool -L {} \; 2>/dev/null || true
  if [ -f macdeployqt.log ]; then
    echo "macdeployqt.log:"
    cat macdeployqt.log
  fi
}
trap 'status=$?; dump_bundle_context "$status" "$LINENO" "$BASH_COMMAND"; exit "$status"' ERR

run_logged() {
  echo "+ $*"
  "$@"
}

echo "Running macdeployqt..."
set +e
"${QT_PREFIX}/bin/macdeployqt" "${APP}" -verbose=2 > macdeployqt.log 2>&1
status=$?
set -e
if [ "$status" -ne 0 ]; then
  echo "::error::macdeployqt failed with status ${status}"
  dump_bundle_context "$status" "$LINENO" "macdeployqt"
  exit "$status"
fi
cat macdeployqt.log

resolve_lib() {
  local lib="$1"
  if [ -n "$FORTRAN_ORIGINAL_PREFIX" ] && [[ "$lib" == "$FORTRAN_ORIGINAL_PREFIX"/* ]]; then
    local original_prefix="${FORTRAN_ORIGINAL_PREFIX}/"
    local relocated="${FORTRAN_PREFIX}/${lib:${#original_prefix}}"
    [ -f "$relocated" ] && echo "$relocated" && return 0
  fi
  if [[ "$lib" == $HOMEBREW_PREFIX/* ]]; then
    echo "$lib"
    return 0
  elif [[ "$lib" == "$WORKSPACE_PREFIX"/* ]]; then
    echo "$lib"
    return 0
  elif [[ "$lib" == @rpath/* ]]; then
    local name="${lib#@rpath/}"
    for dir in \
      "$FRAMEWORKS" \
      "${BOOST_PREFIX}/lib" \
      "${FFTW_PREFIX}/lib" \
      "${LIBUSB_PREFIX}/lib" \
      "${PORTAUDIO_PREFIX}/lib" \
      "${FORTRAN_PREFIX}/lib" \
      "$GCC_LIB" \
      "${HOMEBREW_PREFIX}/lib"
    do
      [ -f "$dir/$name" ] && echo "$dir/$name" && return
    done
  fi
  return 0
}

should_bundle_ref() {
  local ref="$1"
  if [ -n "$FORTRAN_ORIGINAL_PREFIX" ] && [[ "$ref" == "$FORTRAN_ORIGINAL_PREFIX"/* ]]; then
    return 0
  fi
  case "$ref" in
    *Qt*.framework/*)
      return 1
      ;;
    "$HOMEBREW_PREFIX"/*|"$WORKSPACE_PREFIX"/*|@rpath/*)
      return 0
      ;;
    *)
      return 1
      ;;
  esac
}

bundle_dylib() {
  local binary="$1"
  local lib
  while IFS= read -r lib; do
    should_bundle_ref "$lib" || continue
    local resolved
    resolved=$(resolve_lib "$lib")
    if [ -z "$resolved" ]; then
      echo "::error::Could not resolve ${lib} required by ${binary}"
      exit 1
    fi
    local libname
    libname=$(basename "$resolved")
    case "$libname" in
      libgomp*.dylib)
        echo "::error::Refusing to bundle ${resolved}; libgomp must be linked statically for deployable macOS artifacts."
        exit 1
        ;;
    esac
    if [ ! -f "${FRAMEWORKS}/${libname}" ]; then
      echo "  Bundling: $resolved -> ${FRAMEWORKS}/${libname}"
      run_logged cp "$resolved" "${FRAMEWORKS}/${libname}"
      run_logged chmod 644 "${FRAMEWORKS}/${libname}"
      run_logged install_name_tool -id "@rpath/${libname}" "${FRAMEWORKS}/${libname}"
      bundle_dylib "${FRAMEWORKS}/${libname}"
    fi
    if [ "$lib" != "@rpath/${libname}" ]; then
      run_logged install_name_tool -change "$lib" "@rpath/${libname}" "$binary"
    fi
  done < <(otool -L "$binary" | awk 'NR > 1 {print $1}' || true)
}

ensure_app_rpath() {
  local binary="$1"
  local rpath

  while IFS= read -r rpath; do
    [ -n "$rpath" ] || continue
    case "$rpath" in
      "$WORKSPACE_PREFIX"/*|"$HOMEBREW_PREFIX"/*)
        run_logged install_name_tool -delete_rpath "$rpath" "$binary"
        ;;
    esac
  done < <(otool -l "$binary" | awk '/LC_RPATH/{getline; getline; print $2}' || true)

  local rpaths
  rpaths=$(otool -l "$binary" | awk '/LC_RPATH/{getline; getline; print $2}' || true)
  case $'\n'"$rpaths"$'\n' in
    *$'\n''@executable_path/../Frameworks'$'\n'*) ;;
    *)
      run_logged install_name_tool -add_rpath "@executable_path/../Frameworks" "$binary"
      ;;
  esac
}

for exe in "${APP}/Contents/MacOS/"*; do
  [ -f "$exe" ] && [ -x "$exe" ] && {
    ensure_app_rpath "$exe"
    bundle_dylib "$exe"
    for fw in $(otool -L "$exe" | awk '{print $1}' | grep '\.framework/' || true); do
      fwname=$(basename "$fw")
      bundled=$(find "${FRAMEWORKS}" -name "${fwname}" -path "*.framework/*" -print -quit 2>/dev/null)
      if [ -n "$bundled" ]; then
        app_contents="${APP}/Contents/"
        fwrel="${bundled:${#app_contents}}"
        if [ "$fw" != "@executable_path/../${fwrel}" ]; then
          run_logged install_name_tool -change "$fw" "@executable_path/../${fwrel}" "$exe"
        fi
      fi
    done
  }
done

for fw in "${FRAMEWORKS}"/*.dylib; do
  [ -f "$fw" ] && bundle_dylib "$fw"
done

for exe in "${APP}/Contents/MacOS/"*; do
  [ -f "$exe" ] && [ -x "$exe" ] && bundle_dylib "$exe"
done

echo "Checking for remaining external references..."
remaining_refs="${RUNNER_TEMP:-/tmp}/wsjtx-app-remaining-refs.txt"
: > "$remaining_refs"
while IFS= read -r binary; do
  while IFS= read -r ref; do
    case "$ref" in
      "$HOMEBREW_PREFIX"/*|"$WORKSPACE_PREFIX"/*)
        echo "${binary}: ${ref}" >> "$remaining_refs"
        ;;
    esac
    if [ -n "$FORTRAN_ORIGINAL_PREFIX" ] && [[ "$ref" == "$FORTRAN_ORIGINAL_PREFIX"/* ]]; then
      echo "${binary}: ${ref}" >> "$remaining_refs"
    fi
  done < <(otool -L "$binary" | awk 'NR > 1 {print $1}' || true)
done < <(find "${APP}/Contents/MacOS" -type f -perm +111 -print 2>/dev/null)
if [ -s "$remaining_refs" ]; then
  echo "::error::External build-time paths still present:"
  cat "$remaining_refs"
  exit 1
fi

echo "All dylibs bundled successfully"