Skip to content

Commit 39f5078

Browse files
authored
ESP: get march and mabi flags from sdk (#72)
Refactors CMake configuration to automatically extract `-march` and `-mabi` flags from existing CMake variables set by ESP-IDF. This change makes it easier to add new Espressif RISC-V targets.
1 parent 27879f9 commit 39f5078

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

esp32-led-blink-sdk/main/CMakeLists.txt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ if("${arch}" STREQUAL "xtensa")
1111
message(FATAL_ERROR "Not supported target: ${target}")
1212
endif()
1313

14-
if(${target} STREQUAL "esp32c2" OR ${target} STREQUAL "esp32c3")
15-
set(march_flag "rv32imc_zicsr_zifencei")
16-
set(mabi_flag "ilp32")
17-
elseif(${target} STREQUAL "esp32p4")
18-
set(march_flag "rv32imafc_zicsr_zifencei")
19-
set(mabi_flag "ilp32f")
20-
else()
21-
set(march_flag "rv32imac_zicsr_zifencei")
22-
set(mabi_flag "ilp32")
14+
# Extract the -march flag and remove any vendor-specific extensions (_x*)
15+
string(REGEX MATCH "-march=[^ ]+" march_flag "${CMAKE_C_FLAGS}")
16+
string(REGEX REPLACE "_x[^ ]*" "" march_flag "${march_flag}")
17+
18+
# Extract the -mabi flag or set a default value if not present
19+
string(REGEX MATCH "-mabi=[^ ]+" mabi_flag "${CMAKE_C_FLAGS}")
20+
if("${mabi_flag}" STREQUAL "")
21+
set(mabi_flag "-mabi=ilp32")
2322
endif()
2423

2524
# Clear the default COMPILE_OPTIONS which include a lot of C/C++ specific compiler flags that the Swift compiler will not accept
@@ -41,7 +40,7 @@ endforeach()
4140
target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHELL:
4241
-target riscv32-none-none-eabi
4342
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
44-
-Xcc -march=${march_flag} -Xcc -mabi=${mabi_flag} -Xcc -fno-pic -Xcc -fno-pie
43+
-Xcc ${march_flag} -Xcc ${mabi_flag} -Xcc -fno-pic -Xcc -fno-pie
4544
4645
-pch-output-dir /tmp
4746
-Xfrontend -enable-single-module-llvm-emission

esp32-led-strip-sdk/main/CMakeLists.txt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ if("${arch}" STREQUAL "xtensa")
1111
message(FATAL_ERROR "Not supported target: ${target}")
1212
endif()
1313

14-
if(${target} STREQUAL "esp32c2" OR ${target} STREQUAL "esp32c3")
15-
set(march_flag "rv32imc_zicsr_zifencei")
16-
set(mabi_flag "ilp32")
17-
elseif(${target} STREQUAL "esp32p4")
18-
set(march_flag "rv32imafc_zicsr_zifencei")
19-
set(mabi_flag "ilp32f")
20-
else()
21-
set(march_flag "rv32imac_zicsr_zifencei")
22-
set(mabi_flag "ilp32")
14+
# Extract the -march flag and remove any vendor-specific extensions (_x*)
15+
string(REGEX MATCH "-march=[^ ]+" march_flag "${CMAKE_C_FLAGS}")
16+
string(REGEX REPLACE "_x[^ ]*" "" march_flag "${march_flag}")
17+
18+
# Extract the -mabi flag or set a default value if not present
19+
string(REGEX MATCH "-mabi=[^ ]+" mabi_flag "${CMAKE_C_FLAGS}")
20+
if("${mabi_flag}" STREQUAL "")
21+
set(mabi_flag "-mabi=ilp32")
2322
endif()
2423

2524
# Clear the default COMPILE_OPTIONS which include a lot of C/C++ specific compiler flags that the Swift compiler will not accept
@@ -41,7 +40,7 @@ endforeach()
4140
target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHELL:
4241
-target riscv32-none-none-eabi
4342
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
44-
-Xcc -march=${march_flag} -Xcc -mabi=${mabi_flag} -Xcc -fno-pic -Xcc -fno-pie
43+
-Xcc ${march_flag} -Xcc ${mabi_flag} -Xcc -fno-pic -Xcc -fno-pie
4544
4645
-pch-output-dir /tmp
4746
-Xfrontend -enable-single-module-llvm-emission

0 commit comments

Comments
 (0)