Add a note about how GCC build is broken, for future investigation.
[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 STAGE3_PACKAGES = gcc rpm-build
40
41 # Versions of cross-compiled packages.
42 NCURSES_VERSION    = 6.0-20160730
43 BASH_VERSION       = 4.3
44 COREUTILS_VERSION  = 8.25
45 GMP_VERSION        = 6.1.1
46 MPFR_VERSION       = 3.1.4
47 MPC_VERSION        = 1.0.3
48 BINUTILS_X_VERSION = 2.26
49 GCC_X_VERSION      = 6.1.0
50 UTIL_LINUX_VERSION = 2.28
51
52 all: stage1 stage2 stage3 stage4
53
54 # Stage 1
55
56 stage1: stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz \
57         stage1-riscv-qemu/riscv-qemu.spec \
58         stamp-riscv-qemu-installed \
59         stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz \
60         stage1-riscv-fesvr/riscv-fesvr.spec \
61         stamp-riscv-fesvr-installed \
62         stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz \
63         stage1-riscv-isa-sim/riscv-isa-sim.spec \
64         stamp-riscv-isa-sim-installed
65
66 stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz:
67         rm -f $@ $@-t
68         wget -O $@-t 'https://github.com/riscv/riscv-qemu/archive/$(RISCV_QEMU_COMMIT)/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz'
69         mv $@-t $@
70
71 stage1-riscv-qemu/riscv-qemu.spec: stage1-riscv-qemu/riscv-qemu.spec.in
72         sed -e 's/@COMMIT@/$(RISCV_QEMU_COMMIT)/g' \
73             -e 's/@SHORTCOMMIT@/$(RISCV_QEMU_SHORTCOMMIT)/g' \
74             < $^ > $@-t
75         mv $@-t $@
76
77 stamp-riscv-qemu-installed:
78         rm -f $@
79         @rpm -q riscv-qemu >/dev/null || { \
80           echo "ERROR: You must install riscv-qemu:"; \
81           echo; \
82           echo "       dnf copr enable rjones/riscv"; \
83           echo "       dnf install riscv-qemu"; \
84           echo; \
85           echo "OR: you can build it yourself from the stage1-riscv-qemu directory."; \
86           echo; \
87           exit 1; \
88         }
89         @qemu-system-riscv --version || { \
90           echo "ERROR: qemu-system-riscv is not working."; \
91           echo "Make sure you installed the riscv-qemu package."; \
92           exit 1; \
93         }
94         touch $@
95
96 stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz:
97         rm -f $@ $@-t
98         wget -O $@-t 'https://github.com/riscv/riscv-fesvr/archive/$(RISCV_FESVR_COMMIT)/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz'
99         mv $@-t $@
100
101 stage1-riscv-fesvr/riscv-fesvr.spec: stage1-riscv-fesvr/riscv-fesvr.spec.in
102         sed -e 's/@COMMIT@/$(RISCV_FESVR_COMMIT)/g' \
103             -e 's/@SHORTCOMMIT@/$(RISCV_FESVR_SHORTCOMMIT)/g' \
104             < $^ > $@-t
105         mv $@-t $@
106
107 stamp-riscv-fesvr-installed:
108         rm -f $@
109         @rpm -q riscv-fesvr >/dev/null || { \
110           echo "ERROR: You must install riscv-fesvr:"; \
111           echo; \
112           echo "       dnf copr enable rjones/riscv"; \
113           echo "       dnf install riscv-fesvr"; \
114           echo; \
115           echo "OR: you can build it yourself from the stage1-riscv-fesvr directory."; \
116           echo; \
117           exit 1; \
118         }
119         touch $@
120
121 stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz:
122         rm -f $@ $@-t
123         wget -O $@-t 'https://github.com/riscv/riscv-isa-sim/archive/$(RISCV_ISA_SIM_COMMIT)/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz'
124         mv $@-t $@
125
126 stage1-riscv-isa-sim/riscv-isa-sim.spec: stage1-riscv-isa-sim/riscv-isa-sim.spec.in
127         sed -e 's/@COMMIT@/$(RISCV_ISA_SIM_COMMIT)/g' \
128             -e 's/@SHORTCOMMIT@/$(RISCV_ISA_SIM_SHORTCOMMIT)/g' \
129             < $^ > $@-t
130         mv $@-t $@
131
132 stamp-riscv-isa-sim-installed:
133         rm -f $@
134         @rpm -q riscv-isa-sim >/dev/null || { \
135           echo "ERROR: You must install riscv-isa-sim:"; \
136           echo; \
137           echo "       dnf copr enable rjones/riscv"; \
138           echo "       dnf install riscv-isa-sim"; \
139           echo; \
140           echo "OR: you can build it yourself from the stage1-riscv-isa-sim directory."; \
141           echo; \
142           exit 1; \
143         }
144         touch $@
145
146 # Stage 2
147
148 stage2: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz \
149         stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz \
150         stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz \
151         stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz \
152         stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz \
153         stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec \
154         stamp-riscv-gnu-toolchain-installed \
155         stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz \
156         stage2-riscv-pk/riscv-pk.spec \
157         stamp-riscv-pk-installed
158
159 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz:
160         rm -f $@ $@-t
161         wget -O $@-t https://github.com/lowRISC/riscv-gnu-toolchain/archive/$(RISCV_GNU_TOOLCHAIN_COMMIT)/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz
162         mv $@-t $@
163
164 stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz:
165         rm -f $@ $@-t
166         wget -O $@-t http://mirrors.kernel.org/gnu/binutils/binutils-$(BINUTILS_VERSION).tar.gz
167         mv $@-t $@
168
169 # GCC 5 no longer compiles with GCC 6 unless we patch it.
170 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69959
171 stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz:
172         rm -f $@ $@-t
173         wget -O $@-t http://mirrors.kernel.org/gnu/gcc/gcc-$(GCC_VERSION)/gcc-$(GCC_VERSION).tar.gz
174         zcat $@-t | tar xf -
175         cd gcc-$(GCC_VERSION) && patch -p0 < ../stage2-riscv-gnu-toolchain/gcc-5-fix-compilation-with-gcc-6.patch
176         tar zcf $@-t gcc-$(GCC_VERSION)
177         rm -r gcc-$(GCC_VERSION)
178         mv $@-t $@
179
180 stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz:
181         rm -f $@ $@-t
182         wget -O $@-t http://mirrors.kernel.org/gnu/glibc/glibc-$(GLIBC_VERSION).tar.gz
183         mv $@-t $@
184
185 stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz:
186         rm -f $@ $@-t
187         wget -O $@-t ftp://sourceware.org/pub/newlib/newlib-$(NEWLIB_VERSION).tar.gz
188         mv $@-t $@
189
190 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec.in
191         sed -e 's/@COMMIT@/$(RISCV_GNU_TOOLCHAIN_COMMIT)/g' \
192             -e 's/@SHORTCOMMIT@/$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT)/g' \
193             -e 's/@BINUTILS_VERSION@/$(BINUTILS_VERSION)/g' \
194             -e 's/@GCC_VERSION@/$(GCC_VERSION)/g' \
195             -e 's/@GLIBC_VERSION@/$(GLIBC_VERSION)/g' \
196             -e 's/@NEWLIB_VERSION@/$(NEWLIB_VERSION)/g' \
197             < $^ > $@-t
198         mv $@-t $@
199
200 stamp-riscv-gnu-toolchain-installed:
201         rm -f $@
202         @rpm -q riscv-gnu-toolchain >/dev/null || { \
203           echo "ERROR: You must install riscv-gnu-toolchain:"; \
204           echo; \
205           echo "       dnf copr enable rjones/riscv"; \
206           echo "       dnf install riscv-gnu-toolchain"; \
207           echo; \
208           echo "OR: you can build it yourself from the stage2-riscv-gnu-toolchain directory."; \
209           echo; \
210           exit 1; \
211         }
212         @riscv64-unknown-elf-gcc --version || { \
213           echo "ERROR: riscv64-unknown-elf-gcc (cross compiler) is not working."; \
214           echo "Make sure you installed the riscv-gnu-toolchain package."; \
215           exit 1; \
216         }
217         touch $@
218
219 stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz:
220         rm -f $@ $@-t
221         wget -O $@-t https://github.com/lowRISC/riscv-pk/archive/$(RISCV_PK_COMMIT)/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz
222         mv $@-t $@
223
224 stage2-riscv-pk/riscv-pk.spec: stage2-riscv-pk/riscv-pk.spec.in
225         sed -e 's/@COMMIT@/$(RISCV_PK_COMMIT)/g' \
226             -e 's/@SHORTCOMMIT@/$(RISCV_PK_SHORTCOMMIT)/g' \
227             < $^ > $@-t
228         mv $@-t $@
229
230 stamp-riscv-pk-installed:
231         rm -f $@
232         @rpm -q riscv-pk >/dev/null || { \
233           echo "ERROR: You must install riscv-pk:"; \
234           echo; \
235           echo "       dnf copr enable rjones/riscv"; \
236           echo "       dnf install riscv-pk"; \
237           echo; \
238           echo "OR: you can build it yourself from the stage2-riscv-pk directory."; \
239           echo; \
240           exit 1; \
241         }
242         touch $@
243
244 # Stage 3
245
246 stage3: stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
247         stage3-chroot-original/etc/fedora-release \
248         stage3-chroot/etc/fedora-release \
249         stage3-chroot/lib64/libc.so.6 \
250         stage3-chroot/usr/bin/tic \
251         stage3-chroot/bin/bash \
252         stage3-chroot/bin/ls \
253         stage3-chroot/usr/lib64/libgmp.so.10 \
254         stage3-chroot/usr/lib64/libmpfr.so.4 \
255         stage3-chroot/usr/lib64/libmpc.so.3 \
256         stage3-chroot/usr/bin/as \
257         stage3-chroot/usr/bin/gcc \
258         stage3-chroot/usr/bin/mount \
259         stage3-chroot/init \
260         stage3-disk.img
261
262 stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux:
263         rm -rf stage3-kernel/linux-$(KERNEL_VERSION)
264         cp -a $(LOCAL_LINUX_GIT_COPY) stage3-kernel/linux-$(KERNEL_VERSION) || { \
265           mkdir stage3-kernel/linux-$(KERNEL_VERSION) && \
266           cd stage3-kernel/linux-$(KERNEL_VERSION) && \
267           git init; \
268         }
269         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
270         git remote add riscv https://github.com/riscv/riscv-linux && \
271         git fetch riscv && \
272         git checkout -f linux-4.1.y-riscv && \
273         make mrproper && \
274         make ARCH=riscv defconfig
275         echo CONFIG_CMDLINE=\"root=/dev/htifblk0 init=/init\" >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
276         echo CONFIG_CROSS_COMPILE=riscv64-unknown-elf- >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
277         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
278         make ARCH=riscv olddefconfig
279         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
280         make ARCH=riscv vmlinux
281         ls -l $@
282
283 # Build an original (x86-64) chroot using supermin.  We then aim to
284 # rebuild (using cross-compiled versions) every ELF binary in this
285 # chroot.
286 stage3-chroot-original/etc/fedora-release:
287         rm -rf stage3-chroot-original tmp-supermin.d
288         supermin --prepare $(STAGE3_PACKAGES) -o tmp-supermin.d
289         supermin --build -f chroot tmp-supermin.d -o stage3-chroot-original
290         rm -r tmp-supermin.d
291         @echo -n "Total files in chroot: "
292         @find stage3-chroot-original -type f | wc -l
293         @echo -n "ELF files to be rebuilt: "
294         @find stage3-chroot-original -type f | xargs file -N | grep -E '\bELF.*LSB\b' | wc -l
295
296 # Copy the original chroot to the final chroot, remove all the ELF
297 # files.
298 stage3-chroot/etc/fedora-release: stage3-chroot-original/etc/fedora-release
299         rm -rf stage3-chroot
300         cp -a stage3-chroot-original stage3-chroot
301         find stage3-chroot -type d | xargs chmod u+w
302         find stage3-chroot -type f | xargs chmod u+w
303         find stage3-chroot -type f | xargs file -N | grep -E '\bELF.*LSB\b' | awk -F: '{print $$1}' | xargs rm -f
304         rm -f stage3-chroot/lib64/libc.so.6
305
306 # Copy in compiled glibc from the riscv-gnu-toolchain sysroot.  Only
307 # copy files and symlinks, leave the target directory structure
308 # intact.
309 stage3-chroot/lib64/libc.so.6:
310         mkdir -p stage3-chroot/usr/lib/audit
311         mkdir -p stage3-chroot/usr/lib/gconv
312         for f in `cd /usr/sysroot && find -type f -o -type l`; do \
313             cp -d /usr/sysroot/$$f stage3-chroot/$$f; \
314         done
315         cd stage3-chroot/lib64 && for f in ../lib/*; do ln -sf $$f; done
316
317 # Cross-compile ncurses.
318 stage3-chroot/usr/bin/tic: ncurses-$(NCURSES_VERSION).tgz
319         tar zxf $^
320         cd ncurses-$(NCURSES_VERSION) && \
321         PATH=$(ROOT)/fixed-gcc:$$PATH \
322         ./configure --host=riscv64-unknown-linux-gnu \
323             --prefix=/usr --libdir=/usr/lib64 \
324             --with-shared
325         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
326         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
327         cd $(ROOT)/stage3-chroot/usr/lib64 && ln -sf libtinfo.so.6 libtinfo.so
328
329 ncurses-$(NCURSES_VERSION).tgz:
330         rm -f $@ $@-t
331         wget -O $@-t ftp://invisible-island.net/ncurses/current/ncurses-$(NCURSES_VERSION).tgz
332         mv $@-t $@
333
334 # Cross-compile bash.
335 stage3-chroot/bin/bash: bash-$(BASH_VERSION).tar.gz
336         tar zxf $^
337         cd bash-$(BASH_VERSION) && \
338         PATH=$(ROOT)/fixed-gcc:$$PATH \
339         ./configure --host=riscv64-unknown-linux-gnu \
340             --prefix=/usr --libdir=/usr/lib64
341         cd bash-$(BASH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
342         cd bash-$(BASH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
343
344 bash-$(BASH_VERSION).tar.gz:
345         rm -f $@ $@-t
346         wget -O $@-t ftp://ftp.gnu.org/gnu/bash/bash-$(BASH_VERSION).tar.gz
347         mv $@-t $@
348
349 # Cross-compile coreutils.  Bleah, coreutils cross-compilation is
350 # known-broken and upstream don't care, hence the 'touch' command.
351
352 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
353
354 stage3-chroot/bin/ls: coreutils-$(COREUTILS_VERSION).tar.xz
355         rm -rf coreutils-$(COREUTILS_VERSION)
356         tar Jxf $^
357         cd coreutils-$(COREUTILS_VERSION) && \
358         PATH=$(ROOT)/fixed-gcc:$$PATH \
359         ./configure --host=riscv64-unknown-linux-gnu \
360             --prefix=/usr --libdir=/usr/lib64
361         -cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
362         cd coreutils-$(COREUTILS_VERSION)/man && \
363         for f in $(COREUTILS_PROGRAMS); do touch $$f.1; done
364         cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
365         cd coreutils-$(COREUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
366
367 coreutils-$(COREUTILS_VERSION).tar.xz:
368         rm -f $@ $@-t
369         wget -O $@-t ftp://ftp.gnu.org/gnu/coreutils/coreutils-$(COREUTILS_VERSION).tar.xz
370         mv $@-t $@
371
372 # Cross-compile binutils.
373 stage3-chroot/usr/bin/as: binutils-$(BINUTILS_X_VERSION).tar.gz
374         rm -rf binutils-$(BINUTILS_X_VERSION)
375         zcat $^ | tar xf -
376         mkdir riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build
377         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && \
378         PATH=$(ROOT)/fixed-gcc:$$PATH \
379         ../configure \
380             --host=riscv64-unknown-linux-gnu \
381             --prefix=/usr --libdir=/usr/lib64
382         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make
383         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && make DESTDIR=$(ROOT)/stage3-chroot install
384
385 binutils-$(BINUTILS_X_VERSION).tar.gz:
386         rm -f $@ $@-t
387         wget -O $@-t https://github.com/riscv/riscv-binutils-gdb/archive/riscv-binutils-$(BINUTILS_X_VERSION).tar.gz
388         mv $@-t $@
389
390 # Cross-compile GMP, MPFR and MPC (deps of GCC).
391 stage3-chroot/usr/lib64/libgmp.so.10: gmp-$(GMP_VERSION).tar.lz
392         rm -rf gmp-$(GMP_VERSION)
393         tar --lzip -xf gmp-$(GMP_VERSION).tar.lz
394         cd gmp-$(GMP_VERSION) && \
395         PATH=$(ROOT)/fixed-gcc:$$PATH \
396         ./configure --host=riscv64-unknown-linux-gnu \
397             --prefix=/usr --libdir=/usr/lib64
398         cd gmp-$(GMP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
399         cd gmp-$(GMP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
400         cd stage3-chroot/usr/lib && ln -s ../lib64/libgmp.so
401
402 gmp-$(GMP_VERSION).tar.lz:
403         rm -f $@ $@-t
404         wget -O $@-t https://gmplib.org/download/gmp/gmp-$(GMP_VERSION).tar.lz
405         mv $@-t $@
406
407 stage3-chroot/usr/lib64/libmpfr.so.4: mpfr-$(MPFR_VERSION).tar.gz
408         rm -rf mpfr-$(MPFR_VERSION)
409         tar -zxf mpfr-$(MPFR_VERSION).tar.gz
410         cd mpfr-$(MPFR_VERSION) && \
411         PATH=$(ROOT)/fixed-gcc:$$PATH \
412         ./configure --host=riscv64-unknown-linux-gnu \
413             --prefix=/usr --libdir=/usr/lib64 \
414             --with-gmp=$(ROOT)/stage3-chroot/usr
415         cd mpfr-$(MPFR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
416         cd mpfr-$(MPFR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
417         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpfr.so
418
419 mpfr-$(MPFR_VERSION).tar.gz:
420         rm -f $@ $@-t
421         wget -O $@-t http://www.mpfr.org/mpfr-current/mpfr-$(MPFR_VERSION).tar.gz
422         mv $@-t $@
423
424 stage3-chroot/usr/lib64/libmpc.so.3: mpc-$(MPC_VERSION).tar.gz
425         rm -rf mpc-$(MPC_VERSION)
426         tar -zxf mpc-$(MPC_VERSION).tar.gz
427         cd mpc-$(MPC_VERSION) && \
428         PATH=$(ROOT)/fixed-gcc:$$PATH \
429         ./configure --host=riscv64-unknown-linux-gnu \
430             --prefix=/usr --libdir=/usr/lib64 \
431             --with-gmp=$(ROOT)/stage3-chroot/usr \
432             --with-mpfr=$(ROOT)/stage3-chroot/usr
433         cd mpc-$(MPC_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
434         cd mpc-$(MPC_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
435         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpc.so
436
437 mpc-$(MPC_VERSION).tar.gz:
438         rm -f $@ $@-t
439         wget -O $@-t ftp://ftp.gnu.org/gnu/mpc/mpc-$(MPC_VERSION).tar.gz
440         mv $@-t $@
441
442 # Cross-compile GCC.
443 stage3-chroot/usr/bin/gcc: gcc-$(GCC_X_VERSION).tar.gz
444         rm -rf riscv-gcc-riscv-gcc-$(GCC_X_VERSION)
445         zcat $^ | tar xf -
446         mkdir riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build
447         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && \
448         PATH=$(ROOT)/fixed-gcc:$$PATH \
449         ../configure \
450             --host=riscv64-unknown-linux-gnu \
451             --prefix=/usr --libdir=/usr/lib64 \
452             --enable-shared \
453             --enable-tls \
454             --enable-languages=c,c++ \
455             --disable-libmudflap \
456             --disable-libssp \
457             --disable-libquadmath \
458             --disable-nls \
459             --disable-multilib
460 # XXX use make all & make install here.  However building libgcc
461 # fails, see 'broken-gcc.log'.
462         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make all-gcc
463         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && make install-gcc DESTDIR=$(ROOT)/stage3-chroot
464
465 gcc-$(GCC_X_VERSION).tar.gz:
466         rm -f $@ $@-t
467         wget -O $@-t https://github.com/riscv/riscv-gcc/archive/riscv-gcc-$(GCC_X_VERSION).tar.gz
468         mv $@-t $@
469
470 # Cross-compile util-linux.
471 # XXX Be nice to fix ncurses/tinfo support which in theory should work.
472 stage3-chroot/usr/bin/mount: util-linux-$(UTIL_LINUX_VERSION).tar.xz
473         rm -rf util-linux-$(UTIL_LINUX_VERSION)
474         tar -Jxf $^
475         cd util-linux-$(UTIL_LINUX_VERSION) && \
476         PATH=$(ROOT)/fixed-gcc:$$PATH \
477         ./configure \
478             --host=riscv64-unknown-linux-gnu \
479             --prefix=/usr --libdir=/usr/lib64 \
480             --without-python \
481             --without-tinfo \
482             --without-ncurses \
483             --without-systemd \
484             --disable-makeinstall-chown
485         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
486 #       libtool fucks something up here, ignore the failure and continue.
487         -cd util-linux-$(UTIL_LINUX_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot -j1 -k V=1
488
489 util-linux-$(UTIL_LINUX_VERSION).tar.xz:
490         rm -f $@ $@-t
491         wget -O $@-t ftp://ftp.kernel.org/pub/linux/utils/util-linux/v$(UTIL_LINUX_VERSION)/util-linux-$(UTIL_LINUX_VERSION).tar.xz
492         mv $@-t $@
493
494 # Create an /init script.
495 stage3-chroot/init: init.sh
496         install -m 0755 $^ $@
497
498 # Create the stage3 disk image.
499 # Note `-s +...' adds spare space to the disk image.
500 stage3-disk.img: stage3-chroot
501         cd stage3-chroot && virt-make-fs . ../$@ -t ext2 -F raw -s +4G
502
503 # Upload the compressed disk image.
504 upload-stage3: stage3-disk.img.xz
505         scp $^ tick:public_html/riscv/
506 stage3-disk.img.xz: stage3-disk.img
507         rm -f $@
508         xz --best $^
509
510 # Helper which boots stage3 disk image in spike.
511 boot-stage3-in-spike: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
512         spike +disk=stage3-disk.img \
513             /usr/bin/bbl stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
514
515 # Helper which boots stage3 disk image in qemu.
516 boot-stage3-in-qemu: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
517         qemu-system-riscv -kernel /usr/bin/bbl \
518             -append ./stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
519             -drive file=stage3-disk.img,format=raw -nographic
520
521 # Stage 4
522
523 stage4:
524         echo "XXX TO DO"
525         exit 1
526
527 .NOTPARALLEL: