rework makefile subarch logic to allow shared files
[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 arch/$(ARCH)/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 LIBCC = -lgcc
27 CPPFLAGS =
28 CFLAGS = -Os -pipe
29 CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc 
30
31 CFLAGS_ALL = $(CFLAGS_C99FSE)
32 CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I./arch/$(ARCH) -I./src/internal -I./include
33 CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS)
34 CFLAGS_ALL_STATIC = $(CFLAGS_ALL)
35 CFLAGS_ALL_SHARED = $(CFLAGS_ALL) -fPIC -DSHARED
36
37 AR      = $(CROSS_COMPILE)ar
38 RANLIB  = $(CROSS_COMPILE)ranlib
39
40 ARCH_INCLUDES = $(wildcard arch/$(ARCH)/bits/*.h)
41 ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH) $(ARCH_INCLUDES:arch/$(ARCH)/%=include/%))
42
43 EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
44 EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
45 CRT_LIBS = lib/crt1.o lib/Scrt1.o lib/crti.o lib/crtn.o
46 STATIC_LIBS = lib/libc.a
47 SHARED_LIBS = lib/libc.so
48 TOOL_LIBS = lib/musl-gcc.specs
49 ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
50 ALL_TOOLS = tools/musl-gcc
51
52 LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
53
54 -include config.mak
55
56 all: $(ALL_LIBS) $(ALL_TOOLS)
57
58 install: install-libs install-headers install-tools
59
60 clean:
61         rm -f crt/*.o
62         rm -f $(OBJS)
63         rm -f $(LOBJS)
64         rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
65         rm -f $(ALL_TOOLS)
66         rm -f $(GENH) 
67         rm -f include/bits
68
69 distclean: clean
70         rm -f config.mak
71
72 include/bits:
73         @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
74         ln -sf ../arch/$(ARCH)/bits $@
75
76 include/bits/alltypes.h.in: include/bits
77
78 include/bits/alltypes.h: include/bits/alltypes.h.in include/alltypes.h.in tools/mkalltypes.sed
79         sed -f tools/mkalltypes.sed include/bits/alltypes.h.in include/alltypes.h.in > $@
80
81 src/ldso/dynlink.lo: arch/$(ARCH)/reloc.h
82
83 crt/crt1.o crt/Scrt1.o: $(wildcard arch/$(ARCH)/crt_arch.h)
84
85 crt/Scrt1.o: CFLAGS += -fPIC
86
87 OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=src/%))
88 $(OPTIMIZE_SRCS:%.c=%.o) $(OPTIMIZE_SRCS:%.c=%.lo): CFLAGS += -O3
89
90 MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
91 $(MEMOPS_SRCS:%.c=%.o) $(MEMOPS_SRCS:%.c=%.lo): CFLAGS += $(CFLAGS_MEMOPS)
92
93 # This incantation ensures that changes to any subarch asm files will
94 # force the corresponding object file to be rebuilt, even if the implicit
95 # rule below goes indirectly through a .sub file.
96 define mkasmdep
97 $(dir $(patsubst %/,%,$(dir $(1))))$(notdir $(1:.s=.o)): $(1)
98 endef
99 $(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s))))
100
101 %.o: $(ARCH)$(ASMSUBARCH)/%.sub
102         $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<)
103
104 %.o: $(ARCH)/%.s
105         $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
106
107 %.o: %.c $(GENH) $(IMPH)
108         $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
109
110 %.lo: $(ARCH)$(ASMSUBARCH)/%.sub
111         $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<)
112
113 %.lo: $(ARCH)/%.s
114         $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
115
116 %.lo: %.c $(GENH) $(IMPH)
117         $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
118
119 lib/libc.so: $(LOBJS)
120         $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS) -nostdlib -shared \
121         -Wl,-e,_start -Wl,-Bsymbolic-functions \
122         -o $@ $(LOBJS) $(LIBCC)
123
124 lib/libc.a: $(OBJS)
125         rm -f $@
126         $(AR) rc $@ $(OBJS)
127         $(RANLIB) $@
128
129 $(EMPTY_LIBS):
130         rm -f $@
131         $(AR) rc $@
132
133 lib/%.o: crt/%.o
134         cp $< $@
135
136 lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
137         sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
138
139 tools/musl-gcc: config.mak
140         printf '#!/bin/sh\nexec "$${REALGCC:-gcc}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
141         chmod +x $@
142
143 $(DESTDIR)$(bindir)/%: tools/%
144         install -D $< $@
145
146 $(DESTDIR)$(libdir)/%.so: lib/%.so
147         install -D -m 755 $< $@
148
149 $(DESTDIR)$(libdir)/%: lib/%
150         install -D -m 644 $< $@
151
152 $(DESTDIR)$(includedir)/bits/%: arch/$(ARCH)/bits/%
153         install -D -m 644 $< $@
154
155 $(DESTDIR)$(includedir)/%: include/%
156         install -D -m 644 $< $@
157
158 $(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(syslibdir)
159         ln -sf $(libdir)/libc.so $@ || true
160
161 $(DESTDIR)$(syslibdir):
162         install -d -m 755 $(DESTDIR)$(syslibdir)
163
164 install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
165
166 install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
167
168 install-tools: $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%)
169
170
171
172 .PRECIOUS: $(CRT_LIBS:lib/%=crt/%)
173
174 .PHONY: all clean install install-libs install-headers install-tools