关于 meson
Meson 是一项指导 Agent 使用 Meson 的技能,涵盖项目初始化、配置、依赖管理和交叉编译,也介绍测试、构建选项及从 CMake 迁移。
主要功能
- 01Meson 项目初始化
- 02构建与测试命令
- 03基于 wrap 的依赖管理
- 04可配置的构建选项
- 05交叉编译配置
- 06CMake 迁移指南
适用场景
- 01配置新的 Meson 项目
- 02使用 wrap 作为依赖回退方案
- 03设置项目专属构建选项
- 04为交叉目标构建
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill meson安装后在 Claude Code 中输入/meson确认已加载。
附带文件references/meson-wrap.md
详细介绍
Meson
用途
指导 Agent 完成 Meson 项目设置、构建配置、wrap 依赖系统和交叉编译,涵盖 GLib、systemd、GStreamer、Mesa 及许多大型 C/C++ 项目使用的构建系统。
触发语句
- “如何设置 Meson 构建?”
- “如何在 Meson 中添加依赖?”
- “如何使用 Meson 进行交叉编译?”
- “Meson wrap 找不到我的依赖”
- “如何配置 Meson 构建选项?”
- “如何从 CMake/Autotools 迁移到 Meson?”
工作流
1. 项目设置
# Configure (source-dir, build-dir are positional)
meson setup builddir
# With options
meson setup builddir \
--buildtype=release \
--prefix=/usr/local \
-Db_lto=true \
-Db_sanitize=address
# Reconfigure (change options on existing builddir)
meson configure builddir -Dbuildtype=debug
# Show all available options
meson configure builddir
--buildtype | 等效标志 |
|---|---|
debug(默认) | -O0 -g |
debugoptimized | -O2 -g |
release | -O3 -DNDEBUG |
minsize | -Os -DNDEBUG |
plain | 不添加标志 |
2. 构建和测试
# Build (from source directory)
meson compile -C builddir
# Or enter builddir and use ninja directly
cd builddir && ninja
# Run tests
meson test -C builddir
meson test -C builddir --verbose # show test output
meson test -C builddir -t 5 # 5x timeout multiplier
meson test -C builddir --suite unit # run only 'unit' suite
# Install
meson install -C builddir
# Dry run
meson install -C builddir --dry-run
3. meson.build 结构
project('myapp', 'c', 'cpp',
version : '1.0.0',
default_options : [
'c_std=c11',
'cpp_std=c++17',
'warning_level=2',
]
)
# Find system dependencies
glib_dep = dependency('glib-2.0', version : '>=2.68')
threads_dep = dependency('threads')
# Include directories
inc = include_directories('include')
# Library
mylib = static_library('mylib',
sources : ['src/lib.c', 'src/util.c'],
include_directories : inc,
)
# Executable
executable('myapp',
sources : ['src/main.c'],
include_directories : inc,
link_with : mylib,
dependencies : [glib_dep, threads_dep],
install : true,
)
# Tests
test('basic', executable('test_basic',
sources : ['tests/test_basic.c'],
link_with : mylib,
))
4. 使用 wrap 管理依赖
# Search for a wrap
meson wrap search zlib
# Install a wrap (downloads .wrap file from wrapdb)
meson wrap install zlib
meson wrap install gtest
# List installed wraps
meson wrap list
# Update all wraps
meson wrap update
在 meson.build 中:
# Use wrap as fallback if system lib not found
zlib_dep = dependency('zlib',
fallback : ['zlib', 'zlib_dep'], # [wrap_name, dep_variable]
)
# Force wrap (for reproducible builds)
zlib_dep = dependency('zlib',
fallback : ['zlib', 'zlib_dep'],
default_options : ['default_library=static'],
)
subprojects/zlib.wrap 的结构:
[wrap-file]
directory = zlib-1.3
source_url = https://zlib.net/zlib-1.3.tar.gz
source_hash = <sha256>
[provide]
zlib = zlib_dep
5. 构建选项
在 meson_options.txt 中定义(或在 Meson ≥1.1 中使用 meson.options):
option('with_tests', type : 'boolean', value : true,
description : 'Build unit tests')
option('backend', type : 'combo',
choices : ['opengl', 'vulkan', 'software'],
value : 'opengl',
description : 'Rendering backend')
option('max_connections', type : 'integer',
min : 1, max : 1024, value : 64)
在 meson.build 中使用:
if get_option('with_tests')
subdir('tests')
endif
6. 交叉编译
创建交叉编译文件(cross/arm-linux.ini):
[binaries]
c = 'arm-linux-gnueabihf-gcc'
cpp = 'arm-linux-gnueabihf-g++'
ar = 'arm-linux-gnueabihf-ar'
strip = 'arm-linux-gnueabihf-strip'
pkgconfig = 'arm-linux-gnueabihf-pkg-config'
[properties]
sys_root = '/path/to/sysroot'
[host_machine]
system = 'linux'
cpu_family = 'arm'
cpu = 'cortex-a9'
endian = 'little'
meson setup builddir-arm --cross-file cross/arm-linux.ini
meson compile -C builddir-arm
7. CMake 迁移速查表
| CMake | Meson 对应项 |
|---|---|
add_executable | executable() |
add_library | library() / static_library() / shared_library() |
target_include_directories | 目标中的 include_directories 参数 |
target_link_libraries | dependencies / link_with |
find_package | dependency() |
option() | meson_options.txt 中的 option() |
add_subdirectory | subdir() |
install(TARGETS ...) | 目标中的 install : true |
有关 wrap 模式和子项目配置,请参阅 references/meson-wrap.md。
相关技能
- 当必须或优先使用 CMake 时,使用
skills/build-systems/cmake - 使用
skills/build-systems/ninja— Meson 使用 Ninja 作为后端 - 使用
skills/compilers/cross-gcc设置交叉编译器工具链 - 使用
skills/compilers/gcc或skills/compilers/clang了解编译器标志
评论
支持 Markdown