Skip to content

Commit a812780

Browse files
committed
feat: Adopt cmake for project build system generation
1 parent 49e917e commit a812780

16 files changed

+9360
-4
lines changed

.astyle-rules.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ submodules:
88
check: false
99
include:
1010
- "/esp-stub-lib/"
11+
12+
libraries:
13+
check: false
14+
include:
15+
- "src/miniz.*"

.check_copyright_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ ignore:
2121
perform_check: false
2222
include:
2323
- src/ld
24+
- src/miniz.*
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
name: Build and release
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
build_stubs:
8+
runs-on: ubuntu-24.04
9+
10+
steps:
11+
- name: Checkout ref commit
12+
uses: actions/checkout@v4
13+
with:
14+
submodules: 'recursive'
15+
16+
- name: Set up Python 3.13
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: 3.13
20+
21+
- name: Install Python dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install esptool
25+
26+
- name: Install toolchains
27+
shell: bash
28+
run: |
29+
mkdir -p toolchains
30+
pushd toolchains
31+
../tools/setup_toolchains.sh
32+
popd
33+
34+
- name: Build stub
35+
shell: bash
36+
run: |
37+
source ./tools/export_toolchains.sh
38+
./tools/build_all_chips.sh
39+
40+
- name: Upload stub JSONs
41+
uses: actions/upload-artifact@v4
42+
with:
43+
path: build*/esp*.json
44+
if-no-files-found: error
45+
retention-days: 3
46+
47+
create_release:
48+
name: Create GitHub release
49+
needs: build_stubs
50+
if: startsWith(github.ref, 'refs/tags/')
51+
runs-on: ubuntu-24.04
52+
permissions:
53+
contents: write
54+
steps:
55+
- name: Get version
56+
id: get_version
57+
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
58+
shell: bash
59+
- name: Checkout
60+
uses: actions/checkout@v4
61+
with:
62+
fetch-depth: 0
63+
- name: Set up Python 3.13
64+
uses: actions/setup-python@v5
65+
with:
66+
python-version: 3.13
67+
- name: Install dependencies
68+
run: |
69+
python -m pip install --upgrade pip
70+
pip install commitizen czespressif
71+
- name: Generate changelog
72+
run: |
73+
cz changelog ${{ steps.get_version.outputs.VERSION }} --file-name changelog_body.md
74+
cat changelog_body.md
75+
- name: Download stub JSONs
76+
uses: actions/download-artifact@v4
77+
- name: Create release
78+
id: create_release
79+
uses: softprops/action-gh-release@v2
80+
env:
81+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
82+
with:
83+
body_path: changelog_body.md
84+
name: Version ${{ steps.get_version.outputs.VERSION }}
85+
draft: true
86+
prerelease: false
87+
fail_on_unmatched_files: true
88+
files: artifact/esp*.json

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
src/build/
1+
build*/
2+
toolchains/

CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
3+
project(flasher-stub CXX)
4+
5+
option(TARGET_CHIP "Target ESP chip" "OFF")
6+
7+
if("${TARGET_CHIP}" STREQUAL "OFF")
8+
message(FATAL_ERROR "Please set target chip via -DTARGET_CHIP.")
9+
endif()
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 -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+
set(EXTRA_XTENSA_FLAGS -mtext-section-literals -mlongcalls)
15+
16+
set(ESP8266_FLAGS "-DESP8266" ${EXTRA_XTENSA_FLAGS})
17+
set(XTENSA_FLAGS "-DXTENSA" ${EXTRA_XTENSA_FLAGS})
18+
set(RISCV_FLAGS "-DRISCV" ${EXTRA_RISCV_FLAGS})
19+
20+
message(STATUS "Building for ${TARGET_CHIP}")
21+
add_executable(${TARGET_CHIP})
22+
23+
set(ESP32_XTENSA_CHIPS esp32 esp32s2 esp32s3)
24+
25+
if(TARGET_CHIP IN_LIST ESP32_XTENSA_CHIPS)
26+
set(TOOLCHAIN_EXECUTABLE_PREFIX "xtensa-${TARGET_CHIP}-elf-")
27+
set(CHIP_FLAGS "${XTENSA_FLAGS}")
28+
elseif(TARGET_CHIP STREQUAL "esp8266")
29+
set(TOOLCHAIN_EXECUTABLE_PREFIX "xtensa-lx106-elf-")
30+
set(CHIP_FLAGS "${ESP8266_FLAGS}")
31+
# The lx106 toolchain is of version 8.4 which doesn't support the --dependency-file linker option.
32+
set(CMAKE_LINK_DEPENDS_USE_LINKER FALSE) # requires CMake 3.27 or higher
33+
else()
34+
set(TOOLCHAIN_EXECUTABLE_PREFIX "riscv32-esp-elf-")
35+
set(CHIP_FLAGS "${RISCV_FLAGS}")
36+
endif()
37+
38+
set(LINKER_SCRIPTS_DIR "${CMAKE_SOURCE_DIR}/src/ld")
39+
set(CHIP_LINKER_SCRIPT "${LINKER_SCRIPTS_DIR}/${TARGET_CHIP}.ld")
40+
41+
target_compile_options(${TARGET_CHIP} PRIVATE
42+
${CUSTOM_FLAGS}
43+
${CHIP_FLAGS}
44+
)
45+
46+
target_link_options(${TARGET_CHIP} PRIVATE
47+
${CUSTOM_FLAGS}
48+
${CHIP_FLAGS}
49+
"-T${CHIP_LINKER_SCRIPT}"
50+
-Wl,-Map=${CMAKE_BINARY_DIR}/${TARGET_CHIP}.map
51+
)
52+
53+
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_EXECUTABLE_PREFIX}g++)
54+
set(CMAKE_LINKER ${TOOLCHAIN_EXECUTABLE_PREFIX}g++)
55+
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf")
56+
57+
add_subdirectory(${CMAKE_SOURCE_DIR}/src)

