summaryrefslogtreecommitdiff
path: root/.github/workflows/build-macos.yml
blob: 5c11c62b35597bd45a13c2248cf6698adfd77067 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
name: Build macOS

permissions:
  contents: read

on:
  workflow_call:
    inputs:
      version:
        description: "WSJT-X version string"
        required: true
        type: string
      hamlib_branch:
        description: "Hamlib branch to build"
        required: false
        type: string
        default: "4.7.2"
      arch:
        description: "Target architecture (arm64 or x86_64)"
        required: true
        type: string
      runner:
        description: "GitHub Actions runner label"
        required: true
        type: string
      deployment_target:
        description: "macOS deployment target version"
        required: true
        type: string
      cold_build:
        description: "Bypass workflow dependency and compiler caches without saving them"
        required: false
        type: boolean
        default: false
      recache:
        description: "Force ccache to recompile and refresh cached objects"
        required: false
        type: boolean
        default: false
      save_ccache:
        description: "Persist compiler objects from a trusted develop build"
        required: false
        type: boolean
        default: false
      signing_mode:
        description: "Package signing policy: validation or distribution"
        required: false
        type: string
        default: "validation"
      apple_team_id:
        description: "Expected Apple Developer Team ID for distribution signing"
        required: false
        type: string
        default: ""
      application_certificate_sha1:
        description: "Expected Developer ID Application certificate SHA-1"
        required: false
        type: string
        default: ""
      installer_certificate_sha1:
        description: "Expected Developer ID Installer certificate SHA-1"
        required: false
        type: string
        default: ""
    secrets:
      DEVELOPER_ID_CERTIFICATE_P12:
        description: "Base64-encoded Developer ID Application PKCS#12"
        required: false
      DEVELOPER_ID_CERTIFICATE_PASSWORD:
        description: "Password for the Application PKCS#12"
        required: false
      DEVELOPER_ID_INSTALLER_P12:
        description: "Base64-encoded Developer ID Installer PKCS#12"
        required: false
      DEVELOPER_ID_INSTALLER_PASSWORD:
        description: "Password for the Installer PKCS#12"
        required: false
      APP_STORE_CONNECT_KEY_ID:
        description: "App Store Connect API key ID"
        required: false
      APP_STORE_CONNECT_ISSUER_ID:
        description: "App Store Connect API issuer ID"
        required: false
      APP_STORE_CONNECT_PRIVATE_KEY_P8_BASE64:
        description: "Base64-encoded App Store Connect API private key"
        required: false
  # Standalone dispatch builds one architecture. Derive its runner and deployment
  # target from that architecture so unsupported tuples cannot be selected.
  workflow_dispatch:
    inputs:
      version:
        description: "WSJT-X version string (artifact + package names)"
        required: false
        type: string
        default: "3.2.0-devel"
      hamlib_branch:
        description: "Hamlib branch to build"
        required: false
        type: string
        default: "4.7.2"
      arch:
        description: "Target architecture"
        required: false
        type: choice
        options:
          - arm64
          - x86_64
        default: "arm64"
      cold_build:
        description: "Bypass workflow dependency and compiler caches without saving them"
        required: false
        type: boolean
        default: false
      recache:
        description: "Force ccache to recompile and refresh cached objects"
        required: false
        type: boolean
        default: false
      save_ccache:
        description: "Persist compiler objects when dispatching on develop"
        required: false
        type: boolean
        default: true
      release_channel:
        description: "Build release channel"
        required: false
        type: choice
        options:
          - DEVEL
          - BETA
          - RC
          - GA
        default: "DEVEL"
      prerelease_number:
        description: "Prerelease number when release_channel is BETA or RC"
        required: false
        type: string
        default: ""
      signing_mode:
        description: "Package signing policy"
        required: false
        type: choice
        options:
          - validation
          - distribution
        default: validation
      apple_team_id:
        description: "Expected Apple Developer Team ID (distribution only)"
        required: false
        type: string
        default: ""
      application_certificate_sha1:
        description: "Expected Developer ID Application certificate SHA-1"
        required: false
        type: string
        default: ""
      installer_certificate_sha1:
        description: "Expected Developer ID Installer certificate SHA-1"
        required: false
        type: string
        default: ""

