summaryrefslogtreecommitdiff
path: root/xmake/modules/package/manager/cargo/find_package.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-02 12:26:14 +0800
committerruki <[email protected]>2022-04-02 12:26:14 +0800
commita9f5e6556fc8ee8584aedb9d4b5b88ab475bef0e (patch)
treef25ecd1018f930482b55cbebde81957a415f4fd1 /xmake/modules/package/manager/cargo/find_package.lua
parent3261ecb32c7c5c0c4c71e5b78a03b0cd203652f1 (diff)
improve to find cargo package
Diffstat (limited to 'xmake/modules/package/manager/cargo/find_package.lua')
-rw-r--r--xmake/modules/package/manager/cargo/find_package.lua48
1 files changed, 43 insertions, 5 deletions
diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua
index 08b3acb39..68fc60445 100644
--- a/xmake/modules/package/manager/cargo/find_package.lua
+++ b/xmake/modules/package/manager/cargo/find_package.lua
@@ -21,11 +21,44 @@
-- imports
import("core.base.option")
import("core.base.semver")
+import("core.base.hashset")
import("core.project.config")
import("core.project.target")
import("lib.detect.find_tool")
import("lib.detect.find_file")
+-- get the name set of libraries
+function _get_names_of_libraries(name, configs)
+ local names = hashset.new()
+ if configs.cargo_toml then
+ local dependencies = false
+ local cargo_file = io.open(configs.cargo_toml)
+ for line in cargo_file:lines() do
+ line = line:trim()
+ if not dependencies and line == "[dependencies]" then
+ dependencies = true
+ elseif dependencies then
+ if not line:startswith("[") then
+ local splitinfo = line:split("=", {plain = true})
+ if splitinfo and #splitinfo > 1 then
+ name = splitinfo[1]:trim()
+ if #name > 0 then
+ names:insert("lib" .. name:gsub("-", "_"))
+ end
+ end
+ else
+ break
+ end
+ end
+ end
+ cargo_file:close()
+ else
+ -- rust packages like actix-web will produce a file named actix_web-<...>
+ names:insert("lib" .. name:gsub("-", "_"))
+ end
+ return names
+end
+
-- find package using the cargo package manager
--
-- @param name the package name
@@ -33,8 +66,13 @@ import("lib.detect.find_file")
--
function main(name, opt)
- -- Rust packages like actix-web will produce a file named actix_web-<...>
- name = name:gsub("-", "_")
+ -- get configs
+ opt = opt or {}
+ local configs = opt.configs or {}
+
+ -- get names of libraries
+ local names = _get_names_of_libraries(name, configs)
+ assert(not names:empty())
local frameworkdirs
local frameworks
@@ -42,19 +80,19 @@ function main(name, opt)
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
+ local libraryname = filename:split('-', {plain = true})[1]
+ if names:has(libraryname) then
frameworkdirs = frameworkdirs or {}
frameworks = frameworks or {}
table.insert(frameworkdirs, librarydir)
table.insert(frameworks, libraryfile)
- break
end
end
local result
if frameworks and frameworkdirs then
result = result or {}
result.libfiles = libfiles
- result.frameworkdirs = frameworkdirs
+ result.frameworkdirs = frameworkdirs and table.unique(frameworkdirs) or nil
result.frameworks = frameworks
result.version = opt.require_version
end