用cygwin交叉编译环境构造基于newlib的目标系统

%%howto-version: 1.0
%%title: Building Cygwin hosted newlib-based target toolchain
%%url: http://www.nanotech.wisc.edu/~khan/software/gnu-win32/
%%category: cygwin
%%filename: cygwin-to-newlib-cross-howto
%%author: Mumit Khan

This howto provides a roadmap to building a newlib-based target
development toolchain such as powerpc-eabi hosted on Cygwin
(ix86-pc-cygwin) platform. Even though I am using Cygwin as the host
in this HOWTO, the information here is just as applicable to other hosts.

TOC:
- Background
- Preliminaries
- Build steps
- Postscript

Created: Thu Jan 25 11:10:11 CST 2001
Last Modified: Thu Jan 25 15:05:13 CST 2001

Background:
===========

When it comes to cross-compiling (the simple kind or the canadian kind),
three terms are very important -- host, target and build. The host is
the machine that the resulting toolchain will run on, the build is the
machine that the resulting toolchain are being built on, and target is
the machine that resulting toolchain will create binaries for. The most
usual case is where host == build == target (eg., if you're using a Linux
compiler on a Linux box that was created on a Linux box); in the case of
most cross-compilers, host == build, target is different (eg., host and
build could be say Linux and target could be say i686-pc-cygwin, so that
when you compile/link on Linux box using this toolchain, you create
binaries that will run on i686-pc-cygwin); in the case of building a
canadian cross compiler, host, build and target may all be different
(I'll refrain from expounding on this one, and leave it to your
imagination).

Ok, so let's say we have a PC running Win2k and Cygwin, and we want to
able to build powerpc-eabi binaries on that PC. The runtime library
for that target is newlib, which should be quite known to those who
are into this sort of thing. This HOWTO is applicable to all supported
GCC targets that use newlib as the runtime library, and I just happen
to be using powerpc-eabi as an example.

CrossGCC folks use various schemes, and I personally find those too
complicated, but do it my way mostly because I'm too lazy to read the
instructions.

Here're the basic steps: (Preliminaries) Decide on where you want to
install and so on, (1) Gather all the source packages you need, move
these over to the Cygwin host, (2) Build and install Binutils, (3) Build
and install *just* the C compiler in GCC, then (4) use the C compiler
just installed to build and install newlib, and finally (5) go back
and build the rest of the compilers and language support runtimes in
GCC and install those as well. Postscript shows a simple example,
and shows how to avoid GCC from always adding .exe to the executable
name (if you want to avoid that, read that before step 3).

There is one thing to note here -- you will run into the term "single
tree" build, which means that all the required components are in one
tree, and set up so that you can simply do a `configure', followed by
a `make', and you're good to go without having to go through the steps
I have outlined here. However, unless you already have a tested and
verified single-tree configuration such as GNUPro formerly from Cygnus
Solutions and now from RedHat if you are a customer, you are much better
off building each piece individually. One might think that building a
single tree out of all the individual pieces is trivial -- just unpack
on top of each other, and then combine all the common pieces. Try that
with different versions of bfd included in binutils and in gdb, and
you'll soon run into a disaster. Stick with multiple separate packages,
and you will not regret the extra keystrokes. It is after all scriptable,
so keystrokes go down to just a few.

For the purposes of this HOWTO, I've used the following packages:

