From 990430ca785e293ee81ee39360581fda06de621b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 29 Aug 2023 23:00:23 +0800 Subject: [PATCH 1/5] 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 00000000000..15210576129 --- /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 00000000000..168a1773a7f --- /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 00000000000..5499ee54734 --- /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") + From 404575d33dd364563d0b582c524d6219c1277c49 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 29 Aug 2023 23:01:26 +0800 Subject: [PATCH 2/5] add cppfront rule --- .../cppfront => cppfront/console}/.gitignore | 0 .../console}/src/main.cpp2 | 0 .../cppfront => cppfront/console}/xmake.lua | 1 + xmake/rules/cppfront/xmake.lua | 38 +++++++++++++++++++ 4 files changed, 39 insertions(+) rename tests/projects/{other/cppfront => cppfront/console}/.gitignore (100%) rename tests/projects/{other/cppfront => cppfront/console}/src/main.cpp2 (100%) rename tests/projects/{other/cppfront => cppfront/console}/xmake.lua (80%) create mode 100644 xmake/rules/cppfront/xmake.lua diff --git a/tests/projects/other/cppfront/.gitignore b/tests/projects/cppfront/console/.gitignore similarity index 100% rename from tests/projects/other/cppfront/.gitignore rename to tests/projects/cppfront/console/.gitignore diff --git a/tests/projects/other/cppfront/src/main.cpp2 b/tests/projects/cppfront/console/src/main.cpp2 similarity index 100% rename from tests/projects/other/cppfront/src/main.cpp2 rename to tests/projects/cppfront/console/src/main.cpp2 diff --git a/tests/projects/other/cppfront/xmake.lua b/tests/projects/cppfront/console/xmake.lua similarity index 80% rename from tests/projects/other/cppfront/xmake.lua rename to tests/projects/cppfront/console/xmake.lua index 5499ee54734..c9744ada5d9 100644 --- a/tests/projects/other/cppfront/xmake.lua +++ b/tests/projects/cppfront/console/xmake.lua @@ -1,6 +1,7 @@ add_rules("mode.debug", "mode.release") target("test") + add_rules("cppfront") 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 00000000000..c6e41137cf8 --- /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") From b19a421b52157c06021d6782ac6cde61e57ec07a Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 30 Aug 2023 00:04:52 +0800 Subject: [PATCH 3/5] 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 168a1773a7f..6f9aff6a9ee 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 c9744ada5d9..65b68cd6e50 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 c6e41137cf8..5ef208e3bfa 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") + From 4c1dfbee44148256bb9f05e776810e3a1efa53a5 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 30 Aug 2023 00:05:31 +0800 Subject: [PATCH 4/5] 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 5ef208e3bfa..90153df85e0 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") From 801b1ef7756eb5fa0d28fb943c6305d525d47ce4 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 30 Aug 2023 00:07:22 +0800 Subject: [PATCH 5/5] 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 6f9aff6a9ee..1ab54b94bd3 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; }