Need to blow out the parlib archive before building glibc
[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         touch $@
207
208 .$(BINARY_PREFIX)binutils-make: .$(BINARY_PREFIX)binutils-configure
209         cd $(BINARY_PREFIX)binutils-builddir; \
210         make -j $(MAKE_JOBS)
211
212 .$(BINARY_PREFIX)binutils-install:
213         cd $(BINARY_PREFIX)binutils-builddir; \
214         make -j $(MAKE_JOBS) install
215
216 .$(BINARY_PREFIX)ros-headers-install: $(ROSDIR)
217         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/*
218         mkdir -p $(INSTDIR)/$(ARCH)-ros/sys-include
219         cp -r $(ROSDIR)/kern/include/ros \
220            $(INSTDIR)/$(ARCH)-ros/sys-include/
221         rm -f $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
222         mkdir $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
223         cp -r $(ROSDIR)/kern/arch/$(ROS_ARCH_DIR)/ros/* \
224            $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch/
225         cp -r $(ROSDIR)/user/parlib/include/* \
226            $(INSTDIR)/$(ARCH)-ros/sys-include/
227         cp -r $(ROSDIR)/user/pthread/*.h \
228            $(INSTDIR)/$(ARCH)-ros/sys-include/
229         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/arch
230         ln -s $(ARCH) $(INSTDIR)/$(ARCH)-ros/sys-include/arch 
231
232 .$(BINARY_PREFIX)gcc-stage1-configure: 
233         $(MAKE) $(BINARY_PREFIX)gcc-stage1-builddir
234         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
235         ../gcc-$(GCC_VERSION)/configure \
236                 --enable-languages=c \
237                 --enable-shared \
238                 --enable-tls \
239                 --disable-checking \
240                 --disable-werror \
241                 --disable-multilib \
242                 --disable-libmudflap \
243                 --disable-libssp \
244                 --disable-libquadmath \
245                 --disable-libgomp \
246                 --disable-nls \
247                 --program-prefix=$(BINARY_PREFIX) \
248                 --target=$(ARCH)-ros \
249                 --prefix=$(INSTDIR) \
250                 --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
251                 $(GCC_BUILD_FLAGS);
252         touch $@
253
254 .$(BINARY_PREFIX)gcc-stage1-make: .$(BINARY_PREFIX)gcc-stage1-configure
255         # Put the - in front of make because it actually fails and
256         # we don't want the whole make process to fail.
257         # It appears to still work though since this is just stage 1 of compiling
258         # Should really look into fixing this though...
259         -cd $(BINARY_PREFIX)gcc-stage1-builddir; \
260         make -j $(MAKE_JOBS) inhibit_libc=true cross-compiling=yes
261
262 .$(BINARY_PREFIX)gcc-stage1-install: 
263         -cd $(BINARY_PREFIX)gcc-stage1-builddir; \
264         make -j $(MAKE_JOBS) install
265         #-ln -vs libgcc.a `$(BINARY_PREFIX)gcc -print-libgcc-file-name | \
266         #                 sed 's/libgcc/&_eh/'`
267
268 .$(BINARY_PREFIX)ros-install-dummy-libs:
269         rm -rf $(INSTDIR)/$(ARCH)-ros/lib/libparlib.a
270         $(ARCH)-ros-ar cr $(INSTDIR)/$(ARCH)-ros/lib/libparlib.a
271
272 .$(BINARY_PREFIX)glibc-configure: 
273         $(MAKE) $(BINARY_PREFIX)glibc-builddir
274         cd $(BINARY_PREFIX)glibc-builddir; \
275         ../glibc-$(GLIBC_VERSION)/configure \
276                   --host=$(ARCH)-ros \
277                   --enable-hacker-mode \
278                   --enable-shared \
279                   --with-elf \
280                   --with-tls \
281                   --disable-nls \
282                   --with-__thread \
283                   --with-headers=$(INSTDIR)/$(ARCH)-ros/sys-include \
284                   --prefix=$(INSTDIR)/$(ARCH)-ros \
285                   libc_cv_forced_unwind=yes \
286                   libc_cv_c_cleanup=yes \
287                   CFLAGS="$(ARCH_CFLAGS) -g -fpermissive -O2 -pipe";
288         touch $@
289
290 .$(BINARY_PREFIX)glibc-make: .$(BINARY_PREFIX)glibc-configure
291         cd $(BINARY_PREFIX)glibc-builddir; \
292         make -j $(MAKE_JOBS)
293
294 .$(BINARY_PREFIX)glibc-install: 
295         cd $(BINARY_PREFIX)glibc-builddir; \
296         make -j $(MAKE_JOBS) install
297
298 .$(BINARY_PREFIX)ros-install-libs:
299         cd $(ROSDIR); \
300         make TARGET_ARCH=$(ARCH) -j $(MAKE_JOBS) install-libs
301
302 .$(BINARY_PREFIX)gcc-stage2-configure:
303         $(MAKE) $(BINARY_PREFIX)gcc-stage2-builddir
304         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
305         ../gcc-$(GCC_VERSION)/configure \
306                   --enable-shared \
307                   --enable-tls \
308                   --enable-libgomp \
309                   --disable-multilib \
310                   --disable-nls \
311                   --program-prefix=$(BINARY_PREFIX) \
312                   --target=$(ARCH)-ros \
313                   --prefix=$(INSTDIR) \
314                   --enable-languages=c,c++,go \
315                   $(GCC_BUILD_FLAGS) \
316                   --without-libffi \
317                   --with-__thread \
318                   --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
319                   --disable-werror;
320         touch $@
321
322 .$(BINARY_PREFIX)gcc-stage2-make: .$(BINARY_PREFIX)gcc-stage2-configure
323         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
324         make -j $(MAKE_JOBS)
325
326 .$(BINARY_PREFIX)gcc-stage2-install:
327         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
328         make -j $(MAKE_JOBS) install
329
330 patches: gcc-$(GCC_VERSION) binutils-$(BINUTILS_VERSION)
331         rm -rf orig-src
332         mkdir orig-src
333         
334         find . -name autom4te.cache | xargs rm -rf
335         patch --no-backup-if-mismatch -Rp0 < gcc-$(GCC_VERSION)-riscv.patch
336         tar -C orig-src -jxf gcc-$(GCC_VERSION).tar.bz2
337         -diff -ruN orig-src/gcc-$(GCC_VERSION) gcc-$(GCC_VERSION) \
338               > gcc-$(GCC_VERSION)-ros.patch
339         patch --no-backup-if-mismatch -p0 < gcc-$(GCC_VERSION)-riscv.patch
340         
341         patch --no-backup-if-mismatch -Rp0 < binutils-$(BINUTILS_VERSION)-riscv.patch
342         tar -C orig-src -jxf binutils-$(BINUTILS_VERSION).tar.bz2
343         -diff -ruN orig-src/binutils-$(BINUTILS_VERSION) binutils-$(BINUTILS_VERSION) \
344               > binutils-$(BINUTILS_VERSION)-ros.patch
345         patch --no-backup-if-mismatch -p0 < binutils-$(BINUTILS_VERSION)-riscv.patch
346         
347         rm -rf orig-src