summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-06-17 09:01:43 +0800
committerGitHub <[email protected]>2024-06-17 09:01:43 +0800
commiteb8de7ea5792dce9cd9a76f59086678111a94def (patch)
treec25d375f7c5fc6e9f46978a9a618574a0905c681
parent417313e29e1d85c8a3157a58f023df307ae28492 (diff)
parentba03d63bedaab53f77aaacd2550ff32c53e4211d (diff)
Merge pull request #5220 from xq114/dev
fix cmake package finding regarding target mode
-rw-r--r--xmake/modules/package/manager/cmake/find_package.lua37
1 files changed, 23 insertions, 14 deletions
diff --git a/xmake/modules/package/manager/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua
index 48063b5dd..6c3a9f194 100644
--- a/xmake/modules/package/manager/cmake/find_package.lua
+++ b/xmake/modules/package/manager/cmake/find_package.lua
@@ -24,6 +24,21 @@ import("core.base.hashset")
import("core.project.target")
import("lib.detect.find_tool")
+-- exclude cmake internal definitions https://github.com/xmake-io/xmake/issues/5217
+function _should_exclude(define)
+ local name = define:split("=")[1]
+ return table.contains({"CMAKE_INTDIR", "_DEBUG", "NDEBUG"}, name)
+end
+
+-- map xmake mode to cmake mode
+function _cmake_mode(mode)
+ if mode == "debug" then return "Debug"
+ elseif mode == "releasedbg" then return "RelWithDebInfo"
+ elseif mode == "minsizerel" then return "MinSizeRel"
+ else return "Release"
+ end
+end
+
-- find package
function _find_package(cmake, name, opt)
@@ -112,11 +127,8 @@ function _find_package(cmake, name, opt)
end
-- run cmake
- local envs = configs.envs or opt.envs
- if opt.mode == "debug" then
- envs = envs or {}
- envs.CMAKE_BUILD_TYPE = envs.CMAKE_BUILD_TYPE or "Debug"
- end
+ local envs = configs.envs or opt.envs or {}
+ envs.CMAKE_BUILD_TYPE = envs.CMAKE_BUILD_TYPE or _cmake_mode(opt.mode or "release")
try {function() return os.vrunv(cmake.program, {workdir}, {curdir = workdir, envs = envs}) end}
-- pares defines and includedirs for macosx/linux
@@ -150,12 +162,12 @@ function _find_package(cmake, name, opt)
end
end
elseif line:find("CXX_DEFINES =", 1, true) then
+ defines = defines or {}
local flags = os.argv(line:split("=", {plain = true})[2]:trim())
for _, flag in ipairs(flags) do
if flag:startswith("-D") and #flag > 2 then
local define = flag:sub(3)
- if define then
- defines = defines or {}
+ if define and not _should_exclude(define) then
table.insert(defines, define)
end
end
@@ -225,6 +237,9 @@ function _find_package(cmake, name, opt)
local vcprojfile = path.join(workdir, testname .. ".vcxproj")
if os.isfile(vcprojfile) then
local vcprojdata = io.readfile(vcprojfile)
+ local vs_mode = envs.CMAKE_BUILD_TYPE or _cmake_mode(opt.mode or "release")
+ vcprojdata = vcprojdata:match("<ItemDefinitionGroup Condition=\"'$%(Configuration%)|$%(Platform%)'=='" .. vs_mode .. "|.->(.-)</ItemDefinitionGroup>")
+
if vcprojdata then
for _, line in ipairs(vcprojdata:split("\n", {plain = true})) do
local values = line:match("<AdditionalIncludeDirectories>(.+);%%%(AdditionalIncludeDirectories%)</AdditionalIncludeDirectories>")
@@ -259,16 +274,10 @@ function _find_package(cmake, name, opt)
values = line:match("<PreprocessorDefinitions>%%%(PreprocessorDefinitions%);(.+)</PreprocessorDefinitions>")
if values then
- -- https://github.com/xmake-io/xmake/issues/5217
- local excludes = hashset.from {
- "NDEBUG", "_DEBUG", "CMAKE_INTDIR", "WIN32",
- "_WINDOWS", "USE_DISTRIBUTED", "USE_C10D_GLOO"
- }
defines = defines or {}
values = path.splitenv(values)
for _, value in ipairs(values) do
- local name = value:split("=")[1]
- if not excludes:has(name) then
+ if not _should_exclude(value) then
table.insert(defines, value)
end
end