1. Cygwin -- 1.1.7 (with all updates applied to date)
2. GCC -- 2.95.2-6 (part of Cygwin source distribution)
3. Binutils -- 2.10.1 (standard GNU distribution)
4. newlib -- 1.9.0 (http://sources.redhat.com/newlib/)

Preliminaries:
==============

Scattered throughout this article are user commands that you'll type
in, and I've used ``cygwin$'' as the shell prompt.

Let's say we'll be using the following (I'm using bash, so if you're
using a csh/tcsh type shell, do the right thing):

cygwin$ host=i686-pc-cygwin
cygwin$ build=i686-pc-cygwin
cygwin$ target=powerpc-eabi
cygwin$ prefix=/usr/local/powerpc
cygwin$ src_root=/usr/local/src/gnu

Feel free to change $prefix and/or $src_root to match your environment,
but please don't mess with $host, $build and $target. Do make sure that
$src_root directory does exist. You don't have to set these shell
variables of course, but it saves me some typing and saves you from my
inevitable typos.

You should also add $prefix/bin to your PATH, but you can wait till you
have installed binutils for that (ie., end of Step 2).

Step 1:
=======
Gather all the source packages you need. The minimal set is the compiler
sources, binary utilities.

Let's say we get the following from a GNU or RedHat sourceware mirror:

gcc-2.95.2.tar.gz
binutils-2.10.1.tar.gz
newlib-1.9.0.tar.gz

Since we're building on a Cygwin host, the safe bet is to get a version
of gcc-2.95.2 from the Cygwin distribution instead which may have some
Cygwin specific fixes, which is what I use. See Cygwin home page at
http://sources.redhat.com/cygwin/ to see how to download the GCC source
package using the network "setup" utility (or just ftp it over from one
the mirrors). At the time of this writing, the latest one is gcc-2.95.2-6.

Download these on your Cygwin box and unpack:

cygwin$ cd $src_root
cygwin$ tar zxf /tmp/gcc-2.95.2.tar.gz
cygwin$ tar zxf /tmp/binutils-2.10.1.tar.gz
cygwin$ tar zxf /tmp/newlib-1.9.0.tar.gz

Step 2:
=======

Build and install binutils. Never build in the source tree, so I'll just
arbitrarily pick $src_root/BUILD as the top build directory, under which
I'll build both binutils and gcc.

cygwin$ mkdir -p $src_root/BUILD/binutils
cygwin$ cd $src_root/BUILD/binutils
cygwin$ $src_root/binutils-2.10.1/configure /
--with-included-gettext /
--target=$target --host=$host --build=$build /
--prefix=$prefix -v
cygwin$ make > make.log 2>&1

If all goes well, install.

cygwin$ make install > install.log 2>&1

*IMPORTANT* Add $prefix/bin to your PATH Before going forward.

cygwin$ export PATH=$PATH:$prefix/bin

Check:

cygwin$ $target-ld --version
GNU ld 2.10.1
Copyright 2000 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License. This program has absolutely no warranty.
Supported emulations:
elf32ppc

Done.

Step 3:
=======

Build and install *just the C compiler* from GCC.

cygwin$ mkdir -p $src_root/BUILD/gcc
cygwin$ cd $src_root/BUILD/gcc
cygwin$ $src_root/gcc-2.95.2/configure /
--enable-languages=c,c++ /
--with-included-gettext --enable-shared --enable-threads /
--target=$target --host=$host --build=$build /
--with-newlib /
--prefix=$prefix -v

Feel free to change the arguments to --enable-languages, or leave it
out altogether if you want to build all available languages. Also,
do as you wish with --enable-shared and --enable-threads parameters.
Don't mess with the rest. The --with-newlib is critical since it
tells the compiler not to try to include stdlib.h and unistd.h when
building libgcc.a, which is built as part of the core C compiler
build (short answer: the configure script defines the inhibit_libc
macro, which in turn guards against certain target includes in
gcc/libgcc2.c file).

Now, please note that we're specifically only building the C compiler
and just that.

cygwin$ make LANGUAGES=c all-gcc > make.log 2>&1
cygwin$ mv make.log make-c-only.log

If all goes well, install.

cygwin$ make LANGUAGES=c install-gcc > install.log 2>&1
cygwin$ mv install.log install-c-only.log

Now you can build C code with the installed compiler. You could have
also built the C++ compiler as well, but since we don't need that for
building newlib, let's hold on till we're done with step 4.

Step 4:
=======

Build and install newlib:

cygwin$ mkdir -p $src_root/BUILD/newlib
cygwin$ cd $src_root/BUILD/newlib
cygwin$ $src_root/newlib-1.9.0/configure /
--target=$target --host=$host --build=$build /
--prefix=$prefix -v

cygwin$ make > make.log 2>&1

If all goes well, install.

cygwin$ make install > install.log 2>&1

Now you have the target environment in place, and you can build the
rest of the world.

Step 5:
=======

Build and install the rest of the GCC compilers and language runtime
and/or support libraries.

cygwin$ cd $src_root/BUILD/gcc
cygwin$ make > make.log 2>&1
cygwin$ make install > install.log 2>&1

Postscript:
===========

Now that you're run, you should be able to create $target target
binaries. If all goes well:

$ cat > hello.c
#include <stdio.h>
int
main()
{
printf("hello world/n");
return 0;
}
^D
$ $target-gcc -o hello hello.c
$ ls -l hello*
hello.c hello.exe

[ btw, since our example target is powerpc-eabi, you may have to supply
at least one -m<xx> parameter, such as -msim, for this to work ]

Huh, why the .exe extension? Well, it has to do with gcc hosted on
Cygwin, and you can always patch gcc sources to get rid of the automatic
.exe addition. Just comment out the EXECUTABLE_SUFFIX macro in
$src_root/gcc-2.95.2-6/gcc/config/i386/xm-cygwin.h and rebuild gcc.

Incidentally, the whole business of first building and installing only
the C compiler part in GCC is only needed if you don't have the target
environment, newlib in this case, fully installed. After the first time,
all that is unnecessary of course.

If you're building gdb-5.0 as well (which includes the ppc simulator
by the way), you may need to comment out the declaration for strsignal
in gdb-5.0/gdb/defs.h to get it to build. Otherwise, the declaration
will not match the one in newlib (newlib's one returns a const char *,
whereas gdb's declaration omits the const).

THE END
=======

For more information on GNU Compiler Collection (GCC), see GCC home
page:
http://gcc.gnu.org/

For more information on Cygwin, see Cygnus's cygwin project page:
http://www.cygwin.com/

For more information on Crossgcc, see:
http://www.objsw.com/CrossGCC/

For more information on binutils, see:
http://sources.redhat.com/binutils/

Latest version of this documentation, and other information related to
GNU tools on various types of windows32 system, is available from my
gnu-win32 page:
http://www.nanotech.wisc.edu/~khan/software/gnu-win32/

Created: Thu Jan 25 11:10:11 CST 2001
Last Modified: Thu Jan 25 15:05:13 CST 2001
Mumit Khan <khan@nanotech.wisc.edu>

Good luck.

%%end-howto 

 

 

 这位老兄真是大牛。按照他的资料和文档,是有可能做出CYGWIN的。

但这份文档对通用的制作过程说明得不够详细,应该参看他的网页和
文档后面的网站里的文档。制作WINDOWS版本肯定比LINUX的难度要
大很多,要有心理准备。
还有,把文档中的newlib改成glibc就是基于glibc的系统了。以后
是采用newlib还是glibc,恐怕还会有一番争论。




Re: 用cygwin交叉编译环境构造基于newlib的目标系统

说明:
1 如果都翻译成中文就没有味道了
2 我对这方面的东东也不是很熟,如有错误请批评指正,thx
3 没有必要的话(呵呵,我认为没有必要的话了:),我就不翻了 :)

