diff --git a/arch/sim/CMakeLists.txt b/arch/sim/CMakeLists.txt index ded850e13a593..2f448c8135d33 100644 --- a/arch/sim/CMakeLists.txt +++ b/arch/sim/CMakeLists.txt @@ -25,113 +25,6 @@ add_subdirectory(src) target_include_directories(nuttx PRIVATE ${CONFIG_ARCH_CHIP}) -target_compile_definitions(nuttx - PRIVATE CONFIG_USEC_PER_TICK=${CONFIG_USEC_PER_TICK}) - -if(APPLE) - target_compile_options(nuttx PRIVATE -Wno-deprecated-declarations) -endif() - -# configure simulated nuttx ################################################## - -if(NOT WIN32) - # Add -fvisibility=hidden Because we don't want export nuttx's symbols to - # shared libraries - - set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_COMPILE_OPTIONS -fvisibility=hidden) - - # Add -fno-common because macOS "ld -r" doesn't seem to pick objects for - # common symbols. - - set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_COMPILE_OPTIONS -fno-common) -else() - set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_COMPILE_OPTIONS /std:c11 /experimental:c11atomics) -endif() - -if(CONFIG_SIM_SANITIZE) - set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_COMPILE_OPTIONS -fsanitize=address -fsanitize=undefined - -fno-omit-frame-pointer) -endif() - -set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_COMPILE_OPTIONS - # NuttX is sometimes built as a native target. In that case, the - # __NuttX__ macro is predefined by the compiler. - # https://github.com/NuttX/buildroot - # - # In other cases, __NuttX__ is an ordinary user-definded macro. It's - # especially the case for NuttX sim, which is a target to run the - # entire NuttX as a program on the host OS, which can be Linux, - # macOS, Windows, etc. - # https://cwiki.apache.org/confluence/display/NUTTX/NuttX+Simulation - # In that case, the host OS compiler is used to build NuttX. Thus, - # eg. NuttX sim on macOS is built with __APPLE__. We #undef - # predefined macros for those possible host OSes here because the OS - # APIs this library should use are of NuttX, not the host OS. - -U_AIX - -U_WIN32 - -U__APPLE__ - -U__FreeBSD__ - -U__NetBSD__ - -U__linux__ - -U__sun__ - -U__unix__ - -U__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) - -# common options ############################################################# - -if(X86_64 AND CONFIG_SIM_M32) - set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_COMPILE_OPTIONS -m32) - target_compile_options(nuttx PRIVATE -m32) - target_link_options(nuttx PRIVATE -m32) -endif() - -if(CONFIG_LIBCXX) - if(APPLE) - # macOS uses libc++abi - - set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_DEFINITIONS LIBCXX_BUILDING_LIBCXXABI) - else() - # Linux C++ ABI seems vary. Probably __GLIBCXX__ is the best bet. XXX what - # to do for windows? - set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_DEFINITIONS __GLIBCXX__) - endif() - - # Disable availability macros. The availability on Host OS is not likely - # appropriate for NuttX. - # - # Note: When compiling NuttX apps, we undefine __APPLE__. It makes libcxx - # __availability header unhappy. - # https://github.com/llvm/llvm-project/blob/2e2999cd44f6ec9a5e396fa0113497ea82582f69/libcxx/include/__availability#L258 - set_property( - TARGET nuttx - APPEND - PROPERTY NUTTX_DEFINITIONS _LIBCPP_DISABLE_AVAILABILITY) -endif() - # This is part of the top-level export target TODO: how to deal with in CMake? # export_startup: board/libboard$(LIBEXT) up_head.o $(HOSTOBJS) nuttx-names.dat diff --git a/arch/sim/src/cmake/Toolchain.cmake b/arch/sim/src/cmake/Toolchain.cmake index c5f4eff60b5b6..16890e1ce0336 100644 --- a/arch/sim/src/cmake/Toolchain.cmake +++ b/arch/sim/src/cmake/Toolchain.cmake @@ -18,6 +18,31 @@ # # ############################################################################## +if(APPLE) + find_program(CMAKE_C_ELF_COMPILER x86_64-elf-gcc) + find_program(CMAKE_CXX_ELF_COMPILER x86_64-elf-g++) +endif() + +if(WIN32) + return() +endif() + +find_program(CMAKE_C_COMPILER gcc) +find_program(CMAKE_CXX_COMPILER g++) + +set(CMAKE_PREPROCESSOR cc -E -P -x c) + +# NuttX is sometimes built as a native target. In that case, the __NuttX__ macro +# is predefined by the compiler. https://github.com/NuttX/buildroot +# +# In other cases, __NuttX__ is an ordinary user-definded macro. It's especially +# the case for NuttX sim, which is a target to run the entire NuttX as a program +# on the host OS, which can be Linux, macOS, Windows, etc. +# https://cwiki.apache.org/confluence/display/NUTTX/NuttX+Simulation In that +# case, the host OS compiler is used to build NuttX. Thus, eg. NuttX sim on +# macOS is built with __APPLE__. We #undef predefined macros for those possible +# host OSes here because the OS APIs this library should use are of NuttX, not +# the host OS. add_compile_options( -U_AIX -U_WIN32 @@ -29,15 +54,6 @@ add_compile_options( -U__unix__ -U__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) -if(APPLE) - find_program(CMAKE_C_ELF_COMPILER x86_64-elf-gcc) - find_program(CMAKE_CXX_ELF_COMPILER x86_64-elf-g++) -endif() - -if(WIN32) - return() -endif() - set(NO_LTO "-fno-lto") if(CONFIG_DEBUG_SYMBOLS) @@ -72,18 +88,10 @@ if(CONFIG_STACK_USAGE_WARNING) add_compile_options(-Wstack-usage=${CONFIG_STACK_USAGE_WARNING}) endif() -if(CONFIG_ARCH_COVERAGE) - add_compile_options(-fprofile-generate -ftest-coverage) -endif() - if(CONFIG_SIM_GCOV_ALL) add_compile_options(-fprofile-generate -ftest-coverage) endif() -if(CONFIG_SCHED_GPROF_ALL OR CONFIG_SIM_GPROF) - add_compile_options(-pg) -endif() - if(CONFIG_SIM_ASAN) add_compile_options(-fsanitize=address) add_link_options(-fsanitize=address) @@ -126,18 +134,28 @@ if(CONFIG_ARCH_INSTRUMENT_ALL) add_compile_options(-finstrument-functions) endif() -add_compile_options( - -fno-common - -fvisibility=hidden - -ffunction-sections - -fdata-sections - -Wall - -Wshadow - -Wundef - -Wno-attributes - -Wno-unknown-pragmas - $<$:-Wstrict-prototypes> - $<$:-nostdinc++>) +if(NOT WIN32) + # Add -fvisibility=hidden Because we don't want export nuttx's symbols to + # shared libraries Add -fno-common because macOS "ld -r" doesn't seem to pick + # objects for common symbols. + add_compile_options( + -fno-common + -fvisibility=hidden + -ffunction-sections + -fdata-sections + -Wall + -Wshadow + -Wundef + -Wno-attributes + -Wno-unknown-pragmas + $<$:-Wstrict-prototypes>) +else() + add_compile_options(/std:c11 /experimental:c11atomics) +endif() + +if(APPLE) + add_compile_options(-Wno-deprecated-declarations) +endif() if(CONFIG_CXX_STANDARD) add_compile_options($<$:-std=${CONFIG_CXX_STANDARD}>) @@ -152,6 +170,10 @@ if(NOT CONFIG_CXX_RTTI) add_compile_options($<$:-fno-rtti>) endif() +if(NOT CONFIG_LIBCXXTOOLCHAIN) + add_compile_options($<$:-nostdinc++>) +endif() + if(CONFIG_SIM_M32) add_compile_options(-m32) add_link_options(-m32)