Fixes 4.6.1 compiler build with gcc 4.7 (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 -p1 < gcc-4.6.1-compile.patch
149         patch --no-backup-if-mismatch -p0 < gcc-$(GCC_VERSION)-riscv.patch
150
151 .gcc-ros-patch:
152         -cd gcc-$(GCC_VERSION); \
153         diff -ruB --unidirectional-new-file \
154              ../gcc-$(GCC_VERSION) \
155              ../gcc-$(GCC_VERSION)-ros \
156              | grep -v "Only in ../gcc-$(GCC_VERSION)" \
157              | patch -p2 
158
159 glibc-$(GLIBC_VERSION): glibc-$(GLIBC_VERSION).tar.bz2
160         rm -rf glibc-$(GLIBC_VERSION)
161         tar -jxf glibc-$(GLIBC_VERSION).tar.bz2
162         $(MAKE) .glibc-ros-patch
163         patch --no-backup-if-mismatch -p0 < glibc-$(GLIBC_VERSION)-riscv.patch
164         rm glibc-$(GLIBC_VERSION)/sysdeps/sparc/sparc32/elf/start.S
165         rm glibc-$(GLIBC_VERSION)/sysdeps/riscv/elf/start.S
166         rm glibc-$(GLIBC_VERSION)/sysdeps/riscv/init-first.c
167         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/elf/start.S
168         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/____longjmp_chk.S
169         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/init-first.c
170         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/sysdep.h
171
172 .glibc-ros-patch:
173         -cd glibc-$(GLIBC_VERSION); \
174         diff -ruB --unidirectional-new-file \
175              ../glibc-$(GLIBC_VERSION) \
176              ../glibc-$(GLIBC_VERSION)-ros \
177              | grep -v "Only in ../glibc-$(GLIBC_VERSION)" \
178              | patch -p2 
179
180 $(BINARY_PREFIX)binutils-builddir: binutils-$(BINUTILS_VERSION)
181         rm -rf $@
182         mkdir -p $@
183
184 $(BINARY_PREFIX)gcc-stage1-builddir: gcc-$(GCC_VERSION)
185         rm -rf $@
186         mkdir -p $@
187         
188 $(BINARY_PREFIX)glibc-builddir: glibc-$(GLIBC_VERSION)
189         rm -rf $@
190         mkdir -p $@
191
192 $(BINARY_PREFIX)gcc-stage2-builddir: gcc-$(GCC_VERSION)
193         rm -rf $@
194         mkdir -p $@
195         
196 .$(BINARY_PREFIX)binutils-configure: 
197         $(MAKE) $(BINARY_PREFIX)binutils-builddir 
198         cd $(BINARY_PREFIX)binutils-builddir; \
199         ../binutils-$(BINUTILS_VERSION)/configure \
200                     --enable-shared \
201                     --disable-multilib \
202                     --disable-nls \
203                     --target=$(ARCH)-ros \
204                     --prefix=$(INSTDIR) \
205                     --includedir=$(INSTDIR)/include \
206                     --disable-werror;
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                 --enable-shared \
239                 --enable-tls \
240                 --disable-checking \
241                 --disable-werror \
242                 --disable-multilib \
243                 --disable-libmudflap \
244                 --disable-libssp \
245                 --disable-libquadmath \
246                 --disable-libgomp \
247                 --disable-nls \
248                 --program-prefix=$(BINARY_PREFIX) \
249                 --target=$(ARCH)-ros \
250                 --prefix=$(INSTDIR) \
251                 --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
252                 $(GCC_BUILD_FLAGS);
253         touch $@
254
255 .$(BINARY_PREFIX)gcc-stage1-make: .$(BINARY_PREFIX)gcc-stage1-configure
256         # Put the - in front of make because it actually fails and
257         # we don't want the whole make process to fail.
258         # It appears to still work though since this is just stage 1 of compiling
259         # Should really look into fixing this though...
260         -cd $(BINARY_PREFIX)gcc-stage1-builddir; \
261         make -j $(MAKE_JOBS) inhibit_libc=true cross-compiling=yes
262
263 .$(BINARY_PREFIX)gcc-stage1-install: 
264         -cd $(BINARY_PREFIX)gcc-stage1-builddir; \
265         make -j $(MAKE_JOBS) install
266         #-ln -vs libgcc.a `$(BINARY_PREFIX)gcc -print-libgcc-file-name | \
267         #                 sed 's/libgcc/&_eh/'`
268
269 .$(BINARY_PREFIX)ros-install-dummy-libs:
270         rm -rf $(INSTDIR)/$(ARCH)-ros/lib/libparlib.a
271         $(ARCH)-ros-ar cr $(INSTDIR)/$(ARCH)-ros/lib/libparlib.a
272
273 .$(BINARY_PREFIX)glibc-configure: 
274         $(MAKE) $(BINARY_PREFIX)glibc-builddir
275         cd $(BINARY_PREFIX)glibc-builddir; \
276         ../glibc-$(GLIBC_VERSION)/configure \
277                   --host=$(ARCH)-ros \
278                   --enable-hacker-mode \
279                   --enable-shared \
280                   --with-elf \
281                   --with-tls \
282                   --disable-nls \
283                   --with-__thread \
284                   --with-headers=$(INSTDIR)/$(ARCH)-ros/sys-include \
285                   --prefix=$(INSTDIR)/$(ARCH)-ros \
286                   libc_cv_forced_unwind=yes \
287                   libc_cv_c_cleanup=yes \
288                   CFLAGS="$(ARCH_CFLAGS) -g -fpermissive -O2 -pipe";
289         touch $@
290
291 .$(BINARY_PREFIX)glibc-make: .$(BINARY_PREFIX)glibc-configure
292         cd $(BINARY_PREFIX)glibc-builddir; \
293         make -j $(MAKE_JOBS)
294
295 .$(BINARY_PREFIX)glibc-install: 
296         cd $(BINARY_PREFIX)glibc-builddir; \
297         make -j $(MAKE_JOBS) install
298
299 .$(BINARY_PREFIX)ros-install-libs:
300         cd $(ROSDIR); \
301         make TARGET_ARCH=$(ARCH) -j $(MAKE_JOBS) install-libs
302
303 .$(BINARY_PREFIX)gcc-stage2-configure:
304         $(MAKE) $(BINARY_PREFIX)gcc-stage2-builddir
305         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
306         ../gcc-$(GCC_VERSION)/configure \
307                   --enable-shared \
308                   --enable-tls \
309                   --enable-libgomp \
310                   --disable-multilib \
311                   --disable-nls \
312                   --program-prefix=$(BINARY_PREFIX) \
313                   --target=$(ARCH)-ros \
314                   --prefix=$(INSTDIR) \
315                   --enable-languages=c,c++,go \
316                   $(GCC_BUILD_FLAGS) \
317                   --without-libffi \
318                   --with-__thread \
319                   --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
320                   --disable-werror;
321         touch $@
322
323 .$(BINARY_PREFIX)gcc-stage2-make: .$(BINARY_PREFIX)gcc-stage2-configure
324         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
325         make -j $(MAKE_JOBS)
326
327 .$(BINARY_PREFIX)gcc-stage2-install:
328         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
329         make -j $(MAKE_JOBS) install
330
331 patches: gcc-$(GCC_VERSION) binutils-$(BINUTILS_VERSION)
332         rm -rf orig-src
333         mkdir orig-src
334         
335         find . -name autom4te.cache | xargs rm -rf
336         patch --no-backup-if-mismatch -Rp0 < gcc-$(GCC_VERSION)-riscv.patch
337         tar -C orig-src -jxf gcc-$(GCC_VERSION).tar.bz2
338         -diff -ruN orig-src/gcc-$(GCC_VERSION) gcc-$(GCC_VERSION) \
339               > gcc-$(GCC_VERSION)-ros.patch
340         patch --no-backup-if-mismatch -p0 < gcc-$(GCC_VERSION)-riscv.patch
341         
342         patch --no-backup-if-mismatch -Rp0 < binutils-$(BINUTILS_VERSION)-riscv.patch
343         tar -C orig-src -jxf binutils-$(BINUTILS_VERSION).tar.bz2
344         -diff -ruN orig-src/binutils-$(BINUTILS_VERSION) binutils-$(BINUTILS_VERSION) \
345               > binutils-$(BINUTILS_VERSION)-ros.patch
346         patch --no-backup-if-mismatch -p0 < binutils-$(BINUTILS_VERSION)-riscv.patch
347         
348         rm -rf orig-src