目录
  1. 1. LFS系统构建02-编译基本工具
    1. 1.1. Gnu Binutils
    2. 1.2. GCC
    3. 1.3. Linux-4.20.12
    4. 1.4. glibc
    5. 1.5. Libstdc++
    6. 1.6. Binutils(第二次编译)
    7. 1.7. gcc(第二次编译)
    8. 1.8. tcl
    9. 1.9. expect
    10. 1.10. dejagnu
    11. 1.11. m4
    12. 1.12. ncurses
    13. 1.13. bash
    14. 1.14. bison
    15. 1.15. bzip
    16. 1.16. coreutils
    17. 1.17. diffutils
    18. 1.18. file
    19. 1.19. findutils
    20. 1.20. gettext
    21. 1.21. grep
    22. 1.22. gzip
    23. 1.23. make
    24. 1.24. patch
    25. 1.25. perl
    26. 1.26. python
    27. 1.27. sed
    28. 1.28. tar
    29. 1.29. texinfo
    30. 1.30. xz
    31. 1.31. 移除不必要的文件
    32. 1.32. 改变所有者
LFS系统构建02-编译基本工具

LFS系统构建02-编译基本工具

Gnu Binutils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cd $LFS/sources
xz -d binutils-2.32.tar.xz
tar -xvf binutils-2.32.tar
cd binutils-2.32
mkdir -v build
cd build

#--prefix=/tools : 表示将编译后的文件存放到tools里
#--with-sysroot : 表示讲$LFS设为编译的根目录
#--with-lib-path : 将静态库和动态库放入/tools/lib中
#--target : 将编译中的对象文件放入$LFS_TGT
../configure --prefix=/tools --with-sysroot=$LFS --with-lib-path=/tools/lib --target=$LFS_TGT --disable-nls --disable-werror
#编译
make
#如果是x86_64,需要创建链接到/tools/lib64
case $(uname -m) in
x86_64) mkdir -v /tools/lib && ln -sv lib /tools/lib64 ;;
esac
#安装
make install

GCC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
cd $LFS/sources
xz -d gcc-8.2.0.tar.xz
tar -xvf gcc-8.2.0.tar
cd gcc-8.2.0
#安装gcc需要gmp mpfr mpc
tar xvf ../gmp-6.1.2.tar.xz
mv -v gmp-6.1.2/ gmp
tar xvf ../mpfr-4.0.2.tar.xz
mv -v mpfr-4.0.2/ mpfr
tar -zxvf ../mpc-1.1.0.tar.gz
mv -v mpc-1.1.0/ mpc

#脚本
cd $LFS/sources/gcc-8.2.0/
for file in gcc/config/{linux,i386/linux{,64}}.h
do
cp -uv $file{,.orig}
sed -e 's@/lib(64)\?(32)\?/ld@/tools&@g' \
-e 's@/usr@/tools@g' $file.orig > $file
echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
touch $file.orig
done
#执行结果
'gcc/config/linux.h' -> 'gcc/config/linux.h.orig'
'gcc/config/i386/linux.h' -> 'gcc/config/i386/linux.h.orig'
'gcc/config/i386/linux64.h' -> 'gcc/config/i386/linux64.h.orig'


#设置64为库默认目录名称为lib
#脚本
case $(uname -m) in
x86_64)
sed -e '/m64=/s/lib64/lib/' \
-i.orig gcc/config/i386/t-linux64
;;
esac

mkdir -v build
cd build
../configure --target=$LFS_TGT --prefix=/tools --with-glibc-version=2.17 --with-sysroot=$LFS --with-newlib --without-headers --with-local-prefix=/tools --with-native-system-header-dir=/tools/include --disable-nls --disable-shared --disable-multilib --disable-decimal-float --disable-threads --disable-libatomic --disable-libgomp --disable-libmpx --disable-libquadmath --disable-libssp --disable-libvtv --disable-libstdcxx --enable-languages=c,c++
make
#错误
g++: internal compiler error: Killed (program cc1plus)
#原因
内存不足造成的
#解决
在虚拟机上可以重新给虚拟机分配更大的内存,我的虚拟机只分配了512M内存,直接就造成内存不足
如果还出现内存不足,那可以临时使用交换分区临来解决
sudo dd if=/dev/zero of=/swapfile bs=64M count=16
sudo mkswap /swapfile
sudo swapon /swapfile
使用完之后,就删除
sudo swapoff /swapfile
sudo rm /swapfile

