前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >conan入门(十五):AttributeError: ‘CMake‘ object has no attribute ‘definitions‘

conan入门(十五):AttributeError: ‘CMake‘ object has no attribute ‘definitions‘

作者头像
10km
发布2022-04-13 12:17:01
2.3K0
发布2022-04-13 12:17:01
举报
文章被收录于专栏:10km的专栏

conan: AttributeError: ‘CMake’ object has no attribute ‘definitions’

如下是一个简单的使用conan new--template参数指定模板为cmake_exe生成的构建exe程序的conan包定义脚本(参见我的上一篇博客《conan new 命令的新特性–模板功能(–template)》).

conanfile.py

代码语言:javascript
复制
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 .命令报错了

代码语言:javascript
复制
    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.definitions["USE_BZ2"] = False
        cmake.build()

错误输出如下:

代码语言:javascript
复制
$ 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.CMakeconan.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函数中增加一行代码

代码语言:javascript
复制
    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["USE_BZ2"] = False
        tc.generate()

参见《variables》

下图显示修改对比

两种方式各有好处,你可以根据自己的需要选择

conan.tools.cmake这个实验包应该会在conan 2.0变为正式的,如果你和我一样也是刚conan入门,建议使用解决方案一,以避免今后使用中遇到其他与官方参考不一致的问题。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022/04/06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • conan: AttributeError: ‘CMake’ object has no attribute ‘definitions’
    • 解决方案一
      • 解决方案二
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档