Skip to content

Commit ba4bd1e

Browse files
committed
feat: Adopt cmake for project build system generation
1 parent 3268116 commit ba4bd1e

File tree

7 files changed

+1912
-1
lines changed

7 files changed

+1912
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
src/build/
1+
build/

CMakeLists.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
3+
project(${TARGET_CHIP}.elf CXX)
4+
5+
set(DEFAULT_CHIP "esp32")
6+
option(TARGET_CHIP "Target ESP chip" ${DEFAULT_CHIP})
7+
8+
# TODO move these to esp-stub-lib
9+
set(LINKER_SCRIPTS_DIR "${CMAKE_SOURCE_DIR}/src/ld")
10+
11+
# CXX standard set based on the GCC version of the toolchain for ESP8266 (oldest one)
12+
set(CUSTOM_FLAGS --std=gnu++17 -Wall -Wextra -Werror -Wshadow -Wundef -Wconversion -Os -fno-common -mtext-section-literals -mlongcalls -nostdlib -fno-builtin -Wl,-static -g -ffunction-sections -Wl,--gc-sections)
13+
set(EXTRA_RISCV_FLAGS -march=rv32imc -mabi=ilp32 -msmall-data-limit=0)
14+
15+
set(ESP8266_FLAGS "-DESP8266")
16+
set(XTENSA_FLAGS "-DXTENSA")
17+
set(RISCV_FLAGS "-DRISCV" ${EXTRA_RISCV_FLAGS})
18+
set(ESP32_XTENSA_CHIPS esp32 esp32-s2 esp32-s3)
19+
20+
# TODO move this to esp-stub-lib's utility directory
21+
include(FetchContent)
22+
23+
function(download_toolchain TOOLCHAIN_NAME TOOLCHAIN_URL TOOLCHAIN_HASH)
24+
FetchContent_Declare(${TOOLCHAIN_NAME}
25+
URL ${TOOLCHAIN_URL}
26+
URL_HASH SHA256=${TOOLCHAIN_HASH}
27+
)
28+
29+
message(STATUS "Downloading ${TOOLCHAIN_NAME} toolchain from ${TOOLCHAIN_URL}. This may take a while...")
30+
31+
FetchContent_MakeAvailable(${TOOLCHAIN_NAME})
32+
endfunction()
33+
34+
message(STATUS "Building for ${TARGET_CHIP}")
35+
add_executable(${PROJECT_NAME})
36+
37+
if(TARGET_CHIP IN_LIST ESP32_XTENSA_CHIPS)
38+
set(CHIP_TOOLCHAIN_URL "https://github.com/espressif/crosstool-NG/releases/download/esp-14.2.0_20241119/xtensa-esp-elf-14.2.0_20241119-x86_64-linux-gnu.tar.gz")
39+
set(TOOLCHAIN_NAME "xtensa_toolchain")
40+
set(TOOLCHAIN_EXECUTABLE_PREFIX "xtensa-${TARGET_CHIP}-elf-")
41+
download_toolchain(${TOOLCHAIN_NAME} ${CHIP_TOOLCHAIN_URL} "b1859df334a85541ae746e1b86439f59180d87f8cf1cc04c2e770fadf9f006e9")
42+
set(CHIP_FLAGS "${XTENSA_FLAGS}")
43+
elseif(TARGET_CHIP STREQUAL "esp8266")
44+
set(CHIP_TOOLCHAIN_URL "https://dl.espressif.com/dl/xtensa-lx106-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz")
45+
set(TOOLCHAIN_NAME "esp8266_toolchain")
46+
set(TOOLCHAIN_EXECUTABLE_PREFIX "xtensa-lx106-elf-")
47+
download_toolchain(${TOOLCHAIN_NAME} ${CHIP_TOOLCHAIN_URL} "0a1804b5e2231c6db8b72af6bc2a0f9a5b6994cfba29956d412651109f13fe7e")
48+
set(CHIP_FLAGS "${ESP8266_FLAGS}")
49+
else()
50+
set(CHIP_TOOLCHAIN_URL "https://github.com/espressif/crosstool-NG/releases/download/esp-14.2.0_20241119/riscv32-esp-elf-14.2.0_20241119-x86_64-linux-gnu.tar.gz")
51+
set(TOOLCHAIN_NAME "riscv_toolchain")
52+
set(TOOLCHAIN_EXECUTABLE_PREFIX "riscv32-esp-elf-")
53+
download_toolchain(${TOOLCHAIN_NAME} ${CHIP_TOOLCHAIN_URL} "a16942465d33c7f0334c16e83bc6feb62e06eeb79cf19099293480bb8d48c0cd")
54+
set(CHIP_FLAGS "${RISCV_FLAGS}")
55+
endif()
56+
57+
set(CHIP_LINKER_SCRIPT "${LINKER_SCRIPTS_DIR}/${TARGET_CHIP}.ld")
58+
59+
target_compile_options(${PROJECT_NAME} PRIVATE
60+
${CUSTOM_FLAGS}
61+
${CHIP_FLAGS}
62+
"-T${CHIP_LINKER_SCRIPT}"
63+
)
64+
65+
target_link_options(${PROJECT_NAME} PRIVATE
66+
${CUSTOM_FLAGS}
67+
-Wl,-Map=${CMAKE_BINARY_DIR}/${TARGET_CHIP}.map -L${LINKER_SCRIPTS_DIR}
68+
)
69+
70+
# TODO move the toolchains out from the build directory
71+
set(TOOLCHAIN_SEARCH_PATH "${CMAKE_BINARY_DIR}/_deps/${TOOLCHAIN_NAME}-src/bin")
72+
find_program(COMPILER_PATH NAMES ${TOOLCHAIN_EXECUTABLE_PREFIX}g++ PATHS "${TOOLCHAIN_SEARCH_PATH}" REQUIRED NO_DEFAULT_PATH)
73+
set(CMAKE_CXX_COMPILER ${COMPILER_PATH})
74+
75+
add_subdirectory(${CMAKE_SOURCE_DIR}/src)

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ This project is experimental and not yet ready for production use.
66

