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