From 275ec7102c24fc25e3a4c4a67f9a528f23d940fa Mon Sep 17 00:00:00 2001 From: xuxin19 Date: Wed, 29 May 2024 15:57:16 +0800 Subject: [PATCH] cmake:bugfix CMake compilation options settings should not use strings when repeatedly enabling and disabling string-controlled configurations, the generated toolchain configuration may be incorrect. Signed-off-by: xuxin19 --- CMakeLists.txt | 5 ++- arch/arm/src/cmake/Toolchain.cmake | 30 ++++++---------- arch/arm/src/cmake/platform.cmake | 16 +++++++-- arch/arm64/src/cmake/Toolchain.cmake | 49 +++++++++++---------------- arch/arm64/src/cmake/platform.cmake | 32 ++++++++++++----- arch/risc-v/src/cmake/Toolchain.cmake | 47 ++++++++++--------------- arch/risc-v/src/cmake/platform.cmake | 16 +++++++-- arch/sim/src/cmake/Toolchain.cmake | 25 +++----------- arch/x86_64/src/cmake/Toolchain.cmake | 41 +++++++++++----------- arch/x86_64/src/cmake/platform.cmake | 16 +++++++-- 10 files changed, 138 insertions(+), 139 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dba8ace983..1e0f194fec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -622,9 +622,8 @@ if(NOT CONFIG_ARCH_SIM) # TODO: nostart/nodefault not applicable to nuttx toolchain target_link_libraries( - nuttx - PRIVATE ${NUTTX_EXTRA_FLAGS} -Wl,--script=${ldscript} -Wl,--start-group - ${nuttx_libs} ${nuttx_extra_libs} -Wl,--end-group) + nuttx PRIVATE -Wl,--script=${ldscript} -Wl,--start-group ${nuttx_libs} + ${nuttx_extra_libs} -Wl,--end-group) # generate binary outputs in different formats (.bin, .hex, etc) nuttx_generate_outputs(nuttx) diff --git a/arch/arm/src/cmake/Toolchain.cmake b/arch/arm/src/cmake/Toolchain.cmake index 4c55953543..0db1429891 100644 --- a/arch/arm/src/cmake/Toolchain.cmake +++ b/arch/arm/src/cmake/Toolchain.cmake @@ -183,34 +183,24 @@ if(CONFIG_DEBUG_SYMBOLS) add_compile_options(-g) endif() -set(ARCHCFLAGS "-Wstrict-prototypes") +add_compile_options(-Wno-attributes -Wno-unknown-pragmas + $<$:-Wstrict-prototypes>) + +if(CONFIG_CXX_STANDARD) + add_compile_options($<$:-std=${CONFIG_CXX_STANDARD}>) +endif() if(NOT CONFIG_LIBCXXTOOLCHAIN) - set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++") + add_compile_options($<$:-nostdinc++>) endif() if(NOT CONFIG_CXX_EXCEPTION) - string(APPEND ARCHCXXFLAGS " -fno-exceptions -fcheck-new") + add_compile_options($<$:-fno-exceptions> + $<$:-fcheck-new>) endif() if(NOT CONFIG_CXX_RTTI) - string(APPEND ARCHCXXFLAGS " -fno-rtti") -endif() - -if(NOT "${CMAKE_C_FLAGS}" STREQUAL "") - string(REGEX MATCH "${ARCHCFLAGS}" EXISTS_FLAGS "${CMAKE_C_FLAGS}") -endif() - -if(NOT EXISTS_FLAGS) - set(CMAKE_ASM_FLAGS - "${CMAKE_ASM_FLAGS} ${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_C_FLAGS - "${CMAKE_C_FLAGS} ${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} ${ARCHCXXFLAGS}" - CACHE STRING "" FORCE) + add_compile_options($<$:-fno-rtti>) endif() if(CONFIG_ARCH_TOOLCHAIN_CLANG) diff --git a/arch/arm/src/cmake/platform.cmake b/arch/arm/src/cmake/platform.cmake index 3806f6f17f..e435d5c48c 100644 --- a/arch/arm/src/cmake/platform.cmake +++ b/arch/arm/src/cmake/platform.cmake @@ -56,8 +56,20 @@ else() message(FATAL_ERROR "CMAKE_SYSTEM_PROCESSOR not set") endif() -get_directory_property(NUTTX_EXTRA_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} - COMPILE_OPTIONS) +get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} + COMPILE_OPTIONS) + +set(NUTTX_EXTRA_FLAGS "") +foreach(FLAG ${TOOLCHAIN_DIR_FLAGS}) + if(NOT FLAG MATCHES "^\\$<.*>$") + list(APPEND NUTTX_EXTRA_FLAGS ${FLAG}) + else() + string(REGEX MATCH "\\$<\\$:(.*)>" matched ${FLAG}) + if(matched) + list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1}) + endif() + endif() +endforeach() separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS}) diff --git a/arch/arm64/src/cmake/Toolchain.cmake b/arch/arm64/src/cmake/Toolchain.cmake index 233172e68b..9d579d26e6 100644 --- a/arch/arm64/src/cmake/Toolchain.cmake +++ b/arch/arm64/src/cmake/Toolchain.cmake @@ -119,31 +119,35 @@ if(CONFIG_ARCH_FPU) add_compile_options(-D_LDBL_EQ_DBL) endif() -set(ARCHCFLAGS - "-Wstrict-prototypes -fno-common -Wall -Wshadow -Werror -Wundef -Wno-attributes -Wno-unknown-pragmas" -) -set(ARCHCXXFLAGS - "-fno-common -Wall -Wshadow -Wundef -Wno-attributes -Wno-unknown-pragmas") +add_compile_options( + -fno-common + -Wall + -Wshadow + -Wundef + -Wno-attributes + -Wno-unknown-pragmas + $<$:-Werror> + $<$:-Wstrict-prototypes>) if(NOT CONFIG_LIBCXXTOOLCHAIN) - set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++") + add_compile_options($<$:-nostdinc++>) endif() -if(NOT ${CONFIG_ARCH_TOOLCHAIN_CLANG}) - string(APPEND ARCHCFLAGS " -Wno-psabi") - string(APPEND ARCHCXXFLAGS " -Wno-psabi") +if(NOT CONFIG_ARCH_TOOLCHAIN_CLANG) + add_compile_options(-Wno-psabi) endif() -if(${CONFIG_CXX_STANDARD}) - string(APPEND ARCHCXXFLAGS " -std=${CONFIG_CXX_STANDARD}") +if(CONFIG_CXX_STANDARD) + add_compile_options($<$:-std=${CONFIG_CXX_STANDARD}>) endif() -if(NOT ${CONFIG_CXX_EXCEPTION}) - string(APPEND ARCHCXXFLAGS " -fno-exceptions -fcheck-new") +if(NOT CONFIG_CXX_EXCEPTION) + add_compile_options($<$:-fno-exceptions> + $<$:-fcheck-new>) endif() -if(NOT ${CONFIG_CXX_RTTI}) - string(APPEND ARCHCXXFLAGS " -fno-rtti") +if(NOT CONFIG_CXX_RTTI) + add_compile_options($<$:-fno-rtti>) endif() add_link_options(-nostdlib) @@ -176,18 +180,3 @@ if(CONFIG_ARCH_TOOLCHAIN_GNU) add_link_options(-Wl,--no-warn-rwx-segments) endif() endif() - -if(NOT "${CMAKE_C_FLAGS}" STREQUAL "") - string(REGEX MATCH "${ARCHCFLAGS}" EXISTS_FLAGS "${CMAKE_C_FLAGS}") -endif() -if(NOT EXISTS_FLAGS) - set(CMAKE_ASM_FLAGS - "${CMAKE_ASM_FLAGS} ${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_C_FLAGS - "${CMAKE_C_FLAGS} ${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} ${ARCHCXXFLAGS}" - CACHE STRING "" FORCE) -endif() diff --git a/arch/arm64/src/cmake/platform.cmake b/arch/arm64/src/cmake/platform.cmake index c8b3e61a95..92bead9260 100644 --- a/arch/arm64/src/cmake/platform.cmake +++ b/arch/arm64/src/cmake/platform.cmake @@ -17,17 +17,32 @@ # the License. # # ############################################################################## -get_directory_property(NUTTX_EXTRA_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} - COMPILE_OPTIONS) +get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} + COMPILE_OPTIONS) + +set(NUTTX_EXTRA_FLAGS "") +foreach(FLAG ${TOOLCHAIN_DIR_FLAGS}) + if(NOT FLAG MATCHES "^\\$<.*>$") + list(APPEND NUTTX_EXTRA_FLAGS ${FLAG}) + else() + string(REGEX MATCH "\\$<\\$:(.*)>" matched ${FLAG}) + if(matched) + list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1}) + endif() + endif() +endforeach() + +separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS}) + execute_process( - COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${NUTTX_EXTRA_FLAGS} + COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS} --print-libgcc-file-name OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE extra_library) list(APPEND EXTRA_LIB ${extra_library}) if(NOT CONFIG_LIBM) execute_process( - COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${NUTTX_EXTRA_FLAGS} + COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS} --print-file-name=libm.a OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE extra_library) @@ -35,7 +50,7 @@ if(NOT CONFIG_LIBM) endif() if(CONFIG_LIBSUPCXX) execute_process( - COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${NUTTX_EXTRA_FLAGS} + COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS} --print-file-name=libsupc++.a OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE extra_library) @@ -43,12 +58,13 @@ if(CONFIG_LIBSUPCXX) endif() if(CONFIG_ARCH_COVERAGE) execute_process( - COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${NUTTX_EXTRA_FLAGS} + COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS} --print-file-name=libgcov.a OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE extra_library) list(APPEND EXTRA_LIB ${extra_library}) endif() -set_property(GLOBAL APPEND PROPERTY NUTTX_EXTRA_LIBRARIES ${EXTRA_LIB}) -separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS}) + +nuttx_add_extra_library(${EXTRA_LIB}) + set(PREPROCES ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} -E -P -x c) diff --git a/arch/risc-v/src/cmake/Toolchain.cmake b/arch/risc-v/src/cmake/Toolchain.cmake index a72460c048..05de8ac833 100644 --- a/arch/risc-v/src/cmake/Toolchain.cmake +++ b/arch/risc-v/src/cmake/Toolchain.cmake @@ -150,31 +150,34 @@ if(CONFIG_ARCH_COVERAGE) add_compile_options(-fprofile-generate -ftest-coverage) endif() -set(ARCHCFLAGS - "-Wstrict-prototypes -fno-common -Wall -Wshadow -Wundef -Wno-attributes -Wno-unknown-pragmas" -) -set(ARCHCXXFLAGS - "-fno-common -Wall -Wshadow -Wundef -Wno-attributes -Wno-unknown-pragmas") +add_compile_options( + -fno-common + -Wall + -Wshadow + -Wundef + -Wno-attributes + -Wno-unknown-pragmas + $<$:-Wstrict-prototypes>) if(NOT CONFIG_LIBCXXTOOLCHAIN) - set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++") + add_compile_options($<$:-nostdinc++>) endif() if(NOT ${CONFIG_ARCH_TOOLCHAIN_CLANG}) - string(APPEND ARCHCFLAGS " -Wno-psabi") - string(APPEND ARCHCXXFLAGS " -Wno-psabi") + add_compile_options(-Wno-psabi) endif() -if(${CONFIG_CXX_STANDARD}) - string(APPEND ARCHCXXFLAGS " -std=${CONFIG_CXX_STANDARD}") +if(CONFIG_CXX_STANDARD) + add_compile_options($<$:-std=${CONFIG_CXX_STANDARD}>) endif() -if(NOT ${CONFIG_CXX_EXCEPTION}) - string(APPEND ARCHCXXFLAGS " -fno-exceptions -fcheck-new") +if(NOT CONFIG_CXX_EXCEPTION) + add_compile_options($<$:-fno-exceptions> + $<$:-fcheck-new>) endif() -if(NOT ${CONFIG_CXX_RTTI}) - string(APPEND ARCHCXXFLAGS " -fno-rtti") +if(NOT CONFIG_CXX_RTTI) + add_compile_options($<$:-fno-rtti>) endif() if(CONFIG_ARCH_RV32) @@ -200,22 +203,6 @@ if(CONFIG_DEBUG_SYMBOLS) add_compile_options(-g) endif() -if(NOT "${CMAKE_C_FLAGS}" STREQUAL "") - string(REGEX MATCH "${ARCHCFLAGS}" EXISTS_FLAGS "${CMAKE_C_FLAGS}") -endif() - -if(NOT EXISTS_FLAGS) - set(CMAKE_ASM_FLAGS - "${CMAKE_ASM_FLAGS} ${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_C_FLAGS - "${CMAKE_C_FLAGS} ${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} ${ARCHCXXFLAGS}" - CACHE STRING "" FORCE) -endif() - # Generic GNU RVG toolchain if(${CONFIG_RISCV_TOOLCHAIN} STREQUAL GNU_RVG) diff --git a/arch/risc-v/src/cmake/platform.cmake b/arch/risc-v/src/cmake/platform.cmake index 9f2f3fc9d6..90fbfcd155 100644 --- a/arch/risc-v/src/cmake/platform.cmake +++ b/arch/risc-v/src/cmake/platform.cmake @@ -18,8 +18,20 @@ # # ############################################################################## -get_directory_property(NUTTX_EXTRA_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} - COMPILE_OPTIONS) +get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} + COMPILE_OPTIONS) + +set(NUTTX_EXTRA_FLAGS "") +foreach(FLAG ${TOOLCHAIN_DIR_FLAGS}) + if(NOT FLAG MATCHES "^\\$<.*>$") + list(APPEND NUTTX_EXTRA_FLAGS ${FLAG}) + else() + string(REGEX MATCH "\\$<\\$:(.*)>" matched ${FLAG}) + if(matched) + list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1}) + endif() + endif() +endforeach() separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS}) diff --git a/arch/sim/src/cmake/Toolchain.cmake b/arch/sim/src/cmake/Toolchain.cmake index 3f7611f9ee..90aa92a33b 100644 --- a/arch/sim/src/cmake/Toolchain.cmake +++ b/arch/sim/src/cmake/Toolchain.cmake @@ -98,32 +98,17 @@ if(CONFIG_CXX_STANDARD) add_compile_options($<$:-std=${CONFIG_CXX_STANDARD}>) endif() -set(ARCHCFLAGS "-Wstrict-prototypes") +add_compile_options($<$:-Wstrict-prototypes>) if(NOT CONFIG_LIBCXXTOOLCHAIN) - set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++") + add_compile_options($<$:-nostdinc++>) endif() if(NOT CONFIG_CXX_EXCEPTION) - string(APPEND ARCHCXXFLAGS " -fno-exceptions -fcheck-new") + add_compile_options($<$:-fno-exceptions> + $<$:-fcheck-new>) endif() if(NOT CONFIG_CXX_RTTI) - string(APPEND ARCHCXXFLAGS " -fno-rtti") -endif() - -if(NOT "${CMAKE_C_FLAGS}" STREQUAL "") - string(REGEX MATCH "${ARCHCFLAGS}" EXISTS_FLAGS "${CMAKE_C_FLAGS}") -endif() - -if(NOT EXISTS_FLAGS) - set(CMAKE_ASM_FLAGS - "${CMAKE_ASM_FLAGS}${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_C_FLAGS - "${CMAKE_C_FLAGS}${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS}${ARCHCXXFLAGS}" - CACHE STRING "" FORCE) + add_compile_options($<$:-fno-rtti>) endif() diff --git a/arch/x86_64/src/cmake/Toolchain.cmake b/arch/x86_64/src/cmake/Toolchain.cmake index 1bd11bca4b..f28384815b 100644 --- a/arch/x86_64/src/cmake/Toolchain.cmake +++ b/arch/x86_64/src/cmake/Toolchain.cmake @@ -85,38 +85,35 @@ if(CONFIG_DEBUG_LINK_MAP) add_link_options(-Wl,--cref -Wl,-Map=nuttx.map) endif() -set(ARCHCFLAGS - "-Wstrict-prototypes -fno-common -Wall -Wshadow -Wundef -Wno-attributes -Wno-unknown-pragmas" -) -set(ARCHCXXFLAGS - "-fno-common -Wall -Wshadow -Wundef -Wno-attributes -Wno-unknown-pragmas") +add_compile_options( + -fno-common + -Wall + -Wshadow + -Wundef + -Wno-attributes + -Wno-unknown-pragmas + $<$:-Wstrict-prototypes>) + +if(CONFIG_CXX_STANDARD) + add_compile_options($<$:-std=${CONFIG_CXX_STANDARD}>) +endif() if(NOT CONFIG_LIBCXXTOOLCHAIN) - set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++") + add_compile_options($<$:-nostdinc++>) endif() if(NOT CONFIG_CXX_EXCEPTION) - string(APPEND ARCHCXXFLAGS " -fno-exceptions -fcheck-new") + add_compile_options($<$:-fno-exceptions> + $<$:-fcheck-new>) endif() if(NOT CONFIG_CXX_RTTI) - string(APPEND ARCHCXXFLAGS " -fno-rtti") + add_compile_options($<$:-fno-rtti>) endif() -if(NOT "${CMAKE_C_FLAGS}" STREQUAL "") - string(REGEX MATCH "${ARCHCFLAGS}" EXISTS_FLAGS "${CMAKE_C_FLAGS}") -endif() - -if(NOT EXISTS_FLAGS) - set(CMAKE_ASM_FLAGS - "${CMAKE_ASM_FLAGS} ${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_C_FLAGS - "${CMAKE_C_FLAGS} ${ARCHCFLAGS}" - CACHE STRING "" FORCE) - set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} ${ARCHCXXFLAGS}" - CACHE STRING "" FORCE) +if(CONFIG_DEBUG_OPT_UNUSED_SECTIONS) + add_link_options(-Wl,--gc-sections) + add_compile_options(-ffunction-sections -fdata-sections) endif() if(CONFIG_ARCH_INTEL64_HAVE_RDRAND) diff --git a/arch/x86_64/src/cmake/platform.cmake b/arch/x86_64/src/cmake/platform.cmake index 5e53d971d0..8c6b8b504c 100644 --- a/arch/x86_64/src/cmake/platform.cmake +++ b/arch/x86_64/src/cmake/platform.cmake @@ -18,8 +18,20 @@ # # ############################################################################## -get_directory_property(NUTTX_EXTRA_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} - COMPILE_OPTIONS) +get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR} + COMPILE_OPTIONS) + +set(NUTTX_EXTRA_FLAGS "") +foreach(FLAG ${TOOLCHAIN_DIR_FLAGS}) + if(NOT FLAG MATCHES "^\\$<.*>$") + list(APPEND NUTTX_EXTRA_FLAGS ${FLAG}) + else() + string(REGEX MATCH "\\$<\\$:(.*)>" matched ${FLAG}) + if(matched) + list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1}) + endif() + endif() +endforeach() execute_process( COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${NUTTX_EXTRA_FLAGS}