使用 Meson 创建你的第一个 Conan 包¶
在 创建你的第一个 Conan 包教程 中,CMake 被用作构建系统。 如果你还没有阅读该部分,请先阅读它以熟悉 conanfile.py
和 test_package
的概念,然后返回阅读关于 Meson
包创建的具体信息。
使用 conan new 命令创建一个 “Hello World” C++ 库示例项目
$ conan new meson_lib -d name=hello -d version=1.0
这将创建一个具有以下结构的 Conan 包项目。
├── conanfile.py
├── meson.build
├── hello.vcxproj
├── src
│ ├── hello.h
│ └── hello.cpp
└── test_package
├── conanfile.py
├── meson.build
└── src
└── example.cpp
该结构和文件与之前的 CMake 示例非常相似
conanfile.py:在根文件夹中,有一个 *conanfile.py*,它是主要的配方文件,负责定义如何构建和使用包。
meson.build:一个 Meson 构建脚本。 该脚本不需要包含任何 Conan 特有的内容,它完全与 Conan 无关,因为集成是透明的。
src 文件夹:包含简单的 C++ “hello” 库的文件夹。
test_package 文件夹:包含一个 *example* 应用程序,该应用程序将需要并链接到创建的包。 在本例中,
test_package
也包含一个meson.build
,但如果需要,test_package
也可以使用其他构建系统,例如 CMake。 test_package 使用与包相同的构建系统不是强制性的。
让我们看看包配方 *conanfile.py*(仅相关的新的部分)
exports_sources = "meson.build", "src/*"
def layout(self):
basic_layout(self)
def generate(self):
tc = MesonToolchain(self)
tc.generate()
def build(self):
meson = Meson(self)
meson.configure()
meson.build()
def package(self):
meson = Meson(self)
meson.install()
让我们简要解释一下配方的不同部分
layout()
定义了一个basic_layout()
,它不如 CMake 的那么灵活,因此它不允许任何参数化。generate()
方法调用MesonToolchain
,它可以为交叉构建生成conan_meson_native.ini
和conan_meson_cross.ini
Meson 工具链文件。 如果项目有带有 Conanrequires
的依赖项,它也应该添加PkgConfigDeps
build()
方法使用Meson()
助手来驱动构建package()
方法使用Meson
安装功能来定义最终工件并将其复制到包文件夹。
test_package 文件夹还包含一个 meson.build
文件,该文件声明了对被测试包的依赖关系,并链接了一个应用程序,以验证包是否已正确创建并包含该库
project('Testhello', 'cpp')
hello = dependency('hello', version : '>=0.1')
executable('example', 'src/example.cpp', dependencies: hello)
请注意,test_package/conanfile.py
还包含 generators = "PkgConfigDeps", "MesonToolchain"
,因为 test_package
具有 “hello” 包作为依赖项,并且需要 PkgConfigDeps
才能找到它。
注意
此示例假设 Meson、Ninja 和 PkgConfig 已安装在系统中,但情况并非总是如此。 如果它们没有安装,你可以创建一个带有以下内容的 myprofile
配置文件
include(default)
[tool_requires]
meson/[*]
pkgconf/[*]
我们添加了 Meson 和 pkg-config 作为 工具要求到配置文件。 通过执行 conan create . -pr=myprofile
,这些工具将被安装并在包的构建过程中可用。
让我们使用当前默认配置从源代码构建包,然后让 test_package
文件夹测试该包
$ conan create .
...
======== Testing the package: Executing test ========
hello/1.0 (test package): Running test()
hello/1.0 (test package): RUN: .\example
hello/1.0: Hello World Release!
hello/1.0: _M_X64 defined
hello/1.0: MSVC runtime: MultiThreadedDLL
hello/1.0: _MSC_VER1939
hello/1.0: _MSVC_LANG201402
hello/1.0: __cplusplus201402
hello/1.0 test_package
我们现在可以验证配方和包二进制文件是否在缓存中
$ conan list "hello/1.0:*"
Local Cache:
hello
hello/1.0
revisions
856c535669f78da11502a119b7d8a6c9 (2024-03-04 17:52:39 UTC)
packages
c13a22a41ecd72caf9e556f68b406569547e0861
info
settings
arch: x86_64
build_type: Release
compiler: msvc
compiler.cppstd: 14
compiler.runtime: dynamic
compiler.runtime_type: Release
compiler.version: 193
os: Windows
另请参阅