-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathxtensa-build-zephyr.py
More file actions
executable file
·1381 lines (1170 loc) · 50.3 KB
/
xtensa-build-zephyr.py
File metadata and controls
executable file
·1381 lines (1170 loc) · 50.3 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
# SPDX-License-Identifier: BSD-3-Clause
# Too much noise for now, these can be re-enabled after they've been
# fixed (if that does not break `git blame` too much)
# W0311, W0312, W0603
# pylint:disable=bad-indentation
# pylint:disable=mixed-indentation
# pylint:disable=global-statement
# C0103, C0114, C0116
# pylint:disable=invalid-name
# pylint:disable=missing-module-docstring
# pylint:disable=missing-function-docstring
# Non-indentation whitespace has been removed from newer pylint. It does
# not hurt to keep them for older versions. The recommendation is to use
# a formatter like `black` instead, unfortunately this would totally
# destroy git blame, git revert, etc.
# C0326, C0330
# pylint:disable=bad-whitespace
# pylint:disable=bad-continuation
import argparse
import shlex
import subprocess
import pathlib
import errno
import platform as py_platform
import sys
import shutil
import os
import warnings
import fnmatch
import hashlib
import json
import gzip
import dataclasses
import concurrent.futures as concurrent
import tarfile
import re
from west import configuration as west_config
# anytree module is defined in Zephyr build requirements
from anytree import AnyNode, RenderTree, render
from packaging import version
# https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html#case-3-importing-from-parent-directory
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from tools.sof_ri_info import sof_ri_info
MIN_PYTHON_VERSION = 3, 8
assert sys.version_info >= MIN_PYTHON_VERSION, \
f"Python {MIN_PYTHON_VERSION} or above is required."
# Version of this script matching Major.Minor.Patch style.
VERSION = version.Version("2.0.0")
# Constant value resolves SOF_TOP directory as: "this script directory/.."
SOF_TOP = pathlib.Path(__file__).parents[1].resolve()
west_top = pathlib.Path(SOF_TOP, "..").resolve()
sof_fw_version = None
signing_key = None
if py_platform.system() == "Windows":
xtensa_tools_version_postfix = "-win32"
elif py_platform.system() == "Linux":
xtensa_tools_version_postfix = "-linux"
else:
xtensa_tools_version_postfix = "-unsupportedOS"
warnings.warn(f"Your operating system: {py_platform.system()} is not supported")
@dataclasses.dataclass
# pylint:disable=too-many-instance-attributes
class PlatformConfig:
"Product parameters"
vendor: str
PLAT_CONFIG: str
XTENSA_TOOLS_VERSION: str
XTENSA_CORE: str
DEFAULT_TOOLCHAIN_VARIANT: str = "xt-clang"
RIMAGE_KEY: pathlib.Path = pathlib.Path(SOF_TOP, "keys", "otc_private_key_3k.pem")
aliases: list = dataclasses.field(default_factory=list)
ipc4: bool = False
# These cannot be built by everyone out of the box yet.
# For instance: there's no open-source toolchain available for them yet.
extra_platform_configs = {
"wcl-sim" : PlatformConfig(
"intel", "intel_adsp/ace30/wcl/sim",
f"RI-2022.10{xtensa_tools_version_postfix}",
"ace30_LX7HiFi4_PIF",
ipc4 = True
),
"ptl-sim" : PlatformConfig(
"intel", "intel_adsp/ace30/ptl/sim",
f"RI-2022.10{xtensa_tools_version_postfix}",
"ace30_LX7HiFi4_PIF",
ipc4 = True
),
# AMD platforms
"acp_6_0" : PlatformConfig(
"amd", "acp_6_0_adsp/acp_6_0",
f"RI-2019.1{xtensa_tools_version_postfix}",
"rmb_LX7_HiFi5_PROD",
RIMAGE_KEY = "key param ignored by acp_6_0"
),
# MediaTek platforms
# (move to platform_configs_all on next Zephyr SDK release after 0.17.0)
"mt8195" : PlatformConfig(
"mtk", "mt8195/mt8195/adsp",
f"RJ-2024.3{xtensa_tools_version_postfix}",
"hifi4_8195_PROD",
),
"mt8186" : PlatformConfig(
"mtk", "mt8186/mt8186/adsp",
f"RJ-2024.3{xtensa_tools_version_postfix}",
"hifi5_7stg_I64D128",
),
"mt8188" : PlatformConfig(
"mtk", "mt8188/mt8188/adsp",
f"RJ-2024.3{xtensa_tools_version_postfix}",
"hifi5_7stg_I64D128",
),
"mt8196" : PlatformConfig(
"mtk", "mt8196/mt8196/adsp",
f"RJ-2024.3{xtensa_tools_version_postfix}",
"HiFi5_MPU_lock_2023_11",
),
"mt8365" : PlatformConfig(
"mtk", "mt8365/mt8365/adsp",
f"RJ-2024.3{xtensa_tools_version_postfix}",
"hifi4_Aquila_E2_PROD",
),
}
# These can all be built out of the box. --all builds all these.
# Some of these values are duplicated in sof/scripts/set_xtensa_param.sh: keep them in sync.
platform_configs_all = {
# Intel platforms
"tgl" : PlatformConfig(
"intel", "intel_adsp/cavs25",
f"RG-2017.8{xtensa_tools_version_postfix}",
"cavs2x_LX6HiFi3_2017_8",
"xcc",
aliases = ['adl', 'adl-n', 'rpl'],
ipc4 = True
),
"tgl-h" : PlatformConfig(
"intel", "intel_adsp/cavs25/tgph",
f"RG-2017.8{xtensa_tools_version_postfix}",
"cavs2x_LX6HiFi3_2017_8",
"xcc",
aliases = ['adl-s', 'rpl-s'],
ipc4 = True
),
"mtl" : PlatformConfig(
"intel", "intel_adsp/ace15_mtpm",
f"RI-2022.10{xtensa_tools_version_postfix}",
"ace10_LX7HiFi4_2022_10",
aliases = ['arl', 'arl-s'],
ipc4 = True
),
"lnl" : PlatformConfig(
"intel", "intel_adsp/ace20_lnl",
f"RI-2022.10{xtensa_tools_version_postfix}",
"ace10_LX7HiFi4_2022_10",
ipc4 = True
),
"ptl" : PlatformConfig(
"intel", "intel_adsp/ace30/ptl",
f"RI-2022.10{xtensa_tools_version_postfix}",
"ace30_LX7HiFi4_PIF",
ipc4 = True
),
"wcl" : PlatformConfig(
"intel", "intel_adsp/ace30/wcl",
f"RI-2022.10{xtensa_tools_version_postfix}",
"ace30_LX7HiFi4_PIF",
ipc4 = True
),
"nvl" : PlatformConfig(
"intel", "intel_adsp/ace40/nvl",
f"RI-2022.10{xtensa_tools_version_postfix}",
"ace4px_HiFi5MMU_PIF_nlib",
ipc4 = True
),
"nvl-s" : PlatformConfig(
"intel", "intel_adsp/ace40/nvls",
f"RI-2022.10{xtensa_tools_version_postfix}",
"ace4px_HiFi5MMU_PIF_nlib",
ipc4 = True
),
# NXP platforms
"imx8" : PlatformConfig(
"imx", "imx8qm_mek/mimx8qm6/adsp",
f"RI-2023.11{xtensa_tools_version_postfix}",
"hifi4_nxp_v5_3_1_prod",
RIMAGE_KEY = "key param ignored by imx8",
),
"imx8x" : PlatformConfig(
"imx", "imx8qxp_mek/mimx8qx6/adsp",
f"RI-2023.11{xtensa_tools_version_postfix}",
"hifi4_nxp_v5_3_1_prod",
RIMAGE_KEY = "key param ignored by imx8x"
),
"imx8m" : PlatformConfig(
"imx", "imx8mp_evk/mimx8ml8/adsp",
f"RI-2023.11{xtensa_tools_version_postfix}",
"hifi4_mscale_v2_0_2_prod",
RIMAGE_KEY = "key param ignored by imx8m"
),
"imx8ulp" : PlatformConfig(
"imx", "imx8ulp_evk/mimx8ud7/adsp",
f"RI-2023.11{xtensa_tools_version_postfix}",
"hifi4_nxp2_s7_v2_1a_prod",
RIMAGE_KEY = "key param ignored by imx8ulp"
),
"imx95" : PlatformConfig(
"imx", "imx95_evk/mimx9596/m7/ddr",
"", "", "", ""
),
"qemu_xtensa" : PlatformConfig(
"zephyr", "qemu_xtensa/dc233c",
"", "", "zephyr"
),
"qemu_xtensa_mmu" : PlatformConfig(
"zephyr", "qemu_xtensa/dc233c/mmu",
"", "", "zephyr"
),
"native_sim" : PlatformConfig(
"zephyr", "native_sim",
"", "", "zephyr"
),
}
platform_configs = platform_configs_all.copy()
platform_configs.update(extra_platform_configs)
class validate_platforms_arguments(argparse.Action):
"""Validates positional platform arguments whether provided platform name is supported."""
def __call__(self, parser, namespace, values, option_string=None):
if values:
for value in values:
if value not in platform_configs:
raise argparse.ArgumentError(self, f"Unsupported platform: {value}")
setattr(namespace, "platforms", values)
args = None
def parse_args():
global args
global west_top
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
epilog=(
f"""This script supports XtensaTools but only when installed in a specific directory
structure where a single --xtensa-core is registered per --xtensa-system registry, example
below. Different layouts may or may not work.
myXtensa/
└── install/
├── builds/
│ ├── RD-2012.5{xtensa_tools_version_postfix}
│ │ └── Intel_HiFiEP/config/ # XTENSA_SYSTEM registry
│ │ ├── default-params
│ │ └── Intel_HiFiEP-params # single, default XTENSA_CORE per registry
│ └── RG-2017.8{xtensa_tools_version_postfix}
│ ├── LX4_langwell_audio_17_8/config/
│ │ ├── default-params
│ │ └── LX4_langwell_audio_17_8-params
│ └── X4H3I16w2D48w3a_2017_8/config/
│ ├── default-params
│ └── X4H3I16w2D48w3a_2017_8-params
└── tools/
├── RD-2012.5{xtensa_tools_version_postfix}
│ └── XtensaTools
└── RG-2017.8{xtensa_tools_version_postfix}
└── XtensaTools/
$XTENSA_TOOLS_ROOT=/path/to/myXtensa ...
Supported platforms: {list(platform_configs)}"""
), add_help=False)
parser.add_argument('-h', '--help', action='store_true', help='show help')
parser.add_argument("-a", "--all", required=False, action="store_true",
help="Build all currently supported platforms")
parser.add_argument("platforms", nargs="*", action=validate_platforms_arguments,
help="List of platforms to build. Zero platform + any flag (-p) still creates 'tools/'")
parser.add_argument("-d", "--debug", required=False, action="store_true",
help="Shortcut for: -o sof/app/debug_overlay.conf")
# NO SOF release will ever user the option --fw-naming.
# This option is only for disguising SOF IPC4 as CAVS IPC4 and only in cases where
# the kernel 'ipc_type' expects CAVS IPC4. In this way, developers and CI can test
# IPC4 on older platforms.
parser.add_argument("--fw-naming", required=False, choices=["AVS", "SOF"],
default="SOF",
help="""Determine firmware naming conversion and folder structure.
See also the newer and better '--deployable-build' for IPC4.
With "SOF" (default):
build-sof-staging (for /lib/firmware/intel/sof/)
├───community
│ └── sof-tgl.ri
├── dbgkey
│ └── sof-tgl.ri
├── sof-tgl.ri
└── sof-tgl.ldc
With "AVS"; filename 'dsp_basefw.bin'.
'AVS' automatically enables --use-platform-subdir which uses one subdirectory for each platform:
build-sof-staging (for old, developer /lib/firmware/intel/sof-ipc4/)
└── tgl
├── community
│ └── dsp_basefw.bin
├── dbgkey
│ └── dsp_basefw.bin
├── dsp_basefw.bin
└── sof-tgl.ldc
""")
parser.add_argument("-j", "--jobs", required=False, type=int,
help="Number of concurrent jobs. Passed to west build and"
" to cmake (for rimage)")
parser.add_argument("-k", "--key", type=pathlib.Path, required=False,
help="Path to a non-default rimage signing key.")
# https://docs.zephyrproject.org/latest/build/kconfig/setting.html#initial-conf
# https://docs.zephyrproject.org/latest/develop/application/index.html#important-build-system-variables
# TODO, support "snippets"?
# https://docs.zephyrproject.org/latest/build/snippets/writing.html
parser.add_argument("-o", "--overlay", type=pathlib.Path, required=False, action='append',
default=[], help=
"""All '-o arg1 -o arg2 ...' arguments are combined into a single -DEXTRA_CONF_FILE='arg1;arg2;...'
list. This tweaks the build CONFIGuration like Device Tree "overlays" and Zephyr snippets do.
Files latter in the list seem to have precedence. Direct -C=-DCONFIG_xxx=.. options seem to
have precedence over -DEXTRA_CONF_FILE=... Rely on precedence as little as possible.""")
parser.add_argument("-p", "--pristine", required=False, action="store_true",
help="Perform pristine build removing build directory.")
parser.add_argument("-u", "--update", required=False, action="store_true",
help="""Runs west update command - clones SOF dependencies. Downloads next to this sof clone a new Zephyr
project with its required dependencies. Creates a modules/audio/sof symbolic link pointing
back at this sof clone. All projects are checkout out to
revision defined in manifests of SOF and Zephyr.""")
parser.add_argument('-v', '--verbose', default=0, action='count',
help="""Verbosity level. Repetition of the flag increases verbosity.
The same number of '-v' is passed to "west".
""",
)
# Cannot use a standard -- delimiter because argparse deletes it.
parser.add_argument("-C", "--cmake-args", action='append', default=[],
help="""Cmake arguments passed as is to cmake configure step.
Can be passed multiple times; whitespace is preserved Example:
-C=--warn-uninitialized -C '-DEXTRA_FLAGS=-Werror -g0'
Note '-C --warn-uninitialized' is not supported by argparse, an equal
sign must be used (https://bugs.python.org/issue9334)""",
)
parser.add_argument("--key-type-subdir", default="community",
choices=["community", "none", "dbgkey"],
help="""Output subdirectory for rimage signing key type.
Default key type subdirectory is \"community\".""")
parser.add_argument("--use-platform-subdir", default = False,
action="store_true",
help="""Use an output subdirectory for each platform.
Otherwise, all firmware files are installed in the same staging directory by default.""")
parser.add_argument("--no-interactive", default=False, action="store_true",
help="""Run script in non-interactive mode when user input can not be provided.
This should be used with programmatic script invocations (eg. Continuous Integration).
""")
deploy_args = parser.add_mutually_exclusive_group()
# argparse.BooleanOptionalAction requires Python 3.9
parser.set_defaults(deployable_build=True)
deploy_args.add_argument("--no-deployable-build", dest='deployable_build', action='store_false')
deploy_args.add_argument("--deployable-build", dest='deployable_build', action='store_true',
help="""Create a directory structure for the firmware files which can be deployed on target as it is.
This option will cause the --fw-naming and --use-platform-subdir options to be ignored!
The generic, default directory and file structure is IPC version dependent:
IPC3
build-sof-staging/sof/VENDOR/sof/ (on target: /lib/firmware/VENDOR/sof/)
├── community
│ └── sof-PLAT.ri
├── dbgkey
│ └── sof-PLAT.ri
└── sof-PLAT.ri
IPC4
build-sof-staging/sof/VENDOR/sof-ipc4/ (on target: /lib/firmware/VENDOR/sof-ipc4/)
└── PLAT
├── community
│ └── sof-PLAT.ri
├── dbgkey
│ └── sof-PLAT.ri
└── sof-PLAT.ri\n
""")
parser.add_argument("--no-tarball", default=False, action='store_true',
help="""Do not create a tar file of the installed firmware files under build-sof-staging.""")
parser.add_argument("--version", required=False, action="store_true",
help="Prints version of this script.")
parser.add_argument("-m", "--menuconfig", required=False, action="store_true",
help="Build menuconfig for target")
parser.add_argument("-z", "--zephyrsdk", required=False, action="store_true",
help="Force Build using Zephyr SDK for target")
args = parser.parse_args()
# This does NOT include "extra", experimental platforms.
if args.all:
args.platforms = list(platform_configs_all)
# print help message if no arguments provided
if len(sys.argv) == 1 or args.help:
for platform in platform_configs:
if platform_configs[platform].aliases:
parser.epilog += "\nPlatform aliases for '" + platform + "':\t"
parser.epilog += f"{list(platform_configs[platform].aliases)}"
parser.print_help()
sys.exit(0)
if args.deployable_build:
if args.fw_naming == 'AVS' or args.use_platform_subdir:
sys.exit("Options '--fw-naming=AVS' and '--use-platform-subdir'"
" are incompatible with deployable builds, try --no-deployable-build?")
if args.fw_naming == 'AVS':
if not args.use_platform_subdir:
args.use_platform_subdir=True
warnings.warn("The option '--fw-naming AVS' has to be used with '--use-platform-subdir'. Enable '--use-platform-subdir' automatically.")
def execute_command(*run_args, **run_kwargs):
"""[summary] Provides wrapper for subprocess.run that prints
command executed when 'more verbose' verbosity level is set."""
command_args = run_args[0]
# If you really need the shell in some non-portable section then
# invoke subprocess.run() directly.
if run_kwargs.get('shell') or not isinstance(command_args, list):
raise RuntimeError("Do not rely on non-portable shell parsing")
if args.verbose >= 0:
cwd = run_kwargs.get('cwd')
print_cwd = f"In dir: {cwd}" if cwd else f"in current dir: {os.getcwd()}"
print_args = shlex.join(command_args)
output = f"{print_cwd}; running command:\n {print_args}"
env_arg = run_kwargs.get('env')
env_change = set(env_arg.items()) - set(os.environ.items()) if env_arg else None
if env_change and (run_kwargs.get('sof_log_env') or args.verbose >= 1):
output += "\n... with extra/modified environment:\n"
for k_v in env_change:
output += f"{k_v[0]}={k_v[1]}\n"
print(output, flush=True)
run_kwargs = {k: run_kwargs[k] for k in run_kwargs if not k.startswith("sof_")}
if not 'check' in run_kwargs:
run_kwargs['check'] = True
#pylint:disable=subprocess-run-check
return subprocess.run(*run_args, **run_kwargs)
def symlink_or_copy(targetdir, targetfile, linkdir, linkfile):
"""Create a relative symbolic link or copy. Don't bother Windows users
with symbolic links because they require special privileges.
Windows don't care about /lib/firmware/sof/ anyway.
Make a copy instead to preserve cross-platform consistency.
"""
target = pathlib.Path(targetdir) / targetfile
link = pathlib.Path(linkdir) / linkfile
if py_platform.system() == "Windows":
shutil.copy2(target, link)
else:
link.symlink_to(os.path.relpath(target, linkdir))
def show_installed_files():
"""[summary] Scans output directory building binary tree from files and folders
then presents them in similar way to linux tree command."""
graph_root = AnyNode(name=STAGING_DIR.name, long_name=".", parent=None)
relative_entries = [
entry.relative_to(STAGING_DIR) for entry in sorted(STAGING_DIR.glob("**/*"))
]
nodes = [ graph_root ]
for entry in relative_entries:
# Node's documentation does allow random attributes
# pylint: disable=no-member
# sorted() makes sure our parent is already there.
# This is slightly awkward, a recursive function would be more readable
matches = [node for node in nodes if node.long_name == str(entry.parent)]
assert len(matches) == 1, f'"{entry}" does not have exactly one parent'
nodes.append(AnyNode(name=entry.name, long_name=str(entry), parent=matches[0]))
for pre, _, node in RenderTree(graph_root, render.AsciiStyle):
fpath = STAGING_DIR / node.long_name
# pathLib.readlink() requires Python 3.9
symlink_trailer = f" -> {os.readlink(fpath)}" if fpath.is_symlink() else ""
stem = node.name[:-3] if node.name.endswith(".gz") else node.name
shasum_trailer = ""
if checksum_wanted(stem) and fpath.is_file() and not fpath.is_symlink():
shasum_trailer = "\tsha256=" + checksum(fpath)
print(f"{pre}{node.name} {symlink_trailer} {shasum_trailer}")
# TODO: among other things in this file it should be less SOF-specific;
# try to move as much as possible to generic Zephyr code. See
# discussions in https://github.com/zephyrproject-rtos/zephyr/pull/51954
def checksum_wanted(stem):
for pattern in CHECKSUM_WANTED:
if fnmatch.fnmatch(stem, pattern):
return True
return False
def checksum(fpath):
if fpath.suffix == ".gz":
inputf = gzip.GzipFile(fpath, "rb")
else:
inputf = open(fpath, "rb")
chksum = hashlib.sha256(inputf.read()).hexdigest()
inputf.close()
return chksum
def check_west_installation():
west_path = shutil.which("west")
if not west_path:
raise FileNotFoundError("Install west and a west toolchain,"
"https://docs.zephyrproject.org/latest/getting_started/index.html")
print(f"Found west: {west_path}")
def west_reinitialize(west_root_dir: pathlib.Path, west_manifest_path: pathlib.Path):
"""[summary] Performs west reinitialization to SOF manifest file asking user for permission.
Prints error message if script is running in non-interactive mode.
:param west_root_dir: directory where is initialized.
:type west_root_dir: pathlib.Path
:param west_manifest_path: manifest file to which west is initialized.
:type west_manifest_path: pathlib.Path
:raises RuntimeError: Raised when west is initialized to wrong manifest file
(not SOFs manifest) and script is running in non-interactive mode.
"""
global west_top
message = "West is initialized to manifest other than SOFs!\n"
message += f"Initialized to manifest: {west_manifest_path}." + "\n"
dot_west_directory = pathlib.Path(west_root_dir.resolve(), ".west")
if args.no_interactive:
message += f"Try deleting {dot_west_directory } directory and rerun this script."
raise RuntimeError(message)
question = message + "Reinitialize west to SOF manifest? [Y/n] "
print(f"{question}")
while True:
reinitialize_answer = input().lower()
if reinitialize_answer in ["y", "n"]:
break
sys.stdout.write('Please respond with \'Y\' or \'n\'.\n')
if reinitialize_answer != 'y':
sys.exit("Can not proceed. Reinitialize your west manifest to SOF and rerun this script.")
shutil.rmtree(dot_west_directory)
execute_command(["west", "init", "-l", f"{SOF_TOP}"], cwd=west_top)
# TODO: use west APIs directly instead of all these indirect subprocess.run("west", ...) processes
def west_init_if_needed():
"""[summary] Validates whether west workspace had been initialized and points to SOF manifest.
Peforms west initialization if needed.
"""
global west_top, SOF_TOP
west_manifest_path = pathlib.Path(SOF_TOP, "west.yml")
result_rootdir = execute_command(["west", "topdir"], capture_output=True, cwd=west_top,
timeout=10, check=False)
if result_rootdir.returncode != 0:
execute_command(["west", "init", "-l", f"{SOF_TOP}"], cwd=west_top)
return
west_root_dir = pathlib.Path(result_rootdir.stdout.decode().rstrip()).resolve()
result_manifest_dir = execute_command(["west", "config", "manifest.path"], capture_output=True,
cwd=west_top, timeout=10, check=True)
west_manifest_dir = pathlib.Path(west_root_dir, result_manifest_dir.stdout.decode().rstrip()).resolve()
manifest_file_result = execute_command(["west", "config", "manifest.file"], capture_output=True,
cwd=west_top, timeout=10, check=True)
returned_manifest_path = pathlib.Path(west_manifest_dir, manifest_file_result.stdout.decode().rstrip())
if not returned_manifest_path.samefile(west_manifest_path):
west_reinitialize(west_root_dir, returned_manifest_path)
else:
print(f"West workspace: {west_root_dir}")
print(f"West manifest path: {west_manifest_path}")
def create_zephyr_directory():
global west_top
# Do not fail when there's only an empty directory left over
# (because of some early interruption of this script or proxy
# misconfiguration, etc.)
try:
# rmdir() is safe: it deletes empty directories ONLY.
west_top.rmdir()
except OSError as oserr:
if oserr.errno not in [errno.ENOTEMPTY, errno.ENOENT]:
raise oserr
# else when not empty then let the next line fail with a
# _better_ error message:
# "zephyrproject already exists"
west_top.mkdir(parents=False, exist_ok=False)
west_top = west_top.resolve(strict=True)
def create_zephyr_sof_symlink():
global west_top, SOF_TOP
if not west_top.exists():
raise FileNotFoundError("No west top: {}".format(west_top))
audio_modules_dir = pathlib.Path(west_top, "modules", "audio")
audio_modules_dir.mkdir(parents=True, exist_ok=True)
sof_symlink = pathlib.Path(audio_modules_dir, "sof")
# Symlinks creation requires administrative privileges in Windows or special user rights
try:
if not sof_symlink.exists():
sof_symlink.symlink_to(SOF_TOP, target_is_directory=True)
except:
print(f"Failed to create symbolic link: {sof_symlink} to {SOF_TOP}."
"\nIf you run script on Windows run it with administrative privileges or\n"
"grant user symlink creation rights -"
"see: https://docs.microsoft.com/en-us/windows/security/threat-protection/"
"security-policy-settings/create-symbolic-links")
raise
def west_update():
"""[summary] Clones all west manifest projects to specified revisions"""
global west_top
execute_command(["west", "update"], check=True, timeout=3000, cwd=west_top)
def get_sof_version():
"""[summary] Get version string major.minor.micro of SOF
firmware file. When building multiple platforms from the same SOF
commit, all platforms share the same version. So for the 1st platform,
extract the version information from sof/versions.json and later platforms will
reuse it.
"""
global sof_fw_version
if sof_fw_version:
return sof_fw_version
versions = {}
with open(SOF_TOP / "versions.json") as versions_file:
versions = json.load(versions_file)
# Keep this default value the same as the default SOF_MICRO in version.cmake
sof_micro = versions['SOF'].get('MICRO', "0")
sof_fw_version = (
f"{versions['SOF']['MAJOR']}.{versions['SOF']['MINOR']}.{sof_micro}"
)
return sof_fw_version
def rmtree_if_exists(directory):
"This is different from ignore_errors=False because it deletes everything or nothing"
if os.path.exists(directory):
shutil.rmtree(directory)
def clean_staging(platform):
print(f"Cleaning {platform} from {STAGING_DIR}")
rmtree_if_exists(STAGING_DIR / "sof-info" / platform)
sof_output_dir = STAGING_DIR / "sof"
# --use-platform-subdir
rmtree_if_exists(sof_output_dir / platform)
# Remaining .ri and .ldc files
for p in [ platform ] + platform_configs[platform].aliases:
for f in sof_output_dir.glob(f"**/sof-{p}.*"):
os.remove(f)
# remove IPC4 deployable build directories
if platform_configs[platform].ipc4:
# e.g.: "build-sof-staging/sof/intel/sof-ipc4/mtl/"
sof_vendor_dir = sof_output_dir / platform_configs[platform].vendor
rmtree_if_exists(sof_vendor_dir / "sof-ipc4" / platform)
rmtree_if_exists(sof_vendor_dir / "sof-ipc4-lib" / platform)
for p_alias in platform_configs[platform].aliases:
rmtree_if_exists(sof_vendor_dir / "sof-ipc4" / p_alias)
rmtree_if_exists(sof_vendor_dir / "sof-ipc4-lib" / p_alias)
RIMAGE_BUILD_DIR = west_top / "build-rimage"
RIMAGE_SOURCE_DIR = west_top / "sof" / "tools" / "rimage"
def rimage_west_configuration(platform_dict, dest_dir):
"""Configure rimage in a new file `dest_dir/westconfig.ini`, starting
from the workspace .west/config.
Returns a tuple: (west.configuration.Configuration, pathlib.Path to that new file).
"""
# This complex and painful saving and copying can be dropped and
# greatly simplified once any of these alternatives gets finally
# implemented in `west config`:
# https://github.com/zephyrproject-rtos/west/issues/429 or 849, 867,..
saved_local_var = os.environ.get('WEST_CONFIG_LOCAL')
workspace_west_config_path = os.environ.get('WEST_CONFIG_LOCAL',
str(west_top / ".west" / "config"))
platform_west_config_path = dest_dir / "westconfig.ini"
dest_dir.mkdir(parents=True, exist_ok=True)
shutil.copyfile(workspace_west_config_path, platform_west_config_path)
# Create `platform_wconfig` object pointing at our copy
os.environ['WEST_CONFIG_LOCAL'] = str(platform_west_config_path)
platform_wconfig = west_config.Configuration()
if saved_local_var is None:
del os.environ['WEST_CONFIG_LOCAL']
else:
os.environ['WEST_CONFIG_LOCAL'] = saved_local_var
# By default, run rimage directly from the rimage build directory
if platform_wconfig.get("rimage.path") is None:
rimage_executable = shutil.which("rimage", path=RIMAGE_BUILD_DIR)
assert pathlib.Path(str(rimage_executable)).exists()
platform_wconfig.set("rimage.path", shlex.quote(rimage_executable),
west_config.ConfigFile.LOCAL)
_ws_args = platform_wconfig.get("rimage.extra-args")
workspace_extra_args = [] if _ws_args is None else shlex.split(_ws_args)
# Flatten default rimage options while giving precedence to the workspace =
# the user input. We could just append and leave duplicates but that would be
# at best confusing and at worst relying on undocumented rimage precedence.
extra_args = []
for default_opt in rimage_options(platform_dict):
if not default_opt[0] in workspace_extra_args:
extra_args += default_opt
extra_args += workspace_extra_args
platform_wconfig.set("rimage.extra-args", shlex.join(extra_args))
return platform_wconfig, platform_west_config_path
def build_rimage():
old_rimage_loc = SOF_TOP / "rimage"
# Don't warn on empty directories
if ( old_rimage_loc / "CMakeLists.txt" ).exists():
warnings.warn(f"""{old_rimage_loc} is now ignored,
new location is {RIMAGE_SOURCE_DIR}"""
)
rimage_dir_name = RIMAGE_BUILD_DIR.name
# CMake build rimage module
if not (RIMAGE_BUILD_DIR / "CMakeCache.txt").is_file():
execute_command(["cmake", "-B", rimage_dir_name, "-G", "Ninja",
"-S", str(RIMAGE_SOURCE_DIR)],
cwd=west_top)
rimage_build_cmd = ["cmake", "--build", rimage_dir_name]
if args.jobs is not None:
rimage_build_cmd.append(f"-j{args.jobs}")
if args.verbose > 1:
rimage_build_cmd.append("-v")
execute_command(rimage_build_cmd, cwd=west_top)
def rimage_options(platform_dict):
"""Return a list of default rimage options as a list of tuples,
example: [ (-f, 2.5.0), (-b, 1), (-k, key.pem),... ]
"""
global signing_key
opts = []
if args.verbose > 0:
opts.append(("-v",) * args.verbose)
if args.key:
key_path = pathlib.Path(args.key)
assert key_path.exists(), f"{key_path} not found"
signing_key = key_path.resolve()
elif "RIMAGE_KEY" in platform_dict:
signing_key = platform_dict["RIMAGE_KEY"]
if signing_key is not None:
opts.append(("-k", str(signing_key)))
sof_fw_vers = get_sof_version()
opts.append(("-f", sof_fw_vers))
# Default value is 0 in rimage but for Zephyr the "build counter" has always
# been hardcoded to 1 in CMake and there is even a (broken) test that fails
# when it's not hardcoded to 1.
# FIXME: drop this line once the following test is fixed
# tests/avs/fw_00_basic/test_01_load_fw_extended.py::TestLoadFwExtended::()::
# test_00_01_load_fw_and_check_version
opts.append(("-b", "1"))
return opts
STAGING_DIR = None
def build_platforms():
global west_top, SOF_TOP
print(f"SOF_TOP={SOF_TOP}")
print(f"west_top={west_top}")
global STAGING_DIR
STAGING_DIR = pathlib.Path(west_top, "build-sof-staging")
# Don't leave the install of an old build behind
if args.pristine:
rmtree_if_exists(STAGING_DIR)
else:
# This is important in (at least) two use cases:
# - when switching `--use-platform-subdir` on/off or changing key subdir,
# - when the build starts failing after a code change.
# Do not delete platforms that were not requested so this script can be
# invoked once per platform.
for platform in args.platforms:
clean_staging(platform)
rmtree_if_exists(STAGING_DIR / "tools")
# smex does not use 'install -D'
sof_output_dir = pathlib.Path(STAGING_DIR, "sof")
sof_output_dir.mkdir(parents=True, exist_ok=True)
platform_build_dir_name = None
for platform in args.platforms:
platf_build_environ = os.environ.copy()
if args.deployable_build:
vendor_output_dir = pathlib.Path(sof_output_dir, platform_configs[platform].vendor)
if platform_configs[platform].ipc4:
sof_platform_output_dir = pathlib.Path(vendor_output_dir, "sof-ipc4")
else:
sof_platform_output_dir = pathlib.Path(vendor_output_dir, "sof")
sof_platform_output_dir.mkdir(parents=True, exist_ok=True)
else:
if args.use_platform_subdir:
sof_platform_output_dir = pathlib.Path(sof_output_dir, platform)
sof_platform_output_dir.mkdir(parents=True, exist_ok=True)
else:
sof_platform_output_dir = sof_output_dir
# For now convert the new dataclass to what it used to be
_dict = dataclasses.asdict(platform_configs[platform])
platform_dict = { k:v for (k,v) in _dict.items() if _dict[k] is not None }
if args.zephyrsdk:
print("Using Zephyr SDK for building")
xtensa_tools_root_dir = None
else:
print("Using Xtensa tools for building")
xtensa_tools_root_dir = os.getenv("XTENSA_TOOLS_ROOT")
# when XTENSA_TOOLS_ROOT environmental variable is set,
# use user installed Xtensa tools not Zephyr SDK
if "XTENSA_TOOLS_VERSION" in platform_dict and xtensa_tools_root_dir:
xtensa_tools_root_dir = pathlib.Path(xtensa_tools_root_dir)
if not xtensa_tools_root_dir.is_dir():
raise RuntimeError(f"Platform {platform} uses Xtensa toolchain."
f"\nVariable XTENSA_TOOLS_ROOT={xtensa_tools_root_dir} points "
"to a path that does not exist or is not a directory")
# Set environment variables expected by
# zephyr/cmake/toolchain/x*/*.cmake. Note CMake cannot set (evil)
# build-time environment variables at configure time:
# https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-can-i-get-or-set-environment-variables
# Top-level "switch". When undefined, the Zephyr build system
# automatically searches for the Zephyr SDK and ignores all the
# following variables.
platf_build_environ["ZEPHYR_TOOLCHAIN_VARIANT"] = platf_build_environ.get("ZEPHYR_TOOLCHAIN_VARIANT",
platform_dict["DEFAULT_TOOLCHAIN_VARIANT"])
platf_build_environ["XTENSA_TOOLCHAIN_PATH"] = str(
xtensa_tools_root_dir / "install" / "tools"
)
# Toolchain sub-directory
TOOLCHAIN_VER = platform_dict["XTENSA_TOOLS_VERSION"]
platf_build_environ["TOOLCHAIN_VER"] = TOOLCHAIN_VER
# This XTENSA_SYSTEM variable was copied as is from XTOS
# scripts/xtensa-build-all.sh but it is not required by (recent?)
# toolchains when installed exactly as described in the --help (= a
# single --xtensa-core installed per --xtensa-system registry and maybe
# other constraints). Let's keep exporting it in case some build
# systems have their toolchains installed differently which might
# require it. Note: "not needed" is different from "ignored"! The
# compiler fails when the value is incorrect.
builds_toolchainver = xtensa_tools_root_dir / "install" / "builds" / TOOLCHAIN_VER
XTENSA_SYSTEM = str(builds_toolchainver / platform_dict["XTENSA_CORE"] / "config")
platf_build_environ["XTENSA_SYSTEM"] = XTENSA_SYSTEM
platform_build_dir_name = f"build-{platform}"
PLAT_CONFIG = platform_dict["PLAT_CONFIG"]
build_cmd = ["west"]
build_cmd += ["-v"] * args.verbose
if args.menuconfig:
build_cmd += ["build", "-t", "menuconfig", "--build-dir", platform_build_dir_name]
else:
build_cmd += ["build", "--build-dir", platform_build_dir_name]
source_dir = pathlib.Path(SOF_TOP, "app")
build_cmd += ["--board", PLAT_CONFIG, str(source_dir)]
if args.pristine:
build_cmd += ["-p", "always"]
if args.jobs is not None:
build_cmd += [f"--build-opt=-j{args.jobs}"]
build_cmd.append('--')
if args.cmake_args:
build_cmd += args.cmake_args
extra_conf_files = [str(item.resolve(True)) for item in args.overlay]
# The '-d' option is a shortcut for '-o path_to_debug_overlay', we are good
# if both are provided, because it's no harm to merge the same overlay twice.
if args.debug:
extra_conf_files.append(str(pathlib.Path(SOF_TOP, "app", "debug_overlay.conf")))
# The xt-cland Cadence toolchain currently cannot link shared
# libraries for Xtensa. Therefore when it's used we switch to
# building relocatable ELF objects.
if platf_build_environ.get("ZEPHYR_TOOLCHAIN_VARIANT") == 'xt-clang':
extra_conf_files.append(str(pathlib.Path(SOF_TOP, "app", "llext_relocatable.conf")))
if extra_conf_files:
extra_conf_files = ";".join(extra_conf_files)
build_cmd.append(f"-DEXTRA_CONF_FILE={extra_conf_files}")
abs_build_dir = pathlib.Path(west_top, platform_build_dir_name)
# Longer story in https://github.com/zephyrproject-rtos/zephyr/pull/56671
if not args.pristine and (
pathlib.Path(abs_build_dir, "build.ninja").is_file()
or pathlib.Path(abs_build_dir, "Makefile").is_file()
):
if args.cmake_args or extra_conf_files:
warnings.warn("""CMake args slow down incremental builds.
Passing CMake parameters and -o conf files on the command line slows down incremental builds
see https://docs.zephyrproject.org/latest/guides/west/build-flash-debug.html#one-time-cmake-arguments
Try "west config build.cmake-args -- ..." instead.""")
platform_wcfg, wcfg_path = rimage_west_configuration(
platform_dict,
STAGING_DIR / "sof-info" / platform
)
platf_build_environ['WEST_CONFIG_LOCAL'] = str(wcfg_path)
# Make sure the build logs don't leave anything hidden
execute_command(['west', 'config', '-l'], cwd=west_top,
env=platf_build_environ, sof_log_env=True)
print()
# Build
try:
execute_command(build_cmd, cwd=west_top, env=platf_build_environ, sof_log_env=True)
except subprocess.CalledProcessError as cpe:
zephyr_path = pathlib.Path(west_top, "zephyr")
if not os.path.exists(zephyr_path):
sys.exit("Zephyr project not found. Please run this script with -u flag or `west update zephyr` manually.")
else: # unknown failure
raise cpe
# no output to display if building menuconfig
if args.menuconfig:
return
# reproducible-zephyr.ri is less useful now that show_installed_files() shows
# checksums too. However: - it's still useful when only the .ri file is
# available (no build logs for the other image), - it makes sure sof_ri_info.py
# itself does not bitrot, - it can catch rimage issues, see for instance rimage
# fix 4fb9fe00575b
if platform not in RI_INFO_UNSUPPORTED:
reproducible_checksum(platform, west_top / platform_build_dir_name / "zephyr" / "zephyr.ri")
install_platform(platform, sof_platform_output_dir, platf_build_environ, platform_wcfg)
src_dest_list = []
tools_output_dir = pathlib.Path(STAGING_DIR, "tools")
src_dest_list += [(pathlib.Path(SOF_TOP) /
"tools" / "mtrace"/ "mtrace-reader.py",
tools_output_dir / "mtrace-reader.py")]
# Append future files to `src_dest_list` here (but prefer
# copying entire directories; more flexible)