2019-04-06 15:08:09 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-07-04 10:50:27 +02:00
|
|
|
if (NOT CMAKE_HOST_UNIX OR CMAKE_HOST_APPLE)
|
|
|
|
message(FATAL_ERROR "The POSIX architecture only works on Linux. If on Windows or macOS "
|
|
|
|
"consider using a virtual machine to run a Linux guest.")
|
|
|
|
endif()
|
|
|
|
|
2023-06-07 10:39:48 +02:00
|
|
|
# This native_simulator library is used to pass options to the
|
|
|
|
# native_simulator runner build. Currently the following are used:
|
|
|
|
# INTERFACE_COMPILE_OPTIONS:
|
|
|
|
# Extra compile options to be used during the build of the runner files
|
|
|
|
# For ex. target_compile_options(native_simulator INTERFACE "-m64")
|
|
|
|
# INTERFACE_LINK_OPTIONS:
|
|
|
|
# Extra link options to be passed during the *final* link of the runner
|
|
|
|
# with the embedded SW.
|
|
|
|
# For ex. target_link_options(native_simulator INTERFACE "-lstdc++")
|
|
|
|
# INTERFACE_SOURCES:
|
|
|
|
# Extra sources to be built in the native simulator runner context
|
|
|
|
# For ex. target_sources(native_simulator INTERFACE silly.c)
|
|
|
|
# Note that these are built with the host libC and the include directories
|
|
|
|
# the runner is built with.
|
2023-08-24 12:55:53 +02:00
|
|
|
# RUNNER_LINK_LIBRARIES:
|
|
|
|
# Extra libraries to link with the runner
|
|
|
|
# For ex. set_property(TARGET native_simulator APPEND PROPERTY RUNNER_LINK_LIBRARIES "mylib.a")
|
2023-12-05 11:03:43 +01:00
|
|
|
# LOCALIZE_EXTRA_OPTIONS:
|
|
|
|
# Extra options to be passed to objcopy when localizing each Zephyr MCU image symbols
|
|
|
|
# This can be used to hide symbols a library may have set as visible outside of
|
|
|
|
# itself once the MCU image has been assembled.
|
|
|
|
# For ex. set_property(TARGET native_simulator APPEND PROPERTY LOCALIZE_EXTRA_OPTIONS "--localize-symbol=spinel*")
|
2023-07-05 17:41:51 +02:00
|
|
|
# Note: target_link_libraries() cannot be used on this library at this point.
|
|
|
|
# target_link_libraries() updates INTERFACE_LINK_LIBRARIES but wrapping it with extra
|
|
|
|
# information. This means we cannot directly pass it to the native_simulator runner build.
|
|
|
|
# Check https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_LIBRARIES.html for more
|
|
|
|
# info.
|
|
|
|
# We use target_link_options() instead
|
2023-06-07 10:39:48 +02:00
|
|
|
add_library(native_simulator INTERFACE)
|
2023-08-24 12:55:53 +02:00
|
|
|
set_property(TARGET native_simulator PROPERTY RUNNER_LINK_LIBRARIES "")
|
2023-12-05 11:03:43 +01:00
|
|
|
set_property(TARGET native_simulator PROPERTY LOCALIZE_EXTRA_OPTIONS "")
|
2023-06-07 10:39:48 +02:00
|
|
|
|
2023-08-02 14:46:27 +02:00
|
|
|
set(NSI_DIR ${ZEPHYR_BASE}/scripts/native_simulator CACHE PATH "Path to the native simulator")
|
|
|
|
|
2020-05-08 20:52:24 +02:00
|
|
|
if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/${CMAKE_HOST_SYSTEM_NAME}.${CMAKE_HOST_SYSTEM_PROCESSOR}.cmake)
|
|
|
|
# @Intent: Set necessary compiler & linker options for this specific host architecture & OS
|
|
|
|
include(${CMAKE_HOST_SYSTEM_NAME}.${CMAKE_HOST_SYSTEM_PROCESSOR}.cmake)
|
2023-08-10 09:39:04 +02:00
|
|
|
else() # Linux.x86_64
|
2020-05-08 20:52:24 +02:00
|
|
|
if (CONFIG_64BIT)
|
|
|
|
# some gcc versions fail to build without -fPIC
|
|
|
|
zephyr_compile_options(-m64 -fPIC)
|
|
|
|
zephyr_link_libraries(-m64)
|
2023-06-07 10:39:48 +02:00
|
|
|
|
|
|
|
target_link_options(native_simulator INTERFACE "-m64")
|
|
|
|
target_compile_options(native_simulator INTERFACE "-m64")
|
2020-05-08 20:52:24 +02:00
|
|
|
else ()
|
|
|
|
zephyr_compile_options(-m32)
|
|
|
|
zephyr_link_libraries(-m32)
|
2023-06-07 10:39:48 +02:00
|
|
|
|
|
|
|
target_link_options(native_simulator INTERFACE "-m32")
|
|
|
|
target_compile_options(native_simulator INTERFACE "-m32")
|
2023-08-10 09:39:04 +02:00
|
|
|
|
|
|
|
# When building for 32bits x86, gcc defaults to using the old 8087 float arithmetic
|
|
|
|
# which causes some issues with literal float comparisons. So we set it
|
|
|
|
# to use the SSE2 float path instead
|
|
|
|
# (clang defaults to use SSE, but, setting this option for it is safe)
|
|
|
|
check_set_compiler_property(APPEND PROPERTY fpsse2 "SHELL:-msse2 -mfpmath=sse")
|
|
|
|
zephyr_compile_options($<TARGET_PROPERTY:compiler,fpsse2>)
|
|
|
|
target_compile_options(native_simulator INTERFACE "$<TARGET_PROPERTY:compiler,fpsse2>")
|
2020-05-08 20:52:24 +02:00
|
|
|
endif ()
|
|
|
|
endif()
|
2019-07-03 19:21:40 +02:00
|
|
|
|
2017-10-03 16:31:55 +02:00
|
|
|
zephyr_compile_options(
|
|
|
|
${ARCH_FLAG}
|
|
|
|
)
|
|
|
|
|
2023-06-07 10:29:10 +02:00
|
|
|
if (CONFIG_NATIVE_APPLICATION)
|
|
|
|
zephyr_compile_options(
|
|
|
|
-include ${ZEPHYR_BASE}/arch/posix/include/posix_cheats.h
|
|
|
|
)
|
2023-06-07 10:39:48 +02:00
|
|
|
elseif (CONFIG_NATIVE_LIBRARY)
|
|
|
|
zephyr_compile_options(
|
|
|
|
-fvisibility=hidden
|
|
|
|
)
|
|
|
|
|
|
|
|
# While doing the partial linking of the native library, some symbols will be missing
|
|
|
|
# which are provided by the native simulator runner
|
|
|
|
zephyr_ld_options(
|
|
|
|
-Wl,--unresolved-symbols=ignore-all
|
|
|
|
)
|
|
|
|
|
|
|
|
if (NOT CONFIG_EXTERNAL_LIBC)
|
|
|
|
# Get the *compiler* include path, that is where the *compiler* provided headers are (not the
|
|
|
|
# default libC ones). This includes basic headers like stdint.h, stddef.h or float.h
|
|
|
|
# We expect something like
|
|
|
|
# /usr/lib/gcc/x86_64-linux-gnu/12/include or /usr/lib/llvm-14/lib/clang/14.0.0/include
|
|
|
|
execute_process(
|
|
|
|
COMMAND ${CMAKE_C_COMPILER} --print-file-name=include/stddef.h
|
|
|
|
OUTPUT_VARIABLE _OUTPUT
|
|
|
|
COMMAND_ERROR_IS_FATAL ANY
|
|
|
|
)
|
|
|
|
get_filename_component(COMPILER_OWN_INCLUDE_PATH "${_OUTPUT}" DIRECTORY)
|
|
|
|
|
|
|
|
# Do not use the C library from this compiler/host,
|
2024-04-26 18:02:31 +02:00
|
|
|
# but still use the basic compiler headers,
|
|
|
|
# remove all operating system specific predefined macros,
|
2023-11-12 21:34:50 +01:00
|
|
|
# no_builtin to avoid the compiler using builtin replacements for std library functions
|
2023-06-07 10:39:48 +02:00
|
|
|
zephyr_compile_options(
|
|
|
|
-nostdinc
|
|
|
|
-isystem ${COMPILER_OWN_INCLUDE_PATH}
|
2024-04-26 18:02:31 +02:00
|
|
|
"SHELL:-include ${ZEPHYR_BASE}/arch/posix/include/undef_system_defines.h"
|
2023-06-07 10:39:48 +02:00
|
|
|
$<TARGET_PROPERTY:compiler,freestanding>
|
2023-11-12 21:34:50 +01:00
|
|
|
$<TARGET_PROPERTY:compiler,no_builtin>
|
2023-06-07 10:39:48 +02:00
|
|
|
)
|
|
|
|
endif()
|
2024-01-25 11:03:05 +01:00
|
|
|
|
|
|
|
if (CONFIG_COMPILER_WARNINGS_AS_ERRORS)
|
|
|
|
target_compile_options(native_simulator INTERFACE $<TARGET_PROPERTY:compiler,warnings_as_errors>)
|
|
|
|
endif()
|
2023-06-07 10:29:10 +02:00
|
|
|
endif()
|
|
|
|
|
2023-06-07 10:39:48 +02:00
|
|
|
if(CONFIG_EXTERNAL_LIBC)
|
|
|
|
# @Intent: Obtain compiler specific flags for no freestanding compilation
|
|
|
|
zephyr_compile_options($<TARGET_PROPERTY:compiler,hosted>)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(CONFIG_EXTERNAL_LIBCPP)
|
|
|
|
target_link_options(native_simulator INTERFACE "-lstdc++")
|
|
|
|
endif()
|
2019-03-08 11:29:31 +01:00
|
|
|
|
2018-11-16 04:27:13 +01:00
|
|
|
zephyr_include_directories(${BOARD_DIR})
|
|
|
|
|
2023-06-07 10:39:48 +02:00
|
|
|
if(CONFIG_COVERAGE)
|
|
|
|
target_compile_options(native_simulator INTERFACE $<TARGET_PROPERTY:compiler,coverage>)
|
|
|
|
target_link_options(native_simulator INTERFACE $<TARGET_PROPERTY:linker,coverage>)
|
|
|
|
endif()
|
|
|
|
|
2022-04-21 16:38:32 +02:00
|
|
|
if (CONFIG_GPROF)
|
|
|
|
zephyr_compile_options($<TARGET_PROPERTY:compiler,gprof>)
|
|
|
|
zephyr_link_libraries($<TARGET_PROPERTY:linker,gprof>)
|
2023-06-07 10:39:48 +02:00
|
|
|
|
|
|
|
target_link_options(native_simulator INTERFACE "-pg")
|
2022-04-21 16:38:32 +02:00
|
|
|
endif()
|
2019-10-04 00:54:00 +02:00
|
|
|
|
2023-06-07 10:39:48 +02:00
|
|
|
if (CONFIG_NATIVE_APPLICATION)
|
|
|
|
zephyr_ld_options(
|
|
|
|
-ldl
|
|
|
|
-pthread
|
|
|
|
)
|
|
|
|
endif()
|
2017-10-03 16:31:55 +02:00
|
|
|
|
|
|
|
# About the -include directive: The reason to do it this way, is because in this
|
|
|
|
# manner it is transparent to the application. Otherwise posix_cheats.h needs to
|
|
|
|
# be included in all the applications' files which define main( ), and in any
|
|
|
|
# app file which uses the pthreads like API provided by Zephyr
|
|
|
|
# ( include/posix/pthread.h / kernel/pthread.c ) [And any future API added to
|
|
|
|
# Zephyr which will clash with the native POSIX API] . It would also need to
|
|
|
|
# be included in a few zephyr kernel files.
|
|
|
|
|
cmake: Update CONFIG_ASAN support
This had bitrotten a bit, and didn't build as shipped. Current
libasan implementations want -fsanitize=address passed as a linker
argument too. We have grown a "lld" linker variant that needs the
same cmake treatment as the "ld" binutils one, but never got it. But
the various flags had been cut/pasted around to different places, with
slightly different forms. That's really sort of a mess, as sanitizer
support was only ever support with host toolchains for native_posix
(and AFAICT no one anywhere has made this work on cross compilers in
an embedded environment). And the separate "gcc" vs. "llvm" layers
were silly, as there has only ever been one API for this feature (from
LLVM, then picked up compatibly by gcc).
Pull this stuff out and just do it in one place in the posix arch for
simplicity.
Also recent sanitizers are trying to add instrumentation padding
around data that we use linker trickery to pack tightly
(c.f. SYS_INIT, STRUCT_SECTION_ITERABLE) and we need a way
("__noasan") to turn that off. Actually for gcc, it was enough to
just make the records const (already true for most of them, except a
native_posix init struct), but clang apparently isn't smart enough.
Finally, add an ASAN_RECOVER kconfig that enables the use of
"halt_on_error=0" in $ASAN_OPTIONS, which continues execution past the
first error.
Signed-off-by: Andy Ross <andyross@google.com>
2022-08-10 02:57:42 +02:00
|
|
|
#
|
|
|
|
# Support for the LLVM Sanitizer toolchain instrumentation frameworks
|
|
|
|
# (supported by current gcc's as well)
|
|
|
|
#
|
|
|
|
|
|
|
|
if(CONFIG_ASAN)
|
|
|
|
list(APPEND LLVM_SANITIZERS "address")
|
|
|
|
endif()
|
|
|
|
|
2022-08-16 20:42:53 +02:00
|
|
|
if(CONFIG_MSAN)
|
|
|
|
list(APPEND LLVM_SANITIZERS "memory")
|
|
|
|
endif()
|
|
|
|
|
cmake: Update CONFIG_ASAN support
This had bitrotten a bit, and didn't build as shipped. Current
libasan implementations want -fsanitize=address passed as a linker
argument too. We have grown a "lld" linker variant that needs the
same cmake treatment as the "ld" binutils one, but never got it. But
the various flags had been cut/pasted around to different places, with
slightly different forms. That's really sort of a mess, as sanitizer
support was only ever support with host toolchains for native_posix
(and AFAICT no one anywhere has made this work on cross compilers in
an embedded environment). And the separate "gcc" vs. "llvm" layers
were silly, as there has only ever been one API for this feature (from
LLVM, then picked up compatibly by gcc).
Pull this stuff out and just do it in one place in the posix arch for
simplicity.
Also recent sanitizers are trying to add instrumentation padding
around data that we use linker trickery to pack tightly
(c.f. SYS_INIT, STRUCT_SECTION_ITERABLE) and we need a way
("__noasan") to turn that off. Actually for gcc, it was enough to
just make the records const (already true for most of them, except a
native_posix init struct), but clang apparently isn't smart enough.
Finally, add an ASAN_RECOVER kconfig that enables the use of
"halt_on_error=0" in $ASAN_OPTIONS, which continues execution past the
first error.
Signed-off-by: Andy Ross <andyross@google.com>
2022-08-10 02:57:42 +02:00
|
|
|
if(CONFIG_UBSAN)
|
|
|
|
list(APPEND LLVM_SANITIZERS "undefined")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(CONFIG_ASAN_RECOVER)
|
|
|
|
zephyr_compile_options(-fsanitize-recover=all)
|
2024-04-10 13:13:11 +02:00
|
|
|
target_compile_options(native_simulator INTERFACE "-fsanitize-recover=all")
|
cmake: Update CONFIG_ASAN support
This had bitrotten a bit, and didn't build as shipped. Current
libasan implementations want -fsanitize=address passed as a linker
argument too. We have grown a "lld" linker variant that needs the
same cmake treatment as the "ld" binutils one, but never got it. But
the various flags had been cut/pasted around to different places, with
slightly different forms. That's really sort of a mess, as sanitizer
support was only ever support with host toolchains for native_posix
(and AFAICT no one anywhere has made this work on cross compilers in
an embedded environment). And the separate "gcc" vs. "llvm" layers
were silly, as there has only ever been one API for this feature (from
LLVM, then picked up compatibly by gcc).
Pull this stuff out and just do it in one place in the posix arch for
simplicity.
Also recent sanitizers are trying to add instrumentation padding
around data that we use linker trickery to pack tightly
(c.f. SYS_INIT, STRUCT_SECTION_ITERABLE) and we need a way
("__noasan") to turn that off. Actually for gcc, it was enough to
just make the records const (already true for most of them, except a
native_posix init struct), but clang apparently isn't smart enough.
Finally, add an ASAN_RECOVER kconfig that enables the use of
"halt_on_error=0" in $ASAN_OPTIONS, which continues execution past the
first error.
Signed-off-by: Andy Ross <andyross@google.com>
2022-08-10 02:57:42 +02:00
|
|
|
endif()
|
|
|
|
|
2022-08-23 21:24:14 +02:00
|
|
|
if(CONFIG_ARCH_POSIX_LIBFUZZER)
|
|
|
|
list(APPEND LLVM_SANITIZERS "fuzzer")
|
2024-04-11 11:56:30 +02:00
|
|
|
target_compile_options(native_simulator INTERFACE "-DNSI_NO_MAIN=1")
|
2022-08-24 23:05:42 +02:00
|
|
|
if(NOT CONFIG_64BIT)
|
|
|
|
# On i386, libfuzzer seems to dynamically relocate the binary, so
|
|
|
|
# we need to emit PIC code. This limitation is undocumented and
|
|
|
|
# poorly understood...
|
|
|
|
zephyr_compile_options(-fPIC)
|
|
|
|
endif()
|
2022-08-23 21:24:14 +02:00
|
|
|
endif()
|
|
|
|
|
cmake: Update CONFIG_ASAN support
This had bitrotten a bit, and didn't build as shipped. Current
libasan implementations want -fsanitize=address passed as a linker
argument too. We have grown a "lld" linker variant that needs the
same cmake treatment as the "ld" binutils one, but never got it. But
the various flags had been cut/pasted around to different places, with
slightly different forms. That's really sort of a mess, as sanitizer
support was only ever support with host toolchains for native_posix
(and AFAICT no one anywhere has made this work on cross compilers in
an embedded environment). And the separate "gcc" vs. "llvm" layers
were silly, as there has only ever been one API for this feature (from
LLVM, then picked up compatibly by gcc).
Pull this stuff out and just do it in one place in the posix arch for
simplicity.
Also recent sanitizers are trying to add instrumentation padding
around data that we use linker trickery to pack tightly
(c.f. SYS_INIT, STRUCT_SECTION_ITERABLE) and we need a way
("__noasan") to turn that off. Actually for gcc, it was enough to
just make the records const (already true for most of them, except a
native_posix init struct), but clang apparently isn't smart enough.
Finally, add an ASAN_RECOVER kconfig that enables the use of
"halt_on_error=0" in $ASAN_OPTIONS, which continues execution past the
first error.
Signed-off-by: Andy Ross <andyross@google.com>
2022-08-10 02:57:42 +02:00
|
|
|
list(JOIN LLVM_SANITIZERS "," LLVM_SANITIZERS_ARG)
|
|
|
|
if(NOT ${LLVM_SANITIZERS_ARG} STREQUAL "")
|
|
|
|
set(LLVM_SANITIZERS_ARG "-fsanitize=${LLVM_SANITIZERS_ARG}")
|
|
|
|
zephyr_compile_options("${LLVM_SANITIZERS_ARG}")
|
2024-04-10 13:14:31 +02:00
|
|
|
if (CONFIG_NATIVE_APPLICATION)
|
|
|
|
zephyr_link_libraries("${LLVM_SANITIZERS_ARG}")
|
|
|
|
endif()
|
2023-06-07 10:39:48 +02:00
|
|
|
|
|
|
|
target_link_options(native_simulator INTERFACE ${LLVM_SANITIZERS_ARG})
|
|
|
|
target_compile_options(native_simulator INTERFACE ${LLVM_SANITIZERS_ARG})
|
cmake: Update CONFIG_ASAN support
This had bitrotten a bit, and didn't build as shipped. Current
libasan implementations want -fsanitize=address passed as a linker
argument too. We have grown a "lld" linker variant that needs the
same cmake treatment as the "ld" binutils one, but never got it. But
the various flags had been cut/pasted around to different places, with
slightly different forms. That's really sort of a mess, as sanitizer
support was only ever support with host toolchains for native_posix
(and AFAICT no one anywhere has made this work on cross compilers in
an embedded environment). And the separate "gcc" vs. "llvm" layers
were silly, as there has only ever been one API for this feature (from
LLVM, then picked up compatibly by gcc).
Pull this stuff out and just do it in one place in the posix arch for
simplicity.
Also recent sanitizers are trying to add instrumentation padding
around data that we use linker trickery to pack tightly
(c.f. SYS_INIT, STRUCT_SECTION_ITERABLE) and we need a way
("__noasan") to turn that off. Actually for gcc, it was enough to
just make the records const (already true for most of them, except a
native_posix init struct), but clang apparently isn't smart enough.
Finally, add an ASAN_RECOVER kconfig that enables the use of
"halt_on_error=0" in $ASAN_OPTIONS, which continues execution past the
first error.
Signed-off-by: Andy Ross <andyross@google.com>
2022-08-10 02:57:42 +02:00
|
|
|
endif()
|
|
|
|
|
2017-10-03 16:31:55 +02:00
|
|
|
add_subdirectory(core)
|