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