Merge branch 'feature/compiler_rt_support' into 'master'

build: Adds support for compiler-rt in Clang toolchain

See merge request espressif/esp-idf!21213
This commit is contained in:
Alexey Gerenkov 2023-06-09 04:41:34 +08:00
commit d4e1935ba8
23 changed files with 146 additions and 40 deletions

View File

@ -176,6 +176,15 @@ cache:
source ./export.sh
# Custom clang
if [[ ! -z "$CI_CLANG_DISTRO_URL" ]]; then
echo "Using custom clang from ${CI_CLANG_DISTRO_URL}"
wget $CI_CLANG_DISTRO_URL
ARCH_NAME=$(basename $CI_CLANG_DISTRO_URL)
tar -x -f $ARCH_NAME
export PATH=$PWD/esp-clang/bin:$PATH
fi
# Custom OpenOCD
if [[ ! -z "$OOCD_DISTRO_URL" && "$CI_JOB_STAGE" == "target_test" ]]; then
echo "Using custom OpenOCD from ${OOCD_DISTRO_URL}"

View File

@ -115,6 +115,8 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang")
list(APPEND compile_options "-Wno-c2x-extensions")
# warning on xMPU_SETTINGS for esp32s2 has size 0 for C and 1 for C++
list(APPEND compile_options "-Wno-extern-c-compat")
# warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1
list(APPEND compile_options "-Wno-single-bit-bitfield-constant-conversion")
endif()
# More warnings may exist in unit tests and example projects.
@ -222,6 +224,11 @@ endif()
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
list(APPEND compile_options "-fno-use-cxa-atexit")
if(CONFIG_COMPILER_RT_LIB_GCCLIB)
list(APPEND link_options "-rtlib=libgcc")
elseif(CONFIG_COMPILER_RT_LIB_CLANGRT)
list(APPEND link_options "-rtlib=compiler-rt")
endif()
endif()
# For the transition period from 32-bit time_t to 64-bit time_t,

33
Kconfig
View File

@ -19,6 +19,18 @@ mainmenu "Espressif IoT Development Framework Configuration"
bool
default y if "$(IDF_CI_BUILD)" = "y" || "$(IDF_CI_BUILD)" = 1
config IDF_TOOLCHAIN
# This option records the IDF target when sdkconfig is generated the first time.
# It is not updated if environment variable $IDF_TOOLCHAIN changes later, and
# the build system is responsible for detecting the mismatch between
# CONFIG_IDF_TOOLCHAIN and $IDF_TOOLCHAIN.
string
default "$IDF_TOOLCHAIN"
config IDF_TOOLCHAIN_CLANG
bool
default "y" if IDF_TOOLCHAIN="clang"
config IDF_TARGET_ARCH_RISCV
bool
default "n"
@ -493,6 +505,27 @@ mainmenu "Espressif IoT Development Framework Configuration"
If enabled, RTL files will be produced during compilation. These files
can be used by other tools, for example to calculate call graphs.
choice COMPILER_RT_LIB
prompt "Compiler runtime library"
default COMPILER_RT_LIB_CLANGRT if IDF_TOOLCHAIN_CLANG
default COMPILER_RT_LIB_GCCLIB
help
Select runtime library to be used by compiler.
- GCC toolchain supports libgcc only.
- Clang allows to choose between libgcc or libclang_rt.
config COMPILER_RT_LIB_GCCLIB
bool "libgcc"
config COMPILER_RT_LIB_CLANGRT
depends on IDF_TOOLCHAIN_CLANG
bool "libclang_rt"
endchoice
config COMPILER_RT_LIB_NAME
string
default "clang_rt.builtins" if COMPILER_RT_LIB_CLANGRT
default "gcc" if COMPILER_RT_LIB_GCCLIB
endmenu # Compiler Options
menu "Component config"

View File