Background: 背景
===========
在交叉编译中,三个术语非常重要:host,target,build
host:toolchain要运行的目标机器
build:用来建立toolchain的机器
target:toolchain所创建的二进制代码可以运行的机器
最通常的情况是三者都是一样的,即host == build == target;
绝大多数交叉编译的情况是host == build,但是target不同;
在canadian交叉编译情况下,三个机器可能都不一样。
.... build .............. host .................. target
-----..........-------..........------
| cross .|...--->...| toolchain .|...--->...| binaries |
-----..........-------..........------

假设我们有一台运行Win2k和Cygwin的PC,我们想在该PC上建立powerpc-eabi的
二进制代码,target上的运行库选用newlib。

我们选定下面的源代码包:

1. Cygwin -- 1.1.7 (with all updates applied to date)
2. GCC -- 2.95.2-6 (part of Cygwin source distribution)
3. Binutils -- 2.10.1 (standard GNU distribution)
4. newlib -- 1.9.0 (http://sources.redhat.com/newlib/)

基本的步骤如下:

Preliminaries: 预备
==============

确定你想要安装的位置以及其它一些环境变量

cygwin$ host=i686-pc-cygwin
cygwin$ build=i686-pc-cygwin
cygwin$ target=powerpc-eabi
cygwin$ prefix=/usr/local/powerpc
cygwin$ src_root=/usr/local/src/gnu

可以根据你自己的机器环境修改$prefix和$src_root,但是最好不要修改$host,
$build和$target。同时,必须确保$src_root目录存在。

Step 1:
=======
(1)获取你所需要的所有源代码包,并放到host上。

cygwin$ cd $src_root
cygwin$ tar zxf /tmp/gcc-2.95.2.tar.gz
cygwin$ tar zxf /tmp/binutils-2.10.1.tar.gz
cygwin$ tar zxf /tmp/newlib-1.9.0.tar.gz

由于我们的host机上运行的是cygwin,所以最好是获取一个cygwin发行的gcc-2.95.2。
可以参看cygwin的主页:http://sources.redhat.com/cygwin/

Step 2:
=======
(2)建立并安装binutils。
注意:千万不要建立在源代码目录上,所以这里强行指定为$src_root/BUILD,并
将在该目录下建立binutils和gcc

cygwin$ mkdir -p $src_root/BUILD/binutils
cygwin$ cd $src_root/BUILD/binutils
cygwin$ $src_root/binutils-2.10.1/configure /
--with-included-gettext /
--target=$target --host=$host --build=$build /
--prefix=$prefix -v
cygwin$ make > make.log 2>&1

如果一切正常,下面开始安装:

cygwin$ make install > install.log 2>&1

注意*IMPORTANT*:一定要记得增加$prefix/bin目录到PATH中

cygwin$ export PATH=$PATH:$prefix/bin

检查一下安装结果:

cygwin$ $target-ld --version
GNU ld 2.10.1
Copyright 2000 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License. This program has absolutely no warranty.
Supported emulations:
elf32ppc

一切正常!:)

