west build cmake option

I added a cmake configuration option to CMakeLists.txt, but sending a value through west build does not set the cmake option. Here is what I have done:

CMakeLists.txt

make_minimum_required(VERSION 3.20.0)

# Option to choose keyboard side (left or right)
option(KEYBOARD_RIGHT "Build for right keyboard half" OFF)

if(KEYBOARD_RIGHT)
    message(STATUS "Enabling right overlay")
    set(DTC_OVERLAY_FILE app_right.overlay)
else()
    message(STATUS "Enabling left overlay")
    set(DTC_OVERLAY_FILE app_left.overlay)
endif()

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

project(leaf_fall_keyboard)

if(KEYBOARD_RIGHT)
    message(STATUS "Enabling right compile option")
    target_compile_definitions(app PRIVATE KEYBOARD_RIGHT)
else()
    message(STATUS "Enabling left compile option")
    target_compile_definitions(app PRIVATE KEYBOARD_LEFT)
endif()

include_directories(config)

target_sources(app PRIVATE src/main.c)

The build command:

west build --pristine --build-dir $HOME/src/leaf-fall/firmware/keyboard/build --board nrf52_adafruit_feather/nrf52832 -- -DKEYBOARD_RIGHT=ON

The result shows that KEYBOARD_RIGHT is getting ignored by both sysbuild and the application build:

-- west build: making build dir /home/mwu/src/leaf-fall/firmware/keyboard/build pristine
-- west build: generating a build system
Loading Zephyr module(s) (Zephyr base): sysbuild_default
-- Found Python3: /home/mwu/ncs/toolchains/2ac5840438/usr/local/bin/python3.12 (found suitable version "3.12.4", minimum required is "3.10") found components: Interpreter
-- Cache files will be written to: /home/mwu/.cache/zephyr
-- Found west (found suitable version "1.4.0", minimum required is "0.14.0")
-- Board: nrf52_adafruit_feather, qualifiers: nrf52832
Parsing /home/mwu/ncs/v3.2.4/zephyr/share/sysbuild/Kconfig
Loaded configuration '/home/mwu/src/leaf-fall/firmware/keyboard/build/_sysbuild/empty.conf'
Merged configuration '/home/mwu/src/leaf-fall/firmware/keyboard/build/_sysbuild/empty.conf'
Configuration saved to '/home/mwu/src/leaf-fall/firmware/keyboard/build/zephyr/.config'
Kconfig header saved to '/home/mwu/src/leaf-fall/firmware/keyboard/build/_sysbuild/autoconf.h'
--
   ******************************
   * Running CMake for keyboard *
   ******************************

-- Enabling left overlay
Loading Zephyr default modules (Zephyr base).
-- Application: /home/mwu/src/leaf-fall/firmware/keyboard
-- CMake version: 3.21.0
-- Found Python3: /home/mwu/ncs/toolchains/2ac5840438/usr/local/bin/python (found suitable version "3.12.4", minimum required is "3.10") found components: Interpreter
-- Cache files will be written to: /home/mwu/.cache/zephyr
-- Zephyr version: 4.2.99 (/home/mwu/ncs/v3.2.4/zephyr)
-- Found west (found suitable version "1.4.0", minimum required is "0.14.0")
-- Board: nrf52_adafruit_feather, qualifiers: nrf52832
-- Found host-tools: zephyr 0.17.0 (/home/mwu/ncs/toolchains/2ac5840438/opt/zephyr-sdk)
-- Found toolchain: zephyr 0.17.0 (/home/mwu/ncs/toolchains/2ac5840438/opt/zephyr-sdk)
-- Found Dtc: /home/mwu/ncs/toolchains/2ac5840438/usr/local/bin/dtc (found suitable version "1.5.0", minimum required is "1.4.6")
-- Found BOARD.dts: /home/mwu/ncs/v3.2.4/zephyr/boards/adafruit/nrf52_adafruit_feather/nrf52_adafruit_feather.dts
-- Found devicetree overlay: app_left.overlay
-- Generated zephyr.dts: /home/mwu/src/leaf-fall/firmware/keyboard/build/keyboard/zephyr/zephyr.dts
-- Generated pickled edt: /home/mwu/src/leaf-fall/firmware/keyboard/build/keyboard/zephyr/edt.pickle
-- Generated devicetree_generated.h: /home/mwu/src/leaf-fall/firmware/keyboard/build/keyboard/zephyr/include/generated/zephyr/devicetree_generated.h
Parsing /home/mwu/ncs/v3.2.4/zephyr/Kconfig
Loaded configuration '/home/mwu/ncs/v3.2.4/zephyr/boards/adafruit/nrf52_adafruit_feather/nrf52_adafruit_feather_defconfig'
Merged configuration '/home/mwu/src/leaf-fall/firmware/keyboard/prj.conf'
Merged configuration '/home/mwu/src/leaf-fall/firmware/keyboard/build/keyboard/zephyr/.config.sysbuild'
Configuration saved to '/home/mwu/src/leaf-fall/firmware/keyboard/build/keyboard/zephyr/.config'
Kconfig header saved to '/home/mwu/src/leaf-fall/firmware/keyboard/build/keyboard/zephyr/include/generated/zephyr/autoconf.h'
-- Found GnuLd: /home/mwu/ncs/toolchains/2ac5840438/opt/zephyr-sdk/arm-zephyr-eabi/arm-zephyr-eabi/bin/ld.bfd (found version "2.38")
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/mwu/ncs/toolchains/2ac5840438/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Found gen_kobject_list: /home/mwu/ncs/v3.2.4/zephyr/scripts/build/gen_kobject_list.py
-- Enabling left compile option
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mwu/src/leaf-fall/firmware/keyboard/build/keyboard
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mwu/src/leaf-fall/firmware/keyboard/build
-- west build: building application
[5/10] Performing build step for 'keyboard'
[1/162] Preparing syscall dependency handling

[6/162] Generating include/generated/zephyr/version.h
-- Zephyr version: 4.2.99 (/home/mwu/ncs/v3.2.4/zephyr), build: ncs-v3.2.4
[162/162] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       39256 B       512 KB      7.49%
             RAM:        7080 B        64 KB     10.80%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/mwu/src/leaf-fall/firmware/keyboard/build/keyboard/zephyr/zephyr.elf for board: nrf52_adafruit_feather
[10/10] Generating ../merged.hex

My code is open source and can be found at https://github.com/marcuswu/leaf-fall/tree/main/firmware/keyboard

Related