Merge branch 'bugfix/idf_as_lib_cmakelists' into 'release/v4.4'

Build: fix idf_as_lib example and toolchain CMake files (v4.4)

See merge request espressif/esp-idf!18589
This commit is contained in:
Jiang Jiang Jian
2022-07-01 10:44:30 +08:00
23 changed files with 171 additions and 34 deletions

View File

@ -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.
@ -184,6 +181,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)

View File

@ -2,21 +2,24 @@ cmake_minimum_required(VERSION 3.5)
project(idf_as_lib C)
if("${TARGET}" STREQUAL "esp32")
set(targets "esp32" "esp32s2" "esp32s3" "esp32c3" "esp32h2")
if("${TARGET}" IN_LIST targets)
# Include for ESP-IDF build system functions
include($ENV{IDF_PATH}/tools/cmake/idf.cmake)
# Create idf::esp32 and idf::freertos static libraries
idf_build_process(esp32
# Create idf::{target} and idf::freertos static libraries
idf_build_process("${TARGET}"
# try and trim the build; additional components
# will be included as needed based on dependency tree
#
# although esptool_py does not generate static library,
# processing the component is needed for flashing related
# targets and file generation
COMPONENTS esp32 freertos esptool_py
COMPONENTS "${TARGET}" freertos esptool_py
SDKCONFIG ${CMAKE_CURRENT_LIST_DIR}/sdkconfig
BUILD_DIR ${CMAKE_BINARY_DIR})
else()
message(WARNING "Unknown target ${TARGET}, creating stubs for esp32 instead")
# Create stubs for esp32 and freertos, stub::esp32 and stub::freertos
add_subdirectory(stubs/esp32)
add_subdirectory(stubs/freertos)
@ -29,8 +32,8 @@ set(elf_file ${CMAKE_PROJECT_NAME}.elf)
add_executable(${elf_file} main.c)
# Link the static libraries to the executable
if("${TARGET}" STREQUAL "esp32")
target_link_libraries(${elf_file} idf::esp32 idf::freertos idf::spi_flash)
if("${TARGET}" IN_LIST targets)
target_link_libraries(${elf_file} "idf::${TARGET}" idf::freertos idf::spi_flash)
# Attach additional targets to the executable file for flashing,
# linker script generation, partition_table generation, etc.
idf_build_executable(${elf_file})

View File

@ -1,4 +0,0 @@
#!/usr/bin/env bash
rm -rf build && mkdir build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32 -GNinja
cmake --build .

View File

@ -0,0 +1 @@
build.sh

View File

@ -0,0 +1 @@
build.sh

View File

@ -0,0 +1 @@
build.sh

View File

@ -0,0 +1 @@
build.sh

View File

@ -0,0 +1 @@
build.sh

View File

@ -1,4 +1,14 @@
#!/usr/bin/env bash
# Retrive the target from the current filename, if no target specified,
# the variable will be empty
TARGET=$(echo $0 | cut -s -f2 -d- | cut -s -f1 -d.)
if [[ -n $TARGET ]]
then
# Target is not null, specify the build parameters
PARAM="-DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-${TARGET}.cmake -DTARGET=${TARGET} -GNinja"
fi
rm -rf build && mkdir build && cd build
cmake ..
cmake .. $PARAM
cmake --build .

View File

@ -0,0 +1 @@
run-esp32.sh

View File

@ -0,0 +1 @@
run-esp32.sh

View File

@ -0,0 +1 @@
run-esp32.sh

View File

@ -0,0 +1 @@
run-esp32.sh

View File

@ -22,8 +22,16 @@ components/spiffs/test_spiffsgen/test_spiffsgen.py
components/ulp/esp32ulp_mapgen.py
docs/check_lang_folder_sync.sh
examples/build_system/cmake/idf_as_lib/build-esp32.sh
examples/build_system/cmake/idf_as_lib/build-esp32c3.sh
examples/build_system/cmake/idf_as_lib/build-esp32h2.sh
examples/build_system/cmake/idf_as_lib/build-esp32s2.sh
examples/build_system/cmake/idf_as_lib/build-esp32s3.sh
examples/build_system/cmake/idf_as_lib/build.sh
examples/build_system/cmake/idf_as_lib/run-esp32.sh
examples/build_system/cmake/idf_as_lib/run-esp32c3.sh
examples/build_system/cmake/idf_as_lib/run-esp32h2.sh
examples/build_system/cmake/idf_as_lib/run-esp32s2.sh
examples/build_system/cmake/idf_as_lib/run-esp32s3.sh
examples/build_system/cmake/idf_as_lib/run.sh
examples/storage/parttool/parttool_example.py
examples/storage/parttool/parttool_example.sh

View File

@ -354,6 +354,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

View File

@ -442,12 +442,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

View File

@ -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,19 @@ 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")
remove_duplicated_flags("--target=xtensa -mcpu=esp32 -ffreestanding ${CMAKE_C_FLAGS}"
UNIQ_CMAKE_C_FLAGS)
set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}"
CACHE STRING "C Compiler Base Flags"
FORCE)
remove_duplicated_flags("--target=xtensa -mcpu=esp32 -ffreestanding ${CMAKE_CXX_FLAGS}"
UNIQ_CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}"
CACHE STRING "C++ Compiler Base Flags"
FORCE)
remove_duplicated_flags("--target=xtensa -mcpu=esp32 ${CMAKE_ASM_FLAGS}" UNIQ_CMAKE_ASM_FLAGS)
set(CMAKE_ASM_FLAGS "${UNIQ_CMAKE_ASM_FLAGS}"
CACHE STRING "Assembler Base Flags"
FORCE)