make install

Linux-4.20.12

1
2
3
4
5
6
cd $LFS/sources
tar xvf linux-4.20.12.tar.xz
cd linux-4.20.12
make mrproper
make INSTALL_HDR_PATH=dest headers_install
cp -rv dest/include/* /tools/include/

glibc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cd $LFS/sources
tar xvf glibc-2.29.tar.xz
cd glibc-2.29
mkdir -v build
cd build/
../configure --prefix=/tools --host=$LFS_TGT --build=$(../scripts/config.guess) --enable-kernel=3.10.0 --with-headers=/tools/include libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes
#编译错误(虚拟机centos编译错误,如果是实体机archlinux编译通过)
python版本需要3.7
make版本需要4.2
gmake是make命令的符号链接

#实体机编译安装完全通过
make
make install

#测试
echo 'int main(){}' > dummy.c
$LFS_TGT-gcc dummy.c
readelf -l a.out | grep ': /tools'
#如果编译通过不会出现错误,并且会打印
[Requesting program interpreter: /tools/lib64/ld-linux-x86-64.so.2]
#然后删除测试文件
rm -v dummy.c a.out

Libstdc++

gcc包里面的一个子包

1
2
3
4
5
6
7
8
cd $LFS/sources/
tar xvf gcc-8.2.0.tar.xz
cd gcc-8.2.0
mkdir -v build
cd build/
../libstdc++-v3/configure --host=$LFS_TGT --prefix=/tools --disable-multilib --disable-nls --disable-libstdcxx-threads --disable-libstdcxx-pch --with-gxx-include-dir=/tools/$LFS_TGT/include/c++/8.2.0
make
make install

Binutils(第二次编译)

这一次编译使用第一次编译后的gcc来对其进行编译,以后的编译都会用属于lfs的gcc编译器来对包进行编译。这一次编译binutils,重新生成的binutils包依赖于新的gcc编译器,这使得整个工具链开始脱离宿主机,自形成体系

1
2
3
4
5
6
7
8
9
10
11
12
cd $LFS/sources/
cd binutils-2.32
mkdir -v build
cd build/
CC=$LFS_TGT-gcc AR=$LFS_TGT-ar RANLIB=$LFS_TGT-ranlib ../configure --prefix=/tools --disable-nls --disable-werror --with-lib-path=/tools/lib --with-sysroot
make
make install

#这个准备用在下一章
make -C ld clean
make -C ld LIB_PATH=/usr/lib:/lib
cp -v ld/ld-new /tools/bin

gcc(第二次编译)

第一次编译使用了宿主机的binutils工具集,因为binutils之前已经编译了,现在可以使用lfs的gcc和binutls来对gcc进行编译,使得编译后的gcc彻底成为lfs的编译器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
cd $LFS/sources/
tar xvf gcc-8.2.0.tar.xz
cd gcc-8.2.0
cat gcc/limitx.h gcc/glimits.h gcc/limity.h > `dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/include-fixed/limits.h

#脚本
for file in gcc/config/{linux,i386/linux{,64}}.h
do
cp -uv $file{,.orig}
sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
-e 's@/usr@/tools@g' $file.orig > $file
echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
touch $file.orig
done

case $(uname -m) in
x86_64)
sed -e '/m64=/s/lib64/lib/' \
-i.orig gcc/config/i386/t-linux64
;;
esac

tar -xvf ../mpfr-4.0.2.tar.xz
mv -v mpfr-4.0.2 mpfr
tar -xvf ../gmp-6.1.2.tar.xz
mv -v gmp-6.1.2 gmp
tar -xvf ../mpc-1.1.0.tar.gz
mv -v mpc-1.1.0 mpc

mkdir -v build
cd build/

CC=$LFS_TGT-gcc CXX=$LFS_TGT-g++ AR=$LFS_TGT-ar RANLIB=$LFS_TGT-ranlib ../configure --prefix=/tools --with-local-prefix=/tools --with-native-system-header-dir=/tools/include --enable-languages=c,c++ --disable-libstdcxx-pch --disable-multilib --disable-bootstrap --disable-libgomp
make
make install
ln -sv gcc /tools/bin/cc

echo 'int main(){}' > dummy.c
cc dummy.c
readelf -l a.out | grep ': /tools'

#编译通过会打印,这表示gcc成功编译安装
[Requesting program interpreter: /tools/lib64/ld-linux-x86-64.so.2]

#删除
rm -v dummy.c a.out

tcl

1
2
3
4
5
6
7
8
9
10
11
12
tar xvf tcl8.6.9-src.tar.gz
cd tcl8.6.9
cd unix/
./configure --prefix=/tools
make

TZ=UTC make test
make install

chmod -v u+w /tools/lib/libtcl8.6.so
make install-private-headers
ln -sv tclsh8.6 /tools/bin/tclsh

expect

1
2
3
4
5
6
7
8
9
10
11
cd $LFS/sources
tar -zxvf expect5.45.4.tar.gz
cd expect5.45.4

cp -v configure{,.orig}
sed 's:/usr/local/bin:/bin:' configure.orig > configure

./configure --prefix=/tools --with-tcl=/tools/lib --with-tclinclude=/tools/include
make
make test
make SCRIPTS="" install

dejagnu

1
2
3
4
5
6
cd $LFS/sources/
tar -zxvf dejagnu-1.6.2.tar.gz
cd dejagnu-1.6.2
./configure --prefix=/tools
make install
make check

m4

1
2
3
4
5
6
7
8
9
10
11
cd $LFS/sources/
tar xvf m4-1.4.18.tar.xz
cd m4-1.4.18

sed -i 's/IO_ftrylockfile/IO_EOF_SEEN/' lib/*.c
echo "#define _IO_IN_BACKUP 0x100" >> lib/stdio-impl.h

./configure --prefix=/tools
make
make check
make install

ncurses

1
2
3
4
5
6
7
8
9
cd $LFS/sources/
tar -zxvf ncurses-6.1.tar.gz
cd ncurses-6.1
sed -i s/mawk// configure

./configure --prefix=/tools --with-shared --without-debug --without-ada --enable-widec --enable-overwrite
make
make install
ln -s libncursesw.so /tools/lib/libncurses.so

bash

1
2
3
4
5
6
7
8
cd $LFS/sources/
tar -zxvf bash-5.0.tar.gz
cd bash-5.0
./configure --prefix=/tools --without-bash-malloc
make
make tests
make install
ln -sv bash /tools/bin/sh

bison

1
2
3
4
5
6
7
cd $LFS/sources/
tar xvf bison-3.3.2.tar.xz
cd bison-3.3.2
./configure --prefix=/tools
make
make check
make install

bzip

1
2
3
4
5
cd $LFS/sources
tar -zxvf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6
make
make PREFIX=/tools install

coreutils

1
2
3
4
5
6
7
cd $LFS/sources
tar xvf coreutils-8.30.tar.xz
cd coreutils-8.30
./configure --prefix=/tools --enable-install-program=hostname
make
make RUN_EXPENSIVE_TESTS=yes check
make install

diffutils

1
2
3
4
5
6
7
cd $LFS/sources
tar xvf diffutils-3.7.tar.xz
cd diffutils-3.7
./configure --prefix=/tools
make
make check
make install

file

1
2
3
4
5
6
7
cd $LFS/sources
tar -zxvf file-5.36.tar.gz
./configure --prefix=/tools
cd file-5.36
make
make check
make install

findutils

1
2
3
4
5
6
7
8
9
10
11
12
13
cd $LFS/sources
tar -zxvf findutils-4.6.0.tar.gz
cd findutils-4.6.0


sed -i 's/IO_ftrylockfile/IO_EOF_SEEN/' gl/lib/*.c
sed -i '/unistd/a #include <sys/sysmacros.h>' gl/lib/mountlist.c
echo "#define _IO_IN_BACKUP 0x100" >> gl/lib/stdio-impl.h

./configure --prefix=/tools
make
make check
make install

gettext

1
2
3
4
5
6
7
8
9
10
11
12
13
cd $LFS/sources
tar xvf gettext-0.19.8.1.tar.xz
cd gettext-0.19.8.1
cd gettext-tools/
EMACS="no" ./configure --prefix=/tools --disable-shared

make -C gnulib-lib
make -C intl pluralx.c
make -C src msgfmt
make -C src msgmerge
make -C src xgettext

cp -v src/{msgfmt,msgmerge,xgettext} /tools/bin

grep

1
2
3
4
5
6
tar xvf grep-3.3.tar.xz
cd grep-3.3
./configure --prefix=/tools
make
make check
make install

gzip

1
2
3
4
5
6
tar xvf gzip-1.10.tar.xz
cd gzip-1.10
./configure --prefix=/tools
make
make check
make install

make

1
2
3
4
5
6
7
tar -jxvf make-4.2.1.tar.bz2 
cd make-4.2.1
sed -i '211,217 d; 219,229 d; 232 d' glob/glob.c
./configure --prefix=/tools --without-guile
make
make check
make install

patch

1
2
3
4
5
6
tar xvf patch-2.7.6.tar.xz
cd patch-2.7.6
./configure --prefix=/tools
make
make check
make install

perl

1
2
3
4
5
6
7
tar xvf perl-5.28.1.tar.xz
cd perl-5.28.1
sh Configure -des -Dprefix=/tools -Dlibs=-lm -Uloclibpth -Ulocincpth
make
cp -v perl cpan/podlators/scripts/pod2man /tools/bin
mkdir -pv /tools/lib/perl5/5.28.1
cp -Rv lib/* /tools/lib/perl5/5.28.1

python

1
2
3
4
5
6
7
tar xvf Python-3.7.2.tar.xz
cd Python-3.7.2
sed -i '/def add_multiarch_paths/a \ return' setup.py

./configure --prefix=/tools --without-ensurepip
make #如果编译错误,可能是你用sed加入语句到setup中的格式问题,只需要进入setup.py找到增加的那一行,改一下python语句格式
make install

sed

1
2
3
4
5
6
tar xvf sed-4.7.tar.xz
cd sed-4.7
./configure --prefix=/tools
make
make check
make install

tar

1
2
3
4
5
6
tar xvf tar-1.31.tar.xz
cd tar-1.31
./configure --prefix=/tools
make
make check
make install

texinfo

1
2
3
4
5
tar xvf texinfo-6.5.tar.xz
cd tar-1.31
make
make check
make install

xz

1
2
3
4
5
6
tar xvf xz-5.2.4.tar.xz
cd xz-5.2.4
./configure --prefix=/tools
make
make check
make install

移除不必要的文件

1
2
3
4
strip --strip-debug /tools/lib/*
/usr/bin/strip --strip-unneeded /tools/{,s}bin/*
rm -rf /tools/{,share}/{info,man,doc}
find /tools/{lib,libexec} -name \*.la -delete

改变所有者

1
chown -R root:root $LFS/tools
文章作者: rack-leen
文章链接: http://yoursite.com/2019/06/03/LFS/LFS%E7%B3%BB%E7%BB%9F%E6%9E%84%E5%BB%BA02-%E7%BC%96%E8%AF%91%E5%9F%BA%E6%9C%AC%E5%B7%A5%E5%85%B7/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 rack-leen's blog
打赏
  • 微信
  • 支付宝

评论