Skip to content

Fix up native_sim for #89073 #90353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 18 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,52 +196,22 @@ elseif(CONFIG_STACK_CANARIES_EXPLICIT)
zephyr_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler,security_canaries_explicit>>")
endif()

# @Intent: Obtain compiler optimizations flags and store in variables
# @details:
# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
# only *one* of CONFIG_{NO,DEBUG,SPEED,SIZE}_OPTIMIZATIONS is set.
# Refer to Kconfig.zephyr for selection logic and description of these choices.
# toolchain_cc_optimize_*() macros must provide the mapping from these kconfigs
# to compiler flags. Each macro will store the flags in a CMake variable, whose
# name is passed as argument (somewhat like by reference).
#
# If the user wants to tweak the optimizations, there are two ways:
# 1) Using EXTRA_CFLAGS which is applied regardless of kconfig choice, or
# 2) Rely on override support being implemented by your toolchain_cc_optimize_*()
#
get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_optimization)
get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size)
get_property(OPTIMIZE_FOR_SIZE_AGGRESSIVE_FLAG TARGET compiler PROPERTY optimization_size_aggressive)

# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
if(CONFIG_NO_OPTIMIZATIONS)
set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
elseif(CONFIG_DEBUG_OPTIMIZATIONS)
set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
elseif(CONFIG_SPEED_OPTIMIZATIONS)
set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
elseif(CONFIG_SIZE_OPTIMIZATIONS)
set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
elseif(CONFIG_SIZE_OPTIMIZATIONS_AGGRESSIVE)
set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_AGGRESSIVE_FLAG})
else()
message(FATAL_ERROR
"Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")
endif()

if(NOT CONFIG_ARCH_IS_SET)
message(WARNING "\
None of the CONFIG_<arch> (e.g. CONFIG_X86) symbols are set. \
Select one of them from the SOC_SERIES_* symbol or, lacking that, from the \
SOC_* symbol.")
endif()

# Apply the final optimization flag(s)
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:${OPTIMIZATION_FLAG}>)
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${OPTIMIZATION_FLAG}>)
# Apply the default optimization flag(s)
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,optimization>>)
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler,optimization>>)

# Genex to fetch linker property defining the target providing optimization settings for linking.
set(opt_target_genex $<TARGET_PROPERTY:linker,optimization_target>)
# Genex to use optimization properties from defined target with fallback to linker target.
set(opt_target_select $<IF:$<BOOL:${opt_target_genex}>,${opt_target_genex},linker>)
add_link_options($<TARGET_PROPERTY:${opt_target_select},optimization>)

if(CONFIG_LTO)
zephyr_compile_options($<TARGET_PROPERTY:compiler,optimization_lto>)
Expand Down Expand Up @@ -2308,8 +2278,15 @@ endif()
# Done at the very end, so any other system includes which may
# be added by Zephyr components were first in list.
# Note, the compile flags are moved, but the system include is still present here.
zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
target_include_directories(zephyr_interface SYSTEM INTERFACE $<TARGET_PROPERTY:compiler,nostdinc_include>)
if (NOT CONFIG_NEWLIB_LIBC AND
NOT (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_USE_MODULE) AND
NOT CONFIG_HAS_ESPRESSIF_HAL AND
NOT CONFIG_NATIVE_BUILD)
zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
target_include_directories(zephyr_interface SYSTEM INTERFACE
$<TARGET_PROPERTY:compiler,nostdinc_include>
)
endif()

if(CONFIG_MINIMAL_LIBCPP)
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,nostdincxx>>)
Expand Down
2 changes: 2 additions & 0 deletions arch/x86/ia32.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# Find out if we are optimizing for size
get_target_property(zephyr_COMPILE_OPTIONS zephyr_interface INTERFACE_COMPILE_OPTIONS)
get_property(zephyr_OPTIMIZE_FLAG TARGET compiler PROPERTY optimization)
list(APPEND zephyr_COMPILE_OPTIONS ${zephyr_OPTIMIZE_FLAG})
#Any -Os is (or may be) wraped in $<COMPILE_LANGUAGE> guards
list(FILTER zephyr_COMPILE_OPTIONS INCLUDE REGEX "-Os")
list(LENGTH zephyr_COMPILE_OPTIONS have_os)
Expand Down
18 changes: 12 additions & 6 deletions cmake/compiler/arcmwdt/compiler_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ list(APPEND CXX_EXCLUDED_OPTIONS
#####################################################
# This section covers flags related to optimization #
#####################################################
set_compiler_property(PROPERTY no_optimization -O0)
set_compiler_property(PROPERTY optimization_debug -O0)
set_compiler_property(PROPERTY optimization_speed -O2)
set_compiler_property(PROPERTY optimization_size -Os)
set_compiler_property(PROPERTY optimization_fast -O3)
set_compiler_property(PROPERTY no_optimizations -O0)
set_compiler_property(PROPERTY debug_optimizations -O0)
set_compiler_property(PROPERTY speed_optimizations -O2)
set_compiler_property(PROPERTY size_optimizations -Os)
set_compiler_property(PROPERTY optimization_fast -O3)

set_compiler_property(PROPERTY optimization
CHOICE no_optimizations
debug_optimizations
speed_optimizations
size_optimizations
)

#######################################################
# This section covers flags related to warning levels #
Expand Down Expand Up @@ -118,7 +125,6 @@ set_compiler_property(PROPERTY cstd -std=)

if (NOT CONFIG_ARCMWDT_LIBC)
set_compiler_property(PROPERTY nostdinc -Hno_default_include -Hnoarcexlib -U__STDC_LIB_EXT1__)
set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC})
endif()

# C++ std options
Expand Down
3 changes: 3 additions & 0 deletions cmake/compiler/arcmwdt/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ list(APPEND CMAKE_REQUIRED_FLAGS
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")

set(NOSTDINC ${TOOLCHAIN_HOME}/arc/inc)
if (NOT CONFIG_ARCMWDT_LIBC)
set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC})
endif()

# For CMake to be able to test if a compiler flag is supported by the toolchain
# (check_c_compiler_flag function which we wrap with target_cc_option in extensions.cmake)
Expand Down
1 change: 1 addition & 0 deletions cmake/compiler/armclang/compiler_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set_property(TARGET asm APPEND PROPERTY required "--target=${triple}")

# Only the ARM Compiler C library is currently supported.
set_compiler_property(PROPERTY nostdinc)
set_compiler_property(APPEND PROPERTY nostdinc_include)

# Remove after testing that -Wshadow works
set_compiler_property(PROPERTY warning_shadow_variables)
28 changes: 28 additions & 0 deletions cmake/compiler/clang/functions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2025, Nordic Semiconductor ASA

# File for Zephyr clang compiler / linker support functions.
#

# Inherit functions from GCC as Zephyr's clang toolchain infrastructure is
# compatible and can re-use the functions with a slight adjustment which is
# applying the `--target=<target>` flag that clang requires.
include(${ZEPHYR_BASE}/cmake/compiler/gcc/functions.cmake)

function(compiler_file_path filepath_out filename flags)
if(DEFINED CMAKE_C_COMPILER_TARGET)
set(clang_target_flag "--target=${CMAKE_C_COMPILER_TARGET}")
endif()
_compiler_file_path(${filepath_out} ${filename} "${clang_target_flag};${flags}")
set(${filepath_out} ${${filepath_out}} PARENT_SCOPE)
endfunction()

function(compiler_rt_library filepath_out library_name_out flags)
if(DEFINED CMAKE_C_COMPILER_TARGET)
set(clang_target_flag "--target=${CMAKE_C_COMPILER_TARGET}")
endif()
_compiler_rt_library(${filepath_out} ${library_name_out} "${clang_target_flag};${flags}")
set(${filepath_out} ${${filepath_out}} PARENT_SCOPE)
set(${library_name_out} ${${library_name_out}} PARENT_SCOPE)
endfunction()
17 changes: 2 additions & 15 deletions cmake/compiler/clang/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Configuration for host installed clang
#
include(${CMAKE_CURRENT_LIST_DIR}/functions.cmake)

set(NOSTDINC "")

Expand Down Expand Up @@ -55,6 +56,7 @@ if(NOT "${ARCH}" STREQUAL "posix")
foreach(isystem_include_dir ${NOSTDINC})
list(APPEND isystem_include_flags -isystem "\"${isystem_include_dir}\"")
endforeach()
set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC})

if(CONFIG_X86)
if(CONFIG_64BIT)
Expand Down Expand Up @@ -100,21 +102,6 @@ if(NOT "${ARCH}" STREQUAL "posix")
endif()
endif()

