• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
11srcdir = .
12exec_prefix = /usr/local
13bindir = $(exec_prefix)/bin
14
15prefix = /usr/local/musl
16includedir = $(prefix)/include
17libdir = $(prefix)/lib
18syslibdir = /lib
19
20MALLOC_DIR = mallocng
21SRC_DIRS = $(addprefix $(srcdir)/,src/* src/malloc/$(MALLOC_DIR) crt ldso $(COMPAT_SRC_DIRS))
22BASE_GLOBS = $(addsuffix /*.c,$(SRC_DIRS))
23ARCH_GLOBS = $(addsuffix /$(ARCH)/*.[csS],$(SRC_DIRS))
24BASE_SRCS = $(sort $(wildcard $(BASE_GLOBS)))
25ARCH_SRCS = $(sort $(wildcard $(ARCH_GLOBS)))
26BASE_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(BASE_SRCS)))
27ARCH_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(ARCH_SRCS)))
28REPLACED_OBJS = $(sort $(subst /$(ARCH)/,/,$(ARCH_OBJS)))
29ALL_OBJS = $(addprefix obj/, $(filter-out $(REPLACED_OBJS), $(sort $(BASE_OBJS) $(ARCH_OBJS))))
30
31LIBC_OBJS = $(filter obj/src/%,$(ALL_OBJS)) $(filter obj/compat/%,$(ALL_OBJS))
32LDSO_OBJS = $(filter obj/ldso/%,$(ALL_OBJS:%.o=%.lo))
33CRT_OBJS = $(filter obj/crt/%,$(ALL_OBJS))
34
35AOBJS = $(LIBC_OBJS)
36LOBJS = $(LIBC_OBJS:.o=.lo)
37GENH = obj/include/bits/alltypes.h obj/include/bits/syscall.h
38GENH_INT = obj/src/internal/version.h
39IMPH = $(addprefix $(srcdir)/, src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/locale_impl.h src/internal/libc.h)
40
41LDFLAGS =
42LDFLAGS_AUTO =
43LIBCC = -lgcc
44CPPFLAGS =
45CFLAGS =
46CFLAGS_AUTO = -Os -pipe
47CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc
48
49CFLAGS_ALL = $(CFLAGS_C99FSE)
50CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I$(srcdir)/arch/$(ARCH) -I$(srcdir)/arch/generic -Iobj/src/internal -I$(srcdir)/src/include -I$(srcdir)/src/internal -Iobj/include -I$(srcdir)/include
51CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS_AUTO) $(CFLAGS)
52
53LDFLAGS_ALL = $(LDFLAGS_AUTO) $(LDFLAGS)
54
55AR      = $(CROSS_COMPILE)ar
56RANLIB  = $(CROSS_COMPILE)ranlib
57INSTALL = $(srcdir)/tools/install.sh
58
59ARCH_INCLUDES = $(wildcard $(srcdir)/arch/$(ARCH)/bits/*.h)
60GENERIC_INCLUDES = $(wildcard $(srcdir)/arch/generic/bits/*.h)
61INCLUDES = $(wildcard $(srcdir)/include/*.h $(srcdir)/include/*/*.h)
62ALL_INCLUDES = $(sort $(INCLUDES:$(srcdir)/%=%) $(GENH:obj/%=%) $(ARCH_INCLUDES:$(srcdir)/arch/$(ARCH)/%=include/%) $(GENERIC_INCLUDES:$(srcdir)/arch/generic/%=include/%))
63
64EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
65EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
66CRT_LIBS = $(addprefix lib/,$(notdir $(CRT_OBJS)))
67STATIC_LIBS = lib/libc.a
68SHARED_LIBS = lib/libc.so
69TOOL_LIBS = lib/musl-gcc.specs
70ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
71ALL_TOOLS = obj/musl-gcc
72
73WRAPCC_GCC = gcc
74WRAPCC_CLANG = clang
75
76LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
77
78-include config.mak
79-include $(srcdir)/arch/$(ARCH)/arch.mak
80
81ifeq ($(ARCH),)
82
83all:
84	@echo "Please set ARCH in config.mak before running make."
85	@exit 1
86
87else
88
89all: $(ALL_LIBS) $(ALL_TOOLS)
90
91OBJ_DIRS = $(sort $(patsubst %/,%,$(dir $(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(GENH) $(GENH_INT))) obj/include)
92
93$(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(ALL_OBJS:%.o=%.lo) $(GENH) $(GENH_INT): | $(OBJ_DIRS)
94
95$(OBJ_DIRS):
96	mkdir -p $@
97
98obj/include/bits/alltypes.h: $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in $(srcdir)/tools/mkalltypes.sed
99	sed -f $(srcdir)/tools/mkalltypes.sed $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in > $@
100
101obj/include/bits/syscall.h: $(srcdir)/arch/$(ARCH)/bits/syscall.h.in
102	cp $< $@
103	sed -n -e s/__NR_/SYS_/p < $< >> $@
104
105obj/src/internal/version.h: $(wildcard $(srcdir)/VERSION $(srcdir)/.git)
106	printf '#define VERSION "%s"\n' "$$(cd $(srcdir); sh tools/version.sh)" > $@
107
108obj/src/internal/version.o obj/src/internal/version.lo: obj/src/internal/version.h
109
110obj/crt/rcrt1.o obj/ldso/dlstart.lo obj/ldso/dynlink.lo: $(srcdir)/src/internal/dynlink.h $(srcdir)/arch/$(ARCH)/reloc.h
111
112obj/crt/crt1.o obj/crt/scrt1.o obj/crt/rcrt1.o obj/ldso/dlstart.lo: $(srcdir)/arch/$(ARCH)/crt_arch.h
113
114obj/crt/rcrt1.o: $(srcdir)/ldso/dlstart.c
115
116obj/crt/Scrt1.o obj/crt/rcrt1.o: CFLAGS_ALL += -fPIC
117
118OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=$(srcdir)/src/%))
119$(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.o) $(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.lo): CFLAGS += -O3
120
121MEMOPS_OBJS = $(filter %/memcpy.o %/memmove.o %/memcmp.o %/memset.o, $(LIBC_OBJS))
122$(MEMOPS_OBJS) $(MEMOPS_OBJS:%.o=%.lo): CFLAGS_ALL += $(CFLAGS_MEMOPS)
123
124NOSSP_OBJS = $(CRT_OBJS) $(LDSO_OBJS) $(filter \
125	%/__libc_start_main.o %/__init_tls.o %/__stack_chk_fail.o \
126	%/__set_thread_area.o %/memset.o %/memcpy.o \
127	, $(LIBC_OBJS))
128$(NOSSP_OBJS) $(NOSSP_OBJS:%.o=%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP)
129
130$(CRT_OBJS): CFLAGS_ALL += -DCRT
131
132$(LOBJS) $(LDSO_OBJS): CFLAGS_ALL += -fPIC
133
134CC_CMD = $(CC) $(CFLAGS_ALL) -c -o $@ $<
135
136# Choose invocation of assembler to be used
137ifeq ($(ADD_CFI),yes)
138	AS_CMD = LC_ALL=C awk -f $(srcdir)/tools/add-cfi.common.awk -f $(srcdir)/tools/add-cfi.$(ARCH).awk $< | $(CC) $(CFLAGS_ALL) -x assembler -c -o $@ -
139else
140	AS_CMD = $(CC_CMD)
141endif
142
143obj/%.o: $(srcdir)/%.s
144	$(AS_CMD)
145
146obj/%.o: $(srcdir)/%.S
147	$(CC_CMD)
148
149obj/%.o: $(srcdir)/%.c $(GENH) $(IMPH)
150	$(CC_CMD)
151
152obj/%.lo: $(srcdir)/%.s
153	$(AS_CMD)
154
155obj/%.lo: $(srcdir)/%.S
156	$(CC_CMD)
157
158obj/%.lo: $(srcdir)/%.c $(GENH) $(IMPH)
159	$(CC_CMD)
160
161lib/libc.so: $(LOBJS) $(LDSO_OBJS)
162	$(CC) $(CFLAGS_ALL) $(LDFLAGS_ALL) -nostdlib -shared \
163	-Wl,-e,_dlstart -o $@ $(LOBJS) $(LDSO_OBJS) $(LIBCC)
164
165lib/libc.a: $(AOBJS)
166	rm -f $@
167	$(AR) rc $@ $(AOBJS)
168	$(RANLIB) $@
169
170$(EMPTY_LIBS):
171	rm -f $@
172	$(AR) rc $@
173
174lib/%.o: obj/crt/$(ARCH)/%.o
175	cp $< $@
176
177lib/%.o: obj/crt/%.o
178	cp $< $@
179
180lib/musl-gcc.specs: $(srcdir)/tools/musl-gcc.specs.sh config.mak
181	sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
182
183obj/musl-gcc: config.mak
184	printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
185	chmod +x $@
186
187obj/%-clang: $(srcdir)/tools/%-clang.in config.mak
188	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' $< > $@
189	chmod +x $@
190
191$(DESTDIR)$(bindir)/%: obj/%
192	$(INSTALL) -D $< $@
193
194$(DESTDIR)$(libdir)/%.so: lib/%.so
195	$(INSTALL) -D -m 755 $< $@
196
197$(DESTDIR)$(libdir)/%: lib/%
198	$(INSTALL) -D -m 644 $< $@
199
200$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/$(ARCH)/bits/%
201	$(INSTALL) -D -m 644 $< $@
202
203$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/generic/bits/%
204	$(INSTALL) -D -m 644 $< $@
205
206$(DESTDIR)$(includedir)/bits/%: obj/include/bits/%
207	$(INSTALL) -D -m 644 $< $@
208
209$(DESTDIR)$(includedir)/%: $(srcdir)/include/%
210	$(INSTALL) -D -m 644 $< $@
211
212$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
213	$(INSTALL) -D -l $(libdir)/libc.so $@ || true
214
215install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
216
217install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
218
219install-tools: $(ALL_TOOLS:obj/%=$(DESTDIR)$(bindir)/%)
220
221install: install-libs install-headers install-tools
222
223musl-git-%.tar.gz: .git
224	 git --git-dir=$(srcdir)/.git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@)
225
226musl-%.tar.gz: .git
227	 git --git-dir=$(srcdir)/.git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@)
228
229endif
230
231clean:
232	rm -rf obj lib
233
234distclean: clean
235	rm -f config.mak
236
237.PHONY: all clean install install-libs install-headers install-tools
238