[cfe-dev] Using clang for cross-compiling to Cortex M4

James Gregurich bayoubengal at mac.com
Tue Jun 3 12:31:32 PDT 2014



On Jun 3, 2014, at 1:35 PM, Rick Mann <rmann at latencyzero.com> wrote:

> The host is the latest released OS X. I do not have a working sysroot; that's what I'm trying to achieve. I thought compiler-rt was part of getting that into shape.



‘sysroot’ is not your toolchain. sysroot is the rough equivalent of an Apple SDK. That is where your system headers and libraries are located so that the compiler can find them. I have successfully built a mac/clang cross compiler for powerpc-unknown-linux. I got my sysroot out of the yocto/openembedded project build for my board. If you don’t have a sysroot, you can probably create one just by creating a directory on OSX that contains the contents of /usr/include and /usr/lib and maybe /lib.

What worked for me was to:


1) build clang for OSX (host platform) as usual. 
	a) configure it to install into a specific directory that will hold your tool chain.
	b) compile a simple program for OSX and make sure that works.
2) setup your sysroot in some way.
3) determine from clang the target triplet it requires. mine is 'powerpc-unknown-linux’
4) get the source code for binutils. There are online tutorials for cross compiling binutils. 
	a) configure it to target your platform as the default platform. If you don’t, it will assume OSX is the target and not build pieces like ‘ld’. use the triplet from #3
	b) when you configure, set the PREFIX value so that you don’t install it into your OSX system
	c) install it. you end up with a directory that has all the pieces staged.
	d)  manually merge those pieces into the directory you have clang installed

5) set up a simple C program that includes nothing. just have a main function that returns argc;
6) get that to build. here is my redacted compile line to show you how to call your compiler:



“<some path>/linux_toolchain_root_3.8/bin/clang++" -target powerpc-unknown-linux  -m32 -mcpu=e500v2 --sysroot="<some path>/linux_sysroot_3.8" -I "<some path>/linux_sysroot_3.8/usr/include/c++" -I "<some path>/usr/include/c++/powerpc-fsl_networking-linux-gnuspe" -ggdb -Os -std=gnu++11 -MD -MP -pthread  -c "../../Embedded/SysCommon/VSysLog.cc" -o "../../Embedded/SysCommon/VSysLog.o"



redacted linker line:

“<some path>/linux_toolchain_root_3.8/bin/clang++"   main.o whatever.o ... -lgcc_s -lstdc++ -lrt -lpthread -target powerpc-unknown-linux --sysroot=“<some path>/linux_sysroot_3.8"   -stdlib=libstdc++ -std=gnu++11 -o SuperNodeDmxUpdater.elf




“<some path>” just means some arbitrary path. depends on how you want to structure your layout.



I don’t believe I’m using compiler-rt.  I don’t see it in my toolchain or sysroot. I don’t recall doing anything special to build it. It was straightforward to set up clang and llvm.  My strategy is to use my platform’s runtime rather than what comes with llvm & clang. The unwinder library doesn’t work on linux. that is why you see me using -lgcc_s.  I believe that that component is supplied by libgcc in my setup. If I do a verbose link, I get…



“<some path>/linux_toolchain_root_3.8/bin/powerpc-unknown-linux-ld" —sysroot=<some path>/linux_sysroot_3.8 --eh-frame-hdr -m elf32ppclinux -dynamic-linker /lib/ld.so.1 -o myexe <some path>/linux_sysroot_3.8/usr/lib/crt1.o <some path>/linux_sysroot_3.8/usr/lib/crti.o <some path>/linux_sysroot_3.8/usr/lib/crtbegin.o -L<some path>/linux_sysroot_3.8/lib -L<some path>/linux_sysroot_3.8/usr/lib <some path>/main.o ...  -lgcc_s -lstdc++ -lrt -lpthread -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc <some path> linux_sysroot_3.8/usr/lib/crtend.o <some path>/linux_sysroot_3.8/usr/lib/crtn.o




7) when you go to build a simple program, you’ll get errors telling you what is missing, you then have to find those missing elements and adjust your compile/linker lines to find them. In some cases, I added symlinks to my sysroot tree to map low-level runtime files like crt1.o to where my toolchain was expecting to find them.

8) keep using trial and error until you can build a c++ program with dependencies.


9) I did get libc++ to build using the libstdc++.so option and with the system’s unwinder (libgcc_s). I haven’t tested it out yet. I had a little trouble with libc++ because I don’t speak CMAKE. So, I had to unravel some of that to get all the compiler/linker flags right.  When you get to that stage, let me know and I’ll give you my notes. I made it a point to take notes by the time I got to libc++.



Hopefully, this will give you enough info to muddle your way through it. it isn’t hard, but it can be hard to know where to go since there is no one authoritative list of steps. You have to look at all the tutorials and documentation and cherry-pick what you need as you go.


-James





















-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140603/ff42e328/attachment.html>


More information about the cfe-dev mailing list