|
| 1 | +# Automatic Code Generation {#autogen} |
| 2 | + |
| 3 | +In many cases, we need to preprocess and automatically generate code before compilation, and then include the generated code in the subsequent build process. |
| 4 | + |
| 5 | +As Xmake continues to evolve, its support for code generation features is also being updated and improved. Currently, the following approaches are supported: |
| 6 | + |
| 7 | +## The Simplest Approach |
| 8 | + |
| 9 | +This is the most straightforward method: simply generate the code before building and forcibly add it using `add_files`. |
| 10 | + |
| 11 | +By default, `add_files` will not add files that do not exist, so you need to set `always_added = true` to force the addition, even if the file does not exist yet. |
| 12 | + |
| 13 | +```lua |
| 14 | +add_rules("mode.debug", "mode.release") |
| 15 | + |
| 16 | +target("test") |
| 17 | + set_kind("binary") |
| 18 | + add_files("$(builddir)/autogen.cpp", {always_added = true}) |
| 19 | + before_build(function (target) |
| 20 | + io.writefile("$(builddir)/autogen.cpp", [[ |
| 21 | +#include <iostream> |
| 22 | +using namespace std; |
| 23 | +int main(int argc, char** argv) { |
| 24 | + cout << "hello world!" << endl; |
| 25 | + return 0; |
| 26 | +} |
| 27 | + ]]) |
| 28 | + end) |
| 29 | +``` |
| 30 | + |
| 31 | +This approach has many limitations and is not commonly used in real-world scenarios, but it is simple and easy to understand. |
| 32 | + |
| 33 | +## Generation via Dependent Target |
| 34 | + |
| 35 | +Sometimes, code generation requires running a target program within the project. This can be achieved as follows: |
| 36 | + |
| 37 | +```lua |
| 38 | +add_rules("mode.debug", "mode.release") |
| 39 | + |
| 40 | +rule("autogen") |
| 41 | + set_extensions(".in") |
| 42 | + before_buildcmd_file(function (target, batchcmds, sourcefile, opt) |
| 43 | + local sourcefile_cx = path.join(target:autogendir(), "rules", "autogen", path.basename(sourcefile) .. ".cpp") |
| 44 | + local objectfile = target:objectfile(sourcefile_cx) |
| 45 | + table.insert(target:objectfiles(), objectfile) |
| 46 | + |
| 47 | + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.autogen %s", sourcefile) |
| 48 | + batchcmds:mkdir(path.directory(sourcefile_cx)) |
| 49 | + batchcmds:vrunv(target:dep("autogen"):targetfile(), {sourcefile, sourcefile_cx}) |
| 50 | + batchcmds:compile(sourcefile_cx, objectfile) |
| 51 | + |
| 52 | + batchcmds:add_depfiles(sourcefile, target:dep("autogen"):targetfile()) |
| 53 | + batchcmds:set_depmtime(os.mtime(objectfile)) |
| 54 | + batchcmds:set_depcache(target:dependfile(objectfile)) |
| 55 | + end) |
| 56 | + |
| 57 | +target("autogen") |
| 58 | + set_default(false) |
| 59 | + set_kind("binary") |
| 60 | + set_plat(os.host()) -- Build for the host platform |
| 61 | + set_arch(os.arch()) |
| 62 | + add_files("src/autogen.cpp") |
| 63 | + set_languages("c++11") |
| 64 | + set_policy("build.fence", true) -- Disable parallel compilation between source files |
| 65 | + |
| 66 | +target("test") |
| 67 | + set_kind("binary") |
| 68 | + add_deps("autogen") |
| 69 | + add_rules("autogen") |
| 70 | + add_files("src/main.cpp") |
| 71 | + add_files("src/*.in") |
| 72 | +``` |
| 73 | + |
| 74 | +First, configure an `autogen` target program that must be runnable on the current build platform, so use `set_plat(os.host())` to force building for the host platform. |
| 75 | + |
| 76 | +Also, set the `build.fence` policy to disable parallel compilation between source files, ensuring the `autogen` target is built first and its executable is available in advance. |
| 77 | + |
| 78 | +Then, define a custom rule to invoke the `autogen` target program before building, generate the source code, and compile the generated code into object files for linking. |
| 79 | + |
| 80 | +See the full example: [autogen_codedep](https://github.com/xmake-io/xmake/blob/dev/tests/projects/other/autogen/autogen_codedep/xmake.lua) |
| 81 | + |
| 82 | +## Generation via Native Module |
| 83 | + |
| 84 | +Xmake introduces native module development, allowing code generation via native modules without defining an extra `autogen` target program. |
| 85 | + |
| 86 | +For details on native module development, see: [Native Module Development](/api/scripts/native-modules). |
| 87 | + |
| 88 | +```lua |
| 89 | +add_rules("mode.debug", "mode.release") |
| 90 | + |
| 91 | +add_moduledirs("modules") |
| 92 | + |
| 93 | +rule("autogen") |
| 94 | + set_extensions(".in") |
| 95 | + before_build_file(function (target, sourcefile, opt) |
| 96 | + import("utils.progress") |
| 97 | + import("core.project.depend") |
| 98 | + import("core.tool.compiler") |
| 99 | + import("autogen.foo", {always_build = true}) |
| 100 | + |
| 101 | + local sourcefile_cx = path.join(target:autogendir(), "rules", "autogen", path.basename(sourcefile) .. ".cpp") |
| 102 | + local objectfile = target:objectfile(sourcefile_cx) |
| 103 | + table.insert(target:objectfiles(), objectfile) |
| 104 | + |
| 105 | + depend.on_changed(function () |
| 106 | + progress.show(opt.progress, "${color.build.object}compiling.autogen %s", sourcefile) |
| 107 | + os.mkdir(path.directory(sourcefile_cx)) |
| 108 | + foo.generate(sourcefile, sourcefile_cx) |
| 109 | + compiler.compile(sourcefile_cx, objectfile, {target = target}) |
| 110 | + end, {dependfile = target:dependfile(objectfile), |
| 111 | + files = sourcefile, |
| 112 | + changed = target:is_rebuilt()}) |
| 113 | + end) |
| 114 | + |
| 115 | +target("test") |
| 116 | + set_kind("binary") |
| 117 | + add_rules("autogen") |
| 118 | + add_files("src/main.cpp") |
| 119 | + add_files("src/*.in") |
| 120 | +``` |
| 121 | + |
| 122 | +See the full example: [Native Module Autogen](https://github.com/xmake-io/xmake/blob/dev/tests/projects/other/autogen/autogen_shared_module/xmake.lua). |
| 123 | + |
| 124 | +## Generation in the Prepare Phase |
| 125 | + |
| 126 | +Since Xmake 3.x, the `on_prepare` and `on_prepare_files` interfaces have been introduced to enable two-phase builds. The Prepare phase can be used specifically for source code generation and preprocessing. |
| 127 | + |
| 128 | +The Prepare phase is executed before all `on_build` and `on_build_files` interfaces. |
| 129 | + |
| 130 | +For details, see: [Prepare Interface Manual](/api/description/project-target.html#on-prepare). |
0 commit comments