5f020fe4f0c077fc1f381ea425e163d8fbb99363
[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         patch -p0 < binutils-$(BINUTILS_VERSION)-objdump.patch
144
145 gcc-$(GCC_VERSION): gcc-$(GCC_VERSION).tar.bz2
146         rm -rf gcc-$(GCC_VERSION)
147         tar -jxf gcc-$(GCC_VERSION).tar.bz2
148         $(MAKE) .gcc-ros-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         echo "MAKEINFO = :" >> $(BINARY_PREFIX)binutils-builddir/Makefile;
208         touch $@
209
210 .$(BINARY_PREFIX)binutils-make: .$(BINARY_PREFIX)binutils-configure
211         cd $(BINARY_PREFIX)binutils-builddir; \
212         make -j $(MAKE_JOBS)
213
214 .$(BINARY_PREFIX)binutils-install:
215         cd $(BINARY_PREFIX)binutils-builddir; \
216         make -j $(MAKE_JOBS) install
217
218 .$(BINARY_PREFIX)ros-headers-install: $(ROSDIR)
219         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/*
220         mkdir -p $(INSTDIR)/$(ARCH)-ros/sys-include
221         cp -r $(ROSDIR)/kern/include/ros \
222            $(INSTDIR)/$(ARCH)-ros/sys-include/
223         rm -f $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
224         mkdir $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch
225         cp -r $(ROSDIR)/kern/arch/$(ROS_ARCH_DIR)/ros/* \
226            $(INSTDIR)/$(ARCH)-ros/sys-include/ros/arch/
227         cp -r $(ROSDIR)/user/parlib/include/* \
228            $(INSTDIR)/$(ARCH)-ros/sys-include/
229         cp -r $(ROSDIR)/user/pthread/*.h \
230            $(INSTDIR)/$(ARCH)-ros/sys-include/
231         rm -rf $(INSTDIR)/$(ARCH)-ros/sys-include/arch
232         ln -s $(ARCH) $(INSTDIR)/$(ARCH)-ros/sys-include/arch 
233
234 .$(BINARY_PREFIX)gcc-stage1-configure: 
235         $(MAKE) $(BINARY_PREFIX)gcc-stage1-builddir
236         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
237         ../gcc-$(GCC_VERSION)/configure \
238                 --enable-languages=c \
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                 --disable-libgcj \
248                 --disable-bootstrap \
249                         --disable-build-with-cxx \
250                         --disable-build-poststage1-with-cxx \
251                 --disable-shared \
252                 --program-prefix=$(BINARY_PREFIX) \
253                 --target=$(ARCH)-ros \
254                 --prefix=$(INSTDIR) \
255                 --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
256                 $(GCC_BUILD_FLAGS);
257         echo "MAKEINFO = :" >> $(BINARY_PREFIX)gcc-stage1-builddir/Makefile;
258         touch $@
259
260 .$(BINARY_PREFIX)gcc-stage1-make: .$(BINARY_PREFIX)gcc-stage1-configure
261         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
262         make -j $(MAKE_JOBS) inhibit_libc=true cross-compiling=yes all-gcc all-target-libgcc
263
264 # Stage 1 didn't build libgcc_eh.a, which is needed later on.  We can fake it
265 # with a symlink, for now.  Later, when we build stage2 gcc, we'll get a real
266 # libgcc_eh
267 .$(BINARY_PREFIX)gcc-stage1-install: 
268         cd $(BINARY_PREFIX)gcc-stage1-builddir; \
269         make -j $(MAKE_JOBS) install-gcc install-target-libgcc;
270         ln -fs libgcc.a `$(BINARY_PREFIX)gcc -print-libgcc-file-name | \
271                          sed 's/libgcc/&_eh/'`
272
273 .$(BINARY_PREFIX)ros-install-dummy-libs:
274         rm -rf $(INSTDIR)/$(ARCH)-ros/lib/libparlib.a
275         $(ARCH)-ros-ar cr $(INSTDIR)/$(ARCH)-ros/lib/libparlib.a
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)ros-install-libs:
304         cd $(ROSDIR); \
305         make userclean; \
306         make ARCH=$(ARCH) -j $(MAKE_JOBS) install-libs
307
308 .$(BINARY_PREFIX)gcc-stage2-configure:
309         $(MAKE) $(BINARY_PREFIX)gcc-stage2-builddir
310         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
311         ../gcc-$(GCC_VERSION)/configure \
312                   --enable-shared \
313                   --enable-tls \
314                   --enable-libgomp \
315                   --disable-multilib \
316                   --disable-nls \
317                   --program-prefix=$(BINARY_PREFIX) \
318                   --target=$(ARCH)-ros \
319                   --prefix=$(INSTDIR) \
320                   --enable-languages=c,c++ \
321                   $(GCC_BUILD_FLAGS) \
322                   --without-libffi \
323                   --with-__thread \
324                   --with-headers=$(INSTDIR)/$(ARCH)-ros/include \
325                   --disable-werror;
326         echo "MAKEINFO = :" >> $(BINARY_PREFIX)gcc-stage2-builddir/Makefile;
327         touch $@
328
329 .$(BINARY_PREFIX)gcc-stage2-make: .$(BINARY_PREFIX)gcc-stage2-configure
330         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
331         make -j $(MAKE_JOBS)
332
333 .$(BINARY_PREFIX)gcc-stage2-install:
334         cd $(BINARY_PREFIX)gcc-stage2-builddir; \
335         make -j $(MAKE_JOBS) install
336
337 patches: gcc-$(GCC_VERSION) binutils-$(BINUTILS_VERSION)
338         rm -rf orig-src
339         mkdir orig-src
340         
341         find . -name autom4te.cache | xargs rm -rf
342         patch --no-backup-if-mismatch -Rp0 < gcc-$(GCC_VERSION)-riscv.patch
343         tar -C orig-src -jxf gcc-$(GCC_VERSION).tar.bz2
344         -diff -ruN orig-src/gcc-$(GCC_VERSION) gcc-$(GCC_VERSION) \
345               > gcc-$(GCC_VERSION)-ros.patch
346         patch --no-backup-if-mismatch -p0 < gcc-$(GCC_VERSION)-riscv.patch
347         
348         patch --no-backup-if-mismatch -Rp0 < binutils-$(BINUTILS_VERSION)-riscv.patch
349         tar -C orig-src -jxf binutils-$(BINUTILS_VERSION).tar.bz2
350         -diff -ruN orig-src/binutils-$(BINUTILS_VERSION) binutils-$(BINUTILS_VERSION) \
351               > binutils-$(BINUTILS_VERSION)-ros.patch
352         patch --no-backup-if-mismatch -p0 < binutils-$(BINUTILS_VERSION)-riscv.patch
353         
354         rm -rf orig-src