summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-12-17 22:33:57 +0800
committerruki <[email protected]>2021-12-17 22:33:57 +0800
commite8aa2b2b36fc5977bb0997cf2697837435937e98 (patch)
tree25df1ba52f7661db77c093fd005ab4cc28da9106
parent369afb0809747329524839edbeab813ad3cf0de8 (diff)
improve cmake.find_package and check gcc ldflags
-rw-r--r--xmake/modules/detect/tools/gcc/has_flags.lua27
-rw-r--r--xmake/modules/package/manager/cmake/find_package.lua16
2 files changed, 22 insertions, 21 deletions
diff --git a/xmake/modules/detect/tools/gcc/has_flags.lua b/xmake/modules/detect/tools/gcc/has_flags.lua
index 7b4a40fd2..f7cf1d29e 100644
--- a/xmake/modules/detect/tools/gcc/has_flags.lua
+++ b/xmake/modules/detect/tools/gcc/has_flags.lua
@@ -44,36 +44,27 @@ end
-- attempt to check it from the argument list
function _check_from_arglist(flags, opt, islinker)
-
- -- only for compiler
- if islinker or #flags > 1 then
- return
- end
-
- -- make cache key
- local key = "detect.tools.gcc.has_flags"
-
- -- make flags key
+ local key = "detect.tools.gcc." .. (islinker and "has_ldflags" or "has_cflags")
local flagskey = opt.program .. "_" .. (opt.programver or "")
-
- -- get all flags from argument list
local allflags = detectcache:get2(key, flagskey)
if not allflags then
-
- -- get argument list
allflags = {}
- local arglist = os.iorunv(opt.program, {"--help"}, {envs = opt.envs})
+ local arglist = try {function () return os.iorunv(opt.program, {islinker and "-Wl,--help" or "--help"}, {envs = opt.envs}) end}
if arglist then
for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do
allflags[arg] = true
end
end
-
- -- save cache
detectcache:set2(key, flagskey, allflags)
detectcache:save()
end
- return allflags[flags[1]]
+ local flag = flags[1]
+ if islinker and flag then
+ if flag:startswith("-Wl,") then
+ flag = flag:match("-Wl,(.-),") or flag:sub(5)
+ end
+ end
+ return allflags[flag]
end
-- get extension
diff --git a/xmake/modules/package/manager/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua
index b78b784c5..d9abd6b00 100644
--- a/xmake/modules/package/manager/cmake/find_package.lua
+++ b/xmake/modules/package/manager/cmake/find_package.lua
@@ -96,6 +96,7 @@ function _find_package(cmake, name, opt)
local libfiles
local defines
local includedirs
+ local ldflags
local flagsfile = path.join(workdir, "CMakeFiles", name .. ".dir", "flags.make")
if os.isfile(flagsfile) then
local flagsdata = io.readfile(flagsfile)
@@ -144,14 +145,21 @@ function _find_package(cmake, name, opt)
vprint(linkdata)
end
for _, line in ipairs(os.argv(linkdata)) do
+ local is_ldflags = false
local is_library = false
for _, suffix in ipairs({".so", ".dylib", ".dylib", ".tbd", ".lib"}) do
- if line:find(suffix, 1, true) then
+ if line:startswith("-Wl,") then
+ is_ldflags = true
+ break
+ elseif line:find(suffix, 1, true) then
is_library = true
break
end
end
- if is_library then
+ if is_ldflags then
+ ldflags = ldflags or {}
+ table.insert(ldflags, line)
+ elseif is_library then
-- strip library version suffix, e.g. libxxx.so.1.1 -> libxxx.so
if line:find(".so", 1, true) then
line = line:gsub("lib(.-)%.so%..+$", "lib%1.so")
@@ -207,7 +215,7 @@ function _find_package(cmake, name, opt)
-- get links and linkdirs
local linkdir = path.directory(library)
- linkdir = path.translate(linkdir)
+ linkdir = path.translate(linkdir)
if linkdir ~= "." and not linkdir:startswith(workdir) then
linkdirs = linkdirs or {}
table.insert(linkdirs, linkdir)
@@ -230,10 +238,12 @@ function _find_package(cmake, name, opt)
if links or includedirs then
local results = {}
results.links = table.reverse_unique(links)
+ results.ldflags = table.reverse_unique(ldflags)
results.linkdirs = table.unique(linkdirs)
results.defines = table.unique(defines)
results.libfiles = table.unique(libfiles)
results.includedirs = table.unique(includedirs)
+ print(results)
return results
end
end