Description
I'm trying to get arduino-esp32 to compile using the esp32-led-blink-sdk example as a template, which would be amazing if it worked.
I'm pretty sure it shouldn't be super difficult given how it's literally just a C++ library, but the only issue I've run into is that swiftc
doesn't seem to like the preprocessor macros that Arduino uses to handle different architectures etc.
Here's the bit of arduino's CMakeLists.txt that defines the compiler flags:
target_compile_options(${COMPONENT_TARGET} PUBLIC
-DARDUINO=10812
-DARDUINO_${idf_target_for_macro}_DEV
-DARDUINO_ARCH_ESP32
-DARDUINO_BOARD="${idf_target_caps}_DEV"
-DARDUINO_VARIANT="${CONFIG_ARDUINO_VARIANT}"
-DESP32)
This is what the swiftc
call then looks like:
/usr/bin/swiftc -j 8 -num-threads 8 -c
-DESP_PLATFORM -DIDF_VER=\"v5.1.4\" -DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" -DSOC_MMU_PAGE_SIZE=CONFIG_MMU_PAGE_SIZE -D_GNU_SOURCE -D_POSIX_READER_WRITER_LOCKS
-parse-as-library -static -emit-module
-emit-module-path esp-idf/main/__idf_main.swiftmodule
-module-name __idf_main
-module-link-name main
-wmo -color-diagnostics
-target riscv32-none-none-eabi
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
-Xcc -march=rv32imc_zicsr_zifencei -Xcc -mabi=ilp32
-pch-output-dir /tmp
-Xfrontend
-enable-single-module-llvm-emission
-Xcc -I... -Xcc -I....
-import-bridging-header main/BridgingHeader.h
-cxx-interoperability-mode=default
-DARDUINO=10812 -DARDUINO_ESP32C3_DEV -DARDUINO_ARCH_ESP32 -DARDUINO_BOARD=\"ESP32C3_DEV\" -DARDUINO_VARIANT=\"esp32c3\" -DESP32
-output-file-map esp-idf/main/CMakeFiles/__idf_main.dir//output-file-map.json
-I ... -I ....
Which raises the following warnings:
warning: conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'IDF_VER="v5.1.4"')
warning: conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'MBEDTLS_CONFIG_FILE="mbedtls/esp_config.h"')
warning: conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'SOC_MMU_PAGE_SIZE=CONFIG_MMU_PAGE_SIZE')
warning: conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'ARDUINO=10812')
warning: conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'ARDUINO_BOARD="ESP32C3_DEV"')
warning: conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'ARDUINO_VARIANT="esp32c3"')
Which is then used by a lot of Arduino libraries like OneWire for example (list):
#if ARDUINO >= 100
#include <Arduino.h> // for delayMicroseconds, digitalPinToBitMask, etc
#else
#include "WProgram.h" // for delayMicroseconds
#include "pins_arduino.h" // for digitalPinToBitMask, etc
#endif
Creating an error like this:
<unknown>:0: error: failed to emit precompiled header '/tmp/BridgingHeader-swift_1PSIJ0LN6QHW8-clang_1Y9RGGHIW2TB2.pch' for bridging header 'main/BridgingHeader.h'
2 warnings and 4 errors generated.
components/OneWire/OneWire.h:15:10: error: 'WProgram.h' file not found
13 | #include <Arduino.h> // for delayMicroseconds, digitalPinToBitMask, etc
14 | #else
15 | #include "WProgram.h" // for delayMicroseconds
| `- error: 'WProgram.h' file not found
16 | #include "pins_arduino.h" // for digitalPinToBitMask, etc
17 | #endif
So it seems that the issue is swiftc
only supports boolean preproccessor macros; I've tried putting the attributes in my BridgingHeader.h
as #define
s but that doesn't seem to work, and I don't know enough about swift's internals how to know what else to try?
Here's what is basically a MNWE (minimal not working example): https://github.com/maartin0/esp32-arduino-swift-test but I'm happy to test any ideas you might have so you don't have to set up a workspace (Arduino is like 1.5 GB)