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
|
All expansion modules need to be imported through the [import](#import) interface.
### core.base.option
Commonly used to get the value of the xmake command parameter option, often used for plugin development.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [option.get](#option-get) | Get Parameter Option Value | >= 2.0.1 |
#### option.get
- Get parameter option values
Used to get parameter option values in plugin development, for example:
```lua
-- Import option module
import("core.base.option")
-- Plugin entry function
function main(...)
print(option.get("info"))
end
```
The above code gets the hello plugin and executes: `xmake hello --info=xxxx` The value of the `--info=` option passed in the command, and shows: `xxxx`
For task tasks or plugins that are not a main entry, you can use this:
```lua
task("hello")
on_run(function ())
import("core.base.option")
print(option.get("info"))
end)
```
### core.base.global
Used to get the configuration information of xmake global, that is, the value of the parameter option passed in `xmake g|global --xxx=val`.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [global.get](#global-get) | Get the specified configuration value | >= 2.0.1 |
| [global.load](#global-load) | Load Configuration | >= 2.0.1 |
| [global.directory](#global-directory) | Get Global Configuration Information Directory | >= 2.0.1 |
| [global.dump](#global-dump) | Print out all global configuration information | >= 2.0.1 |
<p class="tip">
Prior to version 2.1.5, it was `core.project.global`.
</p>
#### global.get
- Get the specified configuration value
Similar to [config.get](#config-get), the only difference is that this is obtained from the global configuration.
#### global.load
- Load configuration
Similar to [global.get](#global-get), the only difference is that this is loaded from the global configuration.
#### global.directory
- Get the global configuration information directory
The default is the `~/.config` directory.
#### global.dump
- Print out all global configuration information
The output is as follows:
```lua
{
clean = true
, ccache = "ccache"
, xcode_dir = "/Applications/Xcode.app"
}
```
### core.base.task
Used for task operations, generally used to call other task tasks in custom scripts and plug-in tasks.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [task.run](#task-run) | Run the specified task | >= 2.0.1 |
<p class="tip">
Prior to version 2.1.5, it was `core.project.task`.
</p>
#### task.run
- Run the specified task
Used to run tasks or plugins defined by [task](#task) in custom scripts, plugin tasks, for example:
```lua
task("hello")
on_run(function ()
print("hello xmake!")
end)
target("demo")
on_clean(function(target)
-- Import task module
import("core.base.task")
-- Run this hello task
task.run("hello")
end)
```
We can also increase parameter passing when running a task, for example:
```lua
task("hello")
on_run(function (arg1, arg2)
print("hello xmake: %s %s!", arg1, arg2)
end)
target("demo")
on_clean(function(target)
-- Import task
import("core.base.task")
-- {} This is used for the first option, which is set to null, where two arguments are passed in the last: arg1, arg2
task.run("hello", {}, "arg1", "arg2")
end)
```
The second argument to `task.run` is used to pass options from the command line menu instead of passing directly into the `function (arg, ...)` function entry, for example:
```lua
-- Import task
import("core.base.task")
-- Plugin entry
function main(...)
-- Run the built-in xmake configuration task, equivalent to: xmake f|config --plat=iphoneos --arch=armv7
task.run("config", {plat="iphoneos", arch="armv7"})
emd
```
### core.tool.linker
Linker related operations, often used for plugin development.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [linker.link](#linker-link) | Execute Link | >= 2.0.1 |
| [linker.linkcmd](#linker-linkcmd) | Get Link Command Line | >= 2.0.1 |
| [linker.linkargv](#linker-linkargv) | Get Link Command Line List | >= 2.1.5 |
| [linker.linkflags](#linker-linkflags) | Get LinksOptions | >= 2.0.1 |
| [linker.has_flags](#linker-has_flags) | Determine if the specified link option is supported | >= 2.1.5 |
#### linker.link
- Execute link
For the target, link the specified object file list to generate the corresponding target file, for example:
```lua
linker.link("binary", "cc", {"a.o", "b.o", "c.o"}, target:targetfile(), {target = target})
```
Where [target](#target) is the project target, here is passed in, mainly used to get the target-specific link options. For the project target object, see: [core.project.project](#core-project-project )
Of course, you can also not specify the target, for example:
```lua
linker.link("binary", "cc", {"a.o", "b.o", "c.o"}, "/tmp/targetfile")
```
The first parameter specifies the link type and currently supports: binary, static, shared
The second parameter tells the linker that it should be linked as the source file object, and what compiler source files are compiled with, for example:
| Second Parameter Value | Description |
| ------------ | ------------ |
| cc | c compiler |
| cxx | c++ compiler |
| mm | objc compiler |
| mxx | objc++ compiler |
| gc | go compiler |
| as | assembler |
| sc | swift compiler |
| rc | rust compiler |
| dc | dlang compiler |
Specifying different compiler types, the linker will adapt the most appropriate linker to handle the link, and if several languages support mixed compilation, you can pass in multiple compiler types at the same time, specifying that the linker chooses to support these hybrid compilations. The linker of the language performs link processing:
```lua
linker.link("binary", {"cc", "mxx", "sc"}, {"a.o", "b.o", "c.o"}, "/tmp/targetfile")
```
The above code tells the linker that the three object files a, b, c may be c, objc++, compiled by swift code. The linker will select the most suitable linker from the current system and toolchain to handle the link process. .
#### linker.linkcmd
- Get link command line string
Get the command line string executed in [linker.link](#linker-link) directly, which is equivalent to:
```lua
local cmdstr = linker.linkcmd("static", "cxx", {"a.o", "b.o", "c.o"}, target:targetfile(), {target = target})
```
Note: The extension part of ``target = target}` is optional. If the target object is passed, the generated link command will add the link option corresponding to this target configuration.
And you can also pass various configurations yourself, for example:
```lua
local cmdstr = linker.linkcmd("static", "cxx", {"a.o", "b.o", "c.o"}, target:targetfile(), {config = {linkdirs = "/usr/lib"}})
```
#### linker.linkargv
- Get a list of link command line arguments
A little different from [linker.linkcmd](#linker-linkcmd) is that this interface returns a list of parameters, table representation, more convenient to operate:
```lua
local program, argv = linker.linkargv("static", "cxx", {"a.o", "b.o", "c.o"}, target:targetfile(), {target = target})
```
The first value returned is the main program name, followed by the parameter list, and `os.args(table.join(program, argv))` is equivalent to `linker.linkcmd`.
We can also run it directly by passing the return value to [os.runv](#os-runv): `os.runv(linker.linkargv(..))`
#### linker.linkflags
- Get link options
Get the link option string part of [linker.linkcmd](#linker-linkcmd) without shellname and object file list, and return by array, for example:
```lua
local flags = linker.linkflags("shared", "cc", {target = target})
for _, flag in ipairs(flags) do
print(flag)
end
```
The returned array of flags is an array.
#### linker.has_flags
- Determine if the specified link option is supported
Although it can be judged by [lib.detect.has_flags](detect-has_flags), but the interface is more low-level, you need to specify the linker name.
This interface only needs to specify the target type of the target, the source file type, which will automatically switch to select the currently supported linker.
```lua
if linker.has_flags(target:targetkind(), target:sourcekinds(), "-L/usr/lib -lpthread") then
-- ok
end
```
### core.tool.compiler
Compiler related operations, often used for plugin development.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [compiler.compile](#compiler-compile) | Execute Compilation | >= 2.0.1 |
| [compiler.compcmd](#compiler-compcmd) | Get Compiler Command Line | >= 2.0.1 |
| [compiler.compargv](#compiler-compargv) | Get Compiled Command Line List | >= 2.1.5 |
| [compiler.compflags](#compiler-compflags) | Get Compilation Options | >= 2.0.1 |
| [compiler.has_flags](#compiler-has_flags) | Determine if the specified compilation option is supported | >= 2.1.5 |
| [compiler.features](#compiler-features) | Get all compiler features | >= 2.1.5 |
| [compiler.has_features](#compiler-has_features) | Determine if the specified compilation feature is supported | >= 2.1.5 |
#### compiler.compile
- Perform compilation
For the target, link the specified object file list to generate the corresponding target file, for example:
```lua
compiler.compile("xxx.c", "xxx.o", "xxx.h.d", {target = target})
```
Where [target](#target) is the project target, here is the specific compile option that is mainly used to get the taeget. If you get the project target object, see: [core.project.project](#core-project-project)
The `xxx.h.d` file is used to store the header file dependency file list for this source file. Finally, these two parameters are optional. You can not pass them when compiling:
```lua
compiler.compile("xxx.c", "xxx.o")
```
To simply compile a source file.
#### compiler.compcmd
- Get the compile command line
Get the command line string executed directly in [compiler.compile](#compiler-compile), which is equivalent to:
```lua
local cmdstr = compiler.compcmd("xxx.c", "xxx.o", {target = target})
```
Note: The extension part of ``target = target}` is optional. If the target object is passed, the generated compile command will add the link option corresponding to this target configuration.
And you can also pass various configurations yourself, for example:
```lua
local cmdstr = compiler.compcmd("xxx.c", "xxx.o", {config = {includedirs = "/usr/include", defines = "DEBUG"}})
```
With target, we can export all source file compilation commands for the specified target:
```lua
import("core.project.project")
for _, target in pairs(project.targets()) do
for sourcekind, sourcebatch in pairs(target:sourcebatches()) do
for index, objectfile in ipairs(sourcebatch.objectfiles) do
local cmdstr = compiler.compcmd(sourcebatch.sourcefiles[index], objectfile, {target = target})
end
end
end
```
#### compiler.compargv
- Get compiled command line list
A little different from [compiler.compargv](#compiler-compargv) is that this interface returns a list of parameters, table representation, more convenient to operate:
```lua
local program, argv = compiler.compargv("xxx.c", "xxx.o")
```
#### compiler.compflags
- Get compilation options
Get the compile option string part of [compiler.compcmd](#compiler-compcmd) without shList of ellnames and files, for example:
```lua
local flags = compiler.compflags(sourcefile, {target = target})
for _, flag in ipairs(flags) do
print(flag)
end
```
The returned array of flags is an array.
#### compiler.has_flags
- Determine if the specified compilation option is supported
Although it can be judged by [lib.detect.has_flags](detect-has_flags), but the interface is more low-level, you need to specify the compiler name.
This interface only needs to specify the language type, it will automatically switch to select the currently supported compiler.
```lua
-- Determine if the c language compiler supports the option: -g
if compiler.has_flags("c", "-g") then
-- ok
end
-- Determine if the C++ language compiler supports the option: -g
if compiler.has_flags("cxx", "-g") then
-- ok
end
```
#### compiler.features
- Get all compiler features
Although it can be obtained by [lib.detect.features](detect-features), but the interface is more low-level, you need to specify the compiler name.
This interface only needs to specify the language type, it will automatically switch to select the currently supported compiler, and then get the current list of compiler features.
```lua
-- Get all the features of the current c compiler
local features = compiler.features("c")
-- Get all the features of the current C++ language compiler, enable the C++11 standard, otherwise you will not get the new standard features.
local features = compiler.features("cxx", {cofnig = {cxxflags = "-std=c++11"}})
-- Get all the features of the current C++ language compiler, pass all configuration information of the project target
local features = compiler.features("cxx", {target = target, config = {defines = "..", includedirs = ".."}})
```
A list of all c compiler features:
| Feature Name |
| --------------------- |
| c_static_assert |
| c_restrict |
| c_variadic_macros |
| c_function_prototypes |
A list of all C++ compiler features:
| Feature Name |
| ------------------------------------ |
| cxx_variable_templates |
| cxx_relaxed_constexpr |
| cxx_aggregate_default_initializers |
| cxx_contextual_conversions |
| cxx_attribute_deprecated |
| cxx_decltype_auto |
| cxx_digit_separators |
| cxx_generic_lambdas |
| cxx_lambda_init_captures |
| cxx_binary_literals |
| cxx_return_type_deduction |
| cxx_decltype_incomplete_return_types |
| cxx_reference_qualified_functions |
| cxx_alignof |
| cxx_attributes |
| cxx_inheriting_constructors |
| cxx_thread_local |
| cxx_alias_templates |
| cxx_delegating_constructors |
| cxx_extended_friend_declarations |
| cxx_final |
| cxx_nonstatic_member_init |
| cxx_override |
| cxx_user_literals |
| cxx_constexpr |
| cxx_defaulted_move_initializers |
| cxx_enum_forward_declarations |
| cxx_noexcept |
| cxx_nullptr |
| cxx_range_for |
| cxx_unrestricted_unions |
| cxx_explicit_conversions |
| cxx_lambdas |
| cxx_local_type_template_args |
| cxx_raw_string_literals |
| cxx_auto_type |
| cxx_defaulted_functions |
| cxx_deleted_functions |
| cxx_generalized_initializers |
| cxx_inline_namespaces |
| cxx_sizeof_member |
| cxx_strong_enums |
| cxx_trailing_return_types |
| cxx_unicode_literals |
| cxx_uniform_initialization |
| cxx_variadic_templates |
| cxx_decltype |
| cxx_default_function_template_args |
| cxx_long_long_type |
| cxx_right_angle_brackets |
| cxx_rvalue_references |
| cxx_static_assert |
| cxx_extern_templates |
| cxx_func_identifier |
| cxx_variadic_macros |
| cxx_template_template_parameters |
#### compiler.has_features
- Determine if the specified compiler feature is supported
Although it can be obtained by [lib.detect.has_features](detect-has-features), but the interface is more low-level, you need to specify the compiler name.
And this interface only needs to specify the special name list that needs to be detected, it can automatically switch to select the currently supported compiler, and then determine whether the specified feature is supported in the current compiler.
```lua
if compiler.has_features("c_static_assert") then
-- ok
end
if compiler.has_features({"c_static_assert", "cxx_constexpr"}, {languages = "cxx11"}) then
-- ok
end
if compiler.has_features("cxx_constexpr", {target = target, defines = "..", includedirs = ".."}) then
-- ok
end
```
For specific feature names, refer to [compiler.features](#compiler-features).
### core.project.config
Used to get the configuration information when the project is compiled, that is, the value of the parameter option passed in `xmake f|config --xxx=val`.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [config.get](#config-get) | Get the specified configuration value | >= 2.0.1 |
| [config.load](#config-load) | Load Configuration | >= 2.0.1 |
| [config.arch](#config-arch) | Get the schema configuration of the current project | >= 2.0.1 |
| [config.plat](#config-plat) | Get the platform configuration of the current project | >= 2.0.1 |
| [config.mode](#config-mode) | Get the compilation mode configuration of the current project | >= 2.0.1 |
| [config.buildir](#config-buildir) | Get the output directory configuration of the current project | >= 2.0.1 |
| [config.directory](#config-dIrectory) | Get the configuration information directory of the current project | >= 2.0.1 |
| [config.dump](#config-dump) | Print out all configuration information for the current project | >= 2.0.1 |
#### config.get
- Get the specified configuration value
Used to get the configuration value of `xmake f|config --xxx=val`, for example:
```lua
target("test")
on_run(function (target)
-- Import configuration module
import("core.project.config")
-- Get configuration values
print(config.get("xxx"))
end)
```
#### config.load
- Load configuration
Generally used in plug-in development, the plug-in task is not like the custom script of the project, the environment needs to be initialized and loaded by itself, the default project configuration is not loaded, if you want to use [config.get](#config-get) interface to get the project Configuration, then you need to:
```lua
-- Import configuration module
import("core.project.config")
function main(...)
-- Load project configuration first
config.load()
-- Get configuration values
print(config.get("xxx"))
end
```
#### config.arch
- Get the schema configuration of the current project
That is to get the platform configuration of `xmake f|config --arch=armv7`, which is equivalent to `config.get("arch")`.
#### config.plat
- Get the platform configuration of the current project
That is to get the platform configuration of `xmake f|config --plat=iphoneos`, which is equivalent to `config.get("plat")`.
#### config.mode
- Get the compilation mode configuration of the current project
That is to get the platform configuration of `xmake f|config --mode=debug`, which is equivalent to `config.get("mode")`.
#### config.buildir
- Get the output directory configuration of the current project
That is to get the platform configuration of `xmake f|config -o /tmp/output`, which is equivalent to `config.get("buildir")`.
#### config.directory
- Get the configuration information directory of the current project
Get the storage directory of the project configuration, the default is: `projectdir/.config`
#### config.dump
- Print out all configuration information of the current project
The output is for example:
```lua
{
sh = "xcrun -sdk macosx clang++"
, xcode_dir = "/Applications/Xcode.app"
, ar = "xcrun -sdk macosx ar"
, small = true
, object = false
, arch = "x86_64"
, xcode_sdkver = "10.12"
, ex = "xcrun -sdk macosx ar"
, cc = "xcrun -sdk macosx clang"
, rc = "rustc"
, plat = "macosx"
, micro = false
, host = "macosx"
, as = "xcrun -sdk macosx clang"
, dc = "dmd"
, gc = "go"
, openssl = false
, ccache = "ccache"
, cxx = "xcrun -sdk macosx clang"
, sc = "xcrun -sdk macosx swiftc"
, mm = "xcrun -sdk macosx clang"
, buildir = "build"
, mxx = "xcrun -sdk macosx clang++"
, ld = "xcrun -sdk macosx clang++"
, mode = "release"
, kind = "static"
}
```
### core.project.global
<p class="tip">
This module was migrated to [core.base.global](#core-base-global) since version 2.1.5.
</p>
### core.project.task
<p class="tip">
This module has been migrated to [core.base.task](#core-base-task) since version 2.1.5.
</p>
### core.project.project
Used to get some description information of the current project, that is, the configuration information defined in the `xmake.lua` project description file, for example: [target](#target), [option](#option), etc.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------------------- |
| [project.load](#project-load) | Load Project Configuration | >= 2.0.1 (2.1.5 Obsolete) |
| [project.directory](#project-directory) | Get Project Directory | >= 2.0.1 |
| [project.target](#project-target) | Get the specified project target object | >= 2.0.1 |
| [project.targets](#project-targets) | Get the list of project target objects | >= 2.0.1 |
| [project.option](#project-option) | Get the specified option object | >= 2.1.5 |
| [project.options](#project-options) | Get all option objects for the project | >= 2.1.5 |
| [project.name](#project-name) | Get current project name | >= 2.0.1 |
| [project.version](#project-version) | Get current project version number | >= 2.0.1 |
#### project.load
- Load project description configuration
It is only used in the plugin, because the project configuration information has not been loaded at this time. In the custom script of the project target, you do not need to perform this operation, you can directly access the project configuration.
```lua
-- Import engineering modules
import("core.project.project")
-- Plugin entry
function main(...)
-- Load project description configuration
project.load()
-- access project descriptions, such as getting specified project goals
local target = project.target("test")
end
```
<p class="tip">
After version 2.1.5, if not needed, the project load will automatically load at the appropriate time.
</p>
#### project.directory
- Get the project directory
Get the current project directory, which is the directory specified in `xmake -P xxx`, otherwise it is the default current `xmake` command execution directory.
<p class="tip">
After version 2.1.5, it is recommended to use [os.projectdir](#os-projectdir) to get it.
</p>
#### project.target
- Get the specified project target object
Get and access the specified project target configuration, for example:
```lua
local target = project.target("test")
if target then
-- Get the target file name
print(target:targetfile())
-- Get the target type, which is: binary, static, shared
print(target:targetkind())
-- Get the target name
print(target:name())
-- Get the target source file
local sourcefiles = target:sourcefiles()
-- Get a list of target installation header files
local srcheaders, dstheaders = target:headerfiles()
-- Get target dependencies
print(target:get("deps"))
end
```
#### project.targets
- Get a list of project target objects
Returns all compilation targets for the current project, for example:
```lua
for targetname, target in pairs(project.targets())
print(target:targetfile())
end
```
#### project.option
- Get the specified option object
Get and access the option objects specified in the project, for example:
```lua
local option = project.option("test")
if option:enabled() then
option:enable(false)
end
```
#### project.options
- Get all project option objects
Returns all compilation targets for the current project, for example:
```lua
for optionname, option in pairs(project.options())
print(option:enabled())
end
```
#### project.name
- Get the current project name
That is, get the project name configuration of [set_project](#set_project).
```lua
print(project.name())
```
#### project.version
- Get the current project version number
That is, get [set_version](#set_version) project version configuration.
```lua
print(project.version())
```
### core.language.language
Used to obtain information about the compiled language, generally used for the operation of code files.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [language.extensions](#language-extensions) | Get a list of code suffixes for all languages | >= 2.1.1 |
| [language.targetkinds](#language-targetkinds) | Get a list of target types for all languages | >= 2.1.1 |
| [language.sourcekinds](#language-sourcekinds) | Get a list of source file types for all languages | >= 2.1.1 |
| [language.sourceflags](#language-sourceflags) | Load source file compilation option name list for all languages | >= 2.1.1 |
| [language.load](#language-load) | Load the specified language | >= 2.1.1 |
| [language.load_sk](#language-load_sk) | Load the specified language from the source file type | >= 2.1.1 |
| [language.load_ex](#language-load_ex) | Load the specified language from the source file suffix | >= 2.1.1 |
| [language.sourcekind_of](#language-sourcekind_of) | Get the source file type of the specified source file | >= 2.1.1 |
#### language.extensions
- Get a list of code suffixes for all languages
The results are as follows:
```lua
{
[".c"] = cc
, [".cc"] = cxx
, [".cpp"] = cxx
, [".m"] = mm
, [".mm"] = mxx
, [".swift"] = sc
, [".go"] = gc
}
```
#### language.targetkinds
- Get a list of target types in all languages
The results are as follows:
```lua
{
binary = {"ld", "gc-ld", "dc-ld"}
, static = {"ar", "gc-ar", "dc-ar"}
, shared = {"sh", "dc-sh"}
}
```
#### language.sourcekinds
- Get a list of source file types in all languages
The results are as follows:
```lua
{
cc = ".c"
, cxx = {".cc", ".cpp", ".cxx"}
, mm = ".m"
, mxx = ".mm"
, sc = ".swift"
, gc = ".go"
, rc = ".rs"
, dc = ".d"
, as = {".s", ".S", ".asm"}
}
```
#### language.sourceflags
- Load a list of source file compilation option names for all languages
The results are as follows:
```lua
{
cc = {"cflags", "cxflags"}
, cxx = {"cxxflags", "cxflags"}
, ...
}
```
#### language.load
- Load the specified language
Load a specific language object from the language name, for example:
```lua
local lang = language.load("c++")
if lang then
print(lang:name())
end
```
#### language.load_sk
- Load the specified language from the source file type
Load specific language objects from the source file type: `cc, cxx, mm, mxx, sc, gc, as ..`, for example:
```lua
local lang = language.load_sk("cxx")
if lang then
print(lang:name())
end
```
#### language.load_ex
- Load the specified language from the source file suffix name
Load specific language objects from the source file extension: `.cc, .c, .cpp, .mm, .swift, .go ..`, for example:
```lua
local lang = language.load_sk(".cpp")
if lang then
print(lang:name())
end
```
#### language.sourcekind_of
- Get the source file type of the specified source file
That is, from a given source file path, get the type of source file it belongs to, for example:
```lua
print(language.sourcekind_of("/xxxx/test.cpp"))
```
The result is: `cxx`, which is the `c++` type. For the corresponding list, see: [language.sourcekinds](#language-sourcekinds)
### core.platform.platform
Platform information related operations
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [platform.get](#platform-get) | Get configuration information about the specified platform | >= 2.0.1 |
#### platform.get
- Get configuration information about the specified platform
Get the information set in the platform configuration `xmake.lua`, which is generally only used when writing plugins, for example:
```lua
-- Get all support architectures for the current platform
print(platform.get("archs"))
-- Get the target file format information of the specified iphoneos platform
local formats = platform.get("formats", "iphoneos")
table.dump(formats)
```
For specific readable platform configuration information, please refer to: [platform](#platform)
### core.platform.environment
Environment-related operations, used to enter and leave the terminal environment corresponding to the specified environment variables, generally used for the entry and departure of the `path` environment, especially some build tools that require a specific environment, such as: msvc toolchain.
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [environment.enter](#environment-enter) | Enter the specified environment | >= 2.0.1 |
| [environment.leave](#environment-leave) | Leave the specified environment | >= 2.0.1 |
The currently supported environments are:
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| toolchains | Toolchain Execution Environment | >= 2.0.1 |
#### environment.enter
- Enter the specified environment
Enter the specified environment, for example, msvc has its own environment variable environment for running build tools, such as: `cl.exe`, `link.exe`, these time you want to run them in xmake, you need:
```lua
-- Enter the toolchain environment
environment.enter("toolchains")
-- At this time, run cl.exe to run normally. At this time, environment variables such as path will enter the environment mode of msvc.
os.run("cl.exe ..")
-- leaving the toolchain environment
environment.leave("toolchains")
```
Therefore, for the sake of versatility, the default xmake compiler will set this environment. Under Linux, basically the internal environment does not need special switching. At present, only msvc under Windows is processed.
#### environment.leave
- leaving the designated environment
For specific use, see: [environment.enter](#environment-enter)
### lib.detect
This module provides very powerful probing capabilities for probing programs, compilers, language features, dependencies, and more.
<p class="tip">
The interface of this module is spread across multiple module directories, try to import it by importing a single interface, which is more efficient, for example: `import("lib.detect.find_package")` instead of `import("lib.detect ") `Import all to call.
</p>
| Interface | Description | Supported Versions |
| --------------------------------------------------- | -------------------------------------------- | -------------------- |
| [detect.find_file](#detect-find_file) | Find Files | >= 2.1.5 |
| [detect.find_path](#detect-find_path) | Find File Path | >= 2.1.5 |
| [detect.find_library](#detect-find_library) | Find Library Files | >= 2.1.5 |
| [detect.find_program](#detect-find_program) | Find executables | >= 2.1.5 |
| [detect.find_programver](#detect-find_programver) | Find executable version number | >= 2.1.5 |
| [detect.find_package](#detect-find_package) | Find package files, including library files and search paths | >= 2.1.5 |
| [detect.find_tool](#detect-find_tool) | Find Tool | >= 2.1.5 |
| [detect.find_toolname](#detect-find_toolname) | Find Tool Name | >= 2.1.5 |
| [detect.find_cudadevices](#detect-find_cudadevices) | Find CUDA devices of the host | >= 2.2.7 |
| [detect.features](#detect-features) | Get all the features of the specified tool | >= 2.1.5 |
| [detect.has_features](#detect-has_features) | Determine if the specified feature is supported | >= 2.1.5 |
| [detect.has_flags](#detect-has_flags) | Determine if the specified parameter options are supported | >= 2.1.5 |
| [detect.has_cfuncs](#detect-has_cfuncs) | Determine if the specified c function exists | >= 2.1.5 |
| [detect.has_cxxfuncs](#detect-has_cxxfuncs) | Determine if the specified c++ function exists | >= 2.1.5 |
| [detect.has_cincludes](#detect-has_cincludes) | Determine if the specified c header file exists | >= 2.1.5 |
| [detect.has_cxxincludess](#detect-has_cxxincludes) | Determine if the specified c++ header file exists | >= 2.1.5 |
| [detect.has_ctypes](#detect-has_ctypes) | Determine if the specified c type exists | >= 2.1.5 |
| [detect.has_cxxtypes](#detect-has_cxxtypes) | Determine if the specified c++ type exists | >= 2.1.5 |
| [detect.check_cxsnippets](#detect-check_cxsnippets) | Check if c/c++ code snippets can be compiled by | >= 2.1.5 |
#### detect.find_file
- Find files
This interface provides a more powerful project than [os.files](#os-files), which can specify multiple search directories at the same time, and can also specify additional subdirectories for each directory to match the pattern lookup, which is equivalent to [ An enhanced version of os.files](#os-files).
E.g:
```lua
import("lib.detect.find_file")
local file = find_file("ccache", { "/usr/bin", "/usr/local/bin"})
```
If found, the result returned is: `/usr/bin/ccache`
It also supports pattern matching paths for recursive lookups, similar to `os.files`:
```lua
local file = find_file("test.h", { "/usr/include", "/usr/local/include/**"})
```
Not only that, but the path inside also supports built-in variables to get the path from the environment variables and the registry to find:
```lua
local file = find_file("xxx.h", { "$(env PATH)", "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name)"})
```
If the path rules are more complex, you can also dynamically generate path entries through a custom script:
```lua
local file = find_file("xxx.h", { "$(env PATH)", function () return val("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name"):match ("\"(.-)\"") end})
```
In most cases, the above use has met various needs. If you need some extended functions, you can customize some optional configurations by passing in the third parameter, for example:
```lua
local file = find_file("test.h", { "/usr", "/usr/local"}, {suffixes = {"/include", "/lib"}})
```
By specifying a list of suffixes subdirectories, you can extend the list of paths (the second parameter) so that the actual search directory is expanded to:
```
/usr/include
/usr/lib
/usr/local/include
/usr/local/lib
```
And without changing the path list, you can dynamically switch subdirectories to search for files.
<p class="tip">
We can also quickly call and test this interface with the `xmake lua` plugin: `xmake lua lib.detect.find_file test.h /usr/local`
</p>
#### detect.find_path
- Find the path
The usage of this interface is similar to [lib.detect.find_file](#detect-find_file), the only difference is that the returned results are different.
After the interface finds the incoming file path, it returns the corresponding search path, not the file path itself. It is generally used to find the parent directory location corresponding to the file.
```lua
import("lib.detect.find_path")
local p = find_path("include/test.h", { "/usr", "/usr/local"})
```
If the above code is successful, it returns: `/usr/local`, if `test.h` is in `/usr/local/include/test.h`.
Another difference is that this interface is passed in not only the file path, but also the directory path to find:
```lua
local p = find_path("lib/xxx", { "$(env PATH)", "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name)"})
```
Again, this interface also supports pattern matching and suffix subdirectories:
```lua
local p = find_path("include/*.h", { "/usr", "/usr/local/**"}, {suffixes = "/subdir"})
```
#### detect.find_library
- Find library files
This interface is used to find library files (static libraries, dynamic libraries) in the specified search directory, for example:
```lua
import("lib.detect.find_library")
local library = find_library("crypto", {"/usr/lib", "/usr/local/lib"})
```
Running on macosx, the results returned are as follows:
```lua
{
filename = libcrypto.dylib
, linkdir = /usr/lib
, link = crypto
, kind = shared
}
```
If you do not specify whether you need a static library or a dynamic library, then this interface will automatically select an existing library (either a static library or a dynamic library) to return.
If you need to force the library type you need to find, you can specify the kind parameter as (`static/shared`):
```lua
local library = find_library("crypto", {"/usr/lib", "/usr/local/lib"}, {kind = "static"})
```
This interface also supports suffixes suffix subdirectory search and pattern matching operations:
```lua
local library = find_library("cryp*", {"/usr", "/usr/local"}, {suffixes = "/lib"})
```
#### detect.find_program
- Find executable programs
This interface is more primitive than [lib.detect.find_tool](#detect-find_tool), looking for executables through the specified parameter directory.
```lua
import("lib.detect.find_program")
local program = find_program("ccache")
```
The above code is like not passing the search directory, so it will try to execute the specified program directly. If it runs ok, it will return directly: `ccache`, indicating that the search is successful.
Specify the search directory and modify the test command parameters that are attempted to run (default: `ccache --version`):
```lua
localProgram = find_program("ccache", {pathes = {"/usr/bin", "/usr/local/bin"}, check = "--help"})
```
The above code will try to run: `/usr/bin/ccache --help`, if it runs successfully, it returns: `/usr/bin/ccache`.
If `--help` can't satisfy the requirement, some programs don't have the `--version/--help` parameter, then you can customize the run script to run the test:
```lua
local program = find_program("ccache", {pathes = {"/usr/bin", "/usr/local/bin"}, check = function (program) os.run("%s -h", program) end })
```
Similarly, the search path list supports built-in variables and custom scripts:
```lua
local program = find_program("ccache", {pathes = {"$(env PATH)", "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug;Debugger)"}})
local program = find_program("ccache", {pathes = {"$(env PATH)", function () return "/usr/local/bin" end}})
```
<p class="tip">
In order to speed up the efficiency of frequent lookups, this interface comes with a default cache, so even if you frequently find the same program, it will not take too much time.
If you want to disable the cache, you can clear the local cache by executing `xmake f -c` in the project directory.
</p>
We can also test quickly with `xmake lua lib.detect.find_program ccache`.
#### detect.find_programver
- Find the executable version number
```lua
import("lib.detect.find_programver")
local programver = find_programver("ccache")
```
The return result is: 3.2.2
By default it will try to get the version via `ccache --version`. If this parameter doesn't exist, you can specify other parameters yourself:
```lua
local version = find_programver("ccache", {command = "-v"})
```
Even the custom version gets the script:
```lua
local version = find_programver("ccache", {command = function () return os.iorun("ccache --version") end})
```
For the extraction rule of the version number, if the built-in matching mode does not meet the requirements, you can also customize:
```lua
local version = find_programver("ccache", {command = "--version", parse = "(%d+%.?%d*%.?%d*.-)%s"})
local version = find_programver("ccache", {command = "--version", parse = function (output) return output:match("(%d+%.?%d*%.?%d*.-)%s ") end})
```
<p class="tip">
In order to speed up the efficiency of frequent lookups, this interface is self-contained by default. If you want to disable the cache, you can execute `xmake f -c` in the project directory to clear the local cache.
</p>
We can also test quickly with `xmake lua lib.detect.find_programver ccache`.
#### detect.find_package
- Find package files
This interface is also used to find library files, but it is higher than [lib.detect.find_library](#detect-find_library), and it is more powerful and easy to use, because it is based on the strength of the package.
So what is a complete package, it contains:
1. Multiple static libraries or dynamic library files
2. Library search directory
3. Search directory for header files
4. Optional compile link options, such as `defines`
5. Optional version number
For example, we look for an openssl package:
```lua
import("lib.detect.find_package")
local package = find_package("openssl")
```
The returned results are as follows:
```lua
{links = {"ssl", "crypto", "z"}, linkdirs = {"/usr/local/lib"}, includedirs = {"/usr/local/include"}}
```
If the search is successful, return a table containing all the package information, if it fails, return nil
The return result here can be directly passed as the parameter of `target:add`, `option:add`, which is used to dynamically increase the configuration of `target/option`:
```lua
option("zlib")
set_showmenu(true)
before_check(function (option)
import("lib.detect.find_package")
option:add(find_package("zlib"))
end)
```
```lua
target("test")
on_load(function (target)
import("lib.detect.find_package")
target:add(find_package("zlib"))
end)
```
If third-party tools such as `homebrew`, `pkg-config` are installed on the system, then this interface will try to use them to improve the search results.
We can also choose to find the package of the specified version by specifying the version number (if the package does not get the version information or there is no matching version of the package, then return nil):
```lua
local package = find_package("openssl", {version = "1.0.1"})
```
The packages that are looked up by default are matched to the platform, architecture, and mode according to the following rules:
1. If the parameter passed in specifies `{plat = "iphoneos", arch = "arm64", mode = "release"}`, then match first, for example: `find_package("openssl", {plat = "iphoneos"} )`.
2. If there is a configuration file in the current project environment, try to get it from `config.get("plat")`, `config.get("arch")` and `config.get("mode")` The platform architecture is matched.
3. Finally, the matching is done from `os.host()` and `os.arch()`, which is the platform architecture environment of the current host.
If the system's library directory and `pkg-config` are not enough to meet the requirements and the package cannot be found, you can manually set the search path yourself:
```lua
local package = find_package("openssl", {linkdirs = {"/usr/lib", "/usr/local/lib"}, includedirs = "/usr/local/include"})
```
You can also specify the link name you want to search at the same time, the header file name:
```lua
local package = find_package("openssl", {links = {"ssl", "crypto"}, includes = "ssl.h"}})
```
You can even specify xmake's `packagedir/*.pkg` package directory to find the corresponding `openssl.pkg` package, which is typically used to find local packages built into the project directory.
For example, the tbox project has a built-in `pkg/openssl.pkg` local package project. We can use the following script to pass in the `{packagedirs = ""}` parameter to find the local package first. If it can't find it, go to the system. package.
```lua
target("test")
on_load(function (target)
import("lib.detect.find_package")
target:add(find_package("openssl", {packagedirs = path.join(os.projectdir(), "pkg")}))
end)
```
To summarize, the current search order:
1. If you specify the `{packagedirs = ""}` parameter, look for the local package `*.pkg` from the path specified by this parameter.
2. If there is a `detect.packages.find_xxx` script under `xmake/modules`, try calling this script to improve the lookup results.
3. If vcpkg exists in the system, obtain the package from the vcpkg package management system.
4. If the system has `pkg-config` and you are looking for a library for the system environment, try to find it using the path and link information provided by `pkg-config`
5. If the system has `homebrew` and you are looking for a library for the system environment, try to find it using the information provided by `brew --prefix xxx`
6. Find from the pathes path specified in the parameter and some known system paths `/usr/lib`, `/usr/include`
Here we need to focus on the second point, through the `detect.packages.find_xxx` script to improve the search results, many times automatic packet detection is unable to fully detect the package path,
Especially for the windows platform, there is no default library directory, and there is no package management app. When many libraries are installed, they are placed in the system directory, or add a registry key.
Therefore, there is no uniform rule for finding it. At this time, you can customize a search script to improve the lookup mechanism of `find_package` and perform more accurate search for the specified package.
In the xmake/modules/detect/packages` directory that comes with xmake, there are already many built-in package scripts for better lookup support for commonly used packages.
Of course, this is not enough for all users. If the package that the user needs is still not found, then you can define a search script yourself, for example:
To find a package named `openssl`, you can write a script for `find_openssl.lua` placed in the project directory:
```
Projectdir
- xmake
- modules
- detect/package/find_openssl.lua
```
Then specify the directory for this module at the beginning of the project's `xmake.lua` file:
```lua
add_moduledirs("$(projectdir)/xmake/modules")
```
This way xmake will be able to find custom extensions.
Next we look at the implementation of `find_openssl.lua`:
```lua
-- imports
import("lib.detect.find_path")
import("lib.detect.find_library")
-- find openssl
--
-- @param opt the package options. e.g. see the options of find_package()
--
-- @return see the return value of find_package()
--
function main(opt)
-- for windows platform
--
-- http://www.slproweb.com/products/Win32OpenSSL.html
--
if opt.plat == "windows" then
-- init bits
local bits = ifelse(opt.arch == "x64", "64", "32")
-- init search pathes
local pathes = {"$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL %(" .. bits .. "-bit%)_is1;Inno Setup: App Path)",
"$(env PROGRAMFILES)/OpenSSL",
"$(env PROGRAMFILES)/OpenSSL-Win" .. bits,
"C:/OpenSSL",
"C:/OpenSSL-Win" .. bits}
-- find library
local result = {links = {}, linkdirs = {}, includedirs = {}}
for _, name in ipairs({"libssl", "libcrypto"}) do
local linkinfo = find_library(name, pathes, {suffixes = "lib"})
if linkinfo then
table.insert(result.links, linkinfo.link)
table.insert(result.linkdirs, linkinfo.linkdir)
end
end
-- not found?
if #result.links ~= 2 then
return
end
-- find include
table.insert(result.includedirs, find_path("openssl/ssl.h", pathes, {suffixes = "include"}))
-- ok
return result
end
end
```
Inside the windows platform to read the registry, to find the
specified library file, the bottom layer is actually called
[find_library](#detect-find_library) and other interfaces.
<p class="tip"> In order to speed up the efficiency of frequent
lookups, this interface is self-contained by default. If you want to
disable the cache, you can execute `xmake f -c` in the project
directory to clear the local cache. You can also disable the cache by
specifying the force parameter, forcing a re-find:
`find_package("openssl", {force = true})` </p>
We can also test quickly with `xmake lua lib.detect.find_package
openssl`.
After the 2.2.5 version, the built-in interface [find_packages]
(#find_packages) has been added, which can find multiple packages at
the same time, and can be used directly without importing by import.
And after this release, support for explicitly looking for packages
from a specified third-party package manager, for example:
```lua find_package("brew::pcre2/libpcre2-8") ```
Since the package name of each third-party package manager is not
completely consistent, for example, pcre2 has three library versions
in homebrew, we can specify the library corresponding to libpcre2-8
version by the above method.
In addition, for vcpkg, conan can also specify the library inside by
adding the `vcpkg::`, `conan::` package namespace.
#### detect.find_tool
- Find tool
This interface is also used to find executable programs, but more
advanced than [lib.detect.find_program](#detect-find_program), the
function is also more powerful, it encapsulates the executable
program, providing the concept of tools:
* toolname: tool name, short for executable program, used to mark a
* tool, for example: `gcc`, `clang`, etc. program: executable program
* command, for example: `xcrun -sdk macosx clang`
The corresponding relationship is as follows:
| toolname | program |
| --------- | ----------------------------------- |
| clang | `xcrun -sdk macosx clang` |
| gcc | `/usr/toolchains/bin/arm-linux-gcc` |
| link | `link.exe -lib` |
[lib.detect.find_program](#detect-find_program) can only determine
whether the program exists by passing in the original program command
or path. And `find_tool` can find the tool through a more consistent
toolname, and return the corresponding program complete command path,
for example:
```lua
import("lib.detect.find_tool")
local tool = find_tool("clang")
```
The result returned is: `{name = "clang", program = "clang"}`, at this
time there is no difference, we can manually specify the executable
command:
```lua
local tool = find_tool("clang", {program = "xcrun -sdk macosx clang"})
```
The result returned is: `{name = "clang", program = "xcrun -sdk macosx
clang"}`
In macosx, gcc is clang. If we execute `gcc --version`, we can see
that it is a vest of clang. We can intelligently identify it through
the `find_tool` interface:
```lua
local tool = find_tool("gcc")
```
The result returned is: `{name = "clang", program = "gcc"}`
The difference can be seen by this result. The tool name will actually
be marked as clang, but the executable command uses gcc.
We can also specify the `{version = true}` parameter to get the
version of the tool, and specify a custom search path. It also
supports built-in variables and custom scripts:
```lua
local tool = find_tool("clang", {version = true, {pathes = {"/usr/bin", "/usr/local/bin", "$(env PATH)", function () return "/usr/xxx/bin" end}})
```
The result returned is: `{name = "clang", program = "/usr/bin/clang",
version = "4.0"}`
This interface is a high-level wrapper around `find_program`, so it
also supports custom script detection:
```lua
local tool = find_tool("clang", {check = "--help"})
local tool = find_tool("clang", {check = function (tool) os.run("%s -h", tool) end})
```
Finally, the search process of `find_tool`:
1. First try to run and detect with the argument of `{program =
"xxx"}`. 2. If there is a `detect.tools.find_xxx` script in
`xmake/modules/detect/tools`, call this script for more accurate
detection. 3. Try to detect from the system directory such as
`/usr/bin`, `/usr/local/bin`.
We can also add a custom lookup script to the module directory
specified by `add_moduledirs` in the project `xmake.lua` to improve
the detection mechanism:
```
projectdir
- xmake/modules
- detect/tools/find_xxx.lua
```
For example, we customize a lookup script for `find_7z.lua`:
```lua
import("lib.detect.find_program")
import("lib.detect.find_programver")
function main(opt)
-- init options
opt = opt or {}
-- find program
local program = find_program(opt.program or "7z", opt.pathes, opt.check or "--help")
-- find program version
local version = nil
if program and opt and opt.version then
version = find_programver(program, "--help", "(%d+%.?%d*)%s")
end
-- ok?
return program, version
end
```
After placing it in the project's module directory, execute: `xmake l
lib.detect.find_tool 7z` to find it.
<p class="tip"> In order to speed up the efficiency of frequent
lookups, this interface is self-contained by default. If you want to
disable the cache, you can execute `xmake f -c` in the project
directory to clear the local cache. </p>
We can also test quickly with `xmake lua lib.detect.find_tool clang`.
#### detect.find_toolname
- Find tool name
Match the corresponding tool name with the program command, for
example:
| program | toolname |
| ------------------------- | ---------- |
| `xcrun -sdk macosx clang` | clang |
| `/usr/bin/arm-linux-gcc` | gcc |
| `link.exe -lib` | link |
| `gcc-5` | gcc |
| `arm-android-clang++` | clangxx |
| `pkg-config` | pkg_config |
Compared with program, toolname can uniquely mark a tool, and it is also convenient to find and load the corresponding script `find_xxx.lua`.
#### detect.find_cudadevices
- Find CUDA devices of the host
Enumerate CUDA devices through the CUDA Runtime API and query theirs properties.
```lua
import("lib.detect.find_cudadevices")
local devices = find_cudadevices({ skip_compute_mode_prohibited = true })
local devices = find_cudadevices({ min_sm_arch = 35, order_by_flops = true })
```
The result returned is: `{ { ['$id'] = 0, name = "GeForce GTX 960M", major = 5, minor = 0, ... }, ... }`
The included properties will vary depending on the current CUDA version.
Please refer to [CUDA Toolkit Documentation](https://docs.nvidia.com/cuda/cuda-runtime-api/structcudaDeviceProp.html#structcudaDeviceProp) and its historical version for more information.
#### detect.features
- Get all the features of the specified tool
This interface is similar to [compiler.features](#compiler-features). The difference is that this interface is more primitive. The passed argument is the actual tool name toolname.
And this interface not only can get the characteristics of the compiler, the characteristics of any tool can be obtained, so it is more versatile.
```lua
import("lib.detect.features")
local features = features("clang")
local features = features("clang", {flags = "-O0", program = "xcrun -sdk macosx clang"})
local features = features("clang", {flags = {"-g", "-O0", "-std=c++11"}})
```
By passing in flags, you can change the result of the feature, for example, some features of C++11, which are not available by default. After enabling `-std=c++11`, you can get it.
A list of all compiler features can be found at [compiler.features](#compiler-features).
#### detect.has_features
- Determine if the specified feature is supported
This interface is similar to [compiler.has_features](#compiler-has_features), but more primitive, the passed argument is the actual tool name toolname.
And this interface can not only judge the characteristics of the compiler, but the characteristics of any tool can be judged, so it is more versatile.
```lua
import("lib.detect.has_features")
local features = has_features("clang", "cxx_constexpr")
local features = has_features("clang", {"cxx_constexpr", "c_static_assert"}, {flags = {"-g", "-O0"}, program = "xcrun -sdk macosx clang"})
local features = has_features("clang", {"cxx_constexpr", "c_static_assert"}, {flags = "-g"})
```
If the specified feature list exists, the actual supported feature sublist is returned. If none is supported, nil is returned. We can also change the feature acquisition rule by specifying flags.
A list of all compiler features can be found at [compiler.features](#compiler-features).
#### detect.has_flags
- Determine if the specified parameter option is supported
This interface is similar to [compiler.has_flags](#compiler-has_flags), but more primitive, the passed argument is the actual tool name toolname.
```lua
import("lib.detect.has_flags")
local ok = has_flags("clang", "-g")
local ok = has_flags("clang", {"-g", "-O0"}, {program = "xcrun -sdk macosx clang"})
local ok = has_flags("clang", "-g -O0", {toolkind = "cxx"})
```
Returns true if the test passed.
The detection of this interface has been optimized. Except for the cache mechanism, in most cases, the tool's option list (`--help`) will be directly judged. If the option list is not available, it will be tried. The way to run to detect.
#### detect.has_cfuncs
- Determine if the specified c function exists
This interface is a simplified version of [lib.detect.check_cxsnippets](#detect-check_cxsnippets) and is only used to detect functions.
```lua
import("lib.detect.has_cfuncs")
local ok = has_cfuncs("setjmp")
local ok = has_cfuncs({"sigsetjmp((void*)0, 0)", "setjmp"}, {includes = "setjmp.h"})
```
The rules for describing functions are as follows:
| Function Description | Description |
| ----------------------------------------------- | ------------- |
| `sigsetjmp` | pure function name |
| `sigsetjmp((void*)0, 0)` | Function Call |
| `sigsetjmp{int a = 0; sigsetjmp((void*)a, a);}` | function name + {} block |
In the last optional parameter, in addition to specifying `includes`, you can also specify other parameters to control the option conditions for compile detection:
```lua
{ verbose = false, target = [target|option], includes = .., config = {linkdirs = .., links = .., defines = ..}}
```
The verbose is used to echo the detection information, the target is used to append the configuration information in the target before the detection, and the config is used to customize the compilation options related to the target.
#### detect.has_cxxfuncs
- Determine if the specified c++ function exists
This interface is similar to [lib.detect.has_cfuncs](#detect-has_cfuncs), please refer to its instructions for use. The only difference is that this interface is used to detect c++ functions.
#### detect.has_cincludes
- Determine if the specified c header file exists
This interface is a simplified version of [lib.detect.check_cxsnippets](#detect-check_cxsnippets) and is only used to detect header files.
```lua
import("lib.detect.has_cincludes")
local ok = has_cincludes("stdio.h")
local ok = has_cincludes({"stdio.h", "stdlib.h"}, {target = target})
local ok = has_cincludes({"stdio.h", "stdlib.h"}, {config = {defines = "_GNU_SOURCE=1", languages = "cxx11"}})
```
#### detect.has_cxxincludes
- Determine if the specified c++ header file exists
This interface is similar to [lib.detect.has_cincludess](#detect-has_cincludes), please refer to its instructions for use. The only difference is that this interface is used to detect c++ header files.
#### detect.has_ctypes
- Determine if the specified c type exists
This interface is a simplified version of [lib.detect.check_cxsnippets](#detect-check_cxsnippets) and is only used to detect functions.
```lua
import("lib.detect.has_ctypes")
local ok = has_ctypes("wchar_t")
local ok = has_ctypes({"char", "wchar_t"}, {includes = "stdio.h"})
local ok = has_ctypes("wchar_t", {includes = {"stdio.h", "stdlib.h"}, config = {"defines = "_GNU_SOURCE=1", languages = "cxx11"}})
```
#### detect.has_cxxtypes
- Determine if the specified c++ type exists
This interface is similar to [lib.detect.has_ctypess](#detect-has_ctypes). Please refer to its instructions for use. The only difference is that this interface is used to detect c++ types.
#### detect.check_cxsnippets
- Check if the c/c++ code snippet can be compiled
The generic c/c++ code snippet detection interface, by passing in a list of multiple code snippets, it will automatically generate a compiled file, and then common sense to compile it, if the compilation pass returns true.
For some complex compiler features, even if [compiler.has_features](#compiler-has_features) can't detect it, you can detect it by trying to compile through this interface.
```lua
import("lib.detect.check_cxsnippets")
local ok = check_cxsnippets("void test() {}")
local ok = check_cxsnippets({"void test(){}", "#define TEST 1"}, {types = "wchar_t", includes = "stdio.h"})
```
This interface is a generic version of interfaces such as [detect.has_cfuncs](#detect-has_cfuncs), [detect.has_cincludes](#detect-has_cincludes), and [detect.has_ctypes](detect-has_ctypes), and is also lower level.
So we can use it to detect: types, functions, includes and links, or combine them together to detect.
The first parameter is a list of code fragments, which are generally used for the detection of some custom features. If it is empty, it can only detect the conditions in the optional parameters, for example:
```lua
local ok = check_cxsnippets({}, {types = {"wchar_t", "char*"}, includes = "stdio.h", funcs = {"sigsetjmp", "sigsetjmp((void*)0, 0)"} })
```
The above call will check if the types, includes and funcs are both satisfied, and return true if passed.
There are other optional parameters:
```lua
{ verbose = false, target = [target|option], sourcekind = "[cc|cxx]"}
```
The verbose is used to echo the detection information. The target is used to append the configuration information in the target before the detection. The sourcekind is used to specify the tool type such as the compiler. For example, the incoming `cxx` is forced to be detected as c++ code.
### net.http
This module provides various operational support for http. The currently available interfaces are as follows:
| Interface | Description | Supported version|
| --------------------------------------------------- | -------------------------------------------- | -------------------- |
[http.download](#http-download) | Download http file | >= 2.1.5 |
#### http.download
- Download http file
This interface is relatively simple, is simply download files.
```lua
import("net.http")
http.download("https://xmake.io", "/tmp/index.html")
```
### privilege.sudo
This interface is used to run commands via `sudo` and provides platform consistency handling, which can be used for scripts that require root privileges to run.
<p class="warn">
In order to ensure security, unless you must use it, try not to use this interface in other cases.
</p>
| Interface | Description | Supported Versions |
| --------------------------------------------------- | -------------------------------------------- | -------------------- |
| [sudo.has](#sudo-has) | Determine if sudo supports | >= 2.1.5 |
| [sudo.run](#sudo-run) | Quiet running program | >= 2.1.5 |
| [sudo.runv](#sudo-runv) | Quiet running program with parameter list | >= 2.1.5 |
| [sudo.exec](#sudo-exec) | Evoke Run Program | >= 2.1.5 |
| [sudo.execv](#sudo-execv) | Echo running program with parameter list | >= 2.1.5 |
| [sudo.iorun](#sudo-iorun) | Run and get the program output | >= 2.1.5 |
| [sudo.iorunv](#sudo-iorunv) | Run and get the program output with parameter list | >= 2.1.5 |
#### sudo.has
- Determine if sudo supports
At present, sudo is supported only under `macosx/linux`. The administrator privilege running on Windows is not supported yet. Therefore, it is recommended to use the interface to judge the support situation before use.
```lua
import("privilege.sudo")
if sudo.has() then
sudo.run("rm /system/file")
end
```
#### sudo.run
- Quietly running native shell commands
For specific usage, please refer to: [os.run](#os-run).
```lua
import("privilege.sudo")
sudo.run("rm /system/file")
```
#### sudo.runv
- Quietly running native shell commands with parameter list
For specific usage, please refer to: [os.runv](#os-runv).
#### sudo.exec
- Echo running native shell commands
For specific usage, please refer to: [os.exec](#os-exec).
#### sudo.execv
- Echo running native shell commands with parameter list
For specific usage, please refer to: [os.execv](#os-execv).
#### sudo.iorun
- Quietly running native shell commands and getting output
For specific usage, please refer to: [os.iorun](#os-iorun).
#### sudo.iorunv
- Run the native shell command quietly and get the output with a list of parameters
For specific usage, please refer to: [os.iorunv](#os-iorunv).
### devel.git
This interface provides access to various commands of git. Compared to the direct call to git command, this module provides a more easy-to-use package interface and provides automatic detection and cross-platform processing for git.
<p class="tip">
Currently on Windows, you need to manually install the git package before you can detect it. The subsequent version will provide automatic integration of git function. Users will not need to care about how to install git, they can be used directly.
</p>
| Interface | Description | Supported Versions |
| --------------------------------------------------- | -------------------------------------------- | -------------------- |
| [git.clone](#git-clone) | clone codebase | >= 2.1.5 |
| [git.pull](#git-pull) | Pull the codebase latest commit | >= 2.1.5 |
| [git.clean](#git-clean) | Clean up the codebase file | >= 2.1.5 |
| [git.checkout](#git-checkout) | Check out the specified branch version | >= 2.1.5 |
| [git.refs](#git-refs) | Get a list of all references | >= 2.1.5 |
| [git.tags](#git-tags) | Get all tag lists | >= 2.1.5 |
| [git.branches](#git-branches) | Get a list of all branches | >= 2.1.5 |
#### git.clone
- clone codebase
This interface corresponds to the `git clone` command.
```lua
import("devel.git")
git.clone("git@github.com:tboox/xmake.git")
git.clone("git@github.com:tboox/xmake.git", {depth = 1, branch = "master", outputdir = "/tmp/xmake"})
```
#### git.pull
- Pull the code base for the latest submission
This interface corresponds to the `git pull` command.
```lua
import("devel.git")
git.pull()
git.pull({remote = "origin", tags = true, branch = "master", repodir = "/tmp/xmake"})
```
#### git.clean
- Clean up the code base file
This interface corresponds to the `git clean` command.
```lua
import("devel.git")
git.clean()
git.clean({repodir = "/tmp/xmake", force = true})
```
#### git.checkout
- Check out the specified branch version
This interface corresponds to the `git checkout` command
```lua
import("devel.git")
git.checkout("master", {repodir = "/tmp/xmake"})
git.checkout("v1.0.1", {repodir = "/tmp/xmake"})
```
#### git.refs
- Get a list of all references
This interface corresponds to the `git ls-remote --refs` command
```lua
import("devel.git")
local refs = git.refs(url)
```
#### git.tags
- Get a list of all tags
This interface corresponds to the `git ls-remote --tags` command
```lua
import("devel.git")
local tags = git.tags(url)
```
#### git.branches
- Get a list of all branches
This interface corresponds to the `git ls-remote --heads` command
```lua
import("devel.git")
local branches = git.branches(url)
```
### utils.archive
This module is used to compress and decompress files.
| Interface | Description | Supported Versions |
| --------------------------------------------------- | -------------------------------------------- | -------------------- |
| [archive.extract](#archive-extract)| Extract files | >= 2.1.5 |
#### archive.extract
- unzip files
Supports the decompression of most commonly used compressed files. It automatically detects which decompression tools are provided by the system, and then adapts them to the most suitable decompressor to decompress the specified compressed files.
```lua
import("utils.archive")
archive.extract("/tmp/a.zip", "/tmp/outputdir")
archive.extract("/tmp/a.7z", "/tmp/outputdir")
archive.extract("/tmp/a.gzip", "/tmp/outputdir")
archive.extract("/tmp/a.tar.bz2", "/tmp/outputdir")
```
|