diff --git a/cmake/bintools/gnu/target_bintools.cmake b/cmake/bintools/gnu/target_bintools.cmake index e8cb6e181d55af..8aac55b0400cdc 100644 --- a/cmake/bintools/gnu/target_bintools.cmake +++ b/cmake/bintools/gnu/target_bintools.cmake @@ -93,6 +93,7 @@ set_property(TARGET bintools PROPERTY strip_flag_final "") set_property(TARGET bintools PROPERTY strip_flag_all --strip-all) set_property(TARGET bintools PROPERTY strip_flag_debug --strip-debug) set_property(TARGET bintools PROPERTY strip_flag_dwo --strip-dwo) +set_property(TARGET bintools PROPERTY strip_flag_remove_section -R ) set_property(TARGET bintools PROPERTY strip_flag_infile "") set_property(TARGET bintools PROPERTY strip_flag_outfile -o ) diff --git a/cmake/compiler/gcc/target.cmake b/cmake/compiler/gcc/target.cmake index 86807bd8a4f69d..5b1e5db12180a3 100644 --- a/cmake/compiler/gcc/target.cmake +++ b/cmake/compiler/gcc/target.cmake @@ -76,6 +76,8 @@ elseif("${ARCH}" STREQUAL "sparc") include(${CMAKE_CURRENT_LIST_DIR}/target_sparc.cmake) elseif("${ARCH}" STREQUAL "mips") include(${CMAKE_CURRENT_LIST_DIR}/target_mips.cmake) +elseif("${ARCH}" STREQUAL "xtensa") + include(${CMAKE_CURRENT_LIST_DIR}/target_xtensa.cmake) endif() if(SYSROOT_DIR) diff --git a/cmake/compiler/gcc/target_arm.cmake b/cmake/compiler/gcc/target_arm.cmake index a813c2563a3b0b..6659c7bf417018 100644 --- a/cmake/compiler/gcc/target_arm.cmake +++ b/cmake/compiler/gcc/target_arm.cmake @@ -41,3 +41,22 @@ endif() list(APPEND TOOLCHAIN_C_FLAGS ${ARM_C_FLAGS}) list(APPEND TOOLCHAIN_LD_FLAGS NO_SPLIT ${ARM_C_FLAGS}) + +# Flags not supported by llext linker +# (regexps are supported and match whole word) +set(LLEXT_REMOVE_FLAGS + -fno-pic + -fno-pie + -ffunction-sections + -fdata-sections + -g.* + -Os + -mcpu=.* +) + +# Flags to be added to llext code compilation +set(LLEXT_APPEND_FLAGS + -mlong-calls + -mthumb + -mcpu=cortex-m33+nodsp +) diff --git a/cmake/compiler/gcc/target_xtensa.cmake b/cmake/compiler/gcc/target_xtensa.cmake new file mode 100644 index 00000000000000..177830427dbdda --- /dev/null +++ b/cmake/compiler/gcc/target_xtensa.cmake @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Flags not supported by llext linker +# (regexps are supported and match whole word) +set(LLEXT_REMOVE_FLAGS + -fno-pic + -fno-pie + -ffunction-sections + -fdata-sections + -g.* + -Os + -mcpu=.* +) + +# Flags to be added to llext code compilation +set(LLEXT_APPEND_FLAGS + -fPIC + -nostdlib + -nodefaultlibs + -shared +) diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index 0d50385d8ce095..b51ff49d2fd227 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -4801,9 +4801,13 @@ endmacro() # Output and source files must be specified using the OUTPUT and SOURCES # arguments. Only one source file is currently supported. # -# Arch-specific flags will be added automatically. The C_FLAGS argument -# can be used to pass additional compiler flags to the compilation of -# the source file. +# The llext code will be compiled with mostly the same C compiler flags used +# in the Zephyr build, but with some important modifications. The list of +# flags to remove and flags to append is controlled respectively by the +# LLEXT_REMOVE_FLAGS and LLEXT_APPEND_FLAGS global variables. + +# The C_FLAGS argument can be used to pass additional compiler flags to the +# compilation of this particular llext. # # Example usage: # add_llext_target(hello_world @@ -4845,14 +4849,19 @@ function(add_llext_target target_name) DEPENDS ${output_file} ) - # Define arch-specific flags for the supported architectures - if(CONFIG_ARM) - list(PREPEND LLEXT_C_FLAGS "-mlong-calls" "-mthumb") - elseif(CONFIG_XTENSA) - list(PREPEND LLEXT_C_FLAGS "-shared" "-fPIC" "-nostdlib" "-nodefaultlibs") - else() - message(FATAL_ERROR "add_llext_target: unsupported architecture") - endif() + # Convert the LLEXT_REMOVE_FLAGS list to a regular expression, and use it to + # filter out these flags from the Zephyr target settings + list(TRANSFORM LLEXT_REMOVE_FLAGS + REPLACE "(.+)" "^\\1$" + OUTPUT_VARIABLE llext_remove_flags_regexp + ) + string(REPLACE ";" "|" llext_remove_flags_regexp "${llext_remove_flags_regexp}") + set(zephyr_flags + "$" + ) + set(zephyr_filtered_flags + "$" + ) # Compile the source file to an object file using current Zephyr settings # but a different set of flags @@ -4861,12 +4870,12 @@ function(add_llext_target target_name) $ ) target_compile_options(${target_name}_lib PRIVATE + ${zephyr_filtered_flags} + ${LLEXT_APPEND_FLAGS} ${LLEXT_C_FLAGS} - -imacros "${AUTOCONF_H}" ) target_include_directories(${target_name}_lib PRIVATE $ - -I${PROJECT_BINARY_DIR}/include/generated ) target_include_directories(${target_name}_lib SYSTEM PUBLIC $ @@ -4897,14 +4906,19 @@ function(add_llext_target target_name) add_custom_command( OUTPUT ${output_file} BYPRODUCTS ${pre_output_file} - COMMAND ${CMAKE_C_COMPILER} ${LLEXT_C_FLAGS} + COMMAND ${CMAKE_C_COMPILER} ${LLEXT_APPEND_FLAGS} -o ${pre_output_file} $ - COMMAND ${CROSS_COMPILE}strip -R .xt.* - -o ${output_file} - ${pre_output_file} + COMMAND $ + $ + $.xt.* + $${pre_output_file} + $${output_file} + $ DEPENDS ${target_name}_lib $ ) + else() + message(FATAL_ERROR "add_llext_target: unsupported architecture") endif() endfunction()