お試しMeson


まえがき

前にビルドツールのninjaを手書きで書いたbuild.ninjaで動かしてみましたが、今回はメタビルドツールと呼ばれるmesonをつかってbuild.ninjaを生成してみようと思います。mesonはmakeで言うところのAutotoolsみたいなものです。meson公式サイトが参考になります。

環境

  • ubuntu 18.04
  • meson 0.54.0
  • ninja 1.8.2

インストール

mesonはpythonで作られているらしく、pipで管理されています。
pipでインストールします。

$ sudo apt install python3-pip
$ pip3 install meson --user
$ pip3 list --user --format=columns
Package Version
------- -------
meson   0.54.0

バージョン0.54.0がインストールされました。

$ meson --version
0.54.0
$ meson --help
usage: meson [-h] {setup,configure,dist,install,introspect,init,test,wrap,subprojects,help,rewrite,compile} ...

optional arguments:
  -h, --help                                                               show this help message and exit

Commands:
  If no command is specified it defaults to setup command.

  {setup,configure,dist,install,introspect,init,test,wrap,subprojects,help,rewrite,compile}
    setup                                                                  Configure the project
    configure                                                              Change project options
    dist                                                                   Generate release archive
    install                                                                Install the project
    introspect                                                             Introspect project
    init                                                                   Create a new project
    test                                                                   Run tests
    wrap                                                                   Wrap tools
    subprojects                                                            Manage subprojects
    help                                                                   Print help of a subcommand
    rewrite                                                                Modify the project definition
    compile                                                                Build the project

サンプル

// hello.cpp
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!" << endl;
}
project('helloworld',
        'cpp',
        default_options : [
                'warning_level=3',
                'cpp_std=gnu++14'
        ])

src = ['hello.cpp']

executable('hello', src)

ビルド

二つのファイルがある状態でmeson setup <アウトプットディレクトリ>で実行します。

$ ls
hello.cpp  meson.build
$ meson setup ./build
The Meson build system
Version: 0.54.0
Source dir: /home/yoshi/meson
Build dir: /home/yoshi/meson/build
Build type: native build
Project name: helloworld
Project version: undefined
C++ compiler for the host machine: c++ (gcc 7.5.0 "c++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0")
C++ linker for the host machine: c++ ld.bfd 2.30
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1

Found ninja-1.8.2 at /usr/bin/ninja

./buildディレクトリの下にbuild.ninjaとその他ファイルが生成されています。

$ ls ./build
build.ninja  compile_commands.json  meson-info  meson-logs  meson-private

実際のビルドはninjaコマンドで行います。-Cオプションでビルドディレクトリを指定します。
-vオプションで実行したコマンドの詳細を表示させます。

$ ninja -v -C ./build
ninja: Entering directory `./build'
[1/2] c++ -Ihello@exe -I. -I.. -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -Wextra -Wpedantic -std=gnu++14 -g -MD -MQ 'hello@exe/hello.cpp.o' -MF 'hello@exe/hello.cpp.o.d' -o 'hello@exe/hello.cpp.o' -c ../hello.cpp
[2/2] c++  -o hello 'hello@exe/hello.cpp.o' -Wl,--as-needed -Wl,--no-undefined

./buildディレクトリに実行ファイルが作成されてます。

$ ./build/hello
Hello, World!