用柯南和cmake选择文件

时间:2018-06-20 11:01:04

标签: c++ cmake conan

我有一个带有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

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

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中定义切换,我朝着正确的方向前进)吗?

1 个答案:

答案 0 :(得分:2)

您可以将变量传递给由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逻辑。