jobs:
  build:
    # runs-on cannot reference job-level env, so keep this expression and RUNNER
    # aligned. RUNNER also keeps cache keys consistent across both triggers.
    runs-on: ${{ inputs.runner || (inputs.arch == 'x86_64' && 'macos-15-intel' || 'macos-15') }}
    timeout-minutes: 180
    env:
      RUNNER: ${{ inputs.runner || (inputs.arch == 'x86_64' && 'macos-15-intel' || 'macos-15') }}
      DEPLOYMENT_TARGET: ${{ inputs.deployment_target || (inputs.arch == 'x86_64' && '10.13' || '11.0') }}
      VERSION: ${{ inputs.version }}
      HAMLIB_BRANCH: ${{ inputs.hamlib_branch }}
      WSJT_RELEASE_CHANNEL: ${{ inputs.release_channel }}
      WSJT_PRERELEASE_NUMBER: ${{ inputs.prerelease_number }}
      SIGNING_MODE: ${{ inputs.signing_mode }}
      ARCH: ${{ inputs.arch }}
      BOOST_VERSION: 1.85.0
      BOOST_SOURCE_SHA256: 7009fe1faa1697476bdc7027703a2badb84e849b7b0baad5086b087b971f8617
    steps:
      - uses: actions/checkout@v7

      - name: Validate workflow inputs
        env:
          WSJTX_VERSION: ${{ inputs.version }}
        run: .github/scripts/validate-ci-inputs.sh

      - name: Verify macOS build tuple
        run: |
          case "${ARCH}:${RUNNER}:${DEPLOYMENT_TARGET}" in
            arm64:macos-15:11.0|x86_64:macos-15-intel:10.13)
              echo "Using supported macOS build tuple: ${ARCH} on ${RUNNER} targeting ${DEPLOYMENT_TARGET}"
              ;;
            *)
              echo "::error::Unsupported macOS build tuple: arch=${ARCH}, runner=${RUNNER}, deployment_target=${DEPLOYMENT_TARGET}"
              echo "::error::Use arm64:macos-15:11.0 or x86_64:macos-15-intel:10.13 for deployable macOS artifacts."
              exit 1
              ;;
          esac

      - name: Validate signing policy
        run: |
          case "$SIGNING_MODE" in
            validation)
              echo "macOS signing mode: validation (ad-hoc app signature and explicitly unsigned package)"
              ;;
            distribution)
              echo "macOS signing mode: distribution (build job remains secretless; a fresh job performs signing)"
              ;;
            *)
              echo "::error::signing_mode must be validation or distribution, got: ${SIGNING_MODE}"
              exit 1
              ;;
          esac

      - name: Set Homebrew prefix
        run: echo "HOMEBREW_PREFIX=$(brew --prefix)" >> "$GITHUB_ENV"

      - name: Install dependencies
        run: brew install cmake autoconf automake libtool asciidoctor ccache pkg-config

      - name: Select ccache key
        id: ccache-key
        env:
          RECACHE: ${{ inputs.recache }}
        run: .github/scripts/select-ccache-key-suffix.sh

      - name: Restore ccache
        id: ccache
        if: ${{ !inputs.cold_build }}
        uses: actions/cache/restore@v6
        with:
          path: .ccache
          key: ccache-macos-${{ inputs.arch }}-appleclang-${{ github.sha }}${{ steps.ccache-key.outputs.suffix }}
          restore-keys: |
            ccache-macos-${{ inputs.arch }}-appleclang-

      - name: Configure ccache
        run: |
          if [ "${{ inputs.cold_build }}" = "true" ]; then
            echo "CCACHE_DIR=$(mktemp -d "${RUNNER_TEMP}/wsjtx-ccache.XXXXXX")" >> "$GITHUB_ENV"
          else
            echo "CCACHE_DIR=${GITHUB_WORKSPACE}/.ccache" >> "$GITHUB_ENV"
          fi
          echo "ccache recache: ${{ inputs.recache }}"
          if [ "${{ inputs.recache }}" = "true" ]; then
            echo "CCACHE_RECACHE=1" >> "$GITHUB_ENV"
            echo "Forcing ccache recache for this run"
          fi
          mkdir -p "${GITHUB_WORKSPACE}/.ccache"
          ccache --version
          ccache --zero-stats

      - name: Compute Qt cache key
        id: qt-key
        run: |
          .github/scripts/macos-qt-cache-key.sh \
            --arch "${{ inputs.arch }}" \
            --runner "${{ env.RUNNER }}" \
            --deployment-target "${{ env.DEPLOYMENT_TARGET }}" \
            --github-output

      - name: Restore Qt cache
        id: qt-cache
        if: ${{ !inputs.cold_build }}
        uses: actions/cache/restore@v6
        with:
          path: qt-prefix
          key: ${{ steps.qt-key.outputs.key }}

      - name: Build Qt
        id: qt-build
        if: steps.qt-cache.outputs.cache-hit != 'true'
        run: |
          .github/scripts/build-qt-macos.sh \
            --arch "${{ inputs.arch }}" \
            --deployment-target "${{ env.DEPLOYMENT_TARGET }}" \
            --prefix "${GITHUB_WORKSPACE}/qt-prefix"

      - name: Save Qt cache
        if: ${{ !inputs.cold_build && steps.qt-cache.outputs.cache-hit != 'true' }}
        uses: actions/cache/save@v6
        with:
          path: qt-prefix
          key: ${{ steps.qt-cache.outputs.cache-primary-key }}

      - name: Verify Qt prefix
        run: |
          .github/scripts/verify-macos-artifacts.sh \
            --target "${{ env.DEPLOYMENT_TARGET }}" \
            --arch "${{ inputs.arch }}" \
            --allow-prefix "${GITHUB_WORKSPACE}/qt-prefix" \
            "${GITHUB_WORKSPACE}/qt-prefix"

      - name: Install controlled ARM gfortran
        id: controlled-arm-fc
        if: inputs.arch == 'arm64'
        run: |
          .github/scripts/install-r-macos-gfortran.sh \
            --target "${{ env.DEPLOYMENT_TARGET }}" \
            --arch "${{ inputs.arch }}" \
            --work-dir "${RUNNER_TEMP}/r-macos-gfortran" \
            --prefix "${GITHUB_WORKSPACE}/gfortran-prefix" \
            --github-output

      - name: Install controlled Intel gfortran
        id: controlled-intel-fc
        if: inputs.arch == 'x86_64'
        run: |
          .github/scripts/install-xpack-gfortran-macos.sh \
            --target "${{ env.DEPLOYMENT_TARGET }}" \
            --arch "${{ inputs.arch }}" \
            --work-dir "${RUNNER_TEMP}/xpack-gfortran" \
            --prefix "${GITHUB_WORKSPACE}/gfortran-prefix" \
            --github-output

      - name: Select gfortran
        id: fc
        run: |
          if [ "${{ inputs.arch }}" = "x86_64" ]; then
            echo "Using controlled gfortran: ${{ steps.controlled-intel-fc.outputs.path }}"
            {
              echo "path=${{ steps.controlled-intel-fc.outputs.path }}"
              echo "prefix=${{ steps.controlled-intel-fc.outputs.prefix }}"
              echo "original_prefix=${{ steps.controlled-intel-fc.outputs.original_prefix }}"
              echo "runtime_lib_dir=${{ steps.controlled-intel-fc.outputs.runtime_lib_dir }}"
              echo "libgomp=${{ steps.controlled-intel-fc.outputs.libgomp }}"
              echo "fortran_runtime_flags=${{ steps.controlled-intel-fc.outputs.fortran_runtime_flags }}"
              echo "runtime_link_flags=${{ steps.controlled-intel-fc.outputs.runtime_link_flags }}"
              echo "runtime_archives=${{ steps.controlled-intel-fc.outputs.runtime_archives }}"
              echo "version=${{ steps.controlled-intel-fc.outputs.version }}"
              echo "cache_key=${{ steps.controlled-intel-fc.outputs.cache_key }}"
            } >> "$GITHUB_OUTPUT"
            exit 0
          fi

          echo "Using controlled gfortran: ${{ steps.controlled-arm-fc.outputs.path }}"
          {
            echo "path=${{ steps.controlled-arm-fc.outputs.path }}"
            echo "prefix=${{ steps.controlled-arm-fc.outputs.prefix }}"
            echo "original_prefix=${{ steps.controlled-arm-fc.outputs.original_prefix }}"
            echo "runtime_lib_dir=${{ steps.controlled-arm-fc.outputs.runtime_lib_dir }}"
            echo "libgomp=${{ steps.controlled-arm-fc.outputs.libgomp }}"
            echo "fortran_runtime_flags=${{ steps.controlled-arm-fc.outputs.fortran_runtime_flags }}"
            echo "runtime_link_flags=${{ steps.controlled-arm-fc.outputs.runtime_link_flags }}"
            echo "runtime_archives=${{ steps.controlled-arm-fc.outputs.runtime_archives }}"
            echo "version=${{ steps.controlled-arm-fc.outputs.version }}"
            echo "cache_key=${{ steps.controlled-arm-fc.outputs.cache_key }}"
          } >> "$GITHUB_OUTPUT"

      - name: Verify static Fortran runtime
        run: |
          .github/scripts/verify-macos-static-archives.sh \
            --target "${{ env.DEPLOYMENT_TARGET }}" \
            --arch "${{ inputs.arch }}" \
            --mode error \
            ${{ steps.fc.outputs.runtime_archives }}

      - name: Restore libusb cache
        id: libusb-cache
        if: ${{ !inputs.cold_build }}
        uses: actions/cache/restore@v6
        with:
          path: libusb-prefix
          key: libusb-macos-${{ inputs.arch }}-${{ env.RUNNER }}-1.0.27-min${{ env.DEPLOYMENT_TARGET }}-epoch1-${{ hashFiles('.github/scripts/build-libusb-macos.sh') }}

      - name: Build libusb
        id: libusb-build
        if: steps.libusb-cache.outputs.cache-hit != 'true'
        run: |
          .github/scripts/build-libusb-macos.sh \
            1.0.27 \
            "${{ env.DEPLOYMENT_TARGET }}" \
            "${GITHUB_WORKSPACE}/libusb-prefix"

      - name: Save libusb cache
        if: ${{ !inputs.cold_build && steps.libusb-cache.outputs.cache-hit != 'true' }}
        uses: actions/cache/save@v6
        with:
          path: libusb-prefix
          key: ${{ steps.libusb-cache.outputs.cache-primary-key }}

      - name: Restore PortAudio cache
        id: portaudio-cache
        if: ${{ !inputs.cold_build }}
        uses: actions/cache/restore@v6
        with:
          path: portaudio-prefix
          key: portaudio-macos-${{ inputs.arch }}-${{ env.RUNNER }}-19.7.0-min${{ env.DEPLOYMENT_TARGET }}-epoch1-${{ hashFiles('.github/scripts/build-portaudio-macos.sh') }}

      - name: Build PortAudio
        id: portaudio-build
        if: steps.portaudio-cache.outputs.cache-hit != 'true'
        run: |
          .github/scripts/build-portaudio-macos.sh \
            19.7.0 \
            "${{ env.DEPLOYMENT_TARGET }}" \
            "${GITHUB_WORKSPACE}/portaudio-prefix"

      - name: Save PortAudio cache
        if: ${{ !inputs.cold_build && steps.portaudio-cache.outputs.cache-hit != 'true' }}
        uses: actions/cache/save@v6
        with:
          path: portaudio-prefix
          key: ${{ steps.portaudio-cache.outputs.cache-primary-key }}

      - name: Restore FFTW cache
        id: fftw-cache
        if: ${{ !inputs.cold_build }}
        uses: actions/cache/restore@v6
        with:
          path: fftw-prefix
          key: fftw-macos-${{ inputs.arch }}-${{ env.RUNNER }}-3.3.10-min${{ env.DEPLOYMENT_TARGET }}-epoch1-${{ hashFiles('.github/scripts/build-fftw-macos.sh') }}

      - name: Build FFTW
        id: fftw-build
        if: steps.fftw-cache.outputs.cache-hit != 'true'
        run: |
          .github/scripts/build-fftw-macos.sh \
            3.3.10 \
            "${{ env.DEPLOYMENT_TARGET }}" \
            "${GITHUB_WORKSPACE}/fftw-prefix"

      - name: Save FFTW cache
        if: ${{ !inputs.cold_build && steps.fftw-cache.outputs.cache-hit != 'true' }}
        uses: actions/cache/save@v6
        with:
          path: fftw-prefix
          key: ${{ steps.fftw-cache.outputs.cache-primary-key }}

      - name: Restore Boost cache
        id: boost-cache
        if: ${{ !inputs.cold_build }}
        uses: actions/cache/restore@v6
        with:
          path: boost-prefix
          key: boost-macos-${{ inputs.arch }}-${{ env.RUNNER }}-${{ env.BOOST_VERSION }}-${{ env.BOOST_SOURCE_SHA256 }}-min${{ env.DEPLOYMENT_TARGET }}-epoch1-${{ hashFiles('.github/scripts/build-boost-macos.sh') }}

      - name: Build Boost
        id: boost-build
        if: steps.boost-cache.outputs.cache-hit != 'true'
        run: |
          .github/scripts/build-boost-macos.sh \
            "${BOOST_VERSION}" \
            "${BOOST_SOURCE_SHA256}" \
            "${{ inputs.arch }}" \
            "${{ env.DEPLOYMENT_TARGET }}" \
            "${GITHUB_WORKSPACE}/boost-prefix"

      - name: Save Boost cache
        if: ${{ !inputs.cold_build && steps.boost-cache.outputs.cache-hit != 'true' }}
        uses: actions/cache/save@v6
        with:
          path: boost-prefix
          key: ${{ steps.boost-cache.outputs.cache-primary-key }}

      - name: Verify pinned runtime prefixes
        run: |
          .github/scripts/verify-macos-artifacts.sh \
            --target "${{ env.DEPLOYMENT_TARGET }}" \
            --arch "${{ inputs.arch }}" \
            --allow-prefix "${GITHUB_WORKSPACE}/libusb-prefix" \
            --allow-prefix "${GITHUB_WORKSPACE}/portaudio-prefix" \
            --allow-prefix "${GITHUB_WORKSPACE}/fftw-prefix" \
            --allow-prefix "${GITHUB_WORKSPACE}/boost-prefix" \
            "${GITHUB_WORKSPACE}/libusb-prefix" \
            "${GITHUB_WORKSPACE}/portaudio-prefix" \
            "${GITHUB_WORKSPACE}/fftw-prefix" \
            "${GITHUB_WORKSPACE}/boost-prefix"

      - name: Restore Hamlib cache
        id: hamlib-cache
        if: ${{ !inputs.cold_build }}
        uses: actions/cache/restore@v6
        with:
          path: hamlib-prefix
          key: hamlib-macos-${{ inputs.arch }}-${{ env.RUNNER }}-${{ inputs.hamlib_branch }}-libusb1.0.27-min${{ env.DEPLOYMENT_TARGET }}-epoch1-${{ hashFiles('.github/scripts/build-hamlib-macos.sh', '.github/scripts/build-libusb-macos.sh') }}

      - name: Build Hamlib
        id: hamlib-build
        if: steps.hamlib-cache.outputs.cache-hit != 'true'
        run: |
          .github/scripts/build-hamlib-macos.sh \
            "${HAMLIB_BRANCH}" \
            "${DEPLOYMENT_TARGET}" \
            "${GITHUB_WORKSPACE}/hamlib-prefix" \
            "${GITHUB_WORKSPACE}/libusb-prefix"

      - name: Verify Hamlib prefix
        run: |
          test -f "${GITHUB_WORKSPACE}/hamlib-prefix/lib/libhamlib.a"
          .github/scripts/verify-macos-static-archives.sh \
            --target "${{ env.DEPLOYMENT_TARGET }}" \
            --arch "${{ inputs.arch }}" \
            --mode error \
            "${GITHUB_WORKSPACE}/hamlib-prefix/lib/libhamlib.a"

      - name: Save Hamlib cache
        if: ${{ !inputs.cold_build && steps.hamlib-cache.outputs.cache-hit != 'true' }}
        uses: actions/cache/save@v6
        with:
          path: hamlib-prefix
          key: ${{ steps.hamlib-cache.outputs.cache-primary-key }}

      - name: Restore pFUnit cache
        id: pfunit-cache
        if: ${{ !inputs.cold_build }}
        uses: actions/cache/restore@v6
        with:
          path: pfunit-prefix
          key: pfunit-macos-${{ inputs.arch }}-${{ env.RUNNER }}-v4.14.0-${{ steps.fc.outputs.cache_key }}-min${{ env.DEPLOYMENT_TARGET }}-epoch3-${{ hashFiles('.github/scripts/build-pfunit-macos.sh') }}

      - name: Build pFUnit
        id: pfunit-build
        if: steps.pfunit-cache.outputs.cache-hit != 'true'
        env:
          # pFUnit configure uses try_run probes; expose only the controlled
          # compiler runtime for those probes, not for the final test binaries.
          DYLD_LIBRARY_PATH: ${{ steps.fc.outputs.runtime_lib_dir }}
        run: |
          .github/scripts/build-pfunit-macos.sh \
            v4.14.0 \
            "${{ steps.fc.outputs.path }}" \
            "${{ env.DEPLOYMENT_TARGET }}" \
            "${GITHUB_WORKSPACE}/pfunit-prefix"

      - name: Locate pFUnit config
        id: pfunit
        run: |
          CONFIG=$(find "${GITHUB_WORKSPACE}/pfunit-prefix" -name PFUNITConfig.cmake -print -quit)
          if [ -z "$CONFIG" ]; then
            echo "ERROR: PFUNITConfig.cmake not found under pfunit-prefix"
            find "${GITHUB_WORKSPACE}/pfunit-prefix" -maxdepth 3 -type d
            exit 1
          fi
          PFUNIT_DIR="$(dirname "$CONFIG")"
          echo "Found PFUNITConfig.cmake at: $CONFIG"
          echo "dir=${PFUNIT_DIR}" >> "$GITHUB_OUTPUT"

      - name: Save pFUnit cache
        if: ${{ !inputs.cold_build && steps.pfunit-cache.outputs.cache-hit != 'true' }}
        uses: actions/cache/save@v6
        with:
          path: pfunit-prefix
          key: ${{ steps.pfunit-cache.outputs.cache-primary-key }}

      - name: Configure build and tests
        env:
          PKG_CONFIG_PATH: ${{ github.workspace }}/qt-prefix/lib/pkgconfig:${{ github.workspace }}/boost-prefix/lib/pkgconfig:${{ github.workspace }}/fftw-prefix/lib/pkgconfig:${{ github.workspace }}/libusb-prefix/lib/pkgconfig:${{ github.workspace }}/portaudio-prefix/lib/pkgconfig:${{ github.workspace }}/hamlib-prefix/lib/pkgconfig:${{ github.workspace }}/pfunit-prefix/lib/pkgconfig
        run: |
          cmake -S . -B wsjtx-build \
            -DCMAKE_PREFIX_PATH="${GITHUB_WORKSPACE}/qt-prefix;${GITHUB_WORKSPACE}/boost-prefix;${GITHUB_WORKSPACE}/fftw-prefix;${GITHUB_WORKSPACE}/libusb-prefix;${GITHUB_WORKSPACE}/portaudio-prefix;${GITHUB_WORKSPACE}/hamlib-prefix;${GITHUB_WORKSPACE}/pfunit-prefix" \
            -DCMAKE_C_COMPILER_LAUNCHER=ccache \
            -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
            -DCMAKE_Fortran_COMPILER="${{ steps.fc.outputs.path }}" \
            -DCMAKE_Fortran_FLAGS="${{ steps.fc.outputs.fortran_runtime_flags }}" \
            -DCMAKE_EXE_LINKER_FLAGS="${{ steps.fc.outputs.runtime_link_flags }}" \
            -DCMAKE_INSTALL_RPATH="${GITHUB_WORKSPACE}/qt-prefix/lib;${GITHUB_WORKSPACE}/boost-prefix/lib;${GITHUB_WORKSPACE}/fftw-prefix/lib;${GITHUB_WORKSPACE}/libusb-prefix/lib;${GITHUB_WORKSPACE}/portaudio-prefix/lib;${GITHUB_WORKSPACE}/hamlib-prefix/lib" \
            -DCMAKE_OSX_DEPLOYMENT_TARGET=${{ env.DEPLOYMENT_TARGET }} \
            -DWSJT_SKIP_MANPAGES=ON \
            -DWSJT_ENABLE_TESTS=ON \
            -DWSJT_ENABLE_GUI_SMOKE_TESTS=ON \
            -DWSJT_MACOS_LIBGOMP_LIBRARY="${{ steps.fc.outputs.libgomp }}" \
            -DPFUNIT_DIR="${{ steps.pfunit.outputs.dir }}" \
            -DWSJT_SOURCE_REVISION="${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || '' }}" \
            -DWSJT_RELEASE_CHANNEL="${WSJT_RELEASE_CHANNEL}" \
            -DWSJT_PRERELEASE_NUMBER="${WSJT_PRERELEASE_NUMBER}" \
            -Wno-dev

      - name: Build and test targets
        id: compile
        run: cmake --build wsjtx-build -j"$(sysctl -n hw.ncpu)"

      - name: Report suppressed ccache save
        if: ${{ !inputs.cold_build && (inputs.save_ccache || inputs.recache) && github.ref != 'refs/heads/develop' }}
        run: echo "::notice::Not saving ccache because shared compiler caches are published only from develop"

      - name: Save ccache
        if: ${{ !inputs.cold_build && success() && (inputs.save_ccache || inputs.recache) && github.ref == 'refs/heads/develop' && steps.ccache.outputs.cache-hit != 'true' }}
        uses: actions/cache/save@v6
        with:
          path: .ccache
          key: ${{ steps.ccache.outputs.cache-primary-key }}

      - name: Run tests
        id: ctest
        run: ctest --parallel 3 --output-on-failure --output-junit ctest-results.xml
        working-directory: wsjtx-build

      - name: Publish test summary
        if: always() && steps.ctest.conclusion != 'skipped'
        working-directory: wsjtx-build
        run: python3 ../.github/scripts/publish-ctest-summary.py "macOS ${{ inputs.arch }}" >> "$GITHUB_STEP_SUMMARY"

      - name: Upload test results
        if: always() && steps.ctest.conclusion != 'skipped'
        uses: actions/upload-artifact@v7
        with:
          name: ctest-results-macos-${{ inputs.arch }}
          path: wsjtx-build/ctest-results.xml
          if-no-files-found: ignore

      - name: Upload WSJT-X startup diagnostics
        if: failure() && steps.ctest.outcome == 'failure'
        uses: actions/upload-artifact@v7
        with:
          name: wsjtx-startup-smoke-macos-${{ inputs.arch }}
          path: wsjtx-build/startup-smoke-diagnostics
          if-no-files-found: ignore
          retention-days: 7

      - name: Show ccache stats
        if: always()
        run: ccache --show-stats

      - name: Verify architecture
        run: |
          file wsjtx-build/jt9
          ARCHS=$(lipo -archs wsjtx-build/jt9)
          case " ${ARCHS} " in
            *" ${{ inputs.arch }} "*) ;;
            *)
              echo "::error::Expected ${{ inputs.arch }} in jt9 architectures, found: ${ARCHS}"
              exit 1
              ;;
          esac

      - name: Install app bundle and collect CLI assets
        id: collect
        run: |
          BUILD=wsjtx-build
          STAGE="stage/wsjtx-${VERSION}-${ARCH}"
          ASSETS=assets

          mkdir -p "${ASSETS}"
          cmake --install "${BUILD}" --prefix "${STAGE}"

          APP="${STAGE}/wsjtx.app"
          if [ ! -d "${APP}" ]; then
            echo "::error::CMake install did not create ${APP}"
            exit 1
          fi

          for bin in qmap map65; do
            if [ ! -x "${APP}/Contents/MacOS/${bin}" ]; then
              echo "::error::Expected app companion binary is missing after install: ${APP}/Contents/MacOS/${bin}"
              exit 1
            fi
          done

          missing=()
          while IFS= read -r bin; do
            [ -n "${bin}" ] || continue
            if [ -f "${BUILD}/${bin}" ]; then
              cp "${BUILD}/${bin}" "${ASSETS}/"
            else
              missing+=("${bin}")
            fi
          done < "${BUILD}/cli-tools.txt"
          if [ "${#missing[@]}" -gt 0 ]; then
            echo "::error::CLI tools listed in ${BUILD}/cli-tools.txt were not built: ${missing[*]}"
            exit 1
          fi

          echo "stage_dir=${STAGE}" >> "$GITHUB_OUTPUT"
          echo "assets_dir=${ASSETS}" >> "$GITHUB_OUTPUT"

      - name: Verify CMake-installed app resources
        run: |
          APP="${{ steps.collect.outputs.stage_dir }}/wsjtx.app"
          DATADIR="${APP}/Contents/Resources/wsjtx"
          DOCDIR="${APP}/Contents/Resources/doc/wsjtx"
          SOUNDDIR="${APP}/Contents/Resources/sounds"

          for file in cty.dat cty.dat_copyright.txt grid.dat sat.dat eclipse.txt ALLCALL7.TXT JPLEPH; do
            if [ ! -f "${DATADIR}/${file}" ]; then
              echo "::error::Expected CMake-installed data file is missing: ${DATADIR}/${file}"
              exit 1
            fi
          done

          set -- "${DOCDIR}/wsjtx-main"*.html
          if [ "$#" -lt 1 ] || [ ! -f "$1" ]; then
            echo "::error::Expected at least one CMake-installed local user guide in ${DOCDIR}"
            printf 'Matched paths:\n'
            printf '  %s\n' "$@"
            exit 1
          fi

          if [ ! -f "${SOUNDDIR}/Wanted.wav" ]; then
            echo "::error::Expected CMake-installed alert sound is missing: ${SOUNDDIR}/Wanted.wav"
            exit 1
          fi

          if [ -e "${APP}/Contents/MacOS/sounds" ]; then
            echo "::error::Alert sounds must not be installed under Contents/MacOS; codesign treats them as unsigned code."
            exit 1
          fi

          echo "Verified CMake-installed data files in ${DATADIR}"
          echo "Verified CMake-installed local user guides:"
          printf '  %s\n' "$@"
          echo "Verified CMake-installed alert sounds in ${SOUNDDIR}"

      - name: Deploy macOS app dependencies
        run: |
          .github/scripts/deploy-macos-app.sh \
            --app "${{ steps.collect.outputs.stage_dir }}/wsjtx.app" \
            --workspace-prefix "${GITHUB_WORKSPACE}" \
            --qt-prefix "${GITHUB_WORKSPACE}/qt-prefix" \
            --boost-prefix "${GITHUB_WORKSPACE}/boost-prefix" \
            --fftw-prefix "${GITHUB_WORKSPACE}/fftw-prefix" \
            --libusb-prefix "${GITHUB_WORKSPACE}/libusb-prefix" \
            --portaudio-prefix "${GITHUB_WORKSPACE}/portaudio-prefix" \
            --fortran-prefix "${{ steps.fc.outputs.prefix }}" \
            --fortran-original-prefix "${{ steps.fc.outputs.original_prefix }}" \
            --gcc-lib "${{ steps.fc.outputs.runtime_lib_dir }}" \
            --homebrew-prefix "${HOMEBREW_PREFIX}"

          APP="${{ steps.collect.outputs.stage_dir }}/wsjtx.app"
          for file in \
            "${APP}/Contents/PlugIns/platforms/libqcocoa.dylib" \
            "${APP}/Contents/Resources/qt.conf"
          do
            if [ ! -f "$file" ]; then
              echo "::error::Expected macOS app deployment file is missing: ${file}"
              exit 1
            fi
          done

      - name: Deploy macOS CLI tool dependencies
        run: |
          .github/scripts/deploy-macos-cli-tools.sh \
            --assets "${{ steps.collect.outputs.assets_dir }}" \
            --stage "${{ steps.collect.outputs.stage_dir }}" \
            --workspace-prefix "${GITHUB_WORKSPACE}" \
            --boost-prefix "${GITHUB_WORKSPACE}/boost-prefix" \
            --fftw-prefix "${GITHUB_WORKSPACE}/fftw-prefix" \
            --libusb-prefix "${GITHUB_WORKSPACE}/libusb-prefix" \
            --portaudio-prefix "${GITHUB_WORKSPACE}/portaudio-prefix" \
            --fortran-prefix "${{ steps.fc.outputs.prefix }}" \
            --fortran-original-prefix "${{ steps.fc.outputs.original_prefix }}" \
            --gcc-lib "${{ steps.fc.outputs.runtime_lib_dir }}" \
            --homebrew-prefix "${HOMEBREW_PREFIX}"

      - name: Verify bundled macOS deployment targets
        run: |
          STAGE="${{ steps.collect.outputs.stage_dir }}"
          .github/scripts/verify-macos-artifacts.sh \
            --target "${{ env.DEPLOYMENT_TARGET }}" \
            --arch "${{ inputs.arch }}" \
            "$STAGE"

      - name: Ad-hoc sign binaries for build validation
        run: |
          STAGE="${{ steps.collect.outputs.stage_dir }}"
          ASSETS="${{ steps.collect.outputs.assets_dir }}"
          APP="${STAGE}/wsjtx.app"
          ENTITLEMENTS="${GITHUB_WORKSPACE}/entitlements.plist"

          IDENTITY="-"
          echo "Code signing mode: ad-hoc (no Apple trust chain; pkg will be unsigned)"

          # Sign nested code before the containing bundle. Executable
          # entitlements are defined in the repository-root plist.
          find "${APP}/Contents/Frameworks" -type f \( -name "*.dylib" -o -perm +111 \) \
            -exec codesign --force --options runtime --sign "$IDENTITY" {} \;
          find "${APP}/Contents/Frameworks" -name "*.framework" -type d | while read -r fw; do
            codesign --force --options runtime --sign "$IDENTITY" "$fw"
          done
          find "${APP}/Contents/PlugIns" -type f -name "*.dylib" \
            -exec codesign --force --options runtime --sign "$IDENTITY" {} \; 2>/dev/null || true

          find "${APP}/Contents/MacOS" -type f -perm +111 \
            -exec codesign --force --options runtime --entitlements "$ENTITLEMENTS" --sign "$IDENTITY" {} \;
          # Bundle signing re-signs the main executable, so it must receive the
          # entitlement file again.
          codesign --force --options runtime --entitlements "$ENTITLEMENTS" --sign "$IDENTITY" "${APP}"
          codesign --verify --deep --strict --verbose=2 "${APP}"

          # codesign --verify validates the signature, not required entitlement
          # presence.
          MAIN_BIN="${APP}/Contents/MacOS/wsjtx"
          ENTITLEMENTS_OUT=$(codesign -d --entitlements - "$MAIN_BIN" 2>/dev/null)
          if [[ "$ENTITLEMENTS_OUT" != *"disable-library-validation"* ]]; then
            echo "::error::wsjtx binary is missing com.apple.security.cs.disable-library-validation entitlement."
            echo "::error::Bundled Qt frameworks will fail to load under hardened runtime."
            codesign -d --entitlements - "$MAIN_BIN" || true
            exit 1
          fi
          echo "Confirmed disable-library-validation entitlement on $MAIN_BIN"
          if [[ "$ENTITLEMENTS_OUT" == *"allow-unsigned-executable-memory"* ]]; then
            echo "::error::${MAIN_BIN} must not grant unsigned executable memory."
            exit 1
          fi
          echo "Confirmed no unsigned executable memory entitlement on $MAIN_BIN"

          for dir in "${STAGE}" "${ASSETS}"; do
            find "${dir}" -maxdepth 1 -type f -perm +111 \
              -exec codesign --force --options runtime --entitlements "$ENTITLEMENTS" --sign "$IDENTITY" {} \;
          done

          for dir in "${STAGE}/lib" "${ASSETS}/lib"; do
            [ -d "$dir" ] && find "$dir" -type f \
              -exec codesign --force --options runtime --sign "$IDENTITY" {} \;
          done

      - name: Prepare installer package
        run: |
          STAGE="${{ steps.collect.outputs.stage_dir }}"
          PKG_ID="org.k1jt.wsjtx.${ARCH}"
          PKG_VERSION="${VERSION}"

          PAYLOAD="$RUNNER_TEMP/pkg-payload"
          SCRIPTS="$RUNNER_TEMP/pkg-scripts"
          mkdir -p "${PAYLOAD}/Applications" "${SCRIPTS}"

          cp -R "${STAGE}/wsjtx.app" "${PAYLOAD}/Applications/"

          mkdir -p "${PAYLOAD}/usr/local/wsjtx/lib"
          missing=()
          while IFS= read -r bin; do
            [ -n "${bin}" ] || continue
            if [ -f "${STAGE}/${bin}" ]; then
              cp "${STAGE}/${bin}" "${PAYLOAD}/usr/local/wsjtx/"
            else
              missing+=("${bin}")
            fi
          done < wsjtx-build/installed-cli-tools.txt
          if [ "${#missing[@]}" -gt 0 ]; then
            echo "::error::CLI tools listed in wsjtx-build/installed-cli-tools.txt are missing from ${STAGE}: ${missing[*]}"
            exit 1
          fi
          [ -d "${STAGE}/lib" ] && cp -R "${STAGE}/lib/"* "${PAYLOAD}/usr/local/wsjtx/lib/"

          cp wsjtx-build/installed-cli-tools.txt "${SCRIPTS}/"
          cat > "${SCRIPTS}/postinstall" << 'SCRIPT'
          #!/bin/bash
          root="${3%/}"   # $3 is the installation's target volume
          shipped="$(dirname "$0")/installed-cli-tools.txt"
          mkdir -p "$root/usr/local/bin"
          if [ -s "$shipped" ] && [ ! -L "$root/usr/local/wsjtx" ]; then
            for bin in "$root"/usr/local/wsjtx/*; do
              [ -f "$bin" ] && [ -x "$bin" ] || continue
              name=$(basename "$bin")
              grep -qxF -- "$name" "$shipped" && continue
              rm -f "$bin"
              link="$root/usr/local/bin/$name"
              if [ -L "$link" ] && [ ! -e "$link" ]; then
                case "$(readlink "$link")" in
                  "$root"/usr/local/wsjtx/*) rm -f "$link" ;;
                esac
              fi
            done
          fi
          for bin in "$root"/usr/local/wsjtx/*; do
            if [ -f "$bin" ] && [ -x "$bin" ]; then
              ln -sf "$bin" "$root/usr/local/bin/$(basename "$bin")"
            fi
          done
          SCRIPT
          chmod +x "${SCRIPTS}/postinstall"

          pkgbuild --analyze --root "${PAYLOAD}" "$RUNNER_TEMP/components.plist"
          /usr/libexec/PlistBuddy -c "Set :0:BundleIsRelocatable false" "$RUNNER_TEMP/components.plist" 2>/dev/null || true
          /usr/libexec/PlistBuddy -c "Set :0:BundleIsVersionChecked false" "$RUNNER_TEMP/components.plist" 2>/dev/null || true
          /usr/libexec/PlistBuddy -c "Set :0:BundleOverwriteAction upgrade" "$RUNNER_TEMP/components.plist" 2>/dev/null || true

          pkgbuild \
            --root "${PAYLOAD}" \
            --scripts "${SCRIPTS}" \
            --component-plist "$RUNNER_TEMP/components.plist" \
            --identifier "${PKG_ID}" \
            --version "${PKG_VERSION}" \
            --install-location / \
            "$RUNNER_TEMP/wsjtx-component.pkg"

      - name: Build unsigned validation package
        if: inputs.signing_mode == 'validation'
        id: package
        run: |
          PKG_OUT="wsjtx-${VERSION}-${ARCH}-macOS.pkg"
          productbuild \
            --package "$RUNNER_TEMP/wsjtx-component.pkg" \
            "$PKG_OUT"
          echo "path=$PKG_OUT" >> "$GITHUB_OUTPUT"
          echo "artifact_name=wsjtx-${VERSION}-${ARCH}-macOS-unsigned.pkg" >> "$GITHUB_OUTPUT"

      - name: Verify unsigned validation package
        if: inputs.signing_mode == 'validation'
        run: |
          PKG_OUT="${{ steps.package.outputs.path }}"
          CHECK_PAYLOAD="$RUNNER_TEMP/pkg-check"
          pkgutil --expand-full "$PKG_OUT" "$CHECK_PAYLOAD"
          [ "$(/usr/libexec/PlistBuddy -c "Print :0:BundleIsRelocatable" "$RUNNER_TEMP/components.plist")" = "false" ]
          [ "$(/usr/libexec/PlistBuddy -c "Print :0:BundleIsVersionChecked" "$RUNNER_TEMP/components.plist")" = "false" ]
          [ "$(/usr/libexec/PlistBuddy -c "Print :0:BundleOverwriteAction" "$RUNNER_TEMP/components.plist")" = "upgrade" ]
          test -x "$CHECK_PAYLOAD/wsjtx-component.pkg/Payload/Applications/wsjtx.app/Contents/MacOS/qmap"
          test -x "$CHECK_PAYLOAD/wsjtx-component.pkg/Payload/Applications/wsjtx.app/Contents/MacOS/map65"
          APP_PAYLOAD="$CHECK_PAYLOAD/wsjtx-component.pkg/Payload/Applications/wsjtx.app"
          SOUNDDIR="${APP_PAYLOAD}/Contents/Resources/sounds"
          set -- "${APP_PAYLOAD}/Contents/Resources/doc/wsjtx/wsjtx-main"*.html
          if [ "$#" -lt 1 ] || [ ! -f "$1" ]; then
            echo "::error::Expected at least one local user guide in the packaged app bundle."
            printf 'Matched paths:\n'
            printf '  %s\n' "$@"
            exit 1
          fi
          if [ ! -f "${SOUNDDIR}/Wanted.wav" ]; then
            echo "::error::Expected packaged alert sound is missing: ${SOUNDDIR}/Wanted.wav"
            exit 1
          fi
          if [ -e "${APP_PAYLOAD}/Contents/MacOS/sounds" ]; then
            echo "::error::Packaged alert sounds must not be under Contents/MacOS."
            exit 1
          fi
          echo "Verified packaged alert sounds in ${SOUNDDIR}"
          .github/scripts/verify-macos-artifacts.sh \
            --target "${{ env.DEPLOYMENT_TARGET }}" \
            --arch "${{ inputs.arch }}" \
            "$CHECK_PAYLOAD/wsjtx-component.pkg/Payload"

      - name: Stage distribution signing input
        if: inputs.signing_mode == 'distribution'
        run: |
          mkdir -p distribution-signing-input
          cp "${{ runner.temp }}/wsjtx-component.pkg" distribution-signing-input/
          cp "${{ runner.temp }}/components.plist" distribution-signing-input/
          cp entitlements.plist distribution-signing-input/

      - name: Upload distribution signing input
        if: inputs.signing_mode == 'distribution'
        uses: actions/upload-artifact@v7
        with:
          name: macos-signing-input-${{ inputs.version }}-${{ inputs.arch }}
          path: distribution-signing-input
          retention-days: 1

      - name: Write validation signing report
        if: inputs.signing_mode == 'validation'
        run: |
          PKG_OUT="${{ steps.package.outputs.path }}"
          PKG_SIGNATURE=$(pkgutil --check-signature "$PKG_OUT" 2>&1 || true)
          printf '%s\n' "$PKG_SIGNATURE"
          if [[ "$PKG_SIGNATURE" != *"Status: no signature"* ]]; then
            echo "::error::Validation package did not report the expected unsigned status."
            exit 1
          fi
          SHA256=$(shasum -a 256 "$PKG_OUT" | awk '{print $1}')
          jq -n \
            --arg mode validation \
            --arg artifact "$PKG_OUT" \
            --arg sha256 "$SHA256" \
            --arg git_sha "$GITHUB_SHA" \
            '{schema_version: 1, mode: $mode, artifact: $artifact, sha256: $sha256, git_sha: $git_sha, signed: false, notarized: false, publishable: false}' \
            > macos-signing-report.json

      - name: Upload pkg
        if: inputs.signing_mode == 'validation'
        uses: actions/upload-artifact@v7
        with:
          name: ${{ steps.package.outputs.artifact_name }}
          path: ${{ steps.package.outputs.path }}

      - name: Upload signing report
        if: inputs.signing_mode == 'validation'
        uses: actions/upload-artifact@v7
        with:
          name: macos-signing-report-${{ inputs.version }}-${{ inputs.arch }}
          path: macos-signing-report.json

      - name: Package individual binaries
        run: |
          # Architecture-qualified names prevent release-asset collisions.
          ASSETS="${{ steps.collect.outputs.assets_dir }}"
          ARCH="${{ inputs.arch }}"
          PKGS=packages
          mkdir -p "${PKGS}"
          for bin in "${ASSETS}"/*; do
            [ -f "$bin" ] && [ -x "$bin" ] || continue
            name=$(basename "$bin")
            arch_name="${name}-${ARCH}"
            dir="${PKGS}/${arch_name}"
            mkdir -p "${dir}"
            cp "$bin" "${dir}/"
            [ -d "${ASSETS}/lib" ] && cp -R "${ASSETS}/lib" "${dir}/lib"
            tar -czf "${PKGS}/${arch_name}.tar.gz" -C "${PKGS}" "${arch_name}"
            rm -rf "${dir}"
          done

      - name: Upload individual binaries
        uses: actions/upload-artifact@v7
        with:
          name: individual-binaries-macos-${{ inputs.arch }}
          path: packages/*.tar.gz

      - name: Package release tarballs
        run: |
          .github/scripts/package-cli-tools.sh \
            --platform macos \
            --version "${{ inputs.version }}" \
            --arch "${{ inputs.arch }}" \
            --groups CMake/release-tarballs.txt \
            --source "${{ steps.collect.outputs.assets_dir }}" \
            --out release-tarballs

      - name: Smoke-test release tarballs
        timeout-minutes: 5
        run: |
          .github/scripts/smoke-release-tarballs.sh \
            --platform macos \
            --version "${{ inputs.version }}" \
            --target "${{ inputs.arch }}-macOS" \
            --groups CMake/release-tarballs.txt \
            --dir release-tarballs

      - name: Upload release tarballs
        uses: ./.github/actions/upload-release-tarballs
        with:
          version: ${{ inputs.version }}
          target: ${{ inputs.arch }}-macOS
          directory: release-tarballs

      # Keep repository-built code after signing, notarization, cleanup, and
      # artifact publication so a detached child cannot reach later secrets.
      - name: Smoke-launch deployed WSJT-X app
        id: deployed_wsjtx_smoke
        timeout-minutes: 1
        run: |
          APP="${GITHUB_WORKSPACE}/${{ steps.collect.outputs.stage_dir }}/wsjtx.app"
          DIAGNOSTICS="${GITHUB_WORKSPACE}/wsjtx-build/deployed-startup-smoke-diagnostics"

          unset DYLD_LIBRARY_PATH
          unset DYLD_FRAMEWORK_PATH
          unset DYLD_INSERT_LIBRARIES
          unset DYLD_ROOT_PATH
          unset DYLD_IMAGE_SUFFIX
          unset QT_PLUGIN_PATH
          unset QT_QPA_PLATFORM_PLUGIN_PATH
          unset QML_IMPORT_PATH
          unset QML2_IMPORT_PATH

          export DYLD_FALLBACK_LIBRARY_PATH="/usr/lib"
          export DYLD_FALLBACK_FRAMEWORK_PATH="/System/Library/Frameworks"
          export QT_QPA_PLATFORM="cocoa"
          export PATH="/usr/bin:/bin:/usr/sbin:/sbin"

          cd "$RUNNER_TEMP"
          /bin/bash "${GITHUB_WORKSPACE}/test/run-wsjtx-startup-macos.sh" \
            "$DIAGNOSTICS" \
            "${APP}/Contents/MacOS/wsjtx" \
            --startup-smoke-test \
            --rig-name CI-DEPLOYED-STARTUP

      - name: Upload deployed WSJT-X startup diagnostics
        if: failure() && steps.deployed_wsjtx_smoke.outcome == 'failure'
        uses: actions/upload-artifact@v7
        with:
          name: wsjtx-deployed-startup-smoke-macos-${{ inputs.arch }}
          path: wsjtx-build/deployed-startup-smoke-diagnostics
          if-no-files-found: ignore
          retention-days: 7

      - name: Summarize native caches
        if: always()
        shell: bash
        env:
          CACHE_STEPS: ${{ toJSON(steps) }}
          COLD_BUILD: ${{ inputs.cold_build }}
          RECACHE: ${{ inputs.recache }}
        run: python3 .github/scripts/summarize-native-caches.py --compiler qt libusb portaudio fftw boost hamlib pfunit >> "$GITHUB_STEP_SUMMARY"

  sign_distribution:
    if: inputs.signing_mode == 'distribution'
    needs: build
    runs-on: ${{ inputs.runner || (inputs.arch == 'x86_64' && 'macos-15-intel' || 'macos-15') }}
    timeout-minutes: 30
    environment: apple-release-signing
    env:
      VERSION: ${{ inputs.version }}
      ARCH: ${{ inputs.arch }}
      APPLE_TEAM_ID: ${{ inputs.apple_team_id || vars.APPLE_TEAM_ID }}
      APP_CERT_SHA1: ${{ inputs.application_certificate_sha1 || vars.APPLE_APPLICATION_CERTIFICATE_SHA1 }}
      INST_CERT_SHA1: ${{ inputs.installer_certificate_sha1 || vars.APPLE_INSTALLER_CERTIFICATE_SHA1 }}
    steps:
      - name: Validate distribution credentials
        env:
          APP_P12: ${{ secrets.DEVELOPER_ID_CERTIFICATE_P12 }}
          APP_P12_PW: ${{ secrets.DEVELOPER_ID_CERTIFICATE_PASSWORD }}
          INST_P12: ${{ secrets.DEVELOPER_ID_INSTALLER_P12 }}
          INST_P12_PW: ${{ secrets.DEVELOPER_ID_INSTALLER_PASSWORD }}
          ASC_KEY_ID: ${{ secrets.APP_STORE_CONNECT_KEY_ID }}
          ASC_ISSUER_ID: ${{ secrets.APP_STORE_CONNECT_ISSUER_ID }}
          ASC_PRIVATE_KEY_P8_BASE64: ${{ secrets.APP_STORE_CONNECT_PRIVATE_KEY_P8_BASE64 }}
        run: |
          missing=()
          for name in APP_P12 APP_P12_PW INST_P12 INST_P12_PW ASC_KEY_ID ASC_ISSUER_ID ASC_PRIVATE_KEY_P8_BASE64 APPLE_TEAM_ID APP_CERT_SHA1 INST_CERT_SHA1; do
            [ -n "${!name}" ] || missing+=("$name")
          done
          if [ "${#missing[@]}" -ne 0 ]; then
            printf '::error::Distribution signing is missing required credentials or identity configuration: %s\n' "${missing[*]}"
            exit 1
          fi

      - name: Download unsigned component package
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
        with:
          name: macos-signing-input-${{ inputs.version }}-${{ inputs.arch }}
          path: signing-input

      - name: Expand signing input without executing it
        run: |
          pkgutil --expand-full signing-input/wsjtx-component.pkg "$RUNNER_TEMP/expanded-component"
          test -d "$RUNNER_TEMP/expanded-component/Payload/Applications/wsjtx.app"
          test -d "$RUNNER_TEMP/expanded-component/Payload/usr/local/wsjtx"

      - name: Import and verify Developer ID identities
        id: identities
        env:
          APP_P12: ${{ secrets.DEVELOPER_ID_CERTIFICATE_P12 }}
          APP_P12_PW: ${{ secrets.DEVELOPER_ID_CERTIFICATE_PASSWORD }}
          INST_P12: ${{ secrets.DEVELOPER_ID_INSTALLER_P12 }}
          INST_P12_PW: ${{ secrets.DEVELOPER_ID_INSTALLER_PASSWORD }}
        run: |
          KEYCHAIN_PATH="$RUNNER_TEMP/release-signing.keychain-db"
          KEYCHAIN_PASSWORD="$(openssl rand -hex 16)"
          security create-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH"
          security set-keychain-settings -lut 1800 "$KEYCHAIN_PATH"
          security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH"
          printf '%s' "$APP_P12" | base64 --decode > "$RUNNER_TEMP/app.p12"
          printf '%s' "$INST_P12" | base64 --decode > "$RUNNER_TEMP/installer.p12"
          security import "$RUNNER_TEMP/app.p12" -P "$APP_P12_PW" -A -t cert -f pkcs12 -k "$KEYCHAIN_PATH"
          security import "$RUNNER_TEMP/installer.p12" -P "$INST_P12_PW" -A -t cert -f pkcs12 -k "$KEYCHAIN_PATH"
          rm -f "$RUNNER_TEMP/app.p12" "$RUNNER_TEMP/installer.p12"
          security set-key-partition-list -S apple-tool:,apple: -k "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH"
          security list-keychains -d user -s "$KEYCHAIN_PATH" login.keychain-db

          APP_IDENTITY=$(security find-identity -v -p codesigning "$KEYCHAIN_PATH" | awk -F'"' '/Developer ID Application/ {print $2; exit}')
          INST_IDENTITY=$(security find-identity -v "$KEYCHAIN_PATH" | awk -F'"' '/Developer ID Installer/ {print $2; exit}')
          [ -n "$APP_IDENTITY" ] || { echo "::error::No Developer ID Application identity was imported."; exit 1; }
          [ -n "$INST_IDENTITY" ] || { echo "::error::No Developer ID Installer identity was imported."; exit 1; }
          [[ "$APP_IDENTITY" == *"(${APPLE_TEAM_ID})"* ]] || { echo "::error::Application identity is not owned by the expected team."; exit 1; }
          [[ "$INST_IDENTITY" == *"(${APPLE_TEAM_ID})"* ]] || { echo "::error::Installer identity is not owned by the expected team."; exit 1; }

          app_actual=$(security find-certificate -c "$APP_IDENTITY" -Z "$KEYCHAIN_PATH" | awk '/SHA-1 hash:/ {print toupper($3); exit}')
          inst_actual=$(security find-certificate -c "$INST_IDENTITY" -Z "$KEYCHAIN_PATH" | awk '/SHA-1 hash:/ {print toupper($3); exit}')
          app_expected=$(printf '%s' "$APP_CERT_SHA1" | tr -d ': ' | tr '[:lower:]' '[:upper:]')
          inst_expected=$(printf '%s' "$INST_CERT_SHA1" | tr -d ': ' | tr '[:lower:]' '[:upper:]')
          [ "$app_actual" = "$app_expected" ] || { echo "::error::Application certificate fingerprint mismatch."; exit 1; }
          [ "$inst_actual" = "$inst_expected" ] || { echo "::error::Installer certificate fingerprint mismatch."; exit 1; }
          echo "application=$APP_IDENTITY" >> "$GITHUB_OUTPUT"
          echo "installer=$INST_IDENTITY" >> "$GITHUB_OUTPUT"

      - name: Sign staged application and command-line tools
        env:
          APP_IDENTITY: ${{ steps.identities.outputs.application }}
        run: |
          PAYLOAD="$RUNNER_TEMP/expanded-component/Payload"
          APP="$PAYLOAD/Applications/wsjtx.app"
          ENTITLEMENTS="$GITHUB_WORKSPACE/signing-input/entitlements.plist"
          test -f "$ENTITLEMENTS"

          find "$APP/Contents/Frameworks" -type f \( -name "*.dylib" -o -perm +111 \) \
            -exec codesign --force --options runtime --timestamp --sign "$APP_IDENTITY" {} \;
          find "$APP/Contents/Frameworks" -name "*.framework" -type d | while read -r framework; do
            codesign --force --options runtime --timestamp --sign "$APP_IDENTITY" "$framework"
          done
          find "$APP/Contents/PlugIns" -type f -name "*.dylib" \
            -exec codesign --force --options runtime --timestamp --sign "$APP_IDENTITY" {} \;
          find "$APP/Contents/MacOS" -type f -perm +111 \
            -exec codesign --force --options runtime --entitlements "$ENTITLEMENTS" --timestamp --sign "$APP_IDENTITY" {} \;
          codesign --force --options runtime --entitlements "$ENTITLEMENTS" --timestamp --sign "$APP_IDENTITY" "$APP"

          find "$PAYLOAD/usr/local/wsjtx" -maxdepth 1 -type f -perm +111 \
            -exec codesign --force --options runtime --entitlements "$ENTITLEMENTS" --timestamp --sign "$APP_IDENTITY" {} \;
          [ ! -d "$PAYLOAD/usr/local/wsjtx/lib" ] || find "$PAYLOAD/usr/local/wsjtx/lib" -type f \
            -exec codesign --force --options runtime --timestamp --sign "$APP_IDENTITY" {} \;
          codesign --verify --deep --strict --verbose=2 "$APP"

      - name: Build signed distribution package
        id: package
        env:
          INST_IDENTITY: ${{ steps.identities.outputs.installer }}
        run: |
          COMPONENT="$RUNNER_TEMP/wsjtx-signed-component.pkg"
          pkgbuild \
            --root "$RUNNER_TEMP/expanded-component/Payload" \
            --scripts "$RUNNER_TEMP/expanded-component/Scripts" \
            --component-plist "$GITHUB_WORKSPACE/signing-input/components.plist" \
            --identifier "org.k1jt.wsjtx.${ARCH}" \
            --version "$VERSION" \
            --install-location / \
            "$COMPONENT"
          pkgutil --payload-files "$COMPONENT" >/dev/null
          PKG_OUT="wsjtx-${VERSION}-${ARCH}-macOS.pkg"
          productbuild \
            --package "$COMPONENT" \
            --sign "$INST_IDENTITY" \
            "$PKG_OUT"
          echo "path=$PKG_OUT" >> "$GITHUB_OUTPUT"

      - name: Verify rebuilt distribution package
        run: |
          PKG_OUT="${{ steps.package.outputs.path }}"
          CHECK_PACKAGE="$RUNNER_TEMP/signed-package-check"
          pkgutil --expand-full "$PKG_OUT" "$CHECK_PACKAGE"
          COMPONENT="$CHECK_PACKAGE/wsjtx-signed-component.pkg"
          PAYLOAD="$COMPONENT/Payload"
          APP="$PAYLOAD/Applications/wsjtx.app"
          test -x "$APP/Contents/MacOS/qmap"
          test -x "$APP/Contents/MacOS/map65"
          test -x "$COMPONENT/Scripts/postinstall"
          test -d "$PAYLOAD/usr/local/wsjtx"
          codesign --verify --deep --strict --verbose=2 "$APP"

      - name: Notarize and staple distribution package
        id: notarize
        env:
          ASC_KEY_ID: ${{ secrets.APP_STORE_CONNECT_KEY_ID }}
          ASC_ISSUER_ID: ${{ secrets.APP_STORE_CONNECT_ISSUER_ID }}
          ASC_PRIVATE_KEY_P8_BASE64: ${{ secrets.APP_STORE_CONNECT_PRIVATE_KEY_P8_BASE64 }}
        run: |
          KEY_PATH="$RUNNER_TEMP/AuthKey_${ASC_KEY_ID}.p8"
          printf '%s' "$ASC_PRIVATE_KEY_P8_BASE64" | base64 --decode > "$KEY_PATH"
          chmod 600 "$KEY_PATH"
          xcrun notarytool submit "${{ steps.package.outputs.path }}" \
            --key "$KEY_PATH" \
            --key-id "$ASC_KEY_ID" \
            --issuer "$ASC_ISSUER_ID" \
            --wait --output-format json > notarization.json
          SUBMISSION_ID=$(jq -r '.id // empty' notarization.json)
          STATUS=$(jq -r '.status // empty' notarization.json)
          if [ "$STATUS" != "Accepted" ] || [ -z "$SUBMISSION_ID" ]; then
            echo "::error::Apple notarization did not return Accepted (status=${STATUS:-missing})."
            [ -n "$SUBMISSION_ID" ] && xcrun notarytool log "$SUBMISSION_ID" \
              --key "$KEY_PATH" --key-id "$ASC_KEY_ID" --issuer "$ASC_ISSUER_ID" || true
            exit 1
          fi
          xcrun stapler staple "${{ steps.package.outputs.path }}"
          xcrun stapler validate "${{ steps.package.outputs.path }}"
          echo "submission_id=$SUBMISSION_ID" >> "$GITHUB_OUTPUT"
          echo "status=$STATUS" >> "$GITHUB_OUTPUT"
          rm -f "$KEY_PATH"

      - name: Verify distribution trust and write signing report
        run: |
          PKG_OUT="${{ steps.package.outputs.path }}"
          COMPONENT="$RUNNER_TEMP/signed-package-check/wsjtx-signed-component.pkg"
          APP="$COMPONENT/Payload/Applications/wsjtx.app"
          codesign --verify --deep --strict --verbose=2 "$APP"
          codesign -dv --verbose=4 "$APP" 2> codesign-details.txt
          grep -F "TeamIdentifier=${APPLE_TEAM_ID}" codesign-details.txt
          pkgutil --check-signature "$PKG_OUT" | tee pkg-signature.txt
          grep -F "Developer ID Installer:" pkg-signature.txt
          grep -F "(${APPLE_TEAM_ID})" pkg-signature.txt
          xcrun stapler validate "$PKG_OUT"
          spctl --assess --type install --verbose=4 "$PKG_OUT"
          SHA256=$(shasum -a 256 "$PKG_OUT" | awk '{print $1}')
          jq -n \
            --arg mode distribution \
            --arg artifact "$PKG_OUT" \
            --arg sha256 "$SHA256" \
            --arg git_sha "$GITHUB_SHA" \
            --arg team_id "$APPLE_TEAM_ID" \
            --arg app_certificate_sha1 "$(printf '%s' "$APP_CERT_SHA1" | tr -d ': ' | tr '[:lower:]' '[:upper:]')" \
            --arg installer_certificate_sha1 "$(printf '%s' "$INST_CERT_SHA1" | tr -d ': ' | tr '[:lower:]' '[:upper:]')" \
            --arg notarization_id "${{ steps.notarize.outputs.submission_id }}" \
            --arg notarization_status "${{ steps.notarize.outputs.status }}" \
            '{schema_version: 1, mode: $mode, artifact: $artifact, sha256: $sha256, git_sha: $git_sha, team_id: $team_id, application_certificate_sha1: $app_certificate_sha1, installer_certificate_sha1: $installer_certificate_sha1, notarization: {id: $notarization_id, status: $notarization_status}, stapled: true, gatekeeper_accepted: true, isolated_signing_job: true}' \
            > macos-signing-report.json

      - name: Remove signing credentials
        if: always()
        run: |
          rm -f "$RUNNER_TEMP/app.p12" "$RUNNER_TEMP/installer.p12" "$RUNNER_TEMP"/AuthKey_*.p8
          security delete-keychain "$RUNNER_TEMP/release-signing.keychain-db" 2>/dev/null || true

      - name: Upload signed distribution package
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: ${{ steps.package.outputs.path }}
          path: ${{ steps.package.outputs.path }}

      - name: Upload distribution signing report
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: macos-signing-report-${{ inputs.version }}-${{ inputs.arch }}
          path: macos-signing-report.json