Skip to content

Commit

Permalink
llext: move all flags to compiler specific cmake files
Browse files Browse the repository at this point in the history
Minimize the amount of flags that are hardcoded in the llext.cmake
module by moving them to the compiler specific cmake files.

The llext.cmake module will now use the new LLEXT_REMOVE_FLAGS and
LLEXT_APPEND_FLAGS global variables to adjust the set of flags used
to compile the llext code. This will make it easier to add support
for new architectures and compilers.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
  • Loading branch information
pillo79 committed Jan 19, 2024
1 parent faddff3 commit 83ecbda
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
1 change: 1 addition & 0 deletions cmake/bintools/gnu/target_bintools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 2 additions & 0 deletions cmake/compiler/gcc/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions cmake/compiler/gcc/target_arm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
21 changes: 21 additions & 0 deletions cmake/compiler/gcc/target_xtensa.cmake
Original file line number Diff line number Diff line change
@@ -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
)
48 changes: 31 additions & 17 deletions cmake/modules/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
"$<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_OPTIONS>"
)
set(zephyr_filtered_flags
"$<FILTER:${zephyr_flags},EXCLUDE,${llext_remove_flags_regexp}>"
)

# Compile the source file to an object file using current Zephyr settings
# but a different set of flags
Expand All @@ -4861,12 +4870,12 @@ function(add_llext_target target_name)
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
)
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
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
-I${PROJECT_BINARY_DIR}/include/generated
)
target_include_directories(${target_name}_lib SYSTEM PUBLIC
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Expand Down Expand Up @@ -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}
$<TARGET_OBJECTS:${target_name}_lib>
COMMAND ${CROSS_COMPILE}strip -R .xt.*
-o ${output_file}
${pre_output_file}
COMMAND $<TARGET_PROPERTY:bintools,strip_command>
$<TARGET_PROPERTY:bintools,strip_flag>
$<TARGET_PROPERTY:bintools,strip_flag_remove_section>.xt.*
$<TARGET_PROPERTY:bintools,strip_flag_infile>${pre_output_file}
$<TARGET_PROPERTY:bintools,strip_flag_outfile>${output_file}
$<TARGET_PROPERTY:bintools,strip_flag_final>
DEPENDS ${target_name}_lib $<TARGET_OBJECTS:${target_name}_lib>
)

else()
message(FATAL_ERROR "add_llext_target: unsupported architecture")
endif()
endfunction()

0 comments on commit 83ecbda

Please sign in to comment.