Add CONFIG_CMDLINE to kernel.
[fedora-riscv.git] / Makefile
1 # Refer to the README file to understand how Fedora on RISC-V is
2 # bootstrapped.
3
4 # Note these are chosen very specifically to ensure the different
5 # versions work together.  Don't blindly update to the latest
6 # versions.  See also:
7 # https://github.com/riscv/riscv-pk/issues/18#issuecomment-206115996
8 RISCV_QEMU_COMMIT               = 94f5eb73091fb4fe272db3e943f173ecc0f78ffd
9 RISCV_QEMU_SHORTCOMMIT          = 94f5eb73
10 RISCV_FESVR_COMMIT              = 0f34d7ad311f78455a674224225f5b3056efba1d
11 RISCV_FESVR_SHORTCOMMIT         = 0f34d7ad
12 RISCV_ISA_SIM_COMMIT            = 3bfc00ef2a1b1f0b0472a39a866261b00f67027e
13 RISCV_ISA_SIM_SHORTCOMMIT       = 3bfc00ef
14 RISCV_GNU_TOOLCHAIN_COMMIT      = 728afcddcb0526a0f6560c4032da82805f054d58
15 RISCV_GNU_TOOLCHAIN_SHORTCOMMIT = 728afcdd
16 RISCV_PK_COMMIT                 = 85ae17aa149b9ea114bdd70cc30ea7e73813fb48
17 RISCV_PK_SHORTCOMMIT            = 85ae17aa
18
19 # For the correct versions, see
20 # riscv-gnu-toolchain/Makefile.in *_version variables
21 BINUTILS_VERSION = 2.25.1
22 GLIBC_VERSION    = 2.22
23 GCC_VERSION      = 5.3.0
24 NEWLIB_VERSION   = 2.2.0
25
26 # See linux-4.1.y-riscv branch of
27 # https://github.com/riscv/riscv-linux
28 KERNEL_VERSION   = 4.1.26
29
30 # A local copy of Linux git repo so you don't have to keep downloading
31 # git commits (optional).
32 LOCAL_LINUX_GIT_COPY = $(HOME)/d/linux
33
34 # The root packages (plus their dependencies) that we want to
35 # cross-compile into the stage 3 chroot.
36 STAGE3_PACKAGES = gcc rpm-build
37
38 # Versions of cross-compiled packages.
39 BASH_VERSION    = 4.3
40
41 all: stage1 stage2 stage3 stage4
42
43 # Stage 1
44
45 stage1: stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz \
46         stage1-riscv-qemu/riscv-qemu.spec \
47         stamp-riscv-qemu-installed \
48         stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz \
49         stage1-riscv-fesvr/riscv-fesvr.spec \
50         stamp-riscv-fesvr-installed \
51         stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz \
52         stage1-riscv-isa-sim/riscv-isa-sim.spec \
53         stamp-riscv-isa-sim-installed
54
55 stage1-riscv-qemu/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz:
56         rm -f $@ $@-t
57         wget -O $@-t 'https://github.com/riscv/riscv-qemu/archive/$(RISCV_QEMU_COMMIT)/riscv-qemu-$(RISCV_QEMU_SHORTCOMMIT).tar.gz'
58         mv $@-t $@
59
60 stage1-riscv-qemu/riscv-qemu.spec: stage1-riscv-qemu/riscv-qemu.spec.in
61         sed -e 's/@COMMIT@/$(RISCV_QEMU_COMMIT)/g' \
62             -e 's/@SHORTCOMMIT@/$(RISCV_QEMU_SHORTCOMMIT)/g' \
63             < $^ > $@-t
64         mv $@-t $@
65
66 stamp-riscv-qemu-installed:
67         rm -f $@
68         @rpm -q riscv-qemu >/dev/null || { \
69           echo "ERROR: You must install riscv-qemu:"; \
70           echo; \
71           echo "       dnf copr enable rjones/riscv"; \
72           echo "       dnf install riscv-qemu"; \
73           echo; \
74           echo "OR: you can build it yourself from the stage1-riscv-qemu directory."; \
75           echo; \
76           exit 1; \
77         }
78         @qemu-system-riscv --version || { \
79           echo "ERROR: qemu-system-riscv is not working."; \
80           echo "Make sure you installed the riscv-qemu package."; \
81           exit 1; \
82         }
83         touch $@
84
85 stage1-riscv-fesvr/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz:
86         rm -f $@ $@-t
87         wget -O $@-t 'https://github.com/riscv/riscv-fesvr/archive/$(RISCV_FESVR_COMMIT)/riscv-fesvr-$(RISCV_FESVR_SHORTCOMMIT).tar.gz'
88         mv $@-t $@
89
90 stage1-riscv-fesvr/riscv-fesvr.spec: stage1-riscv-fesvr/riscv-fesvr.spec.in
91         sed -e 's/@COMMIT@/$(RISCV_FESVR_COMMIT)/g' \
92             -e 's/@SHORTCOMMIT@/$(RISCV_FESVR_SHORTCOMMIT)/g' \
93             < $^ > $@-t
94         mv $@-t $@
95
96 stamp-riscv-fesvr-installed:
97         rm -f $@
98         @rpm -q riscv-fesvr >/dev/null || { \
99           echo "ERROR: You must install riscv-fesvr:"; \
100           echo; \
101           echo "       dnf copr enable rjones/riscv"; \
102           echo "       dnf install riscv-fesvr"; \
103           echo; \
104           echo "OR: you can build it yourself from the stage1-riscv-fesvr directory."; \
105           echo; \
106           exit 1; \
107         }
108         touch $@
109
110 stage1-riscv-isa-sim/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz:
111         rm -f $@ $@-t
112         wget -O $@-t 'https://github.com/riscv/riscv-isa-sim/archive/$(RISCV_ISA_SIM_COMMIT)/riscv-isa-sim-$(RISCV_ISA_SIM_SHORTCOMMIT).tar.gz'
113         mv $@-t $@
114
115 stage1-riscv-isa-sim/riscv-isa-sim.spec: stage1-riscv-isa-sim/riscv-isa-sim.spec.in
116         sed -e 's/@COMMIT@/$(RISCV_ISA_SIM_COMMIT)/g' \
117             -e 's/@SHORTCOMMIT@/$(RISCV_ISA_SIM_SHORTCOMMIT)/g' \
118             < $^ > $@-t
119         mv $@-t $@
120
121 stamp-riscv-isa-sim-installed:
122         rm -f $@
123         @rpm -q riscv-isa-sim >/dev/null || { \
124           echo "ERROR: You must install riscv-isa-sim:"; \
125           echo; \
126           echo "       dnf copr enable rjones/riscv"; \
127           echo "       dnf install riscv-isa-sim"; \
128           echo; \
129           echo "OR: you can build it yourself from the stage1-riscv-isa-sim directory."; \
130           echo; \
131           exit 1; \
132         }
133         touch $@
134
135 # Stage 2
136
137 stage2: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz \
138         stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz \
139         stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz \
140         stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz \
141         stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz \
142         stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec \
143         stamp-riscv-gnu-toolchain-installed \
144         stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz \
145         stage2-riscv-pk/riscv-pk.spec \
146         stamp-riscv-pk-installed
147
148 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz:
149         rm -f $@ $@-t
150         wget -O $@-t https://github.com/lowRISC/riscv-gnu-toolchain/archive/$(RISCV_GNU_TOOLCHAIN_COMMIT)/riscv-gnu-toolchain-$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT).tar.gz
151         mv $@-t $@
152
153 stage2-riscv-gnu-toolchain/binutils-$(BINUTILS_VERSION).tar.gz:
154         rm -f $@ $@-t
155         wget -O $@-t http://mirrors.kernel.org/gnu/binutils/binutils-$(BINUTILS_VERSION).tar.gz
156         mv $@-t $@
157
158 # GCC 5 no longer compiles with GCC 6 unless we patch it.
159 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69959
160 stage2-riscv-gnu-toolchain/gcc-$(GCC_VERSION).tar.gz:
161         rm -f $@ $@-t
162         wget -O $@-t http://mirrors.kernel.org/gnu/gcc/gcc-$(GCC_VERSION)/gcc-$(GCC_VERSION).tar.gz
163         zcat $@-t | tar xf -
164         cd gcc-$(GCC_VERSION) && patch -p0 < ../stage2-riscv-gnu-toolchain/gcc-5-fix-compilation-with-gcc-6.patch
165         tar zcf $@-t gcc-$(GCC_VERSION)
166         rm -r gcc-$(GCC_VERSION)
167         mv $@-t $@
168
169 stage2-riscv-gnu-toolchain/glibc-$(GLIBC_VERSION).tar.gz:
170         rm -f $@ $@-t
171         wget -O $@-t http://mirrors.kernel.org/gnu/glibc/glibc-$(GLIBC_VERSION).tar.gz
172         mv $@-t $@
173
174 stage2-riscv-gnu-toolchain/newlib-$(NEWLIB_VERSION).tar.gz:
175         rm -f $@ $@-t
176         wget -O $@-t ftp://sourceware.org/pub/newlib/newlib-$(NEWLIB_VERSION).tar.gz
177         mv $@-t $@
178
179 stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec: stage2-riscv-gnu-toolchain/riscv-gnu-toolchain.spec.in
180         sed -e 's/@COMMIT@/$(RISCV_GNU_TOOLCHAIN_COMMIT)/g' \
181             -e 's/@SHORTCOMMIT@/$(RISCV_GNU_TOOLCHAIN_SHORTCOMMIT)/g' \
182             -e 's/@BINUTILS_VERSION@/$(BINUTILS_VERSION)/g' \
183             -e 's/@GCC_VERSION@/$(GCC_VERSION)/g' \
184             -e 's/@GLIBC_VERSION@/$(GLIBC_VERSION)/g' \
185             -e 's/@NEWLIB_VERSION@/$(NEWLIB_VERSION)/g' \
186             < $^ > $@-t
187         mv $@-t $@
188
189 stamp-riscv-gnu-toolchain-installed:
190         rm -f $@
191         @rpm -q riscv-gnu-toolchain >/dev/null || { \
192           echo "ERROR: You must install riscv-gnu-toolchain:"; \
193           echo; \
194           echo "       dnf copr enable rjones/riscv"; \
195           echo "       dnf install riscv-gnu-toolchain"; \
196           echo; \
197           echo "OR: you can build it yourself from the stage2-riscv-gnu-toolchain directory."; \
198           echo; \
199           exit 1; \
200         }
201         @riscv64-unknown-elf-gcc --version || { \
202           echo "ERROR: riscv64-unknown-elf-gcc (cross compiler) is not working."; \
203           echo "Make sure you installed the riscv-gnu-toolchain package."; \
204           exit 1; \
205         }
206         touch $@
207
208 stage2-riscv-pk/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz:
209         rm -f $@ $@-t
210         wget -O $@-t https://github.com/lowRISC/riscv-pk/archive/$(RISCV_PK_COMMIT)/riscv-pk-$(RISCV_PK_SHORTCOMMIT).tar.gz
211         mv $@-t $@
212
213 stage2-riscv-pk/riscv-pk.spec: stage2-riscv-pk/riscv-pk.spec.in
214         sed -e 's/@COMMIT@/$(RISCV_PK_COMMIT)/g' \
215             -e 's/@SHORTCOMMIT@/$(RISCV_PK_SHORTCOMMIT)/g' \
216             < $^ > $@-t
217         mv $@-t $@
218
219 stamp-riscv-pk-installed:
220         rm -f $@
221         @rpm -q riscv-pk >/dev/null || { \
222           echo "ERROR: You must install riscv-pk:"; \
223           echo; \
224           echo "       dnf copr enable rjones/riscv"; \
225           echo "       dnf install riscv-pk"; \
226           echo; \
227           echo "OR: you can build it yourself from the stage2-riscv-pk directory."; \
228           echo; \
229           exit 1; \
230         }
231         touch $@
232
233 # Stage 3
234
235 stage3: stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux \
236         stage3-chroot-original/etc/fedora-release \
237         stage3-chroot/etc/fedora-release \
238         stage3-chroot/lib64/libc.so.6 \
239         stage3-chroot/bin/bash \
240         stage3-disk.img
241
242 stage3-kernel/linux-$(KERNEL_VERSION)/vmlinux:
243         rm -rf stage3-kernel/linux-$(KERNEL_VERSION)
244         cp -a $(LOCAL_LINUX_GIT_COPY) stage3-kernel/linux-$(KERNEL_VERSION) || { \
245           mkdir stage3-kernel/linux-$(KERNEL_VERSION) && \
246           cd stage3-kernel/linux-$(KERNEL_VERSION) && \
247           git init; \
248         }
249         cd stage3-kernel/linux-$(KERNEL_VERSION) && \
250         git remote add riscv https://github.com/riscv/riscv-linux && \
251         git fetch riscv && \
252         git checkout -f linux-4.1.y-riscv && \
253         make mrproper && \
254         make ARCH=riscv defconfig && \
255         make ARCH=riscv \
256             CONFIG_CROSS_COMPILE=riscv64-unknown-elf- \
257             CONFIG_CMDLINE="root=/dev/htifblk0 init=/bin/bash" \
258             vmlinux
259         ls -l $@
260
261 # Build an original (x86-64) chroot using supermin.  We then aim to
262 # rebuild (using cross-compiled versions) every ELF binary in this
263 # chroot.
264 stage3-chroot-original/etc/fedora-release:
265         rm -rf stage3-chroot-original tmp-supermin.d
266         supermin --prepare $(STAGE3_PACKAGES) -o tmp-supermin.d
267         supermin --build -f chroot tmp-supermin.d -o stage3-chroot-original
268         rm -r tmp-supermin.d
269         @echo -n "Total files in chroot: "
270         @find stage3-chroot-original -type f | wc -l
271         @echo -n "ELF files to be rebuilt: "
272         @find stage3-chroot-original -type f | xargs file -N | grep -E '\bELF.*LSB\b' | wc -l
273
274 # Copy the original chroot to the final chroot, remove all the ELF
275 # files.
276 stage3-chroot/etc/fedora-release: stage3-chroot-original/etc/fedora-release
277         rm -rf stage3-chroot
278         cp -a stage3-chroot-original stage3-chroot
279         find stage3-chroot -type d | xargs chmod u+w
280         find stage3-chroot -type f | xargs chmod u+w
281         find stage3-chroot -type f | xargs file -N | grep -E '\bELF.*LSB\b' | awk -F: '{print $$1}' | xargs rm -f
282         rm -f stage3-chroot/lib64/libc.so.6
283
284 # Copy in compiled glibc from the riscv-gnu-toolchain sysroot.  Only
285 # copy files and symlinks, leave the target directory structure
286 # intact.
287 stage3-chroot/lib64/libc.so.6:
288         mkdir -p stage3-chroot/usr/lib/audit
289         mkdir -p stage3-chroot/usr/lib/gconv
290         for f in `cd /usr/sysroot && find -type f -o -type l`; do \
291             cp -d /usr/sysroot/$$f stage3-chroot/$$f; \
292         done
293         cd stage3-chroot/lib64 && for f in ../lib/*; do ln -sf $$f; done
294
295 # Cross-compile bash.
296 stage3-chroot/bin/bash: bash-$(BASH_VERSION).tar.gz
297         tar zxf $^
298         cd bash-$(BASH_VERSION) && \
299         ./configure --host=riscv64-unknown-linux-gnu CFLAGS="--sysroot=/usr/sysroot" && \
300         make
301         cp bash-$(BASH_VERSION)/bash stage3-chroot/bin/
302
303 bash-$(BASH_VERSION).tar.gz:
304         rm -f $@ $@-t
305         wget -O $@-t ftp://ftp.gnu.org/gnu/bash/bash-$(BASH_VERSION).tar.gz
306         mv $@-t $@
307
308 # Create the stage3 disk image.
309 # Note `-s +...' adds spare space to the disk image.
310 stage3-disk.img: stage3-chroot
311         cd stage3-chroot && virt-make-fs . ../$@ -t ext2 -F raw -s +4G
312
313 # Stage 4
314
315 stage4:
316         echo "XXX TO DO"
317         exit 1
318
319 .NOTPARALLEL: