meson について
Meson は、Meson を使ったプロジェクトの初期設定、構成、依存関係管理、クロスコンパイルをエージェントに案内するスキルです。テストやビルドオプション、CMake からの移行も扱います。
主な機能
- 01Meson プロジェクトの初期設定
- 02ビルドとテストのコマンド
- 03wrap による依存関係管理
- 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
目的
GLib、systemd、GStreamer、Mesa をはじめ、多くの主要な C/C++ プロジェクトで使われているビルドシステムについて、Meson プロジェクトのセットアップ、ビルド設定、wrap 依存関係システム、クロスコンパイルをエージェントに案内します。
トリガー
- 「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 対応