Conanfile.py 设置 cmake 标志?
Conanfile.py that sets a cmake flag?
我有一个第三方库,我现在正尝试添加柯南食谱,这样我就可以开始通过柯南管理这个库了...
这个第三方库有一个 CMakeLists.txt
,我需要手动添加一个编译器选项...有没有办法在食谱中做到这一点?
CMakeLists.txt
cmake_minimum_required( VERSION 3.0 )
project( ProjectName
LANGUAGES CXX )
add_compile_options(-std=c++11). //I have to add this line
conanfile.py
...
def source(self):
self.run("git clone --depth 1 --branch a_tag git@github.com:GITGROUP/project_name.git")
def build(self):
cmake = CMake(self)
cmake.configure(source_folder="project_name")
cmake.build()
...
如果您想为第三方库创建配方,并且想修改该库中的任何文件,包括 CMakeLists.txt
,您可以在源方法中使用 tools.replace_in_file
来完成。模块 tools
在 conans
包中。
我经常使用
tools.replace_in_file("sources/CMakeLists.txt", "project (ProjectName)",
'''project (ProjectName)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()''')
这样我就可以使用 conan 来获取第三方库依赖项。您可以在替换字符串中添加 add_compile_options(-std=c++11)
。
例如,我在 vtk 库的配方中的完整 source
方法是
def source(self):
tools.get("https://www.vtk.org/files/release/{}/VTK-{}.tar.gz".format(
".".join(self.version.split(".")[:-1]), # Get only X.Y version, instead of X.Y.Z
self.version))
os.rename("VTK-{}".format(self.version), "sources")
tools.replace_in_file("sources/CMakeLists.txt",
"project(VTK)",
"""project(VTK)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
SET(CMAKE_INSTALL_RPATH "$ORIGIN")""")
对于更高版本的 Conan,您不再需要手动注入 conan cmake。您可以简单地要求柯南使用 cmake_paths
生成器,然后使用柯南“force-include”。
from conans import ConanFile, CMake
class MyConanFile(ConanFile):
name = ... # must match the name in the project() in CMake
generators = 'cmake', 'cmake_paths'
def _configure_cmake(self) -> CMake:
cmake = CMake(self)
cmake.definitions[f"CMAKE_PROJECT_{self.name}_INCLUDE"] = f"{self.build_folder}/conan_paths.cmake"
# Add any definition you like
cmake.definitions['FOO'] = '1'
return cmake
def build(self):
cmake = self._configure_cmake()
cmake.build()
我有一个第三方库,我现在正尝试添加柯南食谱,这样我就可以开始通过柯南管理这个库了...
这个第三方库有一个 CMakeLists.txt
,我需要手动添加一个编译器选项...有没有办法在食谱中做到这一点?
CMakeLists.txt
cmake_minimum_required( VERSION 3.0 )
project( ProjectName
LANGUAGES CXX )
add_compile_options(-std=c++11). //I have to add this line
conanfile.py
...
def source(self):
self.run("git clone --depth 1 --branch a_tag git@github.com:GITGROUP/project_name.git")
def build(self):
cmake = CMake(self)
cmake.configure(source_folder="project_name")
cmake.build()
...
如果您想为第三方库创建配方,并且想修改该库中的任何文件,包括 CMakeLists.txt
,您可以在源方法中使用 tools.replace_in_file
来完成。模块 tools
在 conans
包中。
我经常使用
tools.replace_in_file("sources/CMakeLists.txt", "project (ProjectName)",
'''project (ProjectName)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()''')
这样我就可以使用 conan 来获取第三方库依赖项。您可以在替换字符串中添加 add_compile_options(-std=c++11)
。
例如,我在 vtk 库的配方中的完整 source
方法是
def source(self):
tools.get("https://www.vtk.org/files/release/{}/VTK-{}.tar.gz".format(
".".join(self.version.split(".")[:-1]), # Get only X.Y version, instead of X.Y.Z
self.version))
os.rename("VTK-{}".format(self.version), "sources")
tools.replace_in_file("sources/CMakeLists.txt",
"project(VTK)",
"""project(VTK)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
SET(CMAKE_INSTALL_RPATH "$ORIGIN")""")
对于更高版本的 Conan,您不再需要手动注入 conan cmake。您可以简单地要求柯南使用 cmake_paths
生成器,然后使用柯南“force-include”。
from conans import ConanFile, CMake
class MyConanFile(ConanFile):
name = ... # must match the name in the project() in CMake
generators = 'cmake', 'cmake_paths'
def _configure_cmake(self) -> CMake:
cmake = CMake(self)
cmake.definitions[f"CMAKE_PROJECT_{self.name}_INCLUDE"] = f"{self.build_folder}/conan_paths.cmake"
# Add any definition you like
cmake.definitions['FOO'] = '1'
return cmake
def build(self):
cmake = self._configure_cmake()
cmake.build()