Further fixes to RPM.
[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 in the
38 # stage 3 chroot.  This must include all the cross-compiled packages
39 # below, and may also include any noarch package we like.
40 STAGE3_PACKAGES = iso-codes \
41 ncurses-devel readline-devel bash coreutils gmp-devel \
42 mpfr-devel mpc-devel binutils gcc gcc-c++ util-linux tar \
43 gzip zlib-devel file-devel popt-devel beecrypt-devel \
44 rpm rpm-build rpm-devel libdb-utils libdb-devel nano \
45 grep less strace bzip2-devel make diffutils findutils \
46 sed patch hostname gettext-devel lua-devel
47
48 # Versions of cross-compiled packages.
49 NCURSES_VERSION    = 6.0-20160730
50 READLINE_VERSION   = 6.3
51 BASH_VERSION       = 4.3
52 COREUTILS_VERSION  = 8.25
53 GMP_VERSION        = 6.1.1
54 MPFR_VERSION       = 3.1.4
55 MPC_VERSION        = 1.0.3
56 BINUTILS_X_VERSION = 2.26
57 GCC_X_VERSION      = 6.1.0
58 UTIL_LINUX_VERSION = 2.28
59 TAR_VERSION        = 1.29
60 GZIP_VERSION       = 1.8
61 ZLIB_VERSION       = 1.2.8
62 # Needs to match the version of 'file' installed (on host), otherwise:
63 #   "Cannot use the installed version of file (xx) to cross-compile file yy"
64 # Also note that 5.25 is definitely broken (segfaults in libmagic:magic_close).
65 FILE_VERSION       = 5.28
66 POPT_VERSION       = 1.16
67 BEECRYPT_VERSION   = 4.2.1
68 RPM_COMMIT         = 95712183458748ea6cafebac1bdd5daa097d9bee
69 RPM_SHORT_COMMIT   = 9571218
70 BDB_VERSION        = 4.5.20
71 NANO_VERSION       = 2.6.2
72 GREP_VERSION       = 2.25
73 LESS_VERSION       = 481
74 STRACE_COMMIT      = f320e1897832fd07a62e18ed288e75d8e79f4c5b
75 STRACE_SHORT_COMMIT = f320e189
76 BZIP2_VERSION      = 1.0.6
77 MAKE_VERSION       = 4.1
78 DIFFUTILS_VERSION  = 3.4
79 FINDUTILS_VERSION  = 4.6.0
80 SED_VERSION        = 4.2
81 PATCH_VERSION      = 2.7.5
82 HOSTNAME_VERSION   = 3.15
83 GETTEXT_VERSION    = 0.19
84 LUA_VERSION        = 5.3.3
85
86 all: stage1 stage2 stage3 stage4
87
88 # Stage 1
89
90 stage1: stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz \
91         stage1-riscv-qemu/riscv-qemu.spec \
92         stamp-riscv-qemu-installed \
93         stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz \
94         stage1-riscv-fesvr/riscv-fesvr.spec \
95         stamp-riscv-fesvr-installed \
96         stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz \
97         stage1-riscv-isa-sim/riscv-isa-sim.spec \
98         stamp-riscv-isa-sim-installed
99
100 stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz:
101         rm -f $@ $@-t
102         wget -O $@-t 'https://github.com/riscv/riscv-qemu/archive/$(RISCV_QEMU_COMMIT)/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz'
103         mv $@-t $@
104
105 stage1-riscv-qemu/riscv-qemu.spec: stage1-riscv-qemu/riscv-qemu.spec.in
106         sed -e 's/@COMMIT@/$(RISCV_QEMU_COMMIT)/g' \
107             -e 's/@SHORTCOMMIT@/$(RISCV_QEMU_SHORTCOMMIT)/g' \
108             < $^ > $@-t
109         mv $@-t $@
110
111 stamp-riscv-qemu-installed:
112         rm -f $@
113         @rpm -q riscv-qemu >/dev/null || { \
114           echo "ERROR: You must install riscv-qemu:"; \
115           echo; \
116           echo "       dnf copr enable rjones/riscv"; \
117           echo "       dnf install riscv-qemu"; \
118           echo; \
119           echo "OR: you can build it yourself from the stage1-riscv-qemu directory."; \
120           echo; \
121           exit 1; \
122         }
123         @qemu-system-riscv --version || { \
124           echo "ERROR: qemu-system-riscv is not working."; \
125           echo "Make sure you installed the riscv-qemu package."; \
126           exit 1; \
127         }
128         touch $@
129
130 stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz:
131         rm -f $@ $@-t
132         wget -O $@-t 'https://github.com/riscv/riscv-fesvr/archive/$(RISCV_FESVR_COMMIT)/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz'
133         mv $@-t $@
134
135 stage1-riscv-fesvr/riscv-fesvr.spec: stage1-riscv-fesvr/riscv-fesvr.spec.in
136         sed -e 's/@COMMIT@/$(RISCV_FESVR_COMMIT)/g' \
137             -e 's/@SHORTCOMMIT@/$(RISCV_FESVR_SHORTCOMMIT)/g' \
138             < $^ > $@-t
139         mv $@-t $@
140
141 stamp-riscv-fesvr-installed:
142         rm -f $@
143         @rpm -q riscv-fesvr >/dev/null || { \
144           echo "ERROR: You must install riscv-fesvr:"; \
145           echo; \
146           echo "       dnf copr enable rjones/riscv"; \
147           echo "       dnf install riscv-fesvr"; \
148           echo; \
149           echo "OR: you can build it yourself from the stage1-riscv-fesvr directory."; \
150           echo; \
151           exit 1; \
152         }
153         touch $@
154
155 stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz:
156         rm -f $@ $@-t
157         wget -O $@-t 'https://github.com/riscv/riscv-isa-sim/archive/$(RISCV_ISA_SIM_COMMIT)/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz'
158         mv $@-t $@
159
160 stage1-riscv-isa-sim/riscv-isa-sim.spec: stage1-riscv-isa-sim/riscv-isa-sim.spec.in
161         sed -e 's/@COMMIT@/$(RISCV_ISA_SIM_COMMIT)/g' \
162             -e 's/@SHORTCOMMIT@/$(RISCV_ISA_SIM_SHORTCOMMIT)/g' \
163             < $^ > $@-t
164         mv $@-t $@
165
166 stamp-riscv-isa-sim-installed:
167         rm -f $@
168         @rpm -q riscv-isa-sim >/dev/null || { \
169           echo "ERROR: You must install riscv-isa-sim:"; \
170           echo; \
171           echo "       dnf copr enable rjones/riscv"; \
172           echo "       dnf install riscv-isa-sim"; \
173           echo; \
174           echo "OR: you can build it yourself from the stage1-riscv-isa-sim directory."; \
175           echo; \
176           exit 1; \
177         }
178         touch $@
179
180 # Stage 2
181
182 stage2: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz \
183         stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz \
184         stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz \
185         stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz \
186         stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz \
187         stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec \
188         stamp-riscv-gnu-toolchain-installed \
189         stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz \
190         stage2-riscv-pk/riscv-pk.spec \
191         stamp-riscv-pk-installed
192
193 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz:
194         rm -f $@ $@-t
195         wget -O $@-t https://github.com/lowRISC/riscv-gnu-toolchain/archive/$(RISCV_GNU_TOOLCHAIN_COMMIT)/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz
196         mv $@-t $@
197
198 stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz:
199         rm -f $@ $@-t
200         wget -O $@-t http://mirrors.kernel.org/gnu/binutils/binutils-$(BINUTILS_VERSION).tar.gz
201         mv $@-t $@
202
203 # GCC 5 no longer compiles with GCC 6 unless we patch it.
204 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69959
205 stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz:
206         rm -f $@ $@-t
207         wget -O $@-t http://mirrors.kernel.org/gnu/gcc/gcc-$(GCC_VERSION)/gcc-$(GCC_VERSION).tar.gz
208         zcat $@-t | tar xf -
209         cd gcc-$(GCC_VERSION) && patch -p0 < ../stage2-riscv-gnu-toolchain/gcc-5-fix-compilation-with-gcc-6.patch
210         tar zcf $@-t gcc-$(GCC_VERSION)
211         rm -r gcc-$(GCC_VERSION)
212         mv $@-t $@
213
214 stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz:
215         rm -f $@ $@-t
216         wget -O $@-t http://mirrors.kernel.org/gnu/glibc/glibc-$(GLIBC_VERSION).tar.gz
217         mv $@-t $@
218
219 stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz:
220         rm -f $@ $@-t
221         wget -O $@-t ftp://sourceware.org/pub/newlib/newlib-$(NEWLIB_VERSION).tar.gz
222         mv $@-t $@
223
224 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec.in
225         sed -e 's/@COMMIT@/$(RISCV_GNU_TOOLCHAIN_COMMIT)/g' \
226             -e 's/@SHORTCOMMIT@/$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT)/g' \
227             -e 's/@BINUTILS_VERSION@/$(BINUTILS_VERSION)/g' \
228             -e 's/@GCC_VERSION@/$(GCC_VERSION)/g' \
229             -e 's/@GLIBC_VERSION@/$(GLIBC_VERSION)/g' \
230             -e 's/@NEWLIB_VERSION@/$(NEWLIB_VERSION)/g' \
231             < $^ > $@-t
232         mv $@-t $@
233
234 stamp-riscv-gnu-toolchain-installed:
235         rm -f $@
236         @rpm -q riscv-gnu-toolchain >/dev/null || { \
237           echo "ERROR: You must install riscv-gnu-toolchain:"; \
238           echo; \
239           echo "       dnf copr enable rjones/riscv"; \
240           echo "       dnf install riscv-gnu-toolchain"; \
241           echo; \
242           echo "OR: you can build it yourself from the stage2-riscv-gnu-toolchain directory."; \
243           echo; \
244           exit 1; \
245         }
246         @riscv64-unknown-elf-gcc --version || { \
247           echo "ERROR: riscv64-unknown-elf-gcc (cross compiler) is not working."; \
248           echo "Make sure you installed the riscv-gnu-toolchain package."; \
249           exit 1; \
250         }
251         touch $@
252
253 stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz:
254         rm -f $@ $@-t
255         wget -O $@-t https://github.com/lowRISC/riscv-pk/archive/$(RISCV_PK_COMMIT)/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz
256         mv $@-t $@
257
258 stage2-riscv-pk/riscv-pk.spec: stage2-riscv-pk/riscv-pk.spec.in
259         sed -e 's/@COMMIT@/$(RISCV_PK_COMMIT)/g' \
260             -e 's/@SHORTCOMMIT@/$(RISCV_PK_SHORTCOMMIT)/g' \
261             < $^ > $@-t
262         mv $@-t $@
263
264 stamp-riscv-pk-installed:
265         rm -f $@
266         @rpm -q riscv-pk >/dev/null || { \
267           echo "ERROR: You must install riscv-pk:"; \
268           echo; \
269           echo "       dnf copr enable rjones/riscv"; \
270           echo "       dnf install riscv-pk"; \
271           echo; \
272           echo "OR: you can build it yourself from the stage2-riscv-pk directory."; \
273           echo; \
274           exit 1; \
275         }
276         touch $@
277
278 # Stage 3
279
280 stage3: stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
281         stage3-chroot-original/etc/fedora-release \
282         stage3-chroot/etc/fedora-release \
283         stage3-chroot/lib64/libc.so.6 \
284         stage3-chroot/usr/bin/tic \
285         stage3-chroot/usr/lib64/libhistory.so.6 \
286         stage3-chroot/bin/bash \
287         stage3-chroot/bin/ls \
288         stage3-chroot/usr/lib64/libgmp.so.10 \
289         stage3-chroot/usr/lib64/libmpfr.so.4 \
290         stage3-chroot/usr/lib64/libmpc.so.3 \
291         stage3-chroot/usr/bin/as \
292         stage3-chroot/usr/bin/gcc \
293         stage3-chroot/usr/bin/mount \
294         stage3-chroot/usr/bin/tar \
295         stage3-chroot/usr/bin/gzip \
296         stage3-chroot/usr/lib64/libz.so \
297         stage3-chroot/usr/bin/file \
298         stage3-chroot/usr/lib64/libpopt.so \
299         stage3-chroot/usr/lib64/libbeecrypt.so \
300         stage3-chroot/usr/bin/nano \
301         stage3-chroot/usr/bin/grep \
302         stage3-chroot/usr/bin/less \
303         stage3-chroot/usr/bin/strace \
304         stage3-chroot/usr/bin/bzip2 \
305         stage3-chroot/usr/bin/make \
306         stage3-chroot/usr/bin/diff \
307         stage3-chroot/usr/bin/find \
308         stage3-chroot/usr/bin/sed \
309         stage3-chroot/usr/bin/patch \
310         stage3-chroot/usr/bin/hostname \
311         stage3-chroot/usr/bin/gettext \
312         stage3-chroot/usr/bin/lua \
313         stage3-chroot/usr/bin/rpm \
314         stage3-chroot/init \
315         stage3-disk.img
316
317 stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux:
318         rm -rf stage3-kernel/linux-$(KERNEL_VERSION)
319         cp -a $(LOCAL_LINUX_GIT_COPY) stage3-kernel/linux-$(KERNEL_VERSION) || { \
320           mkdir stage3-kernel/linux-$(KERNEL_VERSION) && \
321           cd stage3-kernel/linux-$(KERNEL_VERSION) && \
322           git init; \
323         }
324         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
325         git remote add riscv https://github.com/riscv/riscv-linux && \
326         git fetch riscv && \
327         git checkout -f linux-4.1.y-riscv && \
328         make mrproper && \
329         make ARCH=riscv defconfig
330         echo CONFIG_CMDLINE=\"root=/dev/htifblk0 init=/init\" >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
331         echo CONFIG_CROSS_COMPILE=riscv64-unknown-elf- >> stage3-kernel/linux-$(KERNEL_VERSION)/.config
332         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
333         make ARCH=riscv olddefconfig
334         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
335         make ARCH=riscv vmlinux
336         ls -l $@
337
338 # Build an original (x86-64) chroot using supermin.  We then aim to
339 # rebuild (using cross-compiled versions) every ELF binary in this
340 # chroot.
341 stage3-chroot-original/etc/fedora-release:
342         rm -rf stage3-chroot-original tmp-supermin.d
343         supermin --prepare $(STAGE3_PACKAGES) -o tmp-supermin.d
344         supermin --build -f chroot tmp-supermin.d -o stage3-chroot-original
345         rm -r tmp-supermin.d
346         @echo -n "Total files in chroot: "
347         @find stage3-chroot-original -type f | wc -l
348         @echo -n "ELF files to be rebuilt: "
349         @find stage3-chroot-original -type f | xargs file -N | grep -E '\bELF.*LSB\b' | wc -l
350
351 # Copy the original chroot to the final chroot, remove all the ELF
352 # files.
353 stage3-chroot/etc/fedora-release: stage3-chroot-original/etc/fedora-release
354         rm -rf stage3-chroot
355         cp -a stage3-chroot-original stage3-chroot
356         find stage3-chroot -type d -print0 | xargs -0 chmod u+w
357         find stage3-chroot -type f -print0 | xargs -0 chmod u+w
358         find stage3-chroot -type f -print0 | xargs -0 file -N | grep -E '\bELF.*LSB\b' | awk -F: '{print $$1}' | xargs rm -f
359         rm -f stage3-chroot/lib64/libc.so.6
360
361 # Copy in compiled glibc from the riscv-gnu-toolchain sysroot.  Only
362 # copy files and symlinks, leave the target directory structure
363 # intact.
364 stage3-chroot/lib64/libc.so.6:
365         mkdir -p stage3-chroot/usr/lib/audit
366         mkdir -p stage3-chroot/usr/lib/gconv
367         for f in `cd /usr/sysroot && find -type f -o -type l`; do \
368             cp -d /usr/sysroot/$$f stage3-chroot/$$f; \
369         done
370         cd stage3-chroot/lib64 && for f in ../lib/*; do ln -sf $$f; done
371
372 # Cross-compile ncurses.
373 stage3-chroot/usr/bin/tic: ncurses-$(NCURSES_VERSION).tgz
374         tar zxf $^
375         cd ncurses-$(NCURSES_VERSION) && \
376         PATH=$(ROOT)/fixed-gcc:$$PATH \
377         ./configure --host=riscv64-unknown-linux-gnu \
378             --prefix=/usr --libdir=/usr/lib64 \
379             --with-shared \
380             --with-termlib=tinfo \
381             --enable-widec
382         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
383         cd ncurses-$(NCURSES_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
384
385 ncurses-$(NCURSES_VERSION).tgz:
386         rm -f $@ $@-t
387         wget -O $@-t ftp://invisible-island.net/ncurses/current/ncurses-$(NCURSES_VERSION).tgz
388         mv $@-t $@
389
390 # Cross-compile readline.
391 stage3-chroot/usr/lib64/libhistory.so.6: readline-$(READLINE_VERSION).tar.gz
392         tar zxf $^
393         cd readline-$(READLINE_VERSION) && \
394         PATH=$(ROOT)/fixed-gcc:$$PATH \
395         bash_cv_wcwidth_broken=no \
396         ./configure --host=riscv64-unknown-linux-gnu \
397             --prefix=/usr --libdir=/usr/lib64
398         cd readline-$(READLINE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
399         cd readline-$(READLINE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
400         rm -f stage3-chroot/usr/lib64/*.la
401
402 readline-$(READLINE_VERSION).tar.gz:
403         rm -f $@ $@-t
404         wget -O $@-t ftp://ftp.gnu.org/gnu/readline/readline-$(READLINE_VERSION).tar.gz
405         mv $@-t $@
406
407 # Cross-compile bash.
408 stage3-chroot/bin/bash: bash-$(BASH_VERSION).tar.gz
409         tar zxf $^
410         cd bash-$(BASH_VERSION) && \
411         PATH=$(ROOT)/fixed-gcc:$$PATH \
412         ./configure --host=riscv64-unknown-linux-gnu \
413             --prefix=/usr --libdir=/usr/lib64
414         cd bash-$(BASH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
415         cd bash-$(BASH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
416
417 bash-$(BASH_VERSION).tar.gz:
418         rm -f $@ $@-t
419         wget -O $@-t ftp://ftp.gnu.org/gnu/bash/bash-$(BASH_VERSION).tar.gz
420         mv $@-t $@
421
422 # Cross-compile coreutils.  Bleah, coreutils cross-compilation is
423 # known-broken and upstream don't care, hence the 'touch' command.
424
425 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
426
427 stage3-chroot/bin/ls: coreutils-$(COREUTILS_VERSION).tar.xz
428         rm -rf coreutils-$(COREUTILS_VERSION)
429         tar Jxf $^
430         cd coreutils-$(COREUTILS_VERSION) && \
431         PATH=$(ROOT)/fixed-gcc:$$PATH \
432         ./configure --host=riscv64-unknown-linux-gnu \
433             --prefix=/usr --libdir=/usr/lib64
434         -cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
435         cd coreutils-$(COREUTILS_VERSION)/man && \
436         for f in $(COREUTILS_PROGRAMS); do touch $$f.1; done
437         cd coreutils-$(COREUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
438         cd coreutils-$(COREUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
439
440 coreutils-$(COREUTILS_VERSION).tar.xz:
441         rm -f $@ $@-t
442         wget -O $@-t ftp://ftp.gnu.org/gnu/coreutils/coreutils-$(COREUTILS_VERSION).tar.xz
443         mv $@-t $@
444
445 # Cross-compile binutils.
446 stage3-chroot/usr/bin/as: binutils-$(BINUTILS_X_VERSION).tar.gz
447         rm -rf riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)
448         zcat $^ | tar xf -
449         mkdir riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build
450         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && \
451         PATH=$(ROOT)/fixed-gcc:$$PATH \
452         ../configure \
453             --host=riscv64-unknown-linux-gnu \
454             --prefix=/usr --libdir=/usr/lib64
455         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make
456         cd riscv-binutils-gdb-riscv-binutils-$(BINUTILS_X_VERSION)/build && make DESTDIR=$(ROOT)/stage3-chroot install
457
458 binutils-$(BINUTILS_X_VERSION).tar.gz:
459         rm -f $@ $@-t
460         wget -O $@-t https://github.com/riscv/riscv-binutils-gdb/archive/riscv-binutils-$(BINUTILS_X_VERSION).tar.gz
461         mv $@-t $@
462
463 # Cross-compile GMP, MPFR and MPC (deps of GCC).
464 stage3-chroot/usr/lib64/libgmp.so.10: gmp-$(GMP_VERSION).tar.lz
465         rm -rf gmp-$(GMP_VERSION)
466         tar --lzip -xf gmp-$(GMP_VERSION).tar.lz
467         cd gmp-$(GMP_VERSION) && \
468         PATH=$(ROOT)/fixed-gcc:$$PATH \
469         ./configure --host=riscv64-unknown-linux-gnu \
470             --prefix=/usr --libdir=/usr/lib64
471         cd gmp-$(GMP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
472         cd gmp-$(GMP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
473         cd stage3-chroot/usr/lib && ln -s ../lib64/libgmp.so
474
475 gmp-$(GMP_VERSION).tar.lz:
476         rm -f $@ $@-t
477         wget -O $@-t https://gmplib.org/download/gmp/gmp-$(GMP_VERSION).tar.lz
478         mv $@-t $@
479
480 stage3-chroot/usr/lib64/libmpfr.so.4: mpfr-$(MPFR_VERSION).tar.gz
481         rm -rf mpfr-$(MPFR_VERSION)
482         tar -zxf mpfr-$(MPFR_VERSION).tar.gz
483         cd mpfr-$(MPFR_VERSION) && \
484         PATH=$(ROOT)/fixed-gcc:$$PATH \
485         ./configure --host=riscv64-unknown-linux-gnu \
486             --prefix=/usr --libdir=/usr/lib64 \
487             --with-gmp=$(ROOT)/stage3-chroot/usr
488         cd mpfr-$(MPFR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
489         cd mpfr-$(MPFR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
490         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpfr.so
491         rm -f stage3-chroot/usr/lib64/*.la
492
493 mpfr-$(MPFR_VERSION).tar.gz:
494         rm -f $@ $@-t
495         wget -O $@-t http://www.mpfr.org/mpfr-current/mpfr-$(MPFR_VERSION).tar.gz
496         mv $@-t $@
497
498 stage3-chroot/usr/lib64/libmpc.so.3: mpc-$(MPC_VERSION).tar.gz
499         rm -rf mpc-$(MPC_VERSION)
500         tar -zxf mpc-$(MPC_VERSION).tar.gz
501         cd mpc-$(MPC_VERSION) && \
502         PATH=$(ROOT)/fixed-gcc:$$PATH \
503         ./configure --host=riscv64-unknown-linux-gnu \
504             --prefix=/usr --libdir=/usr/lib64 \
505             --with-gmp=$(ROOT)/stage3-chroot/usr \
506             --with-mpfr=$(ROOT)/stage3-chroot/usr
507         cd mpc-$(MPC_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
508         cd mpc-$(MPC_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
509         cd stage3-chroot/usr/lib && ln -s ../lib64/libmpc.so
510         rm -f stage3-chroot/usr/lib64/*.la
511
512 mpc-$(MPC_VERSION).tar.gz:
513         rm -f $@ $@-t
514         wget -O $@-t ftp://ftp.gnu.org/gnu/mpc/mpc-$(MPC_VERSION).tar.gz
515         mv $@-t $@
516
517 # Cross-compile GCC.
518 stage3-chroot/usr/bin/gcc: gcc-$(GCC_X_VERSION).tar.gz
519         rm -rf riscv-gcc-riscv-gcc-$(GCC_X_VERSION)
520         zcat $^ | tar xf -
521         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION) && \
522         patch -p1 < ../0001-HACKS-TO-GET-GCC-TO-COMPILE.patch
523         mkdir riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build
524         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && \
525         PATH=$(ROOT)/fixed-gcc:$$PATH \
526         ../configure \
527             --host=riscv64-unknown-linux-gnu \
528             --prefix=/usr --libdir=/usr/lib64 \
529             --enable-shared \
530             --enable-tls \
531             --enable-languages=c,c++ \
532             --disable-libmudflap \
533             --disable-libssp \
534             --disable-libquadmath \
535             --disable-nls \
536             --disable-multilib
537         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && PATH=$(ROOT)/fixed-gcc:$$PATH make
538         cd riscv-gcc-riscv-gcc-$(GCC_X_VERSION)/build && make install DESTDIR=$(ROOT)/stage3-chroot
539         rm -f stage3-chroot/usr/lib64/*.la
540
541 gcc-$(GCC_X_VERSION).tar.gz:
542         rm -f $@ $@-t
543         wget -O $@-t https://github.com/riscv/riscv-gcc/archive/riscv-gcc-$(GCC_X_VERSION).tar.gz
544         mv $@-t $@
545
546 # Cross-compile util-linux.
547 stage3-chroot/usr/bin/mount: util-linux-$(UTIL_LINUX_VERSION).tar.xz
548         rm -rf util-linux-$(UTIL_LINUX_VERSION)
549         tar -Jxf $^
550         cd util-linux-$(UTIL_LINUX_VERSION) && \
551         PATH=$(ROOT)/fixed-gcc:$$PATH \
552         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
553         ./configure \
554             --host=riscv64-unknown-linux-gnu \
555             --prefix=/usr --libdir=/usr/lib64 \
556             --without-python \
557             --without-systemd \
558             --disable-makeinstall-chown \
559             --enable-static-programs=mount
560         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
561         cd util-linux-$(UTIL_LINUX_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
562         rm -f stage3-chroot/usr/lib64/*.la
563
564 util-linux-$(UTIL_LINUX_VERSION).tar.xz:
565         rm -f $@ $@-t
566         wget -O $@-t ftp://ftp.kernel.org/pub/linux/utils/util-linux/v$(UTIL_LINUX_VERSION)/util-linux-$(UTIL_LINUX_VERSION).tar.xz
567         mv $@-t $@
568
569 # Cross-compile GNU tar.
570 stage3-chroot/usr/bin/tar: tar-$(TAR_VERSION).tar.xz
571         rm -rf tar-$(TAR_VERSION)
572         tar -Jxf $^
573         cd tar-$(TAR_VERSION) && \
574         PATH=$(ROOT)/fixed-gcc:$$PATH \
575         ./configure \
576             --host=riscv64-unknown-linux-gnu \
577             --prefix=/usr --libdir=/usr/lib64
578         cd tar-$(TAR_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
579         cd tar-$(TAR_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
580
581 tar-$(TAR_VERSION).tar.xz:
582         rm -f $@ $@-t
583         wget -O $@-t https://ftp.gnu.org/gnu/tar/tar-$(TAR_VERSION).tar.xz
584         mv $@-t $@
585
586 # Cross-compile GNU gzip.
587 stage3-chroot/usr/bin/gzip: gzip-$(GZIP_VERSION).tar.gz
588         rm -rf gzip-$(GZIP_VERSION)
589         tar -zxf $^
590         cd gzip-$(GZIP_VERSION) && \
591         PATH=$(ROOT)/fixed-gcc:$$PATH \
592         ./configure \
593             --host=riscv64-unknown-linux-gnu \
594             --prefix=/usr --libdir=/usr/lib64
595         cd gzip-$(GZIP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
596         cd gzip-$(GZIP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
597
598 gzip-$(GZIP_VERSION).tar.gz:
599         rm -f $@ $@-t
600         wget -O $@-t https://ftp.gnu.org/gnu/gzip/gzip-$(GZIP_VERSION).tar.gz
601         mv $@-t $@
602
603 # Cross-compile zlib.
604 stage3-chroot/usr/lib64/libz.so: zlib-$(ZLIB_VERSION).tar.gz
605         rm -rf zlib-$(ZLIB_VERSION)
606         tar -zxf $^
607         cd zlib-$(ZLIB_VERSION) && \
608         PATH=$(ROOT)/fixed-gcc:$$PATH \
609         CC=riscv64-unknown-linux-gnu-gcc \
610         CFLAGS="-I$(ROOT)/stage3-chroot/usr/include -L$(ROOT)/stage3-chroot/usr/lib" \
611         ./configure \
612             --prefix=/usr --libdir=/usr/lib64
613         cd zlib-$(ZLIB_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make shared
614         cd zlib-$(ZLIB_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
615
616 zlib-$(ZLIB_VERSION).tar.gz:
617         rm -f $@ $@-t
618         wget -O $@-t http://zlib.net/zlib-$(ZLIB_VERSION).tar.gz
619         mv $@-t $@
620
621 # Cross-compile file/libmagic.
622 stage3-chroot/usr/bin/file: file-$(FILE_VERSION).tar.gz
623         rm -rf file-$(FILE_VERSION)
624         tar -zxf $^
625         cd file-$(FILE_VERSION) && \
626         PATH=$(ROOT)/fixed-gcc:$$PATH \
627         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
628         ./configure \
629             --host=riscv64-unknown-linux-gnu \
630             --prefix=/usr --libdir=/usr/lib64 \
631             --disable-static --enable-shared
632         cd file-$(FILE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
633         cd file-$(FILE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
634         rm -f stage3-chroot/usr/lib64/*.la
635
636 file-$(FILE_VERSION).tar.gz:
637         rm -f $@ $@-t
638         wget -O $@-t ftp://ftp.astron.com/pub/file/file-$(FILE_VERSION).tar.gz
639         mv $@-t $@
640
641 # Cross-compile popt.
642 stage3-chroot/usr/lib64/libpopt.so: popt-$(POPT_VERSION).tar.gz
643         rm -rf popt-$(POPT_VERSION)
644         tar -zxf $^
645         cd popt-$(POPT_VERSION) && \
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-static --enable-shared
652         cd popt-$(POPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
653         cd popt-$(POPT_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
654         rm -f stage3-chroot/usr/lib64/*.la
655
656 popt-$(POPT_VERSION).tar.gz:
657         rm -f $@ $@-t
658         wget -O $@-t http://rpm5.org/files/popt/popt-$(POPT_VERSION).tar.gz
659         mv $@-t $@
660
661 # Cross-compile beecrypt.
662 stage3-chroot/usr/lib64/libbeecrypt.so: beecrypt-$(BEECRYPT_VERSION).tar.gz
663         rm -rf beecrypt-$(BEECRYPT_VERSION)
664         tar -zxf $^
665         cd beecrypt-$(BEECRYPT_VERSION) && patch -p0 < ../beecrypt-disable-cplusplus.patch
666         cd beecrypt-$(BEECRYPT_VERSION) && autoreconf -i
667         cd beecrypt-$(BEECRYPT_VERSION) && \
668         PATH=$(ROOT)/fixed-gcc:$$PATH \
669         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
670         ./configure \
671             --host=riscv64-unknown-linux-gnu \
672             --prefix=/usr --libdir=/usr/lib64 \
673             --without-cplusplus \
674             --without-java \
675             --disable-openmp \
676             --disable-static \
677             --enable-shared
678         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
679         cd beecrypt-$(BEECRYPT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot V=1
680         chrpath -d stage3-chroot/usr/lib64/libbeecrypt.so.*
681         rm -f stage3-chroot/usr/lib64/*.la
682
683 beecrypt-$(BEECRYPT_VERSION).tar.gz:
684         rm -f $@ $@-t
685         wget -O $@-t http://downloads.sourceforge.net/sourceforge/beecrypt/beecrypt-$(BEECRYPT_VERSION).tar.gz
686         mv $@-t $@
687
688 # Cross-compile GNU nano (editor).
689 stage3-chroot/usr/bin/nano: nano-$(NANO_VERSION).tar.gz
690         rm -rf nano-$(NANO_VERSION)
691         tar -zxf $^
692         cd nano-$(NANO_VERSION) && \
693         PATH=$(ROOT)/fixed-gcc:$$PATH \
694         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
695         ./configure \
696             --host=riscv64-unknown-linux-gnu \
697             --prefix=/usr --libdir=/usr/lib64
698         cd nano-$(NANO_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
699         cd nano-$(NANO_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
700
701 nano-$(NANO_VERSION).tar.gz:
702         rm -f $@ $@-t
703         wget -O $@-t https://www.nano-editor.org/dist/v2.6/nano-$(NANO_VERSION).tar.gz
704         mv $@-t $@
705
706 # Cross-compile GNU grep.
707 stage3-chroot/usr/bin/grep: grep-$(GREP_VERSION).tar.xz
708         rm -rf grep-$(GREP_VERSION)
709         tar -Jxf $^
710         cd grep-$(GREP_VERSION) && \
711         PATH=$(ROOT)/fixed-gcc:$$PATH \
712         ./configure \
713             --host=riscv64-unknown-linux-gnu \
714             --prefix=/usr --libdir=/usr/lib64
715         cd grep-$(GREP_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
716         cd grep-$(GREP_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
717
718 grep-$(GREP_VERSION).tar.xz:
719         rm -f $@ $@-t
720         wget -O $@-t https://ftp.gnu.org/gnu/grep/grep-$(GREP_VERSION).tar.xz
721         mv $@-t $@
722
723 # Cross-compile less.
724 stage3-chroot/usr/bin/less: less-$(LESS_VERSION).tar.gz
725         rm -rf less-$(LESS_VERSION)
726         tar -zxf $^
727         cd less-$(LESS_VERSION) && \
728         PATH=$(ROOT)/fixed-gcc:$$PATH \
729         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
730         ./configure \
731             --host=riscv64-unknown-linux-gnu \
732             --prefix=/usr --libdir=/usr/lib64
733         cd less-$(LESS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
734         cd less-$(LESS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
735
736 less-$(LESS_VERSION).tar.gz:
737         rm -f $@ $@-t
738         wget -O $@-t http://www.greenwoodsoftware.com/less/less-$(LESS_VERSION).tar.gz
739         mv $@-t $@
740
741 # Cross-compile strace.
742 # XXX This does not work.
743 stage3-chroot/usr/bin/strace: strace-$(STRACE_SHORT_COMMIT).tar.gz
744         rm -rf strace-$(STRACE_SHORT_COMMIT)
745         tar -zxf $^
746         cd riscv-strace-$(STRACE_COMMIT) && patch -p1 < ../0001-Update-riscv_regs-for-ptrace.h-from-Linux-4.1.x.patch
747         cd riscv-strace-$(STRACE_COMMIT) && \
748         PATH=$(ROOT)/fixed-gcc:$$PATH \
749         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
750         ./configure \
751             --host=riscv64-unknown-linux-gnu \
752             --prefix=/usr --libdir=/usr/lib64
753         cd riscv-strace-$(STRACE_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make
754         cd riscv-strace-$(STRACE_COMMIT) && make install DESTDIR=$(ROOT)/stage3-chroot
755
756 strace-$(STRACE_SHORT_COMMIT).tar.gz:
757         rm -f $@ $@-t
758         wget -O $@-t 'https://github.com/riscv/riscv-strace/archive/$(STRACE_COMMIT)/riscv-strace-$(STRACE_SHORTCOMMIT).tar.gz'
759         mv $@-t $@
760
761 # Cross-compile bzip2.
762 stage3-chroot/usr/bin/bzip2: bzip2-$(BZIP2_VERSION).tar.gz
763         rm -rf bzip2-$(BZIP2_VERSION)
764         tar -zxf $^
765         cd bzip2-$(BZIP2_VERSION) && \
766         PATH=$(ROOT)/fixed-gcc:$$PATH \
767         make libbz2.a bzip2 bzip2recover \
768         PREFIX=/usr \
769         CC=riscv64-unknown-linux-gnu-gcc \
770         AR=riscv64-unknown-linux-gnu-ar \
771         RANLIB=riscv64-unknown-linux-gnu-ranlib \
772         CFLAGS="-Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64 -fPIC"
773         cd bzip2-$(BZIP2_VERSION) && \
774         make install PREFIX=$(ROOT)/stage3-chroot/usr
775
776 bzip2-$(BZIP2_VERSION).tar.gz:
777         rm -f $@ $@-t
778         wget -O $@-t http://www.bzip.org/1.0.6/bzip2-$(BZIP2_VERSION).tar.gz
779         mv $@-t $@
780
781 # Cross-compile GNU make.
782 stage3-chroot/usr/bin/make: make-$(MAKE_VERSION).tar.gz
783         rm -rf make-$(MAKE_VERSION)
784         tar -zxf $^
785         cd make-$(MAKE_VERSION) && \
786         PATH=$(ROOT)/fixed-gcc:$$PATH \
787         ./configure \
788             --host=riscv64-unknown-linux-gnu \
789             --prefix=/usr --libdir=/usr/lib64
790         cd make-$(MAKE_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
791         cd make-$(MAKE_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
792
793 make-$(MAKE_VERSION).tar.gz:
794         rm -f $@ $@-t
795         wget -O $@-t https://ftp.gnu.org/gnu/make/make-$(MAKE_VERSION).tar.gz
796         mv $@-t $@
797
798 # Cross-compile GNU diffutils.
799 stage3-chroot/usr/bin/diff: diffutils-$(DIFFUTILS_VERSION).tar.xz
800         rm -rf diffutils-$(DIFFUTILS_VERSION)
801         tar -Jxf $^
802         cd diffutils-$(DIFFUTILS_VERSION) && \
803         PATH=$(ROOT)/fixed-gcc:$$PATH \
804         ./configure \
805             --host=riscv64-unknown-linux-gnu \
806             --prefix=/usr --libdir=/usr/lib64
807         cd diffutils-$(DIFFUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
808         cd diffutils-$(DIFFUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
809
810 diffutils-$(DIFFUTILS_VERSION).tar.xz:
811         rm -f $@ $@-t
812         wget -O $@-t https://ftp.gnu.org/gnu/diffutils/diffutils-$(DIFFUTILS_VERSION).tar.xz
813         mv $@-t $@
814
815 # Cross-compile GNU findutils.
816 stage3-chroot/usr/bin/find: findutils-$(FINDUTILS_VERSION).tar.gz
817         rm -rf findutils-$(FINDUTILS_VERSION)
818         tar -zxf $^
819         cd findutils-$(FINDUTILS_VERSION) && \
820         PATH=$(ROOT)/fixed-gcc:$$PATH \
821         ./configure \
822             --host=riscv64-unknown-linux-gnu \
823             --prefix=/usr --libdir=/usr/lib64
824         cd findutils-$(FINDUTILS_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
825         cd findutils-$(FINDUTILS_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
826
827 findutils-$(FINDUTILS_VERSION).tar.gz:
828         rm -f $@ $@-t
829         wget -O $@-t https://ftp.gnu.org/gnu/findutils/findutils-$(FINDUTILS_VERSION).tar.gz
830         mv $@-t $@
831
832 # Cross-compile GNU sed.
833 stage3-chroot/usr/bin/sed: sed-$(SED_VERSION).tar.gz
834         rm -rf sed-$(SED_VERSION)
835         tar -zxf $^
836         cd sed-$(SED_VERSION) && \
837         PATH=$(ROOT)/fixed-gcc:$$PATH \
838         ./configure \
839             --host=riscv64-unknown-linux-gnu \
840             --prefix=/usr --libdir=/usr/lib64
841         cd sed-$(SED_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
842         cd sed-$(SED_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
843
844 sed-$(SED_VERSION).tar.gz:
845         rm -f $@ $@-t
846         wget -O $@-t https://ftp.gnu.org/gnu/sed/sed-$(SED_VERSION).tar.gz
847         mv $@-t $@
848
849 # Cross-compile patch.
850 stage3-chroot/usr/bin/patch: patch-$(PATCH_VERSION).tar.gz
851         rm -rf patch-$(PATCH_VERSION)
852         tar -zxf $^
853         cd patch-$(PATCH_VERSION) && \
854         PATH=$(ROOT)/fixed-gcc:$$PATH \
855         ./configure \
856             --host=riscv64-unknown-linux-gnu \
857             --prefix=/usr --libdir=/usr/lib64
858         cd patch-$(PATCH_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
859         cd patch-$(PATCH_VERSION) && make install DESTDIR=$(ROOT)/stage3-chroot
860
861 patch-$(PATCH_VERSION).tar.gz:
862         rm -f $@ $@-t
863         wget -O $@-t https://ftp.gnu.org/gnu/patch/patch-$(PATCH_VERSION).tar.gz
864         mv $@-t $@
865
866 # Cross-compile hostname.
867 stage3-chroot/usr/bin/hostname: hostname-$(HOSTNAME_VERSION).tar.gz
868         rm -rf hostname-$(HOSTNAME_VERSION)
869         tar -zxf $^
870         cd hostname && patch -p1 < ../hostname-rh.patch
871         cd hostname && \
872         PATH=$(ROOT)/fixed-gcc:$$PATH \
873         make \
874         CC=riscv64-unknown-linux-gnu-gcc \
875         CFLAGS="-O2 -g"
876         cd hostname && make install BASEDIR=$(ROOT)/stage3-chroot
877
878 hostname-$(HOSTNAME_VERSION).tar.gz:
879         rm -f $@ $@-t
880         wget -O $@-t http://ftp.de.debian.org/debian/pool/main/h/hostname/hostname_$(HOSTNAME_VERSION).tar.gz
881         mv $@-t $@
882
883 # Cross-compile GNU gettext.
884 stage3-chroot/usr/bin/gettext: gettext-$(GETTEXT_VERSION).tar.gz
885         rm -rf gettext-$(GETTEXT_VERSION)
886         tar -zxf $^
887         cd gettext-$(GETTEXT_VERSION) && \
888         PATH=$(ROOT)/fixed-gcc:$$PATH \
889         ./configure \
890             --host=riscv64-unknown-linux-gnu \
891             --prefix=/usr --libdir=/usr/lib64
892         cd gettext-$(GETTEXT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make
893         cd gettext-$(GETTEXT_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
894         rm -f stage3-chroot/usr/lib64/*.la
895
896 gettext-$(GETTEXT_VERSION).tar.gz:
897         rm -f $@ $@-t
898         wget -O $@-t https://ftp.gnu.org/gnu/gettext/gettext-$(GETTEXT_VERSION).tar.gz
899         mv $@-t $@
900
901 # Cross-compile lua.
902 stage3-chroot/usr/bin/lua: lua-$(LUA_VERSION).tar.gz
903         rm -rf lua-$(LUA_VERSION)
904         tar -zxf $^
905         cd lua-$(LUA_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make PLAT=linux INSTALL_TOP=/usr CC=riscv64-unknown-linux-gnu-gcc AR="riscv64-unknown-linux-gnu-ar rcu" RANLIB="riscv64-unknown-linux-gnu-ranlib" MYLDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 MYLIBS=-ltinfo MYCFLAGS="-fPIC -DLUA_COMPAT_5_1"
906         cd lua-$(LUA_VERSION) && PATH=$(ROOT)/fixed-gcc:$$PATH make install INSTALL_TOP=$(ROOT)/stage3-chroot/usr INSTALL_LIB=$(ROOT)/stage3-chroot/usr/lib64
907
908 lua-$(LUA_VERSION).tar.gz:
909         rm -f $@ $@-t
910         wget -O $@-t https://www.lua.org/ftp/lua-$(LUA_VERSION).tar.gz
911         mv $@-t $@
912
913 # Cross-compile RPM / rpmbuild.
914 # We build this from a git commit, with a few hacks to the configure
915 # script.
916 stage3-chroot/usr/bin/rpm: rpm-$(RPM_SHORT_COMMIT).tar.gz db-$(BDB_VERSION).tar.gz
917         rm -rf rpm-$(RPM_SHORT_COMMIT)
918         tar -zxf rpm-$(RPM_SHORT_COMMIT).tar.gz
919         tar -zxf db-$(BDB_VERSION).tar.gz -C rpm-$(RPM_SHORT_COMMIT)
920         cd rpm-$(RPM_SHORT_COMMIT) && ln -s db-$(BDB_VERSION) db
921         cd rpm-$(RPM_SHORT_COMMIT) && \
922         patch -p1 < ../0001-RISCV-64-bit-riscv64-support.patch && \
923         patch -p1 < ../0002-rpmrc-Convert-uname.machine-riscv-to-riscv32-riscv64.patch && \
924         patch -p1 < ../0003-build-fgetc-returns-int-not-char.patch && \
925         patch -p1 < ../0004-HACKS-TO-GET-RPM-TO-CROSS-COMPILE.patch
926         cd rpm-$(RPM_SHORT_COMMIT) && autoreconf -i
927         cd rpm-$(RPM_SHORT_COMMIT) && \
928         PATH=$(ROOT)/fixed-gcc:$$PATH \
929         LDFLAGS=-L$(ROOT)/stage3-chroot/usr/lib64 \
930         ./configure \
931             --host=riscv64-unknown-linux-gnu \
932             --prefix=/usr --libdir=/usr/lib64 \
933             --disable-rpath \
934             --with-vendor=redhat \
935             --without-libarchive \
936             --with-lua \
937             --with-beecrypt \
938             --without-archive \
939             --without-external-db \
940             --enable-ndb \
941             --disable-plugins
942         cd rpm-$(RPM_SHORT_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make V=1
943         cd rpm-$(RPM_SHORT_COMMIT) && PATH=$(ROOT)/fixed-gcc:$$PATH make install DESTDIR=$(ROOT)/stage3-chroot
944         rm -f stage3-chroot/usr/lib64/*.la
945
946 rpm-$(RPM_SHORT_COMMIT).tar.gz:
947         rm -f $@ $@-t
948         wget -O $@-t 'http://rpm.org/gitweb?p=rpm.git;a=snapshot;h=$(RPM_COMMIT);sf=tgz'
949         mv $@-t $@
950
951 db-$(BDB_VERSION).tar.gz:
952         rm -f $@ $@-t
953         wget -O $@-t http://download.oracle.com/berkeley-db/db-$(BDB_VERSION).tar.gz
954         mv $@-t $@
955
956 # Create an /init script.
957 stage3-chroot/init: init.sh
958         install -m 0755 $^ $@
959
960 # Create the stage3 disk image.
961 # Note `-s +...' adds spare space to the disk image.
962 stage3-disk.img: stage3-chroot
963         cd stage3-chroot && virt-make-fs . ../$@ -t ext2 -F raw -s +4G
964
965 # Upload the compressed disk image.
966 upload-stage3: stage3-disk.img.xz
967         scp $^ tick:public_html/riscv/
968 stage3-disk.img.xz: stage3-disk.img
969         rm -f $@
970         xz --best $^
971
972 # Helper which boots stage3 disk image in spike.
973 boot-stage3-in-spike: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
974         spike +disk=stage3-disk.img \
975             /usr/bin/bbl stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
976
977 # Helper which boots stage3 disk image in qemu.
978 boot-stage3-in-qemu: stage3-disk.img stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux
979         qemu-system-riscv -kernel /usr/bin/bbl \
980             -append ./stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
981             -drive file=stage3-disk.img,format=raw -nographic
982
983 # Stage 4
984
985 stage4:
986         echo "XXX TO DO"
987         exit 1
988
989 .NOTPARALLEL: