meson & ninja 編譯構建指導


升級dpdk-stable-20.11.1版本發現,原來默認make方式構建已經沒有了;變成了meson & ninja組合,試了一下,構建速度提升了不少;

meson build && ninja -C build

// sudo ninja -C build install

趕緊去學習下吧, 后面試着把那些又臭又長的Makefile或者CMakeLists.txt替換掉吧,簡單看了下manual,感覺和cmake差不多,為啥快也沒了解。先學習下傍身!

demo例子就兩行,很簡潔:

project('tutorial', 'c')  # C語言工程

executable('demo', 'main.c') # target為demo 依賴 main.c

官網手冊在這里:

https://mesonbuild.com/Reference-manual.html

大概掃了下,感覺比較詳細;看了下 DPDK 的 meson.build 比看makefile舒坦多了,整個才100+ line; 即使你不完全明白是啥, 根據cmake也猜出來差不多,關鍵是個人體驗真的變快了。下面把 DPDK 的構建系統說下吧。DPDK下載的是LTS版本(DPDK 20.11.1 (LTS) http://core.dpdk.org/download/ )

meson_options.txt (like default_options in project(), they only have effect when Meson is run for the first time, and command line arguments override any default options in build files)

 

get_compiler(language): returns an object describing a compiler, takes one positional argument which is the language to use

例如獲取C compiler:cc = meson.get_compiler('c')

 

This object is returned by configuration_data() and encapsulates configuration values to be used for generating configuration files。

eg:dpdk_conf = configuration_data() # 用於生成配置文件,支撐一些method,參考描述: configuration data object

host_machine objec

eg: 判斷編譯機體系結構: if host_machine.cpu_family().startswith('x86')

[host_machine] system = 'windows' cpu_family = 'x86' cpu = 'i686' endian = 'little'
大概長這樣子,主要是為了交叉編譯環境使用。

include_directories (since 0.38.0): specifies extra directories for header searches. # 這個和cmake 一樣了

eg:

global_inc = include_directories('.', 'config',
'lib/librte_eal/include',
'lib/librte_eal/@0@/include'.format(host_machine.system()),  # 注意這里,@0@   .format(XX)還沒看哪里說明的語法,先記着雜用就行了
'lib/librte_eal/@0@/include'.format(arch_subdir),
)

subdir()  # 描述同camke 構建子目錄
void subdir(dir_name, ...)
Enters the specified subdirectory and executes the meson.build file in it.

EG:

subdir('buildtools')
subdir('config')

# build libs and drivers
subdir('buildtools/pmdinfogen') # 注意上面一句 subdir('buildtools') 說明subdir只構建指定的子目錄,不會去子目錄的子目錄。
subdir('lib')
subdir('drivers')

 

install_subdir() # 指定subdir的 install 路徑
void install_subdir(subdir_name,
install_dir : ...,
exclude_files : ...,
exclude_directories : ...,
strip_directory : ...)
Installs the entire given subdirectory and its contents from the source tree to the location specified by the keyword argument install_dir.

eg: 指定路徑,包含的文件  和 exclude文件

subdir('examples')
install_subdir('examples',
install_dir: get_option('datadir') + '/dpdk',
exclude_files: 'meson.build')

 

get_option() #這個東西獲取工程可配置參數
value get_option(option_name)
Obtains the value of the project build option specified in the positional argument.

Note that the value returned for built-in options that end in dir such as bindir and libdir is always a path relative to (and inside) the prefix.

 

條件編譯開關形式

if get_option('enable_kmods')
subdir('kernel')
endif

 

# write the build config
build_cfg = 'rte_build_config.h'
configure_file(output: build_cfg,
configuration: dpdk_conf,
install_dir: join_paths(get_option('includedir'),
get_option('include_subdir_arch')))

 

支持 foreach

foreach lib:enabled_libs
output_message += lib + ', '
output_count += 1
if output_count == 8
output_message += '\n\t'
output_count = 0
endif
endforeach
message(output_message + '\n')  支持message打印

 

get_variable() # 動態獲取變量值
value get_variable(variable_name, fallback)
This function can be used to dynamically obtain a variable. res = get_variable(varname, fallback) takes the value of varname (which must be a string) and stores the variable of that name into res. If the variable does not exist, the variable fallback is stored to resinstead. If a fallback is not specified, then attempting to read a non-existing variable will cause a fatal error.

meson.build 中出現的相關函數就看完了,很好懂,和cmake語法很像, 后面再看下,編譯具體lib怎么處理庫和文件 還有編譯參數的;

 

隨便看一個 drivers 下面的 meson.build;掃了一眼 200行,可接受;

 

static_library()
buildtarget static_library(library_name, list_of_sources, ...)
Builds a static library with the given sources. Positional and keyword arguments are otherwise the same as for library, but it has one argument the others don't have:

后面在梳理吧 ~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM