Cross-compile ncurses into stage3 chroot.
[fedora-riscv.git] / Makefile
1 # Refer to the README file to understand how Fedora on RISC-V is
2 # bootstrapped.
3
4 # Absolute path to the current directory.
5 ROOT := $(shell pwd)
6
7 # Note these are chosen very specifically to ensure the different
8 # versions work together.  Don't blindly update to the latest
9 # versions.  See also:
10 # https://github.com/riscv/riscv-pk/issues/18#issuecomment-206115996
11 RISCV_QEMU_COMMIT               = 94f5eb73091fb4fe272db3e943f173ecc0f78ffd
12 RISCV_QEMU_SHORTCOMMIT          = 94f5eb73
13 RISCV_FESVR_COMMIT              = 0f34d7ad311f78455a674224225f5b3056efba1d
14 RISCV_FESVR_SHORTCOMMIT         = 0f34d7ad
15 RISCV_ISA_SIM_COMMIT            = 3bfc00ef2a1b1f0b0472a39a866261b00f67027e
16 RISCV_ISA_SIM_SHORTCOMMIT       = 3bfc00ef
17 RISCV_GNU_TOOLCHAIN_COMMIT      = 728afcddcb0526a0f6560c4032da82805f054d58
18 RISCV_GNU_TOOLCHAIN_SHORTCOMMIT = 728afcdd
19 RISCV_PK_COMMIT                 = 85ae17aa149b9ea114bdd70cc30ea7e73813fb48
20 RISCV_PK_SHORTCOMMIT            = 85ae17aa
21
22 # For the correct versions, see
23 # riscv-gnu-toolchain/Makefile.in *_version variables
24 BINUTILS_VERSION = 2.25.1
25 GLIBC_VERSION    = 2.22
26 GCC_VERSION      = 5.3.0
27 NEWLIB_VERSION   = 2.2.0
28
29 # See linux-4.1.y-riscv branch of
30 # https://github.com/riscv/riscv-linux
31 KERNEL_VERSION   = 4.1.26
32
33 # A local copy of Linux git repo so you don't have to keep downloading
34 # git commits (optional).
35 LOCAL_LINUX_GIT_COPY = $(HOME)/d/linux
36
37 # The root packages (plus their dependencies) that we want to
38 # cross-compile into the stage 3 chroot.
39 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 GCC_X_VERSION     = 6.1.0
49
50 all: stage1 stage2 stage3 stage4
51
52 # Stage 1
53
54 stage1: stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz \
55         stage1-riscv-qemu/riscv-qemu.spec \
56         stamp-riscv-qemu-installed \
57         stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz \
58         stage1-riscv-fesvr/riscv-fesvr.spec \
59         stamp-riscv-fesvr-installed \
60         stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz \
61         stage1-riscv-isa-sim/riscv-isa-sim.spec \
62         stamp-riscv-isa-sim-installed
63
64 stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz:
65         rm -f $@ $@-t
66         wget -O $@-t 'https://github.com/riscv/riscv-qemu/archive/$(RISCV_QEMU_COMMIT)/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz'
67         mv $@-t $@
68
69 stage1-riscv-qemu/riscv-qemu.spec: stage1-riscv-qemu/riscv-qemu.spec.in
70         sed -e 's/@COMMIT@/$(RISCV_QEMU_COMMIT)/g' \
71             -e 's/@SHORTCOMMIT@/$(RISCV_QEMU_SHORTCOMMIT)/g' \
72             < $^ > $@-t
73         mv $@-t $@
74
75 stamp-riscv-qemu-installed:
76         rm -f $@
77         @rpm -q riscv-qemu >/dev/null || { \
78           echo "ERROR: You must install riscv-qemu:"; \
79           echo; \
80           echo "       dnf copr enable rjones/riscv"; \
81           echo "       dnf install riscv-qemu"; \
82           echo; \
83           echo "OR: you can build it yourself from the stage1-riscv-qemu directory."; \
84           echo; \
85           exit 1; \
86         }
87         @qemu-system-riscv --version || { \
88           echo "ERROR: qemu-system-riscv is not working."; \
89           echo "Make sure you installed the riscv-qemu package."; \
90           exit 1; \
91         }
92         touch $@
93
94 stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz:
95         rm -f $@ $@-t
96         wget -O $@-t 'https://github.com/riscv/riscv-fesvr/archive/$(RISCV_FESVR_COMMIT)/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz'
97         mv $@-t $@
98
99 stage1-riscv-fesvr/riscv-fesvr.spec: stage1-riscv-fesvr/riscv-fesvr.spec.in
100         sed -e 's/@COMMIT@/$(RISCV_FESVR_COMMIT)/g' \
101             -e 's/@SHORTCOMMIT@/$(RISCV_FESVR_SHORTCOMMIT)/g' \
102             < $^ > $@-t
103         mv $@-t $@
104
105 stamp-riscv-fesvr-installed:
106         rm -f $@
107         @rpm -q riscv-fesvr >/dev/null || { \
108           echo "ERROR: You must install riscv-fesvr:"; \
109           echo; \
110           echo "       dnf copr enable rjones/riscv"; \
111           echo "       dnf install riscv-fesvr"; \
112           echo; \
113           echo "OR: you can build it yourself from the stage1-riscv-fesvr directory."; \
114           echo; \
115           exit 1; \
116         }
117         touch $@
118
119 stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz:
120         rm -f $@ $@-t
121         wget -O $@-t 'https://github.com/riscv/riscv-isa-sim/archive/$(RISCV_ISA_SIM_COMMIT)/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz'
122         mv $@-t $@
123
124 stage1-riscv-isa-sim/riscv-isa-sim.spec: stage1-riscv-isa-sim/riscv-isa-sim.spec.in
125         sed -e 's/@COMMIT@/$(RISCV_ISA_SIM_COMMIT)/g' \
126             -e 's/@SHORTCOMMIT@/$(RISCV_ISA_SIM_SHORTCOMMIT)/g' \
127             < $^ > $@-t
128         mv $@-t $@
129
130 stamp-riscv-isa-sim-installed:
131         rm -f $@
132         @rpm -q riscv-isa-sim >/dev/null || { \
133           echo "ERROR: You must install riscv-isa-sim:"; \
134           echo; \
135           echo "       dnf copr enable rjones/riscv"; \
136           echo "       dnf install riscv-isa-sim"; \
137           echo; \
138           echo "OR: you can build it yourself from the stage1-riscv-isa-sim directory."; \
139           echo; \
140           exit 1; \
141         }
142         touch $@
143
144 # Stage 2
145
146 stage2: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz \
147         stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz \
148         stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz \
149         stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz \
150         stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz \
151         stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec \
152         stamp-riscv-gnu-toolchain-installed \
153         stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz \
154         stage2-riscv-pk/riscv-pk.spec \
155         stamp-riscv-pk-installed
156
157 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz:
158         rm -f $@ $@-t
159         wget -O $@-t https://github.com/lowRISC/riscv-gnu-toolchain/archive/$(RISCV_GNU_TOOLCHAIN_COMMIT)/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz
160         mv $@-t $@
161
162 stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz:
163         rm -f $@ $@-t
164         wget -O $@-t http://mirrors.kernel.org/gnu/binutils/binutils-$(BINUTILS_VERSION).tar.gz
165         mv $@-t $@
166
167 # GCC 5 no longer compiles with GCC 6 unless we patch it.
168 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69959
169 stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz:
170         rm -f $@ $@-t
171         wget -O $@-t http://mirrors.kernel.org/gnu/gcc/gcc-$(GCC_VERSION)/gcc-$(GCC_VERSION).tar.gz
172         zcat $@-t | tar xf -
173         cd gcc-$(GCC_VERSION) && patch -p0 < ../stage2-riscv-gnu-toolchain/gcc-5-fix-compilation-with-gcc-6.patch
174         tar zcf $@-t gcc-$(GCC_VERSION)
175         rm -r gcc-$(GCC_VERSION)
176         mv $@-t $@
177
178 stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz:
179         rm -f $@ $@-t
180         wget -O $@-t http://mirrors.kernel.org/gnu/glibc/glibc-$(GLIBC_VERSION).tar.gz
181         mv $@-t $@
182
183 stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz:
184         rm -f $@ $@-t
185         wget -O $@-t ftp://sourceware.org/pub/newlib/newlib-$(NEWLIB_VERSION).tar.gz
186         mv $@-t $@
187
188 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec.in
189         sed -e 's/@COMMIT@/$(RISCV_GNU_TOOLCHAIN_COMMIT)/g' \
190             -e 's/@SHORTCOMMIT@/$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT)/g' \
191             -e 's/@BINUTILS_VERSION@/$(BINUTILS_VERSION)/g' \
192             -e 's/@GCC_VERSION@/$(GCC_VERSION)/g' \
193             -e 's/@GLIBC_VERSION@/$(GLIBC_VERSION)/g' \
194             -e 's/@NEWLIB_VERSION@/$(NEWLIB_VERSION)/g' \
195             < $^ > $@-t
196         mv $@-t $@
197
198 stamp-riscv-gnu-toolchain-installed:
199         rm -f $@
200         @rpm -q riscv-gnu-toolchain >/dev/null || { \
201           echo "ERROR: You must install riscv-gnu-toolchain:"; \
202           echo; \
203           echo "       dnf copr enable rjones/riscv"; \
204           echo "       dnf install riscv-gnu-toolchain"; \
205           echo; \
206           echo "OR: you can build it yourself from the stage2-riscv-gnu-toolchain directory."; \
207           echo; \
208           exit 1; \
209         }
210         @riscv64-unknown-elf-gcc --version || { \
211           echo "ERROR: riscv64-unknown-elf-gcc (cross compiler) is not working."; \
212           echo "Make sure you installed the riscv-gnu-toolchain package."; \
213           exit 1; \
214         }
215         touch $@
216
217 stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz:
218         rm -f $@ $@-t
219         wget -O $@-t https://github.com/lowRISC/riscv-pk/archive/$(RISCV_PK_COMMIT)/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz
220         mv $@-t $@
221
222 stage2-riscv-pk/riscv-pk.spec: stage2-riscv-pk/riscv-pk.spec.in
223         sed -e 's/@COMMIT@/$(RISCV_PK_COMMIT)/g' \
224             -e 's/@SHORTCOMMIT@/$(RISCV_PK_SHORTCOMMIT)/g' \
225             < $^ > $@-t
226         mv $@-t $@
227
228 stamp-riscv-pk-installed:
229         rm -f $@
230         @rpm -q riscv-pk >/dev/null || { \
231           echo "ERROR: You must install riscv-pk:"; \
232           echo; \
233           echo "       dnf copr enable rjones/riscv"; \
234           echo "       dnf install riscv-pk"; \
235           echo; \
236           echo "OR: you can build it yourself from the stage2-riscv-pk directory."; \
237           echo; \
238           exit 1; \
239         }
240         touch $@
241
242 # Stage 3
243
244 stage3: stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
245         stage3-chroot-original/etc/fedora-release \
246         stage3-chroot/etc/fedora-release \
247         stage3-chroot/lib64/libc.so.6 \
248         stage3-chroot/usr/bin/tic \
249         stage3-chroot/bin/bash \
250         stage3-chroot/bin/ls \
251         stage3-chroot/usr/lib64/libgmp.so.10 \
252         stage3-chroot/usr/lib64/libmpfr.so.4 \
253         stage3-chroot/usr/lib64/libmpc.so.3 \
254         stage3-chroot/usr/bin/gcc \
255         stage3-chroot/init \
256         stage3-disk.img
257
258 stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux:
259         rm -rf stage3-kernel/linux-$(KERNEL_VERSION)
260         cp -a $(LOCAL_LINUX_GIT_COPY) stage3-kernel/linux-$(KERNEL_VERSION) || { \
261           mkdir stage3-kernel/linux-$(KERNEL_VERSION) && \
262           cd stage3-kernel/linux-$(KERNEL_VERSION) && \
263           git init; \
264         }
265         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
266         git remote add riscv https://github.com/riscv/riscv-linux && \
267         git fetch riscv && \
268         git checkout -f linux-4.1.y-riscv && \
269         make mrproper && \
270         make ARCH=riscv defconfig
271         echo CONFIG_CMDLINE=\"root=/dev/htifblk0 init=/init\" >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
272         echo CONFIG_CROSS_COMPILE=riscv64-unknown-elf- >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
273         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
274         make ARCH=riscv olddefconfig
275         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
276         make ARCH=riscv vmlinux
277         ls -l $@
278
279 # Build an original (x86-64) chroot using supermin.  We then aim to
280 # rebuild (using cross-compiled versions) every ELF binary in this
281 # chroot.
282 stage3-chroot-original/etc/fedora-release:
283         rm -rf stage3-chroot-original tmp-supermin.d
284         supermin --prepare $(STAGE3_PACKAGES) -o tmp-supermin.d
285         supermin --build -f chroot tmp-supermin.d -o stage3-chroot-original
286         rm -r tmp-supermin.d
287         @echo -n "Total files in chroot: "
288         @find stage3-chroot-original -type f | wc -l
289         @echo -n "ELF files to be rebuilt: "
290         @find stage3-chroot-original -type f | xargs file -N | grep -E '\bELF.*LSB\b' | wc -l
291
292 # Copy the original chroot to the final chroot, remove all the ELF
293 # files.
294 stage3-chroot/etc/fedora-release: stage3-chroot-original/etc/fedora-release
295         rm -rf stage3-chroot
296         cp -a stage3-chroot-original stage3-chroot
297         find stage3-chroot -type d | xargs chmod u+w
298         find stage3-chroot -type f | xargs chmod u+w
299         find stage3-chroot -type f | xargs file -N | grep -E '\bELF.*LSB\b' | awk -F: '{print $$1}' | xargs rm -f
300         rm -f stage3-chroot/lib64/libc.so.6
301
302 # Copy in compiled glibc from the riscv-gnu-toolchain sysroot.  Only
303 # copy files and symlinks, leave the target directory structure
304 # intact.
305 stage3-chroot/lib64/libc.so.6:
306         mkdir -p stage3-chroot/usr/lib/audit
307         mkdir -p stage3-chroot/usr/lib/gconv
308         for f in `cd /usr/sysroot && find -type f -o -type l`; do \
309             cp -d /usr/sysroot/$$f stage3-chroot/$$f; \
310         done
311         cd stage3-chroot/lib64 && for f in ../lib/*; do ln -sf $$f; done
312
313 # Cross-compile ncurses.
314 stage3-chroot/usr/bin/tic: ncurses-$(NCURSES_VERSION).tgz
315         tar zxf $^
316         cd ncurses-$(NCURSES_VERSION) && \
317         PATH=$(ROOT)/fixed-gcc:$$PATH \
318         ./configure --host=riscv64-unknown-linux-gnu \
319             --prefix=/usr --libdir=/usr/lib64 \
320             --with-shared
321         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
322         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
323         cd $(ROOT)/stage3-chroot/usr/lib64 && ln -sf libtinfo.so.6 libtinfo.so
324
325 ncurses-$(NCURSES_VERSION).tgz:
326         rm -f $@ $@-t
327         wget -O $@-t ftp://invisible-island.net/ncurses/current/ncurses-$(NCURSES_VERSION).tgz
328         mv $@-t $@
329
330 # Cross-compile bash.
331 stage3-chroot/bin/bash: bash-$(BASH_VERSION).tar.gz
332         tar zxf $^
333         cd bash-$(BASH_VERSION) && \
334         PATH=$(ROOT)/fixed-gcc:$$PATH \
335         ./configure --host=riscv64-unknown-linux-gnu \
336             --prefix=/usr --libdir=/usr/lib64
337         cd bash-$(BASH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
338         cd bash-$(BASH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
339
340 bash-$(BASH_VERSION).tar.gz:
341         rm -f $@ $@-t
342         wget -O $@-t ftp://ftp.gnu.org/gnu/bash/bash-$(BASH_VERSION).tar.gz
343         mv $@-t $@
344
345 # Cross-compile coreutils.  Bleah, coreutils cross-compilation is
346 # known-broken and upstream don't care, hence the 'touch' command.
347
348 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
349
350 stage3-chroot/bin/ls: coreutils-$(COREUTILS_VERSION).tar.xz
351         rm -rf coreutils-$(COREUTILS_VERSION)
352         tar Jxf $^
353         cd coreutils-$(COREUTILS_VERSION) && \
354         PATH=$(ROOT)/fixed-gcc:$$PATH \
355         ./configure --host=riscv64-unknown-linux-gnu \
356             --prefix=/usr --libdir=/usr/lib64
357         -cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
358         cd coreutils-$(COREUTILS_VERSION)/man && \
359         for f in $(COREUTILS_PROGRAMS); do touch $$f.1; done
360         cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
361         cd coreutils-$(COREUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
362
363 coreutils-$(COREUTILS_VERSION).tar.xz:
364         rm -f $@ $@-t
365         wget -O $@-t ftp://ftp.gnu.org/gnu/coreutils/coreutils-$(COREUTILS_VERSION).tar.xz
366         mv $@-t $@
367
368 # Cross-compile GMP, MPFR and MPC (deps of GCC).
369 stage3-chroot/usr/lib64/libgmp.so.10: gmp-$(GMP_VERSION).tar.lz
370         rm -rf gmp-$(GMP_VERSION)
371         tar --lzip -xf gmp-$(GMP_VERSION).tar.lz
372         cd gmp-$(GMP_VERSION) && \
373         PATH=$(ROOT)/fixed-gcc:$$PATH \
374         ./configure --host=riscv64-unknown-linux-gnu \
375             --prefix=/usr --libdir=/usr/lib64
376         cd gmp-$(GMP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
377         cd gmp-$(GMP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
378         cd stage3-chroot/usr/lib && ln -s ../lib64/libgmp.so
379
380 gmp-$(GMP_VERSION).tar.lz:
381         rm -f $@ $@-t
382         wget -O $@-t https://gmplib.org/download/gmp/gmp-$(GMP_VERSION).tar.lz
383         mv $@-t $@
384
385 stage3-chroot/usr/lib64/libmpfr.so.4: mpfr-$(MPFR_VERSION).tar.gz
386         rm -rf mpfr-$(MPFR_VERSION)
387         tar -zxf mpfr-$(MPFR_VERSION).tar.gz
388         cd mpfr-$(MPFR_VERSION) && \
389         PATH=$(ROOT)/fixed-gcc:$$PATH \
390         ./configure --host=riscv64-unknown-linux-gnu \
391             --prefix=/usr --libdir=/usr/lib64 \
392             --with-gmp=$(ROOT)/stage3-chroot/usr
393         cd mpfr-$(MPFR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
394         cd mpfr-$(MPFR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
395         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpfr.so
396
397 mpfr-$(MPFR_VERSION).tar.gz:
398         rm -f $@ $@-t
399         wget -O $@-t http://www.mpfr.org/mpfr-current/mpfr-$(MPFR_VERSION).tar.gz
400         mv $@-t $@
401
402 stage3-chroot/usr/lib64/libmpc.so.3: mpc-$(MPC_VERSION).tar.gz
403         rm -rf mpc-$(MPC_VERSION)
404         tar -zxf mpc-$(MPC_VERSION).tar.gz
405         cd mpc-$(MPC_VERSION) && \
406         PATH=$(ROOT)/fixed-gcc:$$PATH \
407         ./configure --host=riscv64-unknown-linux-gnu \
408             --prefix=/usr --libdir=/usr/lib64 \
409             --with-gmp=$(ROOT)/stage3-chroot/usr \
410             --with-mpfr=$(ROOT)/stage3-chroot/usr
411         cd mpc-$(MPC_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
412         cd mpc-$(MPC_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
413         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpc.so
414
415 mpc-$(MPC_VERSION).tar.gz:
416         rm -f $@ $@-t
417         wget -O $@-t ftp://ftp.gnu.org/gnu/mpc/mpc-$(MPC_VERSION).tar.gz
418         mv $@-t $@
419
420 # Cross-compile GCC.
421 stage3-chroot/usr/bin/gcc: gcc-$(GCC_X_VERSION).tar.gz
422         rm -rf riscv-gcc-riscv-gcc-$(GCC_X_VERSION)
423         zcat $^ | tar xf -
424         mkdir riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build
425         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && \
426         PATH=$(ROOT)/fixed-gcc:$$PATH \
427         ../configure \
428             --host=riscv64-unknown-linux-gnu \
429             --prefix=/usr --libdir=/usr/lib64 \
430             --enable-shared \
431             --enable-tls \
432             --enable-languages=c,c++ \
433             --disable-libmudflap \
434             --disable-libssp \
435             --disable-libquadmath \
436             --disable-nls \
437             --disable-multilib
438         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make all-gcc
439         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && make install-gcc DESTDIR=$(ROOT)/stage3-chroot
440
441 gcc-$(GCC_X_VERSION).tar.gz:
442         rm -f $@ $@-t
443         wget -O $@-t https://github.com/riscv/riscv-gcc/archive/riscv-gcc-$(GCC_X_VERSION).tar.gz
444         mv $@-t $@
445
446 # Create an /init script.
447 stage3-chroot/init: init.sh
448         install -m 0755 $^ $@
449
450 # Create the stage3 disk image.
451 # Note `-s +...' adds spare space to the disk image.
452 stage3-disk.img: stage3-chroot
453         cd stage3-chroot && virt-make-fs . ../$@ -t ext2 -F raw -s +4G
454
455 # Upload the compressed disk image.
456 upload-stage3: stage3-disk.img.xz
457         scp $^ tick:public_html/riscv/
458 stage3-disk.img.xz: stage3-disk.img
459         rm -f $@
460         xz --best $^
461
462 # Helper which boots stage3 disk image in spike.
463 boot-stage3-in-spike: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
464         spike +disk=stage3-disk.img \
465             /usr/bin/bbl stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
466
467 # Helper which boots stage3 disk image in qemu.
468 boot-stage3-in-qemu: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
469         qemu-system-riscv -kernel /usr/bin/bbl \
470             -append ./stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
471             -drive file=stage3-disk.img,format=raw -nographic
472
473 # Stage 4
474
475 stage4:
476         echo "XXX TO DO"
477         exit 1
478
479 .NOTPARALLEL: