Skip to content

Commit

Permalink
cmake:bugfix CMake compilation options settings should not use strings
Browse files Browse the repository at this point in the history
when repeatedly enabling and disabling string-controlled configurations,
the generated toolchain configuration may be incorrect.

Signed-off-by: xuxin19 <[email protected]>
  • Loading branch information
xuxin930 authored and acassis committed May 30, 2024
1 parent fde641f commit 275ec71
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 139 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 10 additions & 20 deletions arch/arm/src/cmake/Toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)

if(CONFIG_CXX_STANDARD)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=${CONFIG_CXX_STANDARD}>)
endif()

if(NOT CONFIG_LIBCXXTOOLCHAIN)
set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++")
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
endif()

if(NOT CONFIG_CXX_EXCEPTION)
string(APPEND ARCHCXXFLAGS " -fno-exceptions -fcheck-new")
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-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($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
endif()

if(CONFIG_ARCH_TOOLCHAIN_CLANG)
Expand Down
16 changes: 14 additions & 2 deletions arch/arm/src/cmake/platform.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" 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})

Expand Down
49 changes: 19 additions & 30 deletions arch/arm64/src/cmake/Toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<$<COMPILE_LANGUAGE:C>:-Werror>
$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)

if(NOT CONFIG_LIBCXXTOOLCHAIN)
set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++")
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-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($<$<COMPILE_LANGUAGE:CXX>:-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($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>)
endif()

if(NOT ${CONFIG_CXX_RTTI})
string(APPEND ARCHCXXFLAGS " -fno-rtti")
if(NOT CONFIG_CXX_RTTI)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
endif()

add_link_options(-nostdlib)
Expand Down Expand Up @@ -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()
32 changes: 24 additions & 8 deletions arch/arm64/src/cmake/platform.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,54 @@
# 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 "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" 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)
list(APPEND EXTRA_LIB ${extra_library})
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)
list(APPEND EXTRA_LIB ${extra_library})
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)
47 changes: 17 additions & 30 deletions arch/risc-v/src/cmake/Toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)

if(NOT CONFIG_LIBCXXTOOLCHAIN)
set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++")
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-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($<$<COMPILE_LANGUAGE:CXX>:-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($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>)
endif()

if(NOT ${CONFIG_CXX_RTTI})
string(APPEND ARCHCXXFLAGS " -fno-rtti")
if(NOT CONFIG_CXX_RTTI)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
endif()

if(CONFIG_ARCH_RV32)
Expand All @@ -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)

Expand Down
16 changes: 14 additions & 2 deletions arch/risc-v/src/cmake/platform.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" 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})

Expand Down
25 changes: 5 additions & 20 deletions arch/sim/src/cmake/Toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,17 @@ if(CONFIG_CXX_STANDARD)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=${CONFIG_CXX_STANDARD}>)
endif()

set(ARCHCFLAGS "-Wstrict-prototypes")
add_compile_options($<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)

if(NOT CONFIG_LIBCXXTOOLCHAIN)
set(ARCHCXXFLAGS "${ARCHCXXFLAGS} -nostdinc++")
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
endif()

if(NOT CONFIG_CXX_EXCEPTION)
string(APPEND ARCHCXXFLAGS " -fno-exceptions -fcheck-new")
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-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($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
endif()
Loading

0 comments on commit 275ec71

Please sign in to comment.