GMP, MPFR, and MPC are external dependences (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 BINUTILS_VERSION := 2.21.1
24 GCC_VERSION := 4.3.2
25 GLIBC_VERSION := 2.11.1
26
27 BUILDDIR ?= $(shell pwd)/
28 ROSDIR ?= $(BUILDDIR)/../../../
29 DESTDIR ?= /
30
31 # Configuration parameters for make-riscv
32 RISCV_ARCH := riscv
33 RISCV_BINARY_PREFIX := $(RISCV_ARCH)-ros-
34 RISCV_GCC_BUILD_FLAGS := --with-cpu=v8
35 RISCV_CFLAGS :=
36
37 # Configuration parameters for x86
38 X86_ARCH := i686
39 X86_BINARY_PREFIX := $(X86_ARCH)-ros-
40 X86_GCC_BUILD_FLAGS := 
41 X86_CFLAGS := -march=i486
42
43 all:
44         @echo "You need to run one of the following commands, "
45         @echo "depending on which version of the cross compiler you are trying to generate:"
46         @echo "  make riscv"
47         @echo "  make x86"
48         @echo "  make riscv-revert"
49         @echo "  make x86-revert"
50         @echo "  make riscv-uninstall"
51         @echo "  make x86-uninstall"
52         @echo "  make clean"
53
54 riscv:
55         export LD_LIBRARY_PATH=; \
56         export DESTDIR=$(DESTDIR);\
57         export INSTDIR=$(RISCV_INSTDIR);\
58         export PATH=$(DESTDIR)$(RISCV_INSTDIR)/bin:$(PATH);\
59         export ARCH=$(RISCV_ARCH);\
60         export ARCH_CFLAGS=$(RISCV_CFLAGS);\
61         export ROS_ARCH_DIR=$(RISCV_ARCH);\
62         export BINARY_PREFIX=$(RISCV_BINARY_PREFIX);\
63         export GCC_BUILD_FLAGS=$(RISCV_GCC_BUILD_FLAGS);\
64         $(MAKE) make-all
65
66 x86:
67         $(MAKE) .glibc-patch
68         export LD_LIBRARY_PATH=; \
69         export DESTDIR=$(DESTDIR);\
70         export INSTDIR=$(X86_INSTDIR);\
71         export PATH=$(DESTDIR)$(X86_INSTDIR)/bin:$(PATH);\
72         export ARCH=$(X86_ARCH);\
73         export ARCH_CFLAGS=$(X86_CFLAGS);\
74         export ROS_ARCH_DIR=$(X86_ARCH);\
75         export BINARY_PREFIX=$(X86_BINARY_PREFIX);\
76         export GCC_BUILD_FLAGS=$(X86_GCC_BUILD_FLAGS);\
77         $(MAKE) make-all
78
79 make-all: 
80         $(MAKE) .glibc-patch
81         $(MAKE) .$(BINARY_PREFIX)binutils-make
82         $(MAKE) .$(BINARY_PREFIX)binutils-install
83         $(MAKE) .$(BINARY_PREFIX)ros-headers-install
84         $(MAKE) .$(BINARY_PREFIX)glibc-headers-install
85         $(MAKE) .$(BINARY_PREFIX)gcc-stage1-make
86         $(MAKE) .$(BINARY_PREFIX)gcc-stage1-install
87         $(MAKE) .$(BINARY_PREFIX)glibc-make
88         $(MAKE) .$(BINARY_PREFIX)glibc-install
89         $(MAKE) .$(BINARY_PREFIX)gcc-stage2-make
90         $(MAKE) .$(BINARY_PREFIX)gcc-stage2-install
91         mkdir -p $(ROSDIR)/fs/$(ARCH)/lib
92         cp -r $(INSTDIR)/$(ARCH)-ros/lib/* $(ROSDIR)/fs/$(ARCH)/lib
93
94 riscv-uninstall:
95         -rm -rf $(RISCV_INSTDIR)
96
97 x86-uninstall:
98         -rm -rf $(X86_INSTDIR)
99
100 riscv-revert:
101         rm -rf $(RISCV_INSTDIR)
102         cp -r $(RISCV_BINARY_PREFIX)install.bak $(RISCV_INSTDIR)
103
104 x86-revert: 
105         rm -rf $(X86_INSTDIR)
106         cp -r $(X86_BINARY_PREFIX)install.bak $(X86_INSTDIR)
107
108 clean:
109         rm -rf binutils-$(BINUTILS_VERSION) 
110         rm -rf gcc-$(GCC_VERSION) 
111         rm -rf glibc-$(GLIBC_VERSION) 
112         rm -rf *-builddir
113         rm -rf .*-ros-*-configure
114         rm -rf *$(BINARY_PREFIX)install.bak
115
116 binutils-clean:
117         cd $(BINARY_PREFIX)binutils-builddir; \
118         make clean
119
120 gcc-clean:
121         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
122         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
123         make clean
124
125 glibc-clean:
126         cd $(BINARY_PREFIX)glibc-headers-builddir; \
127         cd $(BINARY_PREFIX)glibc-builddir; \
128         make clean
129
130 %.tar.bz2:
131         wget http://akaros.cs.berkeley.edu/xcc/$@
132
133 binutils-$(BINUTILS_VERSION): binutils-$(BINUTILS_VERSION).tar.bz2
134         rm -rf binutils-$(BINUTILS_VERSION);
135         tar -jxf binutils-$(BINUTILS_VERSION).tar.bz2
136         patch -p0 < binutils-$(BINUTILS_VERSION)-ros.patch
137         patch -p0 < binutils-$(BINUTILS_VERSION)-riscv.patch
138
139 gcc-$(GCC_VERSION):
140         rm -rf gcc-$(GCC_VERSION)
141         tar -zxf gcc-$(GCC_VERSION).tar.gz
142         cd gcc-$(GCC_VERSION); \
143         patch -p1 < ../gcc-$(GCC_VERSION)-ros.patch
144
145 glibc-$(GLIBC_VERSION): 
146         rm -rf glibc-$(GLIBC_VERSION)
147         tar -jxf glibc-$(GLIBC_VERSION).tar.bz2
148         rm glibc-$(GLIBC_VERSION)/sysdeps/sparc/sparc32/elf/start.S
149         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/elf/start.S
150         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/____longjmp_chk.S
151         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/init-first.c
152         rm glibc-$(GLIBC_VERSION)/sysdeps/i386/sysdep.h
153         $(MAKE) .glibc-patch
154
155 .glibc-patch: glibc-$(GLIBC_VERSION)
156         -cd glibc-$(GLIBC_VERSION); \
157         diff -ruB --unidirectional-new-file \
158              ../glibc-$(GLIBC_VERSION) \
159              ../glibc-$(GLIBC_VERSION)-ros \
160              | grep -v "Only in ../glibc-$(GLIBC_VERSION)" \
161              | patch -p2 
162
163 $(BINARY_PREFIX)binutils-builddir: binutils-$(BINUTILS_VERSION)
164         rm -rf $@
165         cp -R binutils-$(BINUTILS_VERSION) $@
166
167 $(BINARY_PREFIX)glibc-headers-builddir: glibc-$(GLIBC_VERSION)
168         rm -rf $@
169         mkdir -p $@
170
171 $(BINARY_PREFIX)gcc-stage1-builddir: gcc-$(GCC_VERSION)
172         rm -rf $@
173         mkdir -p $@
174         
175 $(BINARY_PREFIX)glibc-builddir: glibc-$(GLIBC_VERSION)
176         rm -rf $@
177         mkdir -p $@
178
179 $(BINARY_PREFIX)gcc-stage2-builddir: gcc-$(GCC_VERSION)
180         rm -rf $@
181         mkdir -p $@
182         
183 .$(BINARY_PREFIX)binutils-configure: 
184         $(MAKE) $(BINARY_PREFIX)binutils-builddir 
185         cd $(BINARY_PREFIX)binutils-builddir; \
186         ./configure --enable-shared \
187                     --disable-multilib \
188                     --disable-nls \
189                     --target=$(ARCH)-ros \
190                     --prefix=$(INSTDIR) \
191                     --includedir=$(INSTDIR)/include \
192                     --disable-werror;
193         touch $@
194
195 .$(BINARY_PREFIX)binutils-make: .$(BINARY_PREFIX)binutils-configure
196         cd $(BINARY_PREFIX)binutils-builddir; \
197         make -j $(MAKE_JOBS)
198
199 .$(BINARY_PREFIX)binutils-install:
200         cd $(BINARY_PREFIX)binutils-builddir; \
201         make -j $(MAKE_JOBS) install
202
203 .$(BINARY_PREFIX)glibc-headers-configure:
204         $(MAKE) $(BINARY_PREFIX)glibc-headers-builddir
205         cd $(BINARY_PREFIX)glibc-headers-builddir; \
206         ../glibc-$(GLIBC_VERSION)/configure \
207                   --host=$(ARCH)-ros \
208                   --enable-hacker-mode \
209                   --enable-shared \
210                   --disable-nls \
211                   --with-elf \
212                   --with-tls \
213                   --with-__thread \
214                   --prefix=$(INSTDIR)/$(ARCH)-ros \
215                   --with-headers=$(INSTDIR)/$(ARCH)-ros/sys-include \
216                   libc_cv_forced_unwind=yes \
217                   libc_cv_c_cleanup=yes;
218         touch $@
219
220 .$(BINARY_PREFIX)glibc-headers-install: .$(BINARY_PREFIX)glibc-headers-configure
221         cd $(BINARY_PREFIX)glibc-headers-builddir; \
222         make -j $(MAKE_JOBS) install-headers; \
223         mkdir -p $(INSTDIR)/$(ARCH)-ros/include/gnu; \
224         echo > $(INSTDIR)/$(ARCH)-ros/include/gnu/stubs.h; \
225         cp bits/stdio_lim.h $(INSTDIR)/$(ARCH)-ros/include/bits/
226
227 .$(BINARY_PREFIX)ros-headers-install: $(ROSDIR)
228         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/*
229         mkdir -p $(INSTDIR)/$(ARCH)-ros/sys-include
230         cp -r $(ROSDIR)/kern/include/ros \
231            $(INSTDIR)/$(ARCH)-ros/sys-include/
232         rm -f $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
233         mkdir $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
234         cp -r $(ROSDIR)/kern/arch/$(ROS_ARCH_DIR)/ros/* \
235            $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch/
236         cp -r $(ROSDIR)/user/parlib/include/* \
237            $(INSTDIR)/$(ARCH)-ros/sys-include/
238         cp -r $(ROSDIR)/user/pthread/*.h \
239            $(INSTDIR)/$(ARCH)-ros/sys-include/
240         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/arch
241         ln -s $(ARCH) $(INSTDIR)/$(ARCH)-ros/sys-include/arch 
242
243 .$(BINARY_PREFIX)gcc-stage1-configure: 
244         $(MAKE) $(BINARY_PREFIX)gcc-stage1-builddir
245         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
246         ../gcc-$(GCC_VERSION)/configure \
247                 --enable-languages=c \
248                 --enable-shared \
249                 --disable-checking \
250                 --disable-werror \
251                 --disable-multilib \
252                 --disable-libmudflap \
253                 --disable-libssp \
254                 --disable-libgomp \
255                 --disable-nls \
256                 --program-prefix=$(BINARY_PREFIX) \
257                 --target=$(ARCH)-ros \
258                 --prefix=$(INSTDIR) \
259                 --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
260                 $(GCC_BUILD_FLAGS);
261         touch $@
262
263 .$(BINARY_PREFIX)gcc-stage1-make: .$(BINARY_PREFIX)gcc-stage1-configure
264         # Put the - in front of make because it actually fails and
265         # we don't want the whole make process to fail.
266         # It appears to still work though since this is just stage 1 of compiling
267         # Should really look into fixing this though...
268         -cd $(BINARY_PREFIX)gcc-stage1-builddir; \
269         make -j $(MAKE_JOBS) inhibit_libc=true cross-compiling=yes
270
271 .$(BINARY_PREFIX)gcc-stage1-install: 
272         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
273         make -j $(MAKE_JOBS) install
274         #-ln -vs libgcc.a `$(BINARY_PREFIX)gcc -print-libgcc-file-name | \
275         #                 sed 's/libgcc/&_eh/'`
276
277 .$(BINARY_PREFIX)glibc-configure: 
278         $(MAKE) $(BINARY_PREFIX)glibc-builddir
279         cd $(BINARY_PREFIX)glibc-builddir; \
280         ../glibc-$(GLIBC_VERSION)/configure \
281                   --host=$(ARCH)-ros \
282                   --enable-hacker-mode \
283                   --enable-shared \
284                   --with-elf \
285                   --with-tls \
286                   --disable-nls \
287                   --with-__thread \
288                   --with-headers=$(INSTDIR)/$(ARCH)-ros/sys-include \
289                   --prefix=$(INSTDIR)/$(ARCH)-ros \
290                   libc_cv_forced_unwind=yes \
291                   libc_cv_c_cleanup=yes \
292                   CFLAGS="$(ARCH_CFLAGS) -g -fpermissive -O2 -pipe";
293         touch $@
294
295 .$(BINARY_PREFIX)glibc-make: .$(BINARY_PREFIX)glibc-configure
296         cd $(BINARY_PREFIX)glibc-builddir; \
297         make -j $(MAKE_JOBS)
298
299 .$(BINARY_PREFIX)glibc-install: 
300         cd $(BINARY_PREFIX)glibc-builddir; \
301         make -j $(MAKE_JOBS) install
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                   --disable-multilib \
309                   --disable-nls \
310                   --program-prefix=$(BINARY_PREFIX) \
311                   --target=$(ARCH)-ros \
312                   --prefix=$(INSTDIR) \
313                   --enable-languages=c,c++ \
314                   $(GCC_BUILD_FLAGS) \
315                   --with-tls \
316                   --with-__thread \
317                   --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
318                   --disable-werror;
319         touch $@
320
321 .$(BINARY_PREFIX)gcc-stage2-make: .$(BINARY_PREFIX)gcc-stage2-configure
322         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
323         make -j $(MAKE_JOBS)
324
325 .$(BINARY_PREFIX)gcc-stage2-install:
326         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
327         make -j $(MAKE_JOBS) install
328
329 patches:
330         rm -rf gcc-$(GCC_VERSION)/libstdc++-v3/autom4te.cache
331         mv gcc-$(GCC_VERSION) gcc-$(GCC_VERSION).new
332         tar -zxf gcc-$(GCC_VERSION).tar.gz
333         -diff -ruN gcc-$(GCC_VERSION) gcc-$(GCC_VERSION).new \
334               > gcc-$(GCC_VERSION)-ros.patch
335         rm -rf gcc-$(GCC_VERSION)
336         mv gcc-$(GCC_VERSION).new gcc-$(GCC_VERSION)
337         
338         mv binutils-$(BINUTILS_VERSION) binutils-$(BINUTILS_VERSION).new
339         tar -zxf binutils-$(BINUTILS_VERSION).tar.gz
340         -diff -ruN binutils-$(BINUTILS_VERSION) binutils-$(BINUTILS_VERSION).new \
341               > binutils-$(BINUTILS_VERSION)-ros.patch
342         rm -rf binutils-$(BINUTILS_VERSION)
343         mv binutils-$(BINUTILS_VERSION).new binutils-$(BINUTILS_VERSION)
344