View File

@ -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,20 @@ 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")
remove_duplicated_flags("--target=xtensa -mcpu=esp32s2 -ffreestanding ${CMAKE_C_FLAGS}"
UNIQ_CMAKE_C_FLAGS)
set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}"
CACHE STRING "C Compiler Base Flags"
FORCE)
remove_duplicated_flags("--target=xtensa -mcpu=esp32s2 -ffreestanding ${CMAKE_CXX_FLAGS}"
UNIQ_CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}"
CACHE STRING "C++ Compiler Base Flags"
FORCE)
remove_duplicated_flags("--target=xtensa -mcpu=esp32s2 ${CMAKE_ASM_FLAGS}"
UNIQ_CMAKE_ASM_FLAGS)
set(CMAKE_ASM_FLAGS "${UNIQ_CMAKE_ASM_FLAGS}"
CACHE STRING "Assembler Base Flags"
FORCE)

View File

@ -1,8 +1,15 @@
include($ENV{IDF_PATH}/tools/cmake/utilities.cmake)
set(CMAKE_SYSTEM_NAME Generic)
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")
remove_duplicated_flags("-mlongcalls -Wno-frame-address ${CMAKE_C_FLAGS}" UNIQ_CMAKE_C_FLAGS)
set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}" CACHE STRING "C Compiler Base Flags" FORCE)
remove_duplicated_flags("-mlongcalls -Wno-frame-address ${CMAKE_CXX_FLAGS}" UNIQ_CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}" CACHE STRING "C++ Compiler Base Flags" FORCE)
remove_duplicated_flags("-mlongcalls ${CMAKE_ASM_FLAGS}" UNIQ_CMAKE_ASM_FLAGS)
set(CMAKE_ASM_FLAGS "${UNIQ_CMAKE_ASM_FLAGS}" CACHE STRING "ASM Compiler Base Flags" FORCE)

View File

@ -1,9 +1,19 @@
include($ENV{IDF_PATH}/tools/cmake/utilities.cmake)
set(CMAKE_SYSTEM_NAME Generic)
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")
remove_duplicated_flags("-march=rv32imc ${CMAKE_C_FLAGS}"
UNIQ_CMAKE_C_FLAGS)
set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}" CACHE STRING "C Compiler Base Flags" FORCE)
remove_duplicated_flags("-march=rv32imc ${CMAKE_CXX_FLAGS}"
UNIQ_CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}" CACHE STRING "C++ Compiler Base Flags" FORCE)
remove_duplicated_flags("-nostartfiles -march=rv32imc --specs=nosys.specs \
${CMAKE_EXE_LINKER_FLAGS}"
UNIQ_CMAKE_SAFE_EXE_LINKER_FLAGS)
set(CMAKE_EXE_LINKER_FLAGS "${UNIQ_CMAKE_SAFE_EXE_LINKER_FLAGS}" CACHE STRING "Linker Base Flags" FORCE)

