2023-10-27 11:26:25 +02:00
====================
Compiling with CMake
====================
2020-09-04 23:00:59 +02:00
2023-10-27 11:26:25 +02:00
Initialize Configuration with CMake
===================================
2020-09-04 23:00:59 +02:00
2023-08-22 14:06:52 +02:00
The first step is to initialize NuttX configuration for a given board, based on
2020-09-11 23:55:50 +02:00
a pre-existing configuration. To list all supported configurations you can do:
2020-09-04 23:00:59 +02:00
2020-09-11 23:55:50 +02:00
.. code-block :: console
2020-09-04 23:00:59 +02:00
$ cd nuttx
$ ./tools/configure.sh -L | less
2020-10-18 18:48:44 +02:00
2020-09-11 23:55:50 +02:00
The output is in the format `` <board name>:<board configuration> `` . You will see that
2021-04-28 21:14:44 +02:00
generally all boards support the `` nsh `` configuration which is a good starting point
2020-09-11 23:55:50 +02:00
since it enables booting into the interactive command line
2020-10-20 10:40:45 +02:00
:doc: `/applications/nsh/index` .
2020-09-04 23:00:59 +02:00
2023-10-27 11:26:25 +02:00
To choose a configuration you pass the `` <board name>:<board configuration> `` such as:
2020-09-04 23:00:59 +02:00
2020-09-11 23:55:50 +02:00
.. code-block :: console
2020-09-04 23:00:59 +02:00
$ cd nuttx
2023-10-27 11:26:25 +02:00
$ cmake -B build -DBOARD_CONFIG=stm32f4discovery:nsh -GNinja
2020-10-18 18:48:44 +02:00
2023-10-28 21:40:30 +02:00
The `` -B build `` tells what is the build directory.
2020-09-11 23:55:50 +02:00
2020-11-21 23:55:16 +01:00
You can then customize this configuration by using the menu based
configuration system with:
2020-09-04 23:00:59 +02:00
2020-09-11 23:55:50 +02:00
.. code-block :: console
2020-09-04 23:00:59 +02:00
2022-03-16 18:08:00 +01:00
$ cd nuttx
2023-10-27 11:26:25 +02:00
$ cmake --build build -t menuconfig
2021-04-28 21:14:44 +02:00
2020-11-21 23:55:16 +01:00
Modifying the configuration is covered in :doc: `configuring` .
2020-09-04 23:00:59 +02:00
2023-10-27 11:26:25 +02:00
Build NuttX with CMake
======================
2020-09-04 23:00:59 +02:00
2020-09-11 23:55:50 +02:00
We can now build NuttX. To do so, you can simply run:
2020-09-04 23:00:59 +02:00
2020-09-11 23:55:50 +02:00
.. code-block :: console
2020-09-04 23:00:59 +02:00
2022-03-16 18:08:00 +01:00
$ cd nuttx
2024-02-14 14:03:45 +01:00
$ cmake --build build
2020-10-18 18:48:44 +02:00
2020-09-11 23:55:50 +02:00
The build will complete by generating the binary outputs
2024-02-14 14:03:45 +01:00
inside `` build/ `` directory. Typically this includes the `` nuttx ``
2020-11-21 23:55:16 +01:00
ELF file (suitable for debugging using `` gdb `` ) and a `` nuttx.bin ``
2020-10-18 18:48:44 +02:00
file that can be flashed to the board.
2020-09-11 23:55:50 +02:00
To clean the build, you can do:
2020-09-04 23:00:59 +02:00
2020-09-11 23:55:50 +02:00
.. code-block :: console
2020-09-04 23:00:59 +02:00
2023-10-27 11:26:25 +02:00
$ cmake --build build -t clean
2024-07-17 07:05:01 +02:00
Out-of-tree building
====================
Key benefit of CMake is the out-of-tree building, which allows one to have different build folders for different configs, very proper if one need check multiple configs for the same codebase. Out-of-tree means above `` build `` folders can be out of Nuttx source tree.
Suppose `` $NUTTX_DIR `` is the nuttx source tree, we can use temporary folder for a particular target config as shown below.
.. code-block :: console
$ echo $NUTTX_DIR
/home/user/Projects/Nuttx/nuttx
$ mkdir -p ~/tmp/rv32/nsh
$ cd ~/tmp/rv32/nsh
# Make sure a proper toolchain is in your $PATH
$ riscv64-unknown-elf-gcc -v
$ cmake $NUTTX_DIR -DBOARD_CONFIG=rv-virt:nsh -GNinja
-- Initializing NuttX
-- Board: rv-virt
-- Config: nsh
-- Appdir: /home/yf/Projects/Nuttx/apps
-- The C compiler identification is GNU 10.2.0
-- The CXX compiler identification is GNU 10.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/riscv64-unknown-elf-gcc
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yf/tmp/rv32/nsh
$ ninja
$ size nuttx
text data bss dec hex filename
167411 365 11568 179344 2bc90 nuttx
This approach works for FLAT configs now and PROTECTED configs soon if needed CMake scripts are available already.
Building KERNEL configs
=======================
We can use CMake to build the kernel image for KERNEL configs now, assuming apps ROMFS is prepared using the makefile system. If the development focus is kernel side and apps don't change often, then CMake can help us achieve out-of-tree build if your device's CMake scripts are ready. Let's take `` canm230 `` device as an example:
.. code-block :: console
$ echo $NUTTX_DIR
/home/user/Projects/Nuttx/nuttx
$ mkdir -p ~/tmp/k230/nsbi
# copy the romfs_boot.c to build folder
$ cp romfs_boot.c ~/tmp/k230/nsbi
$ cd ~/tmp/k230/nsbi
$ ls -l
total 976
-rw-rw-r-- 1 yf yf 997843 Jul 15 06:23 romfs_boot.c
$ cmake $NUTTX_DIR -DBOARD_CONFIG=canmv230:nsbi -GNinja
-- Initializing NuttX
-- Board: canmv230
-- Config: nsbi
-- Appdir: /home/yf/Projects/Nuttx/apps
-- The C compiler identification is GNU 10.2.0
-- The CXX compiler identification is GNU 10.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/riscv64-unknown-elf-gcc
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yf/tmp/k230/nsbi
$ ninja
$ size nuttx
text data bss dec hex filename
281671 609 37496 319776 4e120 nuttx
Note that for QEMU targets, we can directly use the apps binary on host folder via `` hostfs `` in QEMU.
So even apps side CMake support is not ready, we still can enjoy CMake for kernel build with KERNEL configs.