Add readline and lua.
[fedora-riscv.git] / Makefile
1 # Refer to the README file to understand how Fedora on RISC-V is
2 # bootstrapped.
3
4 # Absolute path to the current directory.
5 ROOT := $(shell pwd)
6
7 # Note these are chosen very specifically to ensure the different
8 # versions work together.  Don't blindly update to the latest
9 # versions.  See also:
10 # https://github.com/riscv/riscv-pk/issues/18#issuecomment-206115996
11 RISCV_QEMU_COMMIT               = 94f5eb73091fb4fe272db3e943f173ecc0f78ffd
12 RISCV_QEMU_SHORTCOMMIT          = 94f5eb73
13 RISCV_FESVR_COMMIT              = 0f34d7ad311f78455a674224225f5b3056efba1d
14 RISCV_FESVR_SHORTCOMMIT         = 0f34d7ad
15 RISCV_ISA_SIM_COMMIT            = 3bfc00ef2a1b1f0b0472a39a866261b00f67027e
16 RISCV_ISA_SIM_SHORTCOMMIT       = 3bfc00ef
17 RISCV_GNU_TOOLCHAIN_COMMIT      = 728afcddcb0526a0f6560c4032da82805f054d58
18 RISCV_GNU_TOOLCHAIN_SHORTCOMMIT = 728afcdd
19 RISCV_PK_COMMIT                 = 85ae17aa149b9ea114bdd70cc30ea7e73813fb48
20 RISCV_PK_SHORTCOMMIT            = 85ae17aa
21
22 # For the correct versions, see
23 # riscv-gnu-toolchain/Makefile.in *_version variables
24 BINUTILS_VERSION = 2.25.1
25 GLIBC_VERSION    = 2.22
26 GCC_VERSION      = 5.3.0
27 NEWLIB_VERSION   = 2.2.0
28
29 # See linux-4.1.y-riscv branch of
30 # https://github.com/riscv/riscv-linux
31 KERNEL_VERSION   = 4.1.26
32
33 # A local copy of Linux git repo so you don't have to keep downloading
34 # git commits (optional).
35 LOCAL_LINUX_GIT_COPY = $(HOME)/d/linux
36
37 # The root packages (plus their dependencies) that we want to in the
38 # stage 3 chroot.  This must include all the cross-compiled packages
39 # below, and may also include any noarch package we like.
40 STAGE3_PACKAGES = ncurses-devel readline-devel bash coreutils gmp-devel \
41 mpfr-devel mpc-devel binutils gcc gcc-c++ util-linux tar \
42 gzip zlib-devel file-devel popt-devel beecrypt-devel \
43 rpm rpm-build rpm-devel libdb-utils libdb-devel nano \
44 grep less strace bzip2-devel make diffutils findutils \
45 sed patch hostname gettext-devel lua-devel
46
47 # Versions of cross-compiled packages.
48 NCURSES_VERSION    = 6.0-20160730
49 READLINE_VERSION   = 6.3
50 BASH_VERSION       = 4.3
51 COREUTILS_VERSION  = 8.25
52 GMP_VERSION        = 6.1.1
53 MPFR_VERSION       = 3.1.4
54 MPC_VERSION        = 1.0.3
55 BINUTILS_X_VERSION = 2.26
56 GCC_X_VERSION      = 6.1.0
57 UTIL_LINUX_VERSION = 2.28
58 TAR_VERSION        = 1.29
59 GZIP_VERSION       = 1.8
60 ZLIB_VERSION       = 1.2.8
61 # Needs to match the version of 'file' installed (on host), otherwise:
62 #   "Cannot use the installed version of file (xx) to cross-compile file yy"
63 # Also note that 5.25 is definitely broken (segfaults in libmagic:magic_close).
64 FILE_VERSION       = 5.28
65 POPT_VERSION       = 1.16
66 BEECRYPT_VERSION   = 4.2.1
67 RPM_COMMIT         = 95712183458748ea6cafebac1bdd5daa097d9bee
68 RPM_SHORT_COMMIT   = 9571218
69 BDB_VERSION        = 4.5.20
70 NANO_VERSION       = 2.6.2
71 GREP_VERSION       = 2.25
72 LESS_VERSION       = 481
73 STRACE_COMMIT      = f320e1897832fd07a62e18ed288e75d8e79f4c5b
74 STRACE_SHORT_COMMIT = f320e189
75 BZIP2_VERSION      = 1.0.6
76 MAKE_VERSION       = 4.1
77 DIFFUTILS_VERSION  = 3.4
78 FINDUTILS_VERSION  = 4.6.0
79 SED_VERSION        = 4.2
80 PATCH_VERSION      = 2.7.5
81 HOSTNAME_VERSION   = 3.15
82 GETTEXT_VERSION    = 0.19
83 LUA_VERSION        = 5.3.3
84
85 all: stage1 stage2 stage3 stage4
86
87 # Stage 1
88
89 stage1: stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz \
90         stage1-riscv-qemu/riscv-qemu.spec \
91         stamp-riscv-qemu-installed \
92         stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz \
93         stage1-riscv-fesvr/riscv-fesvr.spec \
94         stamp-riscv-fesvr-installed \
95         stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz \
96         stage1-riscv-isa-sim/riscv-isa-sim.spec \
97         stamp-riscv-isa-sim-installed
98
99 stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz:
100         rm -f $@ $@-t
101         wget -O $@-t 'https://github.com/riscv/riscv-qemu/archive/$(RISCV_QEMU_COMMIT)/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz'
102         mv $@-t $@
103
104 stage1-riscv-qemu/riscv-qemu.spec: stage1-riscv-qemu/riscv-qemu.spec.in
105         sed -e 's/@COMMIT@/$(RISCV_QEMU_COMMIT)/g' \
106             -e 's/@SHORTCOMMIT@/$(RISCV_QEMU_SHORTCOMMIT)/g' \
107             < $^ > $@-t
108         mv $@-t $@
109
110 stamp-riscv-qemu-installed:
111         rm -f $@
112         @rpm -q riscv-qemu >/dev/null || { \
113           echo "ERROR: You must install riscv-qemu:"; \
114           echo; \
115           echo "       dnf copr enable rjones/riscv"; \
116           echo "       dnf install riscv-qemu"; \
117           echo; \
118           echo "OR: you can build it yourself from the stage1-riscv-qemu directory."; \
119           echo; \
120           exit 1; \
121         }
122         @qemu-system-riscv --version || { \
123           echo "ERROR: qemu-system-riscv is not working."; \
124           echo "Make sure you installed the riscv-qemu package."; \
125           exit 1; \
126         }
127         touch $@
128
129 stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz:
130         rm -f $@ $@-t
131         wget -O $@-t 'https://github.com/riscv/riscv-fesvr/archive/$(RISCV_FESVR_COMMIT)/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz'
132         mv $@-t $@
133
134 stage1-riscv-fesvr/riscv-fesvr.spec: stage1-riscv-fesvr/riscv-fesvr.spec.in
135         sed -e 's/@COMMIT@/$(RISCV_FESVR_COMMIT)/g' \
136             -e 's/@SHORTCOMMIT@/$(RISCV_FESVR_SHORTCOMMIT)/g' \
137             < $^ > $@-t
138         mv $@-t $@
139
140 stamp-riscv-fesvr-installed:
141         rm -f $@
142         @rpm -q riscv-fesvr >/dev/null || { \
143           echo "ERROR: You must install riscv-fesvr:"; \
144           echo; \
145           echo "       dnf copr enable rjones/riscv"; \
146           echo "       dnf install riscv-fesvr"; \
147           echo; \
148           echo "OR: you can build it yourself from the stage1-riscv-fesvr directory."; \
149           echo; \
150           exit 1; \
151         }
152         touch $@
153
154 stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz:
155         rm -f $@ $@-t
156         wget -O $@-t 'https://github.com/riscv/riscv-isa-sim/archive/$(RISCV_ISA_SIM_COMMIT)/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz'
157         mv $@-t $@
158
159 stage1-riscv-isa-sim/riscv-isa-sim.spec: stage1-riscv-isa-sim/riscv-isa-sim.spec.in
160         sed -e 's/@COMMIT@/$(RISCV_ISA_SIM_COMMIT)/g' \
161             -e 's/@SHORTCOMMIT@/$(RISCV_ISA_SIM_SHORTCOMMIT)/g' \
162             < $^ > $@-t
163         mv $@-t $@
164
165 stamp-riscv-isa-sim-installed:
166         rm -f $@
167         @rpm -q riscv-isa-sim >/dev/null || { \
168           echo "ERROR: You must install riscv-isa-sim:"; \
169           echo; \
170           echo "       dnf copr enable rjones/riscv"; \
171           echo "       dnf install riscv-isa-sim"; \
172           echo; \
173           echo "OR: you can build it yourself from the stage1-riscv-isa-sim directory."; \
174           echo; \
175           exit 1; \
176         }
177         touch $@
178
179 # Stage 2
180
181 stage2: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz \
182         stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz \
183         stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz \
184         stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz \
185         stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz \
186         stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec \
187         stamp-riscv-gnu-toolchain-installed \
188         stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz \
189         stage2-riscv-pk/riscv-pk.spec \
190         stamp-riscv-pk-installed
191
192 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz:
193         rm -f $@ $@-t
194         wget -O $@-t https://github.com/lowRISC/riscv-gnu-toolchain/archive/$(RISCV_GNU_TOOLCHAIN_COMMIT)/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz
195         mv $@-t $@
196
197 stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz:
198         rm -f $@ $@-t
199         wget -O $@-t http://mirrors.kernel.org/gnu/binutils/binutils-$(BINUTILS_VERSION).tar.gz
200         mv $@-t $@
201
202 # GCC 5 no longer compiles with GCC 6 unless we patch it.
203 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69959
204 stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz:
205         rm -f $@ $@-t
206         wget -O $@-t http://mirrors.kernel.org/gnu/gcc/gcc-$(GCC_VERSION)/gcc-$(GCC_VERSION).tar.gz
207         zcat $@-t | tar xf -
208         cd gcc-$(GCC_VERSION) && patch -p0 < ../stage2-riscv-gnu-toolchain/gcc-5-fix-compilation-with-gcc-6.patch
209         tar zcf $@-t gcc-$(GCC_VERSION)
210         rm -r gcc-$(GCC_VERSION)
211         mv $@-t $@
212
213 stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz:
214         rm -f $@ $@-t
215         wget -O $@-t http://mirrors.kernel.org/gnu/glibc/glibc-$(GLIBC_VERSION).tar.gz
216         mv $@-t $@
217
218 stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz:
219         rm -f $@ $@-t
220         wget -O $@-t ftp://sourceware.org/pub/newlib/newlib-$(NEWLIB_VERSION).tar.gz
221         mv $@-t $@
222
223 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec.in
224         sed -e 's/@COMMIT@/$(RISCV_GNU_TOOLCHAIN_COMMIT)/g' \
225             -e 's/@SHORTCOMMIT@/$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT)/g' \
226             -e 's/@BINUTILS_VERSION@/$(BINUTILS_VERSION)/g' \
227             -e 's/@GCC_VERSION@/$(GCC_VERSION)/g' \
228             -e 's/@GLIBC_VERSION@/$(GLIBC_VERSION)/g' \
229             -e 's/@NEWLIB_VERSION@/$(NEWLIB_VERSION)/g' \
230             < $^ > $@-t
231         mv $@-t $@
232
233 stamp-riscv-gnu-toolchain-installed:
234         rm -f $@
235         @rpm -q riscv-gnu-toolchain >/dev/null || { \
236           echo "ERROR: You must install riscv-gnu-toolchain:"; \
237           echo; \
238           echo "       dnf copr enable rjones/riscv"; \
239           echo "       dnf install riscv-gnu-toolchain"; \
240           echo; \
241           echo "OR: you can build it yourself from the stage2-riscv-gnu-toolchain directory."; \
242           echo; \
243           exit 1; \
244         }
245         @riscv64-unknown-elf-gcc --version || { \
246           echo "ERROR: riscv64-unknown-elf-gcc (cross compiler) is not working."; \
247           echo "Make sure you installed the riscv-gnu-toolchain package."; \
248           exit 1; \
249         }
250         touch $@
251
252 stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz:
253         rm -f $@ $@-t
254         wget -O $@-t https://github.com/lowRISC/riscv-pk/archive/$(RISCV_PK_COMMIT)/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz
255         mv $@-t $@
256
257 stage2-riscv-pk/riscv-pk.spec: stage2-riscv-pk/riscv-pk.spec.in
258         sed -e 's/@COMMIT@/$(RISCV_PK_COMMIT)/g' \
259             -e 's/@SHORTCOMMIT@/$(RISCV_PK_SHORTCOMMIT)/g' \
260             < $^ > $@-t
261         mv $@-t $@
262
263 stamp-riscv-pk-installed:
264         rm -f $@
265         @rpm -q riscv-pk >/dev/null || { \
266           echo "ERROR: You must install riscv-pk:"; \
267           echo; \
268           echo "       dnf copr enable rjones/riscv"; \
269           echo "       dnf install riscv-pk"; \
270           echo; \
271           echo "OR: you can build it yourself from the stage2-riscv-pk directory."; \
272           echo; \
273           exit 1; \
274         }
275         touch $@
276
277 # Stage 3
278
279 stage3: stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
280         stage3-chroot-original/etc/fedora-release \
281         stage3-chroot/etc/fedora-release \
282         stage3-chroot/lib64/libc.so.6 \
283         stage3-chroot/usr/bin/tic \
284         stage3-chroot/usr/lib64/libhistory.so.6 \
285         stage3-chroot/bin/bash \
286         stage3-chroot/bin/ls \
287         stage3-chroot/usr/lib64/libgmp.so.10 \
288         stage3-chroot/usr/lib64/libmpfr.so.4 \
289         stage3-chroot/usr/lib64/libmpc.so.3 \
290         stage3-chroot/usr/bin/as \
291         stage3-chroot/usr/bin/gcc \
292         stage3-chroot/usr/bin/mount \
293         stage3-chroot/usr/bin/tar \
294         stage3-chroot/usr/bin/gzip \
295         stage3-chroot/usr/lib64/libz.so \
296         stage3-chroot/usr/bin/file \
297         stage3-chroot/usr/lib64/libpopt.so \
298         stage3-chroot/usr/lib64/libbeecrypt.so \
299         stage3-chroot/usr/bin/nano \
300         stage3-chroot/usr/bin/grep \
301         stage3-chroot/usr/bin/less \
302         stage3-chroot/usr/bin/strace \
303         stage3-chroot/usr/bin/bzip2 \
304         stage3-chroot/usr/bin/make \
305         stage3-chroot/usr/bin/diff \
306         stage3-chroot/usr/bin/find \
307         stage3-chroot/usr/bin/sed \
308         stage3-chroot/usr/bin/patch \
309         stage3-chroot/usr/bin/hostname \
310         stage3-chroot/usr/bin/gettext \
311         stage3-chroot/usr/bin/lua \
312         stage3-chroot/usr/bin/rpm \
313         stage3-chroot/init \
314         stage3-disk.img
315
316 stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux:
317         rm -rf stage3-kernel/linux-$(KERNEL_VERSION)
318         cp -a $(LOCAL_LINUX_GIT_COPY) stage3-kernel/linux-$(KERNEL_VERSION) || { \
319           mkdir stage3-kernel/linux-$(KERNEL_VERSION) && \
320           cd stage3-kernel/linux-$(KERNEL_VERSION) && \
321           git init; \
322         }
323         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
324         git remote add riscv https://github.com/riscv/riscv-linux && \
325         git fetch riscv && \
326         git checkout -f linux-4.1.y-riscv && \
327         make mrproper && \
328         make ARCH=riscv defconfig
329         echo CONFIG_CMDLINE=\"root=/dev/htifblk0 init=/init\" >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
330         echo CONFIG_CROSS_COMPILE=riscv64-unknown-elf- >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
331         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
332         make ARCH=riscv olddefconfig
333         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
334         make ARCH=riscv vmlinux
335         ls -l $@
336
337 # Build an original (x86-64) chroot using supermin.  We then aim to
338 # rebuild (using cross-compiled versions) every ELF binary in this
339 # chroot.
340 stage3-chroot-original/etc/fedora-release:
341         rm -rf stage3-chroot-original tmp-supermin.d
342         supermin --prepare $(STAGE3_PACKAGES) -o tmp-supermin.d
343         supermin --build -f chroot tmp-supermin.d -o stage3-chroot-original
344         rm -r tmp-supermin.d
345         @echo -n "Total files in chroot: "
346         @find stage3-chroot-original -type f | wc -l
347         @echo -n "ELF files to be rebuilt: "
348         @find stage3-chroot-original -type f | xargs file -N | grep -E '\bELF.*LSB\b' | wc -l
349
350 # Copy the original chroot to the final chroot, remove all the ELF
351 # files.
352 stage3-chroot/etc/fedora-release: stage3-chroot-original/etc/fedora-release
353         rm -rf stage3-chroot
354         cp -a stage3-chroot-original stage3-chroot
355         find stage3-chroot -type d -print0 | xargs -0 chmod u+w
356         find stage3-chroot -type f -print0 | xargs -0 chmod u+w
357         find stage3-chroot -type f -print0 | xargs -0 file -N | grep -E '\bELF.*LSB\b' | awk -F: '{print $$1}' | xargs rm -f
358         rm -f stage3-chroot/lib64/libc.so.6
359
360 # Copy in compiled glibc from the riscv-gnu-toolchain sysroot.  Only
361 # copy files and symlinks, leave the target directory structure
362 # intact.
363 stage3-chroot/lib64/libc.so.6:
364         mkdir -p stage3-chroot/usr/lib/audit
365         mkdir -p stage3-chroot/usr/lib/gconv
366         for f in `cd /usr/sysroot && find -type f -o -type l`; do \
367             cp -d /usr/sysroot/$$f stage3-chroot/$$f; \
368         done
369         cd stage3-chroot/lib64 && for f in ../lib/*; do ln -sf $$f; done
370
371 # Cross-compile ncurses.
372 stage3-chroot/usr/bin/tic: ncurses-$(NCURSES_VERSION).tgz
373         tar zxf $^
374         cd ncurses-$(NCURSES_VERSION) && \
375         PATH=$(ROOT)/fixed-gcc:$$PATH \
376         ./configure --host=riscv64-unknown-linux-gnu \
377             --prefix=/usr --libdir=/usr/lib64 \
378             --with-shared \
379             --with-termlib=tinfo \
380             --enable-widec
381         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
382         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
383
384 ncurses-$(NCURSES_VERSION).tgz:
385         rm -f $@ $@-t
386         wget -O $@-t ftp://invisible-island.net/ncurses/current/ncurses-$(NCURSES_VERSION).tgz
387         mv $@-t $@
388
389 # Cross-compile readline.
390 stage3-chroot/usr/lib64/libhistory.so.6: readline-$(READLINE_VERSION).tar.gz
391         tar zxf $^
392         cd readline-$(READLINE_VERSION) && \
393         PATH=$(ROOT)/fixed-gcc:$$PATH \
394         bash_cv_wcwidth_broken=no \
395         ./configure --host=riscv64-unknown-linux-gnu \
396             --prefix=/usr --libdir=/usr/lib64
397         cd readline-$(READLINE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
398         cd readline-$(READLINE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
399         rm -f stage3-chroot/usr/lib64/*.la
400
401 readline-$(READLINE_VERSION).tar.gz:
402         rm -f $@ $@-t
403         wget -O $@-t ftp://ftp.gnu.org/gnu/readline/readline-$(READLINE_VERSION).tar.gz
404         mv $@-t $@
405
406 # Cross-compile bash.
407 stage3-chroot/bin/bash: bash-$(BASH_VERSION).tar.gz
408         tar zxf $^
409         cd bash-$(BASH_VERSION) && \
410         PATH=$(ROOT)/fixed-gcc:$$PATH \
411         ./configure --host=riscv64-unknown-linux-gnu \
412             --prefix=/usr --libdir=/usr/lib64
413         cd bash-$(BASH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
414         cd bash-$(BASH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
415
416 bash-$(BASH_VERSION).tar.gz:
417         rm -f $@ $@-t
418         wget -O $@-t ftp://ftp.gnu.org/gnu/bash/bash-$(BASH_VERSION).tar.gz
419         mv $@-t $@
420
421 # Cross-compile coreutils.  Bleah, coreutils cross-compilation is
422 # known-broken and upstream don't care, hence the 'touch' command.
423
424 COREUTILS_PROGRAMS = arch base32 base64 basename cat chcon chgrp chmod chown chroot cksum comm cp csplit cut date dd df dir dircolors dirname du echo env expand expr factor false fmt fold ginstall groups head hostid hostname id install join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup nproc numfmt od paste pathchk pinky pr printenv printf ptx pwd readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum sha384sum sha512sum shred shuf sleep sort split stat stdbuf stty sum sync tac tail tee test timeout touch tr true truncate tsort tty uname unexpand uniq unlink uptime users vdir wc who whoami yes
425
426 stage3-chroot/bin/ls: coreutils-$(COREUTILS_VERSION).tar.xz
427         rm -rf coreutils-$(COREUTILS_VERSION)
428         tar Jxf $^
429         cd coreutils-$(COREUTILS_VERSION) && \
430         PATH=$(ROOT)/fixed-gcc:$$PATH \
431         ./configure --host=riscv64-unknown-linux-gnu \
432             --prefix=/usr --libdir=/usr/lib64
433         -cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
434         cd coreutils-$(COREUTILS_VERSION)/man && \
435         for f in $(COREUTILS_PROGRAMS); do touch $$f.1; done
436         cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
437         cd coreutils-$(COREUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
438
439 coreutils-$(COREUTILS_VERSION).tar.xz:
440         rm -f $@ $@-t
441         wget -O $@-t ftp://ftp.gnu.org/gnu/coreutils/coreutils-$(COREUTILS_VERSION).tar.xz
442         mv $@-t $@
443
444 # Cross-compile binutils.
445 stage3-chroot/usr/bin/as: binutils-$(BINUTILS_X_VERSION).tar.gz
446         rm -rf riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)
447         zcat $^ | tar xf -
448         mkdir riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build
449         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && \
450         PATH=$(ROOT)/fixed-gcc:$$PATH \
451         ../configure \
452             --host=riscv64-unknown-linux-gnu \
453             --prefix=/usr --libdir=/usr/lib64
454         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make
455         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && make DESTDIR=$(ROOT)/stage3-chroot install
456
457 binutils-$(BINUTILS_X_VERSION).tar.gz:
458         rm -f $@ $@-t
459         wget -O $@-t https://github.com/riscv/riscv-binutils-gdb/archive/riscv-binutils-$(BINUTILS_X_VERSION).tar.gz
460         mv $@-t $@
461
462 # Cross-compile GMP, MPFR and MPC (deps of GCC).
463 stage3-chroot/usr/lib64/libgmp.so.10: gmp-$(GMP_VERSION).tar.lz
464         rm -rf gmp-$(GMP_VERSION)
465         tar --lzip -xf gmp-$(GMP_VERSION).tar.lz
466         cd gmp-$(GMP_VERSION) && \
467         PATH=$(ROOT)/fixed-gcc:$$PATH \
468         ./configure --host=riscv64-unknown-linux-gnu \
469             --prefix=/usr --libdir=/usr/lib64
470         cd gmp-$(GMP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
471         cd gmp-$(GMP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
472         cd stage3-chroot/usr/lib && ln -s ../lib64/libgmp.so
473
474 gmp-$(GMP_VERSION).tar.lz:
475         rm -f $@ $@-t
476         wget -O $@-t https://gmplib.org/download/gmp/gmp-$(GMP_VERSION).tar.lz
477         mv $@-t $@
478
479 stage3-chroot/usr/lib64/libmpfr.so.4: mpfr-$(MPFR_VERSION).tar.gz
480         rm -rf mpfr-$(MPFR_VERSION)
481         tar -zxf mpfr-$(MPFR_VERSION).tar.gz
482         cd mpfr-$(MPFR_VERSION) && \
483         PATH=$(ROOT)/fixed-gcc:$$PATH \
484         ./configure --host=riscv64-unknown-linux-gnu \
485             --prefix=/usr --libdir=/usr/lib64 \
486             --with-gmp=$(ROOT)/stage3-chroot/usr
487         cd mpfr-$(MPFR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
488         cd mpfr-$(MPFR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
489         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpfr.so
490         rm -f stage3-chroot/usr/lib64/*.la
491
492 mpfr-$(MPFR_VERSION).tar.gz:
493         rm -f $@ $@-t
494         wget -O $@-t http://www.mpfr.org/mpfr-current/mpfr-$(MPFR_VERSION).tar.gz
495         mv $@-t $@
496
497 stage3-chroot/usr/lib64/libmpc.so.3: mpc-$(MPC_VERSION).tar.gz
498         rm -rf mpc-$(MPC_VERSION)
499         tar -zxf mpc-$(MPC_VERSION).tar.gz
500         cd mpc-$(MPC_VERSION) && \
501         PATH=$(ROOT)/fixed-gcc:$$PATH \
502         ./configure --host=riscv64-unknown-linux-gnu \
503             --prefix=/usr --libdir=/usr/lib64 \
504             --with-gmp=$(ROOT)/stage3-chroot/usr \
505             --with-mpfr=$(ROOT)/stage3-chroot/usr
506         cd mpc-$(MPC_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
507         cd mpc-$(MPC_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
508         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpc.so
509         rm -f stage3-chroot/usr/lib64/*.la
510
511 mpc-$(MPC_VERSION).tar.gz:
512         rm -f $@ $@-t
513         wget -O $@-t ftp://ftp.gnu.org/gnu/mpc/mpc-$(MPC_VERSION).tar.gz
514         mv $@-t $@
515
516 # Cross-compile GCC.
517 stage3-chroot/usr/bin/gcc: gcc-$(GCC_X_VERSION).tar.gz
518         rm -rf riscv-gcc-riscv-gcc-$(GCC_X_VERSION)
519         zcat $^ | tar xf -
520         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION) && \
521         patch -p1 < ../0001-HACKS-TO-GET-GCC-TO-COMPILE.patch
522         mkdir riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build
523         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && \
524         PATH=$(ROOT)/fixed-gcc:$$PATH \
525         ../configure \
526             --host=riscv64-unknown-linux-gnu \
527             --prefix=/usr --libdir=/usr/lib64 \
528             --enable-shared \
529             --enable-tls \
530             --enable-languages=c,c++ \
531             --disable-libmudflap \
532             --disable-libssp \
533             --disable-libquadmath \
534             --disable-nls \
535             --disable-multilib
536         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make
537         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && make install DESTDIR=$(ROOT)/stage3-chroot
538         rm -f stage3-chroot/usr/lib64/*.la
539
540 gcc-$(GCC_X_VERSION).tar.gz:
541         rm -f $@ $@-t
542         wget -O $@-t https://github.com/riscv/riscv-gcc/archive/riscv-gcc-$(GCC_X_VERSION).tar.gz
543         mv $@-t $@
544
545 # Cross-compile util-linux.
546 stage3-chroot/usr/bin/mount: util-linux-$(UTIL_LINUX_VERSION).tar.xz
547         rm -rf util-linux-$(UTIL_LINUX_VERSION)
548         tar -Jxf $^
549         cd util-linux-$(UTIL_LINUX_VERSION) && \
550         PATH=$(ROOT)/fixed-gcc:$$PATH \
551         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
552         ./configure \
553             --host=riscv64-unknown-linux-gnu \
554             --prefix=/usr --libdir=/usr/lib64 \
555             --without-python \
556             --without-systemd \
557             --disable-makeinstall-chown \
558             --enable-static-programs=mount
559         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
560         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
561         rm -f stage3-chroot/usr/lib64/*.la
562
563 util-linux-$(UTIL_LINUX_VERSION).tar.xz:
564         rm -f $@ $@-t
565         wget -O $@-t ftp://ftp.kernel.org/pub/linux/utils/util-linux/v$(UTIL_LINUX_VERSION)/util-linux-$(UTIL_LINUX_VERSION).tar.xz
566         mv $@-t $@
567
568 # Cross-compile GNU tar.
569 stage3-chroot/usr/bin/tar: tar-$(TAR_VERSION).tar.xz
570         rm -rf tar-$(TAR_VERSION)
571         tar -Jxf $^
572         cd tar-$(TAR_VERSION) && \
573         PATH=$(ROOT)/fixed-gcc:$$PATH \
574         ./configure \
575             --host=riscv64-unknown-linux-gnu \
576             --prefix=/usr --libdir=/usr/lib64
577         cd tar-$(TAR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
578         cd tar-$(TAR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
579
580 tar-$(TAR_VERSION).tar.xz:
581         rm -f $@ $@-t
582         wget -O $@-t https://ftp.gnu.org/gnu/tar/tar-$(TAR_VERSION).tar.xz
583         mv $@-t $@
584
585 # Cross-compile GNU gzip.
586 stage3-chroot/usr/bin/gzip: gzip-$(GZIP_VERSION).tar.gz
587         rm -rf gzip-$(GZIP_VERSION)
588         tar -zxf $^
589         cd gzip-$(GZIP_VERSION) && \
590         PATH=$(ROOT)/fixed-gcc:$$PATH \
591         ./configure \
592             --host=riscv64-unknown-linux-gnu \
593             --prefix=/usr --libdir=/usr/lib64
594         cd gzip-$(GZIP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
595         cd gzip-$(GZIP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
596
597 gzip-$(GZIP_VERSION).tar.gz:
598         rm -f $@ $@-t
599         wget -O $@-t https://ftp.gnu.org/gnu/gzip/gzip-$(GZIP_VERSION).tar.gz
600         mv $@-t $@
601
602 # Cross-compile zlib.
603 stage3-chroot/usr/lib64/libz.so: zlib-$(ZLIB_VERSION).tar.gz
604         rm -rf zlib-$(ZLIB_VERSION)
605         tar -zxf $^
606         cd zlib-$(ZLIB_VERSION) && \
607         PATH=$(ROOT)/fixed-gcc:$$PATH \
608         CC=riscv64-unknown-linux-gnu-gcc \
609         CFLAGS="-I$(ROOT)/stage3-chroot/usr/include -L$(ROOT)/stage3-chroot/usr/lib" \
610         ./configure \
611             --prefix=/usr --libdir=/usr/lib64
612         cd zlib-$(ZLIB_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make shared
613         cd zlib-$(ZLIB_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
614
615 zlib-$(ZLIB_VERSION).tar.gz:
616         rm -f $@ $@-t
617         wget -O $@-t http://zlib.net/zlib-$(ZLIB_VERSION).tar.gz
618         mv $@-t $@
619
620 # Cross-compile file/libmagic.
621 stage3-chroot/usr/bin/file: file-$(FILE_VERSION).tar.gz
622         rm -rf file-$(FILE_VERSION)
623         tar -zxf $^
624         cd file-$(FILE_VERSION) && \
625         PATH=$(ROOT)/fixed-gcc:$$PATH \
626         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
627         ./configure \
628             --host=riscv64-unknown-linux-gnu \
629             --prefix=/usr --libdir=/usr/lib64 \
630             --disable-static --enable-shared
631         cd file-$(FILE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
632         cd file-$(FILE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
633         rm -f stage3-chroot/usr/lib64/*.la
634
635 file-$(FILE_VERSION).tar.gz:
636         rm -f $@ $@-t
637         wget -O $@-t ftp://ftp.astron.com/pub/file/file-$(FILE_VERSION).tar.gz
638         mv $@-t $@
639
640 # Cross-compile popt.
641 stage3-chroot/usr/lib64/libpopt.so: popt-$(POPT_VERSION).tar.gz
642         rm -rf popt-$(POPT_VERSION)
643         tar -zxf $^
644         cd popt-$(POPT_VERSION) && \
645         PATH=$(ROOT)/fixed-gcc:$$PATH \
646         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
647         ./configure \
648             --host=riscv64-unknown-linux-gnu \
649             --prefix=/usr --libdir=/usr/lib64 \
650             --disable-static --enable-shared
651         cd popt-$(POPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
652         cd popt-$(POPT_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
653         rm -f stage3-chroot/usr/lib64/*.la
654
655 popt-$(POPT_VERSION).tar.gz:
656         rm -f $@ $@-t
657         wget -O $@-t http://rpm5.org/files/popt/popt-$(POPT_VERSION).tar.gz
658         mv $@-t $@
659
660 # Cross-compile beecrypt.
661 stage3-chroot/usr/lib64/libbeecrypt.so: beecrypt-$(BEECRYPT_VERSION).tar.gz
662         rm -rf beecrypt-$(BEECRYPT_VERSION)
663         tar -zxf $^
664         cd beecrypt-$(BEECRYPT_VERSION) && patch -p0 < ../beecrypt-disable-cplusplus.patch
665         cd beecrypt-$(BEECRYPT_VERSION) && autoreconf -i
666         cd beecrypt-$(BEECRYPT_VERSION) && \
667         PATH=$(ROOT)/fixed-gcc:$$PATH \
668         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
669         ./configure \
670             --host=riscv64-unknown-linux-gnu \
671             --prefix=/usr --libdir=/usr/lib64 \
672             --without-cplusplus \
673             --without-java \
674             --disable-openmp \
675             --disable-static \
676             --enable-shared
677         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
678         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot V=1
679         chrpath -d stage3-chroot/usr/lib64/libbeecrypt.so.*
680         rm -f stage3-chroot/usr/lib64/*.la
681
682 beecrypt-$(BEECRYPT_VERSION).tar.gz:
683         rm -f $@ $@-t
684         wget -O $@-t http://downloads.sourceforge.net/sourceforge/beecrypt/beecrypt-$(BEECRYPT_VERSION).tar.gz
685         mv $@-t $@
686
687 # Cross-compile GNU nano (editor).
688 stage3-chroot/usr/bin/nano: nano-$(NANO_VERSION).tar.gz
689         rm -rf nano-$(NANO_VERSION)
690         tar -zxf $^
691         cd nano-$(NANO_VERSION) && \
692         PATH=$(ROOT)/fixed-gcc:$$PATH \
693         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
694         ./configure \
695             --host=riscv64-unknown-linux-gnu \
696             --prefix=/usr --libdir=/usr/lib64
697         cd nano-$(NANO_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
698         cd nano-$(NANO_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
699
700 nano-$(NANO_VERSION).tar.gz:
701         rm -f $@ $@-t
702         wget -O $@-t https://www.nano-editor.org/dist/v2.6/nano-$(NANO_VERSION).tar.gz
703         mv $@-t $@
704
705 # Cross-compile GNU grep.
706 stage3-chroot/usr/bin/grep: grep-$(GREP_VERSION).tar.xz
707         rm -rf grep-$(GREP_VERSION)
708         tar -Jxf $^
709         cd grep-$(GREP_VERSION) && \
710         PATH=$(ROOT)/fixed-gcc:$$PATH \
711         ./configure \
712             --host=riscv64-unknown-linux-gnu \
713             --prefix=/usr --libdir=/usr/lib64
714         cd grep-$(GREP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
715         cd grep-$(GREP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
716
717 grep-$(GREP_VERSION).tar.xz:
718         rm -f $@ $@-t
719         wget -O $@-t https://ftp.gnu.org/gnu/grep/grep-$(GREP_VERSION).tar.xz
720         mv $@-t $@
721
722 # Cross-compile less.
723 stage3-chroot/usr/bin/less: less-$(LESS_VERSION).tar.gz
724         rm -rf less-$(LESS_VERSION)
725         tar -zxf $^
726         cd less-$(LESS_VERSION) && \
727         PATH=$(ROOT)/fixed-gcc:$$PATH \
728         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
729         ./configure \
730             --host=riscv64-unknown-linux-gnu \
731             --prefix=/usr --libdir=/usr/lib64
732         cd less-$(LESS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
733         cd less-$(LESS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
734
735 less-$(LESS_VERSION).tar.gz:
736         rm -f $@ $@-t
737         wget -O $@-t http://www.greenwoodsoftware.com/less/less-$(LESS_VERSION).tar.gz
738         mv $@-t $@
739
740 # Cross-compile strace.
741 # XXX This does not work.
742 stage3-chroot/usr/bin/strace: strace-$(STRACE_SHORT_COMMIT).tar.gz
743         rm -rf strace-$(STRACE_SHORT_COMMIT)
744         tar -zxf $^
745         cd riscv-strace-$(STRACE_COMMIT) && patch -p1 < ../0001-Update-riscv_regs-for-ptrace.h-from-Linux-4.1.x.patch
746         cd riscv-strace-$(STRACE_COMMIT) && \
747         PATH=$(ROOT)/fixed-gcc:$$PATH \
748         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
749         ./configure \
750             --host=riscv64-unknown-linux-gnu \
751             --prefix=/usr --libdir=/usr/lib64
752         cd riscv-strace-$(STRACE_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make
753         cd riscv-strace-$(STRACE_COMMIT) && make install DESTDIR=$(ROOT)/stage3-chroot
754
755 strace-$(STRACE_SHORT_COMMIT).tar.gz:
756         rm -f $@ $@-t
757         wget -O $@-t 'https://github.com/riscv/riscv-strace/archive/$(STRACE_COMMIT)/riscv-strace-$(STRACE_SHORTCOMMIT).tar.gz'
758         mv $@-t $@
759
760 # Cross-compile bzip2.
761 stage3-chroot/usr/bin/bzip2: bzip2-$(BZIP2_VERSION).tar.gz
762         rm -rf bzip2-$(BZIP2_VERSION)
763         tar -zxf $^
764         cd bzip2-$(BZIP2_VERSION) && \
765         PATH=$(ROOT)/fixed-gcc:$$PATH \
766         make libbz2.a bzip2 bzip2recover \
767         PREFIX=/usr \
768         CC=riscv64-unknown-linux-gnu-gcc \
769         AR=riscv64-unknown-linux-gnu-ar \
770         RANLIB=riscv64-unknown-linux-gnu-ranlib \
771         CFLAGS="-Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64 -fPIC"
772         cd bzip2-$(BZIP2_VERSION) && \
773         make install PREFIX=$(ROOT)/stage3-chroot/usr
774
775 bzip2-$(BZIP2_VERSION).tar.gz:
776         rm -f $@ $@-t
777         wget -O $@-t http://www.bzip.org/1.0.6/bzip2-$(BZIP2_VERSION).tar.gz
778         mv $@-t $@
779
780 # Cross-compile GNU make.
781 stage3-chroot/usr/bin/make: make-$(MAKE_VERSION).tar.gz
782         rm -rf make-$(MAKE_VERSION)
783         tar -zxf $^
784         cd make-$(MAKE_VERSION) && \
785         PATH=$(ROOT)/fixed-gcc:$$PATH \
786         ./configure \
787             --host=riscv64-unknown-linux-gnu \
788             --prefix=/usr --libdir=/usr/lib64
789         cd make-$(MAKE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
790         cd make-$(MAKE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
791
792 make-$(MAKE_VERSION).tar.gz:
793         rm -f $@ $@-t
794         wget -O $@-t https://ftp.gnu.org/gnu/make/make-$(MAKE_VERSION).tar.gz
795         mv $@-t $@
796
797 # Cross-compile GNU diffutils.
798 stage3-chroot/usr/bin/diff: diffutils-$(DIFFUTILS_VERSION).tar.xz
799         rm -rf diffutils-$(DIFFUTILS_VERSION)
800         tar -Jxf $^
801         cd diffutils-$(DIFFUTILS_VERSION) && \
802         PATH=$(ROOT)/fixed-gcc:$$PATH \
803         ./configure \
804             --host=riscv64-unknown-linux-gnu \
805             --prefix=/usr --libdir=/usr/lib64
806         cd diffutils-$(DIFFUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
807         cd diffutils-$(DIFFUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
808
809 diffutils-$(DIFFUTILS_VERSION).tar.xz:
810         rm -f $@ $@-t
811         wget -O $@-t https://ftp.gnu.org/gnu/diffutils/diffutils-$(DIFFUTILS_VERSION).tar.xz
812         mv $@-t $@
813
814 # Cross-compile GNU findutils.
815 stage3-chroot/usr/bin/find: findutils-$(FINDUTILS_VERSION).tar.gz
816         rm -rf findutils-$(FINDUTILS_VERSION)
817         tar -zxf $^
818         cd findutils-$(FINDUTILS_VERSION) && \
819         PATH=$(ROOT)/fixed-gcc:$$PATH \
820         ./configure \
821             --host=riscv64-unknown-linux-gnu \
822             --prefix=/usr --libdir=/usr/lib64
823         cd findutils-$(FINDUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
824         cd findutils-$(FINDUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
825
826 findutils-$(FINDUTILS_VERSION).tar.gz:
827         rm -f $@ $@-t
828         wget -O $@-t https://ftp.gnu.org/gnu/findutils/findutils-$(FINDUTILS_VERSION).tar.gz
829         mv $@-t $@
830
831 # Cross-compile GNU sed.
832 stage3-chroot/usr/bin/sed: sed-$(SED_VERSION).tar.gz
833         rm -rf sed-$(SED_VERSION)
834         tar -zxf $^
835         cd sed-$(SED_VERSION) && \
836         PATH=$(ROOT)/fixed-gcc:$$PATH \
837         ./configure \
838             --host=riscv64-unknown-linux-gnu \
839             --prefix=/usr --libdir=/usr/lib64
840         cd sed-$(SED_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
841         cd sed-$(SED_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
842
843 sed-$(SED_VERSION).tar.gz:
844         rm -f $@ $@-t
845         wget -O $@-t https://ftp.gnu.org/gnu/sed/sed-$(SED_VERSION).tar.gz
846         mv $@-t $@
847
848 # Cross-compile patch.
849 stage3-chroot/usr/bin/patch: patch-$(PATCH_VERSION).tar.gz
850         rm -rf patch-$(PATCH_VERSION)
851         tar -zxf $^
852         cd patch-$(PATCH_VERSION) && \
853         PATH=$(ROOT)/fixed-gcc:$$PATH \
854         ./configure \
855             --host=riscv64-unknown-linux-gnu \
856             --prefix=/usr --libdir=/usr/lib64
857         cd patch-$(PATCH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
858         cd patch-$(PATCH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
859
860 patch-$(PATCH_VERSION).tar.gz:
861         rm -f $@ $@-t
862         wget -O $@-t https://ftp.gnu.org/gnu/patch/patch-$(PATCH_VERSION).tar.gz
863         mv $@-t $@
864
865 # Cross-compile hostname.
866 stage3-chroot/usr/bin/hostname: hostname-$(HOSTNAME_VERSION).tar.gz
867         rm -rf hostname-$(HOSTNAME_VERSION)
868         tar -zxf $^
869         cd hostname && patch -p1 < ../hostname-rh.patch
870         cd hostname && \
871         PATH=$(ROOT)/fixed-gcc:$$PATH \
872         make \
873         CC=riscv64-unknown-linux-gnu-gcc \
874         CFLAGS="-O2 -g"
875         cd hostname && make install BASEDIR=$(ROOT)/stage3-chroot
876
877 hostname-$(HOSTNAME_VERSION).tar.gz:
878         rm -f $@ $@-t
879         wget -O $@-t http://ftp.de.debian.org/debian/pool/main/h/hostname/hostname_$(HOSTNAME_VERSION).tar.gz
880         mv $@-t $@
881
882 # Cross-compile GNU gettext.
883 stage3-chroot/usr/bin/gettext: gettext-$(GETTEXT_VERSION).tar.gz
884         rm -rf gettext-$(GETTEXT_VERSION)
885         tar -zxf $^
886         cd gettext-$(GETTEXT_VERSION) && \
887         PATH=$(ROOT)/fixed-gcc:$$PATH \
888         ./configure \
889             --host=riscv64-unknown-linux-gnu \
890             --prefix=/usr --libdir=/usr/lib64
891         cd gettext-$(GETTEXT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
892         cd gettext-$(GETTEXT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
893         rm -f stage3-chroot/usr/lib64/*.la
894
895 gettext-$(GETTEXT_VERSION).tar.gz:
896         rm -f $@ $@-t
897         wget -O $@-t https://ftp.gnu.org/gnu/gettext/gettext-$(GETTEXT_VERSION).tar.gz
898         mv $@-t $@
899
900 # Cross-compile lua.
901 stage3-chroot/usr/bin/lua: lua-$(LUA_VERSION).tar.gz
902         rm -rf lua-$(LUA_VERSION)
903         tar -zxf $^
904         cd lua-$(LUA_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make PLAT=linux INSTALL_TOP=/usr CC=riscv64-unknown-linux-gnu-gcc AR="riscv64-unknown-linux-gnu-ar rcu" RANLIB="riscv64-unknown-linux-gnu-ranlib" MYLDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 MYLIBS=-ltinfo MYCFLAGS="-fPIC -DLUA_COMPAT_5_1"
905         cd lua-$(LUA_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install INSTALL_TOP=$(ROOT)/stage3-chroot/usr INSTALL_LIB=$(ROOT)/stage3-chroot/usr/lib64
906
907 lua-$(LUA_VERSION).tar.gz:
908         rm -f $@ $@-t
909         wget -O $@-t https://www.lua.org/ftp/lua-$(LUA_VERSION).tar.gz
910         mv $@-t $@
911
912 # Cross-compile RPM / rpmbuild.
913 # We build this from a git commit, with a few hacks to the configure
914 # script.
915 stage3-chroot/usr/bin/rpm: rpm-$(RPM_SHORT_COMMIT).tar.gz db-$(BDB_VERSION).tar.gz
916         rm -rf rpm-$(RPM_SHORT_COMMIT)
917         tar -zxf rpm-$(RPM_SHORT_COMMIT).tar.gz
918         tar -zxf db-$(BDB_VERSION).tar.gz -C rpm-$(RPM_SHORT_COMMIT)
919         cd rpm-$(RPM_SHORT_COMMIT) && ln -s db-$(BDB_VERSION) db
920         cd rpm-$(RPM_SHORT_COMMIT) && \
921         patch -p1 < ../0001-RISCV-64-bit-riscv64-support.patch && \
922         patch -p1 < ../0002-build-fgetc-returns-int-not-char.patch && \
923         patch -p1 < ../0003-HACKS-TO-GET-RPM-TO-CROSS-COMPILE.patch
924         cd rpm-$(RPM_SHORT_COMMIT) && autoreconf -i
925         cd rpm-$(RPM_SHORT_COMMIT) && \
926         PATH=$(ROOT)/fixed-gcc:$$PATH \
927         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
928         ./configure \
929             --host=riscv64-unknown-linux-gnu \
930             --prefix=/usr --libdir=/usr/lib64 \
931             --disable-rpath \
932             --without-libarchive \
933             --with-lua \
934             --with-beecrypt \
935             --without-archive \
936             --without-external-db \
937             --enable-ndb \
938             --disable-plugins
939         cd rpm-$(RPM_SHORT_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
940         cd rpm-$(RPM_SHORT_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
941         rm -f stage3-chroot/usr/lib64/*.la
942
943 rpm-$(RPM_SHORT_COMMIT).tar.gz:
944         rm -f $@ $@-t
945         wget -O $@-t 'http://rpm.org/gitweb?p=rpm.git;a=snapshot;h=$(RPM_COMMIT);sf=tgz'
946         mv $@-t $@
947
948 db-$(BDB_VERSION).tar.gz:
949         rm -f $@ $@-t
950         wget -O $@-t http://download.oracle.com/berkeley-db/db-$(BDB_VERSION).tar.gz
951         mv $@-t $@
952
953 # Create an /init script.
954 stage3-chroot/init: init.sh
955         install -m 0755 $^ $@
956
957 # Create the stage3 disk image.
958 # Note `-s +...' adds spare space to the disk image.
959 stage3-disk.img: stage3-chroot
960         cd stage3-chroot && virt-make-fs . ../$@ -t ext2 -F raw -s +4G
961
962 # Upload the compressed disk image.
963 upload-stage3: stage3-disk.img.xz
964         scp $^ tick:public_html/riscv/
965 stage3-disk.img.xz: stage3-disk.img
966         rm -f $@
967         xz --best $^
968
969 # Helper which boots stage3 disk image in spike.
970 boot-stage3-in-spike: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
971         spike +disk=stage3-disk.img \
972             /usr/bin/bbl stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
973
974 # Helper which boots stage3 disk image in qemu.
975 boot-stage3-in-qemu: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
976         qemu-system-riscv -kernel /usr/bin/bbl \
977             -append ./stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
978             -drive file=stage3-disk.img,format=raw -nographic
979
980 # Stage 4
981
982 stage4:
983         echo "XXX TO DO"
984         exit 1
985
986 .NOTPARALLEL: