summaryrefslogtreecommitdiffstats
path: root/cmake/QtPublicSbomHelpers.cmake
blob: 3d66e7ff783810f85cf97daffe693eecdaa1cacc (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
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
# Copyright (C) 2024 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause

# Starts repo sbom generation.
# Should be called before any targets are added to the sbom.
#
# INSTALL_PREFIX should be passed a value like CMAKE_INSTALL_PREFIX or QT_STAGING_PREFIX.
# The default value is \${CMAKE_INSTALL_PREFIX}, which is evaluated at install time, not configure
# time.
# This default value is the /preferred/ value, to ensure using cmake --install . --prefix <path>
# works correctly for lookup of installed files during SBOM generation.
#
# INSTALL_SBOM_DIR should be passed a value like CMAKE_INSTALL_DATAROOTDIR or
#   Qt's INSTALL_SBOMDIR.
# The default value is "sbom".
#
# SUPPLIER, SUPPLIER_URL, DOCUMENT_NAMESPACE, COPYRIGHTS are self-explanatory.
function(_qt_internal_sbom_begin_project)
    # Allow opt out via an internal variable. Will be used in CI for repos like qtqa.
    if(QT_INTERNAL_FORCE_NO_GENERATE_SBOM)
        set(QT_GENERATE_SBOM OFF CACHE BOOL "Generate SBOM" FORCE)
    endif()

    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    _qt_internal_sbom_setup_fake_deterministic_build()

    set(opt_args
        USE_GIT_VERSION
        __QT_INTERNAL_HANDLE_QT_REPO
    )
    set(single_args
        INSTALL_PREFIX
        INSTALL_SBOM_DIR
        LICENSE_EXPRESSION
        SUPPLIER
        SUPPLIER_URL
        DOWNLOAD_LOCATION
        DOCUMENT_NAMESPACE
        VERSION
        SBOM_PROJECT_NAME
        QT_REPO_PROJECT_NAME
        CPE
        DOCUMENT_CREATOR_TOOL
    )
    set(multi_args
        COPYRIGHTS
        LICENSE_DIR_PATHS
    )

    cmake_parse_arguments(PARSE_ARGV 0 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    if(CMAKE_VERSION LESS_EQUAL "3.19")
        if(QT_IGNORE_MIN_CMAKE_VERSION_FOR_SBOM)
            message(STATUS
                "Using CMake version older than 3.19, and QT_IGNORE_MIN_CMAKE_VERSION_FOR_SBOM was "
                "set to ON. qt_attribution.json files will not be processed.")
        else()
            message(FATAL_ERROR
                "Generating an SBOM requires CMake version 3.19 or newer. You can pass "
                "-DQT_IGNORE_MIN_CMAKE_VERSION_FOR_SBOM=ON to try to generate the SBOM anyway, "
                "but it is not officially supported, and the SBOM might be incomplete.")
        endif()
    endif()

    # The ntia-conformance-checker insists that a SPDX document contain at least one
    # relationship that DESCRIBES a package, and that the package contains the string
    # "Package-" in the spdx id. boot2qt spdx seems to contain the same.

    if(arg_SBOM_PROJECT_NAME)
        _qt_internal_sbom_set_root_project_name("${arg_SBOM_PROJECT_NAME}")
    else()
        _qt_internal_sbom_set_root_project_name("${PROJECT_NAME}")
    endif()

    if(arg_QT_REPO_PROJECT_NAME)
        _qt_internal_sbom_set_qt_repo_project_name("${arg_QT_REPO_PROJECT_NAME}")
    else()
        _qt_internal_sbom_set_qt_repo_project_name("${PROJECT_NAME}")
    endif()

    _qt_internal_sbom_get_root_project_name_for_spdx_id(repo_project_name_for_spdx_id)
    _qt_internal_sbom_get_root_project_name_lower_case(repo_project_name_lowercase)

    set(begin_project_generate_args_spdx "")
    set(begin_project_generate_args_cydx "")

    if(arg_SUPPLIER_URL)
        set(repo_supplier_url "${arg_SUPPLIER_URL}")
    elseif(arg___QT_INTERNAL_HANDLE_QT_REPO)
        _qt_internal_sbom_get_default_supplier_url(repo_supplier_url)
    endif()
    if(repo_supplier_url)
        list(APPEND begin_project_generate_args_spdx SUPPLIER_URL "${repo_supplier_url}")
        list(APPEND begin_project_generate_args_cydx SUPPLIER_URL "${repo_supplier_url}")
    endif()

    set(sbom_project_version_args "")
    _qt_internal_forward_function_args(
        FORWARD_APPEND
        FORWARD_PREFIX arg
        FORWARD_OUT_VAR sbom_project_version_args
        FORWARD_OPTIONS
            USE_GIT_VERSION
        FORWARD_SINGLE
            VERSION
    )
    _qt_internal_handle_sbom_project_version(${sbom_project_version_args})

    if(arg_DOCUMENT_NAMESPACE)
        set(repo_spdx_namespace "${arg_DOCUMENT_NAMESPACE}")
    else()
        set(compute_project_namespace_args "")
        if(repo_supplier_url)
            list(APPEND compute_project_namespace_args SUPPLIER_URL "${repo_supplier_url}")
        endif()
        _qt_internal_sbom_compute_project_namespace(repo_spdx_namespace
            PROJECT_NAME "${repo_project_name_lowercase}"
            ${compute_project_namespace_args}
        )
    endif()

    if(QT_SBOM_GENERATE_CYDX_V1_6)
        _qt_internal_sbom_get_cyclone_bom_serial_number(
            SPDX_NAMESPACE "${repo_spdx_namespace}"
            OUT_VAR_UUID cyclone_dx_bom_serial_number_uuid
        )
        list(APPEND begin_project_generate_args_cydx
            BOM_SERIAL_NUMBER_UUID "${cyclone_dx_bom_serial_number_uuid}")
    endif()

    if(arg_INSTALL_SBOM_DIR)
        set(install_sbom_dir "${arg_INSTALL_SBOM_DIR}")
    elseif(INSTALL_SBOMDIR)
        set(install_sbom_dir "${INSTALL_SBOMDIR}")
    else()
        set(install_sbom_dir "sbom")
    endif()

    if(arg_INSTALL_PREFIX)
        set(install_prefix "${arg_INSTALL_PREFIX}")
    else()
        # The variable is escaped, so it is evaluated during cmake install time, so that the value
        # can be overridden with cmake --install . --prefix <path>.
        set(install_prefix "\${CMAKE_INSTALL_PREFIX}")
    endif()

    set(compute_project_file_name_args "")
    _qt_internal_sbom_get_project_explicit_version(explicit_version)
    if(explicit_version)
        list(APPEND compute_project_file_name_args VERSION_SUFFIX "${explicit_version}")
    endif()

    _qt_internal_sbom_compute_project_file_name(repo_project_file_name_spdx
        SPDX_TAG_VALUE
        PROJECT_NAME "${repo_project_name_lowercase}"
        ${compute_project_file_name_args}
    )

    _qt_internal_sbom_compute_project_file_name(repo_project_file_name_cydx
        CYCLONEDX_TOML
        PROJECT_NAME "${repo_project_name_lowercase}"
        ${compute_project_file_name_args}
    )

    _qt_internal_path_join(repo_spdx_relative_install_path_spdx
        "${install_sbom_dir}" "${repo_project_file_name_spdx}")
    _qt_internal_path_join(repo_spdx_relative_install_path_cydx
        "${install_sbom_dir}" "${repo_project_file_name_cydx}")

    # Prepend DESTDIR, to allow relocating installed sbom. Needed for CI.
    _qt_internal_path_join(repo_spdx_install_path_spdx
        "\$ENV{DESTDIR}${install_prefix}" "${repo_spdx_relative_install_path_spdx}")
    _qt_internal_path_join(repo_spdx_install_path_cydx
        "\$ENV{DESTDIR}${install_prefix}" "${repo_spdx_relative_install_path_cydx}")

    if(arg_LICENSE_EXPRESSION)
        set(repo_license "${arg_LICENSE_EXPRESSION}")
    else()
        # Default to NOASSERTION for root repo SPDX packages, because we have some repos
        # with multiple licenses and AND-ing them together will create a giant unreadable list.
        # It's better to rely on the more granular package licenses.
        set(repo_license "")
    endif()
    if(repo_license)
        list(APPEND begin_project_generate_args_spdx LICENSE "${repo_license}")
        list(APPEND begin_project_generate_args_cydx LICENSE "${repo_license}")
    endif()

    if(arg_COPYRIGHTS)
        list(JOIN arg_COPYRIGHTS "\n" arg_COPYRIGHTS)
        set(repo_copyright "<text>${arg_COPYRIGHTS}</text>")
    elseif(arg___QT_INTERNAL_HANDLE_QT_REPO)
        _qt_internal_sbom_get_default_qt_copyright_header(repo_copyright)
    endif()
    if(repo_copyright)
        list(APPEND begin_project_generate_args_spdx COPYRIGHT "${repo_copyright}")
        list(APPEND begin_project_generate_args_cydx COPYRIGHT "${repo_copyright}")
    endif()

    if(arg_SUPPLIER)
        set(repo_supplier "${arg_SUPPLIER}")
    elseif(arg___QT_INTERNAL_HANDLE_QT_REPO)
        _qt_internal_sbom_get_default_supplier(repo_supplier)
    endif()
    if(repo_supplier)
        # This must not contain spaces!
        list(APPEND begin_project_generate_args_spdx SUPPLIER "${repo_supplier}")
        list(APPEND begin_project_generate_args_cydx SUPPLIER "${repo_supplier}")
    endif()

    if(arg_CPE)
        set(qt_cpe "${arg_CPE}")
    elseif(arg___QT_INTERNAL_HANDLE_QT_REPO)
        _qt_internal_sbom_get_cpe_qt_repo(qt_cpe)
    else()
        set(qt_cpe "")
    endif()
    if(qt_cpe)
        list(APPEND begin_project_generate_args_spdx CPE "${qt_cpe}")
        list(APPEND begin_project_generate_args_cydx CPE "${qt_cpe}")
    endif()

    if(arg_DOWNLOAD_LOCATION)
        set(download_location "${arg_DOWNLOAD_LOCATION}")
    elseif(arg___QT_INTERNAL_HANDLE_QT_REPO)
        _qt_internal_sbom_get_qt_repo_source_download_location(download_location)
    endif()
    if(download_location)
        list(APPEND begin_project_generate_args_spdx DOWNLOAD_LOCATION "${download_location}")
        list(APPEND begin_project_generate_args_cydx DOWNLOAD_LOCATION "${download_location}")
    endif()

    if(arg_DOCUMENT_CREATOR_TOOL)
        list(APPEND begin_project_generate_args_spdx
            DOCUMENT_CREATOR_TOOL "${arg_DOCUMENT_CREATOR_TOOL}")
    endif()

    set(project_comment "")

    if(arg___QT_INTERNAL_HANDLE_QT_REPO)
        _qt_internal_get_configure_line(configure_line)
        if(configure_line)
            set(configure_line_comment
                "\n${repo_project_name_lowercase} was configured with:\n    ${configure_line}\n")
            string(APPEND project_comment "${configure_line_comment}")
        endif()
    endif()

    if(project_comment)
        # Escape any potential semicolons.
        string(REPLACE ";" "\\;" project_comment "${project_comment}")
        set(project_comment PROJECT_COMMENT "${project_comment}")
    endif()

    if(QT_SBOM_GENERATE_SPDX_V2)
        _qt_internal_sbom_begin_project_generate(
            OUTPUT "${repo_spdx_install_path_spdx}"
            OUTPUT_RELATIVE_PATH "${repo_spdx_relative_install_path_spdx}"
            PROJECT "${repo_project_name_lowercase}"
            ${project_comment}
            PROJECT_FOR_SPDX_ID "${repo_project_name_for_spdx_id}"
            NAMESPACE "${repo_spdx_namespace}"
            ${begin_project_generate_args_spdx}
            OUT_VAR_PROJECT_SPDX_ID repo_project_spdx_id
        )
    endif()

    if(QT_SBOM_GENERATE_CYDX_V1_6)
        _qt_internal_sbom_begin_project_generate_cyclone(
            OUTPUT "${repo_spdx_install_path_cydx}"
            OUTPUT_RELATIVE_PATH "${repo_spdx_relative_install_path_cydx}"
            PROJECT "${repo_project_name_lowercase}"
            ${project_comment}
            PROJECT_FOR_SPDX_ID "${repo_project_name_for_spdx_id}"
            NAMESPACE "${repo_spdx_namespace}"
            ${begin_project_generate_args_cydx}
            OUT_VAR_PROJECT_SPDX_ID repo_project_spdx_id
        )
    endif()

    set_property(GLOBAL PROPERTY _qt_internal_project_attribution_files "")

    set_property(GLOBAL PROPERTY _qt_internal_sbom_repo_document_namespace
        "${repo_spdx_namespace}")

    set_property(GLOBAL PROPERTY _qt_internal_sbom_repo_cyclone_dx_bom_serial_number_uuid
        "${cyclone_dx_bom_serial_number_uuid}")

    set_property(GLOBAL PROPERTY _qt_internal_sbom_relative_installed_repo_document_path
        "${repo_spdx_relative_install_path_spdx}")

    set_property(GLOBAL PROPERTY _qt_internal_sbom_repo_project_name_lowercase
        "${repo_project_name_lowercase}")

    set_property(GLOBAL PROPERTY _qt_internal_sbom_install_prefix
        "${arg_INSTALL_PREFIX}")

    set_property(GLOBAL PROPERTY _qt_internal_sbom_project_spdx_id
        "${repo_project_spdx_id}")

    # Collect project licenses.
    set(license_dirs "")

    if(arg___QT_INTERNAL_HANDLE_QT_REPO AND EXISTS "${PROJECT_SOURCE_DIR}/LICENSES")
        list(APPEND license_dirs "${PROJECT_SOURCE_DIR}/LICENSES")
    endif()

    set(license_dir_candidates "")
    if(arg_LICENSE_DIR_PATHS)
        list(APPEND license_dir_candidates ${arg_LICENSE_DIR_PATHS})
    endif()

    # Allow specifying extra license dirs via a variable. Useful for standalone projects
    # like sqldrivers.
    # Kept for backwards compatibility, the new LICENSE_DIR_PATHS option should be preferred.
    if(QT_SBOM_LICENSE_DIRS)
        list(APPEND license_dir_candidates ${QT_SBOM_LICENSE_DIRS})
    endif()

    foreach(license_dir IN LISTS license_dir_candidates)
        if(EXISTS "${license_dir}")
            list(APPEND license_dirs "${license_dir}")
        endif()
    endforeach()

    list(REMOVE_DUPLICATES license_dirs)

    set(license_file_wildcard "LicenseRef-*.txt")
    list(TRANSFORM license_dirs APPEND "/${license_file_wildcard}" OUTPUT_VARIABLE license_globs)

    file(GLOB license_files ${license_globs})

    foreach(license_file IN LISTS license_files)
        get_filename_component(license_id "${license_file}" NAME_WLE)
        _qt_internal_sbom_add_license(
            LICENSE_ID "${license_id}"
            LICENSE_PATH "${license_file}"
            NO_LICENSE_REF_PREFIX
        )
    endforeach()

    # Make sure that any system library dependencies that have been found via qt_find_package or
    # _qt_internal_find_third_party_dependencies have their spdx id registered now.
    _qt_internal_sbom_record_system_library_spdx_ids()

    set_property(GLOBAL PROPERTY _qt_internal_sbom_repo_begin_called TRUE)

    _qt_internal_sbom_setup_project_ops()
endfunction()

# Check various internal options to decide which sbom generation operations should be setup.
# Considered operations are generation of a JSON sbom, validation of the SBOM, NTIA checker, etc.
function(_qt_internal_sbom_setup_project_ops)
    set(options "")

    if(QT_SBOM_GENERATE_JSON
            OR QT_SBOM_GENERATE_SPDX_V2_JSON
            OR QT_INTERNAL_SBOM_GENERATE_JSON
            OR QT_INTERNAL_SBOM_DEFAULT_CHECKS)
        list(APPEND options GENERATE_JSON)
    endif()

    # Tring to generate the JSON might fail if the python dependencies are not available.
    # The user can explicitly request to fail the build if dependencies are not found.
    # error out. For internal options that the CI uses, we always want to fail the build if the
    # deps are not found.
    if(QT_SBOM_REQUIRE_GENERATE_JSON
            OR QT_SBOM_REQUIRE_GENERATE_SPDX_V2_JSON
            OR QT_INTERNAL_SBOM_GENERATE_JSON
            OR QT_INTERNAL_SBOM_DEFAULT_CHECKS)
        list(APPEND options GENERATE_JSON_REQUIRED)
    endif()

    if(QT_SBOM_VERIFY
            OR QT_SBOM_VERIFY_SPDX_V2
            OR QT_INTERNAL_SBOM_VERIFY
            OR QT_INTERNAL_SBOM_DEFAULT_CHECKS)
        list(APPEND options VERIFY_SBOM)
    endif()

    # Do the same requirement check for SBOM verification.
    if(QT_SBOM_REQUIRE_VERIFY
            OR QT_SBOM_REQUIRE_VERIFY_SPDX_V2
            OR QT_INTERNAL_SBOM_VERIFY
            OR QT_INTERNAL_SBOM_DEFAULT_CHECKS)
        list(APPEND options VERIFY_SBOM_REQUIRED)
    endif()

    if(QT_SBOM_GENERATE_CYDX_V1_6)
        list(APPEND options GENERATE_CYCLONE_DX_V1_6)
    endif()

    if(QT_SBOM_REQUIRE_GENERATE_CYDX_V1_6)
        list(APPEND options GENERATE_CYCLONE_DX_V1_6_REQUIRED)
    endif()

    if(QT_SBOM_VERIFY_CYDX_V1_6)
        list(APPEND options VERIFY_CYCLONE_DX_V1_6)
    endif()

    if(QT_SBOM_REQUIRE_VERIFY_CYDX_V1_6)
        list(APPEND options VERIFY_CYCLONE_DX_V1_6_REQUIRED)
    endif()

    if(QT_SBOM_VERBOSE_CYDX_V1_6)
        list(APPEND options VERBOSE_CYCLONE_DX_V1_6)
    endif()

    if(QT_SBOM_VERIFY_NTIA_COMPLIANT
            OR QT_INTERNAL_SBOM_VERIFY_NTIA_COMPLIANT OR QT_INTERNAL_SBOM_DEFAULT_CHECKS)
        list(APPEND options VERIFY_NTIA_COMPLIANT)
    endif()

    if(QT_SBOM_SHOW_TABLE OR QT_INTERNAL_SBOM_SHOW_TABLE OR QT_INTERNAL_SBOM_DEFAULT_CHECKS)
        list(APPEND options SHOW_TABLE)
    endif()

    if(QT_SBOM_AUDIT OR QT_INTERNAL_SBOM_AUDIT OR QT_INTERNAL_SBOM_AUDIT_NO_ERROR)
        list(APPEND options AUDIT)
    endif()

    if(QT_SBOM_AUDIT_NO_ERROR OR QT_INTERNAL_SBOM_AUDIT_NO_ERROR)
        list(APPEND options AUDIT_NO_ERROR)
    endif()

    if(QT_GENERATE_SOURCE_SBOM)
        list(APPEND options GENERATE_SOURCE_SBOM)
    endif()

    if(QT_LINT_SOURCE_SBOM)
        list(APPEND options LINT_SOURCE_SBOM)
    endif()

    if(QT_LINT_SOURCE_SBOM_NO_ERROR OR QT_INTERNAL_LINT_SOURCE_SBOM_NO_ERROR)
        list(APPEND options LINT_SOURCE_SBOM_NO_ERROR)
    endif()

    _qt_internal_sbom_setup_project_ops_generation(${options})
endfunction()

# Sets up SBOM generation and verification options.
#
# By default, the main toggle for SBOM generation is disabled. The GENERATE_SBOM_DEFAULT option
# overrides that value and can be set by a project that sets up SBOM generation.
#
# If the main toggle gets enabled, we enable SPDX V2.3 tag:value generation, and try to enable
# CycloneDX V1.6 generation. Try, because CDX generation needs python dependencies. If they are
# not found, the generation is silently skipped.
#
# By default SPDX v2.3 JSON generation and verification is enabled, if the python dependencies
# are found. Otherwise they will be silently skipped.
# Unless the user explicitly requests to fail the build if the dependencies are not found.
# The same can be done for CycloneDX generation.
#
# Some older variables that were added pre-CycloneDX generation are deprecated.
function(_qt_internal_setup_sbom)
    set(opt_args "")
    set(single_args
        GENERATE_SBOM_DEFAULT
    )
    set(multi_args "")

    cmake_parse_arguments(PARSE_ARGV 0 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    set(default_value "OFF")
    if(NOT "${arg_GENERATE_SBOM_DEFAULT}" STREQUAL "")
        set(default_value "${arg_GENERATE_SBOM_DEFAULT}")
    endif()

    # Main SBOM toggle. Used to be the toggle for SPDX v2.3 only, but now would also enable Cyclone
    # DX as well.
    set(sbom_help_string "Generate SBOM.")
    option(QT_GENERATE_SBOM "${sbom_help_string}" "${default_value}")


    # Toggle for SPDX V2.3 generation.
    set(spdx_v2_help_string "Generate SBOM documents in SPDX v2.3 tag:value format.")
    option(QT_SBOM_GENERATE_SPDX_V2 "${spdx_v2_help_string}" ON)


    # Toggles for CycloneDX V1.6 generation.
    set(cydx_help_string "Generate SBOM documents in CycloneDX v1.6 JSON format.")
    option(QT_SBOM_GENERATE_CYDX_V1_6 "${cydx_help_string}" ON)

    set(cydx_require_help_string
        "Error out if CycloneDX SBOM generation dependencies are not found.")
    option(QT_SBOM_REQUIRE_GENERATE_CYDX_V1_6 "${cydx_require_help_string}" OFF)


    # Options for SPDX v2.3 JSON generation and verification.

    string(CONCAT spdx_v23_json_help_string
        "Generate SBOM documents in SPDX v2.3 JSON format if required python dependency "
        "spdx-tools is available."
    )

    set(spdx_v23_json_require_help_string
        "Error out if JSON SBOM generation depdendency is not found.")

    set(spdx_v23_verify_help_string
        "Verify generated SBOM documents using python spdx-tools package.")

    set(spdx_v23_verify_require_help_string
        "Error out if SBOM verification dependencies are not found.")

    option(QT_SBOM_GENERATE_SPDX_V2_JSON "${spdx_v23_json_help_string}" ON)
    option(QT_SBOM_REQUIRE_GENERATE_SPDX_V2_JSON "${spdx_v23_json_require_help_string}" OFF)

    option(QT_SBOM_VERIFY_SPDX_V2 "${spdx_v23_verify_help_string}" ON)
    option(QT_SBOM_REQUIRE_VERIFY_SPDX_V2 "${spdx_v23_verify_require_help_string}" OFF)


    # Options for CycloneDX verification and verbosity.

    set(cydx_verify_help_string
        "Verify generated CycloneDX document against its json schema.")
    option(QT_SBOM_VERIFY_CYDX_V1_6 "${cydx_verify_help_string}" ON)

    set(cydx_verify_require_help_string
        "Error out if SBOM verification dependencies are not found.")
    option(QT_SBOM_REQUIRE_VERIFY_CYDX_V1_6 "${cydx_verify_require_help_string}" OFF)

    set(cydx_verbose_help_string
        "Enable verbose output for CycloneDX generation.")
    option(QT_SBOM_VERBOSE_CYDX_V1_6 "${cydx_verbose_help_string}" OFF)


    # Deprecated options, superseded by the options above.
    # Only add them if the values was previously defined, but update the doc string.

    if(DEFINED QT_SBOM_GENERATE_JSON)
        option(QT_SBOM_GENERATE_JSON "Deprecated: ${spdx_v23_json_help_string}" ON)
    endif()
    if(DEFINED QT_SBOM_REQUIRE_GENERATE_JSON)
        option(QT_SBOM_REQUIRE_GENERATE_JSON "Deprecated: ${spdx_v23_json_require_help_string}" OFF)
    endif()
    if(DEFINED QT_SBOM_VERIFY)
        option(QT_SBOM_VERIFY "Deprecated: ${spdx_v23_verify_help_string}" ON)
    endif()
    if(DEFINED QT_SBOM_REQUIRE_VERIFY)
        option(QT_SBOM_REQUIRE_VERIFY "Deprecated: ${spdx_v23_verify_require_help_string}" OFF)
    endif()

    # Semi-public, undocumented options to allow enabling all SBOM stuff, for easier testing.
    if(QT_SBOM_GENERATE_AND_VERIFY_ALL)
        set(QT_SBOM_GENERATE_ALL ON)
        set(QT_SBOM_GENERATE_REQUIRED_ALL ON)
        set(QT_SBOM_VERIFY_REQUIRED_ALL ON)
    endif()

    if(QT_SBOM_GENERATE_ALL)
        set(QT_GENERATE_SBOM ON CACHE BOOL "${sbom_help_string}" FORCE)
        set(QT_SBOM_GENERATE_SPDX_V2 ON CACHE BOOL "${spdx_v2_help_string}" FORCE)
        set(QT_SBOM_GENERATE_SPDX_V2_JSON ON CACHE BOOL "${spdx_v23_json_help_string}" FORCE)
        set(QT_SBOM_GENERATE_CYDX_V1_6 ON CACHE BOOL "${cydx_help_string}" FORCE)
        unset(QT_SBOM_GENERATE_ALL CACHE)
        unset(QT_SBOM_GENERATE_ALL)
    endif()

    if(QT_SBOM_GENERATE_REQUIRED_ALL)
        set(QT_SBOM_REQUIRE_GENERATE_SPDX_V2_JSON ON CACHE BOOL
            "${spdx_v23_json_require_help_string}" FORCE)
        set(QT_SBOM_REQUIRE_GENERATE_CYDX_V1_6 ON CACHE BOOL "${cydx_require_help_string}" FORCE)

        unset(QT_SBOM_GENERATE_REQUIRED_ALL CACHE)
        unset(QT_SBOM_GENERATE_REQUIRED_ALL)
    endif()

    if(QT_SBOM_VERIFY_REQUIRED_ALL)
        set(QT_SBOM_VERIFY_SPDX_V2 ON CACHE BOOL "${spdx_v23_verify_help_string}" FORCE)
        set(QT_SBOM_VERIFY_CYDX_V1_6 ON CACHE BOOL "${cydx_verify_help_string}" FORCE)

        set(QT_SBOM_REQUIRE_VERIFY_SPDX_V2 ON CACHE BOOL "${spdx_v23_verify_require_help_string}"
            FORCE)
        set(QT_SBOM_REQUIRE_VERIFY_CYDX_V1_6 ON CACHE BOOL "${cydx_verify_require_help_string}"
            FORCE)

        unset(QT_SBOM_VERIFY_ALL CACHE)
        unset(QT_SBOM_VERIFY_ALL)
    endif()

    # Various sanity checks.

    # Disable SPDX v2.3 JSON generation if tag:value generation is disabled.
    if(QT_GENERATE_SBOM
            AND QT_SBOM_GENERATE_SPDX_V2_JSON
            AND NOT QT_SBOM_GENERATE_SPDX_V2)
        if(NOT QT_NO_SBOM_INFORMATIONAL_MESSAGES)
            message(STATUS
                "Disabling SPDX v2.3 SBOM JSON generation because tag:value generation is "
                "disabled and that is a requirement for JSON generation.")
        endif()
        set(QT_SBOM_GENERATE_SPDX_V2_JSON OFF CACHE BOOL "${spdx_v23_json_help_string}" FORCE)
        set(QT_SBOM_VERIFY_SPDX_V2 OFF CACHE BOOL "${spdx_v23_verify_help_string}" FORCE)
    endif()

    # Disable CycloneDX generation if dependencies are not found and it wasn't required.
    if(QT_GENERATE_SBOM
            AND QT_SBOM_GENERATE_CYDX_V1_6
            AND NOT QT_SBOM_REQUIRE_GENERATE_CYDX_V1_6)
        _qt_internal_sbom_find_cydx_dependencies(OUT_VAR_DEPS_FOUND deps_found)
        if(NOT deps_found)
            if(NOT QT_NO_SBOM_INFORMATIONAL_MESSAGES)
                message(STATUS
                    "Disabling Cyclone DX SBOM generation because dependencies were not found, "
                    "and generation was not marked as required.")
            endif()
            set(QT_SBOM_GENERATE_CYDX_V1_6 OFF CACHE BOOL "${cydx_help_string}" FORCE)
        endif()
    endif()

    # Disable sbom generation if none of the formats are enabled. Failing to do so will cause
    # errors in _qt_internal_sbom_begin_project.
    if(QT_GENERATE_SBOM
            AND NOT QT_SBOM_GENERATE_SPDX_V2
            AND NOT QT_SBOM_GENERATE_CYDX_V1_6)
        if(NOT QT_NO_SBOM_INFORMATIONAL_MESSAGES)
            message(STATUS
                "Disabling SBOM generation because none of the supported formats were enabled.")
        endif()
        set(QT_GENERATE_SBOM OFF CACHE BOOL "${sbom_help_string}" FORCE)
    endif()
endfunction()

# Ends repo sbom project generation.
# Should be called after all relevant targets are added to the sbom.
# Handles registering sbom info for recorded system libraries and then creates the sbom build
# and install rules.
function(_qt_internal_sbom_end_project)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    # Run sbom finalization for targets that had it scheduled, but haven't run yet.
    # This can happen when _qt_internal_sbom_end_project is called within the same
    # subdirectory scope as where the targets are meant to be finalized, but that would be too late
    # and the targets wouldn't be added to the sbom.
    # This can happen in user projects, but also it happens for qtbase with the Platform targets.
    # Check the list of targets to finalize in a loop, because finalizing a target can schedule
    # finalization of a different attribution target.
    set(targets_to_finalize "")
    get_cmake_property(new_targets _qt_internal_sbom_targets_waiting_for_finalization)
    if(new_targets)
        list(APPEND targets_to_finalize ${new_targets})
    endif()

    set(finalization_iterations 0)
    while(targets_to_finalize)
        # Make sure we don't accidentally create a never-ending loop.
        math(EXPR finalization_iterations "${finalization_iterations} + 1")
        if(finalization_iterations GREATER 500)
            message(WARNING
                "SBOM warning: Too many iterations while handling finalization of SBOM targets. "
                "Possible circular dependency. "
                "Please report to https://bugreports.qt.io with details "
                "about the project and a trace log. "
                "Targets left to finalize: '${targets_to_finalize}'"
            )
            break()
        endif()

        # Clear the list.
        set_property(GLOBAL PROPERTY _qt_internal_sbom_targets_waiting_for_finalization "")

        foreach(target IN LISTS targets_to_finalize)
            _qt_internal_finalize_sbom("${target}")
        endforeach()

        # Retrieve any new targets.
        set(targets_to_finalize "")
        get_cmake_property(new_targets _qt_internal_sbom_targets_waiting_for_finalization)
        if(new_targets)
            list(APPEND targets_to_finalize ${new_targets})
        endif()
    endwhile()

    # Now that we know which system libraries are linked against because we added all
    # subdirectories and finalized all targets, we can add the recorded system libs to the sbom.
    _qt_internal_sbom_add_recorded_system_libraries()

    # Add any external target dependencies, for CycloneDX generation.
    # E.g. For QtSvg, we need to create a QtCore component in the QtSvg document, so that we
    # can declare a dependency on it.
    if(QT_SBOM_GENERATE_CYDX_V1_6)
        _qt_internal_sbom_add_cydx_external_target_dependencies()
    endif()

    if(QT_SBOM_GENERATE_SPDX_V2)
        _qt_internal_sbom_end_project_generate()
    endif()

    if(QT_SBOM_GENERATE_CYDX_V1_6)
        _qt_internal_sbom_end_project_generate_cyclone()
    endif()

    # Clean up external document ref properties, because each repo needs to start from scratch
    # in a top-level build.
    get_cmake_property(known_external_documents _qt_known_external_documents)
    set_property(GLOBAL PROPERTY _qt_known_external_documents "")
    foreach(external_document IN LISTS known_external_documents)
        set_property(GLOBAL PROPERTY _qt_known_external_documents_${external_document} "")
    endforeach()

    set_property(GLOBAL PROPERTY _qt_internal_sbom_repo_begin_called FALSE)

    # Add configure-time dependency on project attribution files.
    get_property(attribution_files GLOBAL PROPERTY _qt_internal_project_attribution_files)
    _qt_internal_append_cmake_configure_depends(${attribution_files})
endfunction()

# Automatically begins sbom generation for a qt git repo unless QT_SKIP_SBOM_AUTO_PROJECT is TRUE.
function(_qt_internal_sbom_auto_begin_qt_repo_project)
    # Allow skipping auto generation of sbom project, in case it needs to be manually adjusted with
    # extra parameters.
    if(QT_SKIP_SBOM_AUTO_PROJECT)
        return()
    endif()

    _qt_internal_sbom_begin_qt_repo_project()
endfunction()

# Sets up sbom generation for a qt git repo or qt-git-repo-sub-project (e.g. qtpdf in qtwebengine).
#
# In the case of a qt-git-repo-sub-project, the function expects the following options:
# - SBOM_PROJECT_NAME (e.g. QtPdf)
# - QT_REPO_PROJECT_NAME (e.g. QtWebEngine)
#
# Expects the following variables to always be set before the function call:
# - QT_STAGING_PREFIX
# - INSTALL_SBOMDIR
function(_qt_internal_sbom_begin_qt_repo_project)
    set(opt_args "")
    set(single_args
        SBOM_PROJECT_NAME
        QT_REPO_PROJECT_NAME
    )
    set(multi_args "")

    cmake_parse_arguments(PARSE_ARGV 0 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    set(sbom_project_args "")

    _qt_internal_forward_function_args(
        FORWARD_APPEND
        FORWARD_PREFIX arg
        FORWARD_OUT_VAR sbom_project_args
        FORWARD_OPTIONS
            ${opt_args}
        FORWARD_SINGLE
            ${single_args}
        FORWARD_MULTI
            ${multi_args}
    )

    _qt_internal_sbom_begin_project(
        INSTALL_SBOM_DIR "${INSTALL_SBOMDIR}"
        USE_GIT_VERSION
        __QT_INTERNAL_HANDLE_QT_REPO
        ${sbom_project_args}
    )
endfunction()

# Automatically ends sbom generation for a qt git repo unless QT_SKIP_SBOM_AUTO_PROJECT is TRUE.
function(_qt_internal_sbom_auto_end_qt_repo_project)
    # Allow skipping auto generation of sbom project, in case it needs to be manually adjusted with
    # extra parameters.
    if(QT_SKIP_SBOM_AUTO_PROJECT)
        return()
    endif()

    _qt_internal_sbom_end_qt_repo_project()
endfunction()

# Ends sbom generation for a qt git repo or qt-git-repo-sub-project.
function(_qt_internal_sbom_end_qt_repo_project)
    _qt_internal_sbom_end_project()
endfunction()


# Enables a fake deterministic SBOM build, for easier inter-diffs between sbom files. Useful
# for local development.
function(_qt_internal_sbom_setup_fake_deterministic_build)
    if(NOT DEFINED QT_SBOM_FAKE_DETERMINISTIC_BUILD)
        return()
    endif()

    if(QT_SBOM_FAKE_DETERMINISTIC_BUILD)
        set(value "ON")
    else()
        set(value "OFF")
    endif()

    set(QT_SBOM_FAKE_GIT_VERSION "${value}" CACHE BOOL "SBOM fake git version")
    set(QT_SBOM_FAKE_TIMESTAMP "${value}" CACHE BOOL "SBOM fake timestamp")
    set(QT_SBOM_FAKE_CHECKSUM "${value}" CACHE BOOL "SBOM fake checksums")
endfunction()

# Helper to get purl entry-specific options.
macro(_qt_internal_get_sbom_purl_parsing_options opt_args single_args multi_args)
    set(${opt_args}
        NO_DEFAULT_QT_PURL
        PURL_USE_PACKAGE_VERSION
    )
    set(${single_args}
        PURL_ID
        PURL_TYPE
        PURL_NAMESPACE
        PURL_NAME
        PURL_VERSION
        PURL_SUBPATH
        PURL_VCS_URL
    )
    set(${multi_args}
        PURL_QUALIFIERS
    )
endmacro()

# Helper to get the purl options that should be recongized by sbom functions like
# _qt_internal_sbom_add_target.
macro(_qt_internal_get_sbom_purl_add_target_options opt_args single_args multi_args)
    set(${opt_args}
        __QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_PURL
    )
    set(${single_args} "")
    set(${multi_args}
        PURLS
        PURL_VALUES
    )
endmacro()

# Helper to get purl options that should be forwarded from _qt_internal_sbom_add_target to
# _qt_internal_sbom_handle_purl_values.
macro(_qt_internal_get_sbom_purl_handling_options opt_args single_args multi_args)
    set(${opt_args} "")
    set(${single_args}
        SUPPLIER
        SBOM_ENTITY_TYPE
        PACKAGE_VERSION
    )
    set(${multi_args} "")

    _qt_internal_get_sbom_purl_add_target_options(
        purl_add_target_opt_args purl_add_target_single_args purl_add_target_multi_args)
    list(APPEND ${opt_args} ${purl_add_target_opt_args})
    list(APPEND ${single_args} ${purl_add_target_single_args})
    list(APPEND ${multi_args} ${purl_add_target_multi_args})
endmacro()

# Helper to get the options that _qt_internal_sbom_add_target understands, but that are also
# a safe subset for qt_internal_add_module, qt_internal_extend_target, etc to understand.
macro(_qt_internal_get_sbom_add_target_common_options opt_args single_args multi_args)
    set(${opt_args}
        NO_CURRENT_DIR_ATTRIBUTION
        NO_ATTRIBUTION_LICENSE_ID
        NO_DEFAULT_QT_LICENSE
        NO_DEFAULT_QT_LICENSE_ID_LIBRARIES
        NO_DEFAULT_QT_LICENSE_ID_EXECUTABLES
        NO_DEFAULT_DIRECTORY_QT_LICENSE
        NO_DEFAULT_QT_COPYRIGHTS
        NO_DEFAULT_QT_PACKAGE_VERSION
        NO_DEFAULT_QT_SUPPLIER
        SBOM_INCOMPLETE_3RD_PARTY_DEPENDENCIES
        IS_QT_3RD_PARTY_HEADER_MODULE
        USE_ATTRIBUTION_FILES
        CREATE_SBOM_FOR_EACH_ATTRIBUTION
        __QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_PACKAGE_VERSION
        __QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_SUPPLIER
        __QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_DOWNLOAD_LOCATION
        __QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_LICENSE
        __QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_COPYRIGHTS
        __QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_CPE
        __QT_INTERNAL_HANDLE_QT_ENTITY_ATTRIBUTION_FILES
    )
    set(${single_args}
        DEFAULT_SBOM_ENTITY_TYPE
        SBOM_ENTITY_TYPE
        PACKAGE_VERSION
        FRIENDLY_PACKAGE_NAME
        SUPPLIER
        CPE_VENDOR
        CPE_PRODUCT
        LICENSE_EXPRESSION
        QT_LICENSE_ID
        DOWNLOAD_LOCATION
        ATTRIBUTION_ENTRY_INDEX
        ATTRIBUTION_PARENT_TARGET
        ATTRIBUTION_PARENT_TARGET_SBOM_ENTITY_TYPE
        SBOM_PACKAGE_COMMENT
    )
    set(${multi_args}
        COPYRIGHTS
        CPE
        SBOM_DEPENDENCIES
        ATTRIBUTION_FILE_PATHS
        ATTRIBUTION_FILE_DIR_PATHS
        ATTRIBUTION_IDS
        SBOM_RELATIONSHIPS
    )

    _qt_internal_get_sbom_purl_add_target_options(
        purl_add_target_opt_args purl_add_target_single_args purl_add_target_multi_args)
    list(APPEND ${opt_args} ${purl_add_target_opt_args})
    list(APPEND ${single_args} ${purl_add_target_single_args})
    list(APPEND ${multi_args} ${purl_add_target_multi_args})
endmacro()

# Helper to get all known SBOM specific options, without the ones that qt_internal_add_module
# and similar functions understand, like LIBRARIES, INCLUDES, etc.
macro(_qt_internal_get_sbom_specific_options opt_args single_args multi_args)
    set(${opt_args} "")
    set(${single_args} "")
    set(${multi_args} "")

    _qt_internal_get_sbom_add_target_common_options(
        common_opt_args common_single_args common_multi_args)
    list(APPEND ${opt_args} ${common_opt_args})
    list(APPEND ${single_args} ${common_single_args})
    list(APPEND ${multi_args} ${common_multi_args})

    _qt_internal_sbom_get_multi_config_single_args(multi_config_single_args)
    list(APPEND ${single_args} ${multi_config_single_args})
endmacro()

# Helper to get the options that _qt_internal_sbom_add_target understands.
# Also used in qt_find_package_extend_sbom.
macro(_qt_internal_get_sbom_add_target_options opt_args single_args multi_args)
    set(${opt_args}
        NO_INSTALL
    )
    set(${single_args}
        TYPE # deprecated, use SBOM_ENTITY_TYPE or DEFAULT_SBOM_ENTITY_TYPE instead
    )
    set(${multi_args}
        LIBRARIES
        PUBLIC_LIBRARIES
    )

    _qt_internal_get_sbom_specific_options(
        specific_opt_args specific_single_args specific_multi_args)
    list(APPEND ${opt_args} ${specific_opt_args})
    list(APPEND ${single_args} ${specific_single_args})
    list(APPEND ${multi_args} ${specific_multi_args})
endmacro()

# Chooses between SBOM_ENTITY_TYPE, TYPE (deprecated) or DEFAULT_SBOM_ENTITY_TYPE and assigns it to
# out_var.
function(_qt_internal_map_sbom_entity_type out_var)
    set(opt_args "")
    set(single_args
        TYPE # deprecated
        SBOM_ENTITY_TYPE
        DEFAULT_SBOM_ENTITY_TYPE
    )
    set(multi_args "")
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")

    # Previously only TYPE existed, now there is also SBOM_ENTITY_TYPE and DEFAULT_SBOM_ENTITY_TYPE.
    # DEFAULT_SBOM_ENTITY_TYPE is the fallback value, TYPE is deprecated but left for compatibility
    # and SBOM_ENTITY_TYPE has the highest priority.
    if(arg_SBOM_ENTITY_TYPE)
        set(sbom_entity_type "${arg_SBOM_ENTITY_TYPE}")
    elseif(arg_TYPE)
        # deprecated code path
        if(QT_WARN_DEPRECATED_SBOM_TYPE_OPTION)
            message(DEPRECATION "TYPE option is deprecated, use SBOM_ENTITY_TYPE instead.")
        endif()
        set(sbom_entity_type "${arg_TYPE}")
    elseif(arg_DEFAULT_SBOM_ENTITY_TYPE)
        set(sbom_entity_type "${arg_DEFAULT_SBOM_ENTITY_TYPE}")
    else()
        set(sbom_entity_type "")
    endif()

    set(${out_var} "${sbom_entity_type}" PARENT_SCOPE)
endfunction()

# Generate sbom information for a given target.
# Creates:
# - a SPDX package for the target
# - zero or more SPDX file entries for each installed binary file
# - each binary file entry gets a list of 'generated from source files' section
# - dependency relationships to other target packages
# - other relevant information like licenses, copyright, etc.
# For licenses, copyrights, these can either be passed as options, or read from qt_attribution.json
# files.
# For dependencies, these are either specified via options, or read from properties set on the
# target by qt_internal_extend_target.
function(_qt_internal_sbom_add_target target)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    _qt_internal_get_sbom_add_target_options(opt_args single_args multi_args)
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    get_target_property(target_type ${target} TYPE)

    _qt_internal_map_sbom_entity_type(sbom_entity_type ${ARGN})

    # Mark the target as a Qt module for sbom processing purposes.
    # Needed for non-standard targets like Bootstrap and QtLibraryInfo, that don't have a Qt::
    # namespace prefix.
    if(sbom_entity_type STREQUAL QT_MODULE)
        set_target_properties(${target} PROPERTIES _qt_sbom_is_qt_module TRUE)
    endif()

    set(project_package_options_spdx "")
    set(project_package_options_cydx "")

    if(arg_FRIENDLY_PACKAGE_NAME)
        set(package_name_for_spdx_id "${arg_FRIENDLY_PACKAGE_NAME}")
    else()
        set(package_name_for_spdx_id "${target}")
    endif()

    set(package_comment "")

    if(arg_SBOM_INCOMPLETE_3RD_PARTY_DEPENDENCIES)
        string(APPEND package_comment
            "Note: This package was marked as not listing all of its consumed 3rd party "
            "dependencies.\nThus the licensing and copyright information might be incomplete.\n")
    endif()

    if(arg_SBOM_PACKAGE_COMMENT)
        string(APPEND package_comment "${arg_SBOM_PACKAGE_COMMENT}\n")
    endif()

    string(APPEND package_comment "CMake target name: ${target}\n")

    get_target_property(qt_package_name "${target}" _qt_package_name)
    if(qt_package_name)
        get_target_property(qt_module_interface_name "${target}" _qt_module_interface_name)
        set(namespaced_target_name "${QT_CMAKE_EXPORT_NAMESPACE}::${qt_module_interface_name}")

        string(APPEND package_comment
            "CMake exported target name: ${namespaced_target_name}\n")
        string(APPEND package_comment "Contained in CMake package: ${qt_package_name}\n")
    endif()

    # Record the target spdx id right now, so we can refer to it in later attribution targets
    # if needed.
    _qt_internal_sbom_record_target_spdx_id(${target}
        SBOM_ENTITY_TYPE "${sbom_entity_type}"
        PACKAGE_NAME "${package_name_for_spdx_id}"
        OUT_VAR package_spdx_id
    )

    if(arg_USE_ATTRIBUTION_FILES)
        set(attribution_args
            ATTRIBUTION_PARENT_TARGET "${target}"
            ATTRIBUTION_PARENT_TARGET_SBOM_ENTITY_TYPE "${sbom_entity_type}"
        )

        # Forward the sbom specific options when handling attribution files because those might
        # create other sbom targets that need to inherit the parent ones.
        _qt_internal_get_sbom_specific_options(sbom_opt_args sbom_single_args sbom_multi_args)

        _qt_internal_forward_function_args(
            FORWARD_APPEND
            FORWARD_PREFIX arg
            FORWARD_OUT_VAR attribution_args
            FORWARD_OPTIONS
                ${sbom_opt_args}
            FORWARD_SINGLE
                ${sbom_single_args}
            FORWARD_MULTI
                ${sbom_multi_args}
        )

        if(NOT arg_NO_CURRENT_DIR_ATTRIBUTION
                AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/qt_attribution.json")
            list(APPEND attribution_args
                ATTRIBUTION_FILE_PATHS "${CMAKE_CURRENT_SOURCE_DIR}/qt_attribution.json"
            )
        endif()

        _qt_internal_sbom_handle_qt_attribution_files(qa ${attribution_args})
    endif()

    # Collect license expressions, but in most cases, each expression needs to be abided, so we
    # AND the accumulated license expressions.
    set(license_expression "")

    if(arg_LICENSE_EXPRESSION)
        set(license_expression "${arg_LICENSE_EXPRESSION}")
    endif()

    if(arg___QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_LICENSE)
        _qt_internal_sbom_forward_sbom_add_target_options_modified(sbom_add_target_args)
        _qt_internal_sbom_handle_qt_entity_license_expression(${target} ${sbom_add_target_args}
            OUT_VAR qt_entity_license_expression)
        if(qt_entity_license_expression)
            _qt_internal_sbom_join_two_license_ids_with_op(
                "${license_expression}" "AND" "${qt_entity_license_expression}"
                license_expression)
        endif()
    endif()

    # Read a license expression from the attribution json file.
    if(arg_USE_ATTRIBUTION_FILES
            AND qa_license_id
            AND NOT arg_NO_ATTRIBUTION_LICENSE_ID)
        if(NOT qa_license_id MATCHES "urn:dje:license")
            _qt_internal_sbom_join_two_license_ids_with_op(
                "${license_expression}" "AND" "${qa_license_id}"
                license_expression)
        else()
            message(DEBUG
                "Attribution license id contains invalid spdx license reference: ${qa_license_id}")
            set(invalid_license_comment
                "    Attribution license ID with invalid spdx license reference: ")
            string(APPEND invalid_license_comment "${qa_license_id}\n")
            string(APPEND package_comment "${invalid_license_comment}")
        endif()
    endif()

    if(license_expression)
        list(APPEND project_package_options_spdx LICENSE_CONCLUDED "${license_expression}")
        list(APPEND project_package_options_cydx LICENSE_CONCLUDED "${license_expression}")
    endif()

    if(license_expression AND
            arg___QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_LICENSE)
        _qt_internal_sbom_forward_sbom_add_target_options_modified(sbom_add_target_args)
        _qt_internal_sbom_handle_qt_entity_license_declared_expression(${target}
            ${sbom_add_target_args}
            LICENSE_CONCLUDED_EXPRESSION "${license_expression}"
            OUT_VAR qt_entity_license_declared_expression)
        if(qt_entity_license_declared_expression)
            list(APPEND project_package_options_spdx
                LICENSE_DECLARED "${qt_entity_license_declared_expression}")
            list(APPEND project_package_options_cydx
                LICENSE_DECLARED "${qt_entity_license_declared_expression}")
        endif()
    endif()

    # Copyrights are additive, so we collect them from all sources that were found.
    set(copyrights "")
    if(arg_COPYRIGHTS)
        list(APPEND copyrights "${arg_COPYRIGHTS}")
    endif()

    if(arg___QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_COPYRIGHTS)
        _qt_internal_sbom_forward_sbom_add_target_options_modified(sbom_add_target_args)
        _qt_internal_sbom_handle_qt_entity_copyrights(${target} ${sbom_add_target_args}
            OUT_VAR qt_copyrights)
        if(qt_copyrights)
            list(APPEND copyrights ${qt_copyrights})
        endif()
    endif()

    if(arg_USE_ATTRIBUTION_FILES AND qa_copyrights)
        list(APPEND copyrights "${qa_copyrights}")
    endif()
    if(copyrights)
        list(JOIN copyrights "\n" copyrights)
        list(APPEND project_package_options_spdx COPYRIGHT "<text>${copyrights}</text>")
        list(APPEND project_package_options_cydx COPYRIGHT "${copyrights}")
    endif()

    set(package_version "")
    if(arg_PACKAGE_VERSION)
        set(package_version "${arg_PACKAGE_VERSION}")
    elseif(arg_USE_ATTRIBUTION_FILES AND qa_version)
        set(package_version "${qa_version}")
    endif()

    if(arg___QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_PACKAGE_VERSION)
        _qt_internal_sbom_forward_sbom_add_target_options_modified(sbom_add_target_args)
        _qt_internal_sbom_handle_qt_entity_package_version(${target} ${sbom_add_target_args}
            OUT_VAR qt_entity_package_version)
        if(qt_entity_package_version)
            set(package_version "${qt_entity_package_version}")
        endif()
    endif()

    if(package_version)
        list(APPEND project_package_options_spdx VERSION "${package_version}")
        list(APPEND project_package_options_cydx VERSION "${package_version}")

        # Also export the value in a target property, to make it available for cydx generation.
        set_property(TARGET "${target}" PROPERTY _qt_sbom_package_version "${package_version}")
        set_property(TARGET "${target}" APPEND PROPERTY EXPORT_PROPERTIES _qt_sbom_package_version)
    endif()

    set(supplier "")
    if(arg_SUPPLIER)
        set(supplier "${arg_SUPPLIER}")
    endif()

    if(arg___QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_SUPPLIER)
        _qt_internal_sbom_forward_sbom_add_target_options_modified(sbom_add_target_args)
        _qt_internal_sbom_handle_qt_entity_supplier(${target} ${sbom_add_target_args}
            OUT_VAR qt_entity_supplier)
        if(qt_entity_supplier)
            set(supplier "${qt_entity_supplier}")
        endif()
    endif()

    if(supplier)
        list(APPEND project_package_options_spdx SUPPLIER "Organization: ${supplier}")
        list(APPEND project_package_options_cydx CYDX_SUPPLIER "${supplier}")
    endif()

    set(download_location "")
    if(arg_DOWNLOAD_LOCATION)
        set(download_location "${arg_DOWNLOAD_LOCATION}")
    endif()

    if(arg___QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_DOWNLOAD_LOCATION)
        _qt_internal_sbom_forward_sbom_add_target_options_modified(sbom_add_target_args)
        _qt_internal_sbom_handle_qt_entity_download_location(${target} ${sbom_add_target_args}
            OUT_VAR qt_entity_download_location)
        if(qt_entity_download_location)
            set(download_location "${qt_entity_download_location}")
        endif()
    endif()

    if(arg_USE_ATTRIBUTION_FILES)
        if(qa_download_location)
            set(download_location "${qa_download_location}")
        elseif(qa_homepage)
            set(download_location "${qa_homepage}")
        endif()
    endif()

    if(sbom_entity_type STREQUAL "SYSTEM_LIBRARY")
        # Try to get package url that was set using CMake's set_package_properties function.
        # Relies on querying the internal global property name that CMake sets in its
        # implementation.
        get_cmake_property(target_url _CMAKE_${package_name_for_spdx_id}_URL)
        if(target_url)
            set(download_location "${target_url}")
        endif()
        if(NOT download_location
                AND arg_USE_ATTRIBUTION_FILES
                AND qa_download_location)
            set(download_location "${qa_download_location}")
        endif()
    endif()

    if(download_location)
        list(APPEND project_package_options_spdx DOWNLOAD_LOCATION "${download_location}")
        list(APPEND project_package_options_cydx DOWNLOAD_LOCATION "${download_location}")
    endif()

    _qt_internal_sbom_get_package_purpose("${sbom_entity_type}" package_purpose)
    list(APPEND project_package_options_spdx PURPOSE "${package_purpose}")

    set(cpe_values "")

    if(arg_CPE)
        list(APPEND cpe_values "${arg_CPE}")
    endif()

    if(arg_CPE_VENDOR AND arg_CPE_PRODUCT)
        _qt_internal_sbom_compute_security_cpe(custom_cpe
            VENDOR "${arg_CPE_VENDOR}"
            PRODUCT "${arg_CPE_PRODUCT}"
            VERSION "${package_version}")
        list(APPEND cpe_values "${custom_cpe}")
    endif()

    if(arg_USE_ATTRIBUTION_FILES AND qa_cpes)
        set(placeholder_args "")
        if(package_version)
            list(APPEND placeholder_args VERSION "${package_version}")
        endif()
        _qt_internal_sbom_replace_qa_placeholders(
            VALUES ${qa_cpes}
            ${placeholder_args}
            OUT_VAR qa_cpes_replaced
        )
        list(APPEND cpe_values "${qa_cpes_replaced}")
    endif()

    if(arg___QT_INTERNAL_HANDLE_QT_ENTITY_TYPE_CPE)
        _qt_internal_sbom_forward_sbom_add_target_options_modified(sbom_add_target_args)
        _qt_internal_sbom_handle_qt_entity_cpe(${target} ${sbom_add_target_args}
            CPE "${cpe_values}"
            OUT_VAR qt_cpe_list)
        if(qt_cpe_list)
            list(APPEND cpe_values ${qt_cpe_list})
        endif()
    endif()

    if(cpe_values)
        list(APPEND project_package_options_spdx CPE ${cpe_values})
        list(APPEND project_package_options_cydx CPE ${cpe_values})
    endif()

    # Assemble arguments to forward to the function that handles purl options.
    set(purl_args
        SBOM_ENTITY_TYPE ${sbom_entity_type}
    )

    _qt_internal_get_sbom_purl_add_target_options(purl_opt_args purl_single_args purl_multi_args)
    _qt_internal_forward_function_args(
        FORWARD_APPEND
        FORWARD_PREFIX arg
        FORWARD_OUT_VAR purl_args
        FORWARD_OPTIONS
            ${purl_opt_args}
        FORWARD_SINGLE
            ${purl_single_args}
        FORWARD_MULTI
            ${purl_multi_args}
    )

    if(supplier)
        list(APPEND purl_args SUPPLIER "${supplier}")
    endif()

    if(package_version)
        list(APPEND purl_args PACKAGE_VERSION "${package_version}")
    endif()

    if(arg_USE_ATTRIBUTION_FILES AND qa_purls)
        set(placeholder_args "")
        if(package_version)
            list(APPEND placeholder_args VERSION "${package_version}")
        endif()
        _qt_internal_sbom_replace_qa_placeholders(
            VALUES ${qa_purls}
            ${placeholder_args}
            OUT_VAR qa_purls_replaced
        )

        list(APPEND purl_args PURL_VALUES ${qa_purls_replaced})
    endif()
    list(APPEND purl_args
        OUT_VAR_PURL_VALUES purl_values
        OUT_VAR_SPDX_EXT_REF_VALUES spdx_ext_ref_values
    )

    _qt_internal_sbom_handle_purl_values(${target} ${purl_args})

    if(spdx_ext_ref_values)
        list(APPEND project_package_options_spdx ${spdx_ext_ref_values})
        list(APPEND project_package_options_cydx PURL_VALUES ${purl_values})
    endif()

    if(arg_USE_ATTRIBUTION_FILES)
        if(qa_chosen_attribution_file_path)
            _qt_internal_sbom_map_path_to_reproducible_relative_path(relative_attribution_path
                PATH "${qa_chosen_attribution_file_path}"
            )
            string(APPEND package_comment
                "    Information extracted from:\n     ${relative_attribution_path}\n")
        endif()

        if(NOT "${qa_chosen_attribution_entry_index}" STREQUAL "")
            string(APPEND package_comment
                "    Entry index: ${qa_chosen_attribution_entry_index}\n")
        endif()

        if(qa_attribution_id)
            string(APPEND package_comment "    Id: ${qa_attribution_id}\n")
        endif()

        if(qa_attribution_name)
            string(APPEND package_comment "    Name: ${qa_attribution_name}\n")
        endif()

        if(qa_description)
            string(APPEND package_comment "    Description: ${qa_description}\n")
        endif()

        if(qa_qt_usage)
            string(APPEND package_comment "    Qt usage: ${qa_qt_usage}\n")
        endif()

        if(qa_license)
            string(APPEND package_comment "    License: ${qa_license}\n")
        endif()

        if(qa_license_file)
            string(APPEND package_comment "    License file: ${qa_license_file}\n")
        endif()
    endif()

    if(package_comment)
        list(APPEND project_package_options_spdx COMMENT "<text>\n${package_comment}</text>")
        list(APPEND project_package_options_cydx COMMENT "\n${package_comment}")
    endif()

    _qt_internal_sbom_handle_target_dependencies("${target}"
        SPDX_ID "${package_spdx_id}"
        LIBRARIES "${arg_LIBRARIES}"
        PUBLIC_LIBRARIES "${arg_PUBLIC_LIBRARIES}"
        OUT_CYDX_DEPENDENCIES cydx_dependencies
        OUT_SPDX_RELATIONSHIPS spdx_relationships
        OUT_EXTERNAL_TARGET_DEPENDENCIES external_target_dependencies
    )
    if(cydx_dependencies)
        list(APPEND project_package_options_cydx DEPENDENCIES ${cydx_dependencies})
    endif()

    # These are processed at the end of document generation.
    if(external_target_dependencies)
        _qt_internal_sbom_record_external_target_dependecies(
            TARGETS ${external_target_dependencies}
        )
    endif()

    get_cmake_property(project_spdx_id _qt_internal_sbom_project_spdx_id)
    list(APPEND spdx_relationships "${project_spdx_id} CONTAINS ${package_spdx_id}")

    if(arg_SBOM_RELATIONSHIPS)
        list(APPEND spdx_relationships "${arg_SBOM_RELATIONSHIPS}")
    endif()

    list(REMOVE_DUPLICATES spdx_relationships)
    list(JOIN spdx_relationships "\nRelationship: " relationships)
    list(APPEND project_package_options_spdx RELATIONSHIP "${relationships}")

    if(QT_SBOM_GENERATE_SPDX_V2)
        _qt_internal_sbom_generate_add_package(
            PACKAGE "${package_name_for_spdx_id}"
            SPDXID "${package_spdx_id}"
            ${project_package_options_spdx}
        )
    endif()

    if(QT_SBOM_GENERATE_CYDX_V1_6)
        get_property(external_targets
            GLOBAL PROPERTY _qt_internal_sbom_external_target_dependencies)

        # Prevent against case when a system library is also an external target dependency,
        # which would lead to its creation twice, once via
        # _qt_internal_sbom_add_recorded_system_libraries
        # and second time via
        # _qt_internal_sbom_add_cydx_external_target_dependencies.
        # Skip the case when it's done via the first function, and only allow the second.
        # TODO: Can this be done better somehow?
        if(NOT target IN_LIST external_targets)
            _qt_internal_sbom_handle_qt_entity_cydx_properties(
                SBOM_ENTITY_TYPE "${sbom_entity_type}"
                OUT_CYDX_PROPERTIES cydx_properties
            )

            _qt_internal_sbom_generate_cyclone_add_package(
                PACKAGE "${package_name_for_spdx_id}"
                SPDXID "${package_spdx_id}"
                SBOM_ENTITY_TYPE "${sbom_entity_type}"
                ${project_package_options_cydx}
                CONTAINING_COMPONENT "${project_spdx_id}"
                CYDX_PROPERTIES ${cydx_properties}
            )
        endif()
    endif()

    set(no_install_option "")
    if(arg_NO_INSTALL)
        set(no_install_option NO_INSTALL)
    endif()

    set(framework_option "")
    if(APPLE AND NOT target_type STREQUAL "INTERFACE_LIBRARY")
        get_target_property(is_framework ${target} FRAMEWORK)
        if(is_framework)
            set(framework_option "FRAMEWORK")
        endif()
    endif()

    set(install_prefix_option "")
    get_cmake_property(install_prefix _qt_internal_sbom_install_prefix)
    if(install_prefix)
        set(install_prefix_option INSTALL_PREFIX "${install_prefix}")
    endif()

    _qt_internal_forward_function_args(
        FORWARD_PREFIX arg
        FORWARD_OUT_VAR target_binary_multi_config_args
        FORWARD_SINGLE
            ${multi_config_single_args}
    )

    set(copyrights_option "")
    if(copyrights)
        set(copyrights_option COPYRIGHTS "${copyrights}")
    endif()

    set(license_option "")
    if(license_expression)
        set(license_option LICENSE_EXPRESSION "${license_expression}")
    endif()

    if(QT_SBOM_GENERATE_SPDX_V2)
        _qt_internal_sbom_handle_target_binary_files("${target}"
            ${no_install_option}
            ${framework_option}
            ${install_prefix_option}
            SBOM_ENTITY_TYPE "${sbom_entity_type}"
            ${target_binary_multi_config_args}
            SPDX_ID "${package_spdx_id}"
            ${copyrights_option}
            ${license_option}
        )

        _qt_internal_sbom_handle_target_custom_files("${target}"
            ${no_install_option}
            ${install_prefix_option}
            PACKAGE_TYPE "${sbom_entity_type}"
            PACKAGE_SPDX_ID "${package_spdx_id}"
            ${copyrights_option}
            ${license_option}
        )
    endif()
endfunction()

# Helper to add sbom information for a possibly non-existing target.
# This will defer the actual sbom generation until the end of the directory scope, unless
# immediate finalization was requested.
function(_qt_internal_add_sbom target)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    set(opt_args
        IMMEDIATE_FINALIZATION
    )
    set(single_args "")
    set(multi_args "")
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    # No validation on purpose, the other options will be validated later.

    set(forward_args ${ARGN})

    # If a target doesn't exist we create it.
    if(NOT TARGET "${target}")
        _qt_internal_create_sbom_target("${target}" ${forward_args})
    endif()

    if(arg_IMMEDIATE_FINALIZATION)
        list(APPEND forward_args IMMEDIATE_FINALIZATION)
    endif()

    # Save the passed options.
    _qt_internal_extend_sbom("${target}" ${forward_args})
endfunction()

# Helper to add custom sbom information for some kind of dependency that is not backed by an
# existing target.
# Useful for cases like 3rd party dependencies not represented by an already existing imported
# target, or for 3rd party sources that get compiled into a regular Qt target (PCRE sources compiled
# into Bootstrap).
function(_qt_internal_create_sbom_target target)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    set(opt_args "")
    set(single_args "")
    set(multi_args "")
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    # No validation on purpose, the other options will be validated later.

    if(TARGET "${target}")
        message(FATAL_ERROR "The target ${target} already exists.")
    endif()

    add_library("${target}" INTERFACE IMPORTED)
    set_target_properties(${target} PROPERTIES
        _qt_sbom_is_custom_sbom_target "TRUE"
        IMPORTED_GLOBAL TRUE
    )
endfunction()

# Helper to add additional sbom information for an existing target.
# Just appends the options to the target's sbom args property, which will will be evaluated
# during finalization.
function(_qt_internal_extend_sbom target)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    if(NOT TARGET "${target}")
        message(FATAL_ERROR
            "The target ${target} does not exist, use qt_internal_add_sbom to create "
            "a target first, or call the function on any other exsiting target.")
    endif()

    set(opt_args
        NO_FINALIZATION
        IMMEDIATE_FINALIZATION
    )
    set(single_args
        TYPE # deprecated
        SBOM_ENTITY_TYPE
        DEFAULT_SBOM_ENTITY_TYPE
        FRIENDLY_PACKAGE_NAME
    )
    set(multi_args "")
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    # No validation on purpose, the other options will be validated later.

    set(forward_args ${ARGN})

    # Remove NO_FINALIZATION, IMMEDIATE_FINALIZATION from the forwarded args.
    list(REMOVE_ITEM forward_args
        NO_FINALIZATION
        IMMEDIATE_FINALIZATION
    )

    _qt_internal_map_sbom_entity_type(sbom_entity_type ${ARGN})

    # Make sure a spdx id is recorded for the target right now, so it is "known" when handling
    # relationships for other targets, even if the target was not yet finalized.
    if(sbom_entity_type)
        # Friendly package name is allowed to be empty.
        set(package_name_option "")
        if(arg_FRIENDLY_PACKAGE_NAME)
            set(package_name_option PACKAGE_NAME "${arg_FRIENDLY_PACKAGE_NAME}")
        endif()

        _qt_internal_sbom_record_target_spdx_id(${target}
            SBOM_ENTITY_TYPE "${sbom_entity_type}"
            ${package_name_option}
        )
    endif()
    get_target_property(is_system_library "${target}" _qt_internal_sbom_is_system_library)

    set_property(TARGET ${target} APPEND PROPERTY _qt_finalize_sbom_args "${forward_args}")

    # If requested via NO_FINALIZATION or the target is a system library, don't run finalization.
    # This is necessary for system libraries because those are handled in special code path just
    # before finishing project sbom generation, and finalizing them would cause issues because they
    # don't actually have a TYPE until a later point in time.
    if(NOT arg_NO_FINALIZATION AND NOT is_system_library)
        # Defer finalization. In case it was already deferred, it will be a no-op.
        # Some targets need immediate finalization, like the PlatformInternal ones,
        # because otherwise they would be finalized after the sbom was already generated.
        set(immediate_finalization "")
        if(arg_IMMEDIATE_FINALIZATION)
            set(immediate_finalization IMMEDIATE_FINALIZATION)
        endif()
        _qt_internal_defer_sbom_finalization("${target}" ${immediate_finalization})
    endif()
endfunction()

# Helper to add additional sbom information to targets created by qt_find_package.
# If the package was not found, and the targets were not created, the functions does nothing.
# This is similar to _qt_internal_extend_sbom, but is explicit in the fact that the targets might
# not exist.
function(_qt_find_package_extend_sbom)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    _qt_internal_get_sbom_add_target_options(sbom_opt_args sbom_single_args sbom_multi_args)

    set(opt_args
        ${sbom_opt_args}
    )
    set(single_args
        ${sbom_single_args}
    )
    set(multi_args
        TARGETS
        ${sbom_multi_args}
    )

    cmake_parse_arguments(PARSE_ARGV 0 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    # Make sure not to forward TARGETS.
    # Also don't enable finalization, because system libraries are handled specially in a different
    # code path.
    set(sbom_args
        NO_FINALIZATION
    )
    _qt_internal_forward_function_args(
        FORWARD_APPEND
        FORWARD_PREFIX arg
        FORWARD_OUT_VAR sbom_args
        FORWARD_OPTIONS
            ${sbom_opt_args}
        FORWARD_SINGLE
            ${sbom_single_args}
        FORWARD_MULTI
            ${sbom_multi_args}
    )

    foreach(target IN LISTS arg_TARGETS)
        if(TARGET "${target}")
            _qt_internal_extend_sbom("${target}" ${sbom_args})
        else()
            message(DEBUG "The target ${target} does not exist, skipping extending the sbom info.")
        endif()
    endforeach()
endfunction()

# Helper to defer adding sbom information for a target, at the end of the directory scope.
function(_qt_internal_defer_sbom_finalization target)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    set(opt_args
        IMMEDIATE_FINALIZATION
    )
    set(single_args "")
    set(multi_args "")
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    get_target_property(sbom_finalization_requested ${target} _qt_sbom_finalization_requested)
    if(sbom_finalization_requested)
        # Already requested, nothing to do.
        return()
    endif()
    set_target_properties(${target} PROPERTIES _qt_sbom_finalization_requested TRUE)

    _qt_internal_append_to_cmake_property_without_duplicates(
        _qt_internal_sbom_targets_waiting_for_finalization
        "${target}"
    )

    set(func "_qt_internal_finalize_sbom")

    if(arg_IMMEDIATE_FINALIZATION)
        _qt_internal_finalize_sbom(${target})
    elseif(QT_BUILDING_QT)
        qt_add_list_file_finalizer("${func}" "${target}")
    elseif(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19")
        cmake_language(EVAL CODE "cmake_language(DEFER CALL \"${func}\" \"${target}\")")
    else()
        message(FATAL_ERROR "Defer adding a sbom target requires CMake version 3.19")
    endif()
endfunction()

# Finalizer to add sbom information for the target.
# Expects the target to exist.
function(_qt_internal_finalize_sbom target)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    get_target_property(sbom_finalization_done ${target} _qt_sbom_finalization_done)
    if(sbom_finalization_done)
        # Already done, nothing to do.
        return()
    endif()
    set_target_properties(${target} PROPERTIES _qt_sbom_finalization_done TRUE)

    get_target_property(sbom_args ${target} _qt_finalize_sbom_args)
    if(NOT sbom_args)
        set(sbom_args "")
    endif()
    _qt_internal_sbom_add_target(${target} ${sbom_args})
endfunction()

# Extends the list of targets that are considered dependencies for target.
function(_qt_internal_extend_sbom_dependencies target)
    if(NOT QT_GENERATE_SBOM)
        return()
    endif()

    set(opt_args "")
    set(single_args "")
    set(multi_args
        SBOM_DEPENDENCIES
    )
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    if(NOT TARGET "${target}")
        message(FATAL_ERROR "The target ${target} does not exist.")
    endif()

    _qt_internal_append_to_target_property_without_duplicates(${target}
        _qt_extend_target_sbom_dependencies "${arg_SBOM_DEPENDENCIES}"
    )
endfunction()

# Sets the sbom project name for the root project.
function(_qt_internal_sbom_set_root_project_name project_name)
    set_property(GLOBAL PROPERTY _qt_internal_sbom_repo_project_name "${project_name}")
endfunction()

# Sets the real qt repo project name for a given project (e.g. set QtWebEngine for project QtPdf).
# This is needed to be able to extract the qt repo dependencies in a top-level build.
function(_qt_internal_sbom_set_qt_repo_project_name project_name)
    set_property(GLOBAL PROPERTY _qt_internal_sbom_qt_repo_project_name "${project_name}")
endfunction()

# Get repo project_name spdx id reference, needs to start with Package- to be NTIA compliant.
function(_qt_internal_sbom_get_root_project_name_for_spdx_id out_var)
    _qt_internal_sbom_get_root_project_name_lower_case(repo_project_name_lowercase)
    set(sbom_repo_project_name "Package-${repo_project_name_lowercase}")
    set(${out_var} "${sbom_repo_project_name}" PARENT_SCOPE)
endfunction()

# Returns the lower case sbom project name.
function(_qt_internal_sbom_get_root_project_name_lower_case out_var)
    get_cmake_property(project_name _qt_internal_sbom_repo_project_name)

    if(NOT project_name)
        message(FATAL_ERROR "No SBOM project name was set.")
    endif()

    string(TOLOWER "${project_name}" repo_project_name_lowercase)
    set(${out_var} "${repo_project_name_lowercase}" PARENT_SCOPE)
endfunction()

# Returns the lower case real qt repo project name (e.g. returns 'qtwebengine' when building the
# project qtpdf).
function(_qt_internal_sbom_get_qt_repo_project_name_lower_case out_var)
    get_cmake_property(project_name _qt_internal_sbom_qt_repo_project_name)

    if(NOT project_name)
        message(FATAL_ERROR "No real Qt repo SBOM project name was set.")
    endif()

    string(TOLOWER "${project_name}" repo_project_name_lowercase)
    set(${out_var} "${repo_project_name_lowercase}" PARENT_SCOPE)
endfunction()

# Get a spdx id to reference an external document.
function(_qt_internal_sbom_get_external_document_ref_spdx_id repo_name out_var)
    set(${out_var} "DocumentRef-${repo_name}" PARENT_SCOPE)
endfunction()

# Sanitize a given value to be used as a SPDX id.
function(_qt_internal_sbom_get_sanitized_spdx_id out_var hint)
    # Only allow alphanumeric characters and dashes.
    string(REGEX REPLACE "[^a-zA-Z0-9]+" "-" spdx_id "${hint}")

    # Remove all trailing dashes.
    string(REGEX REPLACE "-+$" "" spdx_id "${spdx_id}")

    set(${out_var} "${spdx_id}" PARENT_SCOPE)
endfunction()

# Generates a spdx id for a target and saves it its properties.
function(_qt_internal_sbom_record_target_spdx_id target)
    set(opt_args "")
    set(single_args
        PACKAGE_NAME
        SBOM_ENTITY_TYPE
        OUT_VAR
    )
    set(multi_args "")
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    _qt_internal_sbom_get_spdx_id_for_target("${target}" spdx_id)

    if(spdx_id)
        # Return early if the target was already recorded and has a spdx id.
        if(arg_OUT_VAR)
            set(${arg_OUT_VAR} "${spdx_id}" PARENT_SCOPE)
        endif()
        return()
    endif()

    if(arg_PACKAGE_NAME)
        set(package_name_for_spdx_id "${arg_PACKAGE_NAME}")
    else()
        set(package_name_for_spdx_id "${target}")
    endif()

    _qt_internal_sbom_generate_target_package_spdx_id(package_spdx_id
        SBOM_ENTITY_TYPE "${arg_SBOM_ENTITY_TYPE}"
        PACKAGE_NAME "${package_name_for_spdx_id}"
    )
    _qt_internal_sbom_save_spdx_id_for_target("${target}"
        SPDX_ID "${package_spdx_id}"
        PACKAGE_NAME "${package_name_for_spdx_id}"
        SBOM_ENTITY_TYPE "${arg_SBOM_ENTITY_TYPE}"
    )

    _qt_internal_sbom_is_qt_entity_type("${arg_SBOM_ENTITY_TYPE}" is_qt_entity_type)
    _qt_internal_sbom_save_spdx_id_for_qt_entity_type(
        "${target}" "${is_qt_entity_type}" "${package_spdx_id}")

    if(arg_OUT_VAR)
        set(${arg_OUT_VAR} "${package_spdx_id}" PARENT_SCOPE)
    endif()
endfunction()

# Generates a sanitized spdx id for a target (package) of a specific type.
function(_qt_internal_sbom_generate_target_package_spdx_id out_var)
    set(opt_args "")
    set(single_args
        PACKAGE_NAME
        SBOM_ENTITY_TYPE
    )
    set(multi_args "")
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    if(NOT arg_PACKAGE_NAME)
        message(FATAL_ERROR "PACKAGE_NAME must be set")
    endif()
    if(NOT arg_SBOM_ENTITY_TYPE)
        message(FATAL_ERROR "SBOM_ENTITY_TYPE must be set")
    endif()

    _qt_internal_sbom_get_root_project_name_for_spdx_id(repo_project_name_spdx_id)
    _qt_internal_sbom_get_package_infix("${arg_SBOM_ENTITY_TYPE}" package_infix)

    _qt_internal_sbom_get_sanitized_spdx_id(spdx_id
        "SPDXRef-${repo_project_name_spdx_id}-${package_infix}-${arg_PACKAGE_NAME}")

    set(${out_var} "${spdx_id}" PARENT_SCOPE)
endfunction()

# Save a spdx id for a target inside its target properties.
# These are used when generating a SPDX external document reference for exported targets, to
# include them in relationships.
# Also saves the repo document namespace and relative installed repo document path.
# Also saves the sbom entity type and package name, because it's needed when creating CycloneDX
# components where the target is in an external document.
function(_qt_internal_sbom_save_spdx_id_for_target target)
    set(opt_args "")
    set(single_args
        SPDX_ID
        PACKAGE_NAME
        SBOM_ENTITY_TYPE
    )
    set(multi_args "")
    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    if(NOT arg_SPDX_ID)
        message(FATAL_ERROR "arg_SPDX_ID must be set")
    endif()

    if(NOT arg_PACKAGE_NAME)
        message(FATAL_ERROR "PACKAGE_NAME must be set")
    endif()

    if(NOT arg_SBOM_ENTITY_TYPE)
        message(FATAL_ERROR "SBOM_ENTITY_TYPE must be set")
    endif()

    set(spdx_id "${arg_SPDX_ID}")

    message(DEBUG "Saving spdx id for target ${target}: ${spdx_id}")

    set(target_unaliased "${target}")
    _qt_internal_dealias_target(target_unaliased)

    set_target_properties(${target_unaliased} PROPERTIES
        _qt_sbom_spdx_id "${spdx_id}")

    # Retrieve repo specific properties.
    get_property(repo_document_namespace
        GLOBAL PROPERTY _qt_internal_sbom_repo_document_namespace)

    get_property(bom_serial_number_uuid
        GLOBAL PROPERTY _qt_internal_sbom_repo_cyclone_dx_bom_serial_number_uuid)

    get_property(relative_installed_repo_document_path
        GLOBAL PROPERTY _qt_internal_sbom_relative_installed_repo_document_path)

    get_property(project_name_lowercase
        GLOBAL PROPERTY _qt_internal_sbom_repo_project_name_lowercase)

    # And save them on the target.
    set_property(TARGET ${target_unaliased} PROPERTY
        _qt_sbom_spdx_repo_document_namespace
        "${repo_document_namespace}")

    set_property(TARGET ${target_unaliased} PROPERTY
        _qt_sbom_cydx_bom_serial_number_uuid
        "${bom_serial_number_uuid}")

    set_property(TARGET ${target_unaliased} PROPERTY
        _qt_sbom_spdx_relative_installed_repo_document_path
        "${relative_installed_repo_document_path}")

    set_property(TARGET ${target_unaliased} PROPERTY
        _qt_sbom_spdx_repo_project_name_lowercase
        "${project_name_lowercase}")

    set_property(TARGET ${target_unaliased} PROPERTY
        _qt_sbom_package_name
        "${arg_PACKAGE_NAME}")

    set_property(TARGET ${target_unaliased} PROPERTY
        _qt_sbom_entity_type
        "${arg_SBOM_ENTITY_TYPE}")

    # Export the properties, so they can be queried by other repos.
    # We also do it for versionless targets.
    set(export_properties
        _qt_sbom_entity_type
        _qt_sbom_package_name
        _qt_sbom_spdx_id
        _qt_sbom_spdx_repo_document_namespace
        _qt_sbom_cydx_bom_serial_number_uuid
        _qt_sbom_spdx_relative_installed_repo_document_path
        _qt_sbom_spdx_repo_project_name_lowercase
    )
    set_property(TARGET "${target_unaliased}" APPEND PROPERTY
        EXPORT_PROPERTIES "${export_properties}")
endfunction()

# Returns whether the given sbom type is considered to be a Qt type like a module or a tool.
function(_qt_internal_sbom_is_qt_entity_type sbom_type out_var)
    set(qt_entity_types
        QT_MODULE
        QT_PLUGIN
        QT_APP
        QT_TOOL
        QT_TRANSLATIONS
        QT_RESOURCES
        QT_CUSTOM
        QT_CUSTOM_NO_INFIX
    )

    set(is_qt_entity_type FALSE)
    if(sbom_type IN_LIST qt_entity_types)
        set(is_qt_entity_type TRUE)
    endif()

    set(${out_var} ${is_qt_entity_type} PARENT_SCOPE)
endfunction()

# Returns whether the given sbom type is considered to a Qt 3rd party entity type.
function(_qt_internal_sbom_is_qt_3rd_party_entity_type sbom_type out_var)
    set(entity_types
        QT_THIRD_PARTY_MODULE
        QT_THIRD_PARTY_SOURCES
    )

    set(is_qt_third_party_entity_type FALSE)
    if(sbom_type IN_LIST entity_types)
        set(is_qt_third_party_entity_type TRUE)
    endif()

    set(${out_var} ${is_qt_third_party_entity_type} PARENT_SCOPE)
endfunction()

# Save a spdx id for all known related target names of a given Qt target.
# Related being the namespaced and versionless variants of a Qt target.
# All the related targets will contain the same spdx id.
# So Core, CorePrivate, Qt6::Core, Qt6::CorePrivate, Qt::Core, Qt::CorePrivate will all be
# referred to by the same spdx id.
function(_qt_internal_sbom_save_spdx_id_for_qt_entity_type target is_qt_entity_type package_spdx_id)
    # Assign the spdx id to all known related target names of given the given Qt target.
    set(target_names "")

    if(is_qt_entity_type)
        set(namespaced_target "${QT_CMAKE_EXPORT_NAMESPACE}::${target}")
        set(namespaced_private_target "${QT_CMAKE_EXPORT_NAMESPACE}::${target}Private")
        set(versionless_target "Qt::${target}")
        set(versionless_private_target "Qt::${target}Private")

        list(APPEND target_names
            namespaced_target
            namespaced_private_target
            versionless_target
            versionless_private_target
        )

        get_property(package_name TARGET "${target}" PROPERTY _qt_sbom_package_name)
        get_property(sbom_entity_type TARGET "${target}" PROPERTY _qt_sbom_entity_type)
    endif()

    foreach(target_name IN LISTS ${target_names})
        if(TARGET "${target_name}")
            _qt_internal_sbom_save_spdx_id_for_target("${target_name}"
                SPDX_ID "${package_spdx_id}"
                PACKAGE_NAME "${package_name}"
                SBOM_ENTITY_TYPE "${sbom_entity_type}"
            )
        endif()
    endforeach()
endfunction()

# Retrieves a saved spdx id from the target. Might be empty.
function(_qt_internal_sbom_get_spdx_id_for_target target out_var)
    get_target_property(spdx_id ${target} _qt_sbom_spdx_id)
    set(${out_var} "${spdx_id}" PARENT_SCOPE)
endfunction()

# Returns a package infix for a given target sbom type to be used in spdx package id generation.
function(_qt_internal_sbom_get_package_infix type out_infix)
    if(type STREQUAL "QT_MODULE")
        set(package_infix "qt-module")
    elseif(type STREQUAL "QT_PLUGIN")
        set(package_infix "qt-plugin")
    elseif(type STREQUAL "QML_PLUGIN")
        set(package_infix "qt-qml-plugin") # not used at the moment
    elseif(type STREQUAL "QT_TOOL")
        set(package_infix "qt-tool")
    elseif(type STREQUAL "QT_APP")
        set(package_infix "qt-app")
    elseif(type STREQUAL "QT_THIRD_PARTY_MODULE")
        set(package_infix "qt-bundled-3rdparty-module")
    elseif(type STREQUAL "QT_THIRD_PARTY_SOURCES")
        set(package_infix "qt-3rdparty-sources")
    elseif(type STREQUAL "QT_TRANSLATIONS")
        set(package_infix "qt-translation")
    elseif(type STREQUAL "QT_RESOURCES")
        set(package_infix "qt-resource")
    elseif(type STREQUAL "QT_CUSTOM")
        set(package_infix "qt-custom")
    elseif(type STREQUAL "QT_CUSTOM_NO_INFIX")
        set(package_infix "qt")
    elseif(type STREQUAL "SYSTEM_LIBRARY")
        set(package_infix "system-3rdparty")
    elseif(type STREQUAL "EXECUTABLE")
        set(package_infix "executable")
    elseif(type STREQUAL "LIBRARY")
        set(package_infix "library")
    elseif(type STREQUAL "THIRD_PARTY_LIBRARY")
        set(package_infix "3rdparty-library")
    elseif(type STREQUAL "THIRD_PARTY_LIBRARY_WITH_FILES")
        set(package_infix "3rdparty-library-with-files")
    elseif(type STREQUAL "THIRD_PARTY_SOURCES")
        set(package_infix "3rdparty-sources")
    elseif(type STREQUAL "TRANSLATIONS")
        set(package_infix "translations")
    elseif(type STREQUAL "RESOURCES")
        set(package_infix "resource")
    elseif(type STREQUAL "CUSTOM")
        set(package_infix "custom")
    elseif(type STREQUAL "CUSTOM_NO_INFIX")
        set(package_infix "")
    else()
        message(DEBUG "No package infix due to unknown type: ${type}")
        set(package_infix "")
    endif()
    set(${out_infix} "${package_infix}" PARENT_SCOPE)
endfunction()

# Returns a package purpose for a given target sbom type.
function(_qt_internal_sbom_get_package_purpose type out_purpose)
    if(type STREQUAL "QT_MODULE")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "QT_PLUGIN")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "QML_PLUGIN")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "QT_TOOL")
        set(package_purpose "APPLICATION")
    elseif(type STREQUAL "QT_APP")
        set(package_purpose "APPLICATION")
    elseif(type STREQUAL "QT_THIRD_PARTY_MODULE")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "QT_THIRD_PARTY_SOURCES")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "QT_TRANSLATIONS")
        set(package_purpose "OTHER")
    elseif(type STREQUAL "QT_RESOURCES")
        set(package_purpose "OTHER")
    elseif(type STREQUAL "QT_CUSTOM")
        set(package_purpose "OTHER")
    elseif(type STREQUAL "QT_CUSTOM_NO_INFIX")
        set(package_purpose "OTHER")
    elseif(type STREQUAL "SYSTEM_LIBRARY")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "EXECUTABLE")
        set(package_purpose "APPLICATION")
    elseif(type STREQUAL "LIBRARY")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "THIRD_PARTY_LIBRARY")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "THIRD_PARTY_LIBRARY_WITH_FILES")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "THIRD_PARTY_SOURCES")
        set(package_purpose "LIBRARY")
    elseif(type STREQUAL "TRANSLATIONS")
        set(package_purpose "OTHER")
    elseif(type STREQUAL "RESOURCES")
        set(package_purpose "OTHER")
    elseif(type STREQUAL "CUSTOM")
        set(package_purpose "OTHER")
    elseif(type STREQUAL "CUSTOM_NO_INFIX")
        set(package_purpose "OTHER")
    else()
        set(package_purpose "OTHER")
    endif()
    set(${out_purpose} "${package_purpose}" PARENT_SCOPE)
endfunction()

# Determines various version vars for the root SBOM package.
# Options:
#   QT_SBOM_GIT_VERSION - tries to query version info from the git repo that is in the current
#     working dir.
#   VERSION - overrides the git version info with the provided value.
#
# Stores the version vars in global properties. The following vars are set:
#  QT_SBOM_GIT_VERSION - the version extracted from git, or the explicit version specified via
#    VERSION. The git version is usually a tag or short sha1 + a branch + dirty flag.
#  QT_SBOM_GIT_VERSION_PATH - the same as above, but the value is sanitized to be path safe
#  QT_SBOM_GIT_HASH - the full git commit sha
#  QT_SBOM_GIT_HASH_SHORT - the short git commit sha
#  QT_SBOM_EXPLICIT_VERSION - the explicit version provided via VERSION or QT_SBOM_VERSION_OVERRIDE
#    if set.
#
# TODO: Consider renaming QT_SBOM_GIT_VERSION and friends to QT_SBOM_VERSION, because the version
# might not come from git, but from VERSION option, so the current naming is somewhat misleading.
function(_qt_internal_handle_sbom_project_version)
    set(opt_args
        USE_GIT_VERSION
    )
    set(single_args
        VERSION
    )
    set(multi_args "")

    cmake_parse_arguments(PARSE_ARGV 0 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    # Query git version info if requested.
    # The git version might not be available for multiple reasons even if requsted, due to e.g. the
    # current working dir not being a git repo, or git not being installed, etc.
    # Some of the values might be overridden further down even.
    if(arg_USE_GIT_VERSION)
        _qt_internal_find_git_package()
        _qt_internal_query_git_version(
            EMPTY_VALUE_WHEN_NOT_GIT_REPO
            OUT_VAR_PREFIX __sbom_
        )
        set(QT_SBOM_GIT_VERSION "${__sbom_git_version}")
        set(QT_SBOM_GIT_VERSION_PATH "${__sbom_git_version_path}")
        set(QT_SBOM_GIT_HASH "${__sbom_git_hash}")
        set(QT_SBOM_GIT_HASH_SHORT "${__sbom_git_hash_short}")
    else()
        # To be clean, set the variables to an empty value rather than keeping them undefined.
        set(QT_SBOM_GIT_VERSION "")
        set(QT_SBOM_GIT_VERSION_PATH "")
        set(QT_SBOM_GIT_HASH "")
        set(QT_SBOM_GIT_HASH_SHORT "")
    endif()

    # If an explicit version was passed, override the git version and the path-safe git version
    # with the provided value.
    if(arg_VERSION)
        set(QT_SBOM_GIT_VERSION "${arg_VERSION}")
        set(QT_SBOM_GIT_VERSION_PATH "${arg_VERSION}")
    endif()

    # Store the explicit version, aka the non-git version, if provided, in a separate variable.
    # Allow overriding with the QT_SBOM_VERSION_OVERRIDE variable just in case, even if it's empty.
    # In case of a qt build, for compatibility we still read the QT_REPO_MODULE_VERSION variable
    if(DEFINED QT_SBOM_VERSION_OVERRIDE)
        set(explicit_version "${QT_SBOM_VERSION_OVERRIDE}")
    elseif(arg_VERSION)
        set(explicit_version "${arg_VERSION}")
    elseif(QT_REPO_MODULE_VERSION)
        set(explicit_version "${QT_REPO_MODULE_VERSION}")
    else()
        set(explicit_version "")
    endif()

    # If the git version hasn't been set yet, set it to the explicit version, if available.
    if(NOT QT_SBOM_GIT_VERSION)
        set(QT_SBOM_GIT_VERSION "${explicit_version}")
    endif()
    if(NOT QT_SBOM_GIT_VERSION_PATH)
        set(QT_SBOM_GIT_VERSION_PATH "${explicit_version}")
    endif()

    # Save the variables in a global property to later query them in other functions.
    set_property(GLOBAL PROPERTY QT_SBOM_GIT_VERSION "${QT_SBOM_GIT_VERSION}")
    set_property(GLOBAL PROPERTY QT_SBOM_GIT_VERSION_PATH "${QT_SBOM_GIT_VERSION_PATH}")
    set_property(GLOBAL PROPERTY QT_SBOM_GIT_HASH "${QT_SBOM_GIT_HASH}")
    set_property(GLOBAL PROPERTY QT_SBOM_GIT_HASH_SHORT "${QT_SBOM_GIT_HASH_SHORT}")
    set_property(GLOBAL PROPERTY QT_SBOM_EXPLICIT_VERSION "${explicit_version}")
endfunction()

# Queries the current project git version variables and sets them in the parent scope.
function(_qt_internal_sbom_get_git_version_vars)
    get_cmake_property(QT_SBOM_GIT_VERSION QT_SBOM_GIT_VERSION)
    get_cmake_property(QT_SBOM_GIT_VERSION_PATH QT_SBOM_GIT_VERSION_PATH)
    get_cmake_property(QT_SBOM_GIT_HASH QT_SBOM_GIT_HASH)
    get_cmake_property(QT_SBOM_GIT_HASH_SHORT QT_SBOM_GIT_HASH_SHORT)
    get_cmake_property(QT_SBOM_EXPLICIT_VERSION QT_SBOM_EXPLICIT_VERSION)

    set(QT_SBOM_GIT_VERSION "${QT_SBOM_GIT_VERSION}" PARENT_SCOPE)
    set(QT_SBOM_GIT_VERSION_PATH "${QT_SBOM_GIT_VERSION_PATH}" PARENT_SCOPE)
    set(QT_SBOM_GIT_HASH "${QT_SBOM_GIT_HASH}" PARENT_SCOPE)
    set(QT_SBOM_GIT_HASH_SHORT "${QT_SBOM_GIT_HASH_SHORT}" PARENT_SCOPE)
    set(QT_SBOM_EXPLICIT_VERSION "${QT_SBOM_EXPLICIT_VERSION}" PARENT_SCOPE)
endfunction()

# Queries the current project git version variables and sets them in the parent scope.
function(_qt_internal_sbom_get_project_explicit_version out_var)
    get_cmake_property(explicit_version QT_SBOM_EXPLICIT_VERSION)
    set(${out_var} "${explicit_version}" PARENT_SCOPE)
endfunction()

# Returns the configure line used to configure the current repo or top-level build, by reading
# the config.opt file that the configure script writes out.
# Returns an empty string if configure was not called, but CMake was called directly.
# If the build is reconfigured with bare CMake, the config.opt remains untouched, and thus
# the previous contents is returned.
function(_qt_internal_get_configure_line out_var)
    set(content "")

    if(QT_SUPERBUILD OR PROJECT_NAME STREQUAL "QtBase")
        set(configure_script_name "qt6/configure")
    elseif(PROJECT_NAME STREQUAL "QtBase")
        set(configure_script_name "qtbase/configure")
    else()
        _qt_internal_sbom_get_root_project_name_lower_case(repo_project_name_lowercase)
        set(configure_script_name "qt-configure-module <sources>/${repo_project_name_lowercase}")
    endif()

    if(QT_SUPERBUILD)
        set(config_opt_path "${PROJECT_BINARY_DIR}/../config.opt")
    else()
        set(config_opt_path "${PROJECT_BINARY_DIR}/config.opt")
    endif()

    if(NOT EXISTS "${config_opt_path}")
        message(DEBUG "Couldn't find config.opt file in ${config_opt} for argument extraction.")
        set(${out_var} "${content}" PARENT_SCOPE)
        return()
    endif()

    file(STRINGS "${config_opt_path}" args)
    list(JOIN args " " joined_args)

    set(content "${configure_script_name} ${joined_args}")
    string(STRIP "${content}" content)

    set(${out_var} "${content}" PARENT_SCOPE)
endfunction()

function(_qt_internal_sbom_compute_project_namespace out_var)
    set(opt_args "")
    set(single_args
        SUPPLIER_URL
        PROJECT_NAME
        VERSION_SUFFIX
    )
    set(multi_args "")

    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    if(NOT arg_PROJECT_NAME)
        message(FATAL_ERROR "PROJECT_NAME must be set")
    endif()

    if(NOT arg_SUPPLIER_URL)
        message(FATAL_ERROR "SUPPLIER_URL must be set")
    endif()

    string(TOLOWER "${arg_PROJECT_NAME}" project_name_lowercase)

    set(version_suffix "")

    if(arg_VERSION_SUFFIX)
        set(version_suffix "-${arg_VERSION_SUFFIX}")
    else()
        _qt_internal_sbom_get_git_version_vars()
        if(QT_SBOM_GIT_VERSION)
            set(version_suffix "-${QT_SBOM_GIT_VERSION}")
        endif()
    endif()

    # Used in external refs, it should be either aa URI + UUID or a URI + checksum.
    # We currently use a URI + git version, which is probably not conformant to the spec.
    set(repo_name_and_version "${project_name_lowercase}${version_suffix}")
    set(repo_spdx_namespace
        "${arg_SUPPLIER_URL}/spdxdocs/${repo_name_and_version}")

    set(${out_var} "${repo_spdx_namespace}" PARENT_SCOPE)
endfunction()

function(_qt_internal_sbom_compute_project_file_name out_var)
    set(opt_args
        SPDX_TAG_VALUE
        SPDX_JSON
        CYCLONEDX_JSON
        CYCLONEDX_TOML

        EXTENSION_JSON # deprecated, used by WebEngine
    )
    set(single_args
        PROJECT_NAME
        VERSION_SUFFIX
    )
    set(multi_args "")

    cmake_parse_arguments(PARSE_ARGV 1 arg "${opt_args}" "${single_args}" "${multi_args}")
    _qt_internal_validate_all_args_are_parsed(arg)

    if(NOT arg_PROJECT_NAME)
        message(FATAL_ERROR "PROJECT_NAME must be set")
    endif()

    if(NOT arg_SPDX_JSON
            AND NOT arg_SPDX_TAG_VALUE
            AND NOT arg_CYCLONEDX_TOML
            AND NOT arg_CYCLONEDX_JSON
            AND NOT arg_EXTENSION_JSON
        )
        message(FATAL_ERROR "One of the following options should be set: "
            "SPDX_TAG_VALUE, SPDX_JSON, CYCLONEDX_JSON, CYCLONEDX_TOML")
    endif()

    string(TOLOWER "${arg_PROJECT_NAME}" project_name_lowercase)

    set(version_suffix "")

    if(arg_VERSION_SUFFIX)
        set(version_suffix "-${arg_VERSION_SUFFIX}")
    elseif(QT_REPO_MODULE_VERSION)
        set(version_suffix "-${QT_REPO_MODULE_VERSION}")
    endif()

    if(arg_SPDX_TAG_VALUE)
        set(extension "spdx")
    elseif(arg_SPDX_JSON OR arg_EXTENSION_JSON)
        set(extension "spdx.json")
    elseif(arg_CYCLONEDX_TOML)
        set(extension "cdx.toml")
    elseif(arg_CYCLONEDX_JSON)
        set(extension "cdx.json")
    else()
        message(FATAL_ERROR "Unknown file extension for SBOM generation.")
    endif()

    set(result
        "${project_name_lowercase}${version_suffix}.${extension}")

    set(${out_var} "${result}" PARENT_SCOPE)
endfunction()