meson ninja例子

meson ninja 构建编译

1. 声明用c还是c++

project('TEST', ['c', 'cpp'],
        default_options: ['buildtype=release', 'default_library=static'],
        meson_version: '>= 0.55.0')

2. 添加宏定义

if (get_option('debug_load') == true)
  add_global_arguments('-DDEBUG', language : ['c', 'cpp'])
endif

3. 加上conflags

cppflags = []
cppflags += ['-fpermissive']
cppflags += ['-g']
cppflags += ['-gdwarf-2']
cppflags += ['-march=native']
cppflags += ['-mmwaitx']
cppflags += ['-mno-avx512f']
cppflags += ['-std=c++17']
cppflags += ['-w']
cppflags += ['-Wall']
cppflags += ['-Wcast-align']
cppflags += ['-Wcast-qual']
cppflags += ['-Wdeprecated']
cppflags += ['-Werror']
cppflags += ['-Wformat-nonliteral']
cppflags += ['-Wformat-security']
cppflags += ['-Wimplicit-fallthrough=2']
cppflags += ['-Wmissing-declarations']
cppflags += ['-Wno-format-truncation']
cppflags += ['-Wno-register']
cppflags += ['-Wpointer-arith']
cppflags += ['-Wundef']
cppflags += ['-Wwrite-strings']
  
  `['-fpermissive']`: This is an array containing a single string, `-fpermissive`. This string is a compiler flag for the GNU Compiler Collection (GCC). When this flag is used, the compiler allows some non-standard code that would normally be rejected. It also downgrades some diagnostics about noncompliant code from errors to warnings. This can be useful for compiling legacy code that doesn't strictly conform to the C++ standard.

- `cppflags += ['-march=native']`: This line adds the `-march=native` flag to the `cppflags` variable. This flag tells the compiler to generate code that is optimized for the host's actual microarchitecture.

- `cppflags += ['-mmwaitx']`: This line adds the `-mmwaitx` flag to the `cppflags` variable. This flag enables the use of the `mwaitx` instruction in the generated code.

- `cppflags += ['-mno-avx512f']`: This line adds the `-mno-avx512f` flag to the `cppflags` variable. This flag disables the use of AVX-512 Foundation instructions in the generated code.

- `cppflags += ['-std=c++17']`: This line adds the `-std=c++17` flag to the `cppflags` variable. This flag tells the compiler to use the C++17 standard.

- `cppflags += ['-w']`: This line adds the `-w` flag to the `cppflags` variable. This flag suppresses all warning messages.

- `cppflags += ['-Wall']`: This line adds the `-Wall` flag to the `cppflags` variable. This flag enables all the warnings about constructions that some users consider questionable.

- `cppflags += ['-Wcast-align']`: This line adds the `-Wcast-align` flag to the `cppflags` variable. This flag warns whenever a pointer is cast such that the required alignment of the target is increased.

- `cppflags += ['-Wcast-qual']`: This line adds the `-Wcast-qual` flag to the `cppflags` variable. This flag warns whenever a pointer is cast so as to remove a type qualifier from the target type.

- `cppflags += ['-Wdeprecated']`: This line adds the `-Wdeprecated` flag to the `cppflags` variable. This flag warns about the use of deprecated features.

- `cppflags += ['-Werror']`: This line adds the `-Werror` flag to the `cppflags` variable. This flag makes all warnings into errors.

- `cppflags += ['-Wformat-nonliteral']`: This line adds the `-Wformat-nonliteral` flag to the `cppflags` variable. This flag warns if format strings are not literals.

- `cppflags += ['-Wformat-security']`: This line adds the `-Wformat-security` flag to the `cppflags` variable. This flag warns about format strings that represent possible security problems.

- `cppflags += ['-Wimplicit-fallthrough=2']`: This line adds the `-Wimplicit-fallthrough=2` flag to the `cppflags` variable. This flag sets the level of warnings about falling through case labels.

- `cppflags += ['-Wmissing-declarations']`: This line adds the `-Wmissing-declarations` flag to the `cppflags` variable. This flag warns if a global function is defined without a previous declaration.