View File

@ -1,9 +1,17 @@
include($ENV{IDF_PATH}/tools/cmake/utilities.cmake)
set(CMAKE_SYSTEM_NAME Generic)
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")
remove_duplicated_flags("-march=rv32imc ${CMAKE_C_FLAGS}" UNIQ_CMAKE_C_FLAGS)
set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}" CACHE STRING "C Compiler Base Flags" FORCE)
remove_duplicated_flags("-march=rv32imc ${CMAKE_CXX_FLAGS}" UNIQ_CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}" CACHE STRING "C++ Compiler Base Flags" FORCE)
remove_duplicated_flags("-nostartfiles -march=rv32imc --specs=nosys.specs \
${CMAKE_EXE_LINKER_FLAGS}"
UNIQ_CMAKE_SAFE_EXE_LINKER_FLAGS)
set(CMAKE_EXE_LINKER_FLAGS "${UNIQ_CMAKE_SAFE_EXE_LINKER_FLAGS}" CACHE STRING "Linker Base Flags" FORCE)

View File

@ -1,8 +1,14 @@
include($ENV{IDF_PATH}/tools/cmake/utilities.cmake)
set(CMAKE_SYSTEM_NAME Generic)
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")
remove_duplicated_flags("-mlongcalls ${CMAKE_C_FLAGS}" UNIQ_CMAKE_C_FLAGS)
set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}" CACHE STRING "C Compiler Base Flags" FORCE)
remove_duplicated_flags("-mlongcalls ${CMAKE_CXX_FLAGS}" UNIQ_CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}" CACHE STRING "C++ Compiler Base Flags" FORCE)
remove_duplicated_flags("-mlongcalls ${CMAKE_ASM_FLAGS}" UNIQ_CMAKE_ASM_FLAGS)
set(CMAKE_ASM_FLAGS "${UNIQ_CMAKE_ASM_FLAGS}" CACHE STRING "ASM Compiler Base Flags" FORCE)

View File

@ -1,8 +1,14 @@
include($ENV{IDF_PATH}/tools/cmake/utilities.cmake)
set(CMAKE_SYSTEM_NAME Generic)
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")
remove_duplicated_flags("-mlongcalls ${CMAKE_C_FLAGS}" UNIQ_CMAKE_C_FLAGS)
set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}" CACHE STRING "C Compiler Base Flags" FORCE)
remove_duplicated_flags("-mlongcalls ${CMAKE_CXX_FLAGS}" UNIQ_CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}" CACHE STRING "C++ Compiler Base Flags" FORCE)
remove_duplicated_flags("-mlongcalls ${CMAKE_ASM_FLAGS}" UNIQ_CMAKE_ASM_FLAGS)
set(CMAKE_ASM_FLAGS "${UNIQ_CMAKE_ASM_FLAGS}" CACHE STRING "ASM Compiler Base Flags" FORCE)

View File

@ -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($<$<COMPILE_LANGUAGE:ASM>:${option}>)
endforeach()
endfunction()
# add_prebuild_library
#
@ -353,3 +364,17 @@ function(add_deprecated_target_alias old_target new_target)
)
add_dependencies(${old_target} ${new_target})
endfunction()
# Remove duplicates from a string containing compilation flags
function(remove_duplicated_flags FLAGS UNIQFLAGS)
set(FLAGS_LIST "${FLAGS}")
# Convert the given flags, as a string, into a CMake list type
separate_arguments(FLAGS_LIST)
# Remove all the duplicated flags
list(REMOVE_DUPLICATES FLAGS_LIST)
# Convert the list back to a string
string(REPLACE ";" " " FLAGS_LIST "${FLAGS_LIST}")
# Return that string to the caller
set(${UNIQFLAGS} "${FLAGS_LIST}" PARENT_SCOPE)
endfunction()