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