From 71217ef2ce95a70aad0fd1cad05eb462b4612a5b Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 27 Oct 2021 22:36:35 +0800 Subject: remove some comments --- xmake/modules/core/tools/rustc.lua | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'xmake/modules/core/tools/rustc.lua') diff --git a/xmake/modules/core/tools/rustc.lua b/xmake/modules/core/tools/rustc.lua index ade2daea4..a30a389af 100644 --- a/xmake/modules/core/tools/rustc.lua +++ b/xmake/modules/core/tools/rustc.lua @@ -41,8 +41,6 @@ end -- make the optimize flag function nf_optimize(self, level) - - -- the maps local maps = { none = "-C opt-level=0" @@ -52,21 +50,15 @@ function nf_optimize(self, level) , smallest = "-C opt-level=s" , aggressive = "-C opt-level=z" } - - -- make it return maps[level] end -- make the symbol flag function nf_symbol(self, level) - - -- the maps local maps = { debug = "-C debuginfo=2" } - - -- make it return maps[level] end @@ -82,11 +74,7 @@ end -- build the target file function build(self, sourcefiles, targetkind, targetfile, flags) - - -- ensure the target directory os.mkdir(path.directory(targetfile)) - - -- build it os.runv(buildargv(self, sourcefiles, targetkind, targetfile, flags)) end @@ -97,11 +85,7 @@ end -- compile the source file function compile(self, sourcefiles, objectfile, dependinfo, flags) - - -- ensure the object directory os.mkdir(path.directory(objectfile)) - - -- compile it os.runv(compargv(self, sourcefiles, objectfile, flags)) end -- cgit v1.3.1 From bc404bc6481de0c934aa5ff4523924688b72a2d4 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 10 Nov 2021 23:05:38 +0800 Subject: fix rpath for rust --- xmake/languages/rust/xmake.lua | 14 ++++++ xmake/modules/core/tools/rustc.lua | 52 +++++++++++++++++------ xmake/modules/detect/tools/rustc/has_flags.lua | 28 +++++++++--- xmake/rules/rust/xmake.lua | 17 ++++++++ xmake/rules/utils/inherit_links/inherit_links.lua | 16 ++++--- 5 files changed, 100 insertions(+), 27 deletions(-) (limited to 'xmake/modules/core/tools/rustc.lua') diff --git a/xmake/languages/rust/xmake.lua b/xmake/languages/rust/xmake.lua index dc6ae3d15..fef2af363 100644 --- a/xmake/languages/rust/xmake.lua +++ b/xmake/languages/rust/xmake.lua @@ -45,13 +45,27 @@ language("rust") , "target.symbols" , "toolchain.linkdirs" , "toolchain.rpathdirs" + , "config.links" + , "target.links" + , "toolchain.links" + , "config.syslinks" + , "target.syslinks" + , "toolchain.syslinks" } , shared = { "config.linkdirs" , "target.linkdirs" + , "target.rpathdirs" , "target.strip" , "target.symbols" , "toolchain.linkdirs" + , "toolchain.rpathdirs" + , "config.links" + , "target.links" + , "toolchain.links" + , "config.syslinks" + , "target.syslinks" + , "toolchain.syslinks" } , static = { "target.strip" diff --git a/xmake/modules/core/tools/rustc.lua b/xmake/modules/core/tools/rustc.lua index a30a389af..fa358e897 100644 --- a/xmake/modules/core/tools/rustc.lua +++ b/xmake/modules/core/tools/rustc.lua @@ -25,18 +25,6 @@ import("core.project.project") -- init it function init(self) - - -- init arflags - self:set("rcarflags", "--crate-type=lib") - - -- init shflags - self:set("rcshflags", "--crate-type=dylib") - - -- init ldflags - self:set("rcldflags", "--crate-type=bin") - - -- init the file formats - self:set("formats", { static = "lib$(name).rlib" }) end -- make the optimize flag @@ -67,9 +55,47 @@ function nf_linkdir(self, dir) return {"-L" .. dir} end +-- make the link flag +function nf_link(self, lib) + return "-l" .. lib +end + +-- make the syslink flag +function nf_syslink(self, lib) + return nf_link(self, lib) +end + +-- make the rpathdir flag +function nf_rpathdir(self, dir) + dir = path.translate(dir) + if self:has_flags({"-C", "link-arg=-Wl,-rpath=$ORIGIN"}, "ldflags") then + return {"-C", "link-arg=-Wl,-rpath=" .. (dir:gsub("@[%w_]+", function (name) + local maps = {["@loader_path"] = "$ORIGIN", ["@executable_path"] = "$ORIGIN"} + return maps[name] + end))} + elseif self:has_flags({"-C", "link-arg=-Xlinker", "-C", "link-arg=-rpath", "-C", "link-arg=-Xlinker", "-C", "link-arg=@loader_path"}, "ldflags") then + return {"-C", "link-arg=-Xlinker", + "-C", "link-arg=-rpath", + "-C", "link-arg=-Xlinker", + "-C", "link-arg=" .. (dir:gsub("%$ORIGIN", "@loader_path"))} + end +end + -- make the build arguments list function buildargv(self, sourcefiles, targetkind, targetfile, flags) - return self:program(), table.join(flags, "-o", targetfile, sourcefiles) + -- add rpath for dylib (macho), e.g. -install_name @rpath/file.dylib + local flags_extra = {} + if targetkind == "shared" and is_plat("macosx", "iphoneos", "watchos") then + table.insert(flags_extra, "-C") + table.insert(flags_extra, "link-arg=-Xlinker") + table.insert(flags_extra, "-C") + table.insert(flags_extra, "link-arg=-install_name") + table.insert(flags_extra, "-C") + table.insert(flags_extra, "link-arg=-Xlinker") + table.insert(flags_extra, "-C") + table.insert(flags_extra, "link-arg=@rpath/" .. path.filename(targetfile)) + end + return self:program(), table.join(flags, flags_extra, "-o", targetfile, sourcefiles) end -- build the target file diff --git a/xmake/modules/detect/tools/rustc/has_flags.lua b/xmake/modules/detect/tools/rustc/has_flags.lua index 904a3c85a..e1696e454 100644 --- a/xmake/modules/detect/tools/rustc/has_flags.lua +++ b/xmake/modules/detect/tools/rustc/has_flags.lua @@ -21,6 +21,16 @@ -- imports import("core.cache.detectcache") +-- is linker? +function _islinker(flags, opt) + local flags_str = table.concat(flags, " ") + if flags_str:startswith("-C linkarg=") then + return true + end + local toolkind = opt.toolkind or "" + return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("ld") or toolkind:endswith("sh") +end + -- try running function _try_running(...) @@ -64,7 +74,7 @@ function _check_from_arglist(flags, opt) end -- try running to check flags -function _check_try_running(flags, opt) +function _check_try_running(flags, opt, islinker) -- make an stub source file local sourcefile = path.join(os.tmpdir(), "detect", "rustc_has_flags.rs") @@ -72,14 +82,15 @@ function _check_try_running(flags, opt) io.writefile(sourcefile, "fn main() {\n}") end - -- check it + -- check flags for linker + if islinker then + return _try_running(opt.program, table.join("--crate-type=bin", flags, "-o", os.tmpfile(), sourcefile), opt) + end + + -- check flags for compiler local objectfile = os.tmpfile() .. ".o" local ok, errors = _try_running(opt.program, table.join("--emit", "obj", flags, "-o", objectfile, sourcefile)) - - -- remove files os.tryrm(objectfile) - - -- ok? return ok, errors end @@ -91,12 +102,15 @@ end -- function main(flags, opt) + -- is linker? + local islinker = _islinker(flags, opt) + -- attempt to check it from the argument list if _check_from_arglist(flags, opt) then return true end -- try running to check it - return _check_try_running(flags, opt) + return _check_try_running(flags, opt, islinker) end diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua index a8ccae429..b823ef753 100644 --- a/xmake/rules/rust/xmake.lua +++ b/xmake/rules/rust/xmake.lua @@ -21,6 +21,23 @@ -- define rule: rust.build rule("rust.build") set_sourcekinds("rc") + on_load(function (target) + if target:is_static() then + target:set("extension", ".rlib") + target:add("arflags", "--crate-type=lib") + elseif target:is_shared() then + target:add("shflags", "--crate-type=dylib") + -- fix cannot satisfy dependencies so `std` only shows up once + -- https://github.com/rust-lang/rust/issues/19680 + -- + -- but it will link dynamic @rpath/libstd-xxx.dylib, + -- so we can no longer modify and set other rpath paths + target:add("shflags", "-C prefer-dynamic") + 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 diff --git a/xmake/rules/utils/inherit_links/inherit_links.lua b/xmake/rules/utils/inherit_links/inherit_links.lua index d5567b605..805f535d8 100644 --- a/xmake/rules/utils/inherit_links/inherit_links.lua +++ b/xmake/rules/utils/inherit_links/inherit_links.lua @@ -46,7 +46,6 @@ function _add_export_value(target, name, value) end end --- main entry function main(target) -- disable inherit.links for `add_deps()`? @@ -59,12 +58,15 @@ function main(target) if targetkind == "shared" or targetkind == "static" then local targetfile = target:targetfile() - -- we need move target link to head - _add_export_value(target, "links", target:linkname()) - local links = target:get("links", {rawref = true}) - if links and type(links) == "table" and #links > 1 then - table.insert(links, 1, links[#links]) - table.remove(links, #links) + -- rust maybe will disable inherit links, only inherit linkdirs + if target:data("inherit.links.deplink") ~= false then + -- we need move target link to head + _add_export_value(target, "links", target:linkname()) + local links = target:get("links", {rawref = true}) + if links and type(links) == "table" and #links > 1 then + table.insert(links, 1, links[#links]) + table.remove(links, #links) + end end _add_export_value(target, "linkdirs", path.directory(targetfile)) -- cgit v1.3.1 From 6b684d5cc448124c31a51fff71e4043667677a57 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 13 Nov 2021 13:46:36 +0800 Subject: add frameworks for rust --- tests/projects/rust/cxx_call_rust_library/xmake.lua | 1 + xmake/languages/rust/xmake.lua | 14 +++++++++++++- xmake/modules/core/tools/rustc.lua | 9 +++++++++ xmake/modules/package/manager/cargo/find_package.lua | 6 +++++- xmake/rules/rust/xmake.lua | 1 + xmake/rules/utils/inherit_links/inherit_links.lua | 12 +++++++----- 6 files changed, 36 insertions(+), 7 deletions(-) (limited to 'xmake/modules/core/tools/rustc.lua') diff --git a/tests/projects/rust/cxx_call_rust_library/xmake.lua b/tests/projects/rust/cxx_call_rust_library/xmake.lua index 30d54cc02..cafd349ee 100644 --- a/tests/projects/rust/cxx_call_rust_library/xmake.lua +++ b/tests/projects/rust/cxx_call_rust_library/xmake.lua @@ -7,6 +7,7 @@ target("foo") add_files("src/foo.rs") set_values("rust.cratetype", "staticlib") add_packages("cargo::cxx") + add_rcflags("--edition=2018") target("test") set_kind("binary") diff --git a/xmake/languages/rust/xmake.lua b/xmake/languages/rust/xmake.lua index b07617a61..1335225e3 100644 --- a/xmake/languages/rust/xmake.lua +++ b/xmake/languages/rust/xmake.lua @@ -45,6 +45,9 @@ language("rust") , "target.symbols" , "toolchain.linkdirs" , "toolchain.rpathdirs" + , "config.frameworks" + , "target.frameworks" + , "toolchain.frameworks" , "config.links" , "target.links" , "toolchain.links" @@ -60,6 +63,9 @@ language("rust") , "target.symbols" , "toolchain.linkdirs" , "toolchain.rpathdirs" + , "config.frameworks" + , "target.frameworks" + , "toolchain.frameworks" , "config.links" , "target.links" , "toolchain.links" @@ -68,8 +74,14 @@ language("rust") , "toolchain.syslinks" } , static = { - "target.strip" + "config.linkdirs" + , "target.linkdirs" + , "target.strip" , "target.symbols" + , "toolchain.linkdirs" + , "config.frameworks" + , "target.frameworks" + , "toolchain.frameworks" } } diff --git a/xmake/modules/core/tools/rustc.lua b/xmake/modules/core/tools/rustc.lua index fa358e897..19588136a 100644 --- a/xmake/modules/core/tools/rustc.lua +++ b/xmake/modules/core/tools/rustc.lua @@ -65,6 +65,15 @@ function nf_syslink(self, lib) return nf_link(self, lib) end +-- make the framework flag, crate module +function nf_framework(self, framework) + local basename = path.basename(framework) + local cratename = basename:match("lib(.-)%-.-") or basename:match("lib(.-)") + if cratename then + return {"--extern", cratename .. "=" .. framework} + end +end + -- make the rpathdir flag function nf_rpathdir(self, dir) dir = path.translate(dir) diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua index 336b4243a..cc546704e 100644 --- a/xmake/modules/package/manager/cargo/find_package.lua +++ b/xmake/modules/package/manager/cargo/find_package.lua @@ -33,21 +33,25 @@ import("lib.detect.find_file") -- function main(name, opt) local linkdirs + local frameworks local librarydir = path.join(opt.installdir, "lib") local libfiles = os.files(path.join(librarydir, "*.rlib")) for _, libraryfile in ipairs(libfiles) do local filename = path.filename(libraryfile) if filename:startswith("lib" .. name .. "-") then linkdirs = linkdirs or {} + frameworks = frameworks or {} table.insert(linkdirs, librarydir) + table.insert(frameworks, libraryfile) break end end local result - if linkdirs then + if frameworks and linkdirs then result = result or {} result.libfiles = libfiles result.linkdirs = linkdirs + result.frameworks = frameworks result.version = opt.require_version end return result diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua index e0952b72e..f371692e1 100644 --- a/xmake/rules/rust/xmake.lua +++ b/xmake/rules/rust/xmake.lua @@ -36,6 +36,7 @@ rule("rust.build") 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") + target:data_set("inherit.links.exportlinks", false) 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") diff --git a/xmake/rules/utils/inherit_links/inherit_links.lua b/xmake/rules/utils/inherit_links/inherit_links.lua index 805f535d8..79c737503 100644 --- a/xmake/rules/utils/inherit_links/inherit_links.lua +++ b/xmake/rules/utils/inherit_links/inherit_links.lua @@ -80,11 +80,13 @@ function main(target) -- @note we only export links for static target, -- and we need pass `{public = true}` to add_packages/add_links/... to export it if want to export links for shared target -- - if targetkind == "static" then - for _, name in ipairs({"rpathdirs", "frameworkdirs", "frameworks", "linkdirs", "links", "syslinks"}) do - local values = _get_values_from_target(target, name) - if values and #values > 0 then - target:add(name, values, {public = true}) + if target:data("inherit.links.exportlinks") ~= false then + if targetkind == "static" then + for _, name in ipairs({"rpathdirs", "frameworkdirs", "frameworks", "linkdirs", "links", "syslinks"}) do + local values = _get_values_from_target(target, name) + if values and #values > 0 then + target:add(name, values, {public = true}) + end end end end -- cgit v1.3.1 From 80326fdfb7fa838d3dc79aa24c9dcc01519b173b Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 13 Nov 2021 13:52:57 +0800 Subject: add edition for rust --- tests/projects/rust/cxx_call_rust_library/xmake.lua | 1 - xmake/languages/rust/xmake.lua | 9 +++++++++ xmake/modules/core/tools/rustc.lua | 5 +++++ xmake/modules/package/manager/cargo/find_package.lua | 10 +++++----- xmake/rules/rust/xmake.lua | 5 +++++ 5 files changed, 24 insertions(+), 6 deletions(-) (limited to 'xmake/modules/core/tools/rustc.lua') diff --git a/tests/projects/rust/cxx_call_rust_library/xmake.lua b/tests/projects/rust/cxx_call_rust_library/xmake.lua index cafd349ee..30d54cc02 100644 --- a/tests/projects/rust/cxx_call_rust_library/xmake.lua +++ b/tests/projects/rust/cxx_call_rust_library/xmake.lua @@ -7,7 +7,6 @@ target("foo") add_files("src/foo.rs") set_values("rust.cratetype", "staticlib") add_packages("cargo::cxx") - add_rcflags("--edition=2018") target("test") set_kind("binary") diff --git a/xmake/languages/rust/xmake.lua b/xmake/languages/rust/xmake.lua index 1335225e3..ea71910b7 100644 --- a/xmake/languages/rust/xmake.lua +++ b/xmake/languages/rust/xmake.lua @@ -39,11 +39,14 @@ language("rust") } , binary = { "config.linkdirs" + , "config.frameworkdirs" , "target.linkdirs" + , "target.frameworkdirs" , "target.rpathdirs" , "target.strip" , "target.symbols" , "toolchain.linkdirs" + , "toolchain.frameworkdirs" , "toolchain.rpathdirs" , "config.frameworks" , "target.frameworks" @@ -57,11 +60,14 @@ language("rust") } , shared = { "config.linkdirs" + , "config.frameworkdirs" , "target.linkdirs" + , "target.frameworkdirs" , "target.rpathdirs" , "target.strip" , "target.symbols" , "toolchain.linkdirs" + , "toolchain.frameworkdirs" , "toolchain.rpathdirs" , "config.frameworks" , "target.frameworks" @@ -75,10 +81,13 @@ language("rust") } , static = { "config.linkdirs" + , "config.frameworkdirs" , "target.linkdirs" + , "target.frameworkdirs" , "target.strip" , "target.symbols" , "toolchain.linkdirs" + , "toolchain.frameworkdirs" , "config.frameworks" , "target.frameworks" , "toolchain.frameworks" diff --git a/xmake/modules/core/tools/rustc.lua b/xmake/modules/core/tools/rustc.lua index 19588136a..ab41620cd 100644 --- a/xmake/modules/core/tools/rustc.lua +++ b/xmake/modules/core/tools/rustc.lua @@ -65,6 +65,11 @@ function nf_syslink(self, lib) return nf_link(self, lib) end +-- make the frameworkdir flag, crate module dependency directories +function nf_frameworkdir(self, frameworkdir) + return {"-L", "dependency=" .. frameworkdir} +end + -- make the framework flag, crate module function nf_framework(self, framework) local basename = path.basename(framework) diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua index cc546704e..7fd151d5c 100644 --- a/xmake/modules/package/manager/cargo/find_package.lua +++ b/xmake/modules/package/manager/cargo/find_package.lua @@ -32,25 +32,25 @@ import("lib.detect.find_file") -- @param opt the options, e.g. {verbose = true, require_version = "1.12.x") -- function main(name, opt) - local linkdirs + local frameworkdirs local frameworks local librarydir = path.join(opt.installdir, "lib") local libfiles = os.files(path.join(librarydir, "*.rlib")) for _, libraryfile in ipairs(libfiles) do local filename = path.filename(libraryfile) if filename:startswith("lib" .. name .. "-") then - linkdirs = linkdirs or {} + frameworkdirs = frameworkdirs or {} frameworks = frameworks or {} - table.insert(linkdirs, librarydir) + table.insert(frameworkdirs, librarydir) table.insert(frameworks, libraryfile) break end end local result - if frameworks and linkdirs then + if frameworks and frameworkdirs then result = result or {} result.libfiles = libfiles - result.linkdirs = linkdirs + result.frameworkdirs = frameworkdirs result.frameworks = frameworks result.version = opt.require_version end diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua index f371692e1..e31e2f046 100644 --- a/xmake/rules/rust/xmake.lua +++ b/xmake/rules/rust/xmake.lua @@ -32,6 +32,7 @@ rule("rust.cxxbridge") rule("rust.build") set_sourcekinds("rc") on_load(function (target) + -- set cratetype 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()) @@ -56,6 +57,10 @@ rule("rust.build") elseif target:is_binary() then target:add("ldflags", "--crate-type=bin") end + + -- set edition + local edition = target:values("rust.edition") or "2018" + target:add("rcflags", "--edition", edition, {force = true}) end) on_build("build.target") -- cgit v1.3.1