使用 conan 和 cmake 选择文件

File selection with conan and cmake

我有一个包含 2 个变体的包,其目录结构如下

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

使用 conan,我试图定义一个选项 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 逻辑。