nuttx icon indicating copy to clipboard operation
nuttx copied to clipboard

add CMake build system support (Enhance #3704)

Open anchao opened this issue 2 years ago • 3 comments

Summary

enhance cmake build system (Enhance #3704)

  1. Update all CMakeLists.txt to adapt to new layout
  2. Fix cmake build break
  3. Update all new file license
  4. Fully compatible with current compilation environment(use configure.sh or cmake as you choose)

How to test

From within nuttx/. Configure:

$ cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja
$ cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat -GNinja
$ cmake -B build -DBOARD_CONFIG=sim/nsh -GNinja

This uses ninja generator (install with sudo apt install ninja-build). To build:

$ cmake --build build

menuconfig:

$ cmake --build build -t menuconfig

Compilation speed:

| lm3s6965-ek/qemu-flat:
| Orignal     : $ time `./tools/configure.sh lm3s6965-ek/qemu-flat > /dev/null && make -j16 > /dev/null`
| CMake       : $ time `cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat > /dev/null;cmake --build build -j16 > /dev/null`
| CMake(Ninja): $ time `cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat -GNinja > /dev/null;cmake --build build -j16 > /dev/null`
|
|         Orignal        CMake     CMake(Ninja)
| real   0m9.982s     0m4.739s         0m4.475s
| user   1m5.336s    0m41.846s        0m37.434s
| sys   0m16.428s     0m9.239s         0m7.998s
|
|
| Orignal     : $ time `./tools/configure.sh sabre-6quad/smp > /dev/null && make -j16 > /dev/null`
| CMake       : $ time `cmake -B build -DBOARD_CONFIG=sabre-6quad/smp > /dev/null;cmake --build build -j16 > /dev/null`
| CMake(Ninja): $ time `cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja > /dev/null;cmake --build build -j16 > /dev/null`
|
|         Orignal        CMake     CMake(Ninja)
| real  0m10.921s     0m3.936s         0m3.616s
| user  0m51.603s    0m32.621s        0m29.102s
| sys   0m13.720s     0m8.075s         0m6.460s

Signed-off-by: chao.an [email protected]

Impact

Depends on: https://github.com/apache/incubator-nuttx-apps/pull/1236

Testing

cmake -B build -DBOARD_CONFIG=sim/nsh -GNinja cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat -GNinja

anchao avatar Jul 27 '22 04:07 anchao

@anchao - glad that worked out for you - The format looks really good.

there are more important features need to support at this stage,

Are there still open tasks or is that ready to come in?

davids5 avatar Jul 29 '22 14:07 davids5

@anchao - glad that worked out for you - The format looks really good.

there are more important features need to support at this stage,

Are there still open tasks or is that ready to come in?

The current PR still needs some minor improvement, but the review can be started now! I hope this PR can be merged in first since the cmake build system fully compatible with current compilation environment, so that more developers can improve it together. (kernel/protect/export build will be implement in a separate PR)

anchao avatar Aug 01 '22 08:08 anchao

@davids5 Could you please review this PR further? the functions required for FLAT build are basically supported

anchao avatar Aug 03 '22 09:08 anchao

Is this already complete or there's still work to be done ? I think CMake would be a great addition to NuttX!

lucasssvaz avatar Apr 10 '23 02:04 lucasssvaz

Hi all, @acassis @davids5 @xiaoxiang781216 @pkarashchenko

I rebase this PR based on the latest dev, please review again, thanks!

anchao avatar Jul 04 '23 07:07 anchao

I would suggest merging CMake patch to the mainline and collect the feedback from the community if no other critical objection.

xiaoxiang781216 avatar Jul 04 '23 16:07 xiaoxiang781216

Given this CMake project is meant to be built from the user project repository (as opposed to the traditional CMake workflow), IMHO instructions like the ones suggested below would be appreciated by some users: I think these instructions would fit either on the top-level CMakeLists.txt or the project README.md - feel free to choose any (or both) of them.

Thanks, I added this comment snippet at start of nuttx/CMakeLists.txt.

anchao avatar Jul 07 '23 05:07 anchao

@anchao What do you think about creating some CI targets to to ensure this continues to be functional?

Hi, @davids5 , of course, I will send a separate PR later to add cmake-related functional verification on ci-flow

anchao avatar Jul 07 '23 06:07 anchao

Let's ignore macOS CI temp broken.

xiaoxiang781216 avatar Jul 08 '23 05:07 xiaoxiang781216

@anchao

I noticed that the following error happens with the latest upstream. Does this PR work with the latest PR?


$ cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja
-- Initializing NuttX
CMake Error at CMakeLists.txt:315 (message):
  Failed to initialize Kconfig configuration:


-- Configuring incomplete, errors occurred!

masayuki2009 avatar Jul 08 '23 12:07 masayuki2009

@anchao

I noticed that the following error happens with the latest upstream. Does this PR work with the latest PR?

Hi @masayuki2009 , The initial version only supports kconfiglib, you can try the following command to install: pip install kconfiglib I will enhance further documentation/kernel/protect mode support for cmake

anchao avatar Jul 10 '23 01:07 anchao

@anchao

The initial version only supports kconfiglib, you can try the following command to install: pip install kconfiglib

Hmm, CMake error still happens after I installed kconfiglib.

$ cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja
-- Initializing NuttX
Kconfig:258: warning: the 'modules' option is not supported. Let me know if this is a problem for you, as it wouldn't be that hard to implement. Note that modules are supported -- Kconfiglib just assumes the symbol name MODULES, like older versions of the C implementation did when 'option modules' wasn't used.
--   Board:  sabre-6quad
--   Config: smp
--   Appdir: /home/ishikawa/opensource/RTOS/nuttx/nuttx/../apps
-- The C compiler identification is GNU 12.2.1
-- The CXX compiler identification is GNU 12.2.1
-- The ASM compiler identification is GNU
-- Found assembler: /opt/arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc
-- Check for working C compiler: /opt/arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc
-- Check for working C compiler: /opt/arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /opt/arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-g++
-- Check for working CXX compiler: /opt/arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
CMake Error at cmake/nuttx_add_library.cmake:128 (add_library):
  Cannot find source file:

    /home/ishikawa/opensource/RTOS/nuttx/nuttx/sched/clock/clock_getres.c

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
  .hpp .hxx .in .txx
Call Stack (most recent call first):
  sched/CMakeLists.txt:22 (nuttx_add_kernel_library)


CMake Error at cmake/nuttx_add_library.cmake:128 (add_library):
  No SOURCES given to target: sched
Call Stack (most recent call first):
  sched/CMakeLists.txt:22 (nuttx_add_kernel_library)


CMake Generate step failed.  Build files cannot be regenerated correctly.

masayuki2009 avatar Jul 10 '23 01:07 masayuki2009

Hmm, CMake error still happens after I installed kconfiglib.

@masayuki2009

emm... Sorry for bad experience, it seems that some files in the base of our internal code has not been updated, please try PR https://github.com/apache/nuttx/pull/9753 , also I will check further if there are similar issues, a few of configurations will add to CI today

anchao avatar Jul 10 '23 02:07 anchao

@anchao Have you updated https://nuttx.apache.org/docs/latest/quickstart/compiling.html with a cmake how to?

davids5 avatar Jul 15 '23 06:07 davids5

@anchao Have you updated https://nuttx.apache.org/docs/latest/quickstart/compiling.html with a cmake how to?

@davids5 Yes, I will add cmake chapter later

anchao avatar Jul 18 '23 04:07 anchao