Meson

mohitmishra786/low-level-dev-skills/skills/build-systems/meson

作者 mohitmishra786bdc58472fa9f无许可证253 个星标收录于 2026年10月9日更新于 2026年10月9日仓库3个月前更新

Meson build system skill for C/C++ projects. Use when setting up a Meson project, configuring build options, managing dependencies with the wrap system, cross-compiling, or integrating Meson with CI. Activates on queries about meson setup, meson compile, meson wrap, meson test, cross-file, meson.build, or migrating from CMake/Autotools to Meson.

AI 生成的概览

指导代理使用 Meson 构建系统完成 C/C++ 项目的配置、选项、wrap 依赖管理与交叉编译。

功能
说明如何用 Meson 配置、构建、测试和安装 C/C++ 项目,包括构建类型、构建选项和 meson.build 结构。涵盖通过 wrap 系统管理依赖、使用交叉文件进行交叉编译,以及从 CMake 迁移到 Meson 的对照表。产出的是命令序列、配置片段和参考指引,而非文件。
适用场景
适用于搭建 Meson 项目、配置构建选项、添加或排查 wrap 依赖、进行交叉编译,或将项目从 CMake、Autotools 迁移到 Meson 的场景。
运行要求
不附带脚本,仅为说明文档。需要安装 Meson 构建系统和 Ninja,以及 C/C++ 工具链;交叉编译还需要交叉工具链和 sysroot。使用 wrap 安装并从 wrapdb 下载源码时需要网络访问。

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.

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?"

Workflow

1. Project setup

bash
# Configure (source-dir, build-dir are positional)meson setup builddir
# With optionsmeson 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 optionsmeson configure builddir
--buildtypeEquivalent flags
debug (default)-O0 -g
debugoptimized-O2 -g
release-O3 -DNDEBUG
minsize-Os -DNDEBUG
plainNo flags added

2. Build and test

bash
# Build (from source directory)meson compile -C builddir
# Or enter builddir and use ninja directlycd builddir && ninja
# Run testsmeson test -C builddirmeson test -C builddir --verbose          # show test outputmeson test -C builddir -t 5               # 5x timeout multipliermeson test -C builddir --suite unit       # run only 'unit' suite
# Installmeson install -C builddir# Dry runmeson install -C builddir --dry-run

3. meson.build structure

python
project('myapp', 'c', 'cpp',  version : '1.0.0',  default_options : [    'c_std=c11',    'cpp_std=c++17',    'warning_level=2',  ])
# Find system dependenciesglib_dep = dependency('glib-2.0', version : '>=2.68')threads_dep = dependency('threads')
# Include directoriesinc = include_directories('include')
# Librarymylib = static_library('mylib',  sources : ['src/lib.c', 'src/util.c'],  include_directories : inc,)
# Executableexecutable('myapp',  sources : ['src/main.c'],  include_directories : inc,  link_with : mylib,  dependencies : [glib_dep, threads_dep],  install : true,)
# Teststest('basic', executable('test_basic',  sources : ['tests/test_basic.c'],  link_with : mylib,))

4. Dependency management with wrap

bash
# Search for a wrapmeson wrap search zlib
# Install a wrap (downloads .wrap file from wrapdb)meson wrap install zlibmeson wrap install gtest
# List installed wrapsmeson wrap list
# Update all wrapsmeson wrap update

In meson.build:

python
# Use wrap as fallback if system lib not foundzlib_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 structure:

ini
[wrap-file]directory = zlib-1.3source_url = https://zlib.net/zlib-1.3.tar.gzsource_hash = <sha256>
[provide]zlib = zlib_dep

5. Build options

Define in meson_options.txt (or meson.options in Meson ≥1.1):

python
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.build:

python
if get_option('with_tests')  subdir('tests')endif

6. Cross-compilation

Create a cross file (cross/arm-linux.ini):

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'
bash
meson setup builddir-arm --cross-file cross/arm-linux.inimeson compile -C builddir-arm

7. CMake migration cheatsheet

CMakeMeson equivalent
add_executableexecutable()
add_librarylibrary() / static_library() / shared_library()
target_include_directoriesinclude_directories arg in target
target_link_librariesdependencies / link_with
find_packagedependency()
option()option() in meson_options.txt
add_subdirectorysubdir()
install(TARGETS ...)install : true in target

For wrap patterns and subproject configuration, see references/meson-wrap.md [blocked].

Related skills

  • Use skills/build-systems/cmake when CMake is required or preferred
  • Use skills/build-systems/ninja — Meson uses Ninja as its backend
  • Use skills/compilers/cross-gcc for cross-compiler toolchain setup
  • Use skills/compilers/gcc or skills/compilers/clang for compiler flag context

来源与署名

来源:mohitmishra786/low-level-dev-skills位于skills/build-systems/meson提交bdc5847

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架