# This libgcc code is partially duplicated in compiler/*/target.cmake
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${clang_target_flag} ${TOOLCHAIN_C_FLAGS}
--print-libgcc-file-name
OUTPUT_VARIABLE RTLIB_FILE_NAME
OUTPUT_STRIP_TRAILING_WHITESPACE
)

get_filename_component(RTLIB_DIR ${RTLIB_FILE_NAME} DIRECTORY)
get_filename_component(RTLIB_NAME_WITH_PREFIX ${RTLIB_FILE_NAME} NAME_WLE)
string(REPLACE lib "" RTLIB_NAME ${RTLIB_NAME_WITH_PREFIX})

set_property(TARGET linker PROPERTY lib_include_dir "-L${RTLIB_DIR}")
set_property(TARGET linker PROPERTY rt_library "-l${RTLIB_NAME}")

list(APPEND CMAKE_REQUIRED_FLAGS -nostartfiles -nostdlib ${isystem_include_flags})
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")

Expand Down
29 changes: 15 additions & 14 deletions cmake/compiler/compiler_flags_template.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@
#####################################################
# This section covers flags related to optimization #
#####################################################
# Old property names now deprecated.
set_compiler_property(PROPERTY no_optimization)

set_compiler_property(PROPERTY optimization_debug)

set_compiler_property(PROPERTY optimization_speed)

set_compiler_property(PROPERTY optimization_size)

set_compiler_property(PROPERTY optimization_size_aggressive)

# New optimization property names corresponds to the similar Kconfig settings.
set_compiler_property(PROPERTY no_optimizations)

set_compiler_property(PROPERTY debug_optimizations)

set_compiler_property(PROPERTY speed_optimizations)

set_compiler_property(PROPERTY size_optimizations)

set_compiler_property(PROPERTY size_optimizations_aggressive)

set_compiler_property(PROPERTY optimization_fast)

# This property is intended to contain the current build system optimization selection.
set_compiler_property(PROPERTY optimization)

#######################################################
# This section covers flags related to warning levels #
#######################################################
Expand Down Expand Up @@ -127,9 +138,6 @@ set_property(TARGET compiler-cpp PROPERTY no_threadsafe_statics)
# Required ASM flags when compiling
set_property(TARGET asm PROPERTY required)

# GCC compiler flags for imacros. The specific header must be appended by user.
set_property(TARGET asm PROPERTY imacros)

# Compiler flag for disabling pointer arithmetic warnings
set_compiler_property(PROPERTY warning_no_pointer_arithmetic)

Expand All @@ -143,8 +151,6 @@ set_compiler_property(PROPERTY no_global_merge)

# Compiler flag for warning about shadow variables
set_compiler_property(PROPERTY warning_shadow_variables)
# Compiler flag for disabling warning about array bounds
set_compiler_property(PROPERTY warning_no_array_bounds)

# Compiler flags to avoid recognizing built-in functions
set_compiler_property(PROPERTY no_builtin)
Expand All @@ -156,8 +162,3 @@ set_compiler_property(PROPERTY specs)

# Compiler flag for defining preinclude files.
set_compiler_property(PROPERTY include_file)

# Compiler flag for trustzone
set_compiler_property(PROPERTY cmse)

