diff --git a/CMakeLists.txt b/CMakeLists.txt index b0b9713ad6..f06ae54ead 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,11 +7,8 @@ if(CMAKE_CURRENT_LIST_DIR STREQUAL CMAKE_SOURCE_DIR) "again.") endif() -unset(compile_options) -unset(c_compile_options) -unset(cxx_compile_options) -unset(compile_definitions) -unset(link_options) +# Variables compile_options, c_compile_options, cxx_compile_options, compile_definitions, link_options shall +# not be unset as they may already contain flags, set by toolchain-TARGET.cmake files. # Add the following build specifications here, since these seem to be dependent # on config values on the root Kconfig. @@ -215,6 +212,7 @@ endif() idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND) idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND) idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND) +idf_build_set_property(ASM_COMPILE_OPTIONS "${asm_compile_options}" APPEND) idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND) idf_build_set_property(LINK_OPTIONS "${link_options}" APPEND) diff --git a/tools/ci/test_build_system_cmake.sh b/tools/ci/test_build_system_cmake.sh index d455f75102..849404a293 100755 --- a/tools/ci/test_build_system_cmake.sh +++ b/tools/ci/test_build_system_cmake.sh @@ -351,6 +351,24 @@ function run_tests() rm sdkconfig rm sdkconfig.defaults + print_status "Compiler flags on build command line are taken into account" + clean_build_dir + # Backup original source file + cp main/main.c main/main.c.bak + # Alter source file to check user flag + echo -e "\n#ifndef USER_FLAG \n \ +#error \"USER_FLAG is not defined!\" \n \ +#endif\n" >> main/main.c + idf.py build -DCMAKE_C_FLAGS=-DUSER_FLAG || failure "User flags should have been taken into account" + # Restore original file + mv main/main.c.bak main/main.c + + print_status "Compiler flags cannot be overwritten" + clean_build_dir + # If the compiler flags are overriden, the following build command will + # cause issues at link time. + idf.py build -DCMAKE_C_FLAGS= -DCMAKE_CXX_FLAGS= || failure "CMake compiler flags have been overriden" + # the next tests use the esp32s2 target export other_target=esp32s2 diff --git a/tools/cmake/component.cmake b/tools/cmake/component.cmake index 25ed8e7d52..5e3053dba5 100644 --- a/tools/cmake/component.cmake +++ b/tools/cmake/component.cmake @@ -446,12 +446,14 @@ function(idf_component_register) idf_build_get_property(compile_options COMPILE_OPTIONS GENERATOR_EXPRESSION) idf_build_get_property(c_compile_options C_COMPILE_OPTIONS GENERATOR_EXPRESSION) idf_build_get_property(cxx_compile_options CXX_COMPILE_OPTIONS GENERATOR_EXPRESSION) + idf_build_get_property(asm_compile_options ASM_COMPILE_OPTIONS GENERATOR_EXPRESSION) idf_build_get_property(common_reqs ___COMPONENT_REQUIRES_COMMON) include_directories("${include_directories}") add_compile_options("${compile_options}") add_c_compile_options("${c_compile_options}") add_cxx_compile_options("${cxx_compile_options}") + add_asm_compile_options("${asm_compile_options}") # Unfortunately add_definitions() does not support generator expressions. A new command # add_compile_definition() does but is only available on CMake 3.12 or newer. This uses diff --git a/tools/cmake/toolchain-clang-esp32.cmake b/tools/cmake/toolchain-clang-esp32.cmake index bb25f98298..42fdbe15dd 100644 --- a/tools/cmake/toolchain-clang-esp32.cmake +++ b/tools/cmake/toolchain-clang-esp32.cmake @@ -1,3 +1,5 @@ +include($ENV{IDF_PATH}/tools/cmake/utilities.cmake) + set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_C_COMPILER clang) @@ -11,6 +13,6 @@ set(CMAKE_OBJDUMP xtensa-esp32-elf-objdump) # -freestanding is a hack to force Clang to use its own stdatomic.h, # without falling back to the (incompatible) GCC stdatomic.h # https://github.com/espressif/llvm-project/blob/d9341b81/clang/lib/Headers/stdatomic.h#L13-L18 -set(CMAKE_C_FLAGS "--target=xtensa -mcpu=esp32 -ffreestanding" CACHE STRING "C Compiler Base Flags") -set(CMAKE_CXX_FLAGS "--target=xtensa -mcpu=esp32 -ffreestanding" CACHE STRING "C++ Compiler Base Flags") -set(CMAKE_ASM_FLAGS "--target=xtensa -mcpu=esp32" CACHE STRING "Assembler Base Flags") +list(APPEND c_compile_options "--target=xtensa" "-mcpu=esp32" "-ffreestanding") +list(APPEND cxx_compile_options "--target=xtensa" "-mcpu=esp32" "-ffreestanding") +list(APPEND asm_compile_options "--target=xtensa" "-mcpu=esp32") diff --git a/tools/cmake/toolchain-clang-esp32s2.cmake b/tools/cmake/toolchain-clang-esp32s2.cmake index bb6e47bd0c..6449ef485e 100644 --- a/tools/cmake/toolchain-clang-esp32s2.cmake +++ b/tools/cmake/toolchain-clang-esp32s2.cmake @@ -11,6 +11,6 @@ set(CMAKE_OBJDUMP xtensa-esp32-elf-objdump) # -freestanding is a hack to force Clang to use its own stdatomic.h, # without falling back to the (incompatible) GCC stdatomic.h # https://github.com/espressif/llvm-project/blob/d9341b81/clang/lib/Headers/stdatomic.h#L13-L18 -set(CMAKE_C_FLAGS "--target=xtensa -mcpu=esp32s2 -ffreestanding" CACHE STRING "C Compiler Base Flags") -set(CMAKE_CXX_FLAGS "--target=xtensa -mcpu=esp32s2 -ffreestanding" CACHE STRING "C++ Compiler Base Flags") -set(CMAKE_ASM_FLAGS "--target=xtensa -mcpu=esp32s2" CACHE STRING "Assembler Base Flags") +list(APPEND c_compile_options "--target=xtensa" "-mcpu=esp32s2" "-ffreestanding") +list(APPEND cxx_compile_options "--target=xtensa" "-mcpu=esp32s2" "-ffreestanding") +list(APPEND asm_compile_options "--target=xtensa" "-mcpu=esp32s2") diff --git a/tools/cmake/toolchain-esp32.cmake b/tools/cmake/toolchain-esp32.cmake index 8e979700a4..4819486608 100644 --- a/tools/cmake/toolchain-esp32.cmake +++ b/tools/cmake/toolchain-esp32.cmake @@ -4,5 +4,4 @@ set(CMAKE_C_COMPILER xtensa-esp32-elf-gcc) set(CMAKE_CXX_COMPILER xtensa-esp32-elf-g++) set(CMAKE_ASM_COMPILER xtensa-esp32-elf-gcc) -set(CMAKE_C_FLAGS "-mlongcalls -Wno-frame-address" CACHE STRING "C Compiler Base Flags") -set(CMAKE_CXX_FLAGS "-mlongcalls -Wno-frame-address" CACHE STRING "C++ Compiler Base Flags") +list(APPEND compile_options "-mlongcalls" "-Wno-frame-address") diff --git a/tools/cmake/toolchain-esp32c3.cmake b/tools/cmake/toolchain-esp32c3.cmake index cd30a2ae04..e02cc1b796 100644 --- a/tools/cmake/toolchain-esp32c3.cmake +++ b/tools/cmake/toolchain-esp32c3.cmake @@ -4,6 +4,10 @@ set(CMAKE_C_COMPILER riscv32-esp-elf-gcc) set(CMAKE_CXX_COMPILER riscv32-esp-elf-g++) set(CMAKE_ASM_COMPILER riscv32-esp-elf-gcc) -set(CMAKE_C_FLAGS "-march=rv32imc" CACHE STRING "C Compiler Base Flags") -set(CMAKE_CXX_FLAGS "-march=rv32imc" CACHE STRING "C++ Compiler Base Flags") -set(CMAKE_EXE_LINKER_FLAGS "-nostartfiles -march=rv32imc --specs=nosys.specs" CACHE STRING "Linker Base Flags") +list(APPEND compile_options "-march=rv32imc") + +# Option `--specs` must only be defined ONCE in the final linker command, else GCC will complain: +# "attempt to rename spec 'link_gcc_c_sequence' to already defined spec 'nosys_link_gcc_c_sequence'" +# so unset `link_options` first. +unset(link_options) +list(APPEND link_options "-nostartfiles" "-march=rv32imc" "--specs=nosys.specs") diff --git a/tools/cmake/toolchain-esp32h2.cmake b/tools/cmake/toolchain-esp32h2.cmake index cd30a2ae04..e02cc1b796 100644 --- a/tools/cmake/toolchain-esp32h2.cmake +++ b/tools/cmake/toolchain-esp32h2.cmake @@ -4,6 +4,10 @@ set(CMAKE_C_COMPILER riscv32-esp-elf-gcc) set(CMAKE_CXX_COMPILER riscv32-esp-elf-g++) set(CMAKE_ASM_COMPILER riscv32-esp-elf-gcc) -set(CMAKE_C_FLAGS "-march=rv32imc" CACHE STRING "C Compiler Base Flags") -set(CMAKE_CXX_FLAGS "-march=rv32imc" CACHE STRING "C++ Compiler Base Flags") -set(CMAKE_EXE_LINKER_FLAGS "-nostartfiles -march=rv32imc --specs=nosys.specs" CACHE STRING "Linker Base Flags") +list(APPEND compile_options "-march=rv32imc") + +# Option `--specs` must only be defined ONCE in the final linker command, else GCC will complain: +# "attempt to rename spec 'link_gcc_c_sequence' to already defined spec 'nosys_link_gcc_c_sequence'" +# so unset `link_options` first. +unset(link_options) +list(APPEND link_options "-nostartfiles" "-march=rv32imc" "--specs=nosys.specs") diff --git a/tools/cmake/toolchain-esp32s2.cmake b/tools/cmake/toolchain-esp32s2.cmake index b776daacf9..52e7d52857 100644 --- a/tools/cmake/toolchain-esp32s2.cmake +++ b/tools/cmake/toolchain-esp32s2.cmake @@ -4,5 +4,5 @@ set(CMAKE_C_COMPILER xtensa-esp32s2-elf-gcc) set(CMAKE_CXX_COMPILER xtensa-esp32s2-elf-g++) set(CMAKE_ASM_COMPILER xtensa-esp32s2-elf-gcc) -set(CMAKE_C_FLAGS "-mlongcalls" CACHE STRING "C Compiler Base Flags") -set(CMAKE_CXX_FLAGS "-mlongcalls" CACHE STRING "C++ Compiler Base Flags") + +list(APPEND compile_options "-mlongcalls") diff --git a/tools/cmake/toolchain-esp32s3.cmake b/tools/cmake/toolchain-esp32s3.cmake index 279622b49e..ff7ea3d6c9 100644 --- a/tools/cmake/toolchain-esp32s3.cmake +++ b/tools/cmake/toolchain-esp32s3.cmake @@ -4,5 +4,4 @@ set(CMAKE_C_COMPILER xtensa-esp32s3-elf-gcc) set(CMAKE_CXX_COMPILER xtensa-esp32s3-elf-g++) set(CMAKE_ASM_COMPILER xtensa-esp32s3-elf-gcc) -set(CMAKE_C_FLAGS "-mlongcalls" CACHE STRING "C Compiler Base Flags") -set(CMAKE_CXX_FLAGS "-mlongcalls" CACHE STRING "C++ Compiler Base Flags") +list(APPEND compile_options "-mlongcalls") diff --git a/tools/cmake/toolchain-esp8684.cmake b/tools/cmake/toolchain-esp8684.cmake index cd30a2ae04..e02cc1b796 100644 --- a/tools/cmake/toolchain-esp8684.cmake +++ b/tools/cmake/toolchain-esp8684.cmake @@ -4,6 +4,10 @@ set(CMAKE_C_COMPILER riscv32-esp-elf-gcc) set(CMAKE_CXX_COMPILER riscv32-esp-elf-g++) set(CMAKE_ASM_COMPILER riscv32-esp-elf-gcc) -set(CMAKE_C_FLAGS "-march=rv32imc" CACHE STRING "C Compiler Base Flags") -set(CMAKE_CXX_FLAGS "-march=rv32imc" CACHE STRING "C++ Compiler Base Flags") -set(CMAKE_EXE_LINKER_FLAGS "-nostartfiles -march=rv32imc --specs=nosys.specs" CACHE STRING "Linker Base Flags") +list(APPEND compile_options "-march=rv32imc") + +# Option `--specs` must only be defined ONCE in the final linker command, else GCC will complain: +# "attempt to rename spec 'link_gcc_c_sequence' to already defined spec 'nosys_link_gcc_c_sequence'" +# so unset `link_options` first. +unset(link_options) +list(APPEND link_options "-nostartfiles" "-march=rv32imc" "--specs=nosys.specs") diff --git a/tools/cmake/utilities.cmake b/tools/cmake/utilities.cmake index 5a78e068b6..49342699d2 100644 --- a/tools/cmake/utilities.cmake +++ b/tools/cmake/utilities.cmake @@ -283,6 +283,17 @@ function(add_c_compile_options) endforeach() endfunction() +# add_compile_options variant for ASM code only +# +# This adds global options, set target properties for +# component-specific flags +function(add_asm_compile_options) + foreach(option ${ARGV}) + # note: the Visual Studio Generator doesn't support this... + add_compile_options($<$:${option}>) + endforeach() +endfunction() + # add_prebuild_library #