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
|
After the 2.2.1 release, xmake not only natively supports the construction of multi-language files, but also allows users to implement complex unknown file builds by custom building rules.
We can extend the build support for other files by pre-setting the file suffixes supported by the rules:
```lua
-- Define a build rule for a markdown file
rule("markdown")
set_extensions(".md", ".markdown")
on_build_file(function (target, sourcefile)
os.cp(sourcefile, path.join(target:targetdir(), path.basename(sourcefile) .. ".html"))
end)
target("test")
set_kind("binary")
-- Make the test target support the construction rules of the markdown file
add_rules("markdown")
-- Adding a markdown file to build
add_files("src/*.md")
add_files("src/*.markdown")
```
We can also specify some other scattered files to be processed as markdown rules:
```lua
target("test")
-- ...
add_files("src/test/*.md.in", {rule = "markdown"})
```
A target can be superimposed to apply multiple rules to more customize its own build behavior, and even support different build environments.
<p class="tips">
Rules specified by `add_files("*.md", {rule = "markdown"})`, with a higher priority than the rule set by `add_rules("markdown")`.
</p>
| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [rule](#rule) | Defining Rules | >= 2.1.9 |
| [add_imports](#ruleadd_imports) | Pre-importing extension modules for all custom scripts | >= 2.1.9 |
| [set_extensions](#ruleset_extensions) | Set the file extension type supported by the rule | >= 2.1.9 |
| [on_load](#ruleon_load) | Custom Load Script | >= 2.2.1 |
| [on_link](#ruleon_link) | Custom Link Script | >= 2.2.7 |
| [on_build](#ruleon_build) | Custom Compilation Script | >= 2.1.9 |
| [on_clean](#ruleon_clean) | Custom Cleanup Script | >= 2.1.9 |
| [on_package](#ruleon_package) | Custom Package Script | >= 2.1.9 |
| [on_install](#ruleon_install) | Custom Installation Script | >= 2.1.9 |
| [on_uninstall](#ruleon_uninstall) | Custom Uninstall Script | >= 2.1.9 |
| [on_build_file](#ruleon_build_file) | Custom the build script to implement single file build | >= 2.2.1 |
| [on_build_files](#ruleon_build_files) | Custom Compilation Scripts for Multi-File Construction | >= 2.2.1 |
| [before_load](#rulebefore_load) | Custom pre-load script | >= 2.2.1 |
| [before_link](#rulebefore_link) | Custom pre-link script | >= 2.2.7 |
| [before_build](#rulebefore_build) | Custom pre-compilation script | >= 2.2.1 |
| [before_clean](#rulebefore_clean) | Custom the script before cleanup | >= 2.2.1 |
| [before_package](#rulebefore_package) | Custom the script before packaging | >= 2.2.1 |
| [before_install](#rulebefore_install) | Custom Pre-Installation Scripts | >= 2.2.1 |
| [before_uninstall](#rulebefore_uninstall) | Custom the script before uninstalling | >= 2.2.1 |
| [before_build_file](#rulebefore_build_file) | Custom pre-compilation scripts to implement single file builds | >= 2.2.1 |
| [before_build_files](#rulebefore_build_files) | Custom pre-compilation scripts for multi-file build | >= 2.2.1 |
| [after_load](#ruleafter_load) | Custom script after loading | >= 2.2.1 |
| [after_link](#ruleafter_link) | Custom script after linking | >= 2.2.7 |
| [after_build](#ruleafter_build) | Custom script after compilation | >= 2.2.1 |
| [after_clean](#ruleafter_clean) | Custom script after cleaning | >= 2.2.1 |
| [after_package](#ruleafter_package) | Custom script after packaging | >= 2.2.1 |
| [after_install](#ruleafter_install) | Custom script after installing | >= 2.2.1 |
| [after_uninstall](#ruleafter_uninstall) | Custom script after uninstalling | >= 2.2.1 |
| [after_build_file](#ruleafter_build_file) | Custom script after compiling the single file | >= 2.2.1 |
| [after_build_files](#ruleafter_build_files) | Custom script after compiling the multiple file | >= 2.2.1 |
| [rule_end](#rule_end) | End Definition Rule | >= 2.1.9 |
### Built-in rules
sinceAfter the 2.2.1 release, xmake provides some built-in rules to simplify the daily xmake.lua description and support for some common build environments.
| Rules | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [mode.debug](#mode-debug) | Debug Mode Compilation Rules | >= 2.2.1 |
| [mode.release](#mode-release) | Release Mode Compilation Rules | >= 2.2.1 |
| [mode.check](#mode-check) | Detection Mode Compilation Rules | >= 2.2.1 |
| [mode.profile](#mode-profile) | Performance Analysis Mode Compilation Rules | >= 2.2.1 |
| [mode.coverage](#mode-coverage) | Coverage Analysis Compilation Mode Rules | >= 2.2.1 |
| [qt.static](#qt-static) | Qt Static Library Compilation Rules | >= 2.2.1 |
| [qt.shared](#qt-shared) | Qt Dynamic Library Compilation Rules | >= 2.2.1 |
| [qt.console](#qt-console) | Qt Console Compilation Rules | >= 2.2.1 |
| [qt.application](#qt-application) | Qt Application Compilation Rules | >= 2.2.1 |
| [wdk.umdf.driver](#wdk-umdf-driver) | WDK Environment umdf Driver Compilation Rules | >= 2.2.1 |
[wdk.umdf.binary](#wdk-umdf-binary) | WDK Environment umdf Driver Application Compilation Rules | >= 2.2.1 |
| [wdk.kmdf.driver](#wdk-kmdf-driver) | WDK Environment kmdf Driver Compilation Rules | >= 2.2.1 |
[wdk.kmdf.binary](#wdk-kmdf-binary) | WDK Environment kmdf Driver Application Compilation Rules | >= 2.2.1 |
| [wdk.wdm.driver](#wdk-wdm-driver) | WDK Environment wdm Driver Compilation Rules | >= 2.2.1 |
[wdk.wdm.binary](#wdk-wdm-binary) | WDK Environment wdm Driver Application Compilation Rules | >= 2.2.1 |
#### mode.debug
Add the configuration rules for the debug compilation mode for the current project xmake.lua, for example:
```lua
add_rules("mode.debug")
```
Equivalent to:
```lua
-- the debug mode
if is_mode("debug") then
-- enable the debug symbols
set_symbols("debug")
-- disable optimization
set_optimize("none")
end
```
We can switch to this compilation mode by ``xmake f -m debug`.
#### mode.release
Add the configuration rules for the release compilation mode for the current project xmake.lua, for example:
```lua
add_rules("mode.release")
```
Equivalent to:
```lua
-- the release mode
if is_mode("release") then
-- set the symbols visibility: hidden
set_symbols("hidden")
-- enable2017 optimization
set_optimize("fastest")
-- strip all symbols
set_strip("all")
end
```
We can switch to this compilation mode by ``xmake f -m release`.
#### mode.check
Add the check compilation mode configuration rules for the current project xmake.lua, generally used for memory detection, for example:
```lua
add_rules("mode.check")
```
Equivalent to:
```lua
-- the check mode
if is_mode("check") then
-- enable the debug symbols
set_symbols("debug")
-- disable optimization
set_optimize("none")
-- attempt to enable some checkers for pc
add_cxflags("-fsanitize=address", "-ftrapv")
add_mxflags("-fsanitize=address", "-ftrapv")
add_ldflags("-fsanitize=address")
end
```
We can switch to this compilation mode by ``xmake f -m check`.
#### mode.profile
Add configuration rules for the profile compilation mode for the current project xmake.lua, which is generally used for performance analysis, for example:
```lua
add_rules("mode.profile")
```
Equivalent to:
```lua
-- the profile mode
if is_mode("profile") then
-- enable the debug symbols
set_symbols("debug")
-- enable gprof
add_cxflags("-pg")
add_ldflags("-pg")
end
```
We can switch to this compilation mode by ``xmake f -m profile`.
#### mode.coverage
Add the configuration rules for the coverage compilation mode for the current project xmake.lua, which is generally used for coverage analysis, for example:
```lua
add_rules("mode.coverage")
```
Equivalent to:
```lua
-- the coverage mode
if is_mode("coverage") then
add_cxflags("--coverage")
add_mxflags("--coverage")
add_ldflags("--coverage")
end
```
We can switch to this compilation mode by ``xmake f -m coverage`.
#### qt.static
A static library program used to compile and generate Qt environments:
```lua
target("qt_static_library")
add_rules("qt.static")
add_files("src/*.cpp")
add_frameworks("QtNetwork", "QtGui")
```
#### qt.shared
Dynamic library program for compiling and generating Qt environment:
```lua
target("qt_shared_library")
add_rules("qt.shared")
add_files("src/*.cpp")
add_frameworks("QtNetwork", "QtGui")
```
#### qt.console
A console program for compiling and generating a Qt environment:
```lua
target("qt_console")
add_rules("qt.console")
add_files("src/*.cpp")
```
#### qt.application
Used to compile ui applications that generate Qt environments.
Quick(qml) application:
```lua
target("qt_quickapp")
add_rules("qt.application")
add_files("src/*.cpp")
add_files("src/qml.qrc")
add_frameworks("QtQuick")
```
Qt Widgets (ui/moc) application:
```lua
-- add target
target("qt_widgetapp")
add_rules("qt.application")
add_files("src/*.cpp")
add_files("src/mainwindow.ui")
add_files("src/mainwindow.h") -- Add a meta header file with Q_OBJECT
add_frameworks("QtWidgets")
```
For more descriptions of Qt, see: [#160](https://github.com/xmake-io/xmake/issues/160)
#### wdk.env.kmdf
Application of the compilation environment setting of kmdf under WDK, need to cooperate with: `wdk.[driver|binary|static|shared]` and other rules to use.
#### wdk.env.umdf
Application of the umdf compiler environment settings under WDK, you need to cooperate with: `wdk.[driver|binary|static|shared]` and other rules to use.
#### wdk.env.wdm
Application wdm compiler environment settings under WDK, need to cooperate with: `wdk.[driver|binary|static|shared]` and other rules to use.
#### wdk.driver
Compile and generate drivers based on the WDK environment under Windows. Currently, only the WDK10 environment is supported.
Note: need to cooperate: `wdk.env.[umdf|kmdf|wdm]`Environmental rules are used.
```lua
-- add target
target("echo")
-- add rules
add_rules("wdk.driver", "wdk.env.kmdf")
-- add files
add_files("driver/*.c")
add_files("driver/*.inx")
-- add includedirs
add_includedirs("exe")
```
#### wdk.binary
Compile and generate executable programs based on WDK environment under Windows. Currently, only WDK10 environment is supported.
Note: It is necessary to cooperate with: environment rules such as `wdk.env.[umdf|kmdf|wdm]`.
```lua
-- add target
target("app")
-- add rules
add_rules("wdk.binary", "wdk.env.umdf")
-- add files
add_files("exe/*.cpp")
```
#### wdk.static
Compile and generate static library programs based on WDK environment under Windows. Currently, only WDK10 environment is supported.
Note: It is necessary to cooperate with: environment rules such as `wdk.env.[umdf|kmdf|wdm]`.
```lua
target("nonpnp")
-- add rules
add_rules("wdk.static", "wdk.env.kmdf")
-- add flags for rule: wdk.tracewpp
add_values("wdk.tracewpp.flags", "-func:TraceEvents(LEVEL,FLAGS,MSG,...)", "-func:Hexdump((LEVEL,FLAGS,MSG,...))")
-- add files
add_files("driver/*.c", {rule = "wdk.tracewpp"})
```
#### wdk.shared
Compile and generate dynamic library programs based on WDK environment under Windows. Currently, only WDK10 environment is supported.
Note: It is necessary to cooperate with: environment rules such as `wdk.env.[umdf|kmdf|wdm]`.
```lua
target("nonpnp")
-- add rules
add_rules("wdk.shared", "wdk.env.wdm")
-- add flags for rule: wdk.tracewpp
add_values("wdk.tracewpp.flags", "-func:TraceEvents(LEVEL,FLAGS,MSG,...)", "-func:Hexdump((LEVEL,FLAGS,MSG,...))")
-- add files
add_files("driver/*.c", {rule = "wdk.tracewpp"})
```
#### wdk.tracewpp
Used to enable tracewpp to preprocess source files:
```lua
target("nonpnp")
-- add rules
add_rules("wdk.driver", "wdk.env.kmdf")
-- add flags for rule: wdk.tracewpp
add_values("wdk.tracewpp.flags", "-func:TraceEvents(LEVEL,FLAGS,MSG,...)", "-func:Hexdump((LEVEL,FLAGS,MSG,...))")
-- add files
add_files("driver/*.c", {rule = "wdk.tracewpp"})
add_files("driver/*.rc")
```
For more information on WDK rules, see: [#159](https://github.com/xmake-io/xmake/issues/159)
#### win.sdk.application
Compile and generate the winsdk application.
```lua
-- add rules
add_rules("mode.debug", "mode.release")
-- define target
target("usbview")
-- windows application
add_rules("win.sdk.application")
-- add files
add_files("*.c", "*.rc")
add_files("xmlhelper.cpp", {rule = "win.sdk.dotnet"})
```
#### wdk.sdk.dotnet
Used to specify certain c++ source files to be compiled as c++.net.
```lua
add_files("xmlhelper.cpp", {rule = "win.sdk.dotnet"})
```
For more information on WDK rules, see: [#159](https://github.com/xmake-io/xmake/issues/159)
### rule
#### Defining rules
```lua
rule("markdown")
set_extensions(".md", ".markdown")
on_build_file(function (target, sourcefile, opt)
os.cp(sourcefile, path.join(target:targetdir(), path.basename(sourcefile) .. ".html"))
end)
```
### rule:add_imports
#### Pre-importing extension modules for all custom scripts
For usage and description, please see: [target:add_imports](#targetadd_imports), the usage is the same.
### rule:set_extensions
#### Setting the file extension type supported by the rule
Apply rules to files with these suffixes by setting the supported extension file types, for example:
```lua
-- Define a build rule for a markdown file
rule("markdown")
set_extensions(".md", ".markdown")
on_build_file(function (target, sourcefile, opt)
os.cp(sourcefile, path.join(target:targetdir(), path.basename(sourcefile) .. ".html"))
end)
target("test")
set_kind("binary")
-- Make the test target support the construction rules of the markdown file
add_rules("markdown")
-- Adding a markdown file to build
add_files("src/*.md")
add_files("src/*.markdown")
```
### rule:on_load
#### Custom load script
The load script used to implement the custom rules will be executed when the target is loaded. You can customize some target configurations in it, for example:
```lua
rule("test")
on_load(function (target)
target:add("defines", "-DTEST")
end)
```
### rule:on_link
#### Custom link script
The link script used to implement the custom rules overrides the default link behavior of the applied target, for example:
```lua
rule("test")
on_link(function (target)
end)
```
### rule:on_build
#### Custom compilation script
The build script used to implement the custom rules overrides the default build behavior of the target being applied, for example:
```lua
rule("markdown")
on_build(function (target)
end)
```
### rule:on_clean
#### Custom cleanup script
The cleanup script used to implement the custom rules will override the default cleanup behavior of the applied target, for example:
```lua
rule("markdown")
on_clean(function (target)
-- remove sourcefile.html
end)
```
### rule:on_package
#### Custom packaging script
A packaging script for implementing custom rules that overrides the default packaging behavior of the target being applied, for example:
```lua
rule("markdown")
on_package(function (target)
-- package sourcefile.html
end)
```
### rule:on_install
#### Custom installation script
An installation script for implementing custom rules that overrides the default installation behavior of the target being applied, for example:
```lua
rule("markdown")
on_install(function (target)
end)
```
### rule:on_uninstall
#### Custom Uninstall Script
An uninstall script for implementing custom rules that overrides the default uninstall behavior of the target being applied, for example:
```lua
rule("markdown")
on_uninstall(function (target)
end)
```
### rule:on_build_file
#### Customizing the build script to process one source file at a time
```lua
rule("markdown")
on_build_file(function (target, sourcefile, opt)
print("%%%d: %s", opt.progress, sourcefile)
end)
```
The third parameter opt is an optional parameter, which is used to obtain some information state during the compilation process. For example, opt.progress is the compilation progress of the current period.
### rule:on_build_files
#### Customizing the build script to process multiple source files at once
Most of the custom build rules, each time processing a single file, output a target file, for example: a.c => a.o
However, in some cases, we need to enter multiple source files together to build an object file, for example: a.c b.c d.c => x.o
For this situation, we can achieve this by customizing this script:
```lua
rule("markdown")
on_build_files(function (target, sourcebatch, opt)
-- build some source files
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
-- ...
end
end)
```
### rule:before_load
#### Custom pre-load script
Used to implement the execution script before the custom target is loaded, for example:
```lua
rule("test")
before_load(function (target)
target:add("defines", "-DTEST")
end)
```
### rule:before_link
#### Custom pre-link script
Execution scripts used to implement custom target links, for example:
```lua
rule("test")
before_link(function (target)
end)
```
### rule:before_build
#### Custom pre-compilation script
Used to implement the execution script before the custom target is built, for example:
```lua
rule("markdown")
before_build(function (target)
end)
```
### rule:before_clean
#### Custom pre-cleanup script
Used to implement the execution script before the custom target cleanup, for example:
```lua
rule("markdown")
before_clean(function (target)
end)
```
### rule:before_package
#### Custom the pre-package script
Used to implement the execution script before the custom target is packaged, for example:
```lua
rule("markdown")
before_package(function (target)
end)
```
### rule:before_install
#### Custom pre-installation script
Used to implement the execution script before the custom target installation, for example:
```lua
rule("markdown")
before_install(function (target)
end)
```
### rule:before_uninstall
#### Custom pre-uninstall script
Used to implement the execution script before the custom target is uninstalled, for example:
```lua
rule("markdown")
before_uninstall(function (target)
end)
```
### rule:before_build_file
#### Custom pre-compilation script to process one source file at a time
Similar to [rule:on_build_file](#ruleon_build_file), but the timing of this interface is called before compiling a source file.
Generally used to preprocess some source files before compiling.
### rule:before_build_files
#### Custom pre-compilation script to process multiple source files at once
Similar to [rule:on_build_files](#ruleon_build_files), but the timing of this interface is called before compiling some source files.
Generally used to preprocess some source files before compiling.
### rule:after_load
#### Custom post-loading script
The execution script used to implement the custom target loading is similar to [rule:after_load](#ruleafter_load).
### rule:after_link
#### Custom post-linking script
The execution script used to implement the custom target link is similar to [rule:after_link](#ruleafter_link).
### rule:after_build
#### Custom post-compilation script
The execution script used to implement the custom target build is similar to [rule:before_build](#rulebefore_build).
### rule:after_clean
#### Custom post-cleaning script
The execution script used to implement the custom target cleanup is similar to [rule:before_clean](#rulebefore_clean).
### rule:after_package
#### Custom post-packaging script
The execution script used to implement the custom target package is similar to [rule:before_package](#rulebefore_package).
### rule:after_install
#### Custom post-installation script
The execution script used to implement the custom target installation is similar to [rule:before_install](#rulebefore_install).
### rule:after_uninstall
#### Custom post-uninstallation Script
The execution script used to implement the custom target uninstallation is similar to [rule:before_uninstall](#rulebefore_uninstall).
### rule:after_build_file
#### Custom post-compilation scripts to process one source file at a time
Similar to [rule:on_build_file](#ruleon_build_file), but the timing of this interface is called after compiling a source file.
Generally used to post-process some compiled object files.
### rule:after_build_files
#### Custom post-compilation scripts to process multiple source files at once
Similar to [rule:on_build_files](#ruleon_build_files), but the timing of this interface is called after compiling some source files.
Generally used to post-process some compiled object files.
### rule_end
#### End definition rules
This is optional. If you want to manually end the rule definition, you can call it:
```lua
rule("test")
-- ..
rule_end()
```
|