Step 3:
=======
(3)建立并"只"安装GCC中的C编译器。

cygwin$ mkdir -p $src_root/BUILD/gcc
cygwin$ cd $src_root/BUILD/gcc
cygwin$ $src_root/gcc-2.95.2/configure /
--enable-languages=c,c++ /
--with-included-gettext --enable-shared --enable-threads /
--target=$target --host=$host --build=$build /
--with-newlib /
--prefix=$prefix -v

可以修改--enable-languages,--enable-shared和--enable-threads。
"必需"--with-newlib,因为它告诉编译器:在建立libgcc.a的时候不要加入
stdlib.h和unistd.h
注意:我们只想建立C编译器!

cygwin$ make LANGUAGES=c all-gcc > make.log 2>&1
cygwin$ mv make.log make-c-only.log

如果一切正常,下面开始安装:

cygwin$ make LANGUAGES=c install-gcc > install.log 2>&1
cygwin$ mv install.log install-c-only.log

Step 4:
=======
(4)使用刚才安装的C编译器建立并安装newlib。

cygwin$ mkdir -p $src_root/BUILD/newlib
cygwin$ cd $src_root/BUILD/newlib
cygwin$ $src_root/newlib-1.9.0/configure /
--target=$target --host=$host --build=$build /
--prefix=$prefix -v

cygwin$ make > make.log 2>&1

如果一切正常,下面开始安装:

cygwin$ make install > install.log 2>&1

Step 5:
=======
(5)建立并安装GCC中的其余部分(支持运行库和其它库文件的编译器和语言)

cygwin$ cd $src_root/BUILD/gcc
cygwin$ make > make.log 2>&1
cygwin$ make install > install.log 2>&1

Postscript: 附言
===========
现在做一个简单示例,创建一个$target的二进制代码。

$ cat > hello.c
#include
int
main()
{
printf("hello world/n");
return 0;
}
^D
$ $target-gcc -o hello hello.c
$ ls -l hello*
hello.c hello.exe

由于我们的目标机是powerpc-eabi,所以可能至少需要一个 -m 参数,比如 -msim

如果希望去掉自动增加的.exe后缀,那么在第三步编译gcc前,修改
$src_root/gcc-2.95.2-6/gcc/config/i386/xm-cygwin.h中的EXECUTABLE_SUFFIX宏,
然后再建立gcc即可。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值