Static code analyze tools for NRF Connect SDK

Hello,

I'm doing reasearch in static code analyze tools for my projects. At link below I saw that since version ncs 2.1.0 there is possible to use "Sparse" tool to static code analyze but some configs are required... I can't find what kind of configs...

 nrf Connect 1.6.1 SDK : Static Code Analysis 

 I also saw this:

https://docs.zephyrproject.org/3.1.0/develop/test/sparse.html

I've tried to build some example with "-- -DSPARSE=y".

west build -p always -b nrf52dk_nrf52832 samples/basic/blinky -- -DSPARSE=y

As a result I've got...

es2@es2-hpprobook450g1:~/ncs/zephyr$ west build -p always -b nrf52dk_nrf52832 samples/basic/blinky -- -DSPARSE=y
-- west build: making build dir /home/es2/ncs/zephyr/build pristine
-- west build: generating a build system                                                                                                                                                      
Loading Zephyr default modules (Zephyr base).                                                                                                                                                 
-- Application: /home/es2/ncs/zephyr/samples/basic/blinky
-- Found Python3: /usr/bin/python3.10 (found suitable exact version "3.10.6") found components: Interpreter
-- Cache files will be written to: /home/es2/.cache//zephyr
-- Zephyr version: 3.1.99 (/home/es2/ncs/zephyr)
-- Found west (found suitable version "0.14.0", minimum required is "0.7.1")
-- Board: nrf52dk_nrf52832
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.14.1 (/home/es2/zephyr-sdk-0.14.1)
-- Found dtc: /home/es2/zephyr-sdk-0.14.1/sysroots/x86_64-pokysdk-linux/usr/bin/dtc (found suitable version "1.6.0", minimum required is "1.4.6")
-- Found toolchain: zephyr 0.14.1 (/home/es2/zephyr-sdk-0.14.1)
-- Found BOARD.dts: /home/es2/ncs/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832.dts
-- Generated zephyr.dts: /home/es2/ncs/zephyr/build/zephyr/zephyr.dts
-- Generated devicetree_unfixed.h: /home/es2/ncs/zephyr/build/zephyr/include/generated/devicetree_unfixed.h
-- Generated device_extern.h: /home/es2/ncs/zephyr/build/zephyr/include/generated/device_extern.h
-- Including generated dts.cmake file: /home/es2/ncs/zephyr/build/zephyr/dts.cmake
Parsing /home/es2/ncs/zephyr/Kconfig
Loaded configuration '/home/es2/ncs/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832_defconfig'
Merged configuration '/home/es2/ncs/zephyr/samples/basic/blinky/prj.conf'
Configuration saved to '/home/es2/ncs/zephyr/build/zephyr/.config'
Kconfig header saved to '/home/es2/ncs/zephyr/build/zephyr/include/generated/autoconf.h'
CMake Error at /home/es2/ncs/zephyr/cmake/compiler/gcc/target.cmake:17 (message):
  C compiler                                                                                                                                                                                  
  /home/es2/zephyr-sdk-0.14.1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc not                                                                                                                     
  found - Please check your toolchain installation                                                                                                                                            
Call Stack (most recent call first):                                                                                                                                                          
  /home/es2/ncs/zephyr/cmake/modules/target_toolchain.cmake:63 (include)                                                                                                                      
  /home/es2/ncs/zephyr/cmake/modules/zephyr_default.cmake:121 (include)                                                                                                                       
  /home/es2/ncs/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include)                                                                                                             
  /home/es2/ncs/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)                                                                                                 
  CMakeLists.txt:4 (find_package)                                                                                                                                                             


-- Configuring incomplete, errors occurred!
FATAL ERROR: command exited with status 1: /usr/bin/cmake -DWEST_PYTHON=/usr/bin/python3 -B/home/es2/ncs/zephyr/build -GNinja -DBOARD=nrf52dk_nrf52832 -DSPARSE=y -S/home/es2/ncs/zephyr/samples/basic/blinky

If this is still in progress or something, what kind of tool You recommend for static code analyzing...?

Best regards,

Kamil

