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