new gcc wrapper, entirely specfile based
[musl] / Makefile
1 #
2 # Makefile for musl (requires GNU make)
3 #
4 # This is how simple every makefile should be...
5 # No, I take that back - actually most should be less than half this size.
6 #
7 # Use config.mak to override any of the following variables.
8 # Do not make changes here.
9 #
10
11 exec_prefix = /usr/local
12 bindir = $(exec_prefix)/bin
13
14 prefix = /usr/local/musl
15 includedir = $(prefix)/include
16 libdir = $(prefix)/lib
17 syslibdir = /lib
18
19 SRCS = $(sort $(wildcard src/*/*.c))
20 OBJS = $(SRCS:.c=.o)
21 LOBJS = $(OBJS:.o=.lo)
22 GENH = include/bits/alltypes.h
23
24 CFLAGS  = -Os -nostdinc -ffreestanding -std=c99 -D_XOPEN_SOURCE=700 -pipe
25 LDFLAGS = -nostdlib -shared -fPIC -Wl,-e,_start -Wl,-Bsymbolic-functions
26 INC     = -I./src/internal -I./include -I./arch/$(ARCH)
27 PIC     = -fPIC -O3
28 AR      = $(CROSS_COMPILE)ar
29 RANLIB  = $(CROSS_COMPILE)ranlib
30 OBJCOPY = $(CROSS_COMPILE)objcopy
31
32 ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH))
33
34 EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
35 EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
36 CRT_LIBS = lib/crt1.o lib/crti.o lib/crtn.o
37 STATIC_LIBS = lib/libc.a $(EMPTY_LIBS)
38 SHARED_LIBS = lib/libc.so
39 TOOL_LIBS = lib/musl-gcc.specs
40 ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(TOOL_LIBS)
41 ALL_TOOLS = tools/musl-gcc
42
43 LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH).so.1
44
45 -include config.mak
46
47 all: $(ALL_LIBS) $(ALL_TOOLS)
48
49 install: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%) $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
50
51 clean:
52         rm -f crt/*.o
53         rm -f $(OBJS)
54         rm -f $(LOBJS)
55         rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
56         rm -f $(ALL_TOOLS)
57         rm -f $(GENH) 
58         rm -f include/bits
59
60 include/bits:
61         @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
62         ln -sf ../arch/$(ARCH)/bits $@
63
64 include/bits/alltypes.h.sh: include/bits
65
66 include/bits/alltypes.h: include/bits/alltypes.h.sh
67         sh $< > $@
68
69 %.o: $(ARCH)/%.s
70         $(CC) $(CFLAGS) $(INC) -c -o $@ $<
71
72 %.o: %.c $(GENH)
73         $(CC) $(CFLAGS) $(INC) -c -o $@ $<
74
75 %.lo: $(ARCH)/%.s
76         $(CC) $(CFLAGS) $(INC) $(PIC) -c -o $@ $<
77
78 %.lo: %.c $(GENH)
79         $(CC) $(CFLAGS) $(INC) $(PIC) -c -o $@ $<
80
81 lib/libc.so: $(LOBJS)
82         $(CC) $(LDFLAGS) -Wl,-soname=libc.so -o $@ $(LOBJS) -lgcc
83         $(OBJCOPY) --weaken $@
84
85 lib/libc.a: $(OBJS)
86         rm -f $@
87         $(AR) rc $@ $(OBJS)
88         $(RANLIB) $@
89
90 $(EMPTY_LIBS):
91         rm -f $@
92         $(AR) rc $@
93
94 lib/%.o: crt/%.o
95         cp $< $@
96
97 lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
98         sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
99
100 tools/musl-gcc: config.mak
101         printf '#!/bin/sh\nexec gcc "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
102         chmod +x $@
103
104 $(DESTDIR)$(bindir)/%: tools/%
105         install -D $< $@
106
107 $(DESTDIR)$(libdir)/%.so: lib/%.so
108         install -D -m 755 $< $@
109
110 $(DESTDIR)$(libdir)/%: lib/%
111         install -D -m 644 $< $@
112
113 $(DESTDIR)$(includedir)/%: include/%
114         install -D -m 644 $< $@
115
116 $(DESTDIR)$(LDSO_PATHNAME): lib/libc.so
117         install -d -m 755 $(DESTDIR)$(syslibdir) || true
118         ln -sf $(libdir)/libc.so $@ || true
119
120 .PRECIOUS: $(CRT_LIBS:lib/%=crt/%)
121
122 .PHONY: all clean install