diff options
| author | ruki <[email protected]> | 2021-09-16 22:44:11 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-16 22:44:11 +0800 |
| commit | b0de078312f7c87f54c9229b913992a495a3b2ed (patch) | |
| tree | a00f5709f96e3e278315dd41e2ba90b5ad657d9f | |
| parent | 8f3d4ec9654ba58a5a098213c2d28c01ecd1649c (diff) | |
improve find_library
6 files changed, 25 insertions, 19 deletions
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 959e94f5b..7b0ce47d9 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -2058,14 +2058,19 @@ function target.filename(targetname, targetkind, opt) end -- get the link name of the target file -function target.linkname(filename) +function target.linkname(filename, opt) -- for implib/mingw, e.g. libxxx.dll.a + opt = opt or {} if filename:startswith("lib") and filename:endswith(".dll.a") then return filename:sub(4, #filename - 6) end - local linkname, count = filename:gsub(target.filename("__pattern__", "static"):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") + local linkname, count = filename:gsub(target.filename("__pattern__", "static", {plat = opt.plat}):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") if count == 0 then - linkname, count = filename:gsub(target.filename("__pattern__", "shared"):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") + linkname, count = filename:gsub(target.filename("__pattern__", "shared", {plat = opt.plat}):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") + end + -- in order to be compatible with mingw/windows library with .lib + if count == 0 and opt.plat == "mingw" then + linkname, count = filename:gsub(target.filename("__pattern__", "static", {plat = "windows"}):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") end return count > 0 and linkname or nil end diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_library.lua b/xmake/core/sandbox/modules/import/lib/detect/find_library.lua index 0b783538c..4fb2618d3 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_library.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_library.lua @@ -38,7 +38,7 @@ local find_file = import("lib.detect.find_file") -- @param paths the search paths -- @param opt the options, e.g. {kind = "static/shared", suffixes = {"/aa", "/bb"}} -- --- @return {kind = "static", link = "crypto", linkdir = "/usr/local/lib", filename = "libcrypto.a"} +-- @return {kind = "static", link = "crypto", linkdir = "/usr/local/lib", filename = "libcrypto.a", plat = ..} -- -- @code -- @@ -54,23 +54,26 @@ function sandbox_lib_detect_find_library.main(names, paths, opt) return end - -- init options + -- find library file from the given paths opt = opt or {} - - -- init kinds local kinds = opt.kind or {"static", "shared"} - - -- find library file from the given paths for _, name in ipairs(table.wrap(names)) do for _, kind in ipairs(table.wrap(kinds)) do - local filepath = find_file(target.filename(name, kind), paths, opt) - if not filepath and kind == "shared" then - -- for implib/mingw, e.g. libxxx.dll.a - filepath = find_file(target.filename(name, kind) .. ".a", paths, opt) + local filepath = find_file(target.filename(name, kind, {plat = opt.plat}), paths, opt) + if opt.plat == "mingw" then + if not filepath and kind == "shared" then + -- for implib/mingw, e.g. libxxx.dll.a + filepath = find_file(target.filename(name, kind, {plat = opt.plat}) .. ".a", paths, opt) + end + if not filepath then + -- in order to be compatible with mingw/windows library with .lib + filepath = find_file(target.filename(name, kind, {plat = "windows"}), paths, opt) + end end if filepath then local filename = path.filename(filepath) - return {kind = kind, filename = filename, linkdir = path.directory(filepath), link = target.linkname(filename)} + local linkname = target.linkname(filename, {plat = opt.plat}) + return {kind = kind, filename = filename, linkdir = path.directory(filepath), link = linkname} end end end diff --git a/xmake/modules/package/manager/pkgconfig/find_package.lua b/xmake/modules/package/manager/pkgconfig/find_package.lua index a7dbf377a..ddc7efeb4 100644 --- a/xmake/modules/package/manager/pkgconfig/find_package.lua +++ b/xmake/modules/package/manager/pkgconfig/find_package.lua @@ -20,7 +20,6 @@ -- imports import("lib.detect.pkgconfig") -import("lib.detect.find_library") import("package.manager.system.find_package", {alias = "find_package_from_system"}) -- find package from the pkg-config package manager diff --git a/xmake/modules/package/manager/system/find_package.lua b/xmake/modules/package/manager/system/find_package.lua index 9bb04566d..692241309 100644 --- a/xmake/modules/package/manager/system/find_package.lua +++ b/xmake/modules/package/manager/system/find_package.lua @@ -60,7 +60,7 @@ function _find_package_from_unixdirs(name, links, opt) -- find library local result = nil for _, link in ipairs(links) do - local libinfo = find_library(link, linkdirs) + local libinfo = find_library(link, linkdirs, {plat = opt.plat}) if libinfo then result = result or {} result.links = table.join(result.links or {}, libinfo.link) diff --git a/xmake/modules/package/manager/vcpkg/find_package.lua b/xmake/modules/package/manager/vcpkg/find_package.lua index e965ace73..56ac15d23 100644 --- a/xmake/modules/package/manager/vcpkg/find_package.lua +++ b/xmake/modules/package/manager/vcpkg/find_package.lua @@ -20,7 +20,6 @@ -- imports import("lib.detect.find_file") -import("lib.detect.find_library") import("lib.detect.find_tool") import("core.base.option") import("core.project.config") diff --git a/xmake/modules/package/manager/xmake/find_package.lua b/xmake/modules/package/manager/xmake/find_package.lua index 0b043cc2f..379e29cbe 100644 --- a/xmake/modules/package/manager/xmake/find_package.lua +++ b/xmake/modules/package/manager/xmake/find_package.lua @@ -124,7 +124,7 @@ function _find_package_from_repo(name, opt) -- find library for _, link in ipairs(links) do - local libinfo = find_library(link, linkdirs) + local libinfo = find_library(link, linkdirs, {plat = opt.plat}) if libinfo then if libinfo.kind == "shared" then result.shared = true @@ -241,7 +241,7 @@ function _find_package_from_packagedirs(name, opt) -- find library local result = nil for _, link in ipairs(packageinfo:get("links")) do - local libinfo = find_library(link, linkdirs) + local libinfo = find_library(link, linkdirs, {plat = opt.plat}) if libinfo then result = result or {} result.links = table.join(result.links or {}, libinfo.link) |
