From 741de4b45055a7faec0d5d676f9f40a45bd1d61b Mon Sep 17 00:00:00 2001 From: xuxin19 Date: Wed, 27 Mar 2024 17:04:03 +0800 Subject: [PATCH] cmake:init protected-mode for CMake build adjust link options for userspace elf specify system libs and apps lib to only link with nuttx target in flat build mode Signed-off-by: xuxin19 --- CMakeLists.txt | 28 +++++++++++++------ arch/arm/src/common/CMakeLists.txt | 3 +- arch/risc-v/src/common/CMakeLists.txt | 2 +- boards/arm/tiva/lm3s6965-ek/CMakeLists.txt | 2 +- .../tiva/lm3s6965-ek/kernel/CMakeLists.txt | 21 ++++++++++++++ 5 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 boards/arm/tiva/lm3s6965-ek/kernel/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 69f856617a..5b2c5d4b22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -416,6 +416,10 @@ include(platform) # Setup main nuttx target #################################################### add_executable(nuttx) +if(CONFIG_BUILD_PROTECTED) + add_executable(nuttx_user) +endif() + add_dependencies(nuttx nuttx_context) if(WIN32) @@ -604,9 +608,6 @@ get_property(nuttx_extra_libs GLOBAL PROPERTY NUTTX_EXTRA_LIBRARIES) if(CONFIG_BUILD_FLAT) get_property(nuttx_system_libs GLOBAL PROPERTY NUTTX_SYSTEM_LIBRARIES) -endif() - -if(NOT CONFIG_BUILD_KERNEL) get_property(nuttx_apps_libs GLOBAL PROPERTY NUTTX_APPS_LIBRARIES) endif() @@ -742,25 +743,36 @@ endif() # Userspace portion ########################################################## if(CONFIG_BUILD_PROTECTED) - add_executable(nuttx_user) get_property(nuttx_system_libs GLOBAL PROPERTY NUTTX_SYSTEM_LIBRARIES) + get_property(nuttx_apps_libs GLOBAL PROPERTY NUTTX_APPS_LIBRARIES) + get_property(user_ldscript GLOBAL PROPERTY LD_SCRIPT_USER) list(TRANSFORM user_ldscript PREPEND "-Wl,--script=") + execute_process( + COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${NUTTX_EXTRA_FLAGS} + --print-libgcc-file-name + OUTPUT_STRIP_TRAILING_WHITESPACE + OUTPUT_VARIABLE nuttx_user_libgcc) + + # reset link options for userspace to prevent sections from being accidentally + # deleted + set_target_properties(nuttx_user PROPERTIES LINK_OPTIONS "") + target_link_options( nuttx_user PRIVATE -nostartfiles -nodefaultlibs - -Wl,--entry=${CONFIG_USER_ENTRYPOINT} - -Wl,--undefined=${CONFIG_USER_ENTRYPOINT}) + -Wl,--entry=${CONFIG_INIT_ENTRYPOINT} + -Wl,--undefined=${CONFIG_INIT_ENTRYPOINT}) target_link_libraries( nuttx_user PRIVATE ${user_ldscript} - userspace $<$>:-Wl,--start-group> ${nuttx_system_libs} - gcc + ${nuttx_apps_libs} + ${nuttx_user_libgcc} $<$:supc++> $<$>:-Wl,--end-group>) diff --git a/arch/arm/src/common/CMakeLists.txt b/arch/arm/src/common/CMakeLists.txt index ac630ec46b..5776283ea2 100644 --- a/arch/arm/src/common/CMakeLists.txt +++ b/arch/arm/src/common/CMakeLists.txt @@ -59,7 +59,8 @@ if(CONFIG_BUILD_PROTECTED OR CONFIG_BUILD_KERNEL) list(APPEND SRCS arm_task_start.c arm_pthread_start.c arm_signal_dispatch.c) if(CONFIG_BUILD_PROTECTED) - list(APPEND SRCS ${ARCH_TOOLCHAIN_PATH}/arm_signal_handler.S) + target_sources(arch_interface + PRIVATE ${ARCH_TOOLCHAIN_PATH}/arm_signal_handler.S) endif() endif() diff --git a/arch/risc-v/src/common/CMakeLists.txt b/arch/risc-v/src/common/CMakeLists.txt index a240435682..164d828bd6 100644 --- a/arch/risc-v/src/common/CMakeLists.txt +++ b/arch/risc-v/src/common/CMakeLists.txt @@ -51,7 +51,7 @@ endif() if(NOT CONFIG_BUILD_FLAT) list(APPEND SRCS riscv_task_start.c riscv_pthread_start.c riscv_signal_dispatch.c) - list(APPEND SRCS riscv_signal_handler.S) + target_sources(arch_interface PRIVATE riscv_signal_handler.S) endif() if(CONFIG_SCHED_BACKTRACE) diff --git a/boards/arm/tiva/lm3s6965-ek/CMakeLists.txt b/boards/arm/tiva/lm3s6965-ek/CMakeLists.txt index d2081c5c05..43e258b869 100644 --- a/boards/arm/tiva/lm3s6965-ek/CMakeLists.txt +++ b/boards/arm/tiva/lm3s6965-ek/CMakeLists.txt @@ -20,7 +20,7 @@ add_subdirectory(src) -if(NOT CONFIG_BUILD_FLAT) +if(CONFIG_BUILD_PROTECTED) add_subdirectory(kernel) set_property( GLOBAL PROPERTY LD_SCRIPT_USER ${CMAKE_CURRENT_LIST_DIR}/scripts/memory.ld diff --git a/boards/arm/tiva/lm3s6965-ek/kernel/CMakeLists.txt b/boards/arm/tiva/lm3s6965-ek/kernel/CMakeLists.txt new file mode 100644 index 0000000000..fa755dfe96 --- /dev/null +++ b/boards/arm/tiva/lm3s6965-ek/kernel/CMakeLists.txt @@ -0,0 +1,21 @@ +# ############################################################################## +# boards/arm/tiva/lm3s6965-ek/kernel/CMakeLists.txt +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +target_sources(nuttx_user PRIVATE lm_userspace.c)