e953bcbecb4dfb7928ce1d5e55a29bc7af8d1bef
[fedora-riscv.git] / Makefile
1 # Refer to the README file to understand how Fedora on RISC-V is
2 # bootstrapped.
3
4 # Absolute path to the current directory.
5 ROOT := $(shell pwd)
6
7 # Note these are chosen very specifically to ensure the different
8 # versions work together.  Don't blindly update to the latest
9 # versions.  See also:
10 # https://github.com/riscv/riscv-pk/issues/18#issuecomment-206115996
11 RISCV_QEMU_COMMIT               = 94f5eb73091fb4fe272db3e943f173ecc0f78ffd
12 RISCV_QEMU_SHORTCOMMIT          = 94f5eb73
13 RISCV_FESVR_COMMIT              = 0f34d7ad311f78455a674224225f5b3056efba1d
14 RISCV_FESVR_SHORTCOMMIT         = 0f34d7ad
15 RISCV_ISA_SIM_COMMIT            = 3bfc00ef2a1b1f0b0472a39a866261b00f67027e
16 RISCV_ISA_SIM_SHORTCOMMIT       = 3bfc00ef
17 RISCV_GNU_TOOLCHAIN_COMMIT      = 728afcddcb0526a0f6560c4032da82805f054d58
18 RISCV_GNU_TOOLCHAIN_SHORTCOMMIT = 728afcdd
19 RISCV_PK_COMMIT                 = 85ae17aa149b9ea114bdd70cc30ea7e73813fb48
20 RISCV_PK_SHORTCOMMIT            = 85ae17aa
21
22 # For the correct versions, see
23 # riscv-gnu-toolchain/Makefile.in *_version variables
24 BINUTILS_VERSION = 2.25.1
25 GLIBC_VERSION    = 2.22
26 GCC_VERSION      = 5.3.0
27 NEWLIB_VERSION   = 2.2.0
28
29 # See linux-4.1.y-riscv branch of
30 # https://github.com/riscv/riscv-linux
31 KERNEL_VERSION   = 4.1.26
32
33 # A local copy of Linux git repo so you don't have to keep downloading
34 # git commits (optional).
35 LOCAL_LINUX_GIT_COPY = $(HOME)/d/linux
36
37 # The root packages (plus their dependencies) that we want to
38 # cross-compile into the stage 3 chroot.
39 # beecrypt-devel is required to build RPM.
40 STAGE3_PACKAGES = gcc rpm-build beecrypt-devel
41
42 # Versions of cross-compiled packages.
43 NCURSES_VERSION    = 6.0-20160730
44 BASH_VERSION       = 4.3
45 COREUTILS_VERSION  = 8.25
46 GMP_VERSION        = 6.1.1
47 MPFR_VERSION       = 3.1.4
48 MPC_VERSION        = 1.0.3
49 BINUTILS_X_VERSION = 2.26
50 GCC_X_VERSION      = 6.1.0
51 UTIL_LINUX_VERSION = 2.28
52 TAR_VERSION        = 1.29
53 GZIP_VERSION       = 1.8
54 ZLIB_VERSION       = 1.2.8
55 # Needs to match the installed (on host) version, otherwise:
56 # "Cannot use the installed version of file (xx) to cross-compile file yy"
57 FILE_VERSION       = 5.25
58 POPT_VERSION       = 1.16
59 BEECRYPT_VERSION   = 4.2.1
60 RPM_COMMIT         = 95712183458748ea6cafebac1bdd5daa097d9bee
61 RPM_SHORT_COMMIT   = 9571218
62 BDB_VERSION        = 4.5.20
63
64 all: stage1 stage2 stage3 stage4
65
66 # Stage 1
67
68 stage1: stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz \
69         stage1-riscv-qemu/riscv-qemu.spec \
70         stamp-riscv-qemu-installed \
71         stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz \
72         stage1-riscv-fesvr/riscv-fesvr.spec \
73         stamp-riscv-fesvr-installed \
74         stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz \
75         stage1-riscv-isa-sim/riscv-isa-sim.spec \
76         stamp-riscv-isa-sim-installed
77
78 stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz:
79         rm -f $@ $@-t
80         wget -O $@-t 'https://github.com/riscv/riscv-qemu/archive/$(RISCV_QEMU_COMMIT)/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz'
81         mv $@-t $@
82
83 stage1-riscv-qemu/riscv-qemu.spec: stage1-riscv-qemu/riscv-qemu.spec.in
84         sed -e 's/@COMMIT@/$(RISCV_QEMU_COMMIT)/g' \
85             -e 's/@SHORTCOMMIT@/$(RISCV_QEMU_SHORTCOMMIT)/g' \
86             < $^ > $@-t
87         mv $@-t $@
88
89 stamp-riscv-qemu-installed:
90         rm -f $@
91         @rpm -q riscv-qemu >/dev/null || { \
92           echo "ERROR: You must install riscv-qemu:"; \
93           echo; \
94           echo "       dnf copr enable rjones/riscv"; \
95           echo "       dnf install riscv-qemu"; \
96           echo; \
97           echo "OR: you can build it yourself from the stage1-riscv-qemu directory."; \
98           echo; \
99           exit 1; \
100         }
101         @qemu-system-riscv --version || { \
102           echo "ERROR: qemu-system-riscv is not working."; \
103           echo "Make sure you installed the riscv-qemu package."; \
104           exit 1; \
105         }
106         touch $@
107
108 stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz:
109         rm -f $@ $@-t
110         wget -O $@-t 'https://github.com/riscv/riscv-fesvr/archive/$(RISCV_FESVR_COMMIT)/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz'
111         mv $@-t $@
112
113 stage1-riscv-fesvr/riscv-fesvr.spec: stage1-riscv-fesvr/riscv-fesvr.spec.in
114         sed -e 's/@COMMIT@/$(RISCV_FESVR_COMMIT)/g' \
115             -e 's/@SHORTCOMMIT@/$(RISCV_FESVR_SHORTCOMMIT)/g' \
116             < $^ > $@-t
117         mv $@-t $@
118
119 stamp-riscv-fesvr-installed:
120         rm -f $@
121         @rpm -q riscv-fesvr >/dev/null || { \
122           echo "ERROR: You must install riscv-fesvr:"; \
123           echo; \
124           echo "       dnf copr enable rjones/riscv"; \
125           echo "       dnf install riscv-fesvr"; \
126           echo; \
127           echo "OR: you can build it yourself from the stage1-riscv-fesvr directory."; \
128           echo; \
129           exit 1; \
130         }
131         touch $@
132
133 stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz:
134         rm -f $@ $@-t
135         wget -O $@-t 'https://github.com/riscv/riscv-isa-sim/archive/$(RISCV_ISA_SIM_COMMIT)/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz'
136         mv $@-t $@
137
138 stage1-riscv-isa-sim/riscv-isa-sim.spec: stage1-riscv-isa-sim/riscv-isa-sim.spec.in
139         sed -e 's/@COMMIT@/$(RISCV_ISA_SIM_COMMIT)/g' \
140             -e 's/@SHORTCOMMIT@/$(RISCV_ISA_SIM_SHORTCOMMIT)/g' \
141             < $^ > $@-t
142         mv $@-t $@
143
144 stamp-riscv-isa-sim-installed:
145         rm -f $@
146         @rpm -q riscv-isa-sim >/dev/null || { \
147           echo "ERROR: You must install riscv-isa-sim:"; \
148           echo; \
149           echo "       dnf copr enable rjones/riscv"; \
150           echo "       dnf install riscv-isa-sim"; \
151           echo; \
152           echo "OR: you can build it yourself from the stage1-riscv-isa-sim directory."; \
153           echo; \
154           exit 1; \
155         }
156         touch $@
157
158 # Stage 2
159
160 stage2: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz \
161         stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz \
162         stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz \
163         stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz \
164         stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz \
165         stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec \
166         stamp-riscv-gnu-toolchain-installed \
167         stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz \
168         stage2-riscv-pk/riscv-pk.spec \
169         stamp-riscv-pk-installed
170
171 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz:
172         rm -f $@ $@-t
173         wget -O $@-t https://github.com/lowRISC/riscv-gnu-toolchain/archive/$(RISCV_GNU_TOOLCHAIN_COMMIT)/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz
174         mv $@-t $@
175
176 stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz:
177         rm -f $@ $@-t
178         wget -O $@-t http://mirrors.kernel.org/gnu/binutils/binutils-$(BINUTILS_VERSION).tar.gz
179         mv $@-t $@
180
181 # GCC 5 no longer compiles with GCC 6 unless we patch it.
182 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69959
183 stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz:
184         rm -f $@ $@-t
185         wget -O $@-t http://mirrors.kernel.org/gnu/gcc/gcc-$(GCC_VERSION)/gcc-$(GCC_VERSION).tar.gz
186         zcat $@-t | tar xf -
187         cd gcc-$(GCC_VERSION) && patch -p0 < ../stage2-riscv-gnu-toolchain/gcc-5-fix-compilation-with-gcc-6.patch
188         tar zcf $@-t gcc-$(GCC_VERSION)
189         rm -r gcc-$(GCC_VERSION)
190         mv $@-t $@
191
192 stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz:
193         rm -f $@ $@-t
194         wget -O $@-t http://mirrors.kernel.org/gnu/glibc/glibc-$(GLIBC_VERSION).tar.gz
195         mv $@-t $@
196
197 stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz:
198         rm -f $@ $@-t
199         wget -O $@-t ftp://sourceware.org/pub/newlib/newlib-$(NEWLIB_VERSION).tar.gz
200         mv $@-t $@
201
202 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec.in
203         sed -e 's/@COMMIT@/$(RISCV_GNU_TOOLCHAIN_COMMIT)/g' \
204             -e 's/@SHORTCOMMIT@/$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT)/g' \
205             -e 's/@BINUTILS_VERSION@/$(BINUTILS_VERSION)/g' \
206             -e 's/@GCC_VERSION@/$(GCC_VERSION)/g' \
207             -e 's/@GLIBC_VERSION@/$(GLIBC_VERSION)/g' \
208             -e 's/@NEWLIB_VERSION@/$(NEWLIB_VERSION)/g' \
209             < $^ > $@-t
210         mv $@-t $@
211
212 stamp-riscv-gnu-toolchain-installed:
213         rm -f $@
214         @rpm -q riscv-gnu-toolchain >/dev/null || { \
215           echo "ERROR: You must install riscv-gnu-toolchain:"; \
216           echo; \
217           echo "       dnf copr enable rjones/riscv"; \
218           echo "       dnf install riscv-gnu-toolchain"; \
219           echo; \
220           echo "OR: you can build it yourself from the stage2-riscv-gnu-toolchain directory."; \
221           echo; \
222           exit 1; \
223         }
224         @riscv64-unknown-elf-gcc --version || { \
225           echo "ERROR: riscv64-unknown-elf-gcc (cross compiler) is not working."; \
226           echo "Make sure you installed the riscv-gnu-toolchain package."; \
227           exit 1; \
228         }
229         touch $@
230
231 stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz:
232         rm -f $@ $@-t
233         wget -O $@-t https://github.com/lowRISC/riscv-pk/archive/$(RISCV_PK_COMMIT)/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz
234         mv $@-t $@
235
236 stage2-riscv-pk/riscv-pk.spec: stage2-riscv-pk/riscv-pk.spec.in
237         sed -e 's/@COMMIT@/$(RISCV_PK_COMMIT)/g' \
238             -e 's/@SHORTCOMMIT@/$(RISCV_PK_SHORTCOMMIT)/g' \
239             < $^ > $@-t
240         mv $@-t $@
241
242 stamp-riscv-pk-installed:
243         rm -f $@
244         @rpm -q riscv-pk >/dev/null || { \
245           echo "ERROR: You must install riscv-pk:"; \
246           echo; \
247           echo "       dnf copr enable rjones/riscv"; \
248           echo "       dnf install riscv-pk"; \
249           echo; \
250           echo "OR: you can build it yourself from the stage2-riscv-pk directory."; \
251           echo; \
252           exit 1; \
253         }
254         touch $@
255
256 # Stage 3
257
258 stage3: stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
259         stage3-chroot-original/etc/fedora-release \
260         stage3-chroot/etc/fedora-release \
261         stage3-chroot/lib64/libc.so.6 \
262         stage3-chroot/usr/bin/tic \
263         stage3-chroot/bin/bash \
264         stage3-chroot/bin/ls \
265         stage3-chroot/usr/lib64/libgmp.so.10 \
266         stage3-chroot/usr/lib64/libmpfr.so.4 \
267         stage3-chroot/usr/lib64/libmpc.so.3 \
268         stage3-chroot/usr/bin/as \
269         stage3-chroot/usr/bin/gcc \
270         stage3-chroot/usr/bin/mount \
271         stage3-chroot/usr/bin/tar \
272         stage3-chroot/usr/bin/gzip \
273         stage3-chroot/usr/lib64/libz.so \
274         stage3-chroot/usr/bin/file \
275         stage3-chroot/usr/lib64/libpopt.so \
276         stage3-chroot/usr/lib64/libbeecrypt.so \
277         stage3-chroot/usr/bin/rpm \
278         stage3-chroot/init \
279         stage3-disk.img
280
281 stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux:
282         rm -rf stage3-kernel/linux-$(KERNEL_VERSION)
283         cp -a $(LOCAL_LINUX_GIT_COPY) stage3-kernel/linux-$(KERNEL_VERSION) || { \
284           mkdir stage3-kernel/linux-$(KERNEL_VERSION) && \
285           cd stage3-kernel/linux-$(KERNEL_VERSION) && \
286           git init; \
287         }
288         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
289         git remote add riscv https://github.com/riscv/riscv-linux && \
290         git fetch riscv && \
291         git checkout -f linux-4.1.y-riscv && \
292         make mrproper && \
293         make ARCH=riscv defconfig
294         echo CONFIG_CMDLINE=\"root=/dev/htifblk0 init=/init\" >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
295         echo CONFIG_CROSS_COMPILE=riscv64-unknown-elf- >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
296         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
297         make ARCH=riscv olddefconfig
298         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
299         make ARCH=riscv vmlinux
300         ls -l $@
301
302 # Build an original (x86-64) chroot using supermin.  We then aim to
303 # rebuild (using cross-compiled versions) every ELF binary in this
304 # chroot.
305 stage3-chroot-original/etc/fedora-release:
306         rm -rf stage3-chroot-original tmp-supermin.d
307         supermin --prepare $(STAGE3_PACKAGES) -o tmp-supermin.d
308         supermin --build -f chroot tmp-supermin.d -o stage3-chroot-original
309         rm -r tmp-supermin.d
310         @echo -n "Total files in chroot: "
311         @find stage3-chroot-original -type f | wc -l
312         @echo -n "ELF files to be rebuilt: "
313         @find stage3-chroot-original -type f | xargs file -N | grep -E '\bELF.*LSB\b' | wc -l
314
315 # Copy the original chroot to the final chroot, remove all the ELF
316 # files.
317 stage3-chroot/etc/fedora-release: stage3-chroot-original/etc/fedora-release
318         rm -rf stage3-chroot
319         cp -a stage3-chroot-original stage3-chroot
320         find stage3-chroot -type d | xargs chmod u+w
321         find stage3-chroot -type f | xargs chmod u+w
322         find stage3-chroot -type f | xargs file -N | grep -E '\bELF.*LSB\b' | awk -F: '{print $$1}' | xargs rm -f
323         rm -f stage3-chroot/lib64/libc.so.6
324
325 # Copy in compiled glibc from the riscv-gnu-toolchain sysroot.  Only
326 # copy files and symlinks, leave the target directory structure
327 # intact.
328 stage3-chroot/lib64/libc.so.6:
329         mkdir -p stage3-chroot/usr/lib/audit
330         mkdir -p stage3-chroot/usr/lib/gconv
331         for f in `cd /usr/sysroot && find -type f -o -type l`; do \
332             cp -d /usr/sysroot/$$f stage3-chroot/$$f; \
333         done
334         cd stage3-chroot/lib64 && for f in ../lib/*; do ln -sf $$f; done
335
336 # Cross-compile ncurses.
337 stage3-chroot/usr/bin/tic: ncurses-$(NCURSES_VERSION).tgz
338         tar zxf $^
339         cd ncurses-$(NCURSES_VERSION) && \
340         PATH=$(ROOT)/fixed-gcc:$$PATH \
341         ./configure --host=riscv64-unknown-linux-gnu \
342             --prefix=/usr --libdir=/usr/lib64 \
343             --with-shared \
344             --with-termlib=tinfo \
345             --enable-widec
346         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
347         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
348
349 ncurses-$(NCURSES_VERSION).tgz:
350         rm -f $@ $@-t
351         wget -O $@-t ftp://invisible-island.net/ncurses/current/ncurses-$(NCURSES_VERSION).tgz
352         mv $@-t $@
353
354 # Cross-compile bash.
355 stage3-chroot/bin/bash: bash-$(BASH_VERSION).tar.gz
356         tar zxf $^
357         cd bash-$(BASH_VERSION) && \
358         PATH=$(ROOT)/fixed-gcc:$$PATH \
359         ./configure --host=riscv64-unknown-linux-gnu \
360             --prefix=/usr --libdir=/usr/lib64
361         cd bash-$(BASH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
362         cd bash-$(BASH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
363
364 bash-$(BASH_VERSION).tar.gz:
365         rm -f $@ $@-t
366         wget -O $@-t ftp://ftp.gnu.org/gnu/bash/bash-$(BASH_VERSION).tar.gz
367         mv $@-t $@
368
369 # Cross-compile coreutils.  Bleah, coreutils cross-compilation is
370 # known-broken and upstream don't care, hence the 'touch' command.
371
372 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
373
374 stage3-chroot/bin/ls: coreutils-$(COREUTILS_VERSION).tar.xz
375         rm -rf coreutils-$(COREUTILS_VERSION)
376         tar Jxf $^
377         cd coreutils-$(COREUTILS_VERSION) && \
378         PATH=$(ROOT)/fixed-gcc:$$PATH \
379         ./configure --host=riscv64-unknown-linux-gnu \
380             --prefix=/usr --libdir=/usr/lib64
381         -cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
382         cd coreutils-$(COREUTILS_VERSION)/man && \
383         for f in $(COREUTILS_PROGRAMS); do touch $$f.1; done
384         cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
385         cd coreutils-$(COREUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
386
387 coreutils-$(COREUTILS_VERSION).tar.xz:
388         rm -f $@ $@-t
389         wget -O $@-t ftp://ftp.gnu.org/gnu/coreutils/coreutils-$(COREUTILS_VERSION).tar.xz
390         mv $@-t $@
391
392 # Cross-compile binutils.
393 stage3-chroot/usr/bin/as: binutils-$(BINUTILS_X_VERSION).tar.gz
394         rm -rf riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)
395         zcat $^ | tar xf -
396         mkdir riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build
397         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && \
398         PATH=$(ROOT)/fixed-gcc:$$PATH \
399         ../configure \
400             --host=riscv64-unknown-linux-gnu \
401             --prefix=/usr --libdir=/usr/lib64
402         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make
403         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && make DESTDIR=$(ROOT)/stage3-chroot install
404
405 binutils-$(BINUTILS_X_VERSION).tar.gz:
406         rm -f $@ $@-t
407         wget -O $@-t https://github.com/riscv/riscv-binutils-gdb/archive/riscv-binutils-$(BINUTILS_X_VERSION).tar.gz
408         mv $@-t $@
409
410 # Cross-compile GMP, MPFR and MPC (deps of GCC).
411 stage3-chroot/usr/lib64/libgmp.so.10: gmp-$(GMP_VERSION).tar.lz
412         rm -rf gmp-$(GMP_VERSION)
413         tar --lzip -xf gmp-$(GMP_VERSION).tar.lz
414         cd gmp-$(GMP_VERSION) && \
415         PATH=$(ROOT)/fixed-gcc:$$PATH \
416         ./configure --host=riscv64-unknown-linux-gnu \
417             --prefix=/usr --libdir=/usr/lib64
418         cd gmp-$(GMP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
419         cd gmp-$(GMP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
420         cd stage3-chroot/usr/lib && ln -s ../lib64/libgmp.so
421
422 gmp-$(GMP_VERSION).tar.lz:
423         rm -f $@ $@-t
424         wget -O $@-t https://gmplib.org/download/gmp/gmp-$(GMP_VERSION).tar.lz
425         mv $@-t $@
426
427 stage3-chroot/usr/lib64/libmpfr.so.4: mpfr-$(MPFR_VERSION).tar.gz
428         rm -rf mpfr-$(MPFR_VERSION)
429         tar -zxf mpfr-$(MPFR_VERSION).tar.gz
430         cd mpfr-$(MPFR_VERSION) && \
431         PATH=$(ROOT)/fixed-gcc:$$PATH \
432         ./configure --host=riscv64-unknown-linux-gnu \
433             --prefix=/usr --libdir=/usr/lib64 \
434             --with-gmp=$(ROOT)/stage3-chroot/usr
435         cd mpfr-$(MPFR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
436         cd mpfr-$(MPFR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
437         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpfr.so
438         rm -f stage3-chroot/usr/lib64/*.la
439
440 mpfr-$(MPFR_VERSION).tar.gz:
441         rm -f $@ $@-t
442         wget -O $@-t http://www.mpfr.org/mpfr-current/mpfr-$(MPFR_VERSION).tar.gz
443         mv $@-t $@
444
445 stage3-chroot/usr/lib64/libmpc.so.3: mpc-$(MPC_VERSION).tar.gz
446         rm -rf mpc-$(MPC_VERSION)
447         tar -zxf mpc-$(MPC_VERSION).tar.gz
448         cd mpc-$(MPC_VERSION) && \
449         PATH=$(ROOT)/fixed-gcc:$$PATH \
450         ./configure --host=riscv64-unknown-linux-gnu \
451             --prefix=/usr --libdir=/usr/lib64 \
452             --with-gmp=$(ROOT)/stage3-chroot/usr \
453             --with-mpfr=$(ROOT)/stage3-chroot/usr
454         cd mpc-$(MPC_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
455         cd mpc-$(MPC_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
456         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpc.so
457         rm -f stage3-chroot/usr/lib64/*.la
458
459 mpc-$(MPC_VERSION).tar.gz:
460         rm -f $@ $@-t
461         wget -O $@-t ftp://ftp.gnu.org/gnu/mpc/mpc-$(MPC_VERSION).tar.gz
462         mv $@-t $@
463
464 # Cross-compile GCC.
465 stage3-chroot/usr/bin/gcc: gcc-$(GCC_X_VERSION).tar.gz
466         rm -rf riscv-gcc-riscv-gcc-$(GCC_X_VERSION)
467         zcat $^ | tar xf -
468         mkdir riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build
469         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && \
470         PATH=$(ROOT)/fixed-gcc:$$PATH \
471         ../configure \
472             --host=riscv64-unknown-linux-gnu \
473             --prefix=/usr --libdir=/usr/lib64 \
474             --enable-shared \
475             --enable-tls \
476             --enable-languages=c,c++ \
477             --disable-libmudflap \
478             --disable-libssp \
479             --disable-libquadmath \
480             --disable-nls \
481             --disable-multilib
482 # XXX use make all & make install here.  However building libgcc
483 # fails, see 'broken-gcc.log'.
484         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make all-gcc
485         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && make install-gcc DESTDIR=$(ROOT)/stage3-chroot
486
487 gcc-$(GCC_X_VERSION).tar.gz:
488         rm -f $@ $@-t
489         wget -O $@-t https://github.com/riscv/riscv-gcc/archive/riscv-gcc-$(GCC_X_VERSION).tar.gz
490         mv $@-t $@
491
492 # Cross-compile util-linux.
493 stage3-chroot/usr/bin/mount: util-linux-$(UTIL_LINUX_VERSION).tar.xz
494         rm -rf util-linux-$(UTIL_LINUX_VERSION)
495         tar -Jxf $^
496         cd util-linux-$(UTIL_LINUX_VERSION) && \
497         PATH=$(ROOT)/fixed-gcc:$$PATH \
498         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
499         ./configure \
500             --host=riscv64-unknown-linux-gnu \
501             --prefix=/usr --libdir=/usr/lib64 \
502             --without-python \
503             --without-systemd \
504             --disable-makeinstall-chown \
505             --enable-static-programs=mount
506         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
507         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
508         rm -f stage3-chroot/usr/lib64/*.la
509
510 util-linux-$(UTIL_LINUX_VERSION).tar.xz:
511         rm -f $@ $@-t
512         wget -O $@-t ftp://ftp.kernel.org/pub/linux/utils/util-linux/v$(UTIL_LINUX_VERSION)/util-linux-$(UTIL_LINUX_VERSION).tar.xz
513         mv $@-t $@
514
515 # Cross-compile GNU tar.
516 stage3-chroot/usr/bin/tar: tar-$(TAR_VERSION).tar.xz
517         rm -rf tar-$(TAR_VERSION)
518         tar -Jxf $^
519         cd tar-$(TAR_VERSION) && \
520         PATH=$(ROOT)/fixed-gcc:$$PATH \
521         ./configure \
522             --host=riscv64-unknown-linux-gnu \
523             --prefix=/usr --libdir=/usr/lib64
524         cd tar-$(TAR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
525         cd tar-$(TAR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
526
527 tar-$(TAR_VERSION).tar.xz:
528         rm -f $@ $@-t
529         wget -O $@-t https://ftp.gnu.org/gnu/tar/tar-$(TAR_VERSION).tar.xz
530         mv $@-t $@
531
532 # Cross-compile GNU gzip.
533 stage3-chroot/usr/bin/gzip: gzip-$(GZIP_VERSION).tar.gz
534         rm -rf gzip-$(GZIP_VERSION)
535         tar -zxf $^
536         cd gzip-$(GZIP_VERSION) && \
537         PATH=$(ROOT)/fixed-gcc:$$PATH \
538         ./configure \
539             --host=riscv64-unknown-linux-gnu \
540             --prefix=/usr --libdir=/usr/lib64
541         cd gzip-$(GZIP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
542         cd gzip-$(GZIP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
543
544 gzip-$(GZIP_VERSION).tar.gz:
545         rm -f $@ $@-t
546         wget -O $@-t https://ftp.gnu.org/gnu/gzip/gzip-$(GZIP_VERSION).tar.gz
547         mv $@-t $@
548
549 # Cross-compile zlib.
550 stage3-chroot/usr/lib64/libz.so: zlib-$(ZLIB_VERSION).tar.gz
551         rm -rf zlib-$(ZLIB_VERSION)
552         tar -zxf $^
553         cd zlib-$(ZLIB_VERSION) && \
554         PATH=$(ROOT)/fixed-gcc:$$PATH \
555         CC=riscv64-unknown-linux-gnu-gcc \
556         CFLAGS="-I$(ROOT)/stage3-chroot/usr/include -L$(ROOT)/stage3-chroot/usr/lib" \
557         ./configure \
558             --prefix=/usr --libdir=/usr/lib64
559         cd zlib-$(ZLIB_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make shared
560         cd zlib-$(ZLIB_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
561
562 zlib-$(ZLIB_VERSION).tar.gz:
563         rm -f $@ $@-t
564         wget -O $@-t http://zlib.net/zlib-$(ZLIB_VERSION).tar.gz
565         mv $@-t $@
566
567 # Cross-compile file/libmagic.
568 stage3-chroot/usr/bin/file: file-$(FILE_VERSION).tar.gz
569         rm -rf file-$(FILE_VERSION)
570         tar -zxf $^
571         cd file-$(FILE_VERSION) && \
572         PATH=$(ROOT)/fixed-gcc:$$PATH \
573         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
574         ./configure \
575             --host=riscv64-unknown-linux-gnu \
576             --prefix=/usr --libdir=/usr/lib64 \
577             --disable-static --enable-shared
578         cd file-$(FILE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
579         cd file-$(FILE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
580         rm -f stage3-chroot/usr/lib64/*.la
581
582 file-$(FILE_VERSION).tar.gz:
583         rm -f $@ $@-t
584         wget -O $@-t ftp://ftp.astron.com/pub/file/file-$(FILE_VERSION).tar.gz
585         mv $@-t $@
586
587 # Cross-compile popt.
588 stage3-chroot/usr/lib64/libpopt.so: popt-$(POPT_VERSION).tar.gz
589         rm -rf popt-$(POPT_VERSION)
590         tar -zxf $^
591         cd popt-$(POPT_VERSION) && \
592         PATH=$(ROOT)/fixed-gcc:$$PATH \
593         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
594         ./configure \
595             --host=riscv64-unknown-linux-gnu \
596             --prefix=/usr --libdir=/usr/lib64 \
597             --disable-static --enable-shared
598         cd popt-$(POPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
599         cd popt-$(POPT_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
600         rm -f stage3-chroot/usr/lib64/*.la
601
602 popt-$(POPT_VERSION).tar.gz:
603         rm -f $@ $@-t
604         wget -O $@-t http://rpm5.org/files/popt/popt-$(POPT_VERSION).tar.gz
605         mv $@-t $@
606
607 # Cross-compile beecrypt.
608 stage3-chroot/usr/lib64/libbeecrypt.so: beecrypt-$(BEECRYPT_VERSION).tar.gz
609         rm -rf beecrypt-$(BEECRYPT_VERSION)
610         tar -zxf $^
611         cd beecrypt-$(BEECRYPT_VERSION) && patch -p0 < ../beecrypt-disable-cplusplus.patch
612         cd beecrypt-$(BEECRYPT_VERSION) && autoreconf -i
613         cd beecrypt-$(BEECRYPT_VERSION) && \
614         PATH=$(ROOT)/fixed-gcc:$$PATH \
615         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
616         ./configure \
617             --host=riscv64-unknown-linux-gnu \
618             --prefix=/usr --libdir=/usr/lib64 \
619             --without-cplusplus \
620             --without-java \
621             --disable-openmp \
622             --disable-static \
623             --enable-shared
624         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
625         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot V=1
626         chrpath -d stage3-chroot/usr/lib64/libbeecrypt.so.*
627         rm -f stage3-chroot/usr/lib64/*.la
628
629 beecrypt-$(BEECRYPT_VERSION).tar.gz:
630         rm -f $@ $@-t
631         wget -O $@-t http://downloads.sourceforge.net/sourceforge/beecrypt/beecrypt-$(BEECRYPT_VERSION).tar.gz
632         mv $@-t $@
633
634 # Cross-compile RPM / rpmbuild.
635 # We build this from a git commit, with a few hacks to the configure
636 # script.
637 stage3-chroot/usr/bin/rpm: rpm-$(RPM_SHORT_COMMIT).tar.gz db-$(BDB_VERSION).tar.gz
638         rm -rf rpm-$(RPM_SHORT_COMMIT)
639         tar -zxf rpm-$(RPM_SHORT_COMMIT).tar.gz
640         tar -zxf db-$(BDB_VERSION).tar.gz -C rpm-$(RPM_SHORT_COMMIT)
641         cd rpm-$(RPM_SHORT_COMMIT) && ln -s db-$(BDB_VERSION) db
642         cd rpm-$(RPM_SHORT_COMMIT) && patch -p1 < ../0001-HACKS-TO-GET-RPM-TO-CROSS-COMPILE.patch
643         cd rpm-$(RPM_SHORT_COMMIT) && patch -p1 < ../0002-RISCV-64-bit-riscv64-support.patch
644         cd rpm-$(RPM_SHORT_COMMIT) && autoreconf -i
645         cd rpm-$(RPM_SHORT_COMMIT) && \
646         PATH=$(ROOT)/fixed-gcc:$$PATH \
647         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
648         ./configure \
649             --host=riscv64-unknown-linux-gnu \
650             --prefix=/usr --libdir=/usr/lib64 \
651             --disable-rpath \
652             --without-libarchive \
653             --without-lua \
654             --with-beecrypt \
655             --without-archive \
656             --without-external-db \
657             --enable-ndb \
658             --disable-plugins
659         cd rpm-$(RPM_SHORT_COMMIT) && \
660         sed -i 's|^sys_lib_dlsearch_path_spec="/lib64|sys_lib_dlsearch_path_spec="$(ROOT)/stage3-chroot/usr/lib64 /lib64|g' libtool
661         cd rpm-$(RPM_SHORT_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
662         cd rpm-$(RPM_SHORT_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
663         rm -f stage3-chroot/usr/lib64/*.la
664
665 rpm-$(RPM_SHORT_COMMIT).tar.gz:
666         rm -f $@ $@-t
667         wget -O $@-t 'http://rpm.org/gitweb?p=rpm.git;a=snapshot;h=$(RPM_COMMIT);sf=tgz'
668         mv $@-t $@
669
670 db-$(BDB_VERSION).tar.gz:
671         rm -f $@ $@-t
672         wget -O $@-t http://download.oracle.com/berkeley-db/db-$(BDB_VERSION).tar.gz
673         mv $@-t $@
674
675 # Create an /init script.
676 stage3-chroot/init: init.sh
677         install -m 0755 $^ $@
678
679 # Create the stage3 disk image.
680 # Note `-s +...' adds spare space to the disk image.
681 stage3-disk.img: stage3-chroot
682         cd stage3-chroot && virt-make-fs . ../$@ -t ext2 -F raw -s +4G
683
684 # Upload the compressed disk image.
685 upload-stage3: stage3-disk.img.xz
686         scp $^ tick:public_html/riscv/
687 stage3-disk.img.xz: stage3-disk.img
688         rm -f $@
689         xz --best $^
690
691 # Helper which boots stage3 disk image in spike.
692 boot-stage3-in-spike: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
693         spike +disk=stage3-disk.img \
694             /usr/bin/bbl stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
695
696 # Helper which boots stage3 disk image in qemu.
697 boot-stage3-in-qemu: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
698         qemu-system-riscv -kernel /usr/bin/bbl \
699             -append ./stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
700             -drive file=stage3-disk.img,format=raw -nographic
701
702 # Stage 4
703
704 stage4:
705         echo "XXX TO DO"
706         exit 1
707
708 .NOTPARALLEL: