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