diff options
| author | ruki <[email protected]> | 2023-04-30 23:11:57 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-30 23:11:57 +0800 |
| commit | 21fb04a180feb12bb57677fc1e1638d185fb10f1 (patch) | |
| tree | 7e2c260de2e9d285cdbe86a00b7a7f284b0ec847 | |
| parent | 76bfacb404b3736934515c7b2ce68b2887bd7b17 (diff) | |
| parent | 3924adf378466e2ffebe768dde1c3c735449b69a (diff) | |
Merge pull request #3698 from xmake-io/autogen
Add autogen test
| -rw-r--r-- | tests/projects/other/autogen_code/xmake.lua | 7 | ||||
| -rw-r--r-- | tests/projects/other/autogen_codedep/.gitignore | 8 | ||||
| -rw-r--r-- | tests/projects/other/autogen_codedep/src/autogen.cpp | 28 | ||||
| -rw-r--r-- | tests/projects/other/autogen_codedep/src/data.in | 1 | ||||
| -rw-r--r-- | tests/projects/other/autogen_codedep/src/main.cpp | 10 | ||||
| -rw-r--r-- | tests/projects/other/autogen_codedep/test.lua | 3 | ||||
| -rw-r--r-- | tests/projects/other/autogen_codedep/xmake.lua | 35 | ||||
| -rw-r--r-- | xmake/core/tool/compiler.lua | 6 | ||||
| -rw-r--r-- | xmake/core/tool/linker.lua | 4 |
9 files changed, 95 insertions, 7 deletions
diff --git a/tests/projects/other/autogen_code/xmake.lua b/tests/projects/other/autogen_code/xmake.lua index fb40d8e9c..9bfe25d9f 100644 --- a/tests/projects/other/autogen_code/xmake.lua +++ b/tests/projects/other/autogen_code/xmake.lua @@ -1,16 +1,13 @@ add_rules("mode.debug", "mode.release") -target("autogen_code") +target("test") set_kind("binary") add_files("$(buildir)/autogen.cpp", {always_added = true}) before_build(function (target) io.writefile("$(buildir)/autogen.cpp", [[ #include <iostream> - using namespace std; - -int main(int argc, char** argv) -{ +int main(int argc, char** argv) { cout << "hello world!" << endl; return 0; } diff --git a/tests/projects/other/autogen_codedep/.gitignore b/tests/projects/other/autogen_codedep/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/other/autogen_codedep/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/other/autogen_codedep/src/autogen.cpp b/tests/projects/other/autogen_codedep/src/autogen.cpp new file mode 100644 index 000000000..cf556ad99 --- /dev/null +++ b/tests/projects/other/autogen_codedep/src/autogen.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include <fstream> +#include <vector> + +using namespace std; + +int main(int argc, char** argv) { + ifstream src_file(argv[1], ios::in | ios::binary); + if (!src_file) { + return 1; + } + vector<char> buffer(istreambuf_iterator<char>(src_file), {}); + src_file.close(); + + ofstream dst_file(argv[2], ios::out); + if (!dst_file) { + return 1; + } + + dst_file << "unsigned char g_codegen_data[] = {"; + for (auto byte : buffer) { + dst_file << "0x" << hex << (int)(unsigned char)byte << ","; + } + dst_file << "0};" << endl; + dst_file.close(); + return 0; +} + diff --git a/tests/projects/other/autogen_codedep/src/data.in b/tests/projects/other/autogen_codedep/src/data.in new file mode 100644 index 000000000..a04238969 --- /dev/null +++ b/tests/projects/other/autogen_codedep/src/data.in @@ -0,0 +1 @@ +hello world! diff --git a/tests/projects/other/autogen_codedep/src/main.cpp b/tests/projects/other/autogen_codedep/src/main.cpp new file mode 100644 index 000000000..3241308f7 --- /dev/null +++ b/tests/projects/other/autogen_codedep/src/main.cpp @@ -0,0 +1,10 @@ +#include <iostream> + +using namespace std; + +extern unsigned char g_codegen_data[]; + +int main(int argc, char** argv) { + cout << (const char*)g_codegen_data << endl; + return 0; +} diff --git a/tests/projects/other/autogen_codedep/test.lua b/tests/projects/other/autogen_codedep/test.lua new file mode 100644 index 000000000..b57362078 --- /dev/null +++ b/tests/projects/other/autogen_codedep/test.lua @@ -0,0 +1,3 @@ +function main(t) + t:build() +end diff --git a/tests/projects/other/autogen_codedep/xmake.lua b/tests/projects/other/autogen_codedep/xmake.lua new file mode 100644 index 000000000..229439fef --- /dev/null +++ b/tests/projects/other/autogen_codedep/xmake.lua @@ -0,0 +1,35 @@ +add_rules("mode.debug", "mode.release") + +rule("autogen") + set_extensions(".in") + before_buildcmd_file(function (target, batchcmds, sourcefile, opt) + local sourcefile_cx = path.join(target:autogendir(), "rules", "autogen", path.basename(sourcefile) .. ".cpp") + local objectfile = target:objectfile(sourcefile_cx) + table.insert(target:objectfiles(), objectfile) + + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.autogen %s", sourcefile) + batchcmds:mkdir(path.directory(sourcefile_cx)) + batchcmds:vrunv(target:dep("autogen"):targetfile(), {sourcefile, sourcefile_cx}) + batchcmds:compile(sourcefile_cx, objectfile) + + batchcmds:add_depfiles(sourcefile, target:dep("autogen"):targetfile()) + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) + end) + +target("autogen") + set_default(false) + set_kind("binary") + set_plat(os.host()) + set_arch(os.arch()) + add_files("src/autogen.cpp") + set_languages("c++11") + +target("test") + set_kind("binary") + add_deps("autogen") + add_rules("autogen") + add_files("src/main.cpp") + add_files("src/*.in") + set_policy("build.across_targets_in_parallel", false) + diff --git a/xmake/core/tool/compiler.lua b/xmake/core/tool/compiler.lua index 8dc7bffd9..c11b2ba1d 100644 --- a/xmake/core/tool/compiler.lua +++ b/xmake/core/tool/compiler.lua @@ -121,7 +121,11 @@ function compiler.load(sourcekind, target) end -- init cache key - local cachekey = sourcekind .. (program_or_errors or "") .. (config.get("arch") or os.arch()) + -- @note we need plat/arch, + -- because it is possible for the compiler to do cross-compilation with the -target parameter + local plat = compiler_tool:plat() or config.plat() or os.host() + local arch = compiler_tool:arch() or config.arch() or os.arch() + local cachekey = sourcekind .. (program_or_errors or "") .. plat .. arch -- get it directly from cache dirst compiler._INSTANCES = compiler._INSTANCES or {} diff --git a/xmake/core/tool/linker.lua b/xmake/core/tool/linker.lua index 88b6da6a9..aa7b1bb8c 100644 --- a/xmake/core/tool/linker.lua +++ b/xmake/core/tool/linker.lua @@ -133,7 +133,9 @@ function linker.load(targetkind, sourcekinds, target) local linkerinfo = linkerinfo_or_errors -- init cache key - local cachekey = targetkind .. "_" .. linkerinfo.linkerkind .. (linkerinfo.program or "") .. (config.get("arch") or os.arch()) + local plat = linkertool:plat() or config.plat() or os.host() + local arch = linkertool:arch() or config.arch() or os.arch() + local cachekey = targetkind .. "_" .. linkerinfo.linkerkind .. (linkerinfo.program or "") .. plat .. arch -- get it directly from cache dirst builder._INSTANCES = builder._INSTANCES or {} |
