Repository navigation
Expand file tree
/
Copy pathbuild.py
More file actions
2838 lines (2583 loc) · 128 KB
/
Copy pathbuild.py
File metadata and controls
2838 lines (2583 loc) · 128 KB
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
#!/usr/bin/env python3
# Copyright (c) Charles J. Cliffe
# SPDX-License-Identifier: GPL-2.0+
"""Build CubicSDR from this checkout: dependencies, configure, build, package.
Layout: third-party sources live under build/deps/src, everything installs
into the build/deps prefix, and stamp files under build/deps/stamps record
completed steps so reruns skip finished work. Pass --dry-run to preview the
full command sequence without running anything.
"""
import argparse
import glob
import hashlib
import os
import platform
import re
import shutil
import subprocess
import sys
import tarfile
import urllib.request
import zipfile
ROOT = os.path.dirname(os.path.abspath(__file__))
WX_VERSION = "3.2.1"
WX_URL = "https://github.com/wxWidgets/wxWidgets/releases/download/v%s/wxWidgets-%s.tar.bz2"
SOAPY_TAG = "soapy-sdr-0.8.1"
SOAPY_URL = "https://github.com/pothosware/SoapySDR.git"
LIQUID_URL = "https://github.com/jgaeddert/liquid-dsp.git"
LIQUID_TAG = "v1.8.3"
HAMLIB_VERSION = "4.4"
HAMLIB_URL = "https://github.com/Hamlib/Hamlib/releases/download/4.4/hamlib-%s-4.4.zip"
LINUXDEPLOY_URL = "https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage"
def log(text):
"""Print a build step immediately; unbuffered so CI logs stay ordered."""
print(text, flush=True)
def run(cmd, cwd=None, env=None, dry_run=False):
"""Echo then execute a command; extra env entries merge over os.environ."""
log("+ %s" % " ".join(cmd))
if dry_run:
return
merged = dict(os.environ)
if env:
merged.update(env)
subprocess.run(cmd, cwd=cwd or ROOT, env=merged, check=True)
def require(prog, dry_run=False):
"""Abort unless prog is on PATH; in dry-run just note that it is needed."""
if shutil.which(prog) is None:
if dry_run:
log("would require tool '%s'" % prog)
else:
sys.exit("error: required tool '%s' not found on PATH" % prog)
def download(url, dest, dry_run=False, retries=3):
"""Fetch url to dest, resuming-safe via a .part file; skip if dest exists.
Sends a browser User-Agent: some hosts (SDRplay's file manager
included) reject urllib's default with 403.
"""
if os.path.exists(dest):
log("have %s; skipping download" % dest)
return
log("+ download %s" % url)
if dry_run:
return
os.makedirs(os.path.dirname(dest), exist_ok=True)
for attempt in range(retries):
try:
request = urllib.request.Request(
url, headers={"User-Agent": "Mozilla/5.0"})
with urllib.request.urlopen(request) as response, \
open(dest + ".part", "wb") as handle:
shutil.copyfileobj(response, handle)
os.replace(dest + ".part", dest)
return
except Exception as error:
log("download attempt %d failed: %s" % (attempt + 1, error))
sys.exit("error: download failed after %d attempts: %s" % (retries, url))
def vs_install_root(what):
"""Newest Visual Studio installation path via vswhere."""
vswhere = os.path.join(os.environ.get("ProgramFiles(x86)", ""),
"Microsoft Visual Studio", "Installer", "vswhere.exe")
if os.path.exists(vswhere):
root = subprocess.run(
[vswhere, "-latest", "-property", "installationPath"],
capture_output=True, text=True).stdout.strip()
if root:
return root
sys.exit("error: %s not found; install the MSVC C++ workload or "
"run from a Visual Studio Developer shell" % what)
def vs_file(parts, what):
"""Locate a Visual Studio tool via vswhere; newest match wins."""
matches = sorted(glob.glob(os.path.join(vs_install_root(what), *parts)))
if matches:
return matches[-1]
sys.exit("error: %s not found; install the MSVC C++ workload or "
"run from a Visual Studio Developer shell" % what)
def msvc_lib_exe():
"""Path to lib.exe for building import libraries from .def files."""
return vs_file(("VC", "Tools", "MSVC", "*", "bin", "Hostx64", "x64",
"lib.exe"), "lib.exe")
def vsdevcmd_env(arch, dry_run=False):
"""Capture a Visual Studio Developer-shell environment for arch.
Ninja-based builds need the MSVC/SDK tools (rc, mt, link, lib)
that only a developer shell puts on PATH; CMake's own VS
discovery does not cover them. Returns the full captured
environment for run(); empty during a dry run. Captured keys are
folded to the parent's case so run()'s merge overrides instead of
duplicating case-variant entries.
"""
if dry_run:
return {}
bat = os.path.join(vs_install_root("VsDevCmd.bat"),
"Common7", "Tools", "VsDevCmd.bat")
vc_arch = "amd64" if arch == "x64" else "x86"
try:
out = subprocess.run(
'"%s" -arch=%s -host_arch=amd64 >nul && set' % (bat, vc_arch),
shell=True, capture_output=True, text=True, check=True).stdout
except subprocess.CalledProcessError:
sys.exit("error: VsDevCmd.bat failed; repair the Visual Studio install")
env = {}
for line in out.splitlines():
if "=" in line:
key, value = line.split("=", 1)
env[key] = value
parent_keys = {key.lower(): key for key in os.environ}
for key in list(env):
parent = parent_keys.get(key.lower())
if parent is not None and parent != key:
env[parent] = env.pop(key)
return env
def vs_llvm_dir():
"""VS-bundled LLVM directory; the only supported clang toolchain."""
root = os.path.join(vs_install_root("Visual Studio"),
"VC", "Tools", "Llvm")
exe = os.path.join(root, "x64", "bin", "clang-cl.exe")
if os.path.isfile(exe):
return root
sys.exit("error: VS clang-cl not found; add 'C++ Clang Compiler for "
"Windows' in the VS Installer")
def msbuild_exe(dry_run=False):
"""Path to msbuild.exe, or its bare name when only previewing."""
if dry_run:
return "msbuild"
return vs_file(("MSBuild", "Current", "Bin", "amd64", "msbuild.exe"),
"msbuild.exe")
def devenv_exe(dry_run=False):
"""Path to devenv.com, used only for one-off .sln upgrades."""
if dry_run:
return "devenv"
return vs_file(("Common7", "IDE", "devenv.com"), "devenv")
def msbuild_sln(sln, config, arch, dry_run=False, upgrade=False):
"""Build a vendored Visual Studio solution in place."""
if upgrade:
run([devenv_exe(dry_run), "/Upgrade", sln], dry_run=dry_run)
run([msbuild_exe(dry_run), sln, "/p:Configuration=%s" % config,
"/p:Platform=%s" % arch], dry_run=dry_run)
def unzip_staged(tarball, dest, dry_run=False):
"""Extract a zip, collapsing a single top-level directory when present."""
if os.path.isdir(dest):
return
if dry_run:
log("+ extract %s" % tarball)
return
stage = dest + ".stage"
shutil.rmtree(stage, ignore_errors=True)
with zipfile.ZipFile(tarball) as archive:
archive.extractall(stage)
entries = os.listdir(stage)
if len(entries) == 1 and os.path.isdir(os.path.join(stage, entries[0])):
shutil.move(os.path.join(stage, entries[0]), dest)
else:
shutil.move(stage, dest)
shutil.rmtree(stage, ignore_errors=True)
def cmake_build(src, build, config, jobs, prefix, arch=None, extra=None,
dry_run=False, env=None):
"""Configure, build, and install one CMake project into the prefix."""
cmd = ["cmake", "-S", src, "-B", build,
"-DCMAKE_BUILD_TYPE=%s" % config,
"-DCMAKE_INSTALL_PREFIX=%s" % prefix] + (extra or [])
if arch:
cmd += ["-G", "Visual Studio 17 2022", "-A", arch]
run(cmd, dry_run=dry_run, env=env)
cmd = ["cmake", "--build", build]
if arch:
cmd += ["--config", config]
cmd += ["--parallel", str(jobs)] if arch else ["-j", str(jobs)]
run(cmd, dry_run=dry_run, env=env)
cmd = ["cmake", "--install", build]
if arch:
cmd += ["--config", config]
run(cmd, dry_run=dry_run, env=env)
# Every SoapySDR module this script knows how to build. netsdr is excluded
# from the default set because it hangs device enumeration; uhd is excluded
# because few users have the hardware and its build is very long.
SUPPORTED_MODULES = ["audio", "remote", "redpitaya", "netsdr",
"rtlsdr", "airspy", "airspyhf", "hackrf",
"limesuite", "bladerf", "pluto", "uhd", "sdrplay"]
BOOST_VERSION = "1.84.0"
BOOST_MSVC = "14.3"
UHD_TAG = "v4.6.0.0"
XML2_VERSION = "2.13.5"
DEFAULT_OPT_OUT = {"netsdr", # hangs device enumeration
"uhd"} # rare hardware, very long build; opt in explicitly
DEFAULT_MODULES = [name for name in SUPPORTED_MODULES
if name not in DEFAULT_OPT_OUT]
# Module key to Soapy source repo and CMake project name. Shared by the
# Windows and Linux module builders; per-module configure flags live in
# each builder's local libs table.
SOAPY_REPOS = {
"audio": "https://github.com/pothosware/SoapyAudio.git",
"remote": "https://github.com/pothosware/SoapyRemote.git",
"redpitaya": "https://github.com/pothosware/SoapyRedPitaya.git",
"netsdr": "https://github.com/pothosware/SoapyNetSDR.git",
"rtlsdr": "https://github.com/pothosware/SoapyRTLSDR.git",
"airspy": "https://github.com/pothosware/SoapyAirspy.git",
"airspyhf": "https://github.com/pothosware/SoapyAirspyHF.git",
"hackrf": "https://github.com/pothosware/SoapyHackRF.git",
"bladerf": "https://github.com/pothosware/SoapyBladeRF.git",
"pluto": "https://github.com/pothosware/SoapyPlutoSDR.git",
"uhd": "https://github.com/pothosware/SoapyUHD.git",
"sdrplay": "https://github.com/pothosware/SoapySDRPlay3.git",
}
SOAPY_NAMES = {
"audio": "SoapyAudio", "remote": "SoapyRemote",
"redpitaya": "SoapyRedPitaya", "netsdr": "SoapyNetSDR",
"rtlsdr": "SoapyRTLSDR", "airspy": "SoapyAirspy",
"airspyhf": "SoapyAirspyHF", "hackrf": "SoapyHackRF",
"bladerf": "SoapyBladeRF", "pluto": "SoapyPlutoSDR",
"uhd": "SoapyUHD", "sdrplay": "SoapySDRPlay3",
}
SDRPLAY_API_VERSION = "3.15"
# Vendor API installers. The wpdmdl IDs are permanent download slots,
# but SDRplay reissues them when a package is re-uploaded: a 404 here
# means re-scrape https://sdrplay.com/hardware-api/ for fresh IDs.
SDRPLAY_API_URLS = {
"windows": "https://sdrplay.com/download/hardware-api-windows/?wpdmdl=1905",
"linux": "https://sdrplay.com/download/hardware-api-linux/?wpdmdl=1906",
"macos": "https://sdrplay.com/download/hardware-api-macos/?wpdmdl=1907",
}
# Unpacks Inno Setup installers without admin rights or a service
# install; only the headers and library are needed to build and ship.
INNOEXTRACT_URL = ("https://constexpr.org/innoextract/files/"
"innoextract-1.9-windows.zip")
def selected_modules(spec):
"""Resolve a --modules value to an ordered module key list."""
if spec == "default":
return list(DEFAULT_MODULES)
if spec == "all":
return list(SUPPORTED_MODULES)
if spec == "none":
return []
selected = spec.split(",")
for name in selected:
if name not in SUPPORTED_MODULES:
sys.exit("error: unknown module '%s' (supported: %s)" % (
name, ", ".join(SUPPORTED_MODULES)))
return selected
def find_prefix_file(prefix, pattern):
"""Find a built file under the prefix, preferring installed copies.
Glob results sort build-tree paths last so an installed library wins
over an intermediate object of the same name.
"""
matches = sorted(glob.glob(os.path.join(prefix, "**", pattern),
recursive=True),
key=lambda path: ("build" in path.split(os.sep), path))
if not matches:
sys.exit("error: %s not found under %s" % (pattern, prefix))
return matches[0]
def header_dir(prefix, header, dry_run=False):
"""Directory containing a built header under the prefix."""
if dry_run:
return os.path.join(prefix, "include")
return os.path.dirname(find_prefix_file(prefix, header))
def stage_runtime_windows(prefix, out, dry_run=False):
"""Copy prefix DLLs beside the exe and SoapySDR modules into modules/.
Modules that are no longer selected are pruned so a deselected module
cannot linger and load on the next launch.
"""
if dry_run:
log("+ stage runtime DLLs and modules to %s" % out)
return
os.makedirs(out, exist_ok=True)
for dll in glob.glob(os.path.join(prefix, "bin", "*.dll")):
shutil.copy(dll, out)
for mods in glob.glob(os.path.join(prefix, "lib", "SoapySDR", "modules*")):
dest = os.path.join(out, "modules")
os.makedirs(dest, exist_ok=True)
wanted = set()
for dll in glob.glob(os.path.join(mods, "*.dll")):
wanted.add(os.path.basename(dll))
shutil.copy(dll, dest)
for stale in glob.glob(os.path.join(dest, "*.dll")):
if os.path.basename(stale) not in wanted:
log("removing deselected module %s" % stale)
if not dry_run:
os.remove(stale)
log("staged runtime to %s" % out)
def stage_dlls(libs, prefix, dry_run=False):
"""Copy each library's sibling DLLs into the prefix bin directory."""
dest = os.path.join(prefix, "bin")
if dry_run:
log("+ stage dependency DLLs to %s" % dest)
return
os.makedirs(dest, exist_ok=True)
for lib in libs:
for dll in glob.glob(os.path.join(os.path.dirname(lib), "*.dll")):
target = os.path.join(dest, os.path.basename(dll))
if os.path.abspath(dll) != os.path.abspath(target):
shutil.copy(dll, target)
def soapy_module(name, url, prefix, config, jobs, arch, dry_run=False,
extra=None, env=None):
"""Clone, build, and install one SoapySDR module into the prefix.
On Linux the installed module gets an $ORIGIN-relative rpath so it
finds libSoapySDR and the device libraries two dirs up (usr/lib),
which holds both for the prefix layout (lib/SoapySDR/modules*/) and
the AppDir layout (bin/modules/). On macOS the module gets an
absolute-prefix rpath so @rpath ids resolve both unbundled and at
fixup_bundle time. The macOS stamp key covers the target arch so a
cross build rebuilds modules first configured natively.
"""
stamp = "mod-" + name.lower()
flags = ["-DCMAKE_PREFIX_PATH=%s" % prefix] + (extra or [])
if platform.system() == "Linux" and arch is None:
# Plain -D passes $ORIGIN through verbatim (no shell involved).
flags += ["-DCMAKE_INSTALL_RPATH=$ORIGIN/../../lib",
"-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON"]
if platform.system() == "Darwin":
flags += ["-DCMAKE_INSTALL_RPATH=%s" % os.path.join(prefix, "lib"),
"-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON"]
target = (env or {}).get("CMAKE_OSX_ARCHITECTURES", "")
floor = (env or {}).get("MACOSX_DEPLOYMENT_TARGET", "")
key = stamp_key(flags + ([target] if target else [])
+ ([floor] if floor else []))
else:
key = stamp_key(extra or [])
floor = (env or {}).get("MACOSX_DEPLOYMENT_TARGET")
if stamp_valid(prefix, stamp, key,
adopt=not dry_run and floor is None):
log("%s already built; skipping" % name)
return
if os.path.exists(stamp_path(prefix, stamp)):
log("%s flags changed; rebuilding" % name)
src = os.path.join(prefix, "src", name)
git_clone(url, src, dry_run)
cmake_build(src, os.path.join(prefix, "build", name), config, jobs,
prefix, arch, flags, dry_run, env)
mark_done(prefix, stamp, dry_run, key)
def stamp_path(prefix, name):
"""Stamp file recording that a named build step completed."""
return os.path.join(prefix, "stamps", name + ".done")
def stamp_key(parts):
"""Short hash of configure inputs; a flag edit must invalidate the stamp."""
return hashlib.md5(repr(parts).encode()).hexdigest()[:12]
def stamp_valid(prefix, name, key=None, adopt=True):
"""True when a step can be skipped: stamp exists and, if keyed, matches.
Pre-keying "ok" stamps are adopted once by writing the current key, so
existing checkouts keep skipping until flags actually change.
"""
path = stamp_path(prefix, name)
if not os.path.exists(path):
return False
if key is None:
return True
with open(path) as handle:
seen = handle.read().strip()
if seen == key:
return True
if seen == "ok" and adopt:
with open(path, "w") as handle:
handle.write("%s\n" % key)
return True
return False
def mark_done(prefix, name, dry_run=False, key=None):
"""Record a completed step; never writes anything during a dry run."""
if dry_run:
return
os.makedirs(os.path.dirname(stamp_path(prefix, name)), exist_ok=True)
with open(stamp_path(prefix, name), "w") as handle:
handle.write("%s\n" % (key or "ok"))
def macos_stamp_parts(env, host=None):
"""Stamp inputs distinguishing macOS build flavors; None off-mac.
Covers the target architecture, the autoconf host triplet, and the
deployment-target floor, so a restored cache from another flavor
(or predating the floor pin) rebuilds instead of silently reusing
binaries that will not load where the bundle targets.
"""
env = env or {}
if "CMAKE_OSX_ARCHITECTURES" not in env:
return None
return [env.get("CMAKE_OSX_ARCHITECTURES"), host,
env.get("MACOSX_DEPLOYMENT_TARGET")]
def ensure_macos_prefix_flavor(prefix, arch, host, min_version, skip_deps,
dry_run=False):
"""Wipe a macOS prefix built for another flavor, or fail when reusing it.
A restored cache (or local prefix) from another architecture or
deployment floor rebuilds nothing on its own: stamps match, build
trees look fresh, and the bundle ships stale binaries. A recorded
flavor mismatch therefore rebuilds from scratch; unknown provenance
wipes only when a floor is demanded, otherwise it is adopted.
--skip-deps never wipes: reusing a mismatched prefix fails loudly
instead. Use --deps-prefix for side-by-side flavors.
"""
flavor = "%s|%s|%s" % (arch, host, min_version)
path = os.path.join(prefix, "macos-flavor")
if dry_run:
log("+ ensure macOS prefix flavor %s" % flavor)
return
try:
with open(path) as handle:
recorded = handle.read().strip()
except OSError:
recorded = None
if recorded == flavor:
return
if skip_deps:
if recorded is None:
return
sys.exit("error: dependency prefix %s holds macOS flavor %s; "
"rebuild without --skip-deps or pass --clean"
% (prefix, recorded))
if recorded is None and min_version is None:
os.makedirs(prefix, exist_ok=True)
with open(path, "w") as handle:
handle.write("%s\n" % flavor)
return
if recorded is None:
log("dependency prefix flavor unknown; rebuilding from scratch "
"for macOS %s (use --deps-prefix for side-by-side flavors)"
% flavor)
else:
log("dependency prefix flavor changed (%s -> %s); rebuilding from "
"scratch (use --deps-prefix for side-by-side flavors)"
% (recorded, flavor))
if os.path.isdir(prefix):
_rmtree(prefix)
os.makedirs(prefix, exist_ok=True)
with open(path, "w") as handle:
handle.write("%s\n" % flavor)
def parse_args(argv):
parser = argparse.ArgumentParser(description="Build CubicSDR from this checkout.")
parser.add_argument("--platform", default="auto",
choices=["auto", "windows", "linux", "macos"])
parser.add_argument("--config", default="Release", choices=["Release", "Debug"])
parser.add_argument("--arch", default="x64", choices=["x64", "Win32"])
parser.add_argument("--macos-arch", default="native",
choices=["native", "x86_64", "arm64"],
help="macOS binary architecture; anything but native "
"cross-compiles via -arch flags")
parser.add_argument("--macos-min-version", default=None,
help="macOS deployment target (e.g. 11.0); pins "
"MACOSX_DEPLOYMENT_TARGET and -mmacosx-version-min "
"for the app and every dependency so the bundle "
"still loads on older macOS releases")
parser.add_argument("--jobs", default=str(min(os.cpu_count() or 2, 8)),
help="parallel build jobs (default: CPU count, max 8)")
parser.add_argument("--deps-prefix", default=None)
parser.add_argument("--skip-deps", action="store_true")
parser.add_argument("--modules", default="default",
help="'default', 'all', 'none', or comma-separated module list")
parser.add_argument("--skip-app", action="store_true",
help="build and stage modules only; skip the CubicSDR app")
parser.add_argument("--build-deps", action="store_true",
help="build all dependencies from source instead of using system packages (Linux)")
parser.add_argument("--package", default="none",
choices=["none", "deb", "nsis", "dmg", "appimage"])
parser.add_argument("--clean", action="store_true")
parser.add_argument("--dry-run", action="store_true")
return parser.parse_args(argv)
def default_prefix():
"""Dependency prefix; overridable with --deps-prefix."""
return os.path.join(ROOT, "build", "deps")
def _rmtree(path):
"""Remove a directory tree, retrying read-only files (git objects)."""
def _onerror(func, target, _exc):
os.chmod(target, 0o700)
func(target)
shutil.rmtree(path, onerror=_onerror)
def _checkout_matches(dest, branch):
"""True when dest's checkout is already at the requested branch or tag."""
try:
wanted = subprocess.run(
["git", "-C", dest, "rev-parse", "--verify", "--quiet",
"%s^{commit}" % branch],
capture_output=True, text=True, check=False)
head = subprocess.run(["git", "-C", dest, "rev-parse", "HEAD"],
capture_output=True, text=True, check=False)
except OSError:
return False
return (wanted.returncode == 0 and head.returncode == 0
and wanted.stdout.strip() == head.stdout.strip())
def git_clone(url, dest, dry_run=False, branch=None):
"""Shallow-clone url once; reuse the existing checkout on later runs.
When a branch (or tag) is pinned but the existing checkout predates the
pin, it is removed and re-cloned so stale sources can never silently
satisfy a new pin.
"""
if os.path.isdir(dest):
if branch and not dry_run and not _checkout_matches(dest, branch):
log("%s predates branch %s; re-cloning" % (dest, branch))
_rmtree(dest)
else:
log("%s exists; reusing" % dest)
return
cmd = ["git", "clone", "--depth", "1"]
if branch:
cmd += ["-b", branch]
run(cmd + [url, dest], dry_run=dry_run)
def wx_source(prefix, dry_run=False):
"""Download and unpack the pinned wxWidgets release under src/."""
dest = os.path.join(prefix, "src", "wxWidgets-%s" % WX_VERSION)
if not os.path.isdir(dest):
tarball = os.path.join(prefix, "src", "wxWidgets-%s.tar.bz2" % WX_VERSION)
download(WX_URL % (WX_VERSION, WX_VERSION), tarball, dry_run)
if not dry_run:
with tarfile.open(tarball) as archive:
archive.extractall(os.path.join(prefix, "src"))
return dest
def build_wx_linux(prefix, jobs, dry_run=False):
"""Build static monolithic wxWidgets for Linux into the prefix."""
src = wx_source(prefix, dry_run)
if os.path.exists(stamp_path(prefix, "wx")):
log("wxWidgets already built; skipping")
return os.path.join(prefix, "wx", "bin", "wx-config")
run(["sh", "autogen.sh"], cwd=src, dry_run=dry_run)
run(["./configure", "--with-opengl", "--disable-glcanvasegl",
"--disable-shared", "--enable-monolithic", "--with-libjpeg",
"--with-libtiff", "--with-libpng", "--with-zlib",
"--disable-sdltest", "--enable-unicode", "--enable-display",
"--enable-propgrid", "--disable-webview", "--disable-webviewwebkit",
"--prefix=%s" % os.path.join(prefix, "wx")], cwd=src, dry_run=dry_run)
run(["make", "-j%s" % jobs], cwd=src, dry_run=dry_run)
run(["make", "install"], cwd=src, dry_run=dry_run)
mark_done(prefix, "wx", dry_run)
return os.path.join(prefix, "wx", "bin", "wx-config")
def scrub_wx_config(src, dry_run=False):
"""Replace surviving @EGREP@ placeholders in generated wx-config.
wx 3.2.1's configure can leave @EGREP@ unsubstituted (seen on the
macOS CI runners for both native and cross builds); every wx-config
query then fails, including the ones CMake's FindwxWidgets relies on.
Repairing the generated scripts covers every cause uniformly.
"""
configs = sorted(glob.glob(os.path.join(src, "lib", "wx", "config", "*")))
if dry_run:
log("+ scrub unsubstituted placeholders from generated wx-config")
return
for path in configs:
if not os.path.isfile(path):
continue
with open(path) as handle:
text = handle.read()
fixed = text.replace("@EGREP@", "grep -E")
if fixed == text:
continue
log("wx-config: substituting unexpanded @EGREP@ in %s"
% os.path.basename(path))
with open(path, "w") as handle:
handle.write(fixed)
leftovers = sorted(set(re.findall(r"@[A-Z_]+@", fixed)))
if leftovers:
log("warning: %s still has placeholders: %s"
% (os.path.basename(path), ", ".join(leftovers)))
def normalize_liquid_macos(prefix, dry_run=False):
"""Make the macOS liquid-dsp install link statically.
liquid-dsp ignores --disable-shared: it always installs its shared
library, and on Darwin the static archive is named libliquid.ar,
which FindLiquid never looks for. Rename the archive to libliquid.a
and drop the shared library so the app links (and bundles nothing
of) the static archive; a bare-install-name dylib breaks
fixup_bundle at packaging time.
"""
pairs = [(os.path.join(prefix, "lib", "libliquid.ar"),
os.path.join(prefix, "lib", "libliquid.a"))]
if dry_run:
log("+ normalize macOS liquid-dsp install to static-only")
return
for src, dest in pairs:
if os.path.isfile(src):
log("renaming %s to %s; macOS links liquid-dsp statically"
% (os.path.basename(src), os.path.basename(dest)))
os.replace(src, dest)
for stale in glob.glob(os.path.join(prefix, "lib", "libliquid*.dylib")):
log("removing shared %s; macOS links liquid-dsp statically"
% os.path.basename(stale))
os.remove(stale)
def build_wx_macos(prefix, jobs, dry_run=False, env=None, host=None):
"""Build static monolithic wxWidgets for macOS into the prefix.
Always exports a working CPP: wx appends its deployment-target flag
to whatever CPP it finds, so an unset CPP becomes bare flags and
kills the bundled-library sub-configures. host is the autoconf
--host triplet for cross-compiles.
"""
src = wx_source(prefix, dry_run)
parts = macos_stamp_parts(env, host)
key = stamp_key(parts) if parts is not None else None
floor = (env or {}).get("MACOSX_DEPLOYMENT_TARGET")
if stamp_valid(prefix, "wx", key,
adopt=not dry_run and floor is None):
log("wxWidgets already built; skipping")
return os.path.join(prefix, "wx", "bin", "wx-config")
if os.path.exists(stamp_path(prefix, "wx")):
log("wxWidgets flags changed; rebuilding")
env = dict(env or {})
cpp = "gcc -E"
if host is not None:
# $CPP never sees $CFLAGS, so the -arch flag rides along here.
cpp += " -arch %s" % host.split("-")[0]
env.setdefault("CPP", cpp)
cmd = ["./configure", "--with-opengl", "--with-libjpeg", "--disable-shared",
"--enable-monolithic", "--with-libtiff", "--with-libpng",
"--with-zlib", "--disable-sdltest", "--enable-unicode",
"--enable-display", "--enable-propgrid", "--disable-webview",
"--disable-webviewwebkit",
"--prefix=%s" % os.path.join(prefix, "wx")]
if host is not None:
cmd += ["--host=%s" % host]
run(["sh", "autogen.sh"], cwd=src, dry_run=dry_run, env=env)
run(cmd, cwd=src, dry_run=dry_run, env=env)
scrub_wx_config(src, dry_run)
run(["make", "-j%s" % jobs], cwd=src, dry_run=dry_run, env=env)
run(["make", "install"], cwd=src, dry_run=dry_run, env=env)
mark_done(prefix, "wx", dry_run, key)
return os.path.join(prefix, "wx", "bin", "wx-config")
def build_wx_windows(prefix, config, arch, jobs, dry_run=False):
"""Build static wxWidgets for Windows via its CMake build into the prefix."""
if os.path.exists(stamp_path(prefix, "wx")):
log("wxWidgets already built; skipping")
return os.path.join(prefix, "wx")
tarball = os.path.join(prefix, "src", "wxWidgets-%s.zip" % WX_VERSION)
download("https://github.com/wxWidgets/wxWidgets/releases/download/v%s/wxWidgets-%s.zip" % (WX_VERSION, WX_VERSION), tarball, dry_run)
src = os.path.join(prefix, "src", "wxWidgets-%s" % WX_VERSION)
if not dry_run:
unzip_staged(tarball, src, dry_run)
build = os.path.join(src, "cmake-build")
run(["cmake", "-S", src, "-B", build, "-G", "Visual Studio 17 2022",
"-A", arch, "-DwxBUILD_SHARED=OFF",
"-DCMAKE_INSTALL_PREFIX=%s" % os.path.join(prefix, "wx")],
dry_run=dry_run)
run(["cmake", "--build", build, "--config", config, "--parallel", str(jobs)],
dry_run=dry_run)
run(["cmake", "--install", build, "--config", config], dry_run=dry_run)
mark_done(prefix, "wx", dry_run)
return os.path.join(prefix, "wx")
def build_liquid(prefix, jobs, dry_run=False, env=None, host=None):
"""Autotools-build liquid-dsp into the prefix (no distro package exists).
Static only: nothing dlopens liquid-dsp, so a shared library would
just be another runtime dependency for packaged builds to carry.
"""
flags = ["--disable-shared", "--enable-static"]
stamp = "lib-liquid"
parts = macos_stamp_parts(env, host)
key = stamp_key([LIQUID_TAG] + flags + (parts or []))
floor = (env or {}).get("MACOSX_DEPLOYMENT_TARGET")
if stamp_valid(prefix, stamp, key,
adopt=not dry_run and floor is None):
log("liquid-dsp already built; skipping")
return
if os.path.exists(stamp_path(prefix, stamp)):
log("liquid-dsp flags changed; rebuilding")
if os.path.exists(stamp_path(prefix, "liquid")):
log("liquid-dsp was shared; rebuilding static")
if not dry_run:
for stale in glob.glob(os.path.join(prefix, "lib", "libliquid.so*")):
os.remove(stale)
os.remove(stamp_path(prefix, "liquid"))
src = os.path.join(prefix, "src", "liquid-dsp")
git_clone(LIQUID_URL, src, dry_run, branch=LIQUID_TAG)
cmd = ["./configure", "--prefix=%s" % prefix] + flags
if host is not None:
cmd += ["--host=%s" % host]
run(["sh", "bootstrap.sh"], cwd=src, dry_run=dry_run, env=env)
run(cmd, cwd=src, dry_run=dry_run, env=env)
run(["make", "-j%s" % jobs], cwd=src, dry_run=dry_run, env=env)
run(["make", "install"], cwd=src, dry_run=dry_run, env=env)
mark_done(prefix, stamp, dry_run, key)
def clang_builtins_lib(llvm_dir, arch):
"""Clang runtime builtins archive that liquid-dsp objects link against.
Clang lowers _Complex multiply to __mulsc3 calls that only its own
runtime provides; MSVC link has no equivalent, so the app link needs
this archive explicitly. The VS layout nests x64 under x64/ but keeps
x86 directly under lib/; newest LLVM release wins.
"""
if arch == "x64":
parts = ["x64", "lib", "clang", "*", "lib", "windows",
"clang_rt.builtins-x86_64.lib"]
else:
parts = ["lib", "clang", "*", "lib", "windows",
"clang_rt.builtins-i386.lib"]
matches = sorted(glob.glob(os.path.join(llvm_dir, *parts)))
if matches:
return os.path.normpath(matches[-1])
sys.exit("error: %s not found under %s; repair the VS Clang install"
% (parts[-1], llvm_dir))
def build_liquid_windows(prefix, config, arch, jobs, dry_run=False):
"""Build liquid-dsp from source with the VS-bundled clang-cl.
MSVC cl cannot compile liquid-dsp (no C99 _Complex), so the Windows
build uses the VS-bundled clang-cl at the pinned tag with a
repo-owned complex.h shim plus a small patch set (timers, cpuid,
bit ops) under cmake/liquid-clangcl. Static only, matching the
other platforms.
Both x64 and Win32 build; x86 cross-compiles from the x64 clang
with --target and links the i386 runtime builtins. Ninja is ensured
before the developer shell is captured so the capture inherits its
PATH entry.
"""
support = os.path.join(ROOT, "cmake", "liquid-clangcl")
shim = os.path.join(support, "shim")
patches = sorted(glob.glob(os.path.join(support, "patches", "*.patch")))
if not patches and not dry_run:
sys.exit("error: no liquid-dsp patches under %s" % support)
cflags = ["/I\"%s\"" % shim.replace(os.sep, "/"),
"-D_USE_MATH_DEFINES", "-D_CRT_SECURE_NO_WARNINGS"]
cxxflags = []
ldflags = []
if arch != "x64":
cflags = ["--target=i686-pc-windows-msvc"] + cflags
cxxflags = ["--target=i686-pc-windows-msvc"]
ldflags = ["-DCMAKE_EXE_LINKER_FLAGS=/machine:X86"]
llvm = vs_llvm_dir() if not dry_run else "Llvm"
clang = (os.path.join(llvm, "x64", "bin", "clang-cl.exe")
if not dry_run else "clang-cl")
opts = ["-DBUILD_SHARED_LIBS=OFF", "-DBUILD_STATIC_LIBS=ON",
"-DBUILD_AUTOTESTS=OFF", "-DBUILD_BENCHMARKS=OFF",
"-DBUILD_EXAMPLES=OFF", "-DBUILD_SANDBOX=OFF",
"-DFIND_SIMD=OFF", "-DFIND_FFTW=OFF", "-DFIND_THREADS=OFF",
"-DC_SSE_FOUND=1", "-DC_SSE2_FOUND=1", "-DC_SSE3_FOUND=1",
"-DC_SSSE3_FOUND=1", "-DC_SSE41_FOUND=1", "-DC_SSE42_FOUND=1",
"-DENABLE_TIMESTAMPS=OFF"]
stamp = "lib-liquid"
parts = [LIQUID_TAG, arch, config, clang] + cflags + cxxflags + ldflags + opts
for path in patches + [os.path.join(shim, "complex.h")]:
with open(path, "rb") as handle:
parts.append(handle.read())
key = stamp_key(parts)
if stamp_valid(prefix, stamp, key):
log("liquid-dsp already built; skipping")
return
if os.path.exists(stamp_path(prefix, stamp)):
log("liquid-dsp flags changed; rebuilding")
ensure_ninja(dry_run)
ninja = shutil.which("ninja") or "ninja"
src = os.path.join(prefix, "src", "liquid-dsp")
git_clone(LIQUID_URL, src, dry_run, branch=LIQUID_TAG)
run(["git", "-C", src, "checkout", "--", "."], dry_run=dry_run)
run(["git", "-C", src, "apply"] + patches, dry_run=dry_run)
build = os.path.join(src, "cmake-build")
if os.path.exists(build) and not dry_run:
_rmtree(build)
env = vsdevcmd_env(arch, dry_run)
cmd = ["cmake", "-S", src, "-B", build, "-G", "Ninja",
"-DCMAKE_MAKE_PROGRAM=%s" % ninja.replace(os.sep, "/"),
"-DCMAKE_C_COMPILER=%s" % clang.replace(os.sep, "/"),
"-DCMAKE_CXX_COMPILER=%s" % clang.replace(os.sep, "/"),
"-DCMAKE_BUILD_TYPE=%s" % config,
"-DCMAKE_C_FLAGS=%s" % " ".join(cflags)]
if cxxflags:
cmd += ["-DCMAKE_CXX_FLAGS=%s" % " ".join(cxxflags)]
run(cmd + ldflags + opts, dry_run=dry_run, env=env)
run(["cmake", "--build", build, "--parallel", str(jobs)],
dry_run=dry_run, env=env)
run(["cmake", "--install", build, "--prefix", prefix], dry_run=dry_run)
if not dry_run:
dest = os.path.join(prefix, "lib")
os.makedirs(dest, exist_ok=True)
shutil.copy(clang_builtins_lib(llvm, arch),
os.path.join(dest, "clang_rt.builtins.lib"))
mark_done(prefix, stamp, dry_run, key)
def soapy_build_dir(prefix):
"""Out-of-source build directory for SoapySDR itself."""
return os.path.join(prefix, "src", "SoapySDR", "build")
def soapy_install_complete(prefix):
"""True when the SoapySDR install has a usable dev link and versioned library.
The stamp only records that an install once ran; a cached prefix can
lose the versioned dylib while keeping the dev link, which still links
but breaks fixup_bundle's @rpath resolution at packaging time.
"""
libdir = os.path.join(prefix, "lib")
if not os.path.exists(os.path.join(libdir, "libSoapySDR.dylib")):
return False
versioned = [path for path in
glob.glob(os.path.join(libdir, "libSoapySDR.*.dylib"))
if os.path.exists(path)]
return len(versioned) > 0
def build_soapy(prefix, config, jobs, arch=None, dry_run=False, env=None,
host=None):
"""Build pinned SoapySDR into the prefix; MSVC when arch is given."""
src = os.path.join(prefix, "src", "SoapySDR")
parts = macos_stamp_parts(env, host)
key = stamp_key(parts) if parts is not None else None
floor = (env or {}).get("MACOSX_DEPLOYMENT_TARGET")
if stamp_valid(prefix, "soapy", key,
adopt=not dry_run and floor is None):
if dry_run or soapy_install_complete(prefix):
log("SoapySDR already built; skipping")
return
log("SoapySDR install incomplete; rebuilding")
os.remove(stamp_path(prefix, "soapy"))
elif key is not None and os.path.exists(stamp_path(prefix, "soapy")):
log("SoapySDR flags changed; rebuilding")
git_clone(SOAPY_URL, src, dry_run, branch=SOAPY_TAG)
cmd = ["cmake", "-S", src, "-B", soapy_build_dir(prefix),
"-DCMAKE_BUILD_TYPE=%s" % config,
"-DCMAKE_INSTALL_PREFIX=%s" % prefix]
if arch:
cmd += ["-G", "Visual Studio 17 2022", "-A", arch]
run(cmd, dry_run=dry_run, env=env)
if arch:
run(["cmake", "--build", soapy_build_dir(prefix), "--config", config,
"--parallel", str(jobs)], dry_run=dry_run, env=env)
run(["cmake", "--install", soapy_build_dir(prefix), "--config", config],
dry_run=dry_run, env=env)
else:
run(["cmake", "--build", soapy_build_dir(prefix), "-j", str(jobs)],
dry_run=dry_run, env=env)
run(["cmake", "--install", soapy_build_dir(prefix)], dry_run=dry_run,
env=env)
mark_done(prefix, "soapy", dry_run, key)
def find_soapysdr_cmake(prefix, dry_run=False):
"""Locate the installed SoapySDRConfig.cmake for the macOS preset."""
matches = glob.glob(os.path.join(prefix, "**", "SoapySDRConfig.cmake"), recursive=True)
if matches:
return os.path.dirname(matches[0])
if dry_run:
return os.path.join(prefix, "lib", "cmake", "SoapySDR")
sys.exit("error: SoapySDRConfig.cmake not found under %s" % prefix)
def fetch_hamlib_windows(prefix, arch, dry_run=False):
"""Unpack the Hamlib Windows archive and build its MSVC import library.
Also comments out the POSIX-only sys/time.h include in rig.h, which
MSVC cannot provide.
"""
if os.path.exists(stamp_path(prefix, "hamlib")):
log("hamlib already fetched; skipping")
else:
tag = "w64" if arch == "x64" else "w32"
dest = os.path.join(prefix, "src", "hamlib-%s-%s" % (tag, HAMLIB_VERSION))
if not os.path.isdir(dest):
download(HAMLIB_URL % tag, dest + ".zip", dry_run)
if not dry_run:
with zipfile.ZipFile(dest + ".zip") as archive:
archive.extractall(os.path.join(prefix, "src"))
machine = "X64" if arch == "x64" else "X86"
run([msvc_lib_exe() if not dry_run else "lib",
"/def:libhamlib-4.def", "/MACHINE:%s" % machine],
cwd=os.path.join(dest, "lib", "msvc"), dry_run=dry_run)
if not dry_run:
rig = os.path.join(dest, "include", "hamlib", "rig.h")
with open(rig) as handle:
text = handle.read()
with open(rig, "w") as handle:
handle.write(text.replace("#include <sys/time.h>", "// # include <sys/time.h>"))
mark_done(prefix, "hamlib", dry_run)
base = os.path.join(prefix, "src", "hamlib-%s-%s" % ("w64" if arch == "x64" else "w32", HAMLIB_VERSION))
return os.path.join(base, "lib", "msvc", "libhamlib-4.lib"), os.path.join(base, "include")
def ensure_nsis(dry_run=False):
"""Make makensis available, falling back to the default install location.
The NSIS installer does not add itself to PATH, so check the standard
Program Files locations before reporting the tool missing.
"""
if shutil.which("makensis") is not None:
return
for base in (os.environ.get("ProgramFiles(x86)", ""),
os.environ.get("ProgramFiles", "")):
candidate = os.path.join(base, "NSIS")
if os.path.isfile(os.path.join(candidate, "makensis.exe")):
log("+ using NSIS at %s" % candidate)
if not dry_run:
os.environ["PATH"] = candidate + os.pathsep + os.environ.get("PATH", "")
return
if dry_run:
log("would require tool 'makensis'")
else:
sys.exit("error: required tool 'makensis' not found on PATH")
def ensure_ninja(dry_run=False):
"""Make ninja available, falling back to the Visual Studio copy.
The CMake workload ships ninja without adding it to PATH, so check
the Visual Studio install before reporting the tool missing.
"""
if shutil.which("ninja") is not None:
return
if not dry_run:
candidate = os.path.join(vs_install_root("ninja"),
"Common7", "IDE", "CommonExtensions",
"Microsoft", "CMake", "Ninja")
if os.path.isfile(os.path.join(candidate, "ninja.exe")):