77
The project's goal is to replace the legacy [flasher stub of esptool](https://github.com/espressif/esptool-legacy-flasher-stub/) in the near future.
88

9+
# How to Build
10+
11+
The project depends on [esp-stub-lib](https://github.com/espressif/esp-stub-lib/) in the form of git submodule. Don't forget to get/update the submodule as well before building:
12+
13+
```sh
14+
git submodule update --init --recursive
15+
mkdir -p build
16+
cd build
17+
cmake .. -G Ninja -DTARGET_CHIP=esp32-s2 # Replace with your desired chip, e.g. esp32, esp8266
18+
ninja
19+
```
20+
921
# How To Use
1022

1123
1. Install esptool in [development mode](https://docs.espressif.com/projects/esptool/en/latest/esp32/contributing.html#development-setup).

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
set(SOURCES "main.cpp")
3+
4+
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

src/ld/esp32.ld

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Note: stub is deliberately loaded close to the very top
2+
of available RAM, to reduce change of colliding with anything
3+
else... */
4+
MEMORY {
5+
iram : org = 0x400BE000, len = 0x1000
6+
dram : org = 0x3ffcc000, len = 0x14000
7+
}
8+
9+
ENTRY(esp_main)
10+
11+
SECTIONS {
12+
.text : ALIGN(4) {
13+
*(.literal)
14+
*(.text .text.*)
15+
} > iram
16+
17+
.bss : ALIGN(4) {
18+
_bss_start = ABSOLUTE(.);
19+
*(.bss)
20+
_bss_end = ABSOLUTE(.);
21+
} > dram
22+
23+
.data : ALIGN(4) {
24+
*(.data)
25+
*(.rodata .rodata.*)
26+
} > dram
27+
}
28+
29+
INCLUDE "esp32.rom.ld"

0 commit comments

Comments
 (0)