使用柯南和cmake选择文件

File selection with conan and cmake

本文关键字:选择 文件 cmake 柯南      更新时间:2023-10-16

我有一个包含 2 个变体的软件包,具有以下目录结构

pkg
pkg_main.h
CMakeLists.txt
var1
pkg_main.cpp
var2
pkg_main.cpp
conanfile.py

对于柯南,我正在尝试定义一个选项fileSelection,其中包含可能的值var1var2。 使用 cmake,我尝试按如下方式进行选择:如果fileSelection设置为var1,则应调用var1/pkg_main.cpp,否则var2/pkg_main.cpp

到目前为止,我已经在conanfile.py
中定义了选项fileSelection

class PkgConan(ConanFile):
name = "pkg"
...
options = {"fileSelection : ['var1', 'var2']"}
default_options = "fileSelection=var1"
generators = "cmake"
def build(self): 
cmake = CMake(self)
cmake.configure(source_folder="pkg")
cmake.build()
def package(self):
self.copy("*.h", dst="include", src="pkg")
self.copy("*pkg.lib", dst="lib", keep_path=False)
self.copy("*.dll", dst="bin", keep_path=False)
self.copy("*.so", dst="lib", keep_path=False)
self.copy("*.dylib", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["pkg"]

现在我正在努力更新CMakeLists.txt文件以根据fileSelection的值进行选择。像这样的东西:
[这是逻辑,不是可运行的代码]

if("${fileSelection}" STREQUAL "var1") 
add_library(pkg var1/pkg_main.cpp)
else
add_library(pkg var2/pkg_main.cpp)
endif

??如何将fileSelection选项传递给 cmake;我在哪里以及如何实现var1var2之间的切换(我是否通过尝试在 CMakeLists.txt 中定义开关而朝着正确的方向前进(?

您可以将变量传递给由 cmake 帮助程序驱动的 cmake 命令行调用。像这样:

options = {"fileSelection": ["var1", "var2"]}
...
def build(self): 
cmake = CMake(self)
cmake.definitions["fileSelection"] = self.options.fileSelection
cmake.configure(source_folder="pkg")
cmake.build()

这假设您具有您描述的 CMakeLists.txt 逻辑。