- `cppflags += ['-Wno-format-truncation']`: This line adds the `-Wno-format-truncation` flag to the `cppflags` variable. This flag disables warnings about format truncation.

- `cppflags += ['-Wno-register']`: This line adds the `-Wno-register` flag to the `cppflags` variable. This flag disables warnings about the deprecated `register` storage class specifier.

- `cppflags += ['-Wpointer-arith']`: This line adds the `-Wpointer-arith` flag to the `cppflags` variable. This flag gives warnings about anything that depends on the "size of" a function type or of `void`.

- `cppflags += ['-Wundef']`: This line adds the `-Wundef` flag to the `cppflags` variable. This flag warns if an undefined identifier is evaluated in an `#if` directive.

- `cppflags += ['-Wwrite-strings']`: This line adds the `-Wwrite-strings` flag to the `cppflags` variable. This flag gives string constants the type `const char[length]` so that copying the address of one into a non-`const` `char *` pointer will get a warning.

  subdir('src')   excute /src/meson.build file

4. 编译子目录01

4.1 头文件

top_inc = include_directories('inc',
                             '/usr/local',
                             '/usr/local/include',
                             '/usr/include',
                             '/usr/include/python2.7')
 
if fs.exists('/usr/include/dpdk')
  cond_inc += include_directories('/usr/include/dpdk',
                                  '/usr/include/x86_64-linux-gnu/dpdk')
endif
  
polling_test_includes = [top_inc] + [cond_inc]
  

  subdir('src')

4.2 lib eg

libraries = {'dpdk'             : 'polling_dpdk_init',
            'polling_dpdk'         : 'polling_dpdk',
            'polling_fs'           : 'polling_fs',
            'polling_ebc'          : 'polling_ebc',
            'polling_eth'          : 'polling_eth',
            'polling_inet'         : 'polling_inet',
            'polling_inet/udp'     : 'polling_udp',
            'polling_inet/icmp'    : 'polling_icmp',
            'polling_inet/arp'     : 'polling_arp',
            'polling_inet/ndp'     : 'polling_ndp',
            'polling_task'         : 'polling_task',
            'polling_gui'          : 'polling_gui',
            'polling'              : 'polling_main',
            'polling_stats'        : 'polling_stats'}

foreach lib_dir, lib_name : libraries
    # Enter a sub directory
    sources = []
    subdir(lib_dir)  # 给source赋值了,跳转到4.3
    incs = include_directories(lib_dir)

    # Build a static library
    polling_lib = static_library(lib_name,
                             sources,
                             include_directories: polling_includes + [incs],
                             c_args: cflags,
                             install: true)
###
install: true: This argument specifies that the static library should be installed. When the project is installed (for example, by running ninja install), the static library will be copied to the installation directory.
###
  
  
    # Create and add dependency to total list
    polling_dep = declare_dependency(link_whole : polling_lib)
    polling_dependencies_dict += {lib_name: polling_dep}
endforeach

  polling_dependencies = []
foreach polling_dep_name, polling_dependency : polling_dependencies_dict
    polling_dependencies += polling_dependency
endforeach

4.3 lib_sub_dir meson.build

sources = files('polling_ebc_rx.c',
                'polling_ebc_rx_dispatch.c',
			   'polling_ebc_tx.c')

includes += include_directories('../inc',
                                'model/inc')

4.4 gprc module

# protoc-c --proto_path=. rrc.proto --c_out=gen.protobuf
my_protoc  = find_program('protoc-c')
proto_path = '/usr/include/google/protobuf/'

gen_c = generator(
  my_protoc,
  output    : ['@BASENAME@.pb-c.c'],
  arguments : ['--proto_path=@CURRENT_SOURCE_DIR@', '--c_out=@BUILD_DIR@', '@INPUT@'])

gen_h = generator(
  my_protoc,
  output    : ['@BASENAME@.pb-c.h'],
  arguments : ['--proto_path=@CURRENT_SOURCE_DIR@', '--c_out=@BUILD_DIR@', '@INPUT@'])
gen_src = gen_c.process(['rrc.proto'])
gen_c_code += [gen_src]
gen_src = gen_h.process(['rrc.proto'])
gen_headers += [gen_src]
  
