summaryrefslogtreecommitdiff
path: root/.github/scripts/verify-macos-static-archives.sh
blob: 419e0674736799c04a52097d7c4405aee80d0d2b (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
#!/usr/bin/env bash
set -euo pipefail

usage() {
  cat <<'USAGE'
Usage: verify-macos-static-archives.sh --target VERSION --arch ARCH [--mode error|warn] PATH...

Verifies static archives before they are linked into deployable macOS binaries:
  * expected architecture
  * object-member deployment targets not newer than --target

--mode error (default) fails the build on any finding. --mode warn keeps
structural problems (missing archive, wrong architecture) fatal but
downgrades object-member deployment-target findings to warning annotations
and exits 0. Member deployment targets in third-party toolchain archives
are outside this workflow's control, so only that uncertain signal is made
non-blocking; a missing input or wrong-arch archive is broken plumbing and
always fails.
USAGE
}

target=""
arch=""
mode="error"
archives=()
max_member_errors=25

while [ "$#" -gt 0 ]; do
  case "$1" in
    --target)
      target="$2"
      shift 2
      ;;
    --arch)
      arch="$2"
      shift 2
      ;;
    --mode)
      mode="$2"
      shift 2
      ;;
    --help|-h)
      usage
      exit 0
      ;;
    --)
      shift
      while [ "$#" -gt 0 ]; do
        archives+=("$1")
        shift
      done
      ;;
    -*)
      echo "Unknown option: $1" >&2
      usage >&2
      exit 2
      ;;
    *)
      archives+=("$1")
      shift
      ;;
  esac
done

if [ -z "$target" ] || [ -z "$arch" ] || [ "${#archives[@]}" -eq 0 ]; then
  usage >&2
  exit 2
fi

case "$mode" in
  error|warn) ;;
  *)
    echo "Invalid --mode: $mode (expected error or warn)" >&2
    exit 2
    ;;
esac

# Annotation level for findings: errors fail the build, warnings do not.
if [ "$mode" = "warn" ]; then
  ann="warning"
else
  ann="error"
fi

version_gt() {
  awk -v a="$1" -v b="$2" '
    BEGIN {
      split(a, av, ".")
      split(b, bv, ".")
      for (i = 1; i <= 3; i++) {
        ai = av[i] + 0
        bi = bv[i] + 0
        if (ai > bi) exit 0
        if (ai < bi) exit 1
      }
      exit 1
    }'
}

check_archive() {
  local archive="$1"
  local structural_fail=0
  local target_fail=0
  local archs
  local line
  local member=""
  local in_build=0
  local in_old=0
  local minos
  local reported=0
  local suppressed=0

  if [ ! -f "$archive" ]; then
    echo "::error::Static archive does not exist: ${archive}"
    return 2
  fi

  archs=$(lipo -archs "$archive" 2>/dev/null || true)
  if [ -n "$archs" ]; then
    case " ${archs} " in
      *" ${arch} "*) ;;
      *)
        echo "::error file=${archive}::Expected architecture ${arch}, found: ${archs}"
        structural_fail=1
        ;;
    esac
  fi

  while IFS= read -r line; do
    case "$line" in
      *".a("*"):")
        member="${line%:}"
        in_build=0
        in_old=0
        ;;
      *"LC_BUILD_VERSION"*)
        in_build=1
        in_old=0
        ;;
      *"LC_VERSION_MIN_MACOSX"*)
        in_build=0
        in_old=1
        ;;
      *" minos "*)
        if [ "$in_build" -eq 1 ]; then
          read -r _ minos _ <<< "$line"
          if version_gt "$minos" "$target"; then
            if [ "$reported" -lt "$max_member_errors" ]; then
              echo "::${ann} file=${archive}::${member} requires macOS ${minos}, newer than archive target ${target}"
            else
              suppressed=$((suppressed + 1))
            fi
            reported=$((reported + 1))
            target_fail=1
          fi
          in_build=0
        fi
        ;;
      *" version "*)
        if [ "$in_old" -eq 1 ]; then
          read -r _ minos _ <<< "$line"
          if version_gt "$minos" "$target"; then
            if [ "$reported" -lt "$max_member_errors" ]; then
              echo "::${ann} file=${archive}::${member} requires macOS ${minos}, newer than archive target ${target}"
            else
              suppressed=$((suppressed + 1))
            fi
            reported=$((reported + 1))
            target_fail=1
          fi
          in_old=0
        fi
        ;;
    esac
  done < <(otool -arch "$arch" -l "$archive" 2>/dev/null || true)

  if [ "$suppressed" -gt 0 ]; then
    echo "::${ann} file=${archive}::Suppressed ${suppressed} additional archive member deployment-target findings"
  fi

  # 2 = structural problem (always fatal), 1 = deployment-target finding
  # (respects --mode), 0 = clean.
  if [ "$structural_fail" -ne 0 ]; then
    return 2
  fi
  if [ "$target_fail" -ne 0 ]; then
    return 1
  fi
  return 0
}

overall_structural=0
overall_target=0
for archive in "${archives[@]}"; do
  rc=0
  check_archive "$archive" || rc=$?
  case "$rc" in
    2) overall_structural=1 ;;
    1) overall_target=1 ;;
  esac
done

if [ "$overall_structural" -ne 0 ]; then
  echo "::error::Static archive verification failed: missing archive or wrong architecture is always fatal."
  exit 1
fi

if [ "$overall_target" -ne 0 ]; then
  if [ "$mode" = "error" ]; then
    exit 1
  fi
  echo "::notice::Static archive verification reported deployment-target findings in warn mode; not failing the build."
  exit 0
fi

echo "All checked static archives match arch ${arch} and target macOS ${target}."