Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cppfront rule #4140

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/projects/cppfront/console/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


7 changes: 7 additions & 0 deletions tests/projects/cppfront/console/src/main.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
main: () -> int =
println("Hello world!\n");

println: (msg: _) -> int = {
std::cout << msg;
return 0;
}
10 changes: 10 additions & 0 deletions tests/projects/cppfront/console/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +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")

84 changes: 84 additions & 0 deletions xmake/rules/cppfront/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
--!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)
-- 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_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("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")

Loading