Skip to content

Commit d3819c9

Browse files
committed
espressif: allow the use of a different toolchain for building
TOOLCHAIN_BIN_DIR can be defined for a different toolchain use. Signed-off-by: Almir Okato <[email protected]>
1 parent 9b92ee9 commit d3819c9

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

boot/espressif/CMakeLists.txt

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ if (NOT DEFINED MCUBOOT_TARGET)
1111
message(FATAL_ERROR "MCUBOOT_TARGET not defined. Please pass -DMCUBOOT_TARGET flag.")
1212
endif()
1313

14-
project(mcuboot_${MCUBOOT_TARGET})
15-
1614
add_definitions(-DMCUBOOT_TARGET=${MCUBOOT_TARGET})
1715
add_definitions(-D__ESPRESSIF__=1)
1816

@@ -27,6 +25,41 @@ elseif("${MCUBOOT_TARGET}" STREQUAL "esp32c3" OR
2725
set(MCUBOOT_ARCH "riscv")
2826
endif()
2927

28+
if (NOT DEFINED CMAKE_TOOLCHAIN_FILE)
29+
if (DEFINED TOOLCHAIN_BIN_DIR)
30+
message("CMAKE_TOOLCHAIN_FILE not defined, searching for toolchain compiler in TOOLCHAIN_BIN_DIR: ${TOOLCHAIN_BIN_DIR}")
31+
set(CMAKE_SYSTEM_NAME Generic)
32+
33+
file(GLOB C_COMPILER_BIN "${TOOLCHAIN_BIN_DIR}/*${MCUBOOT_ARCH}*elf-gcc")
34+
if (NOT C_COMPILER_BIN)
35+
message(FATAL_ERROR "No C compiler found. Please ensure that TOOLCHAIN_BIN_DIR directory contains a set of C compiling tools compatible with the target")
36+
endif()
37+
set(CMAKE_C_COMPILER ${C_COMPILER_BIN})
38+
set(CMAKE_ASM_COMPILER ${C_COMPILER_BIN})
39+
message("C compiler found: ${CMAKE_C_COMPILER}")
40+
41+
file(GLOB CXX_COMPILER_BIN "${TOOLCHAIN_BIN_DIR}/*${MCUBOOT_ARCH}*elf-g++")
42+
if (NOT CXX_COMPILER_BIN)
43+
message(FATAL_ERROR "No C++ compiler found. Please ensure that TOOLCHAIN_BIN_DIR directory contains a set of C++ compiling tools compatible with the target")
44+
endif()
45+
set(CMAKE_CXX_COMPILER ${CXX_COMPILER_BIN})
46+
message("CXX compiler found: ${CMAKE_CXX_COMPILER}")
47+
else()
48+
# Set toolchain file that expect the same toolchain as IDF sets on PATH
49+
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_LIST_DIR}/tools/toolchain-${MCUBOOT_TARGET}.cmake)
50+
message("No user-defined toolchain, setting default toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
51+
endif()
52+
53+
# This flag is needed when redefining a different compiler toolchain at this point
54+
# on CMakeLists, the reason is that CMake does a compiler testing prior to building
55+
# that may fail due to cross-compilation
56+
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
57+
else()
58+
message("CMAKE_TOOLCHAIN_FILE: ${CMAKE_TOOLCHAIN_FILE}")
59+
endif()
60+
61+
project(mcuboot_${MCUBOOT_TARGET})
62+
3063
# Set the minimum revision for each supported chip
3164
if ("${MCUBOOT_TARGET}" STREQUAL "esp32")
3265
set(ESP_MIN_REVISION 3)
@@ -50,10 +83,12 @@ if (NOT DEFINED ESP_HAL_PATH)
5083
if (DEFINED ENV{ESP_HAL_PATH})
5184
set(ESP_HAL_PATH $ENV{ESP_HAL_PATH})
5285
else()
53-
message(WARNING "ESP_HAL_PATH not found. Please set -DESP_HAL_PATH parameter or define ESP_HAL_PATH environment variable.")
86+
message(WARNING "ESP_HAL_PATH not defined, checking if IDF_PATH exists.")
5487
if (DEFINED ENV{IDF_PATH})
5588
set(ESP_HAL_PATH $ENV{IDF_PATH})
5689
message("IDF installation found in the system, using IDF_PATH as ESP_HAL_PATH.")
90+
else ()
91+
message(FATAL_ERROR "Please set -DESP_HAL_PATH parameter or define ESP_HAL_PATH environment variable.")
5792
endif()
5893
endif()
5994
endif()

docs/readme-espressif.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ Additional configuration related to MCUboot features and slot partitioning may b
107107

108108
*If using ESP-IDF as HAL layer source, `ESP_HAL_PATH` can be ommited.*
109109

110+
*If desirable, `<TOOLCHAIN_BIN_DIR>` can be defined with the path for a different compatible
111+
toolchain, however it is recommended to actually create a CMake toolchain file and
112+
pass it through `<CMAKE_TOOLCHAIN_FILE>` variable since it may require a distinct set of
113+
compilation flags.*
114+
110115
---
111116

112117
2. Flash MCUboot in your device:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
espressif: allow the use of a different toolchain for building

0 commit comments

Comments
 (0)