set_property(TARGET asm PROPERTY cmse)
47 changes: 29 additions & 18 deletions cmake/compiler/gcc/compiler_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,34 @@ list(APPEND CXX_EXCLUDED_OPTIONS
#####################################################
# This section covers flags related to optimization #
#####################################################
# Old property names now deprecated.
set_compiler_property(PROPERTY no_optimization -O0)
if(CMAKE_C_COMPILER_VERSION VERSION_LESS "4.8.0")
set_compiler_property(PROPERTY optimization_debug -O0)
else()
set_compiler_property(PROPERTY optimization_debug -Og)
endif()
set_compiler_property(PROPERTY optimization_debug -O0)
set_compiler_property(PROPERTY optimization_speed -O2)
set_compiler_property(PROPERTY optimization_size -Os)
set_compiler_property(PROPERTY optimization_size -Os)
set_compiler_property(PROPERTY optimization_size_aggressive -Oz)
set_compiler_property(PROPERTY optimization_fast -Ofast)

if(CMAKE_C_COMPILER_VERSION GREATER_EQUAL "4.5.0")
set_compiler_property(PROPERTY optimization_lto -flto=auto)
set_compiler_property(PROPERTY prohibit_lto -fno-lto)
endif()
# New optimization property names corresponds to the similar Kconfig settings.
set_compiler_property(PROPERTY no_optimizations -O0)
set_compiler_property(PROPERTY debug_optimizations -O0)
# Very old gcc releases does not support -Og, so let's check and update setting if supported.
check_set_compiler_property(PROPERTY debug_optimizations -Og)
set_compiler_property(PROPERTY speed_optimizations -O2)
set_compiler_property(PROPERTY size_optimizations -Os)
set_compiler_property(PROPERTY size_optimizations_aggressive -Oz)
set_compiler_property(PROPERTY optimization_fast -Ofast)
# Use 'check' version here to wait for debug_optimizations to be set
check_set_compiler_property(PROPERTY optimization
CHOICE no_optimizations
debug_optimizations
speed_optimizations
size_optimizations
size_optimizations_aggressive
)

check_set_compiler_property(PROPERTY optimization_lto -flto=auto)
check_set_compiler_property(PROPERTY prohibit_lto -fno-lto)

#######################################################
# This section covers flags related to warning levels #
Expand Down Expand Up @@ -113,14 +126,12 @@ set_compiler_property(PROPERTY warning_error_coding_guideline
# GCC compiler flags for C standard. The specific standard must be appended by user.
set_compiler_property(PROPERTY cstd -std=)

if (NOT CONFIG_NEWLIB_LIBC AND
NOT (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_USE_MODULE) AND
NOT COMPILER STREQUAL "xcc" AND
NOT CONFIG_HAS_ESPRESSIF_HAL AND
NOT CONFIG_NATIVE_BUILD)
set_compiler_property(PROPERTY nostdinc -nostdinc)
set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC})
endif()
set_compiler_property(PROPERTY nostdinc -nostdinc)
# Keeping nostdinc property empty. The compiler will populate include paths
# from its default search path. The toolchain may adjust the value to a
# specific location, for example gcc infrastructure will set the value based
# on output from --print-libgcc-file-name.
set_compiler_property(APPEND PROPERTY nostdinc_include)

check_set_compiler_property(PROPERTY no_printf_return_value -fno-printf-return-value)

Expand Down
63 changes: 63 additions & 0 deletions cmake/compiler/gcc/functions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2025, Nordic Semiconductor ASA

# File for Zephyr gcc compiler / linker support functions.
#

# Search for filename in default compiler library path using the
# --print-file-name option which is common to gcc and clang. If the
# file is not found, filepath_out will be set to an empty string.
#
# It will use TOOLCHAIN_C_FLAGS and build system selected optimization level.
# Extra compiler flags required for compiler invocation or might impact behavior
# can be passed in the flags argument.
function(compiler_file_path filepath_out filename flags)
get_property(optimization TARGET compiler PROPERTY optimization)
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} ${optimization} ${flags}
--print-file-name ${filename}
OUTPUT_VARIABLE filepath
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(${filepath} STREQUAL ${filename})
set(filepath "")
endif()
set(${filepath_out} "${filepath}" PARENT_SCOPE)
endfunction()

# Return the lib_include_dir and rt_library linker values
# by searching for the runtime library in the compiler default
# library search path. If no runtime library is found, the return
# variable will be set to empty string.
#
# It will use TOOLCHAIN_C_FLAGS and build system selected optimization level.
# Extra compiler flags required for compiler invocation or might impact behavior
function(compiler_rt_library filepath_out library_name_out flags)
get_property(optimization TARGET compiler PROPERTY optimization)
# Compute complete path to the runtime library using the
# --print-libgcc-file-name compiler flag
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} ${optimization} ${flags}
--print-libgcc-file-name
OUTPUT_VARIABLE library_path
OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Compute the library directory name

get_filename_component(library_dir ${library_path} DIRECTORY)
set_linker_property(PROPERTY lib_include_dir "-L${library_dir}")
set(${filepath_out} ${library_dir} PARENT_SCOPE)

# Compute the linker option for this library

get_filename_component(library_basename ${library_path} NAME_WLE)

# Remove the leading 'lib' prefix to leave a value suitable for use with
# the linker -l flag
string(REPLACE lib "" library_name ${library_basename})

set_linker_property(PROPERTY rt_library "-l${library_name}")
set(${library_name_out} ${library_name} PARENT_SCOPE)
endfunction()
Loading