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