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