cmake(feat):implements KERNEL mode in CMake build

1.add application link dependencies for all elf
2.add a global custom target to hold proptry in the toolchain file
3.add startup obj target
4.fix cpp lds error with kernel mod link elf lds

usage:
./build.sh qemu-armv7a:knsh --cmake
elf install in ${CMAKE_BINARY_DIR}/bin

Signed-off-by: xuxin19 <xuxin19@xiaomi.com>
This commit is contained in:
xuxin19
2025-02-18 21:23:27 +08:00
committed by GUIDINGLI
parent a2d2a49ba9
commit b7582b8da1
8 changed files with 66 additions and 15 deletions

View File

@@ -378,6 +378,12 @@ if(NOT EXISTS ${CMAKE_BINARY_DIR}/.config OR NOT "${NUTTX_DEFCONFIG}" STREQUAL
message(STATUS " Appdir: ${NUTTX_APPS_DIR}")
endif()
# declare global custom targets at very beginning `nuttx_global` is used to hold
# global target properties to solve that cmake GLOBAL property can not use
# generator expression this is needed to make sure added before project() create
# before Toolchain file added before all cmake modules are loaded
add_custom_target(nuttx_global)
# Include .cmake files #######################################################
# this exposes all Kconfig vars to CMake
@@ -598,15 +604,11 @@ endif()
# Add apps/ to the build (if present)
if(NOT CONFIG_BUILD_KERNEL)
if(EXISTS ${NUTTX_APPS_DIR}/CMakeLists.txt)
add_subdirectory(${NUTTX_APPS_DIR} apps)
else()
message(
STATUS "Application directory not found at ${NUTTX_APPS_DIR}, skipping")
endif()
if(EXISTS ${NUTTX_APPS_DIR}/CMakeLists.txt)
add_subdirectory(${NUTTX_APPS_DIR} apps)
else()
message(
STATUS "Application directory not found at ${NUTTX_APPS_DIR}, skipping")
endif()
# after we traverse all build directories unify all target dependencies and all

View File

@@ -92,3 +92,5 @@ if(CONFIG_ARCH_HAVE_FETCHADD)
endif()
target_sources(arch PRIVATE ${SRCS})
nuttx_add_aux_library(STARTUP_OBJS crt0.c)

View File

@@ -23,7 +23,7 @@
set(SRCS qemu_boot.c qemu_serial.c qemu_irq.c qemu_timer.c)
if(CONFIG_ARCH_ARMV7A)
list(APPEND SRCS qemu_memorymap.c)
list(APPEND SRCS qemu_pgalloc.c qemu_memorymap.c)
endif()
if(CONFIG_SMP)

View File

@@ -112,14 +112,18 @@ function(nuttx_add_application)
# non-elf output
if(NOT CMAKE_C_ELF_COMPILER)
add_library(${TARGET} ${SRCS})
add_dependencies(${TARGET} nuttx_post)
add_custom_command(
TARGET ${TARGET}
POST_BUILD
COMMAND
${CMAKE_C_COMPILER}
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx,NUTTX_ELF_APP_LINK_OPTIONS>>
$<TARGET_FILE:${TARGET}> -o ${TARGET}
${CMAKE_LD}
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_APP_LINK_OPTIONS>>
-e __start -Bstatic -T ${CMAKE_BINARY_DIR}/gnu-elf.ld
$<TARGET_OBJECTS:STARTUP_OBJS> --start-group
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_LINK_LIBRARIES>>
$<TARGET_FILE:${TARGET}> --end-group -o
${CMAKE_BINARY_DIR}/bin/${TARGET}
COMMAND_EXPAND_LISTS)
else()
add_executable(${TARGET} ${SRCS})

View File

@@ -118,6 +118,13 @@ function(nuttx_add_system_library target)
# add to list of libraries to link to final nuttx binary
set_property(GLOBAL APPEND PROPERTY NUTTX_SYSTEM_LIBRARIES ${target})
set_property(
TARGET nuttx_global
APPEND
PROPERTY NUTTX_ELF_LINK_LIBRARIES $<TARGET_FILE:${target}>)
add_dependencies(nuttx_post ${target})
endfunction()
# Kernel Libraries
@@ -196,6 +203,14 @@ function(nuttx_add_library target)
add_library(${target} ${ARGN})
add_dependencies(${target} apps_context)
set_property(GLOBAL APPEND PROPERTY NUTTX_SYSTEM_LIBRARIES ${target})
set_property(
TARGET nuttx_global
APPEND
PROPERTY NUTTX_ELF_LINK_LIBRARIES $<TARGET_FILE:${target}>)
add_dependencies(nuttx_post ${target})
# Set apps global compile options & definitions hold by nuttx_apps_interface
target_compile_options(
${target}
@@ -236,6 +251,12 @@ function(nuttx_add_extra_library)
set_property(GLOBAL APPEND PROPERTY NUTTX_USER_EXTRA_LIBRARIES
${extra_target})
endif()
if(CONFIG_BUILD_KERNEL)
set_property(
TARGET nuttx_global
APPEND
PROPERTY NUTTX_ELF_LINK_LIBRARIES $<TARGET_FILE:${extra_target}>)
endif()
endif()
endforeach()
endfunction()
@@ -259,6 +280,11 @@ function(nuttx_add_external_library target)
cmake_parse_arguments(ARGS "" MODE "" ${ARGN})
if(NOT ARGS_MODE)
set_property(GLOBAL APPEND PROPERTY NUTTX_SYSTEM_LIBRARIES ${target})
set_property(
TARGET nuttx_global
APPEND
PROPERTY NUTTX_ELF_LINK_LIBRARIES $<TARGET_FILE:${target}>)
add_dependencies(nuttx_post ${target})
elseif("${ARGS_MODE}" STREQUAL "APPS")
set_property(GLOBAL APPEND PROPERTY NUTTX_APPS_LIBRARIES ${target})
elseif("${ARGS_MODE}" STREQUAL "KERNEL")

View File

@@ -281,6 +281,14 @@ function(nuttx_compile_definitions_for_all_apps)
PROPERTY APPS_COMPILE_DEFINITIONS ${ARGN})
endfunction()
# all elf link options
function(nuttx_add_elf_link_options)
set_property(
TARGET nuttx_global
APPEND
PROPERTY NUTTX_ELF_APP_LINK_OPTIONS ${ARGN})
endfunction()
# since we dont call `target_link_libraries` directly, we only inherit their
# compilation configuration
function(nuttx_link_libraries)

View File

@@ -91,7 +91,7 @@ if(NOT NUTTX_TOOLCHAIN_PREPROCESS_DEFINED)
add_custom_command(
OUTPUT ${TARGET_FILE}
COMMAND ${PREPROCESS} -I${CMAKE_BINARY_DIR}/include
COMMAND ${PREPROCESS} -I${CMAKE_BINARY_DIR}/include -I${NUTTX_DIR}/include
-I${NUTTX_CHIP_ABS_DIR} ${SOURCE_FILE} > ${TARGET_FILE}
DEPENDS ${SOURCE_FILE} ${DEPENDS})

View File

@@ -49,4 +49,13 @@ if(CONFIG_LIBC_ELF)
target_sources(c PRIVATE ${SRCS})
set(ELF_LD_SCRIPT_TMP "${CMAKE_BINARY_DIR}/gnu-elf.ld")
nuttx_generate_preprocess_target(
SOURCE_FILE ${NUTTX_DIR}/libs/libc/elf/gnu-elf.ld.in TARGET_FILE
${ELF_LD_SCRIPT_TMP})
add_custom_target(elfldscript_tmp DEPENDS ${ELF_LD_SCRIPT_TMP})
add_dependencies(nuttx elfldscript_tmp)
endif()