aa5bd8cc01ff48d451c686c8042945a9f6275492
[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         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
345         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
346         cd $(ROOT)/stage3-chroot/usr/lib64 && ln -sf libtinfo.so.6 libtinfo.so
347
348 ncurses-$(NCURSES_VERSION).tgz:
349         rm -f $@ $@-t
350         wget -O $@-t ftp://invisible-island.net/ncurses/current/ncurses-$(NCURSES_VERSION).tgz
351         mv $@-t $@
352
353 # Cross-compile bash.
354 stage3-chroot/bin/bash: bash-$(BASH_VERSION).tar.gz
355         tar zxf $^
356         cd bash-$(BASH_VERSION) && \
357         PATH=$(ROOT)/fixed-gcc:$$PATH \
358         ./configure --host=riscv64-unknown-linux-gnu \
359             --prefix=/usr --libdir=/usr/lib64
360         cd bash-$(BASH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
361         cd bash-$(BASH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
362
363 bash-$(BASH_VERSION).tar.gz:
364         rm -f $@ $@-t
365         wget -O $@-t ftp://ftp.gnu.org/gnu/bash/bash-$(BASH_VERSION).tar.gz
366         mv $@-t $@
367
368 # Cross-compile coreutils.  Bleah, coreutils cross-compilation is
369 # known-broken and upstream don't care, hence the 'touch' command.
370
371 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
372
373 stage3-chroot/bin/ls: coreutils-$(COREUTILS_VERSION).tar.xz
374         rm -rf coreutils-$(COREUTILS_VERSION)
375         tar Jxf $^
376         cd coreutils-$(COREUTILS_VERSION) && \
377         PATH=$(ROOT)/fixed-gcc:$$PATH \
378         ./configure --host=riscv64-unknown-linux-gnu \
379             --prefix=/usr --libdir=/usr/lib64
380         -cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
381         cd coreutils-$(COREUTILS_VERSION)/man && \
382         for f in $(COREUTILS_PROGRAMS); do touch $$f.1; done
383         cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
384         cd coreutils-$(COREUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
385
386 coreutils-$(COREUTILS_VERSION).tar.xz:
387         rm -f $@ $@-t
388         wget -O $@-t ftp://ftp.gnu.org/gnu/coreutils/coreutils-$(COREUTILS_VERSION).tar.xz
389         mv $@-t $@
390
391 # Cross-compile binutils.
392 stage3-chroot/usr/bin/as: binutils-$(BINUTILS_X_VERSION).tar.gz
393         rm -rf riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)
394         zcat $^ | tar xf -
395         mkdir riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build
396         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && \
397         PATH=$(ROOT)/fixed-gcc:$$PATH \
398         ../configure \
399             --host=riscv64-unknown-linux-gnu \
400             --prefix=/usr --libdir=/usr/lib64
401         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make
402         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && make DESTDIR=$(ROOT)/stage3-chroot install
403
404 binutils-$(BINUTILS_X_VERSION).tar.gz:
405         rm -f $@ $@-t
406         wget -O $@-t https://github.com/riscv/riscv-binutils-gdb/archive/riscv-binutils-$(BINUTILS_X_VERSION).tar.gz
407         mv $@-t $@
408
409 # Cross-compile GMP, MPFR and MPC (deps of GCC).
410 stage3-chroot/usr/lib64/libgmp.so.10: gmp-$(GMP_VERSION).tar.lz
411         rm -rf gmp-$(GMP_VERSION)
412         tar --lzip -xf gmp-$(GMP_VERSION).tar.lz
413         cd gmp-$(GMP_VERSION) && \
414         PATH=$(ROOT)/fixed-gcc:$$PATH \
415         ./configure --host=riscv64-unknown-linux-gnu \
416             --prefix=/usr --libdir=/usr/lib64
417         cd gmp-$(GMP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
418         cd gmp-$(GMP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
419         cd stage3-chroot/usr/lib && ln -s ../lib64/libgmp.so
420
421 gmp-$(GMP_VERSION).tar.lz:
422         rm -f $@ $@-t
423         wget -O $@-t https://gmplib.org/download/gmp/gmp-$(GMP_VERSION).tar.lz
424         mv $@-t $@
425
426 stage3-chroot/usr/lib64/libmpfr.so.4: mpfr-$(MPFR_VERSION).tar.gz
427         rm -rf mpfr-$(MPFR_VERSION)
428         tar -zxf mpfr-$(MPFR_VERSION).tar.gz
429         cd mpfr-$(MPFR_VERSION) && \
430         PATH=$(ROOT)/fixed-gcc:$$PATH \
431         ./configure --host=riscv64-unknown-linux-gnu \
432             --prefix=/usr --libdir=/usr/lib64 \
433             --with-gmp=$(ROOT)/stage3-chroot/usr
434         cd mpfr-$(MPFR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
435         cd mpfr-$(MPFR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
436         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpfr.so
437         rm -f stage3-chroot/usr/lib64/*.la
438
439 mpfr-$(MPFR_VERSION).tar.gz:
440         rm -f $@ $@-t
441         wget -O $@-t http://www.mpfr.org/mpfr-current/mpfr-$(MPFR_VERSION).tar.gz
442         mv $@-t $@
443
444 stage3-chroot/usr/lib64/libmpc.so.3: mpc-$(MPC_VERSION).tar.gz
445         rm -rf mpc-$(MPC_VERSION)
446         tar -zxf mpc-$(MPC_VERSION).tar.gz
447         cd mpc-$(MPC_VERSION) && \
448         PATH=$(ROOT)/fixed-gcc:$$PATH \
449         ./configure --host=riscv64-unknown-linux-gnu \
450             --prefix=/usr --libdir=/usr/lib64 \
451             --with-gmp=$(ROOT)/stage3-chroot/usr \
452             --with-mpfr=$(ROOT)/stage3-chroot/usr
453         cd mpc-$(MPC_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
454         cd mpc-$(MPC_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
455         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpc.so
456         rm -f stage3-chroot/usr/lib64/*.la
457
458 mpc-$(MPC_VERSION).tar.gz:
459         rm -f $@ $@-t
460         wget -O $@-t ftp://ftp.gnu.org/gnu/mpc/mpc-$(MPC_VERSION).tar.gz
461         mv $@-t $@
462
463 # Cross-compile GCC.
464 stage3-chroot/usr/bin/gcc: gcc-$(GCC_X_VERSION).tar.gz
465         rm -rf riscv-gcc-riscv-gcc-$(GCC_X_VERSION)
466         zcat $^ | tar xf -
467         mkdir riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build
468         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && \
469         PATH=$(ROOT)/fixed-gcc:$$PATH \
470         ../configure \
471             --host=riscv64-unknown-linux-gnu \
472             --prefix=/usr --libdir=/usr/lib64 \
473             --enable-shared \
474             --enable-tls \
475             --enable-languages=c,c++ \
476             --disable-libmudflap \
477             --disable-libssp \
478             --disable-libquadmath \
479             --disable-nls \
480             --disable-multilib
481 # XXX use make all & make install here.  However building libgcc
482 # fails, see 'broken-gcc.log'.
483         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make all-gcc
484         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && make install-gcc DESTDIR=$(ROOT)/stage3-chroot
485
486 gcc-$(GCC_X_VERSION).tar.gz:
487         rm -f $@ $@-t
488         wget -O $@-t https://github.com/riscv/riscv-gcc/archive/riscv-gcc-$(GCC_X_VERSION).tar.gz
489         mv $@-t $@
490
491 # Cross-compile util-linux.
492 # XXX Be nice to fix ncurses/tinfo support which in theory should work.
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         ./configure \
499             --host=riscv64-unknown-linux-gnu \
500             --prefix=/usr --libdir=/usr/lib64 \
501             --without-python \
502             --without-tinfo \
503             --without-ncurses \
504             --without-systemd \
505             --disable-makeinstall-chown \
506             --enable-static-programs=mount
507         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
508         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
509         rm -f stage3-chroot/usr/lib64/*.la
510
511 util-linux-$(UTIL_LINUX_VERSION).tar.xz:
512         rm -f $@ $@-t
513         wget -O $@-t ftp://ftp.kernel.org/pub/linux/utils/util-linux/v$(UTIL_LINUX_VERSION)/util-linux-$(UTIL_LINUX_VERSION).tar.xz
514         mv $@-t $@
515
516 # Cross-compile GNU tar.
517 stage3-chroot/usr/bin/tar: tar-$(TAR_VERSION).tar.xz
518         rm -rf tar-$(TAR_VERSION)
519         tar -Jxf $^
520         cd tar-$(TAR_VERSION) && \
521         PATH=$(ROOT)/fixed-gcc:$$PATH \
522         ./configure \
523             --host=riscv64-unknown-linux-gnu \
524             --prefix=/usr --libdir=/usr/lib64
525         cd tar-$(TAR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
526         cd tar-$(TAR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
527
528 tar-$(TAR_VERSION).tar.xz:
529         rm -f $@ $@-t
530         wget -O $@-t https://ftp.gnu.org/gnu/tar/tar-$(TAR_VERSION).tar.xz
531         mv $@-t $@
532
533 # Cross-compile GNU gzip.
534 stage3-chroot/usr/bin/gzip: gzip-$(GZIP_VERSION).tar.gz
535         rm -rf gzip-$(GZIP_VERSION)
536         tar -zxf $^
537         cd gzip-$(GZIP_VERSION) && \
538         PATH=$(ROOT)/fixed-gcc:$$PATH \
539         ./configure \
540             --host=riscv64-unknown-linux-gnu \
541             --prefix=/usr --libdir=/usr/lib64
542         cd gzip-$(GZIP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
543         cd gzip-$(GZIP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
544
545 gzip-$(GZIP_VERSION).tar.gz:
546         rm -f $@ $@-t
547         wget -O $@-t https://ftp.gnu.org/gnu/gzip/gzip-$(GZIP_VERSION).tar.gz
548         mv $@-t $@
549
550 # Cross-compile zlib.
551 stage3-chroot/usr/lib64/libz.so: zlib-$(ZLIB_VERSION).tar.gz
552         rm -rf zlib-$(ZLIB_VERSION)
553         tar -zxf $^
554         cd zlib-$(ZLIB_VERSION) && \
555         PATH=$(ROOT)/fixed-gcc:$$PATH \
556         CC=riscv64-unknown-linux-gnu-gcc \
557         CFLAGS="-I$(ROOT)/stage3-chroot/usr/include -L$(ROOT)/stage3-chroot/usr/lib" \
558         ./configure \
559             --prefix=/usr --libdir=/usr/lib64
560         cd zlib-$(ZLIB_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make shared
561         cd zlib-$(ZLIB_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
562
563 zlib-$(ZLIB_VERSION).tar.gz:
564         rm -f $@ $@-t
565         wget -O $@-t http://zlib.net/zlib-$(ZLIB_VERSION).tar.gz
566         mv $@-t $@
567
568 # Cross-compile file/libmagic.
569 stage3-chroot/usr/bin/file: file-$(FILE_VERSION).tar.gz
570         rm -rf file-$(FILE_VERSION)
571         tar -zxf $^
572         cd file-$(FILE_VERSION) && \
573         PATH=$(ROOT)/fixed-gcc:$$PATH \
574         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
575         ./configure \
576             --host=riscv64-unknown-linux-gnu \
577             --prefix=/usr --libdir=/usr/lib64 \
578             --disable-static --enable-shared
579         cd file-$(FILE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
580         cd file-$(FILE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
581         rm -f stage3-chroot/usr/lib64/*.la
582
583 file-$(FILE_VERSION).tar.gz:
584         rm -f $@ $@-t
585         wget -O $@-t ftp://ftp.astron.com/pub/file/file-$(FILE_VERSION).tar.gz
586         mv $@-t $@
587
588 # Cross-compile popt.
589 stage3-chroot/usr/lib64/libpopt.so: popt-$(POPT_VERSION).tar.gz
590         rm -rf popt-$(POPT_VERSION)
591         tar -zxf $^
592         cd popt-$(POPT_VERSION) && \
593         PATH=$(ROOT)/fixed-gcc:$$PATH \
594         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
595         ./configure \
596             --host=riscv64-unknown-linux-gnu \
597             --prefix=/usr --libdir=/usr/lib64 \
598             --disable-static --enable-shared
599         cd popt-$(POPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
600         cd popt-$(POPT_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
601         rm -f stage3-chroot/usr/lib64/*.la
602
603 popt-$(POPT_VERSION).tar.gz:
604         rm -f $@ $@-t
605         wget -O $@-t http://rpm5.org/files/popt/popt-$(POPT_VERSION).tar.gz
606         mv $@-t $@
607
608 # Cross-compile beecrypt.
609 stage3-chroot/usr/lib64/libbeecrypt.so: beecrypt-$(BEECRYPT_VERSION).tar.gz
610         rm -rf beecrypt-$(BEECRYPT_VERSION)
611         tar -zxf $^
612         cd beecrypt-$(BEECRYPT_VERSION) && patch -p0 < ../beecrypt-disable-cplusplus.patch
613         cd beecrypt-$(BEECRYPT_VERSION) && autoreconf -i
614         cd beecrypt-$(BEECRYPT_VERSION) && \
615         PATH=$(ROOT)/fixed-gcc:$$PATH \
616         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
617         ./configure \
618             --host=riscv64-unknown-linux-gnu \
619             --prefix=/usr --libdir=/usr/lib64 \
620             --without-cplusplus \
621             --without-java \
622             --disable-openmp \
623             --disable-static \
624             --enable-shared
625         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
626         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot V=1
627         chrpath -d stage3-chroot/usr/lib64/libbeecrypt.so.*
628         rm -f stage3-chroot/usr/lib64/*.la
629
630 beecrypt-$(BEECRYPT_VERSION).tar.gz:
631         rm -f $@ $@-t
632         wget -O $@-t http://downloads.sourceforge.net/sourceforge/beecrypt/beecrypt-$(BEECRYPT_VERSION).tar.gz
633         mv $@-t $@
634
635 # Cross-compile RPM / rpmbuild.
636 # We build this from a git commit, with a few hacks to the configure
637 # script.
638 stage3-chroot/usr/bin/rpm: rpm-$(RPM_SHORT_COMMIT).tar.gz db-$(BDB_VERSION).tar.gz
639         rm -rf rpm-$(RPM_SHORT_COMMIT)
640         tar -zxf rpm-$(RPM_SHORT_COMMIT).tar.gz
641         tar -zxf db-$(BDB_VERSION).tar.gz -C rpm-$(RPM_SHORT_COMMIT)
642         cd rpm-$(RPM_SHORT_COMMIT) && ln -s db-$(BDB_VERSION) db
643         cd rpm-$(RPM_SHORT_COMMIT) && patch -p1 < ../rpm-hacks.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: