Added gcc compiler magic to get libgomp to build for akaros
[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)glibc-make
92         $(MAKE) .$(BINARY_PREFIX)glibc-install
93         $(MAKE) .$(BINARY_PREFIX)ros-install-libs
94         $(MAKE) .$(BINARY_PREFIX)gcc-stage2-make
95         $(MAKE) .$(BINARY_PREFIX)gcc-stage2-install
96         mkdir -p $(ROSDIR)/fs/$(ARCH)/lib
97         cp -r $(INSTDIR)/$(ARCH)-ros/lib/* $(ROSDIR)/fs/$(ARCH)/lib
98
99 riscv-uninstall:
100         -rm -rf $(RISCV_INSTDIR)
101
102 x86-uninstall:
103         -rm -rf $(X86_INSTDIR)
104
105 riscv-revert:
106         rm -rf $(RISCV_INSTDIR)
107         cp -r $(RISCV_BINARY_PREFIX)install.bak $(RISCV_INSTDIR)
108
109 x86-revert: 
110         rm -rf $(X86_INSTDIR)
111         cp -r $(X86_BINARY_PREFIX)install.bak $(X86_INSTDIR)
112
113 clean:
114         rm -rf binutils-$(BINUTILS_VERSION) 
115         rm -rf gcc-$(GCC_VERSION) 
116         rm -rf glibc-$(GLIBC_VERSION) 
117         rm -rf *-builddir
118         rm -rf .*-ros-*-configure
119         rm -rf *$(BINARY_PREFIX)install.bak
120
121 binutils-clean:
122         cd $(BINARY_PREFIX)binutils-builddir; \
123         make clean
124
125 gcc-clean:
126         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
127         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
128         make clean
129
130 glibc-clean:
131         cd $(BINARY_PREFIX)glibc-builddir; \
132         make clean
133
134 %.tar.bz2:
135         wget http://akaros.cs.berkeley.edu/xcc/$@
136
137 binutils-$(BINUTILS_VERSION): binutils-$(BINUTILS_VERSION).tar.bz2
138         rm -rf binutils-$(BINUTILS_VERSION);
139         tar -jxf binutils-$(BINUTILS_VERSION).tar.bz2
140         patch -p0 < binutils-$(BINUTILS_VERSION)-ros.patch
141         patch --no-backup-if-mismatch -p0 < binutils-$(BINUTILS_VERSION)-riscv.patch
142
143 gcc-$(GCC_VERSION): gcc-$(GCC_VERSION).tar.bz2
144         rm -rf gcc-$(GCC_VERSION)
145         tar -jxf gcc-$(GCC_VERSION).tar.bz2
146         $(MAKE) .gcc-ros-patch
147         patch --no-backup-if-mismatch -p0 < gcc-$(GCC_VERSION)-riscv.patch
148
149 .gcc-ros-patch:
150         -cd gcc-$(GCC_VERSION); \
151         diff -ruB --unidirectional-new-file \
152              ../gcc-$(GCC_VERSION) \
153              ../gcc-$(GCC_VERSION)-ros \
154              | grep -v "Only in ../gcc-$(GCC_VERSION)" \
155              | patch -p2 
156
157 glibc-$(GLIBC_VERSION): glibc-$(GLIBC_VERSION).tar.bz2
158         rm -rf glibc-$(GLIBC_VERSION)
159         tar -jxf glibc-$(GLIBC_VERSION).tar.bz2
160         $(MAKE) .glibc-ros-patch
161         patch --no-backup-if-mismatch -p0 < glibc-$(GLIBC_VERSION)-riscv.patch
162         rm glibc-$(GLIBC_VERSION)/sysdeps/sparc/sparc32/elf/start.S
163         rm glibc-$(GLIBC_VERSION)/sysdeps/riscv/elf/start.S
164         rm glibc-$(GLIBC_VERSION)/sysdeps/riscv/init-first.c
165         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/elf/start.S
166         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/____longjmp_chk.S
167         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/init-first.c
168         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/sysdep.h
169
170 .glibc-ros-patch:
171         -cd glibc-$(GLIBC_VERSION); \
172         diff -ruB --unidirectional-new-file \
173              ../glibc-$(GLIBC_VERSION) \
174              ../glibc-$(GLIBC_VERSION)-ros \
175              | grep -v "Only in ../glibc-$(GLIBC_VERSION)" \
176              | patch -p2 
177
178 $(BINARY_PREFIX)binutils-builddir: binutils-$(BINUTILS_VERSION)
179         rm -rf $@
180         mkdir -p $@
181
182 $(BINARY_PREFIX)gcc-stage1-builddir: gcc-$(GCC_VERSION)
183         rm -rf $@
184         mkdir -p $@
185         
186 $(BINARY_PREFIX)glibc-builddir: glibc-$(GLIBC_VERSION)
187         rm -rf $@
188         mkdir -p $@
189
190 $(BINARY_PREFIX)gcc-stage2-builddir: gcc-$(GCC_VERSION)
191         rm -rf $@
192         mkdir -p $@
193         
194 .$(BINARY_PREFIX)binutils-configure: 
195         $(MAKE) $(BINARY_PREFIX)binutils-builddir 
196         cd $(BINARY_PREFIX)binutils-builddir; \
197         ../binutils-$(BINUTILS_VERSION)/configure \
198                     --enable-shared \
199                     --disable-multilib \
200                     --disable-nls \
201                     --target=$(ARCH)-ros \
202                     --prefix=$(INSTDIR) \
203                     --includedir=$(INSTDIR)/include \
204                     --disable-werror;
205         touch $@
206
207 .$(BINARY_PREFIX)binutils-make: .$(BINARY_PREFIX)binutils-configure
208         cd $(BINARY_PREFIX)binutils-builddir; \
209         make -j $(MAKE_JOBS)
210
211 .$(BINARY_PREFIX)binutils-install:
212         cd $(BINARY_PREFIX)binutils-builddir; \
213         make -j $(MAKE_JOBS) install
214
215 .$(BINARY_PREFIX)ros-headers-install: $(ROSDIR)
216         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/*
217         mkdir -p $(INSTDIR)/$(ARCH)-ros/sys-include
218         cp -r $(ROSDIR)/kern/include/ros \
219            $(INSTDIR)/$(ARCH)-ros/sys-include/
220         rm -f $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
221         mkdir $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
222         cp -r $(ROSDIR)/kern/arch/$(ROS_ARCH_DIR)/ros/* \
223            $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch/
224         cp -r $(ROSDIR)/user/parlib/include/* \
225            $(INSTDIR)/$(ARCH)-ros/sys-include/
226         cp -r $(ROSDIR)/user/pthread/*.h \
227            $(INSTDIR)/$(ARCH)-ros/sys-include/
228         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/arch
229         ln -s $(ARCH) $(INSTDIR)/$(ARCH)-ros/sys-include/arch 
230
231 .$(BINARY_PREFIX)gcc-stage1-configure: 
232         $(MAKE) $(BINARY_PREFIX)gcc-stage1-builddir
233         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
234         ../gcc-$(GCC_VERSION)/configure \
235                 --enable-languages=c \
236                 --enable-shared \
237                 --enable-tls \
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                 --program-prefix=$(BINARY_PREFIX) \
247                 --target=$(ARCH)-ros \
248                 --prefix=$(INSTDIR) \
249                 --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
250                 $(GCC_BUILD_FLAGS);
251         touch $@
252
253 .$(BINARY_PREFIX)gcc-stage1-make: .$(BINARY_PREFIX)gcc-stage1-configure
254         # Put the - in front of make because it actually fails and
255         # we don't want the whole make process to fail.
256         # It appears to still work though since this is just stage 1 of compiling
257         # Should really look into fixing this though...
258         -cd $(BINARY_PREFIX)gcc-stage1-builddir; \
259         make -j $(MAKE_JOBS) inhibit_libc=true cross-compiling=yes
260
261 .$(BINARY_PREFIX)gcc-stage1-install: 
262         -cd $(BINARY_PREFIX)gcc-stage1-builddir; \
263         make -j $(MAKE_JOBS) install
264         #-ln -vs libgcc.a `$(BINARY_PREFIX)gcc -print-libgcc-file-name | \
265         #                 sed 's/libgcc/&_eh/'`
266
267 .$(BINARY_PREFIX)glibc-configure: 
268         $(MAKE) $(BINARY_PREFIX)glibc-builddir
269         cd $(BINARY_PREFIX)glibc-builddir; \
270         ../glibc-$(GLIBC_VERSION)/configure \
271                   --host=$(ARCH)-ros \
272                   --enable-hacker-mode \
273                   --enable-shared \
274                   --with-elf \
275                   --with-tls \
276                   --disable-nls \
277                   --with-__thread \
278                   --with-headers=$(INSTDIR)/$(ARCH)-ros/sys-include \
279                   --prefix=$(INSTDIR)/$(ARCH)-ros \
280                   libc_cv_forced_unwind=yes \
281                   libc_cv_c_cleanup=yes \
282                   CFLAGS="$(ARCH_CFLAGS) -g -fpermissive -O2 -pipe";
283         touch $@
284
285 .$(BINARY_PREFIX)glibc-make: .$(BINARY_PREFIX)glibc-configure
286         cd $(BINARY_PREFIX)glibc-builddir; \
287         make -j $(MAKE_JOBS)
288
289 .$(BINARY_PREFIX)glibc-install: 
290         cd $(BINARY_PREFIX)glibc-builddir; \
291         make -j $(MAKE_JOBS) install
292
293 .$(BINARY_PREFIX)ros-install-libs:
294         cd $(ROSDIR); \
295         make TARGET_ARCH=$(ARCH) -j $(MAKE_JOBS) install-libs
296
297 .$(BINARY_PREFIX)gcc-stage2-configure:
298         $(MAKE) $(BINARY_PREFIX)gcc-stage2-builddir
299         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
300         ../gcc-$(GCC_VERSION)/configure \
301                   --enable-shared \
302                   --enable-tls \
303                   --enable-libgomp \
304                   --disable-multilib \
305                   --disable-nls \
306                   --program-prefix=$(BINARY_PREFIX) \
307                   --target=$(ARCH)-ros \
308                   --prefix=$(INSTDIR) \
309                   --enable-languages=c,c++,go \
310                   $(GCC_BUILD_FLAGS) \
311                   --without-libffi \
312                   --with-__thread \
313                   --with-parlib \
314                   --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
315                   --disable-werror;
316         touch $@
317
318 .$(BINARY_PREFIX)gcc-stage2-make: .$(BINARY_PREFIX)gcc-stage2-configure
319         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
320         make -j $(MAKE_JOBS)
321
322 .$(BINARY_PREFIX)gcc-stage2-install:
323         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
324         make -j $(MAKE_JOBS) install
325
326 patches: gcc-$(GCC_VERSION) binutils-$(BINUTILS_VERSION)
327         rm -rf orig-src
328         mkdir orig-src
329         
330         find . -name autom4te.cache | xargs rm -rf
331         patch --no-backup-if-mismatch -Rp0 < gcc-$(GCC_VERSION)-riscv.patch
332         tar -C orig-src -jxf gcc-$(GCC_VERSION).tar.bz2
333         -diff -ruN orig-src/gcc-$(GCC_VERSION) gcc-$(GCC_VERSION) \
334               > gcc-$(GCC_VERSION)-ros.patch
335         patch --no-backup-if-mismatch -p0 < gcc-$(GCC_VERSION)-riscv.patch
336         
337         patch --no-backup-if-mismatch -Rp0 < binutils-$(BINUTILS_VERSION)-riscv.patch
338         tar -C orig-src -jxf binutils-$(BINUTILS_VERSION).tar.bz2
339         -diff -ruN orig-src/binutils-$(BINUTILS_VERSION) binutils-$(BINUTILS_VERSION) \
340               > binutils-$(BINUTILS_VERSION)-ros.patch
341         patch --no-backup-if-mismatch -p0 < binutils-$(BINUTILS_VERSION)-riscv.patch
342         
343         rm -rf orig-src