Skip to content

Commit 0109c8e

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

File tree

15 files changed

+11075
-4
lines changed

15 files changed

+11075
-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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
14+
- name: Set up Python 3.13
15+
uses: actions/setup-python@v5
16+
with:
17+
python-version: 3.13
18+
19+
- name: Install Python dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
pip install esptool
23+
24+
- name: Install toolchains
25+
shell: bash
26+
run: |
27+
mkdir -p toolchains
28+
pushd toolchains
29+
../tools/setup_toolchains.sh
30+
popd
31+
32+
- name: Build stub
33+
shell: bash
34+
run: |
35+
source ./tools/export_toolchains.sh
36+
./tools/build_all_chips.sh
37+
38+
- name: Upload stub JSONs
39+
uses: actions/upload-artifact@v4
40+
with:
41+
path: build*/esp*.json
42+
if-no-files-found: error
43+
retention-days: 3
44+
45+
create_release:
46+
name: Create GitHub release
47+
needs: build_stubs
48+
if: startsWith(github.ref, 'refs/tags/')
49+
runs-on: ubuntu-24.04
50+
permissions:
51+
contents: write
52+
steps:
53+
- name: Get version
54+
id: get_version
55+
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
56+
shell: bash
57+
- name: Checkout
58+
uses: actions/checkout@v4
59+
with:
60+
fetch-depth: 0
61+
- name: Set up Python 3.13
62+
uses: actions/setup-python@v5
63+
with:
64+
python-version: 3.13
65+
- name: Install dependencies
66+
run: |
67+
python -m pip install --upgrade pip
68+
pip install commitizen czespressif
69+
- name: Generate changelog
70+
run: |
71+
cz changelog ${{ steps.get_version.outputs.VERSION }} --file-name changelog_body.md
72+
cat changelog_body.md
73+
- name: Download stub JSONs
74+
uses: actions/download-artifact@v4
75+
- name: Create release
76+
id: create_release
77+
uses: softprops/action-gh-release@v2
78+
env:
79+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80+
with:
81+
body_path: changelog_body.md
82+
name: Version ${{ steps.get_version.outputs.VERSION }}
83+
draft: true
84+
prerelease: false
85+
fail_on_unmatched_files: true
86+
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
# TODO move these to esp-stub-lib
12+
set(LINKER_SCRIPTS_DIR "${CMAKE_SOURCE_DIR}/src/ld")
13+
14+
# CXX standard set based on the GCC version of the toolchain for ESP8266 (oldest one)
15+
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)
16+
set(EXTRA_RISCV_FLAGS -march=rv32imc -mabi=ilp32 -msmall-data-limit=0)
17+
set(EXTRA_XTENSA_FLAGS -mtext-section-literals -mlongcalls)
18+
19+
set(ESP8266_FLAGS "-DESP8266" ${EXTRA_XTENSA_FLAGS})
20+
set(XTENSA_FLAGS "-DXTENSA" ${EXTRA_XTENSA_FLAGS})
21+
set(RISCV_FLAGS "-DRISCV" ${EXTRA_RISCV_FLAGS})
22+
23+
message(STATUS "Building for ${TARGET_CHIP}")
24+
add_executable(${TARGET_CHIP})
25+
26+
set(ESP32_XTENSA_CHIPS esp32 esp32-s2 esp32-s3)
27+
28+
if(TARGET_CHIP IN_LIST ESP32_XTENSA_CHIPS)
29+
set(TOOLCHAIN_EXECUTABLE_PREFIX "xtensa-${TARGET_CHIP}-elf-")
30+
set(CHIP_FLAGS "${XTENSA_FLAGS}")
31+
elseif(TARGET_CHIP STREQUAL "esp8266")
32+
set(TOOLCHAIN_EXECUTABLE_PREFIX "xtensa-lx106-elf-")
33+
set(CHIP_FLAGS "${ESP8266_FLAGS}")
34+
# The lx106 toolchain is of version 8.4 which doesn't support the --dependency-file linker option.
35+
set(CMAKE_LINK_DEPENDS_USE_LINKER FALSE) # requires CMake 3.27 or higher
36+
else()
37+
set(TOOLCHAIN_EXECUTABLE_PREFIX "riscv32-esp-elf-")
38+
set(CHIP_FLAGS "${RISCV_FLAGS}")
39+
endif()
40+
41+
set(CHIP_LINKER_SCRIPT "${LINKER_SCRIPTS_DIR}/${TARGET_CHIP}.ld")
42+
43+
target_compile_options(${TARGET_CHIP} PRIVATE
44+
${CUSTOM_FLAGS}
45+
${CHIP_FLAGS}
46+
"-T${CHIP_LINKER_SCRIPT}"
47+
)
48+
49+
target_link_options(${TARGET_CHIP} PRIVATE
50+
${CUSTOM_FLAGS}
51+
${CHIP_FLAGS}
52+
-Wl,-Map=${CMAKE_BINARY_DIR}/${TARGET_CHIP}.map -L${LINKER_SCRIPTS_DIR}
53+
)
54+
55+
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_EXECUTABLE_PREFIX}g++)
56+
set(CMAKE_LINKER ${TOOLCHAIN_EXECUTABLE_PREFIX}g++)
57+
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf")
58+
59+
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=esp32-s2 # 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set(COMMON_SOURCES "main.cpp")
2+
3+
target_sources(${TARGET_CHIP} PRIVATE ${COMMON_SOURCES})
4+
5+
if(${TARGET_CHIP} STREQUAL "esp8266")
6+
get_target_property(comp_opts esp8266 COMPILE_OPTIONS)
7+
list(REMOVE_ITEM comp_opts -Wconversion)
8+
9+
add_library(miniz_obj OBJECT miniz.cpp)
10+
target_compile_options(miniz_obj PRIVATE ${comp_opts})
11+
target_sources(esp8266 PRIVATE $<TARGET_OBJECTS:miniz_obj>)
12+
endif()

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)