前一节1.2 软件开发环境准备学习了软件开发环境的搭建,但部分细节还讳莫如深,择日不如撞日,我们赶紧认识一下。本节对编译过程中的 Cmake 和 Ninja 做个拓展了解,这也是官方教程没体现的部分。
我正在学习 Zephyr,一个很可能会用到很多物联网设备上的操作系统,如果你也感兴趣,可点此查看帖子zephyr学习笔记汇总。
Build 过程中有用到 Cmake 命令来配置生成 Ninja 编译所需的文件。
# Use cmake to configure a Ninja-based build system:
cmake -GNinja -DBOARD=nrf52_pca10040 ..
使用 Ninja 时需要生成 build.ninja 文件,对于大型项目来说一条一条地写配置文件是不可能的,可以使用Cmake来生成这个配置文件。
Cmake支持参数Camke -G Ninja,Cmake会根据用户给定的CMakeLists.txt来生成build.ninja文件,接着才能进行ninja编译。
CmakeLists文件的语法这里写不深究,结合build日志感受下。
D:\zephyr_path\zephyr\samples\basic\blinky\build>cmake -GNinja -DBOARD=nucleo_l073rz ..
CMake Deprecation Warning at D:/winter/resources/zephyr/zephyr/cmake/app/boilerplate.cmake:38 (cmake_policy):
The OLD behavior for policy CMP0000 will be removed from a future version
of CMake.
The cmake-policies(7) manual explains that the OLD behaviors of all
policies are deprecated and that a policy should be set to OLD only under
specific short-term circumstances. Projects should be ported to the NEW
behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
CMakeLists.txt:1 (include)
-- Selected BOARD nucleo_l073rz
Zephyr version: 1.11.99
Parsing Kconfig tree in D:/winter/resources/zephyr/zephyr//Kconfig
Using D:/zephyr_path/zephyr/samples/basic/blinky/build/zephyr/.config as base
-- Generating zephyr/include/generated/generated_dts_board.h
nucleo_l073rz.dts_compiled: Warning (unit_address_vs_reg): Node /soc/pin-controller has a reg or ranges property, but no unit name
nucleo_l073rz.dts_compiled: Warning (unit_address_vs_reg): Node /soc/pin-controller/usart1@0 has a unit name, but no reg property
nucleo_l073rz.dts_compiled: Warning (unit_address_vs_reg): Node /soc/pin-controller/usart2@0 has a unit name, but no reg property
-- Configuring done
-- Generating done
-- Build files have been written to: D:/zephyr_path/zephyr/samples/basic/blinky/build
在 /zephyr/samples/basic/blinky/build 目录下,就看到了一堆 build 所需的文件。
Ninja 官网点击这里。
不过我们使用 ninja -help 命令,也能看到 Ninja 常见命令,如下:
Cleaning targets:
clean - Remove most generated files but keep configuration and backup files
pristine - Remove all files in the build directory
Configuration targets:
run <ninja kconfig-usage>
Other generic targets:
all - Build a zephyr application
run - Build a zephyr application and run it if the board supports emulation
flash - Build and flash an application
debug - Build and debug an application using GDB
debugserver - Build and start a GDB server (port 1234 for Qemu targets)
ram_report - Build and create RAM usage report
rom_report - Build and create ROM usage report
usage - Display this text
特别是 pristine 命令,如果工程要切换 board,那么就需要这么做。
这篇笔记对工程编译用到的 Cmake 和 Ninja 做了个简单了解。