3f9c6dc44e72fb4c4e2a3d99dd7485734207f041
[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
514 gcc-$(GCC_X_VERSION).tar.gz:
515         rm -f $@ $@-t
516         wget -O $@-t https://github.com/riscv/riscv-gcc/archive/riscv-gcc-$(GCC_X_VERSION).tar.gz
517         mv $@-t $@
518
519 # Cross-compile util-linux.
520 stage3-chroot/usr/bin/mount: util-linux-$(UTIL_LINUX_VERSION).tar.xz
521         rm -rf util-linux-$(UTIL_LINUX_VERSION)
522         tar -Jxf $^
523         cd util-linux-$(UTIL_LINUX_VERSION) && \
524         PATH=$(ROOT)/fixed-gcc:$$PATH \
525         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
526         ./configure \
527             --host=riscv64-unknown-linux-gnu \
528             --prefix=/usr --libdir=/usr/lib64 \
529             --without-python \
530             --without-systemd \
531             --disable-makeinstall-chown \
532             --enable-static-programs=mount
533         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
534         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
535         rm -f stage3-chroot/usr/lib64/*.la
536
537 util-linux-$(UTIL_LINUX_VERSION).tar.xz:
538         rm -f $@ $@-t
539         wget -O $@-t ftp://ftp.kernel.org/pub/linux/utils/util-linux/v$(UTIL_LINUX_VERSION)/util-linux-$(UTIL_LINUX_VERSION).tar.xz
540         mv $@-t $@
541
542 # Cross-compile GNU tar.
543 stage3-chroot/usr/bin/tar: tar-$(TAR_VERSION).tar.xz
544         rm -rf tar-$(TAR_VERSION)
545         tar -Jxf $^
546         cd tar-$(TAR_VERSION) && \
547         PATH=$(ROOT)/fixed-gcc:$$PATH \
548         ./configure \
549             --host=riscv64-unknown-linux-gnu \
550             --prefix=/usr --libdir=/usr/lib64
551         cd tar-$(TAR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
552         cd tar-$(TAR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
553
554 tar-$(TAR_VERSION).tar.xz:
555         rm -f $@ $@-t
556         wget -O $@-t https://ftp.gnu.org/gnu/tar/tar-$(TAR_VERSION).tar.xz
557         mv $@-t $@
558
559 # Cross-compile GNU gzip.
560 stage3-chroot/usr/bin/gzip: gzip-$(GZIP_VERSION).tar.gz
561         rm -rf gzip-$(GZIP_VERSION)
562         tar -zxf $^
563         cd gzip-$(GZIP_VERSION) && \
564         PATH=$(ROOT)/fixed-gcc:$$PATH \
565         ./configure \
566             --host=riscv64-unknown-linux-gnu \
567             --prefix=/usr --libdir=/usr/lib64
568         cd gzip-$(GZIP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
569         cd gzip-$(GZIP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
570
571 gzip-$(GZIP_VERSION).tar.gz:
572         rm -f $@ $@-t
573         wget -O $@-t https://ftp.gnu.org/gnu/gzip/gzip-$(GZIP_VERSION).tar.gz
574         mv $@-t $@
575
576 # Cross-compile zlib.
577 stage3-chroot/usr/lib64/libz.so: zlib-$(ZLIB_VERSION).tar.gz
578         rm -rf zlib-$(ZLIB_VERSION)
579         tar -zxf $^
580         cd zlib-$(ZLIB_VERSION) && \
581         PATH=$(ROOT)/fixed-gcc:$$PATH \
582         CC=riscv64-unknown-linux-gnu-gcc \
583         CFLAGS="-I$(ROOT)/stage3-chroot/usr/include -L$(ROOT)/stage3-chroot/usr/lib" \
584         ./configure \
585             --prefix=/usr --libdir=/usr/lib64
586         cd zlib-$(ZLIB_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make shared
587         cd zlib-$(ZLIB_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
588
589 zlib-$(ZLIB_VERSION).tar.gz:
590         rm -f $@ $@-t
591         wget -O $@-t http://zlib.net/zlib-$(ZLIB_VERSION).tar.gz
592         mv $@-t $@
593
594 # Cross-compile file/libmagic.
595 stage3-chroot/usr/bin/file: file-$(FILE_VERSION).tar.gz
596         rm -rf file-$(FILE_VERSION)
597         tar -zxf $^
598         cd file-$(FILE_VERSION) && \
599         PATH=$(ROOT)/fixed-gcc:$$PATH \
600         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
601         ./configure \
602             --host=riscv64-unknown-linux-gnu \
603             --prefix=/usr --libdir=/usr/lib64 \
604             --disable-static --enable-shared
605         cd file-$(FILE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
606         cd file-$(FILE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
607         rm -f stage3-chroot/usr/lib64/*.la
608
609 file-$(FILE_VERSION).tar.gz:
610         rm -f $@ $@-t
611         wget -O $@-t ftp://ftp.astron.com/pub/file/file-$(FILE_VERSION).tar.gz
612         mv $@-t $@
613
614 # Cross-compile popt.
615 stage3-chroot/usr/lib64/libpopt.so: popt-$(POPT_VERSION).tar.gz
616         rm -rf popt-$(POPT_VERSION)
617         tar -zxf $^
618         cd popt-$(POPT_VERSION) && \
619         PATH=$(ROOT)/fixed-gcc:$$PATH \
620         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
621         ./configure \
622             --host=riscv64-unknown-linux-gnu \
623             --prefix=/usr --libdir=/usr/lib64 \
624             --disable-static --enable-shared
625         cd popt-$(POPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
626         cd popt-$(POPT_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
627         rm -f stage3-chroot/usr/lib64/*.la
628
629 popt-$(POPT_VERSION).tar.gz:
630         rm -f $@ $@-t
631         wget -O $@-t http://rpm5.org/files/popt/popt-$(POPT_VERSION).tar.gz
632         mv $@-t $@
633
634 # Cross-compile beecrypt.
635 stage3-chroot/usr/lib64/libbeecrypt.so: beecrypt-$(BEECRYPT_VERSION).tar.gz
636         rm -rf beecrypt-$(BEECRYPT_VERSION)
637         tar -zxf $^
638         cd beecrypt-$(BEECRYPT_VERSION) && patch -p0 < ../beecrypt-disable-cplusplus.patch
639         cd beecrypt-$(BEECRYPT_VERSION) && autoreconf -i
640         cd beecrypt-$(BEECRYPT_VERSION) && \
641         PATH=$(ROOT)/fixed-gcc:$$PATH \
642         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
643         ./configure \
644             --host=riscv64-unknown-linux-gnu \
645             --prefix=/usr --libdir=/usr/lib64 \
646             --without-cplusplus \
647             --without-java \
648             --disable-openmp \
649             --disable-static \
650             --enable-shared
651         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
652         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot V=1
653         chrpath -d stage3-chroot/usr/lib64/libbeecrypt.so.*
654         rm -f stage3-chroot/usr/lib64/*.la
655
656 beecrypt-$(BEECRYPT_VERSION).tar.gz:
657         rm -f $@ $@-t
658         wget -O $@-t http://downloads.sourceforge.net/sourceforge/beecrypt/beecrypt-$(BEECRYPT_VERSION).tar.gz
659         mv $@-t $@
660
661 # Cross-compile GNU nano (editor).
662 stage3-chroot/usr/bin/nano: nano-$(NANO_VERSION).tar.gz
663         rm -rf nano-$(NANO_VERSION)
664         tar -zxf $^
665         cd nano-$(NANO_VERSION) && \
666         PATH=$(ROOT)/fixed-gcc:$$PATH \
667         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
668         ./configure \
669             --host=riscv64-unknown-linux-gnu \
670             --prefix=/usr --libdir=/usr/lib64
671         cd nano-$(NANO_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
672         cd nano-$(NANO_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
673
674 nano-$(NANO_VERSION).tar.gz:
675         rm -f $@ $@-t
676         wget -O $@-t https://www.nano-editor.org/dist/v2.6/nano-$(NANO_VERSION).tar.gz
677         mv $@-t $@
678
679 # Cross-compile GNU grep.
680 stage3-chroot/usr/bin/grep: grep-$(GREP_VERSION).tar.xz
681         rm -rf grep-$(GREP_VERSION)
682         tar -Jxf $^
683         cd grep-$(GREP_VERSION) && \
684         PATH=$(ROOT)/fixed-gcc:$$PATH \
685         ./configure \
686             --host=riscv64-unknown-linux-gnu \
687             --prefix=/usr --libdir=/usr/lib64
688         cd grep-$(GREP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
689         cd grep-$(GREP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
690
691 grep-$(GREP_VERSION).tar.xz:
692         rm -f $@ $@-t
693         wget -O $@-t https://ftp.gnu.org/gnu/grep/grep-$(GREP_VERSION).tar.xz
694         mv $@-t $@
695
696 # Cross-compile less.
697 stage3-chroot/usr/bin/less: less-$(LESS_VERSION).tar.gz
698         rm -rf less-$(LESS_VERSION)
699         tar -zxf $^
700         cd less-$(LESS_VERSION) && \
701         PATH=$(ROOT)/fixed-gcc:$$PATH \
702         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
703         ./configure \
704             --host=riscv64-unknown-linux-gnu \
705             --prefix=/usr --libdir=/usr/lib64
706         cd less-$(LESS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
707         cd less-$(LESS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
708
709 less-$(LESS_VERSION).tar.gz:
710         rm -f $@ $@-t
711         wget -O $@-t http://www.greenwoodsoftware.com/less/less-$(LESS_VERSION).tar.gz
712         mv $@-t $@
713
714 # Cross-compile strace.
715 # XXX This does not work.
716 stage3-chroot/usr/bin/strace: strace-$(STRACE_SHORT_COMMIT).tar.gz
717         rm -rf strace-$(STRACE_SHORT_COMMIT)
718         tar -zxf $^
719         cd riscv-strace-$(STRACE_COMMIT) && patch -p1 < ../0001-Update-riscv_regs-for-ptrace.h-from-Linux-4.1.x.patch
720         cd riscv-strace-$(STRACE_COMMIT) && \
721         PATH=$(ROOT)/fixed-gcc:$$PATH \
722         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
723         ./configure \
724             --host=riscv64-unknown-linux-gnu \
725             --prefix=/usr --libdir=/usr/lib64
726         cd riscv-strace-$(STRACE_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make
727         cd riscv-strace-$(STRACE_COMMIT) && make install DESTDIR=$(ROOT)/stage3-chroot
728
729 strace-$(STRACE_SHORT_COMMIT).tar.gz:
730         rm -f $@ $@-t
731         wget -O $@-t 'https://github.com/riscv/riscv-strace/archive/$(STRACE_COMMIT)/riscv-strace-$(STRACE_SHORTCOMMIT).tar.gz'
732         mv $@-t $@
733
734 # Cross-compile bzip2.
735 stage3-chroot/usr/bin/bzip2: bzip2-$(BZIP2_VERSION).tar.gz
736         rm -rf bzip2-$(BZIP2_VERSION)
737         tar -zxf $^
738         cd bzip2-$(BZIP2_VERSION) && \
739         PATH=$(ROOT)/fixed-gcc:$$PATH \
740         make libbz2.a bzip2 bzip2recover \
741         PREFIX=/usr \
742         CC=riscv64-unknown-linux-gnu-gcc \
743         AR=riscv64-unknown-linux-gnu-ar \
744         RANLIB=riscv64-unknown-linux-gnu-ranlib \
745         CFLAGS="-Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64 -fPIC"
746         cd bzip2-$(BZIP2_VERSION) && \
747         make install PREFIX=$(ROOT)/stage3-chroot/usr
748
749 bzip2-$(BZIP2_VERSION).tar.gz:
750         rm -f $@ $@-t
751         wget -O $@-t http://www.bzip.org/1.0.6/bzip2-$(BZIP2_VERSION).tar.gz
752         mv $@-t $@
753
754 # Cross-compile GNU make.
755 stage3-chroot/usr/bin/make: make-$(MAKE_VERSION).tar.gz
756         rm -rf make-$(MAKE_VERSION)
757         tar -zxf $^
758         cd make-$(MAKE_VERSION) && \
759         PATH=$(ROOT)/fixed-gcc:$$PATH \
760         ./configure \
761             --host=riscv64-unknown-linux-gnu \
762             --prefix=/usr --libdir=/usr/lib64
763         cd make-$(MAKE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
764         cd make-$(MAKE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
765
766 make-$(MAKE_VERSION).tar.gz:
767         rm -f $@ $@-t
768         wget -O $@-t https://ftp.gnu.org/gnu/make/make-$(MAKE_VERSION).tar.gz
769         mv $@-t $@
770
771 # Cross-compile GNU diffutils.
772 stage3-chroot/usr/bin/diff: diffutils-$(DIFFUTILS_VERSION).tar.xz
773         rm -rf diffutils-$(DIFFUTILS_VERSION)
774         tar -Jxf $^
775         cd diffutils-$(DIFFUTILS_VERSION) && \
776         PATH=$(ROOT)/fixed-gcc:$$PATH \
777         ./configure \
778             --host=riscv64-unknown-linux-gnu \
779             --prefix=/usr --libdir=/usr/lib64
780         cd diffutils-$(DIFFUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
781         cd diffutils-$(DIFFUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
782
783 diffutils-$(DIFFUTILS_VERSION).tar.xz:
784         rm -f $@ $@-t
785         wget -O $@-t https://ftp.gnu.org/gnu/diffutils/diffutils-$(DIFFUTILS_VERSION).tar.xz
786         mv $@-t $@
787
788 # Cross-compile GNU findutils.
789 stage3-chroot/usr/bin/find: findutils-$(FINDUTILS_VERSION).tar.gz
790         rm -rf findutils-$(FINDUTILS_VERSION)
791         tar -zxf $^
792         cd findutils-$(FINDUTILS_VERSION) && \
793         PATH=$(ROOT)/fixed-gcc:$$PATH \
794         ./configure \
795             --host=riscv64-unknown-linux-gnu \
796             --prefix=/usr --libdir=/usr/lib64
797         cd findutils-$(FINDUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
798         cd findutils-$(FINDUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
799
800 findutils-$(FINDUTILS_VERSION).tar.gz:
801         rm -f $@ $@-t
802         wget -O $@-t https://ftp.gnu.org/gnu/findutils/findutils-$(FINDUTILS_VERSION).tar.gz
803         mv $@-t $@
804
805 # Cross-compile GNU sed.
806 stage3-chroot/usr/bin/sed: sed-$(SED_VERSION).tar.gz
807         rm -rf sed-$(SED_VERSION)
808         tar -zxf $^
809         cd sed-$(SED_VERSION) && \
810         PATH=$(ROOT)/fixed-gcc:$$PATH \
811         ./configure \
812             --host=riscv64-unknown-linux-gnu \
813             --prefix=/usr --libdir=/usr/lib64
814         cd sed-$(SED_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
815         cd sed-$(SED_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
816
817 sed-$(SED_VERSION).tar.gz:
818         rm -f $@ $@-t
819         wget -O $@-t https://ftp.gnu.org/gnu/sed/sed-$(SED_VERSION).tar.gz
820         mv $@-t $@
821
822 # Cross-compile patch.
823 stage3-chroot/usr/bin/patch: patch-$(PATCH_VERSION).tar.gz
824         rm -rf patch-$(PATCH_VERSION)
825         tar -zxf $^
826         cd patch-$(PATCH_VERSION) && \
827         PATH=$(ROOT)/fixed-gcc:$$PATH \
828         ./configure \
829             --host=riscv64-unknown-linux-gnu \
830             --prefix=/usr --libdir=/usr/lib64
831         cd patch-$(PATCH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
832         cd patch-$(PATCH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
833
834 patch-$(PATCH_VERSION).tar.gz:
835         rm -f $@ $@-t
836         wget -O $@-t https://ftp.gnu.org/gnu/patch/patch-$(PATCH_VERSION).tar.gz
837         mv $@-t $@
838
839 # Cross-compile hostname.
840 stage3-chroot/usr/bin/hostname: hostname-$(HOSTNAME_VERSION).tar.gz
841         rm -rf hostname-$(HOSTNAME_VERSION)
842         tar -zxf $^
843         cd hostname && patch -p1 < ../hostname-rh.patch
844         cd hostname && \
845         PATH=$(ROOT)/fixed-gcc:$$PATH \
846         make \
847         CC=riscv64-unknown-linux-gnu-gcc \
848         CFLAGS="-O2 -g"
849         cd hostname && make install BASEDIR=$(ROOT)/stage3-chroot
850
851 hostname-$(HOSTNAME_VERSION).tar.gz:
852         rm -f $@ $@-t
853         wget -O $@-t http://ftp.de.debian.org/debian/pool/main/h/hostname/hostname_$(HOSTNAME_VERSION).tar.gz
854         mv $@-t $@
855
856 # Cross-compile GNU gettext.
857 stage3-chroot/usr/bin/gettext: gettext-$(GETTEXT_VERSION).tar.gz
858         rm -rf gettext-$(GETTEXT_VERSION)
859         tar -zxf $^
860         cd gettext-$(GETTEXT_VERSION) && \
861         PATH=$(ROOT)/fixed-gcc:$$PATH \
862         ./configure \
863             --host=riscv64-unknown-linux-gnu \
864             --prefix=/usr --libdir=/usr/lib64
865         cd gettext-$(GETTEXT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
866         cd gettext-$(GETTEXT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
867         rm -f stage3-chroot/usr/lib64/*.la
868
869 gettext-$(GETTEXT_VERSION).tar.gz:
870         rm -f $@ $@-t
871         wget -O $@-t https://ftp.gnu.org/gnu/gettext/gettext-$(GETTEXT_VERSION).tar.gz
872         mv $@-t $@
873
874 # Cross-compile RPM / rpmbuild.
875 # We build this from a git commit, with a few hacks to the configure
876 # script.
877 stage3-chroot/usr/bin/rpm: rpm-$(RPM_SHORT_COMMIT).tar.gz db-$(BDB_VERSION).tar.gz
878         rm -rf rpm-$(RPM_SHORT_COMMIT)
879         tar -zxf rpm-$(RPM_SHORT_COMMIT).tar.gz
880         tar -zxf db-$(BDB_VERSION).tar.gz -C rpm-$(RPM_SHORT_COMMIT)
881         cd rpm-$(RPM_SHORT_COMMIT) && ln -s db-$(BDB_VERSION) db
882         cd rpm-$(RPM_SHORT_COMMIT) && \
883         patch -p1 < ../0001-RISCV-64-bit-riscv64-support.patch && \
884         patch -p1 < ../0002-build-fgetc-returns-int-not-char.patch && \
885         patch -p1 < ../0003-HACKS-TO-GET-RPM-TO-CROSS-COMPILE.patch
886         cd rpm-$(RPM_SHORT_COMMIT) && autoreconf -i
887         cd rpm-$(RPM_SHORT_COMMIT) && \
888         PATH=$(ROOT)/fixed-gcc:$$PATH \
889         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
890         ./configure \
891             --host=riscv64-unknown-linux-gnu \
892             --prefix=/usr --libdir=/usr/lib64 \
893             --disable-rpath \
894             --without-libarchive \
895             --without-lua \
896             --with-beecrypt \
897             --without-archive \
898             --without-external-db \
899             --enable-ndb \
900             --disable-plugins
901         cd rpm-$(RPM_SHORT_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
902         cd rpm-$(RPM_SHORT_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
903         rm -f stage3-chroot/usr/lib64/*.la
904
905 rpm-$(RPM_SHORT_COMMIT).tar.gz:
906         rm -f $@ $@-t
907         wget -O $@-t 'http://rpm.org/gitweb?p=rpm.git;a=snapshot;h=$(RPM_COMMIT);sf=tgz'
908         mv $@-t $@
909
910 db-$(BDB_VERSION).tar.gz:
911         rm -f $@ $@-t
912         wget -O $@-t http://download.oracle.com/berkeley-db/db-$(BDB_VERSION).tar.gz
913         mv $@-t $@
914
915 # Create an /init script.
916 stage3-chroot/init: init.sh
917         install -m 0755 $^ $@
918
919 # Create the stage3 disk image.
920 # Note `-s +...' adds spare space to the disk image.
921 stage3-disk.img: stage3-chroot
922         cd stage3-chroot && virt-make-fs . ../$@ -t ext2 -F raw -s +4G
923
924 # Upload the compressed disk image.
925 upload-stage3: stage3-disk.img.xz
926         scp $^ tick:public_html/riscv/
927 stage3-disk.img.xz: stage3-disk.img
928         rm -f $@
929         xz --best $^
930
931 # Helper which boots stage3 disk image in spike.
932 boot-stage3-in-spike: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
933         spike +disk=stage3-disk.img \
934             /usr/bin/bbl stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
935
936 # Helper which boots stage3 disk image in qemu.
937 boot-stage3-in-qemu: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
938         qemu-system-riscv -kernel /usr/bin/bbl \
939             -append ./stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
940             -drive file=stage3-disk.img,format=raw -nographic
941
942 # Stage 4
943
944 stage4:
945         echo "XXX TO DO"
946         exit 1
947
948 .NOTPARALLEL: