如何使用配置模式和模块模式的后备方式进行find_package搜索? [英] How can I make find_package search with config mode and fallback on module mode?

查看:91
本文介绍了如何使用配置模式和模块模式的后备方式进行find_package搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当库使用CMake定义构建并遇到为其自身构建安装软件包的麻烦时,将出现 XXXConfig.cmake

When a library defines a build with CMake and goes through the trouble of building an install package for itself, there will be a XXXConfig.cmake.

如果库无法将其目标导出到CMake,则CMake会尝试通过提供 FindXXX.cmake 来弥合差距试图找到此类库的脚本。

If a library doesn't have a way to export it's targets to CMake, CMake tries to bridge the gap by providing FindXXX.cmake scripts that attempt to locate such libraries.

在文档中尝试使用 FindXXX.cmake (模块模式)首先,并且只有在失败的情况下,它才会尝试使用 XXXConfig.cmake (配置模式)。

In the docs, FindXXX.cmake (module mode), is attempted first, and only if that fails does it attempt to use XXXConfig.cmake (config mode). But this seems like a really backwards to me.

问题是,例如,我从源代码构建了CURL,而ConfigXXX产生的目标名称不同于FindXXX,因此,当尝试使用它时,它会失败,因为FindXXX负责find_package请求并加载了与我期望的目标名称不同的目标名称。

The problem is, for example, I have built CURL from source, and the ConfigXXX produces a different target name than FindXXX, so, when trying to use it, it fails because FindXXX took responsibility for the find_package request and loaded a different target name than what I was expecting.

我至少可以告诉一下CMake以其他方式做事吗?首先是配置模式。

Can I at least tell CMake somehow to do things the other way around? Config mode first.

我知道我可以完全禁用模块模式,但是我宁愿将其作为备用选项。

I know I can disable module mode entirely, but I'd rather have it as a fallback option.

推荐答案

只需在 CONFIG 模式下使用 find_package ,检查其结果,如果结果为是错误的,请以 MODULE 模式重复通话:

Just use find_package with CONFIG mode, check its result, and, if result is false, repeat the call with MODULE mode:

# First time do not use common *REQUIRED* but use QUIET for do not output error messages on fail.
find_package(XXX CONFIG QUIET)
if(NOT XXX_FOUND)
    # Previous call has been failed. Fallback with MODULE mode.
    find_package(XXX MODULE REQUIRED) # Now it is OK to use REQUIRED if needed.
    # ... There could be additional actions for wrap result "as if" CONFIG mode.
endif()
# ... use XXX

这篇关于如何使用配置模式和模块模式的后备方式进行find_package搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