Parents
  • The case is quite urgent, can I ask about some answer...?

  •   , please "cherry-pick" the commit linked to below as it was not included in SDK 2.1.0. This should fix the "toolchain not found" error you encountered. 

    https://github.com/nrfconnect/sdk-zephyr/commit/3ebb18b882634be29955a52ea70cd59f634492c1 

      , I was not able to reproduce this here. Below is from my terminal when I tried to build the blinky sample with sparse. Could you try to repeat these steps on your end to see if you get the same result? Notice I had to make a symlink for cgcc as cgcc is not included in the ARM toolchain.

    user@user-OptiPlex-7070:~/ncs/v2.2.0/zephyr/samples/basic/blinky$ ln -s /home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc /home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/cgcc
    user@user-OptiPlex-7070:~/ncs/v2.2.0/zephyr/samples/basic/blinky$ ls -la /home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/cgcc
    lrwxrwxrwx 1 user user 90 des.  14 14:02 /home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/cgcc -> /home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
    user@user-OptiPlex-7070:~/ncs/v2.2.0/zephyr/samples/basic/blinky$ export $REAL_CC=/home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
    bash: export: `=/home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc': not a valid identifier
    user@user-OptiPlex-7070:~/ncs/v2.2.0/zephyr/samples/basic/blinky$ export REAL_CC=/home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
    user@user-OptiPlex-7070:~/ncs/v2.2.0/zephyr/samples/basic/blinky$ west build -b nrf52dk_nrf52832 -p -- -DSPARSE=y
    -- west build: making build dir /home/user/ncs/v2.2.0/zephyr/samples/basic/blinky/build pristine
    -- west build: generating a build system
    Loading Zephyr default modules (Zephyr base).
    -- Application: /home/user/ncs/v2.2.0/zephyr/samples/basic/blinky
    -- Using NCS Toolchain 2.2.0 for building. (/home/user/ncs/toolchains/v2.2.0/cmake)
    -- Found Python3: /home/user/ncs/toolchains/v2.2.0/usr/local/bin/python3 (found suitable exact version "3.8.2") found components: Interpreter 
    -- Cache files will be written to: /home/user/.cache/zephyr
    -- Zephyr version: 3.2.99 (/home/user/ncs/v2.2.0/zephyr)
    -- Found west (found suitable version "0.14.0", minimum required is "0.7.1")
    -- Board: nrf52dk_nrf52832
    -- Found host-tools: zephyr 0.15.1 (/home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk)
    -- Found toolchain: zephyr 0.15.1 (/home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk)
    -- Found Dtc: /home/user/ncs/toolchains/v2.2.0/usr/local/bin/dtc (found suitable version "1.4.7", minimum required is "1.4.6") 
    -- Found BOARD.dts: /home/user/ncs/v2.2.0/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832.dts
    -- Generated zephyr.dts: /home/user/ncs/v2.2.0/zephyr/samples/basic/blinky/build/zephyr/zephyr.dts
    -- Generated devicetree_generated.h: /home/user/ncs/v2.2.0/zephyr/samples/basic/blinky/build/zephyr/include/generated/devicetree_generated.h
    -- Including generated dts.cmake file: /home/user/ncs/v2.2.0/zephyr/samples/basic/blinky/build/zephyr/dts.cmake
    Parsing /home/user/ncs/v2.2.0/zephyr/Kconfig
    Loaded configuration '/home/user/ncs/v2.2.0/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832_defconfig'
    Merged configuration '/home/user/ncs/v2.2.0/zephyr/samples/basic/blinky/prj.conf'
    Configuration saved to '/home/user/ncs/v2.2.0/zephyr/samples/basic/blinky/build/zephyr/.config'
    Kconfig header saved to '/home/user/ncs/v2.2.0/zephyr/samples/basic/blinky/build/zephyr/include/generated/autoconf.h'
    -- Found sparse: /home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/cgcc
    -- The C compiler identification is GNU 12.1.0
    -- The CXX compiler identification is GNU 12.1.0
    -- The ASM compiler identification is GNU
    -- Found assembler: /home/user/ncs/toolchains/v2.2.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/cgcc
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/user/ncs/v2.2.0/zephyr/samples/basic/blinky/build
    -- west build: building application
    [1/166] Preparing syscall dependency handling
    
    [2/166] Generating include/generated/version.h
    -- Zephyr version: 3.2.99 (/home/user/ncs/v2.2.0/zephyr), build: v3.2.99-ncs1
    [21/166] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/printk.c.obj
    /home/user/ncs/v2.2.0/zephyr/lib/os/printk.c: In function 'vsnprintk':
    /home/user/ncs/v2.2.0/zephyr/lib/os/printk.c:255:19: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
      255 |         cbvprintf(str_out, &ctx, fmt, ap);
          |                   ^~~~~~~
          |                   |
          |                   int (*)(int,  struct str_context *)
    In file included from ../../../../include/zephyr/logging/log_msg.h:11,
                     from ../../../../include/zephyr/logging/log_core.h:9,
                     from ../../../../include/zephyr/logging/log.h:11,
                     from /home/user/ncs/v2.2.0/zephyr/lib/os/printk.c:21:
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  struct str_context *)'
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    [99/166] Building C object zephyr/lib/libc/minimal/CMakeFiles/lib__libc__minimal.dir/source/stdout/sprintf.c.obj
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c: In function 'snprintf':
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c:45:23: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
       45 |         r = cbvprintf(sprintf_out, (void *) (&p), format, vargs);
          |                       ^~~~~~~~~~~
          |                       |
          |                       int (*)(int,  struct emitter *)
    In file included from /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c:11:
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  struct emitter *)'
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c: In function 'sprintf':
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c:63:23: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
       63 |         r = cbvprintf(sprintf_out, (void *) (&p), format, vargs);
          |                       ^~~~~~~~~~~
          |                       |
          |                       int (*)(int,  struct emitter *)
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  struct emitter *)'
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c: In function 'vsnprintf':
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c:84:23: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
       84 |         r = cbvprintf(sprintf_out, (void *) (&p), format, vargs);
          |                       ^~~~~~~~~~~
          |                       |
          |                       int (*)(int,  struct emitter *)
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  struct emitter *)'
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c: In function 'vsprintf':
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/sprintf.c:99:23: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
       99 |         r = cbvprintf(sprintf_out, (void *) (&p), format, vargs);
          |                       ^~~~~~~~~~~
          |                       |
          |                       int (*)(int,  struct emitter *)
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  struct emitter *)'
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    [101/166] Building C object zephyr/lib/libc/minimal/CMakeFiles/lib__libc__minimal.dir/source/stdout/fprintf.c.obj
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c: In function 'fprintf':
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c:21:23: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
       21 |         r = cbvprintf(fputc, DESC(stream), format, vargs);
          |                       ^~~~~
          |                       |
          |                       int (*)(int,  FILE *) {aka int (*)(int,  int *)}
    In file included from /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c:11:
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  FILE *)' {aka 'int (*)(int,  int *)'}
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c: In function 'vfprintf':
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c:32:23: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
       32 |         r = cbvprintf(fputc, DESC(stream), format, vargs);
          |                       ^~~~~
          |                       |
          |                       int (*)(int,  FILE *) {aka int (*)(int,  int *)}
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  FILE *)' {aka 'int (*)(int,  int *)'}
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c: In function 'printf':
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c:43:23: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
       43 |         r = cbvprintf(fputc, DESC(stdout), format, vargs);
          |                       ^~~~~
          |                       |
          |                       int (*)(int,  FILE *) {aka int (*)(int,  int *)}
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  FILE *)' {aka 'int (*)(int,  int *)'}
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c: In function 'vprintf':
    /home/user/ncs/v2.2.0/zephyr/lib/libc/minimal/source/stdout/fprintf.c:53:23: warning: passing argument 1 of 'cbvprintf' from incompatible pointer type [-Wincompatible-pointer-types]
       53 |         r = cbvprintf(fputc, DESC(stdout), format, vargs);
          |                       ^~~~~
          |                       |
          |                       int (*)(int,  FILE *) {aka int (*)(int,  int *)}
    ../../../../include/zephyr/sys/cbprintf.h:715:27: note: expected 'cbprintf_cb' {aka 'int (*)(int,  void *)'} but argument is of type 'int (*)(int,  FILE *)' {aka 'int (*)(int,  int *)'}
      715 | int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
          |               ~~~~~~~~~~~~^~~
    [156/166] Linking C executable zephyr/zephyr_pre0.elf
    
    [160/166] Linking C executable zephyr/zephyr_pre1.elf
    
    [166/166] Linking C executable zephyr/zephyr.elf
    Memory region         Used Size  Region Size  %age Used
               FLASH:       19390 B       512 KB      3.70%
                 RAM:        5504 B        64 KB      8.40%
            IDT_LIST:          0 GB         2 KB      0.00%

  • As the NCS toolchain worked for me and the others in this thread, I thought it was worth a try. It can be installed via the terminal too (with nRF Util). Anyway, if we take a step back and look at the actual error message, we can see that the build is failing because CMake is unable to locate the "cgcc" executable: 

    CMake Error at /home/ncs/ncs/zephyr/cmake/compiler/gcc/target.cmake:10 (find_program):
    Could not find CMAKE_C_COMPILER using the following names: cgcc
    Call Stack (most recent call first):
    /home/ncs/ncs/zephyr/cmake/modules/FindTargetTools.cmake:102 (include)

    Have you tried to add "/home/ncs/zephyr-sdk-0.15.1/arm-zephyr-eabi/bin/" to your PATH?

    $ export PATH=/home/ncs/zephyr-sdk-0.15.1/arm-zephyr-eabi/bin/:"$PATH"

  • Uff... Works!  Vidar Berg (vibe) thanks a lot...

    Next few questions... 

    * Is it possible to set build directory when we build with vs code extension?

    * Is it possible to include SPARSE when we build with vs code extension?

  • trafficode said:
    Is it possible to set build directory when we build with vs code extension?

    Can you explain what you want to achieve? Is it to give it a different name, or do you want it placed in another directory? 

    trafficode said:
    * Is it possible to include SPARSE when we build with vs code extension?

    Yes, you can add -DSPARSE=y to the CMake argument list in your build configuration: https://nrfconnect.github.io/vscode-nrf-connect/guides/build_config.html 

  • Can you explain what you want to achieve? Is it to give it a different name, or do you want it placed in another directory?

    I'm using separate directory for build output. This directory is placed in RAM memory to make my ssd live longer.

    One more question... Is it possible to automatically run "Reset board" command after "Flash" operation... I can see that reset is done using reset pin, but I'm using reset pin as gpio.

  • It is currently not possible to change the location of the build folder in vs code, unfortunately.

    trafficode said:
    One more question... Is it possible to automatically run "Reset board" command after "Flash" operation... I can see that reset is done using reset pin, but I'm using reset pin as gpio.

    Yes, you can add the --softreset option to your board file as described here:  Enabling pin reset. Is still active even with CONFIG_GPIO_AS_PINRESET=n . This will change the default reset type from pinreset to softreset. 

Reply Children
Related