如下是一个简单的使用conan new
加 --template
参数指定模板为cmake_exe
生成的构建exe程序的conan包定义脚本(参见我的上一篇博客《conan new 命令的新特性–模板功能(–template)》).
conanfile.py
from conans import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake
from conan.tools.layout import cmake_layout
class Bin2cConan(ConanFile):
name = "bin2c"
version = "1.0.0"
# Optional metadata
license = "BSD-2-Clause"
author = "gwilymk 10km"
url = "https://gitee.com/l0km/bin2c"
description = "A simple utility for converting a binary file to a c application which can then be included within an application."
topics = ("bin")
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "CMakeLists.txt", "src/*"
def layout(self):
cmake_layout(self)
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
def configure(self):
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
原本一切都是正常的,
当我在build函数中调用CMake.definitions
函数定义一个cmake变量USE_BZ2
时,执行conan create .
命令报错了
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.definitions["USE_BZ2"] = False
cmake.build()
错误输出如下:
$ conan create .
Exporting package recipe
bin2c/1.0.0 exports_sources: Copied 1 '.txt' file: CMakeLists.txt
bin2c/1.0.0 exports_sources: Copied 1 '.c' file: bin2c.c
bin2c/1.0.0: The stored package has not changed
bin2c/1.0.0: Exported revision: b6fb6e6ab5a27e021f31f7048ca1ea66
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=Visual Studio
compiler.runtime=MD
compiler.version=14
os=Windows
os_build=Windows
[options]
[build_requires]
[env]
bin2c/1.0.0: Forced build from source
bin2c/1.0.0 (test package): Installing package
Requirements
bin2c/1.0.0 from local cache - Cache
Packages
bin2c/1.0.0:63da998e3642b50bee33f4449826b2d623661505 - Build
Installing (downloading, building) binaries...
bin2c/1.0.0: Copying sources to build folder
bin2c/1.0.0: Building your package in C:\Users\guyadong\.conan\data\bin2c\1.0.0\_\_\build\63da998e3642b50bee33f4449826b2d623661505
bin2c/1.0.0: Generator txt created conanbuildinfo.txt
bin2c/1.0.0: Calling generate()
bin2c/1.0.0: Aggregating env generators
bin2c/1.0.0: Calling build()
bin2c/1.0.0:
bin2c/1.0.0: WARN: Build folder is dirty, removing it: C:\Users\guyadong\.conan\data\bin2c\1.0.0\_\_\build\63da998e3642b50bee33f4449826b2d623661505
bin2c/1.0.0: WARN: Using the new toolchains and generators without specifying a build profile (e.g: -pr:b=default) is discouraged and might cause failures and unexpected behavior
bin2c/1.0.0: ERROR: Package '63da998e3642b50bee33f4449826b2d623661505' build failed
bin2c/1.0.0: WARN: Build folder C:\Users\guyadong\.conan\data\bin2c\1.0.0\_\_\build\63da998e3642b50bee33f4449826b2d623661505\build
ERROR: bin2c/1.0.0: Error in build() method, line 32
cmake.definitions["USE_BZ2"] = False
AttributeError: 'CMake' object has no attribute 'definitions'
AttributeError: 'CMake' object has no attribute 'definitions'
意思就是说CMake这个对象中没有definitions
这个成员。
这就太意外了。
按照Conan官方文档《default_options》,《How to reuse cmake install for package() method》以及其他已经发布到conan-center上的第三方库的脚本(如cjson conanfile.py)中都是这么用的啊?为啥?
因为此类非彼类.
conans.CMake
与conan.tools.cmake.CMake
不是同一个类啊。
conans.CMake
是较早设计并已经被广泛使用的一个类,有definitions
成员。
按Conan官方说明conan.tools.cmake
是比较新的还在实验阶段的一个功能,conan.tools.cmake.CMake
中并没definitions
成员。
如果不指定--template参数使用 create new pkgname/ version生成的conanfile.py是引用的是conans.CMake
如果指定--template参数使用 create new pkgname/ version生成的conanfile.py是引用的是conan.tools.cmake.CMake
改回传统的引用conans.CMake
,这需要较多修改conanfile.py脚本
conan.tools.cmake
包下的引用改为conans.CMake
layout,generate
函数下图显示修改对比
使用conan.tools.cmake.CMakeToolchain类的variables成员代替conans.CMake
类的definitions
成员
只需要在generate函数中增加一行代码
def generate(self):
tc = CMakeToolchain(self)
tc.variables["USE_BZ2"] = False
tc.generate()
下图显示修改对比
两种方式各有好处,你可以根据自己的需要选择
conan.tools.cmake
这个实验包应该会在conan 2.0变为正式的,如果你和我一样也是刚conan入门,建议使用解决方案一,以避免今后使用中遇到其他与官方参考不一致的问题。