@ -66,6 +66,12 @@ idf_component_register(SRCS "${srcs}"
idf_component_get_property(app_trace app_trace COMPONENT_LIB)
if(CONFIG_APPTRACE_GCOV_ENABLE)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
# Coverage info is not supported when clang is used
# TODO: LLVM-214
message(FATAL_ERROR "Coverage info is not supported when building with Clang!")
endif()
# The original Gcov library from toolchain will be objcopy with symbols redefinitions (see file gcov/io_sym.map).
# This needs because ESP has no file-system onboard, and redefined functions solves this problem and transmits
# output file to host PC.

View File

@ -42,7 +42,9 @@ SECTIONS
*(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
*(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
*liblog.a:(.literal .text .literal.* .text.*)
/* we use either libgcc or compiler-rt, so put similar entries for them here */
*libgcc.a:(.literal .text .literal.* .text.*)
*libclang_rt.builtins.a:(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*)

View File

@ -63,7 +63,9 @@ SECTIONS
*(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
*(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
*liblog.a:(.literal .text .literal.* .text.*)
/* we use either libgcc or compiler-rt, so put similar entries for them here */
*libgcc.a:(.literal .text .literal.* .text.*)
*libclang_rt.builtins.a:(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*)

View File

@ -63,7 +63,9 @@ SECTIONS
*(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
*(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
*liblog.a:(.literal .text .literal.* .text.*)
/* we use either libgcc or compiler-rt, so put similar entries for them here */
*libgcc.a:(.literal .text .literal.* .text.*)
*libclang_rt.builtins.a:(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*)

View File

@ -60,7 +60,9 @@ SECTIONS
*(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
*(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
*liblog.a:(.literal .text .literal.* .text.*)
/* we use either libgcc or compiler-rt, so put similar entries for them here */
*libgcc.a:(.literal .text .literal.* .text.*)
*libclang_rt.builtins.a:(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*)

View File

@ -61,7 +61,9 @@ SECTIONS
*(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
*(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
*liblog.a:(.literal .text .literal.* .text.*)
/* we use either libgcc or compiler-rt, so put similar entries for them here */
*libgcc.a:(.literal .text .literal.* .text.*)
*libclang_rt.builtins.a:(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*)

View File

@ -29,7 +29,9 @@ SECTIONS
*(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
*(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
*liblog.a:(.literal .text .literal.* .text.*)
/* we use either libgcc or compiler-rt, so put similar entries for them here */
*libgcc.a:(.literal .text .literal.* .text.*)
*libclang_rt.builtins.a:(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*)

View File

@ -64,7 +64,9 @@ SECTIONS
*(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
*(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
*liblog.a:(.literal .text .literal.* .text.*)
/* we use either libgcc or compiler-rt, so put similar entries for them here */
*libgcc.a:(.literal .text .literal.* .text.*)
*libclang_rt.builtins.a:(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*)
*libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*)

View File

@ -43,7 +43,7 @@ endif()
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
# libstdc++ depends on C library, so it should appear later in link order.
# Otherwise we get undefined references for esp-clang toolchain.
target_link_libraries(${COMPONENT_LIB} PUBLIC stdc++ c gcc)
target_link_libraries(${COMPONENT_LIB} PUBLIC stdc++ c ${CONFIG_COMPILER_RT_LIB_NAME})
else()
target_link_libraries(${COMPONENT_LIB} PUBLIC stdc++ gcc)
endif()
@ -63,7 +63,7 @@ else()
endif()
target_link_libraries(${COMPONENT_LIB} PUBLIC stdcpp_pthread)
add_library(libgcc_cxx INTERFACE)
target_link_libraries(libgcc_cxx INTERFACE gcc $<TARGET_FILE:${cxx}>)
target_link_libraries(libgcc_cxx INTERFACE ${CONFIG_COMPILER_RT_LIB_NAME} $<TARGET_FILE:${cxx}>)
target_link_libraries(${COMPONENT_LIB} PUBLIC libgcc_cxx)
if(NOT CONFIG_COMPILER_CXX_EXCEPTIONS)

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -115,24 +115,14 @@ esp_err_t esp_efuse_utility_burn_chip(void)
if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
uint8_t block_rs[12];
efuse_hal_rs_calculate((void *)range_write_addr_blocks[num_block].start, block_rs);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#pragma GCC diagnostic ignored "-Warray-bounds"
memcpy((void *)EFUSE_PGM_CHECK_VALUE0_REG, block_rs, sizeof(block_rs));
#pragma GCC diagnostic pop
hal_memcpy((void *)EFUSE_PGM_CHECK_VALUE0_REG, block_rs, sizeof(block_rs));
}
unsigned r_data_len = (range_read_addr_blocks[num_block].end - range_read_addr_blocks[num_block].start) + sizeof(uint32_t);
unsigned data_len = (range_write_addr_blocks[num_block].end - range_write_addr_blocks[num_block].start) + sizeof(uint32_t);
memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)range_write_addr_blocks[num_block].start, data_len);
uint32_t backup_write_data[8 + 3]; // 8 words are data and 3 words are RS coding data
#pragma GCC diagnostic push
#if __GNUC__ >= 11
#pragma GCC diagnostic ignored "-Wstringop-overread"
#endif
#pragma GCC diagnostic ignored "-Warray-bounds"
memcpy(backup_write_data, (void *)EFUSE_PGM_DATA0_REG, sizeof(backup_write_data));
#pragma GCC diagnostic pop
hal_memcpy(backup_write_data, (void *)EFUSE_PGM_DATA0_REG, sizeof(backup_write_data));
int repeat_burn_op = 1;
bool correct_written_data;
bool coding_error_before = efuse_hal_is_coding_error_in_block(num_block);
@ -161,11 +151,7 @@ esp_err_t esp_efuse_utility_burn_chip(void)
correct_written_data = esp_efuse_utility_is_correct_written_data(num_block, r_data_len);
if (!correct_written_data || coding_error_occurred) {
ESP_LOGW(TAG, "BLOCK%d: next retry to fix an error [%d/3]...", num_block, repeat_burn_op);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#pragma GCC diagnostic ignored "-Warray-bounds"
memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)backup_write_data, sizeof(backup_write_data));
#pragma GCC diagnostic pop
hal_memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)backup_write_data, sizeof(backup_write_data));
}
} while ((!correct_written_data || coding_error_occurred) && repeat_burn_op++ < 3);

View File

@ -36,7 +36,7 @@ idf_component_register(SRCS "${srcs}"
# Toolchain libraries require code defined in this component
idf_component_get_property(newlib newlib COMPONENT_LIB)
target_link_libraries(${COMPONENT_LIB} INTERFACE c m gcc "$<TARGET_FILE:${newlib}>")
target_link_libraries(${COMPONENT_LIB} INTERFACE c m ${CONFIG_COMPILER_RT_LIB_NAME} "$<TARGET_FILE:${newlib}>")
set_source_files_properties(heap.c PROPERTIES COMPILE_FLAGS -fno-builtin)

View File

@ -7,6 +7,15 @@ entries:
if IDF_TARGET_ARCH_RISCV:
save-restore (noflash)
[mapping:clang_rt_builtins]
archive: libclang_rt.builtins.a
entries:
if IDF_TARGET_ESP32 = n:
_divsf3 (noflash)
if IDF_TARGET_ARCH_RISCV:
save (noflash)
restore (noflash)
[mapping:gcov]
archive: libgcov.a
entries:

View File

@ -1,3 +1,4 @@
libc
sha256_coredump
gcc
clang_rt_builtins

View File

@ -468,6 +468,8 @@ macro(idf_build_process target)
idf_build_set_property(BOOTLOADER_BUILD "${BOOTLOADER_BUILD}")
idf_build_set_property(IDF_TOOLCHAIN "${IDF_TOOLCHAIN}")
# Check build target is specified. Since this target corresponds to a component
# name, the target component is automatically added to the list of common component
# requirements.

View File

@ -94,6 +94,7 @@ function(__kconfig_generate_config sdkconfig sdkconfig_defaults)
idf_build_set_property(KCONFIG_PROJBUILDS "${kconfig_projbuilds}")
idf_build_get_property(idf_target IDF_TARGET)
idf_build_get_property(idf_toolchain IDF_TOOLCHAIN)
idf_build_get_property(idf_path IDF_PATH)
idf_build_get_property(idf_env_fpga __IDF_ENV_FPGA)
@ -209,6 +210,7 @@ function(__kconfig_generate_config sdkconfig sdkconfig_defaults)
COMMAND ${prepare_kconfig_files_command}
COMMAND ${kconfgen_basecommand}
--env "IDF_TARGET=${idf_target}"
--env "IDF_TOOLCHAIN=${idf_toolchain}"
--env "IDF_ENV_FPGA=${idf_env_fpga}"
--dont-write-deprecated
--output config ${sdkconfig}
@ -218,6 +220,7 @@ function(__kconfig_generate_config sdkconfig sdkconfig_defaults)
"COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE=${kconfigs_projbuild_path}"
"KCONFIG_CONFIG=${sdkconfig}"
"IDF_TARGET=${idf_target}"
"IDF_TOOLCHAIN=${idf_toolchain}"
"IDF_ENV_FPGA=${idf_env_fpga}"
${MENUCONFIG_CMD} ${root_kconfig}
USES_TERMINAL
@ -225,6 +228,7 @@ function(__kconfig_generate_config sdkconfig sdkconfig_defaults)
# compatibility)
COMMAND ${kconfgen_basecommand}
--env "IDF_TARGET=${idf_target}"
--env "IDF_TOOLCHAIN=${idf_toolchain}"
--env "IDF_ENV_FPGA=${idf_env_fpga}"
--output config ${sdkconfig}
)

View File

@ -0,0 +1,32 @@
include($ENV{IDF_PATH}/tools/cmake/utilities.cmake)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_AR llvm-ar)
set(CMAKE_RANLIB llvm-ranlib)
set(CMAKE_OBJDUMP riscv32-esp-elf-objdump)
remove_duplicated_flags("--target=riscv32-esp-elf -march=rv32imc -mabi=ilp32 \
${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=riscv32-esp-elf -march=rv32imc -mabi=ilp32 \
${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=riscv32-esp-elf -march=rv32imc -mabi=ilp32 \
${CMAKE_ASM_FLAGS}"
UNIQ_CMAKE_ASM_FLAGS)
set(CMAKE_ASM_FLAGS "${UNIQ_CMAKE_ASM_FLAGS}"
CACHE STRING "Assembler Base Flags"
FORCE)

View File

@ -3,6 +3,7 @@
"COMPONENT_KCONFIGS_PROJBUILD": "${kconfig_projbuilds}",
"COMPONENT_SDKCONFIG_RENAMES": "${sdkconfig_renames}",
"IDF_TARGET": "${idf_target}",
"IDF_TOOLCHAIN": "${idf_toolchain}",
"IDF_ENV_FPGA": "${idf_env_fpga}",
"IDF_PATH": "${idf_path}",
"COMPONENT_KCONFIGS_SOURCE_FILE": "${kconfigs_path}",

View File

@ -0,0 +1 @@
CONFIG_COMPILER_RT_LIB_CLANGRT=y

View File

@ -0,0 +1 @@
CONFIG_COMPILER_RT_LIB_GCCLIB=y

View File

@ -401,36 +401,36 @@
"versions": [
{
"linux-amd64": {
"sha256": "839e5adfa7f44982e8a2d828680f6e4aa435dcd3d1df765e02f015b04286056f",
"size": 209731340,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-15.0.0-20221201/llvm-esp-15.0.0-20221201-linux-amd64.tar.xz"
"sha256": "3dbd8dd290913a93e8941da8a451ecd49f9798cc2d74bb9b63ef5cf5c4fee37f",
"size": 215176120,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-16.0.0-20230516/llvm-esp-16.0.0-20230516-linux-amd64.tar.xz"
},
"linux-arm64": {
"sha256": "614c44ab7305d65dde54a884c5614516777038027dc61bcc125d02171c248c53",
"size": 220076792,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-15.0.0-20221201/llvm-esp-15.0.0-20221201-linux-arm64.tar.xz"
"sha256": "4b115af6ddd04a9bffc1908fc05837998ee71d450891d741c446186f2aa9b961",
"size": 222261932,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-16.0.0-20230516/llvm-esp-16.0.0-20230516-linux-arm64.tar.xz"
},
"linux-armhf": {
"sha256": "158076696e4fc608e6e2b54bf739223b78949e0492ad4aa5119632ebfbea0499",
"size": 209791724,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-15.0.0-20221201/llvm-esp-15.0.0-20221201-linux-armhf.tar.xz"
"sha256": "935082bb0704420c5ca42b35038bba8702135348a50cac454ae2fb55af0b4c32",
"size": 214888520,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-16.0.0-20230516/llvm-esp-16.0.0-20230516-linux-armhf.tar.xz"
},
"macos": {
"sha256": "46f0f0368b5aa8d7e81558796c3acd67d943c9071b9619f2b487136c8e59c97c",
"size": 177703432,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-15.0.0-20221201/llvm-esp-15.0.0-20221201-macos.tar.xz"
"sha256": "d9824acafd3e7b1d17ace084243b82a95bbdcb149a26b085bba487ab3d3716d7",
"size": 182440672,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-16.0.0-20230516/llvm-esp-16.0.0-20230516-macos.tar.xz"
},
"macos-arm64": {
"sha256": "dc5a99186f9f532a5076d6900828310e4673cf01e8071a3d041456e8aab2cc4a",
"size": 167630856,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-15.0.0-20221201/llvm-esp-15.0.0-20221201-macos-arm64.tar.xz"
"sha256": "ed5621396dc3e48413e14e8b6caed8e2993e7f2ab5fca1410081f40c940a1060",
"size": 171912324,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-16.0.0-20230516/llvm-esp-16.0.0-20230516-macos-arm64.tar.xz"
},
"name": "15.0.0-23786128ae",
"name": "16.0.1-fe4f10a809",
"status": "recommended",
"win64": {
"sha256": "87c9b2c2b8837535f102ae3fd5789defecbffa80b317f86055f3e9d6292aaa05",
"size": 241235020,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-15.0.0-20221201/llvm-esp-15.0.0-20221201-win64.tar.xz"
"sha256": "598c8241c8bf10fd1be8bd21845307cfc404e127041b4ba4e828350a88692883",
"size": 243979484,
"url": "https://github.com/espressif/llvm-project/releases/download/esp-16.0.0-20230516/llvm-esp-16.0.0-20230516-win64.tar.xz"
}
}
]