make all .o files depend on *_impl.h and libc.h
[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 IMPH = src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/libc.h
24
25 LDFLAGS = 
26 CPPFLAGS =
27 CFLAGS = -Os -pipe
28 CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc 
29
30 CFLAGS_ALL = $(CFLAGS_C99FSE)
31 CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I./src/internal -I./include -I./arch/$(ARCH)
32 CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS)
33 CFLAGS_ALL_STATIC = $(CFLAGS_ALL)
34 CFLAGS_ALL_SHARED = $(CFLAGS_ALL) -fPIC -DSHARED -O3
35
36 AR      = $(CROSS_COMPILE)ar
37 RANLIB  = $(CROSS_COMPILE)ranlib
38
39 ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH))
40
41 EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
42 EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
43 CRT_LIBS = lib/crt1.o lib/Scrt1.o lib/crti.o lib/crtn.o
44 STATIC_LIBS = lib/libc.a
45 SHARED_LIBS = lib/libc.so
46 TOOL_LIBS = lib/musl-gcc.specs
47 ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
48 ALL_TOOLS = tools/musl-gcc
49
50 LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH).so.1
51
52 -include config.mak
53
54 all: $(ALL_LIBS) $(ALL_TOOLS)
55
56 install: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%) $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
57
58 clean:
59         rm -f crt/*.o
60         rm -f $(OBJS)
61         rm -f $(LOBJS)
62         rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
63         rm -f $(ALL_TOOLS)
64         rm -f $(GENH) 
65         rm -f include/bits
66
67 distclean: clean
68         rm -f config.mak
69
70 include/bits:
71         @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
72         ln -sf ../arch/$(ARCH)/bits $@
73
74 include/bits/alltypes.h.sh: include/bits
75
76 include/bits/alltypes.h: include/bits/alltypes.h.sh
77         sh $< > $@
78
79 %.o: $(ARCH)/%.s
80         $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
81
82 %.o: %.c $(GENH) $(IMPH)
83         $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
84
85 %.lo: $(ARCH)/%.s
86         $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
87
88 %.lo: %.c $(GENH) $(IMPH)
89         $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
90
91 lib/libc.so: $(LOBJS)
92         $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS) -nostdlib -shared \
93         -Wl,-e,_start -Wl,-Bsymbolic-functions \
94         -Wl,-soname=libc.so -o $@ $(LOBJS) -lgcc
95
96 lib/libc.a: $(OBJS)
97         rm -f $@
98         $(AR) rc $@ $(OBJS)
99         $(RANLIB) $@
100
101 $(EMPTY_LIBS):
102         rm -f $@
103         $(AR) rc $@
104
105 lib/%.o: crt/%.o
106         cp $< $@
107
108 lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
109         sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
110
111 tools/musl-gcc: config.mak
112         printf '#!/bin/sh\nexec gcc "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
113         chmod +x $@
114
115 $(DESTDIR)$(bindir)/%: tools/%
116         install -D $< $@
117
118 $(DESTDIR)$(libdir)/%.so: lib/%.so
119         install -D -m 755 $< $@
120
121 $(DESTDIR)$(libdir)/%: lib/%
122         install -D -m 644 $< $@
123
124 $(DESTDIR)$(includedir)/%: include/%
125         install -D -m 644 $< $@
126
127 $(DESTDIR)$(LDSO_PATHNAME): lib/libc.so
128         install -d -m 755 $(DESTDIR)$(syslibdir) || true
129         ln -sf $(libdir)/libc.so $@ || true
130
131 .PRECIOUS: $(CRT_LIBS:lib/%=crt/%)
132
133 .PHONY: all clean install