diff options
| author | RichardYCJ <[email protected]> | 2023-03-11 01:37:26 +0800 |
|---|---|---|
| committer | RichardYCJ <[email protected]> | 2023-03-11 01:37:26 +0800 |
| commit | 782550ef08e2f9dc107c46e5fda6040793d7d2ce (patch) | |
| tree | 243a27231edcaf4bea7ffecb05cff55b9ac8f5bd | |
| parent | 1c254ee6ab33a0701c57b5e9dafa2b1641b3c929 (diff) | |
improve cmake::find_package use include_dirs instead of find_name
use `include_dirs` and `link_libraries` to customize cmake detection.
| -rw-r--r-- | xmake/modules/package/manager/cmake/configurations.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/package/manager/cmake/find_package.lua | 30 |
2 files changed, 19 insertions, 13 deletions
diff --git a/xmake/modules/package/manager/cmake/configurations.lua b/xmake/modules/package/manager/cmake/configurations.lua index f8ba1377a..db5136998 100644 --- a/xmake/modules/package/manager/cmake/configurations.lua +++ b/xmake/modules/package/manager/cmake/configurations.lua @@ -23,12 +23,12 @@ function main() return { link_libraries = {description = "Set the cmake package dependencies, e.g. {\"abc::lib1\", \"abc::lib2\"}"}, + include_dirs = {description = "Set the cmake package include directories, e.g. {\"${ZLIB_INCLUDE_DIRS}\"}"}, search_mode = {description = "Set the cmake package search mode, e.g. {\"config\", \"module\"}"}, components = {description = "Set the cmake package components, e.g. {\"regex\", \"system\"}"}, moduledirs = {description = "Set the cmake modules directories."}, presets = {description = "Set the preset values, e.g. {Boost_USE_STATIC_LIB = true}"}, envs = {description = "Set the run environments of cmake, e.g. {CMAKE_PREFIX_PATH = \"xxx\"}"}, - find_name = {description = "Set the name used for ${find_name}_LIBS/_INCLUDE_DIRS in cmake"}, debug = {description = "Set the CMAKE_BUILD_TYPE to Debug if set to true"}, } end diff --git a/xmake/modules/package/manager/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua index a16c8ceeb..c73a2ef4c 100644 --- a/xmake/modules/package/manager/cmake/find_package.lua +++ b/xmake/modules/package/manager/cmake/find_package.lua @@ -78,24 +78,30 @@ function _find_package(cmake, name, opt) end end local testname = "test_" .. name - local findname = configs.find_name or name cmakefile:print("find_package(%s REQUIRED %s)", requirestr, componentstr) cmakefile:print("if(%s_FOUND)", name) cmakefile:print(" add_executable(%s test.cpp)", testname) - cmakefile:print(" target_include_directories(%s PRIVATE ${%s_INCLUDE_DIR} ${%s_INCLUDE_DIRS})", - testname, findname, findname) - cmakefile:print(" target_include_directories(%s PRIVATE ${%s_INCLUDE_DIR} ${%s_INCLUDE_DIRS})", - testname, findname:upper(), findname:upper()) + -- setup include directories + local includedirs = "" + if configs.include_dirs then + includedirs = table.concat(table.wrap(configs.include_dirs), " ") + else + includedirs = string.gsub("${X_INCLUDE_DIR} ${X_INCLUDE_DIRS}", "X", name) + includedirs = includedirs .. string.gsub(" ${X_INCLUDE_DIR} ${X_INCLUDE_DIRS}", "X", name:upper()) + end + cmakefile:print(" target_include_directories(%s PRIVATE %s)", testname, includedirs) + -- reserved for backword compatibility cmakefile:print(" target_include_directories(%s PRIVATE ${%s_CXX_INCLUDE_DIRS})", - testname, findname) - cmakefile:print(" target_link_libraries(%s ${%s_LIBRARY} ${%s_LIBRARIES} ${%s_LIBS})", - testname, findname, findname, findname) - cmakefile:print(" target_link_libraries(%s ${%s_LIBRARY} ${%s_LIBRARIES} ${%s_LIBS})", - testname, findname:upper(), findname:upper(), findname:upper()) + testname, name) + -- setup link library/target + local linklibs = "" if configs.link_libraries then - cmakefile:print(" target_link_libraries(%s %s)", - testname, table.concat(table.wrap(configs.link_libraries), " ")) + linklibs = table.concat(table.wrap(configs.link_libraries), " ") + else + linklibs = string.gsub("${X_LIBRARY} ${X_LIBRARIES} ${X_LIBS}", "X", name) + linklibs = linklibs .. string.gsub(" ${X_LIBRARY} ${X_LIBRARIES} ${X_LIBS}", "X", name:upper()) end + cmakefile:print(" target_link_libraries(%s PRIVATE %s)", testname, linklibs) cmakefile:print("endif(%s_FOUND)", name) cmakefile:close() |
