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#  Note: target_link_libraries() cannot be used on this library at this point.
26#        target_link_libraries() updates INTERFACE_LINK_LIBRARIES but wrapping it with extra
27#        information. This means we cannot directly pass it to the native_simulator runner build.
28#        Check https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_LIBRARIES.html for more
29#        info.
30#        We use target_link_options() instead
31add_library(native_simulator INTERFACE)
32set_property(TARGET native_simulator PROPERTY RUNNER_LINK_LIBRARIES "")
33
34set(NSI_DIR ${ZEPHYR_BASE}/scripts/native_simulator CACHE PATH "Path to the native simulator")
35
36if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/${CMAKE_HOST_SYSTEM_NAME}.${CMAKE_HOST_SYSTEM_PROCESSOR}.cmake)
37  # @Intent: Set necessary compiler & linker options for this specific host architecture & OS
38  include(${CMAKE_HOST_SYSTEM_NAME}.${CMAKE_HOST_SYSTEM_PROCESSOR}.cmake)
39else() # Linux.x86_64
40  if (CONFIG_64BIT)
41    # some gcc versions fail to build without -fPIC
42    zephyr_compile_options(-m64 -fPIC)
43    zephyr_link_libraries(-m64)
44
45    target_link_options(native_simulator INTERFACE "-m64")
46    target_compile_options(native_simulator INTERFACE "-m64")
47  else ()
48    zephyr_compile_options(-m32)
49    zephyr_link_libraries(-m32)
50
51    target_link_options(native_simulator INTERFACE "-m32")
52    target_compile_options(native_simulator INTERFACE "-m32")
53
54    # When building for 32bits x86, gcc defaults to using the old 8087 float arithmetic
55    # which causes some issues with literal float comparisons. So we set it
56    # to use the SSE2 float path instead
57    # (clang defaults to use SSE, but, setting this option for it is safe)
58    check_set_compiler_property(APPEND PROPERTY fpsse2 "SHELL:-msse2 -mfpmath=sse")
59    zephyr_compile_options($<TARGET_PROPERTY:compiler,fpsse2>)
60    target_compile_options(native_simulator INTERFACE "$<TARGET_PROPERTY:compiler,fpsse2>")
61  endif ()
62endif()
63
64zephyr_compile_options(
65  ${ARCH_FLAG}
66  )
67
68if (CONFIG_NATIVE_APPLICATION)
69  zephyr_compile_options(
70    -include ${ZEPHYR_BASE}/arch/posix/include/posix_cheats.h
71  )
72elseif (CONFIG_NATIVE_LIBRARY)
73  zephyr_compile_options(
74    -fvisibility=hidden
75  )
76
77  # While doing the partial linking of the native library, some symbols will be missing
78  # which are provided by the native simulator runner
79  zephyr_ld_options(
80    -Wl,--unresolved-symbols=ignore-all
81  )
82
83  if (NOT CONFIG_EXTERNAL_LIBC)
84    # Get the *compiler* include path, that is where the *compiler* provided headers are (not the
85    # default libC ones). This includes basic headers like stdint.h, stddef.h or float.h
86    # We expect something like
87    #  /usr/lib/gcc/x86_64-linux-gnu/12/include or /usr/lib/llvm-14/lib/clang/14.0.0/include
88    execute_process(
89      COMMAND ${CMAKE_C_COMPILER} --print-file-name=include/stddef.h
90      OUTPUT_VARIABLE _OUTPUT
91      COMMAND_ERROR_IS_FATAL ANY
92    )
93    get_filename_component(COMPILER_OWN_INCLUDE_PATH "${_OUTPUT}" DIRECTORY)
94
95    # Do not use the C library from this compiler/host,
96    # but still use the basic compiler headers
97    # -fno-builtin to avoid the compiler using builtin replacements for std library functions
98    zephyr_compile_options(
99      -nostdinc
100      -isystem ${COMPILER_OWN_INCLUDE_PATH}
101      $<TARGET_PROPERTY:compiler,freestanding>
102      -fno-builtin
103    )
104  endif()
105endif()
106
107if(CONFIG_EXTERNAL_LIBC)
108  # @Intent: Obtain compiler specific flags for no freestanding compilation
109  zephyr_compile_options($<TARGET_PROPERTY:compiler,hosted>)
110endif()
111
112if(CONFIG_EXTERNAL_LIBCPP)
113  target_link_options(native_simulator INTERFACE "-lstdc++")
114endif()
115
116zephyr_include_directories(${BOARD_DIR})
117
118if(CONFIG_COVERAGE)
119  target_compile_options(native_simulator INTERFACE $<TARGET_PROPERTY:compiler,coverage>)
120  target_link_options(native_simulator INTERFACE $<TARGET_PROPERTY:linker,coverage>)
121endif()
122
123if (CONFIG_GPROF)
124  zephyr_compile_options($<TARGET_PROPERTY:compiler,gprof>)
125  zephyr_link_libraries($<TARGET_PROPERTY:linker,gprof>)
126
127  target_link_options(native_simulator INTERFACE "-pg")
128endif()
129
130zephyr_compile_definitions(_POSIX_C_SOURCE=200809 _XOPEN_SOURCE=600 _XOPEN_SOURCE_EXTENDED)
131
132if (CONFIG_NATIVE_APPLICATION)
133  zephyr_ld_options(
134    -ldl
135    -pthread
136  )
137endif()
138
139# About the -include directive: The reason to do it this way, is because in this
140# manner it is transparent to the application. Otherwise posix_cheats.h needs to
141# be included in all the applications' files which define main( ), and in any
142# app file which uses the pthreads like API provided by Zephyr
143# ( include/posix/pthread.h / kernel/pthread.c ) [And any future API added to
144# Zephyr which will clash with the native POSIX API] . It would also need to
145# be included in a few zephyr kernel files.
146
147#
148# Support for the LLVM Sanitizer toolchain instrumentation frameworks
149# (supported by current gcc's as well)
150#
151
152if(CONFIG_ASAN)
153  list(APPEND LLVM_SANITIZERS "address")
154endif()
155
156if(CONFIG_MSAN)
157  list(APPEND LLVM_SANITIZERS "memory")
158endif()
159
160if(CONFIG_UBSAN)
161  list(APPEND LLVM_SANITIZERS "undefined")
162endif()
163
164if(CONFIG_ASAN_RECOVER)
165  zephyr_compile_options(-fsanitize-recover=all)
166endif()
167
168if(CONFIG_ARCH_POSIX_LIBFUZZER)
169  list(APPEND LLVM_SANITIZERS "fuzzer")
170  if(NOT CONFIG_64BIT)
171    # On i386, libfuzzer seems to dynamically relocate the binary, so
172    # we need to emit PIC code.  This limitation is undocumented and
173    # poorly understood...
174    zephyr_compile_options(-fPIC)
175  endif()
176endif()
177
178list(JOIN LLVM_SANITIZERS "," LLVM_SANITIZERS_ARG)
179if(NOT ${LLVM_SANITIZERS_ARG} STREQUAL "")
180  set(LLVM_SANITIZERS_ARG "-fsanitize=${LLVM_SANITIZERS_ARG}")
181  zephyr_compile_options("${LLVM_SANITIZERS_ARG}")
182  zephyr_link_libraries("${LLVM_SANITIZERS_ARG}")
183
184  target_link_options(native_simulator INTERFACE ${LLVM_SANITIZERS_ARG})
185  target_compile_options(native_simulator INTERFACE ${LLVM_SANITIZERS_ARG})
186endif()
187
188# Override the C standard used for compilation to C 2011
189# This is due to some tests using _Static_assert which is a 2011 feature, but
190# otherwise relying on compilers supporting it also when set to C99.
191# This was in general ok, but with some host compilers and C library versions
192# it led to problems. So we override it to 2011 for native_posix.
193set_property(GLOBAL PROPERTY CSTD c11)
194
195add_subdirectory(core)
196