incs_module   = include_directories('.',
                                    '/usr/include')

rrc_headers_dep = declare_dependency(sources : gen_headers)

my_lib = static_library('rrc',
                        gen_c_code,
                        include_directories : incs_module,
                        dependencies        : rrc_headers_dep,
                        install             : true)

4.5 other lib

foreach TEST_module : TEST_modules
  sources = []
  includes = base_includes
  subdir(TEST_module / 'src')

  # Build static lib
  my_lib = static_library(TEST_module,
                          sources,
                          include_directories: includes,
                          install: true,
                          dependencies: external_dependencies,
                          c_args : cflags + ['-DPOLLING_THIS_MODULE=' + TEST_module])

  dep = declare_dependency(link_with: my_lib, link_whole: my_lib)
  TEST_dependencies_dict += {TEST_module: dep}
endforeach

5. 添加第三方依赖

  TEST_dependencies += [compiler.find_library('z')]
  TEST_dependencies += [dependency('gpg-error')]
  TEST_dependencies += [dependency('gpr')]
  TEST_dependencies += [dependency('grpc')]
  TEST_dependencies += [dependency('grpc++')]
  TEST_dependencies += [dependency('libdpdk', static : true)]
  TEST_dependencies += [dependency('lttng-ust')]
    
  args += ['-Wl,--no-whole-archive']
  dep = declare_dependency(link_args : args)
  TEST_dependencies += [dep]
dependency('name'): This function is used to find and add a dependency to your project using pkg-config or other means. It looks for a file named name.pc in the pkg-config path. This file contains metadata about the dependency, including the location of its header files and libraries. If the dependency is found, it is added to the project, and the compiler is instructed to link against it when building the project.

compiler.find_library('name'): This function is used to find a library on the system. It does not use pkg-config, but instead uses the compiler's built-in mechanisms for finding libraries. This function is useful for linking against system libraries that do not provide pkg-config files.
    

6. 编译链接

  executable('TEST-engine',
             sources      : grpc_sources,
             dependencies : TEST_dependencies + polling_dependencies,
             cpp_args     : cppflags + ['-Dpolling_THIS_MODULE=grpc_adapter'],
             link_args    : ['-Bstatic'] + wrap_args)
    
    
executable('TEST-engine', ...): This function call is creating a new executable target named 'TEST-engine'. The executable function is a method provided by Meson that defines an executable target that will be built from a set of source files.

sources : grpc_sources: This argument specifies the source files that will be compiled to create the executable. The grpc_sources variable should contain a list of source files.

dependencies : TEST_dependencies + polling_dependencies: This argument specifies the dependencies that will be linked to the executable. The TEST_dependencies and polling_dependencies variables should contain lists of dependencies.

cpp_args : cppflags + ['-Dpolling_THIS_MODULE=grpc_adapter']: This argument specifies additional arguments that will be passed to the C++ compiler when compiling the source files. The cppflags variable should contain a list of compiler flags, and '-Dpolling_THIS_MODULE=grpc_adapter' is an additional flag that defines a preprocessor macro.

link_args : ['-Bstatic'] + wrap_args: This argument specifies additional arguments that will be passed to the linker when linking the executable. The '-Bstatic' flag tells the linker to link the libraries statically, and wrap_args should contain additional linker arguments.

7. ninja编译

    ninja | tee /src/TEST/TEST_build_log.txt; NINJA_RETURN_CODE=${PIPESTATUS[0]}
    if [[ "$NINJA_RETURN_CODE" -ne 0 ]]
    then
       log_error "[!] Compilation failed, aborting"
    fi

交叉编译

交叉编译需要指定meson构建的配置文件,如下,可以命名文件为cross_cfg.ini

[binaries]
c = 'aarch64-linux-gnu-gcc'
cpp = 'aarch64-linux-gnu-g++'
ar = 'aarch64-linux-gnu-ar'
ld = 'aarch64-linux-gnu-ld'

[host_machine]
system = 'linux'
cpu_family = 'aarch64'
cpu = 'aarch64' 
endian = 'little'

然后用meson builddir --cross-file cross_cfg.ini构建

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值