meson
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMeson
Meson
Purpose
用途
Guide agents through Meson project setup, build configuration, the wrap dependency system, and cross-compilation — covering the build system used by GLib, systemd, GStreamer, Mesa, and many major C/C++ projects.
指导开发者完成Meson项目搭建、构建配置、wrap依赖系统使用以及交叉编译——涵盖GLib、systemd、GStreamer、Mesa等众多主流C/C++项目所使用的构建系统。
Triggers
触发场景
- "How do I set up a Meson build?"
- "How do I add a dependency in Meson?"
- "How do I cross-compile with Meson?"
- "Meson wrap isn't finding my dependency"
- "How do I configure build options in Meson?"
- "How do I migrate from CMake/Autotools to Meson?"
- "如何搭建Meson构建?"
- "如何在Meson中添加依赖?"
- "如何使用Meson进行交叉编译?"
- "Meson wrap找不到我的依赖"
- "如何在Meson中配置构建选项?"
- "如何从CMake/Autotools迁移到Meson?"
Workflow
工作流程
1. Project setup
1. 项目搭建
bash
undefinedbash
undefinedConfigure (source-dir, build-dir are positional)
Configure (source-dir, build-dir are positional)
meson setup builddir
meson setup builddir
With options
With options
meson setup builddir
--buildtype=release
--prefix=/usr/local
-Db_lto=true
-Db_sanitize=address
--buildtype=release
--prefix=/usr/local
-Db_lto=true
-Db_sanitize=address
meson setup builddir
--buildtype=release
--prefix=/usr/local
-Db_lto=true
-Db_sanitize=address
--buildtype=release
--prefix=/usr/local
-Db_lto=true
-Db_sanitize=address
Reconfigure (change options on existing builddir)
Reconfigure (change options on existing builddir)
meson configure builddir -Dbuildtype=debug
meson configure builddir -Dbuildtype=debug
Show all available options
Show all available options
meson configure builddir
| `--buildtype` | Equivalent flags |
|---------------|-----------------|
| `debug` (default) | `-O0 -g` |
| `debugoptimized` | `-O2 -g` |
| `release` | `-O3 -DNDEBUG` |
| `minsize` | `-Os -DNDEBUG` |
| `plain` | No flags added |meson configure builddir
| `--buildtype` | 等效标志 |
|---------------|-----------------|
| `debug` (默认) | `-O0 -g` |
| `debugoptimized` | `-O2 -g` |
| `release` | `-O3 -DNDEBUG` |
| `minsize` | `-Os -DNDEBUG` |
| `plain` | 无额外标志 |2. Build and test
2. 构建与测试
bash
undefinedbash
undefinedBuild (from source directory)
Build (from source directory)
meson compile -C builddir
meson compile -C builddir
Or enter builddir and use ninja directly
Or enter builddir and use ninja directly
cd builddir && ninja
cd builddir && ninja
Run tests
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
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
Install
meson install -C builddir
meson install -C builddir
Dry run
Dry run
meson install -C builddir --dry-run
undefinedmeson install -C builddir --dry-run
undefined3. meson.build structure
3. meson.build文件结构
python
project('myapp', 'c', 'cpp',
version : '1.0.0',
default_options : [
'c_std=c11',
'cpp_std=c++17',
'warning_level=2',
]
)python
project('myapp', 'c', 'cpp',
version : '1.0.0',
default_options : [
'c_std=c11',
'cpp_std=c++17',
'warning_level=2',
]
)Find system dependencies
Find system dependencies
glib_dep = dependency('glib-2.0', version : '>=2.68')
threads_dep = dependency('threads')
glib_dep = dependency('glib-2.0', version : '>=2.68')
threads_dep = dependency('threads')
Include directories
Include directories
inc = include_directories('include')
inc = include_directories('include')
Library
Library
mylib = static_library('mylib',
sources : ['src/lib.c', 'src/util.c'],
include_directories : inc,
)
mylib = static_library('mylib',
sources : ['src/lib.c', 'src/util.c'],
include_directories : inc,
)
Executable
Executable
executable('myapp',
sources : ['src/main.c'],
include_directories : inc,
link_with : mylib,
dependencies : [glib_dep, threads_dep],
install : true,
)
executable('myapp',
sources : ['src/main.c'],
include_directories : inc,
link_with : mylib,
dependencies : [glib_dep, threads_dep],
install : true,
)
Tests
Tests
test('basic', executable('test_basic',
sources : ['tests/test_basic.c'],
link_with : mylib,
))
undefinedtest('basic', executable('test_basic',
sources : ['tests/test_basic.c'],
link_with : mylib,
))
undefined4. Dependency management with wrap
4. 使用wrap管理依赖
bash
undefinedbash
undefinedSearch for a wrap
Search for a wrap
meson wrap search zlib
meson wrap search zlib
Install a wrap (downloads .wrap file from wrapdb)
Install a wrap (downloads .wrap file from wrapdb)
meson wrap install zlib
meson wrap install gtest
meson wrap install zlib
meson wrap install gtest
List installed wraps
List installed wraps
meson wrap list
meson wrap list
Update all wraps
Update all wraps
meson wrap update
In `meson.build`:
```pythonmeson wrap update
在`meson.build`中:
```pythonUse wrap as fallback if system lib not found
Use wrap as fallback if system lib not found
zlib_dep = dependency('zlib',
fallback : ['zlib', 'zlib_dep'], # [wrap_name, dep_variable]
)
zlib_dep = dependency('zlib',
fallback : ['zlib', 'zlib_dep'], # [wrap_name, dep_variable]
)
Force wrap (for reproducible builds)
Force wrap (for reproducible builds)
zlib_dep = dependency('zlib',
fallback : ['zlib', 'zlib_dep'],
default_options : ['default_library=static'],
)
`subprojects/zlib.wrap` structure:
```ini
[wrap-file]
directory = zlib-1.3
source_url = https://zlib.net/zlib-1.3.tar.gz
source_hash = <sha256>
[provide]
zlib = zlib_depzlib_dep = dependency('zlib',
fallback : ['zlib', 'zlib_dep'],
default_options : ['default_library=static'],
)
`subprojects/zlib.wrap`文件结构:
```ini
[wrap-file]
directory = zlib-1.3
source_url = https://zlib.net/zlib-1.3.tar.gz
source_hash = <sha256>
[provide]
zlib = zlib_dep5. Build options
5. 构建选项
Define in (or in Meson ≥1.1):
meson_options.txtmeson.optionspython
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)Use in :
meson.buildpython
if get_option('with_tests')
subdir('tests')
endif在(或Meson ≥1.1版本中的)中定义:
meson_options.txtmeson.optionspython
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.buildpython
if get_option('with_tests')
subdir('tests')
endif6. Cross-compilation
6. 交叉编译
Create a cross file ():
cross/arm-linux.iniini
[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'bash
meson setup builddir-arm --cross-file cross/arm-linux.ini
meson compile -C builddir-arm创建交叉编译配置文件():
cross/arm-linux.iniini
[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'bash
meson setup builddir-arm --cross-file cross/arm-linux.ini
meson compile -C builddir-arm7. CMake migration cheatsheet
7. CMake迁移速查表
| CMake | Meson equivalent |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
For wrap patterns and subproject configuration, see references/meson-wrap.md.
| CMake | Meson等效写法 |
|---|---|
| |
| |
| 目标中的 |
| |
| |
| |
| |
| 目标中的 |
关于wrap模式和子项目配置,请参考references/meson-wrap.md。
Related skills
相关技能
- Use when CMake is required or preferred
skills/build-systems/cmake - Use — Meson uses Ninja as its backend
skills/build-systems/ninja - Use for cross-compiler toolchain setup
skills/compilers/cross-gcc - Use or
skills/compilers/gccfor compiler flag contextskills/compilers/clang
- 当需要或偏好使用CMake时,使用
skills/build-systems/cmake - 使用——Meson将Ninja作为其后端
skills/build-systems/ninja - 使用进行交叉编译工具链配置
skills/compilers/cross-gcc - 使用或
skills/compilers/gcc获取编译器标志相关内容skills/compilers/clang