summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-11-11 22:37:39 +0800
committerruki <[email protected]>2021-11-11 22:37:39 +0800
commit147f4005fe0c549244ebddb660964585c5414652 (patch)
tree91606d57df0d2abcbe2d10e0f0089f8b64549902
parentcf9d3bacd0b0d47946e517dae67800fa7b8f3e1e (diff)
add rust.cxxbridge rules
-rw-r--r--tests/projects/rust/cxx_call_rust_library/xmake.lua1
-rw-r--r--xmake/rules/rust/build/cxxbridge.lua25
-rw-r--r--xmake/rules/rust/xmake.lua23
3 files changed, 40 insertions, 9 deletions
diff --git a/tests/projects/rust/cxx_call_rust_library/xmake.lua b/tests/projects/rust/cxx_call_rust_library/xmake.lua
index cfd0e035e..fb3a48171 100644
--- a/tests/projects/rust/cxx_call_rust_library/xmake.lua
+++ b/tests/projects/rust/cxx_call_rust_library/xmake.lua
@@ -3,6 +3,7 @@ add_rules("mode.debug", "mode.release")
target("foo")
set_kind("static")
add_files("src/foo.rs")
+ set_values("rust.cratetype", "staticlib")
target("test")
set_kind("binary")
diff --git a/xmake/rules/rust/build/cxxbridge.lua b/xmake/rules/rust/build/cxxbridge.lua
index 909006632..84b1f7fea 100644
--- a/xmake/rules/rust/build/cxxbridge.lua
+++ b/xmake/rules/rust/build/cxxbridge.lua
@@ -23,5 +23,28 @@ import("core.base.option")
import("lib.detect.find_tool")
function main(target, batchcmds, sourcefile, opt)
- print(sourcefile)
+ local cxxbridge = assert(find_tool("cxxbridge"), "cxxbridge not found!")
+
+ -- get c/c++ source file for cxxbridge
+ local headerfile = path.join(target:autogendir(), "rules", "cxxbridge", path.basename(sourcefile) .. ".rs.h")
+ local sourcefile_cx = path.join(target:autogendir(), "rules", "cxxbridge", path.basename(sourcefile) .. ".rs.cc")
+
+ -- add includedirs
+ target:add("includedirs", path.directory(headerfile))
+
+ -- add objectfile
+ local objectfile = target:objectfile(sourcefile_cx)
+ table.insert(target:objectfiles(), objectfile)
+
+ -- add commands
+ batchcmds:show_progress(opt.progress, "${color.build.object}compiling.cxxbridge %s", sourcefile)
+ batchcmds:mkdir(path.directory(sourcefile_cx))
+ batchcmds:vrunv(cxxbridge.program, {sourcefile}, {stdout = sourcefile_cx})
+ batchcmds:vrunv(cxxbridge.program, {sourcefile, "--header"}, {stdout = headerfile})
+ batchcmds:compile(sourcefile_cx, objectfile)
+
+ -- add deps
+ batchcmds:add_depfiles(sourcefile)
+ batchcmds:set_depmtime(os.mtime(objectfile))
+ batchcmds:set_depcache(target:dependfile(objectfile))
end
diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua
index b3599e43b..e0952b72e 100644
--- a/xmake/rules/rust/xmake.lua
+++ b/xmake/rules/rust/xmake.lua
@@ -22,15 +22,28 @@
-- @see https://cxx.rs/build/other.html
rule("rust.cxxbridge")
set_extensions(".rsx")
+ on_load(function (target)
+ if not target:get("languages") then
+ target:set("languages", "c++11")
+ end
+ end)
before_buildcmd_file("build.cxxbridge")
--- define rule: rust.build
rule("rust.build")
set_sourcekinds("rc")
on_load(function (target)
- if target:is_static() then
+ local cratetype = target:values("rust.cratetype")
+ if cratetype == "staticlib" then
+ assert(target:is_static(), "target(%s) must be static kind for cratetype(staticlib)!", target:name())
+ target:add("arflags", "--crate-type=staticlib")
+ elseif cratetype == "cdylib" then
+ assert(target:is_shared(), "target(%s) must be shared kind for cratetype(cdylib)!", target:name())
+ target:add("shflags", "--crate-type=cdylib")
+ target:add("shflags", "-C prefer-dynamic")
+ elseif target:is_static() then
target:set("extension", ".rlib")
target:add("arflags", "--crate-type=lib")
+ target:data_set("inherit.links.deplink", false)
elseif target:is_shared() then
target:add("shflags", "--crate-type=dylib")
-- fix cannot satisfy dependencies so `std` only shows up once
@@ -42,15 +55,9 @@ rule("rust.build")
elseif target:is_binary() then
target:add("ldflags", "--crate-type=bin")
end
- target:data_set("inherit.links.deplink", false)
end)
on_build("build.target")
--- define rule: rust
rule("rust")
-
- -- add build rules
add_deps("rust.build")
-
- -- inherit links and linkdirs of all dependent targets by default
add_deps("utils.inherit.links")