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