Meson compilation system

1. What is Meson?

Meson is a compilation system designed to build quickly, and can be built on multiple platforms, supporting Linux, Windows and MacOS. Not only supports compiling C/C++, but also supports D, Fortran, Java, Rust and other languages.

Meson is implemented based on Python3 and depends on Ninja. Meson and Ninja complement each other. Meson takes care of building the project dependencies and Ninja does the compilation. Ninja is also a lightweight compilation system, and I will introduce Ninja later when I find an opportunity.

The official website of Meson is: https://mesonbuild.com

2. Install

  1. install Python3andpip3

  2. InstallNinja

    apt way:

    $ sudo apt-get install ninja-build
    

    pip3 way:

    $ pip3 install ninja
    
  3. InstallMeson

    $ pip3 install meson 
    

    It can also be installed through the apt method, only due to the relationship between the source, the installed version may be relatively low, so it is recommended to use the pip3installation method.

3. Hello World

In this section, we use Meson to build a simple C language program.

  1. Create a C language hello world program:

    #include <stdio.h>
    
    int main(int argc, char **argv) {
          
          
      printf("Hello there.\n");
      return 0;
    }
    
  2. Create a compilation script in the same directory meson.build:

    project('tutorial', 'c')
    executable('demo', 'main.c')
    
  3. Set the compilation directory (builddir is the directory name, and all files generated by compilation will be automatically stored in this directory):

    $ meson setup builddir
    

    After execution, the system will print out the configuration information of the project:

    The Meson build system
    Version: 1.0.0
    Source dir: /home/xupeng/work/project/tmp/meson
    Build dir: /home/xupeng/work/project/tmp/meson/builddir
    Build type: native build
    Project name: tutorial
    Project version: undefined
    C compiler for the host machine: ccache cc (gcc 5.4.0 "cc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609")
    C linker for the host machine: cc ld.bfd 2.26.1
    Host machine cpu family: x86_64
    Host machine cpu: x86_64
    Build targets in project: 1
    
  4. Enter the compilation directory to compile the code:

    $ cd builddir
    $ ninja
    # 或者
    $ cd builddir
    $ meson compile
    

    We can also compile directly in the original directory:

    $ meson compile -C builddir
    

    After execution, the system will print the compilation information:

    INFO: autodetecting backend as ninja
    INFO: calculating backend command to run: /usr/bin/ninja
    [2/2] Linking target demo
    
  5. After the compilation is successful, the target executable file demo is generated in the compilation directory:

    $ ./demo
    Hello there.
    

4. Compile the library

Earlier we used Meson to compile an executable file. If we need to compile an so library, we only need to meson.buildadjust it to:

project('tutorial', 'c')

# 将编译可执行文件的方式改成编译库的方式
# executable('demo', 'main.c')
library('demo', 'main.c')

Execute again meson compile, and the target library will be generated in the compilation directory libdemo.so.

5. Meson script

Meson's compilation configuration and scripts are written in meson.build, so if you want to perform various compilation configurations, you only need to complete the script. Meson provides a series of syntax and API for us to make more flexible and powerful configuration.

In terms of syntax, because Meson is implemented based on Python3, the syntax of Meson scripts is very similar to Python. For details, please refer to the syntax section of the official website - https://mesonbuild.com/Syntax.html

Other compilation configurations and scripts mainly include:

  • Compilation Type - Configure whether to compile as an executable or a library
  • header file - path to the header file that configuration depends on
  • Install - configure the installation actions meson installafter
  • Parameters - configuration input parameters at compile time
  • Dependency - Dependency library when configuring compile time
  • Threads - configures threads at compile time
  • externalcommand - Executes an external shell command
  • Generate - Code Preconfiguration
  • test - compiles and executes unit tests
  • Cross-compilation - cross-compilation information configuration
  • Compile Options - meson_options.txtDynamic configuration of compiled modules in
  • Subproject - configuration of subproject

As you can see, there are still a lot of configurable items. For details, please refer to the manual section of the official website - https://mesonbuild.com/Manual.html .

6. Reference connections

Guess you like

Origin blog.csdn.net/ZivXu/article/details/128551299