Build system overhauled to use Kbuild (XCC)
[akaros.git] / tools / compilers / gcc-glibc / Makefile
1 # Number of make jobs to spawn.  Define it in Makelocal
2 MAKE_JOBS := 
3
4 # Defines where to install the toolchain
5 -include Makelocal
6
7 ifndef RISCV_INSTDIR
8 ifndef X86_INSTDIR
9 busted: 
10         @echo "You need to setup either a RISCV_INSTDIR or X86_INSTDIR to point to where you want the tools installed. The best way to do this is to create a 'Makelocal' file in the current directory and put it in there."; false
11 riscv: busted
12 x86: busted
13 endif
14 endif 
15
16 # Check to make sure that we have gawk installed on our system and not some
17 # other version of awk
18 AWK := $(shell awk -W version 2>&1 | head -n 1 | awk '{print $$1}')
19 ifneq ($(AWK),GNU)
20 $(error You need to have 'gawk' installed on your system!)
21 endif
22
23 # Check to make sure that we have the GMP, MPFR, and MPC libraries installed
24 GMP_OK := $(shell echo "int main() {return 0;}" | gcc -x c - -lgmp -lmpfr -lmpc -o /dev/null > /dev/null && echo OK)
25 ifneq ($(GMP_OK),OK)
26 $(error You need to have the libraries GMP, MPFR, and MPC installed on your system!)
27 endif
28
29
30 BINUTILS_VERSION := 2.21.1
31 GCC_VERSION := 4.6.1
32 GLIBC_VERSION := 2.14.1
33
34 BUILDDIR ?= $(shell pwd)/
35 ROSDIR ?= $(BUILDDIR)/../../../
36 DESTDIR ?= /
37
38 # Configuration parameters for make-riscv
39 RISCV_ARCH := riscv
40 RISCV_BINARY_PREFIX := $(RISCV_ARCH)-ros-
41 RISCV_GCC_BUILD_FLAGS := 
42 RISCV_CFLAGS :=
43
44 # Configuration parameters for x86
45 X86_ARCH := i686
46 X86_BINARY_PREFIX := $(X86_ARCH)-ros-
47 X86_GCC_BUILD_FLAGS := 
48 X86_CFLAGS := -march=i486
49
50 all:
51         @echo "You need to run one of the following commands, "
52         @echo "depending on which version of the cross compiler you are trying to generate:"
53         @echo "  make riscv"
54         @echo "  make x86"
55         @echo "  make riscv-revert"
56         @echo "  make x86-revert"
57         @echo "  make riscv-uninstall"
58         @echo "  make x86-uninstall"
59         @echo "  make clean"
60
61 riscv:
62         export LD_LIBRARY_PATH=; \
63         export DESTDIR=$(DESTDIR);\
64         export INSTDIR=$(RISCV_INSTDIR);\
65         export PATH=$(DESTDIR)$(RISCV_INSTDIR)/bin:$(PATH);\
66         export ARCH=$(RISCV_ARCH);\
67         export ARCH_CFLAGS=$(RISCV_CFLAGS);\
68         export ROS_ARCH_DIR=$(RISCV_ARCH);\
69         export BINARY_PREFIX=$(RISCV_BINARY_PREFIX);\
70         export GCC_BUILD_FLAGS=$(RISCV_GCC_BUILD_FLAGS);\
71         $(MAKE) make-all
72
73 x86:
74         export LD_LIBRARY_PATH=; \
75         export DESTDIR=$(DESTDIR);\
76         export INSTDIR=$(X86_INSTDIR);\
77         export PATH=$(DESTDIR)$(X86_INSTDIR)/bin:$(PATH);\
78         export ARCH=$(X86_ARCH);\
79         export ARCH_CFLAGS=$(X86_CFLAGS);\
80         export ROS_ARCH_DIR=$(X86_ARCH);\
81         export BINARY_PREFIX=$(X86_BINARY_PREFIX);\
82         export GCC_BUILD_FLAGS=$(X86_GCC_BUILD_FLAGS);\
83         $(MAKE) make-all
84
85 make-all: 
86         $(MAKE) .$(BINARY_PREFIX)binutils-make
87         $(MAKE) .$(BINARY_PREFIX)binutils-install
88         $(MAKE) .$(BINARY_PREFIX)ros-headers-install
89         $(MAKE) .$(BINARY_PREFIX)gcc-stage1-make
90         $(MAKE) .$(BINARY_PREFIX)gcc-stage1-install
91         $(MAKE) .$(BINARY_PREFIX)ros-install-dummy-libs
92         $(MAKE) .$(BINARY_PREFIX)glibc-make
93         $(MAKE) .$(BINARY_PREFIX)glibc-install
94         $(MAKE) .$(BINARY_PREFIX)ros-install-libs
95         $(MAKE) .$(BINARY_PREFIX)gcc-stage2-make
96         $(MAKE) .$(BINARY_PREFIX)gcc-stage2-install
97         mkdir -p $(ROSDIR)/fs/$(ARCH)/lib
98         cp -r $(INSTDIR)/$(ARCH)-ros/lib/* $(ROSDIR)/fs/$(ARCH)/lib
99
100 riscv-uninstall:
101         -rm -rf $(RISCV_INSTDIR)
102
103 x86-uninstall:
104         -rm -rf $(X86_INSTDIR)
105
106 riscv-revert:
107         rm -rf $(RISCV_INSTDIR)
108         cp -r $(RISCV_BINARY_PREFIX)install.bak $(RISCV_INSTDIR)
109
110 x86-revert: 
111         rm -rf $(X86_INSTDIR)
112         cp -r $(X86_BINARY_PREFIX)install.bak $(X86_INSTDIR)
113
114 clean:
115         rm -rf binutils-$(BINUTILS_VERSION) 
116         rm -rf gcc-$(GCC_VERSION) 
117         rm -rf glibc-$(GLIBC_VERSION) 
118         rm -rf *-builddir
119         rm -rf .*-ros-*-configure
120         rm -rf *$(BINARY_PREFIX)install.bak
121
122 binutils-clean:
123         cd $(BINARY_PREFIX)binutils-builddir; \
124         make clean
125
126 gcc-clean:
127         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
128         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
129         make clean
130
131 glibc-clean:
132         cd $(BINARY_PREFIX)glibc-builddir; \
133         make clean
134
135 %.tar.bz2:
136         wget http://akaros.cs.berkeley.edu/xcc/$@
137
138 binutils-$(BINUTILS_VERSION): binutils-$(BINUTILS_VERSION).tar.bz2
139         rm -rf binutils-$(BINUTILS_VERSION);
140         tar -jxf binutils-$(BINUTILS_VERSION).tar.bz2
141         patch -p0 < binutils-$(BINUTILS_VERSION)-ros.patch
142         patch --no-backup-if-mismatch -p0 < binutils-$(BINUTILS_VERSION)-riscv.patch
143
144 gcc-$(GCC_VERSION): gcc-$(GCC_VERSION).tar.bz2
145         rm -rf gcc-$(GCC_VERSION)
146         tar -jxf gcc-$(GCC_VERSION).tar.bz2
147         $(MAKE) .gcc-ros-patch
148         patch --no-backup-if-mismatch -p0 < gcc-$(GCC_VERSION)-riscv.patch
149
150 .gcc-ros-patch:
151         -cd gcc-$(GCC_VERSION); \
152         diff -ruB --unidirectional-new-file \
153              ../gcc-$(GCC_VERSION) \
154              ../gcc-$(GCC_VERSION)-ros \
155              | grep -v "Only in ../gcc-$(GCC_VERSION)" \
156              | patch -p2 
157
158 glibc-$(GLIBC_VERSION): glibc-$(GLIBC_VERSION).tar.bz2
159         rm -rf glibc-$(GLIBC_VERSION)
160         tar -jxf glibc-$(GLIBC_VERSION).tar.bz2
161         $(MAKE) .glibc-ros-patch
162         patch --no-backup-if-mismatch -p0 < glibc-$(GLIBC_VERSION)-riscv.patch
163         rm glibc-$(GLIBC_VERSION)/sysdeps/sparc/sparc32/elf/start.S
164         rm glibc-$(GLIBC_VERSION)/sysdeps/riscv/elf/start.S
165         rm glibc-$(GLIBC_VERSION)/sysdeps/riscv/init-first.c
166         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/elf/start.S
167         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/____longjmp_chk.S
168         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/init-first.c
169         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/sysdep.h
170
171 .glibc-ros-patch:
172         -cd glibc-$(GLIBC_VERSION); \
173         diff -ruB --unidirectional-new-file \
174              ../glibc-$(GLIBC_VERSION) \
175              ../glibc-$(GLIBC_VERSION)-ros \
176              | grep -v "Only in ../glibc-$(GLIBC_VERSION)" \
177              | patch -p2 
178
179 $(BINARY_PREFIX)binutils-builddir: binutils-$(BINUTILS_VERSION)
180         rm -rf $@
181         mkdir -p $@
182
183 $(BINARY_PREFIX)gcc-stage1-builddir: gcc-$(GCC_VERSION)
184         rm -rf $@
185         mkdir -p $@
186         
187 $(BINARY_PREFIX)glibc-builddir: glibc-$(GLIBC_VERSION)
188         rm -rf $@
189         mkdir -p $@
190
191 $(BINARY_PREFIX)gcc-stage2-builddir: gcc-$(GCC_VERSION)
192         rm -rf $@
193         mkdir -p $@
194         
195 .$(BINARY_PREFIX)binutils-configure: 
196         $(MAKE) $(BINARY_PREFIX)binutils-builddir 
197         cd $(BINARY_PREFIX)binutils-builddir; \
198         ../binutils-$(BINUTILS_VERSION)/configure \
199                     --enable-shared \
200                     --disable-multilib \
201                     --disable-nls \
202                     --target=$(ARCH)-ros \
203                     --prefix=$(INSTDIR) \
204                     --includedir=$(INSTDIR)/include \
205                     --disable-werror;
206         echo "MAKEINFO = :" >> $(BINARY_PREFIX)binutils-builddir/Makefile;
207         touch $@
208
209 .$(BINARY_PREFIX)binutils-make: .$(BINARY_PREFIX)binutils-configure
210         cd $(BINARY_PREFIX)binutils-builddir; \
211         make -j $(MAKE_JOBS)
212
213 .$(BINARY_PREFIX)binutils-install:
214         cd $(BINARY_PREFIX)binutils-builddir; \
215         make -j $(MAKE_JOBS) install
216
217 .$(BINARY_PREFIX)ros-headers-install: $(ROSDIR)
218         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/*
219         mkdir -p $(INSTDIR)/$(ARCH)-ros/sys-include
220         cp -r $(ROSDIR)/kern/include/ros \
221            $(INSTDIR)/$(ARCH)-ros/sys-include/
222         rm -f $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
223         mkdir $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
224         cp -r $(ROSDIR)/kern/arch/$(ROS_ARCH_DIR)/ros/* \
225            $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch/
226         cp -r $(ROSDIR)/user/parlib/include/* \
227            $(INSTDIR)/$(ARCH)-ros/sys-include/
228         cp -r $(ROSDIR)/user/pthread/*.h \
229            $(INSTDIR)/$(ARCH)-ros/sys-include/
230         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/arch
231         ln -s $(ARCH) $(INSTDIR)/$(ARCH)-ros/sys-include/arch 
232
233 .$(BINARY_PREFIX)gcc-stage1-configure: 
234         $(MAKE) $(BINARY_PREFIX)gcc-stage1-builddir
235         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
236         ../gcc-$(GCC_VERSION)/configure \
237                 --enable-languages=c \
238                 --disable-checking \
239                 --disable-werror \
240                 --disable-multilib \
241                 --disable-libmudflap \
242                 --disable-libssp \
243                 --disable-libquadmath \
244                 --disable-libgomp \
245                 --disable-nls \
246                 --disable-libgcj \
247                 --disable-bootstrap \
248                         --disable-build-with-cxx \
249                         --disable-build-poststage1-with-cxx \
250                 --disable-shared \
251                 --program-prefix=$(BINARY_PREFIX) \
252                 --target=$(ARCH)-ros \
253                 --prefix=$(INSTDIR) \
254                 --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
255                 $(GCC_BUILD_FLAGS);
256         echo "MAKEINFO = :" >> $(BINARY_PREFIX)gcc-stage1-builddir/Makefile;
257         touch $@
258
259 .$(BINARY_PREFIX)gcc-stage1-make: .$(BINARY_PREFIX)gcc-stage1-configure
260         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
261         make -j $(MAKE_JOBS) inhibit_libc=true cross-compiling=yes all-gcc all-target-libgcc
262
263 # Stage 1 didn't build libgcc_eh.a, which is needed later on.  We can fake it
264 # with a symlink, for now.  Later, when we build stage2 gcc, we'll get a real
265 # libgcc_eh
266 .$(BINARY_PREFIX)gcc-stage1-install: 
267         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
268         make -j $(MAKE_JOBS) install-gcc install-target-libgcc;
269         ln -fs libgcc.a `$(BINARY_PREFIX)gcc -print-libgcc-file-name | \
270                          sed 's/libgcc/&_eh/'`
271
272 .$(BINARY_PREFIX)ros-install-dummy-libs:
273         rm -rf $(INSTDIR)/$(ARCH)-ros/lib/libparlib.a
274         $(ARCH)-ros-ar cr $(INSTDIR)/$(ARCH)-ros/lib/libparlib.a
275
276 .$(BINARY_PREFIX)glibc-configure: 
277         $(MAKE) $(BINARY_PREFIX)glibc-builddir
278         cd $(BINARY_PREFIX)glibc-builddir; \
279         ../glibc-$(GLIBC_VERSION)/configure \
280                   --host=$(ARCH)-ros \
281                   --enable-hacker-mode \
282                   --enable-shared \
283                   --with-elf \
284                   --with-tls \
285                   --disable-nls \
286                   --with-__thread \
287                   --with-headers=$(INSTDIR)/$(ARCH)-ros/sys-include \
288                   --prefix=$(INSTDIR)/$(ARCH)-ros \
289                   libc_cv_forced_unwind=yes \
290                   libc_cv_c_cleanup=yes \
291                   CFLAGS="$(ARCH_CFLAGS) -g -fpermissive -O2 -pipe";
292         touch $@
293
294 .$(BINARY_PREFIX)glibc-make: .$(BINARY_PREFIX)glibc-configure
295         cd $(BINARY_PREFIX)glibc-builddir; \
296         make -j $(MAKE_JOBS)
297
298 .$(BINARY_PREFIX)glibc-install: 
299         cd $(BINARY_PREFIX)glibc-builddir; \
300         make -j $(MAKE_JOBS) install
301
302 .$(BINARY_PREFIX)ros-install-libs:
303         cd $(ROSDIR); \
304         make userclean; \
305         make ARCH=$(ARCH) -j $(MAKE_JOBS) install-libs
306
307 .$(BINARY_PREFIX)gcc-stage2-configure:
308         $(MAKE) $(BINARY_PREFIX)gcc-stage2-builddir
309         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
310         ../gcc-$(GCC_VERSION)/configure \
311                   --enable-shared \
312                   --enable-tls \
313                   --enable-libgomp \
314                   --disable-multilib \
315                   --disable-nls \
316                   --program-prefix=$(BINARY_PREFIX) \
317                   --target=$(ARCH)-ros \
318                   --prefix=$(INSTDIR) \
319                   --enable-languages=c,c++ \
320                   $(GCC_BUILD_FLAGS) \
321                   --without-libffi \
322                   --with-__thread \
323                   --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
324                   --disable-werror;
325         echo "MAKEINFO = :" >> $(BINARY_PREFIX)gcc-stage2-builddir/Makefile;
326         touch $@
327
328 .$(BINARY_PREFIX)gcc-stage2-make: .$(BINARY_PREFIX)gcc-stage2-configure
329         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
330         make -j $(MAKE_JOBS)
331
332 .$(BINARY_PREFIX)gcc-stage2-install:
333         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
334         make -j $(MAKE_JOBS) install
335
336 patches: gcc-$(GCC_VERSION) binutils-$(BINUTILS_VERSION)
337         rm -rf orig-src
338         mkdir orig-src
339         
340         find . -name autom4te.cache | xargs rm -rf
341         patch --no-backup-if-mismatch -Rp0 < gcc-$(GCC_VERSION)-riscv.patch
342         tar -C orig-src -jxf gcc-$(GCC_VERSION).tar.bz2
343         -diff -ruN orig-src/gcc-$(GCC_VERSION) gcc-$(GCC_VERSION) \
344               > gcc-$(GCC_VERSION)-ros.patch
345         patch --no-backup-if-mismatch -p0 < gcc-$(GCC_VERSION)-riscv.patch
346         
347         patch --no-backup-if-mismatch -Rp0 < binutils-$(BINUTILS_VERSION)-riscv.patch
348         tar -C orig-src -jxf binutils-$(BINUTILS_VERSION).tar.bz2
349         -diff -ruN orig-src/binutils-$(BINUTILS_VERSION) binutils-$(BINUTILS_VERSION) \
350               > binutils-$(BINUTILS_VERSION)-ros.patch
351         patch --no-backup-if-mismatch -p0 < binutils-$(BINUTILS_VERSION)-riscv.patch
352         
353         rm -rf orig-src