Cross toolchain under linux
Cross compile toolchain是linux常用的一种跨架构,跨设备的开发模式,多用于嵌入式开发过程(由于目标架构的编译能力较弱,比如树莓派等),比如在x86 linux平台作arm开发,在linux下作ios开发等等。即使不作嵌入式开发,在你需要调整系统架构,调整系统基础库,裁剪系统或者在x86_64下cross x86或者反过来,或者,从无到有为一个架构移植新系统时,也总有用到cross toolchain的时候。更有甚者,如MAC OS X,其MAC native应用的开发在xcode中也是cross的方式,这里的cross不再是cross架构,而是cross sysroot,从而实现了SDK和系统Runtime物理的分离。
这篇小文主要是从原理和形式的角度来简单介绍一些cross toolchain到底是个什么构成,写这个东西不是为了解决具体问题,主要是为希望了解cross toolchain的同志们提供一个大概的view。
1,基本构成:
一条Cross toolchain开发环境,主要由以下两个部分组成:
Host cross toolchain:能够运行在host OS,可以生成目标OS代码的汇编器、链接器、编译器,常见的是gcc+binutils。我在Linux ios toolchain中使用的是clang/llvm + 从苹果开源代码移植的cctools。
target sysroot:目标系统的整体镜像,包括了开发库,头文件等等。往往表现为一个目录。host cross toolchain在编译,链接时,使用目标系统的头文件、动态库、静态库等,而不会再使用host系统的头文件等。
生成目标代码后需要在真实物理设备或者在以qemu为代表的虚拟运行环境中运行,所以,android需要一个qemu仿真环境,Xcode ios开发环境也提供了一个IOS模拟器。
注:基于clang/llvm的cross toolchain是非常简单的,如果你系统的clang/llvm编译构建时没有关闭clang/llvm的目标架构支持,那么编译器部分已经可以支持cross,只要系统提供了<tripplet>-ld, <tripplet>-ar等,那么配合target sysroot和--target=<tripplet>直接就可以cross compile。具体可以参考 https://code.google.com/p/ios-toolchain-based-on-clang-for-linux,我在wiki部分比较详细的介绍了linux ios toolchain的过程,这个toolchain也是目前最接近xcode toolchain的实现。
下面主要讲一下binutils, gcc等cross的构成。
2,第一步,汇编器和链接器,cross的binutils
binutils提供了汇编器和链接器(as和ld)以及ranlib等其他常用的二进制处理相关的工具,cross的binutils是运行在host os但能够汇编目标架构的汇编代码、并通过sysroot链接目标机器的开发库,生成目标架构的二进制码。
所以binutils是制作cross toolchain的第一步。
这里主要注意两个configure参数:
--target=<target tripplet>,比如arm可能是arm-myos-linux-gnueabihf
--sysroot=<sysroot absolute path>,指定一下sysroot将要放置的目录,可以是/opt/sysroot等。这个参数,决定了链接时ld到哪里去找动态、静态库。
--prefix可以设置成一个非标准路径,以避免跟标准路径下的系统工具冲突,比如--prefix=/opt/cross-toolchain。
这样做出来的binutils,就是可以运行在host系统并能够支持目标架构汇编代码的编译器和链接器了。其命令的名称可能是<target tripplet>-as等形式。
3,第二步,最小的cross gcc,使用cross的binutils.
gcc为了更好的支持cross toolchain的构建,已经提供了一种最小化编译安装的方式(不依赖系统headers,自包含一个简单的libc,使用的是红帽的newlib)。
这一步是为后面制作完整的cross gcc作准备,也就是我们只需要能够生成一个将C代码转换为目标架构汇编代码的C编译器即可(C++编译器都不需要)。
这里我们需要的只有gcc和libgcc。
注:cross toolchain的gcc是运行在host os的,也就是cross toolchain的gcc命令是host os的二进制代码,但是,cross toolchain中的libgcc*.a,crtbegin/end.o,却是目标架构代码的(因为它是用来编译、链接,生成目标架构二进制码的),所以这里必须使用前面构建的cross binutils.
export PATH=<cross binutils bin path>:$PATH
让编译过程能够找到前面制作的cross binutils.
几个主要的congfigure参数如下:
--without-headers --with-newlib:不使用任何系统header,使用gcc自带的newlib libc库。
--target=<target tripplet>,比如,前面我们使用的是arm-myos-linux-gnueabihf,可以确保编译过程能够找到并使用<target tripplet>-as/ld等命令。
--prefix=<cross toolchain path>,这里跟前面binutils的一样。
其他诸如--enable-languages=c,c++以及一些目标架构相关的配置参数等可以根据实际情况添加,比如树莓派可能需要添加--with-abi=aapcs-linux --with-arch=armv6zk --with-mode=arm --with-float=hard --with-fpu=vfp。
编译时使用:
make all-gcc
make install-gcc
make all-target-libgcc
make install-target-libgcc
这里生成的gcc命令,其名称是<target tripplet>-gcc。
到目前为止,实际上我们已经得到了基本的cross 编译器(gcc/g++,但是没有libstdc++-v3)、链接器和汇编器。
4,第三步,target sysroot中最基本的架构相关的头文件,kernel headers.
kernel headers并不是内核模块开发的kernel development头文件,而是kernel提供的跟架构相关的用户态头文件。
比如asm/unistd_*.h,提供的是系统调用号等,这些头文件,往往会被libc二次封装。
编译安装过程比较简单,解开内核源代码,以arm为例:
make ARCH=arm headers_check
make ARCH=arm INSTALL_HDR_PATH=/opt/sysroot/usr headers_install
将必要的头文件安装到target sysroot中/usr/include相关目录。
5,第四步,target sysroot最基本的库,C库。
C库的选择是比较多的,常见的由glibc/eglibc, uclibc, musl-libc等等。
具体的编译参数取决于你使用哪个C库,但常见的配置原则如下:
export PATH=<cross toolchain bin path>:$PATH
export CC=<target tripplet>-gcc
export LD=<target tripplet>-ld
export AS=<target tripplet>-as
编译时注意:
--host=<target tripplet>
--target=<target tripplet>
如果是glibc,还需要--with-headers=</opt/sysroot/usr/include>.
对于musl-libc之类的轻量级C库,可能相对比较简单,只需要使用target gcc/as/ld编译即可。
configure的过程中如果需要生成临时代码探测系统特征,肯定是会失败的(因为生成的是目标架构二进制,却要在host os运行),所以,往往要通过一些预制的参数或者config.sub的使用来避免这种动态探测。
编译后,需要将其安装到target sysroot(一般使用make install DESTDIR=/opt/sysroot)。这里就是/opt/sysroot,往往会在/opt/sysroot/lib目录安装loader(常见的ld-linux.so.x等),会在/opt/sysroot/usr/include安装C库头文件。
特别注意,这里生成的C库的二进制是目标架构的二进制,而不是host OS的二进制。
6,第五步,完整的cross gcc,支持sysroot的使用。
这一步的构建过程,跟第二步最小的cross gcc类似,主要需要注意以下几点:
6.1,添加--with-sysroot=/opt/sysroot,使用sysroot中的c库和头文件。
6.2,去掉--without-headers --with-newlib,不再使用gcc自带的newlib。
6.3,添加目标代码需要的各种补丁.
6.4,--prefix仍然指定为/opt/cross-toolchain
6.5, --host=<host tripplet> --target=<target tripplet>,也就是仍然用本地gcc构建cross toolchain的<target tripplet>-gcc。
编译,安装。(必要时,可能需要在编译过程中作make install-gcc; make install-target-libgcc; make install-target-libstdc++-v3)
至此,我们得到了:
1,完整的cross toolchian,包括支持sysroot的binutils,gcc等
2,基本的target sysroot,包括kernel headers和C库。
7,第六步,使用最终的cross toolchain,构建内核镜像,基础utils等,直到完成一个可以self boot的sysroot。
目前的target sysroot只包含了kernel headers和基础C库,距离一个可以self boot的基础可运行环境还相去甚远。
至少需要添加内核镜像,busybox或者其他基础工具和shell,并构造initscripts等,直到可以自举为止。
添加必要组件后,即可将target sysroot导入到目标机器存储,从目标机器直接启动系统。
8,如果是从无到有的制作新架构的booting系统,且目标架构具备足够的运算能力,如龙芯,ppc或者较高主频的arm。
可以通过cross toolchain编译出目标架构的gcc和binutils,后续的软件编译可以在目标系统完成。
总结:
1,一个cross 开发环境,包含了运行在host os的cross toolchain和target sysroot支撑环境两个部分。
2,cross toolchain制作的核心思想是在host os上搭建出能够生成目标代码的编译器,汇编器,链接器。
3,target sysroot提供了目标系统软件开发所需要头文件和支撑库,相当于将完整的目标系统安装到了一个系统目录。
4,toolchain制作的步骤,大概是1)先有链接器、汇编器,2)再作最小的编译器,3)准备sysroot的基础头文件和C库,4)重作编译器以便于编译时可以使用target sysroot的头文件和C库。
5,弱运算能力的目标架构,一般会采用cross模式进行开发;如果目标架构的运算能力足够的强,应该将主要的工作转移到完成self boot的目标系统并为目标系统配备binutils,gcc等,直接在目标系统作编译,以避免cross 过程中出现的特征探测等编译配置问题。
6,理论上所有的开发工作都可以cross,但因为上述原因,cross用起来未必舒服,这里也是见开发商功力的地方。比如,xcode作IOS开发的集成度就比Android的SDK用起来要舒服很多,集成度,细节隐藏也要好很多,而工程统一的代码管理方式和配置方式,也不会出现为android迁移开源工程时的配置探测问题等。
顺便说一下,xcode作mac native开发也是cross 思想,目前,mac的编译器主要用的是clang,xcode将mac sdk安装到了单独的目录,编译时会通过sysroot来指定真实的系统路径。
注意:
1,autotools系列工具,也就是configure脚本类型的工程管理,--host, --target, --build参数经常在cross compile中使用,这是需要了解的内容。
2,configure过程,往往会通过临时生成一个二进制运行探测系统特征,在cross compile中,这是很大的问题,因为目标代码的二进制无法在host os运行,可以通过1)config.sub预设或者2)qemu usermode模拟的方式来运行。
3,没有目标架构的硬件,也可以通过qemu等尝试cross toolchain的练习,最后将target sysroot作成qemu镜像来运行。
4,toolchain制作过程中,注意target tripplet, toolchain路径等的统一,尽量不要出错返工。
2024年11月03日 14:27
Really decent post. I just discovered your weblog and needed to say that I have truly delighted in searching your blog entries. After all I'll be subscribing to your food and I trust you compose again soon! This article was written by a real thinking writer. I agree many of the with the solid points made by the writer. I’ll be back. Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! Positive site,
2024年11月03日 15:07
I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you
2024年11月03日 15:09
There are many things hat to do, when to do it, and how to do it. Here are some ideas on what you can do to stop harassing phone calls from reaching your home, cell phone, or an unlisted number.
2024年11月03日 15:09
That's a great article! The neatly organized content is good to see. Can I quote a blog and write it on my blog? My blog has a variety of communities including these articles. Would you like to visit me later?
2024年11月03日 15:10
I love this blog! your happiness that remains constant even if you are ill. what i always said do not lose stamina. your positivity is your strength that can fight your disease. you have to struggle with this condition for your entire life with your family.
2024年11月03日 15:11
IGS will give you themmarizing, analysing, explaining, and evaluating the entire research work. You must explain and focus on the research paper and your specific purpose must be explained in the research paper. For all these need, you must follow what we are saying here.
2024年11月03日 15:12
Therefore, you have the choice to start a canon printer from an official website like “ij.start.cannon.” Look for your Canon printer model in front or top, or find the serial number on the rear to move forward.
2024年11月03日 15:12
Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot.
2024年11月03日 15:14
I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. I am writing on this topic, so I think it will help a lot. I leave my blog address below. Please visit once.!
2024年11月03日 15:14
Situs Casino Online dan juga slot online terpercaya adalah kumpulan situs judi online indonesia yang mereferensikan situs judi terbaik yang ada di indonesia, dengan pelayanan terbaik dan maksimal untuk semua para pemain yang akan melakukan taruhan di agen judi casino online terpercaya
2024年11月03日 15:15
I'm new to your site, and I'm delighted to say that your blog is fantastic and easy to navigate. I've put a lot of time and effort into this site and will continue to do so in the future. Best wishes for the future
2024年11月03日 15:15
I have been looking for articles on these topics for a long time. I don't know how grateful you are for posting on this topic. Thank you for the numerous articles on this site, I will subscribe to those links in my bookmarks and visit them often. Have a nice day
2024年11月03日 15:17
totositefamily wonderful data! I recently came across your blog and were reading alongside. I concept i would depart my first remark. I don’t know what to say besides that i've. Exact publish however i used to be questioning if you may write a litte greater in this situation? I’d be very grateful if you could complicated a bit bit in addition. Respect it! Splendid ¡v i have to surely pronounce, inspired along with your site. I had no hassle navigating thru all tabs in addition to related data ended up being absolutely clean to do to get admission to. I recently discovered what i was hoping for earlier than you are aware of it in any respect. Pretty unusual. Is probable to comprehend it for those who add forums or something, website subject matter . A tones way on your client to speak. First-rate task..
2024年11月03日 15:17
When I read your article on this topic, the first thought seems profound and difficult. There is also a bulletin board for discussion of articles and photos similar to this topic on my site, but I would like to visit once when I have time to discuss this topic.
2024年11月03日 15:19
I am absolutely getting ready to across this facts, is very helpful my friend. Also exquisite weblog here with all the treasured data you've got. Keep up the coolest paintings you're doing right here. This is an excellent publish i visible thanks to proportion it. It's far certainly what i desired to look wish in future you will maintain for sharing this type of super submit. First-rate article, it become especially helpful! I actually began in this and i am turning into greater acquainted with it better! Cheers, keep doing first rate! Exciting and interesting data may be observed on this subject matter right here profile really worth to peer it. Thank you for a completely thrilling blog. What else may additionally i get that kind of info written in this kind of perfect technique? I’ve a project that i am genuinely now working on, and i have been on the appearance out for such info. You there, that is extraordinary post right here. A debt of gratitude is in order for setting aside the attempt to post such sizeable statistics. Great substance is the thing that continuously gets the guests coming . Wonderful data sharing .. I am very happy to examine this newsletter .. Thanks for giving us go through info. First rate high-quality. I recognize this submit. The website is very good and has a excessive value for creativity
2024年11月03日 15:19
what i do not comprehended could be very you're presently not virtually significantly greater alfar one component to do with girl crazy! Your character stuffs brilliant. Constantly manage it up! You're very a whole lot appreciated than you may be at this moment.
2024年11月03日 15:19
I love this blog! your happiness that remains constant even if you are ill. what i always said do not lose stamina. your positivity is your strength that can fight your disease. you have to struggle with this condition for your entire life with your family.
2024年11月03日 15:20
That's a great article! The neatly organized content is good to see. Can I quote a blog and write it on my blog? My blog has a variety of communities including these articles. Would you like to visit me later?
2024年11月03日 15:20
Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot.
2024年11月03日 15:21
I have been looking for articles on these topics for a long time. I don't know how grateful you are for posting on this topic. Thank you for the numerous articles on this site, I will subscribe to those links in my bookmarks and visit them often. Have a nice day
2024年11月03日 15:23
org/why-should-you-do-slot-gamll keep the quality work going on . You have performed a great job on this article. It’s very precise and highly qualitative. You have even managed to make it readable and easy to read. You have some real writing talent. Thank you so much
2024年11月03日 15:25
Unique information you provide us thanks for this - When it comes to antivirus and security aplaborated by many big brands for more security. You must have seen the name of the Norton offering security to the websites and more
2024年11月03日 15:27
is a browser extension that allows you to interact with the Ethereum blockchain. d applications (dApps) and manage your digital assets. If you need any more information or assistance regarding the Metamask Wallet Extension, feel free to ask