[llvm-dev] Build Clang/LLVM for AVR

Sam Elliott via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 25 05:17:43 PDT 2020


It looks like you’re cross-compiling for an embedded target, so you need to be careful about how you treat headers (like libc’s), as the x86 ones will not be compatible with the AVR ones. The clang driver can manage this for you, if you give it enough information:

The first thing to try is adding the --gcc-toolchain argument to clang to point to the root of the avr gcc toolchain (I think in your case this will be /opt/avr/avr-gcc ). This should help the driver pick up the right include paths for AVR.

Try that see how you get on.

Sam

> On 25 Mar 2020, at 12:01 pm, Alexander Entinger via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hi everyone,
> 
> I've been wondering how to correctly build clang/LLVM for the AVR target architecture. Unfortunately documentation is very scarce (or outdated or I didn't find it) and while I've been able to build clang/LLVM for AVR I'm still falling short of compiling an actual binary for the MCU. Here are the steps I've undertaken so far:
> 
>  git clone https://github.com/llvm/llvm-project
>  cd llvm-project
>  mkdir build && cd build
> 
>  cmake -G "Unix Makefiles" -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" -DLLVM_TARGETS_TO_BUILD="AVR" -DCMAKE_BUILD_TYPE=Release ../llvm
>  make -j8
>  sudo make install
> 
> Next I created a pretty empty main.cpp and tried to compile that:
> 
>  #include <stdint.h>
>  int main()
>  {
>    for(;;) { }
>    return 0;
>  }
> 
> Here's the result of attempting the compilation ...
> 
>  $ clang++ --target=avr -mmcu=atmega328p -c main.cpp -o main.o -v
>  clang version 11.0.0 (https://github.com/llvm/llvm-project 177dd63c8d742250dac6ea365e7c30f0fbab3257)
>  Target: avr
>  Thread model: posix
>  InstalledDir: /usr/local/bin
>  Found candidate GCC installation: /usr/lib/gcc/avr/5.4.0
>  Selected GCC installation: /usr/lib/gcc/avr/5.4.0
>  (in-process)
>   "/usr/local/bin/clang-11" -cc1 -triple avr -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model static -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -target-cpu atmega328p -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /usr/local/lib/clang/11.0.0 -fdeprecated-macro -fdebug-compilation-dir /home/alex/tmp/clang-avr -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o main.o -x c++ main.cpp
>  clang -cc1 version 11.0.0 based upon LLVM 11.0.0git default target x86_64-unknown-linux-gnu
>  #include "..." search starts here:
>  #include <...> search starts here:
>   /usr/local/include
>   /usr/local/lib/clang/11.0.0/include
>   /usr/include
>  End of search list.
>  In file included from main.cpp:1:
>  In file included from /usr/local/lib/clang/11.0.0/include/stdint.h:52:
>  /usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
>  #include <bits/libc-header-start.h>
>           ^~~~~~~~~~~~~~~~~~~~~~~~~~
>  1 error generated.
> 
> Looking for that file I can find it in the system
> 
> $ find /usr -iname libc-header-start.h
> /usr/include/x86_64-linux-gnu/bits/libc-header-start.h
> 
> But adding it to the clang call via -isystem or -I does not help either.
> 
>  $ clang --target=avr -mmcu=atmega328p -c main.cpp -o main.o -v -I/usr/include/x86_64-linux-gnu
>    clang version 11.0.0 (https://github.com/llvm/llvm-project 177dd63c8d742250dac6ea365e7c30f0fbab3257)
>  Target: avr
>  Thread model: posix
>  InstalledDir: /usr/local/bin
>   (in-process)
>   "/usr/local/bin/clang-11" -cc1 -triple avr -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model static -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor aliases -target-cpu atmega328p -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /usr/local/lib/clang/11.0.0 -I /usr/include/x86_64-linux-gnu -fdeprecated-macro -fdebug-compilation-dir /home/alex/tmp/clang-avr -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o main.o -x c++ main.cpp
>  clang -cc1 version 11.0.0 based upon LLVM 11.0.0git default target x86_64-unknown-linux-gnu
>  #include "..." search starts here:
>  #include <...> search starts here:
>   /usr/include/x86_64-linux-gnu
>   /usr/local/include
>   /usr/local/lib/clang/11.0.0/include
>   /usr/include
>  End of search list.
>  In file included from main.cpp:1:
>  In file included from /usr/local/lib/clang/11.0.0/include/stdint.h:52:
>  In file included from /usr/include/stdint.h:26:
>  In file included from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33:
>  In file included from /usr/include/features.h:448:
>  /usr/include/x86_64-linux-gnu/gnu/stubs.h:7:11: fatal error: 'gnu/stubs-32.h' file not found
>  # include <gnu/stubs-32.h>
>            ^~~~~~~~~~~~~~~~
>  1 error generated.
> 
> Thinking that maybe the old version (5.4.0) of avr-gcc might be a problem I've built a 9.2.0 version from sources
> 
>  $ avr-gcc -v
>  Using built-in specs.
>  Reading specs from /opt/avr/avr-gcc/lib/gcc/avr/9.2.0/device-specs/specs-avr2
>  COLLECT_GCC=avr-gcc
>  COLLECT_LTO_WRAPPER=/opt/avr/avr-gcc/libexec/gcc/avr/9.2.0/lto-wrapper
>  Target: avr
>  Configured with: ../configure --prefix=/opt/avr/avr-gcc --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp --disable-libada --with-dwarf2 --disable-shared --enable-static --enable-mingw-wildcard
>  Thread model: single
>  gcc version 9.2.0 (GCC) 
> 
> But this didn't change anything, except that when running clang without file arguments it does indicate that there is no avr-gcc in the system (although there is, but probably not spliced in there where the clang/LLVM build process expects it).
> 
>  $ clang --target=avr -mmcu=atmega328p 
>  clang-11: warning: no avr-gcc installation can be found on the system, cannot link standard libraries [-Wavr-rtlib-linking-quirks]
>  clang-11: warning: standard library not linked and so no interrupt vector table or compiler runtime routines will be linked [-Wavr-rtlib-linking-quirks]
>  clang-11: error: no input files
> 
> I'd greatly appreciate any input helping to solve these problems or a pointer to working documentation.
> 
> With kind regards, Alex
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

--
Sam Elliott
Software Developer - LLVM and OpenTitan
lowRISC CIC



More information about the llvm-dev mailing list