README.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,58 @@ 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 Use
9+
# Build Dependencies
10+
11+
### Submodules
12+
13+
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:
14+
15+
```sh
16+
git submodule update --init --recursive
17+
```
18+
19+
### Toolchains
20+
21+
You will need the following toolchains set up and available in your PATH:
22+
1. [`xtensa-lx106-elf-*`](https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/get-started/index.html#setup-toolchain)
23+
2. [`xtensa-*-elf-*`](https://github.com/espressif/crosstool-NG)
24+
3. [`riscv32-esp-elf-*`](https://github.com/espressif/crosstool-NG)
25+
26+
There is a convenience script for AMD64 Linux machines to download and install them into the `toolchains` directory:
27+
```sh
28+
mkdir -p toolchains
29+
cd toolchains
30+
../tools/setup_toolchains.sh
31+
```
32+
33+
Then run the following export script in every terminal where the project is used:
34+
```sh
35+
. ./tools/export_toolchains.sh
36+
```
37+
38+
# How to Build
39+
40+
### Build for one selected chip target
41+
42+
```sh
43+
mkdir -p build
44+
cmake . -B build -G Ninja -DTARGET_CHIP=esp32s2 # Replace with your desired chip, e.g. esp32, esp8266
45+
ninja -C build
46+
```
47+
48+
### Build for all supported chip targets
49+
50+
```sh
51+
./tools/build_all.sh
52+
```
53+
54+
# How To Use With Esptool
1055

1156
1. Install esptool in [development mode](https://docs.espressif.com/projects/esptool/en/latest/esp32/contributing.html#development-setup).
1257
2. Obtain the flasher stub binaries as JSON files either from the [releases page](https://github.com/espressif/esp-flasher-stub) or from the artifacts of your pull request.
1358
3. Replace the esptool's JSONs files in the `esptool/targets/stub_flasher` directory with the obtained JSON files.
1459

15-
## Contributing
60+
# Contributing
1661

1762
Please install the [pre-commit](https://pre-commit.com/) hooks to ensure that your commits are properly formatted:
1863

@@ -34,6 +79,6 @@ git push --tags
3479
```
3580
Create a pull request and edit the automatically created draft [release notes](https://github.com/espressif/esp-flasher-stub/releases).
3681

37-
## License
82+
# License
3883

3984
This document and the attached source code are released as Free Software under either the [Apache License Version 2](LICENSE-APACHE) or [MIT License](LICENSE-MIT) at your option.

src/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
set(COMMON_SOURCES "main.cpp")
2+
3+
target_sources(${TARGET_CHIP} PRIVATE ${COMMON_SOURCES})
4+
5+
if(${TARGET_CHIP} STREQUAL "esp8266")
6+
# zlib library will be available in esp-stub-lib. So miniz probably won't be needed
7+
8+
# -Wconversion produces a warning in miniz.cpp
9+
get_target_property(comp_opts esp8266 COMPILE_OPTIONS)
10+
list(REMOVE_ITEM comp_opts -Wconversion)
11+
12+
add_library(miniz_obj OBJECT miniz.cpp)
13+
target_compile_options(miniz_obj PRIVATE ${comp_opts})
14+
target_sources(esp8266 PRIVATE $<TARGET_OBJECTS:miniz_obj>)
15+
endif()

src/ld/esp32.ld

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

src/ld/esp32c3.ld

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
MEMORY {
2+
iram : org = 0x40380000, len = 0x4000
3+
dram : org = 0x3FC84000, len = 0x18000
4+
}
5+
6+
ENTRY(esp_main)
7+
8+
SECTIONS {
9+
.text : ALIGN(4) {
10+
*(.literal)
11+
*(.text .text.*)
12+
} > iram
13+
14+
.bss : ALIGN(4) {
15+
_bss_start = ABSOLUTE(.);
16+
*(.bss)
17+
_bss_end = ABSOLUTE(.);
18+
} > dram
19+
20+
.data : ALIGN(4) {
21+
*(.data)
22+
*(.rodata .rodata.*)
23+
} > dram
24+
}

src/ld/esp8266.ld

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 = 0x4010D000, len = 0x2100
6+
dram : org = 0x3FFE8100, len = 0x13f00
7+
}
8+
9+
ENTRY(esp_main_esp8266)
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+
PROVIDE(SPIFlashModeConfig = 0x40004568);
30+
PROVIDE(SPIParamCfg = 0x40004c2c);

0 commit comments

Comments
 (0)