From 990430ca785e293ee81ee39360581fda06de621b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 29 Aug 2023 23:00:23 +0800 Subject: add cppfront test --- tests/projects/other/cppfront/.gitignore | 8 ++++++++ tests/projects/other/cppfront/src/main.cpp2 | 7 +++++++ tests/projects/other/cppfront/xmake.lua | 6 ++++++ 3 files changed, 21 insertions(+) create mode 100644 tests/projects/other/cppfront/.gitignore create mode 100644 tests/projects/other/cppfront/src/main.cpp2 create mode 100644 tests/projects/other/cppfront/xmake.lua diff --git a/tests/projects/other/cppfront/.gitignore b/tests/projects/other/cppfront/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/other/cppfront/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/other/cppfront/src/main.cpp2 b/tests/projects/other/cppfront/src/main.cpp2 new file mode 100644 index 000000000..168a1773a --- /dev/null +++ b/tests/projects/other/cppfront/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/other/cppfront/xmake.lua b/tests/projects/other/cppfront/xmake.lua new file mode 100644 index 000000000..5499ee547 --- /dev/null +++ b/tests/projects/other/cppfront/xmake.lua @@ -0,0 +1,6 @@ +add_rules("mode.debug", "mode.release") + +target("test") + set_kind("binary") + add_files("src/*.cpp2") + -- cgit v1.3.1 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 +++++ tests/projects/other/cppfront/.gitignore | 8 ------ tests/projects/other/cppfront/src/main.cpp2 | 7 ----- tests/projects/other/cppfront/xmake.lua | 6 ----- xmake/rules/cppfront/xmake.lua | 38 +++++++++++++++++++++++++++ 7 files changed, 60 insertions(+), 21 deletions(-) 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 delete mode 100644 tests/projects/other/cppfront/.gitignore delete mode 100644 tests/projects/other/cppfront/src/main.cpp2 delete mode 100644 tests/projects/other/cppfront/xmake.lua create mode 100644 xmake/rules/cppfront/xmake.lua 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") + diff --git a/tests/projects/other/cppfront/.gitignore b/tests/projects/other/cppfront/.gitignore deleted file mode 100644 index 152105761..000000000 --- a/tests/projects/other/cppfront/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Xmake cache -.xmake/ -build/ - -# MacOS Cache -.DS_Store - - diff --git a/tests/projects/other/cppfront/src/main.cpp2 b/tests/projects/other/cppfront/src/main.cpp2 deleted file mode 100644 index 168a1773a..000000000 --- a/tests/projects/other/cppfront/src/main.cpp2 +++ /dev/null @@ -1,7 +0,0 @@ -main: () -> int = - println("Hello world!\n"); - -println: (msg: _) -> int = { - std::cout << "msg: " << msg; - return 0; -} diff --git a/tests/projects/other/cppfront/xmake.lua b/tests/projects/other/cppfront/xmake.lua deleted file mode 100644 index 5499ee547..000000000 --- a/tests/projects/other/cppfront/xmake.lua +++ /dev/null @@ -1,6 +0,0 @@ -add_rules("mode.debug", "mode.release") - -target("test") - set_kind("binary") - add_files("src/*.cpp2") - diff --git a/xmake/rules/cppfront/xmake.lua b/xmake/rules/cppfront/xmake.lua new file mode 100644 index 000000000..c6e41137c --- /dev/null +++ b/xmake/rules/cppfront/xmake.lua @@ -0,0 +1,38 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define rule: cppfront.build +rule("cppfront.build") + set_extensions(".cpp2") + on_load(function(target) + print("load") + end) + on_build_file(function(target, sourcefile, opt) + print("build", sourcefile) + end) + +-- define rule: cppfront +rule("cppfront") + + -- add build rules + add_deps("c++", "cppfront.build") + + -- inherit links and linkdirs of all dependent targets by default + add_deps("utils.inherit.links") -- 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(-) 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 4c1dfbee44148256bb9f05e776810e3a1efa53a5 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 30 Aug 2023 00:05:31 +0800 Subject: use on build --- xmake/rules/cppfront/xmake.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/rules/cppfront/xmake.lua b/xmake/rules/cppfront/xmake.lua index 5ef208e3b..90153df85 100644 --- a/xmake/rules/cppfront/xmake.lua +++ b/xmake/rules/cppfront/xmake.lua @@ -35,7 +35,7 @@ rule("cppfront.build") end end end) - before_buildcmd_file(function (target, batchcmds, sourcefile_cpp2, opt) + on_buildcmd_file(function (target, batchcmds, sourcefile_cpp2, opt) -- get cppfront import("lib.detect.find_tool") -- 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(-) 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