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