构建需求的一个例子是作为库实现的测试框架,另一个好的例子是在编译过程中使用的构建工具。让我们将它们称为mytest_framework和cmake_turbo,并想象我们已经为它们两个提供了一个包。
可以在配方中检查构建要求的存在 (是否已应用),这对于配方中的条件逻辑很有用。在此示例中,我们可以使用以下build() 方法获得一个配方:
def build_requirements(self):if self.options.enable_testing:self.build_requires("mytest_framework/0.1@user/channel", force_host_context=True)def build(self):# Use our own 'cmake_turbo' if it is availableuse_cmake_turbo = "cmake_turbo" in self.deps_env_info.depscmake_executable = "cmake_turbo" if use_cmake_turbo else Nonecmake = CMake(self, cmake_program=cmake_executable)cmake.configure(defs={"ENABLE_TESTING": self.options.enable_testing})cmake.build()if enable_testing:cmake.test()
和包CMakeLists.txt:
project(PackageTest CXX)cmake_minimum_required(VERSION 2.8.12)include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)conan_basic_setup()if(ENABLE_TESTING)add_executable(example test.cpp)target_link_libraries(example ${CONAN_LIBS})enable_testing()add_test(NAME exampleWORKING_DIRECTORY ${CMAKE_BINARY_DIR}/binCOMMAND example)endif()
此包配方无法检索cmake_turbo包以进行正常安装:
$ conan install .
但如果定义了以下配置文件:
use_cmake_turbo_profile ¶
[build_requires]cmake_turbo/0.1@user/channel
然后,install命令将检索cmake_turbo并使用它:
$ conan install . --profile=use_cmake_turbo_profile
尽管前一行可以使用,但最好使用Conan v1.24 中的功能并向命令行提供两个配置文件,这样,如果构建环境中的构建需求共享共同的需求,则它们不会与主机图相互干扰 (请参阅关于开发工具的部分)。如果交叉编译,也可能需要它 (请参阅关于交叉编译的部分)。
$ conan install . --profile:host=use_cmake_turbo_profile --profile:build=build_machine
