1# SPDX-License-Identifier: Apache-2.0 2 3if (NOT CMAKE_HOST_UNIX OR CMAKE_HOST_APPLE) 4 message(FATAL_ERROR "The POSIX architecture only works on Linux. If on Windows or macOS " 5 "consider using a virtual machine to run a Linux guest.") 6endif() 7 8# This native_simulator library is used to pass options to the 9# native_simulator runner build. Currently the following are used: 10# INTERFACE_COMPILE_OPTIONS: 11# Extra compile options to be used during the build of the runner files 12# For ex. target_compile_options(native_simulator INTERFACE "-m64") 13# INTERFACE_LINK_OPTIONS: 14# Extra link options to be passed during the *final* link of the runner 15# with the embedded SW. 16# For ex. target_link_options(native_simulator INTERFACE "-lstdc++") 17# INTERFACE_SOURCES: 18# Extra sources to be built in the native simulator runner context 19# For ex. target_sources(native_simulator INTERFACE silly.c) 20# Note that these are built with the host libC and the include directories 21# the runner is built with. 22# RUNNER_LINK_LIBRARIES: 23# Extra libraries to link with the runner 24# For ex. set_property(TARGET native_simulator APPEND PROPERTY RUNNER_LINK_LIBRARIES "mylib.a") 25# LOCALIZE_EXTRA_OPTIONS: 26# Extra options to be passed to objcopy when localizing each Zephyr MCU image symbols 27# This can be used to hide symbols a library may have set as visible outside of 28# itself once the MCU image has been assembled. 29# For ex. set_property(TARGET native_simulator APPEND PROPERTY LOCALIZE_EXTRA_OPTIONS "--localize-symbol=spinel*") 30# Note: target_link_libraries() cannot be used on this library at this point. 31# target_link_libraries() updates INTERFACE_LINK_LIBRARIES but wrapping it with extra 32# information. This means we cannot directly pass it to the native_simulator runner build. 33# Check https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_LIBRARIES.html for more 34# info. 35# We use target_link_options() instead 36add_library(native_simulator INTERFACE) 37set_property(TARGET native_simulator PROPERTY RUNNER_LINK_LIBRARIES "") 38set_property(TARGET native_simulator PROPERTY LOCALIZE_EXTRA_OPTIONS "") 39 40set(NSI_DIR ${ZEPHYR_BASE}/scripts/native_simulator CACHE PATH "Path to the native simulator") 41 42if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/${CMAKE_HOST_SYSTEM_NAME}.${CMAKE_HOST_SYSTEM_PROCESSOR}.cmake) 43 # @Intent: Set necessary compiler & linker options for this specific host architecture & OS 44 include(${CMAKE_HOST_SYSTEM_NAME}.${CMAKE_HOST_SYSTEM_PROCESSOR}.cmake) 45else() # Linux.x86_64 46 if (CONFIG_64BIT) 47 # some gcc versions fail to build without -fPIC 48 zephyr_compile_options(-m64 -fPIC) 49 zephyr_link_libraries(-m64) 50 51 target_link_options(native_simulator INTERFACE "-m64") 52 target_compile_options(native_simulator INTERFACE "-m64") 53 else () 54 zephyr_compile_options(-m32) 55 zephyr_link_libraries(-m32) 56 57 target_link_options(native_simulator INTERFACE "-m32") 58 target_compile_options(native_simulator INTERFACE "-m32") 59 60 # When building for 32bits x86, gcc defaults to using the old 8087 float arithmetic 61 # which causes some issues with literal float comparisons. So we set it 62 # to use the SSE2 float path instead 63 # (clang defaults to use SSE, but, setting this option for it is safe) 64 check_set_compiler_property(APPEND PROPERTY fpsse2 "SHELL:-msse2 -mfpmath=sse") 65 zephyr_compile_options($<TARGET_PROPERTY:compiler,fpsse2>) 66 target_compile_options(native_simulator INTERFACE "$<TARGET_PROPERTY:compiler,fpsse2>") 67 endif () 68endif() 69 70zephyr_compile_options( 71 ${ARCH_FLAG} 72 ) 73 74if (CONFIG_NATIVE_APPLICATION) 75 zephyr_compile_options( 76 -include ${ZEPHYR_BASE}/arch/posix/include/posix_cheats.h 77 ) 78elseif (CONFIG_NATIVE_LIBRARY) 79 zephyr_compile_options( 80 -fvisibility=hidden 81 ) 82 83 # While doing the partial linking of the native library, some symbols will be missing 84 # which are provided by the native simulator runner 85 zephyr_ld_options( 86 -Wl,--unresolved-symbols=ignore-all 87 ) 88 89 if (NOT CONFIG_EXTERNAL_LIBC) 90 # Get the *compiler* include path, that is where the *compiler* provided headers are (not the 91 # default libC ones). This includes basic headers like stdint.h, stddef.h or float.h 92 # We expect something like 93 # /usr/lib/gcc/x86_64-linux-gnu/12/include or /usr/lib/llvm-14/lib/clang/14.0.0/include 94 execute_process( 95 COMMAND ${CMAKE_C_COMPILER} --print-file-name=include/stddef.h 96 OUTPUT_VARIABLE _OUTPUT 97 COMMAND_ERROR_IS_FATAL ANY 98 ) 99 get_filename_component(COMPILER_OWN_INCLUDE_PATH "${_OUTPUT}" DIRECTORY) 100 101 # Do not use the C library from this compiler/host, 102 # but still use the basic compiler headers, 103 # remove all operating system specific predefined macros, 104 # no_builtin to avoid the compiler using builtin replacements for std library functions 105 zephyr_compile_options( 106 -nostdinc 107 -isystem ${COMPILER_OWN_INCLUDE_PATH} 108 "SHELL:-include ${ZEPHYR_BASE}/arch/posix/include/undef_system_defines.h" 109 $<TARGET_PROPERTY:compiler,freestanding> 110 $<TARGET_PROPERTY:compiler,no_builtin> 111 ) 112 endif() 113 114 if (CONFIG_COMPILER_WARNINGS_AS_ERRORS) 115 target_compile_options(native_simulator INTERFACE $<TARGET_PROPERTY:compiler,warnings_as_errors>) 116 endif() 117endif() 118 119if(CONFIG_EXTERNAL_LIBC) 120 # @Intent: Obtain compiler specific flags for no freestanding compilation 121 zephyr_compile_options($<TARGET_PROPERTY:compiler,hosted>) 122endif() 123 124if(CONFIG_EXTERNAL_LIBCPP) 125 target_link_options(native_simulator INTERFACE "-lstdc++") 126endif() 127 128zephyr_include_directories(${BOARD_DIR}) 129 130if(CONFIG_COVERAGE) 131 target_compile_options(native_simulator INTERFACE $<TARGET_PROPERTY:compiler,coverage>) 132 target_link_options(native_simulator INTERFACE $<TARGET_PROPERTY:linker,coverage>) 133endif() 134 135if (CONFIG_GPROF) 136 zephyr_compile_options($<TARGET_PROPERTY:compiler,gprof>) 137 zephyr_link_libraries($<TARGET_PROPERTY:linker,gprof>) 138 139 target_link_options(native_simulator INTERFACE "-pg") 140endif() 141 142if (CONFIG_NATIVE_APPLICATION) 143 zephyr_ld_options( 144 -ldl 145 -pthread 146 ) 147endif() 148 149# About the -include directive: The reason to do it this way, is because in this 150# manner it is transparent to the application. Otherwise posix_cheats.h needs to 151# be included in all the applications' files which define main( ), and in any 152# app file which uses the pthreads like API provided by Zephyr 153# ( include/posix/pthread.h / kernel/pthread.c ) [And any future API added to 154# Zephyr which will clash with the native POSIX API] . It would also need to 155# be included in a few zephyr kernel files. 156 157# 158# Support for the LLVM Sanitizer toolchain instrumentation frameworks 159# (supported by current gcc's as well) 160# 161 162if(CONFIG_ASAN) 163 list(APPEND LLVM_SANITIZERS "address") 164endif() 165 166if(CONFIG_MSAN) 167 list(APPEND LLVM_SANITIZERS "memory") 168endif() 169 170if(CONFIG_UBSAN) 171 list(APPEND LLVM_SANITIZERS "undefined") 172endif() 173 174if(CONFIG_ASAN_RECOVER) 175 zephyr_compile_options(-fsanitize-recover=all) 176 target_compile_options(native_simulator INTERFACE "-fsanitize-recover=all") 177endif() 178 179if(CONFIG_ARCH_POSIX_LIBFUZZER) 180 list(APPEND LLVM_SANITIZERS "fuzzer") 181 target_compile_options(native_simulator INTERFACE "-DNSI_NO_MAIN=1") 182 if(NOT CONFIG_64BIT) 183 # On i386, libfuzzer seems to dynamically relocate the binary, so 184 # we need to emit PIC code. This limitation is undocumented and 185 # poorly understood... 186 zephyr_compile_options(-fPIC) 187 endif() 188endif() 189 190list(JOIN LLVM_SANITIZERS "," LLVM_SANITIZERS_ARG) 191if(NOT ${LLVM_SANITIZERS_ARG} STREQUAL "") 192 set(LLVM_SANITIZERS_ARG "-fsanitize=${LLVM_SANITIZERS_ARG}") 193 zephyr_compile_options("${LLVM_SANITIZERS_ARG}") 194 if (CONFIG_NATIVE_APPLICATION) 195 zephyr_link_libraries("${LLVM_SANITIZERS_ARG}") 196 endif() 197 198 target_link_options(native_simulator INTERFACE ${LLVM_SANITIZERS_ARG}) 199 target_compile_options(native_simulator INTERFACE ${LLVM_SANITIZERS_ARG}) 200endif() 201 202add_subdirectory(core) 203