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