From 99e57fc8e1ac160832860547e7d220254366a96a Mon Sep 17 00:00:00 2001 From: carols Date: Sun, 7 Apr 2024 12:57:02 +0800 Subject: Add cppfront's header(*.h2) support --- xmake/rules/cppfront/xmake.lua | 63 ++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 18 deletions(-) (limited to 'xmake/rules/cppfront/xmake.lua') diff --git a/xmake/rules/cppfront/xmake.lua b/xmake/rules/cppfront/xmake.lua index 90153df85..e0d535e7e 100644 --- a/xmake/rules/cppfront/xmake.lua +++ b/xmake/rules/cppfront/xmake.lua @@ -20,7 +20,7 @@ -- define rule: cppfront.build rule("cppfront.build") - set_extensions(".cpp2") + set_extensions(".cpp2", ".h2") on_load(function (target) -- only cppfront source files? we need to patch cxx source kind for linker local sourcekinds = target:sourcekinds() @@ -35,31 +35,58 @@ rule("cppfront.build") end end end) - on_buildcmd_file(function (target, batchcmds, sourcefile_cpp2, opt) + on_buildcmd_files(function (target, batchcmds, sourcebatch, opt) + -- order files h2->cpp2 + cpp2_files = {} + all_files = {} + for _, sourcefile in ipairs(sourcebatch.sourcefiles) do + if string.endswith(sourcefile, ".h2") then + table.insert(all_files, sourcefile) + elseif string.endswith(sourcefile, ".cpp2") then + table.insert(cpp2_files, sourcefile) + end + end + table.join2(all_files, cpp2_files) -- 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) + for _, sourcefile_cpp2 in ipairs(all_files) do -- get c++ source file for cpp2 + local sourcefile_cpp = "" + local is_h2 = string.endswith(sourcefile_cpp2, ".h2") + if is_h2 then + sourcefile_cpp = target:autogenfile((sourcefile_cpp2:gsub(".h2$", ".h"))) + else + sourcefile_cpp = target:autogenfile((sourcefile_cpp2:gsub(".cpp2$", ".cpp"))) + end + local basedir = path.directory(sourcefile_cpp) + + -- add objectfile + local objectfile = "" + if not is_h2 then + objectfile = target:objectfile(sourcefile_cpp) + table.insert(target:objectfiles(), objectfile) + end - -- add objectfile - local objectfile = target:objectfile(sourcefile_cpp) - table.insert(target:objectfiles(), objectfile) + -- add commands + local argv = {"-verb", "-e", "-o", path(sourcefile_cpp), path(sourcefile_cpp2)} - -- 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"}}) + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.cpp2 %s", sourcefile_cpp2) + batchcmds:mkdir(basedir) + batchcmds:vrunv(cppfront.program, argv) + if not is_h2 then + batchcmds:compile(sourcefile_cpp, objectfile, {configs = {languages = "c++20"}}) + end - -- add deps - batchcmds:add_depfiles(sourcefile_cpp2) - batchcmds:set_depmtime(os.mtime(objectfile)) - batchcmds:set_depcache(target:dependfile(objectfile)) + -- add deps + batchcmds:add_depfiles(sourcefile_cpp2) + if not is_h2 then + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) + end + end + end) -- cgit v1.3.1 From 014639a6dc483e72a9184504f466b9a464dc9748 Mon Sep 17 00:00:00 2001 From: carols Date: Sun, 7 Apr 2024 19:44:02 +0800 Subject: Revert "Add cppfront's header(*.h2) support" This reverts commit 99e57fc8e1ac160832860547e7d220254366a96a. --- tests/projects/cppfront/console/src/main.cpp2 | 6 ++- tests/projects/cppfront/console/src/println.h2 | 4 -- tests/projects/cppfront/console/xmake.lua | 1 - xmake/rules/cppfront/xmake.lua | 63 ++++++++------------------ 4 files changed, 22 insertions(+), 52 deletions(-) delete mode 100644 tests/projects/cppfront/console/src/println.h2 (limited to 'xmake/rules/cppfront/xmake.lua') diff --git a/tests/projects/cppfront/console/src/main.cpp2 b/tests/projects/cppfront/console/src/main.cpp2 index 4bf37d8c3..1ab54b94b 100644 --- a/tests/projects/cppfront/console/src/main.cpp2 +++ b/tests/projects/cppfront/console/src/main.cpp2 @@ -1,5 +1,7 @@ -#include "println.h2" - main: () -> int = println("Hello world!\n"); +println: (msg: _) -> int = { + std::cout << msg; + return 0; +} diff --git a/tests/projects/cppfront/console/src/println.h2 b/tests/projects/cppfront/console/src/println.h2 deleted file mode 100644 index 24c244da4..000000000 --- a/tests/projects/cppfront/console/src/println.h2 +++ /dev/null @@ -1,4 +0,0 @@ -println: (msg: _) -> int = { - std::cout << msg; - return 0; -} diff --git a/tests/projects/cppfront/console/xmake.lua b/tests/projects/cppfront/console/xmake.lua index 4c99bfadb..65b68cd6e 100644 --- a/tests/projects/cppfront/console/xmake.lua +++ b/tests/projects/cppfront/console/xmake.lua @@ -6,6 +6,5 @@ target("test") add_rules("cppfront") set_kind("binary") add_files("src/*.cpp2") - add_files("src/*.h2") add_packages("cppfront") diff --git a/xmake/rules/cppfront/xmake.lua b/xmake/rules/cppfront/xmake.lua index e0d535e7e..90153df85 100644 --- a/xmake/rules/cppfront/xmake.lua +++ b/xmake/rules/cppfront/xmake.lua @@ -20,7 +20,7 @@ -- define rule: cppfront.build rule("cppfront.build") - set_extensions(".cpp2", ".h2") + set_extensions(".cpp2") on_load(function (target) -- only cppfront source files? we need to patch cxx source kind for linker local sourcekinds = target:sourcekinds() @@ -35,58 +35,31 @@ rule("cppfront.build") end end end) - on_buildcmd_files(function (target, batchcmds, sourcebatch, opt) - -- order files h2->cpp2 - cpp2_files = {} - all_files = {} - for _, sourcefile in ipairs(sourcebatch.sourcefiles) do - if string.endswith(sourcefile, ".h2") then - table.insert(all_files, sourcefile) - elseif string.endswith(sourcefile, ".cpp2") then - table.insert(cpp2_files, sourcefile) - end - end - table.join2(all_files, cpp2_files) + on_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!") - for _, sourcefile_cpp2 in ipairs(all_files) do -- get c++ source file for cpp2 - local sourcefile_cpp = "" - local is_h2 = string.endswith(sourcefile_cpp2, ".h2") - if is_h2 then - sourcefile_cpp = target:autogenfile((sourcefile_cpp2:gsub(".h2$", ".h"))) - else - sourcefile_cpp = target:autogenfile((sourcefile_cpp2:gsub(".cpp2$", ".cpp"))) - end - local basedir = path.directory(sourcefile_cpp) - - -- add objectfile - local objectfile = "" - if not is_h2 then - objectfile = target:objectfile(sourcefile_cpp) - table.insert(target:objectfiles(), objectfile) - end + -- get c++ source file for cpp2 + local sourcefile_cpp = target:autogenfile((sourcefile_cpp2:gsub(".cpp2$", ".cpp"))) + local basedir = path.directory(sourcefile_cpp) - -- add commands - local argv = {"-verb", "-e", "-o", path(sourcefile_cpp), path(sourcefile_cpp2)} + -- add objectfile + local objectfile = target:objectfile(sourcefile_cpp) + table.insert(target:objectfiles(), objectfile) - batchcmds:show_progress(opt.progress, "${color.build.object}compiling.cpp2 %s", sourcefile_cpp2) - batchcmds:mkdir(basedir) - batchcmds:vrunv(cppfront.program, argv) - if not is_h2 then - batchcmds:compile(sourcefile_cpp, objectfile, {configs = {languages = "c++20"}}) - end + -- 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) - if not is_h2 then - batchcmds:set_depmtime(os.mtime(objectfile)) - batchcmds:set_depcache(target:dependfile(objectfile)) - end - end - + -- add deps + batchcmds:add_depfiles(sourcefile_cpp2) + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) end) -- cgit v1.3.1 From 4044b796efabe9d22ab4208bebe484c18492bf06 Mon Sep 17 00:00:00 2001 From: carols Date: Sun, 7 Apr 2024 21:10:39 +0800 Subject: Add Add cppfront's header(*.h2) support,use add_deps instead of on_buildcmd_file, --- tests/projects/cppfront/console/src/main.cpp2 | 6 ++--- tests/projects/cppfront/console/src/println.h2 | 4 ++++ tests/projects/cppfront/console/xmake.lua | 1 + xmake/rules/cppfront/xmake.lua | 32 ++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 tests/projects/cppfront/console/src/println.h2 (limited to 'xmake/rules/cppfront/xmake.lua') diff --git a/tests/projects/cppfront/console/src/main.cpp2 b/tests/projects/cppfront/console/src/main.cpp2 index 1ab54b94b..1c8bf1c0e 100644 --- a/tests/projects/cppfront/console/src/main.cpp2 +++ b/tests/projects/cppfront/console/src/main.cpp2 @@ -1,7 +1,5 @@ +#include "println.h2" main: () -> int = println("Hello world!\n"); -println: (msg: _) -> int = { - std::cout << msg; - return 0; -} + diff --git a/tests/projects/cppfront/console/src/println.h2 b/tests/projects/cppfront/console/src/println.h2 new file mode 100644 index 000000000..24c244da4 --- /dev/null +++ b/tests/projects/cppfront/console/src/println.h2 @@ -0,0 +1,4 @@ +println: (msg: _) -> int = { + std::cout << msg; + return 0; +} diff --git a/tests/projects/cppfront/console/xmake.lua b/tests/projects/cppfront/console/xmake.lua index 65b68cd6e..4c99bfadb 100644 --- a/tests/projects/cppfront/console/xmake.lua +++ b/tests/projects/cppfront/console/xmake.lua @@ -6,5 +6,6 @@ target("test") add_rules("cppfront") set_kind("binary") add_files("src/*.cpp2") + add_files("src/*.h2") add_packages("cppfront") diff --git a/xmake/rules/cppfront/xmake.lua b/xmake/rules/cppfront/xmake.lua index 90153df85..4cd6a9675 100644 --- a/xmake/rules/cppfront/xmake.lua +++ b/xmake/rules/cppfront/xmake.lua @@ -18,9 +18,38 @@ -- @file xmake.lua -- +rule("cppfront.build.h2") + set_extensions(".h2") + + on_buildcmd_file(function (target, batchcmds, sourcefile_h2, opt) + + -- get cppfront + import("lib.detect.find_tool") + local cppfront = assert(find_tool("cppfront", {check = "-h"}), "cppfront not found!") + + -- get h header file for h2 + local sourcefile_h = target:autogenfile((sourcefile_h2:gsub(".h2$", ".h"))) + local basedir = path.directory(sourcefile_h) + + -- add commands + local argv = {"-o", path(sourcefile_h), path(sourcefile_h2)} + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.cpp2 %s", sourcefile_h2) + batchcmds:mkdir(basedir) + batchcmds:vrunv(cppfront.program, argv) + + -- add deps + batchcmds:add_depfiles(sourcefile_h2) + batchcmds:set_depmtime(os.mtime(sourcefile_h)) + batchcmds:set_depcache(target:dependfile(sourcefile_h)) + end) + -- define rule: cppfront.build rule("cppfront.build") set_extensions(".cpp2") + + -- .h2 must compile before .cpp2 + add_deps("cppfront.build.h2", {order = true}) + on_load(function (target) -- only cppfront source files? we need to patch cxx source kind for linker local sourcekinds = target:sourcekinds() @@ -66,6 +95,9 @@ rule("cppfront.build") -- define rule: cppfront rule("cppfront") + -- add_build.h2 rules + add_deps("cppfront.build.h2") + -- add build rules add_deps("cppfront.build") -- cgit v1.3.1 From 0539773b74bcc7eabab6f16a8d1760c79bf75a14 Mon Sep 17 00:00:00 2001 From: carols Date: Sun, 7 Apr 2024 22:11:58 +0800 Subject: cppfront.build->cppfront.build.cpp2, .h2 display compiling.h2 --- xmake/rules/cppfront/xmake.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'xmake/rules/cppfront/xmake.lua') diff --git a/xmake/rules/cppfront/xmake.lua b/xmake/rules/cppfront/xmake.lua index 4cd6a9675..b90222aa9 100644 --- a/xmake/rules/cppfront/xmake.lua +++ b/xmake/rules/cppfront/xmake.lua @@ -33,7 +33,7 @@ rule("cppfront.build.h2") -- add commands local argv = {"-o", path(sourcefile_h), path(sourcefile_h2)} - batchcmds:show_progress(opt.progress, "${color.build.object}compiling.cpp2 %s", sourcefile_h2) + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.h2 %s", sourcefile_h2) batchcmds:mkdir(basedir) batchcmds:vrunv(cppfront.program, argv) @@ -44,7 +44,7 @@ rule("cppfront.build.h2") end) -- define rule: cppfront.build -rule("cppfront.build") +rule("cppfront.build.cpp2") set_extensions(".cpp2") -- .h2 must compile before .cpp2 @@ -99,7 +99,7 @@ rule("cppfront") add_deps("cppfront.build.h2") -- add build rules - add_deps("cppfront.build") + add_deps("cppfront.build.cpp2") -- set compiler runtime, e.g. vs runtime add_deps("utils.compiler.runtime") -- cgit v1.3.1