From 5045cb0b591c9bcd50cd09d040d56dbd8fbeee70 Mon Sep 17 00:00:00 2001 From: MeanSquaredError <35379301+MeanSquaredError@users.noreply.github.com> Date: Thu, 20 Nov 2025 17:16:42 +0200 Subject: cmake find_package: Add configuration option allow_empty_package that allows system packages that don't need any include directories or linked libraries. --- xmake/modules/package/manager/cmake/configurations.lua | 1 + xmake/modules/package/manager/cmake/find_package.lua | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/xmake/modules/package/manager/cmake/configurations.lua b/xmake/modules/package/manager/cmake/configurations.lua index e0a9f7d9a..9945ca191 100644 --- a/xmake/modules/package/manager/cmake/configurations.lua +++ b/xmake/modules/package/manager/cmake/configurations.lua @@ -29,6 +29,7 @@ function main() 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\"}"}, + allow_empty_package = {description = "Accept package even if it doesn't have any include directories or linked libraries"}, } end diff --git a/xmake/modules/package/manager/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua index f1b4cd321..376b583dd 100644 --- a/xmake/modules/package/manager/cmake/find_package.lua +++ b/xmake/modules/package/manager/cmake/find_package.lua @@ -301,7 +301,7 @@ function _find_package(cmake, name, opt) os.tryrm(workdir) -- get results - if links or includedirs then + if configs.allow_empty_package or links or includedirs then local results = {} results.links = table.reverse_unique(links) results.ldflags = table.reverse_unique(ldflags) -- cgit v1.3.1 From 4c8fd669a52cfa6676052953457ce65ab0b8b89b Mon Sep 17 00:00:00 2001 From: MeanSquaredError <35379301+MeanSquaredError@users.noreply.github.com> Date: Fri, 21 Nov 2025 12:53:23 +0200 Subject: cmake find_package: Fix comment typo --- xmake/modules/package/manager/cmake/find_package.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/package/manager/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua index 376b583dd..fdf9aa608 100644 --- a/xmake/modules/package/manager/cmake/find_package.lua +++ b/xmake/modules/package/manager/cmake/find_package.lua @@ -140,7 +140,7 @@ function _find_package(cmake, name, opt) 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 + -- parse defines and includedirs for macosx/linux local links local linkdirs local libfiles -- cgit v1.3.1 From 8b165b81f1ab59188bfbbb14ca94a4ebbf42f998 Mon Sep 17 00:00:00 2001 From: MeanSquaredError <35379301+MeanSquaredError@users.noreply.github.com> Date: Sat, 22 Nov 2025 13:10:41 +0200 Subject: cmake find_package: If finding the package fails, return an error, regardless of the allow_empty_package option. --- xmake/modules/package/manager/cmake/find_package.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/xmake/modules/package/manager/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua index fdf9aa608..2683d5e1a 100644 --- a/xmake/modules/package/manager/cmake/find_package.lua +++ b/xmake/modules/package/manager/cmake/find_package.lua @@ -138,7 +138,15 @@ function _find_package(cmake, name, opt) -- run cmake 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} + -- If the generated CMakeLists.txt fails to find the REQUIRED package, CMake will exit + -- with code 1, os.vrunv will raise an error and the try{} block will return nil. + local ok = try {function() + os.vrunv(cmake.program, {workdir}, {curdir = workdir, envs = envs}) + return true + end} + if not ok then + return + end -- parse defines and includedirs for macosx/linux local links -- cgit v1.3.1 From 21ee4d4d8b2c745fe8a3c436f9b839fdd5dd7887 Mon Sep 17 00:00:00 2001 From: MeanSquaredError <35379301+MeanSquaredError@users.noreply.github.com> Date: Sat, 22 Nov 2025 13:18:34 +0200 Subject: cmake find_package: Simplify the generated CMakeLists.txt used to checking if a package is installed locally. --- xmake/modules/package/manager/cmake/find_package.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/xmake/modules/package/manager/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua index 2683d5e1a..7192843a9 100644 --- a/xmake/modules/package/manager/cmake/find_package.lua +++ b/xmake/modules/package/manager/cmake/find_package.lua @@ -104,8 +104,7 @@ function _find_package(cmake, name, opt) end local testname = "test_" .. 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("add_executable(%s test.cpp)", testname) -- setup include directories local includedirs = "" if configs.include_directories then @@ -114,9 +113,9 @@ function _find_package(cmake, name, opt) includedirs = ("${%s_INCLUDE_DIR} ${%s_INCLUDE_DIRS}"):format(name, name) includedirs = includedirs .. (" ${%s_INCLUDE_DIR} ${%s_INCLUDE_DIRS}"):format(name:upper(), name:upper()) end - cmakefile:print(" target_include_directories(%s PRIVATE %s)", testname, includedirs) + 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})", + cmakefile:print("target_include_directories(%s PRIVATE ${%s_CXX_INCLUDE_DIRS})", testname, name) -- setup link library/target local linklibs = "" @@ -126,8 +125,7 @@ function _find_package(cmake, name, opt) linklibs = ("${%s_LIBRARY} ${%s_LIBRARIES} ${%s_LIBS}"):format(name, name, name) linklibs = linklibs .. (" ${%s_LIBRARY} ${%s_LIBRARIES} ${%s_LIBS}"):format(name:upper(), name:upper(), name:upper()) end - cmakefile:print(" target_link_libraries(%s PRIVATE %s)", testname, linklibs) - cmakefile:print("endif(%s_FOUND)", name) + cmakefile:print("target_link_libraries(%s PRIVATE %s)", testname, linklibs) cmakefile:close() if option.get("diagnosis") then local cmakedata = io.readfile(filepath) -- cgit v1.3.1