$npx -y skills add mohitmishra786/low-level-dev-skills --skill mesonMeson 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, c
| 1 | # Meson |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "How do I set up a Meson build?" |
| 10 | - "How do I add a dependency in Meson?" |
| 11 | - "How do I cross-compile with Meson?" |
| 12 | - "Meson wrap isn't finding my dependency" |
| 13 | - "How do I configure build options in Meson?" |
| 14 | - "How do I migrate from CMake/Autotools to Meson?" |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Project setup |
| 19 | |
| 20 | ```bash |
| 21 | # Configure (source-dir, build-dir are positional) |
| 22 | meson setup builddir |
| 23 | |
| 24 | # With options |
| 25 | meson setup builddir \ |
| 26 | --buildtype=release \ |
| 27 | --prefix=/usr/local \ |
| 28 | -Db_lto=true \ |
| 29 | -Db_sanitize=address |
| 30 | |
| 31 | # Reconfigure (change options on existing builddir) |
| 32 | meson configure builddir -Dbuildtype=debug |
| 33 | |
| 34 | # Show all available options |
| 35 | meson configure builddir |
| 36 | ``` |
| 37 | |
| 38 | | `--buildtype` | Equivalent flags | |
| 39 | |---------------|-----------------| |
| 40 | | `debug` (default) | `-O0 -g` | |
| 41 | | `debugoptimized` | `-O2 -g` | |
| 42 | | `release` | `-O3 -DNDEBUG` | |
| 43 | | `minsize` | `-Os -DNDEBUG` | |
| 44 | | `plain` | No flags added | |
| 45 | |
| 46 | ### 2. Build and test |
| 47 | |
| 48 | ```bash |
| 49 | # Build (from source directory) |
| 50 | meson compile -C builddir |
| 51 | |
| 52 | # Or enter builddir and use ninja directly |
| 53 | cd builddir && ninja |
| 54 | |
| 55 | # Run tests |
| 56 | meson test -C builddir |
| 57 | meson test -C builddir --verbose # show test output |
| 58 | meson test -C builddir -t 5 # 5x timeout multiplier |
| 59 | meson test -C builddir --suite unit # run only 'unit' suite |
| 60 | |
| 61 | # Install |
| 62 | meson install -C builddir |
| 63 | # Dry run |
| 64 | meson install -C builddir --dry-run |
| 65 | ``` |
| 66 | |
| 67 | ### 3. meson.build structure |
| 68 | |
| 69 | ```python |
| 70 | project('myapp', 'c', 'cpp', |
| 71 | version : '1.0.0', |
| 72 | default_options : [ |
| 73 | 'c_std=c11', |
| 74 | 'cpp_std=c++17', |
| 75 | 'warning_level=2', |
| 76 | ] |
| 77 | ) |
| 78 | |
| 79 | # Find system dependencies |
| 80 | glib_dep = dependency('glib-2.0', version : '>=2.68') |
| 81 | threads_dep = dependency('threads') |
| 82 | |
| 83 | # Include directories |
| 84 | inc = include_directories('include') |
| 85 | |
| 86 | # Library |
| 87 | mylib = static_library('mylib', |
| 88 | sources : ['src/lib.c', 'src/util.c'], |
| 89 | include_directories : inc, |
| 90 | ) |
| 91 | |
| 92 | # Executable |
| 93 | executable('myapp', |
| 94 | sources : ['src/main.c'], |
| 95 | include_directories : inc, |
| 96 | link_with : mylib, |
| 97 | dependencies : [glib_dep, threads_dep], |
| 98 | install : true, |
| 99 | ) |
| 100 | |
| 101 | # Tests |
| 102 | test('basic', executable('test_basic', |
| 103 | sources : ['tests/test_basic.c'], |
| 104 | link_with : mylib, |
| 105 | )) |
| 106 | ``` |
| 107 | |
| 108 | ### 4. Dependency management with wrap |
| 109 | |
| 110 | ```bash |
| 111 | # Search for a wrap |
| 112 | meson wrap search zlib |
| 113 | |
| 114 | # Install a wrap (downloads .wrap file from wrapdb) |
| 115 | meson wrap install zlib |
| 116 | meson wrap install gtest |
| 117 | |
| 118 | # List installed wraps |
| 119 | meson wrap list |
| 120 | |
| 121 | # Update all wraps |
| 122 | meson wrap update |
| 123 | ``` |
| 124 | |
| 125 | In `meson.build`: |
| 126 | |
| 127 | ```python |
| 128 | # Use wrap as fallback if system lib not found |
| 129 | zlib_dep = dependency('zlib', |
| 130 | fallback : ['zlib', 'zlib_dep'], # [wrap_name, dep_variable] |
| 131 | ) |
| 132 | |
| 133 | # Force wrap (for reproducible builds) |
| 134 | zlib_dep = dependency('zlib', |
| 135 | fallback : ['zlib', 'zlib_dep'], |
| 136 | default_options : ['default_library=static'], |
| 137 | ) |
| 138 | ``` |
| 139 | |
| 140 | `subprojects/zlib.wrap` structure: |
| 141 | |
| 142 | ```ini |
| 143 | [wrap-file] |
| 144 | directory = zlib-1.3 |
| 145 | source_url = https://zlib.net/zlib-1.3.tar.gz |
| 146 | source_hash = <sha256> |
| 147 | |
| 148 | [provide] |
| 149 | zlib = zlib_dep |
| 150 | ``` |
| 151 | |
| 152 | ### 5. Build options |
| 153 | |
| 154 | Define in `meson_options.txt` (or `meson.options` in Meson ≥1.1): |
| 155 | |
| 156 | ```python |
| 157 | option('with_tests', type : 'boolean', value : true, |
| 158 | description : 'Build unit tests') |
| 159 | |
| 160 | option('backend', type : 'combo', |
| 161 | choices : ['opengl', 'vulkan', 'software'], |
| 162 | value : 'opengl', |
| 163 | description : 'Rendering backend') |
| 164 | |
| 165 | option('max_connections', type : 'integer', |
| 166 | min : 1, max : 1024, value : 64) |
| 167 | ``` |
| 168 | |
| 169 | Use in `meson.build`: |
| 170 | |
| 171 | ```python |
| 172 | if get_option('with_tests') |
| 173 | subdir('tests') |
| 174 | endif |
| 175 | ``` |
| 176 | |
| 177 | ### 6. Cross-compilation |
| 178 | |
| 179 | Create a cross file (`cross/arm-linux.ini`): |
| 180 | |
| 181 | ```ini |
| 182 | [binaries] |
| 183 | c = 'arm-linux-gnueabihf-gcc' |
| 184 | cpp = 'arm-linux-gnueabihf-g++' |
| 185 | ar = 'arm-linux-gnueabihf-ar' |
| 186 | strip = 'arm-linux-gnueabihf-strip' |
| 187 | pkgconfig = 'arm-linux-gnueabihf-pkg-config' |
| 188 | |
| 189 | [properties] |
| 190 | sys_root = '/path/to/sysroot' |
| 191 | |
| 192 | [host_machine] |
| 193 | system = 'linux' |
| 194 | cpu_family = 'arm' |
| 195 | cpu = 'cortex-a9' |
| 196 | endian = 'little' |
| 197 | ``` |
| 198 | |
| 199 | ```bash |
| 200 | meson setup builddir-arm --cross-file cross/arm-linux.ini |
| 201 | meson compile -C builddir-arm |
| 202 | ``` |
| 203 | |
| 204 | ### 7. CMake migration cheatsheet |
| 205 | |
| 206 | | CMake | Meson equivalent | |
| 207 | |-------|-----------------| |
| 208 | | `add_executable` | `executable()` | |
| 209 | | `add_library` | `library()` / `static_library()` / `shared_library()` | |
| 210 | | `target_include_directories` | `include_directories` arg in target | |
| 211 | | `target_link_libraries` | `dependencies` / `link_with` | |
| 212 | | `find_package` | `dependency()` | |
| 213 | | `option()` | `option()` in `meson_options.txt` | |
| 214 | | `add_subdirectory` | `subdir()` | |
| 215 | | `i |