From 404575d33dd364563d0b582c524d6219c1277c49 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 29 Aug 2023 23:01:26 +0800 Subject: add cppfront rule --- tests/projects/cppfront/console/.gitignore | 8 ++++++++ tests/projects/cppfront/console/src/main.cpp2 | 7 +++++++ tests/projects/cppfront/console/xmake.lua | 7 +++++++ 3 files changed, 22 insertions(+) create mode 100644 tests/projects/cppfront/console/.gitignore create mode 100644 tests/projects/cppfront/console/src/main.cpp2 create mode 100644 tests/projects/cppfront/console/xmake.lua (limited to 'tests/projects/cppfront/console') diff --git a/tests/projects/cppfront/console/.gitignore b/tests/projects/cppfront/console/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/cppfront/console/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/cppfront/console/src/main.cpp2 b/tests/projects/cppfront/console/src/main.cpp2 new file mode 100644 index 000000000..168a1773a --- /dev/null +++ b/tests/projects/cppfront/console/src/main.cpp2 @@ -0,0 +1,7 @@ +main: () -> int = + println("Hello world!\n"); + +println: (msg: _) -> int = { + std::cout << "msg: " << msg; + return 0; +} diff --git a/tests/projects/cppfront/console/xmake.lua b/tests/projects/cppfront/console/xmake.lua new file mode 100644 index 000000000..c9744ada5 --- /dev/null +++ b/tests/projects/cppfront/console/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.debug", "mode.release") + +target("test") + add_rules("cppfront") + set_kind("binary") + add_files("src/*.cpp2") + -- cgit v1.3.1 From b19a421b52157c06021d6782ac6cde61e57ec07a Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 30 Aug 2023 00:04:52 +0800 Subject: impl cppfront rule --- tests/projects/cppfront/console/src/main.cpp2 | 2 +- tests/projects/cppfront/console/xmake.lua | 3 ++ xmake/rules/cppfront/xmake.lua | 56 ++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 6 deletions(-) (limited to 'tests/projects/cppfront/console') diff --git a/tests/projects/cppfront/console/src/main.cpp2 b/tests/projects/cppfront/console/src/main.cpp2 index 168a1773a..6f9aff6a9 100644 --- a/tests/projects/cppfront/console/src/main.cpp2 +++ b/tests/projects/cppfront/console/src/main.cpp2 @@ -1,4 +1,4 @@ -main: () -> int = +main: () -> int = println("Hello world!\n"); println: (msg: _) -> int = { diff --git a/tests/projects/cppfront/console/xmake.lua b/tests/projects/cppfront/console/xmake.lua index c9744ada5..65b68cd6e 100644 --- a/tests/projects/cppfront/console/xmake.lua +++ b/tests/projects/cppfront/console/xmake.lua @@ -1,7 +1,10 @@ add_rules("mode.debug", "mode.release") +add_requires("cppfront") + target("test") add_rules("cppfront") set_kind("binary") add_files("src/*.cpp2") + add_packages("cppfront") diff --git a/xmake/rules/cppfront/xmake.lua b/xmake/rules/cppfront/xmake.lua index c6e41137c..5ef208e3b 100644 --- a/xmake/rules/cppfront/xmake.lua +++ b/xmake/rules/cppfront/xmake.lua @@ -21,18 +21,64 @@ -- define rule: cppfront.build rule("cppfront.build") set_extensions(".cpp2") - on_load(function(target) - print("load") + on_load(function (target) + -- only cppfront source files? we need to patch cxx source kind for linker + local sourcekinds = target:sourcekinds() + if #sourcekinds == 0 then + table.insert(sourcekinds, "cxx") + end + local cppfront = target:pkg("cppfront") + if cppfront and cppfront:installdir() then + local includedir = path.join(cppfront:installdir(), "include") + if os.isdir(includedir) then + target:add("includedirs", includedir) + end + end end) - on_build_file(function(target, sourcefile, opt) - print("build", sourcefile) + before_buildcmd_file(function (target, batchcmds, sourcefile_cpp2, opt) + + -- get cppfront + import("lib.detect.find_tool") + local cppfront = assert(find_tool("cppfront", {check = "-h"}), "cppfront not found!") + + -- get c++ source file for cpp2 + local sourcefile_cpp = target:autogenfile((sourcefile_cpp2:gsub(".cpp2$", ".cpp"))) + local basedir = path.directory(sourcefile_cpp) + + -- add objectfile + local objectfile = target:objectfile(sourcefile_cpp) + table.insert(target:objectfiles(), objectfile) + + -- add commands + local argv = {"-o", path(sourcefile_cpp), path(sourcefile_cpp2)} + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.cpp2 %s", sourcefile_cpp2) + batchcmds:mkdir(basedir) + batchcmds:vrunv(cppfront.program, argv) + batchcmds:compile(sourcefile_cpp, objectfile, {configs = {languages = "c++20"}}) + + -- add deps + batchcmds:add_depfiles(sourcefile_cpp2) + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) end) + -- define rule: cppfront rule("cppfront") -- add build rules - add_deps("c++", "cppfront.build") + add_deps("cppfront.build") + + -- set compiler runtime, e.g. vs runtime + add_deps("utils.compiler.runtime") -- inherit links and linkdirs of all dependent targets by default add_deps("utils.inherit.links") + + -- support `add_files("src/*.o")` and `add_files("src/*.a")` to merge object and archive files to target + add_deps("utils.merge.object", "utils.merge.archive") + + -- we attempt to extract symbols to the independent file and + -- strip self-target binary if `set_symbols("debug")` and `set_strip("all")` are enabled + add_deps("utils.symbols.extract") + -- cgit v1.3.1 From 801b1ef7756eb5fa0d28fb943c6305d525d47ce4 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 30 Aug 2023 00:07:22 +0800 Subject: improve hello world for cppfront --- tests/projects/cppfront/console/src/main.cpp2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/projects/cppfront/console') diff --git a/tests/projects/cppfront/console/src/main.cpp2 b/tests/projects/cppfront/console/src/main.cpp2 index 6f9aff6a9..1ab54b94b 100644 --- a/tests/projects/cppfront/console/src/main.cpp2 +++ b/tests/projects/cppfront/console/src/main.cpp2 @@ -2,6 +2,6 @@ main: () -> int = println("Hello world!\n"); println: (msg: _) -> int = { - std::cout << "msg: " << msg; + std::cout << msg; return 0; } -- cgit v1.3.1