atilaneves / dpp

Directly include C headers in D source code
Boost Software License 1.0
230 stars 31 forks source link

Question over parsing Arduino libs #252

Closed Hecatron closed 4 years ago

Hecatron commented 4 years ago

Hi, This isn't a bug as such but more of a question I've recently been looking into if it's possible to use DLang with the Arduino

Ignoring the issues associated with compiling to the AVR target (I think one person got around it on the DLang forum by using LDC to compile to IR, then going from IR to AVR Code using LLVM, but I'll probably be using arm or esp32 anyway)

I've been seeing if I could use dpp to process the arduino library headers into something d could use anyway it seems to mostly work

Creating a Arduino.dpp file with a single line of

#include "Arduino.h"

Then running the following works fine

d++ Arduino.dpp --preprocess-only --define="__AVR_ATmega4809__" --include-path="C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" --include-path="C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include" --include-path="C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard" --include-path="C:\Program Files (x86)\Arduino\libraries\SD\src"

Note I'm using Windows 10 / LLVM 10.0 and the latest dpp built from source However that's only doing C parsing not C++ parsing which is needed for some of the libs such as SPI or SD since they use Class's trying this

d++ Arduino.dpp --preprocess-only --parse-as-cpp --c++-standard="c++11" --define="__AVR_ATmega4809__" --include-path="C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" --include-path="C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include" --include-path="C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard" --include-path="C:\Program Files (x86)\Arduino\libraries\SD\src"
d++ SD.dpp --preprocess-only --parse-as-cpp --c++-standard="c++11" --define="__AVR_ATmega4809__" --include-path="C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" --include-path="C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include" --include-path="C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard" --include-path="C:\Program Files (x86)\Arduino\libraries\SD\src"

Leads to the following error

Error: Error parsing 'C:\Users\user\AppData\Local\Temp\libclanga31280 ':
C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\util/delay_basic.h:108:5: error: invalid output constraint '=w' in asm

The error is due to some AVR specific assembler directives LLVM doesn't support within delay_basic.h Commenting out those assembler directives which is just two lines makes everything work fine

The question is, does anyone know of a clang option somewhere to disable the analysis of these assembler directives? that could be passed in with --clang-option

denizzzka commented 4 years ago

@grbd can you provide link to discussed delay_basic.h file?

Hecatron commented 4 years ago

From the looks of things it's part of the avr-gcc / avr-libc bundled with the arduino

I think it's an avr specific assembler directive "=w" and "0" commenting out those two lines fixes it

void
_delay_loop_2(uint16_t __count)
{
    __asm__ volatile (
        "1: sbiw %0,1" "\n\t"
        "brne 1b"
        : "=w" (__count)
        : "0" (__count)
    );
}
Hecatron commented 4 years ago

aha found a way around it

this then overrides / finds the modifed header before the default arduino one

d++ headers/SD.dpp --preprocess-only --parse-as-cpp --c++-standard="c++11" --define="__AVR_ATmega4809__" --include-path="C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" --include-path="D:\test1"  --include-path="C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include" --include-path="C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard" --include-path="C:\Program Files (x86)\Arduino\libraries\SD\src"

Now I just need to try this out with a few libs / look at the compile process via arduino-cli and try and script everything in python