blob: d2e8997906f1883a4bad614e3feb1314ac41157f [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#
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
Petr Hosek2f853dd2015-11-18 12:07:32 -080011srcdir = .
Rich Felker0b44a032011-02-12 00:22:29 -050012exec_prefix = /usr/local
13bindir = $(exec_prefix)/bin
14
15prefix = /usr/local/musl
16includedir = $(prefix)/include
17libdir = $(prefix)/lib
Rich Felker207c45d2011-06-27 21:38:11 -040018syslibdir = /lib
Rich Felker0b44a032011-02-12 00:22:29 -050019
Rich Felker63bcda42016-02-17 13:56:27 -050020SRC_DIRS = $(addprefix $(srcdir)/,src/* crt ldso)
21BASE_GLOBS = $(addsuffix /*.c,$(SRC_DIRS))
22ARCH_GLOBS = $(addsuffix /$(ARCH)/*.[csS],$(SRC_DIRS))
23BASE_SRCS = $(sort $(wildcard $(BASE_GLOBS)))
24ARCH_SRCS = $(sort $(wildcard $(ARCH_GLOBS)))
Rich Felker9f5eb772016-01-19 18:19:01 -050025BASE_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(BASE_SRCS)))
Rich Felker9f5eb772016-01-19 18:19:01 -050026ARCH_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(ARCH_SRCS)))
Rich Felker0f814a42016-01-20 02:49:32 +000027REPLACED_OBJS = $(sort $(subst /$(ARCH)/,/,$(ARCH_OBJS)))
Rich Felker63bcda42016-02-17 13:56:27 -050028ALL_OBJS = $(addprefix obj/, $(filter-out $(REPLACED_OBJS), $(sort $(BASE_OBJS) $(ARCH_OBJS))))
29
30LIBC_OBJS = $(filter obj/src/%,$(ALL_OBJS))
31LDSO_OBJS = $(filter obj/ldso/%,$(ALL_OBJS:%.o=%.lo))
32CRT_OBJS = $(filter obj/crt/%,$(ALL_OBJS))
33
34AOBJS = $(LIBC_OBJS)
35LOBJS = $(LIBC_OBJS:.o=.lo)
Bobby Bingham63e3a162016-05-12 00:34:05 -050036GENH = obj/include/bits/alltypes.h obj/include/bits/syscall.h
Petr Hosek2f853dd2015-11-18 12:07:32 -080037GENH_INT = obj/src/internal/version.h
Rich Felkere6917ec2017-03-21 12:13:59 -040038IMPH = $(addprefix $(srcdir)/, src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/locale_impl.h src/internal/libc.h)
Rich Felker0b44a032011-02-12 00:22:29 -050039
Rich Felker4cd8b472015-11-02 16:58:14 -050040LDFLAGS =
41LDFLAGS_AUTO =
Rich Felker1b231452012-08-02 21:05:43 -040042LIBCC = -lgcc
Rich Felkerd76eafc2012-05-01 19:30:03 -040043CPPFLAGS =
Rich Felker4cd8b472015-11-02 16:58:14 -050044CFLAGS =
45CFLAGS_AUTO = -Os -pipe
Rich Felkerd76eafc2012-05-01 19:30:03 -040046CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc
47
48CFLAGS_ALL = $(CFLAGS_C99FSE)
Rich Felkerefdf04c2016-01-27 19:31:15 -050049CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I$(srcdir)/arch/$(ARCH) -I$(srcdir)/arch/generic -Iobj/src/internal -I$(srcdir)/src/internal -Iobj/include -I$(srcdir)/include
Rich Felker4cd8b472015-11-02 16:58:14 -050050CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS_AUTO) $(CFLAGS)
Rich Felkerd76eafc2012-05-01 19:30:03 -040051
Rich Felker4cd8b472015-11-02 16:58:14 -050052LDFLAGS_ALL = $(LDFLAGS_AUTO) $(LDFLAGS)
53
Rich Felker0b44a032011-02-12 00:22:29 -050054AR = $(CROSS_COMPILE)ar
55RANLIB = $(CROSS_COMPILE)ranlib
Petr Hosek2f853dd2015-11-18 12:07:32 -080056INSTALL = $(srcdir)/tools/install.sh
Rich Felker0b44a032011-02-12 00:22:29 -050057
Petr Hosek2f853dd2015-11-18 12:07:32 -080058ARCH_INCLUDES = $(wildcard $(srcdir)/arch/$(ARCH)/bits/*.h)
Rich Felkerefdf04c2016-01-27 19:31:15 -050059GENERIC_INCLUDES = $(wildcard $(srcdir)/arch/generic/bits/*.h)
Petr Hosek2f853dd2015-11-18 12:07:32 -080060INCLUDES = $(wildcard $(srcdir)/include/*.h $(srcdir)/include/*/*.h)
Rich Felkerefdf04c2016-01-27 19:31:15 -050061ALL_INCLUDES = $(sort $(INCLUDES:$(srcdir)/%=%) $(GENH:obj/%=%) $(ARCH_INCLUDES:$(srcdir)/arch/$(ARCH)/%=include/%) $(GENERIC_INCLUDES:$(srcdir)/arch/generic/%=include/%))
Rich Felker0b44a032011-02-12 00:22:29 -050062
Rich Felker41d51832011-02-24 16:37:21 -050063EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
Rich Felker0b44a032011-02-12 00:22:29 -050064EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
Rich Felker8b084f02016-02-19 14:16:33 -050065CRT_LIBS = $(addprefix lib/,$(notdir $(CRT_OBJS)))
Rich Felkerd76eafc2012-05-01 19:30:03 -040066STATIC_LIBS = lib/libc.a
Rich Felker207c45d2011-06-27 21:38:11 -040067SHARED_LIBS = lib/libc.so
Rich Felker58f430c2012-04-22 14:32:49 -040068TOOL_LIBS = lib/musl-gcc.specs
Rich Felkerd76eafc2012-05-01 19:30:03 -040069ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
Petr Hosek2f853dd2015-11-18 12:07:32 -080070ALL_TOOLS = obj/musl-gcc
Rich Felker0b44a032011-02-12 00:22:29 -050071
Shizb3cd7d12015-06-28 23:08:19 +020072WRAPCC_GCC = gcc
Shizfb585452015-06-28 23:08:21 +020073WRAPCC_CLANG = clang
Shizb3cd7d12015-06-28 23:08:19 +020074
Rich Felker3e7f1862013-07-18 20:30:58 -040075LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
Rich Felker207c45d2011-06-27 21:38:11 -040076
Rich Felker0b44a032011-02-12 00:22:29 -050077-include config.mak
78
Petr Hosek2f853dd2015-11-18 12:07:32 -080079ifeq ($(ARCH),)
Rich Felker157e2842016-02-17 16:11:45 -050080
81all:
82 @echo "Please set ARCH in config.mak before running make."
83 @exit 1
84
85else
Petr Hosek2f853dd2015-11-18 12:07:32 -080086
Rich Felker8d546812011-06-28 08:27:38 -040087all: $(ALL_LIBS) $(ALL_TOOLS)
Rich Felker0b44a032011-02-12 00:22:29 -050088
Rich Felker63bcda42016-02-17 13:56:27 -050089OBJ_DIRS = $(sort $(patsubst %/,%,$(dir $(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(GENH) $(GENH_INT))) obj/include)
Petr Hosek2f853dd2015-11-18 12:07:32 -080090
Rich Felker63bcda42016-02-17 13:56:27 -050091$(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(ALL_OBJS:%.o=%.lo) $(GENH) $(GENH_INT): | $(OBJ_DIRS)
Petr Hosek2f853dd2015-11-18 12:07:32 -080092
93$(OBJ_DIRS):
94 mkdir -p $@
95
Petr Hosek2f853dd2015-11-18 12:07:32 -080096obj/include/bits/alltypes.h: $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in $(srcdir)/tools/mkalltypes.sed
97 sed -f $(srcdir)/tools/mkalltypes.sed $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in > $@
Rich Felker1355fdc2011-02-15 00:33:23 -050098
Bobby Bingham63e3a162016-05-12 00:34:05 -050099obj/include/bits/syscall.h: $(srcdir)/arch/$(ARCH)/bits/syscall.h.in
100 cp $< $@
101 sed -n -e s/__NR_/SYS_/p < $< >> $@
102
Petr Hosek2f853dd2015-11-18 12:07:32 -0800103obj/src/internal/version.h: $(wildcard $(srcdir)/VERSION $(srcdir)/.git)
104 printf '#define VERSION "%s"\n' "$$(cd $(srcdir); sh tools/version.sh)" > $@
Rich Felker0b44a032011-02-12 00:22:29 -0500105
Petr Hosek2f853dd2015-11-18 12:07:32 -0800106obj/src/internal/version.o obj/src/internal/version.lo: obj/src/internal/version.h
Rich Felker0b44a032011-02-12 00:22:29 -0500107
Rich Felker5552ce52016-01-25 19:29:55 -0500108obj/crt/rcrt1.o obj/ldso/dlstart.lo obj/ldso/dynlink.lo: $(srcdir)/src/internal/dynlink.h $(srcdir)/arch/$(ARCH)/reloc.h
Rich Felker179ab5a2013-12-01 17:27:25 -0500109
Rich Felker5552ce52016-01-25 19:29:55 -0500110obj/crt/crt1.o obj/crt/scrt1.o obj/crt/rcrt1.o obj/ldso/dlstart.lo: $(srcdir)/arch/$(ARCH)/crt_arch.h
Rich Felker179ab5a2013-12-01 17:27:25 -0500111
Rich Felker5552ce52016-01-25 19:29:55 -0500112obj/crt/rcrt1.o: $(srcdir)/ldso/dlstart.c
Rich Felkerd0437802012-08-05 12:32:20 -0400113
Petr Hosek2f853dd2015-11-18 12:07:32 -0800114obj/crt/Scrt1.o obj/crt/rcrt1.o: CFLAGS_ALL += -fPIC
Rich Felker585ba142015-06-03 02:00:44 -0400115
Petr Hosek2f853dd2015-11-18 12:07:32 -0800116obj/crt/$(ARCH)/crti.o: $(srcdir)/crt/$(ARCH)/crti.s
Rich Felkerc5e34da2013-07-26 01:49:14 -0400117
Petr Hosek2f853dd2015-11-18 12:07:32 -0800118obj/crt/$(ARCH)/crtn.o: $(srcdir)/crt/$(ARCH)/crtn.s
Rich Felkerc5e34da2013-07-26 01:49:14 -0400119
Petr Hosek2f853dd2015-11-18 12:07:32 -0800120OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=$(srcdir)/src/%))
121$(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.o) $(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.lo): CFLAGS += -O3
Rich Felkera80847d2013-07-22 21:22:04 -0400122
Rich Felker4a1f55e2013-08-01 17:12:23 -0400123MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
Petr Hosek2f853dd2015-11-18 12:07:32 -0800124$(MEMOPS_SRCS:%.c=obj/%.o) $(MEMOPS_SRCS:%.c=obj/%.lo): CFLAGS_ALL += $(CFLAGS_MEMOPS)
Rich Felker4a1f55e2013-08-01 17:12:23 -0400125
Rich Felker1ef849c2015-04-13 20:13:10 -0400126NOSSP_SRCS = $(wildcard crt/*.c) \
127 src/env/__libc_start_main.c src/env/__init_tls.c \
Rich Felkerf9b8df42016-01-25 20:06:31 -0500128 src/env/__stack_chk_fail.c \
129 src/thread/__set_thread_area.c src/thread/$(ARCH)/__set_thread_area.c \
130 src/string/memset.c src/string/$(ARCH)/memset.c \
131 src/string/memcpy.c src/string/$(ARCH)/memcpy.c \
Rich Felker5552ce52016-01-25 19:29:55 -0500132 ldso/dlstart.c ldso/dynlink.c
Petr Hosek2f853dd2015-11-18 12:07:32 -0800133$(NOSSP_SRCS:%.c=obj/%.o) $(NOSSP_SRCS:%.c=obj/%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP)
Rich Felker1ef849c2015-04-13 20:13:10 -0400134
Rich Felker63bcda42016-02-17 13:56:27 -0500135$(CRT_OBJS): CFLAGS_ALL += -DCRT
Rich Felkerde2b67f2015-04-19 22:05:29 -0400136
Rich Felker5552ce52016-01-25 19:29:55 -0500137$(LOBJS) $(LDSO_OBJS): CFLAGS_ALL += -fPIC
Rich Felker9514e702016-01-19 19:08:52 -0500138
Rich Felkerada47982016-01-20 02:58:29 +0000139CC_CMD = $(CC) $(CFLAGS_ALL) -c -o $@ $<
140
Alex Dowad35b33122015-07-10 15:03:24 +0200141# Choose invocation of assembler to be used
Alex Dowad35b33122015-07-10 15:03:24 +0200142ifeq ($(ADD_CFI),yes)
Rich Felker9514e702016-01-19 19:08:52 -0500143 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 $@ -
Alex Dowad35b33122015-07-10 15:03:24 +0200144else
Rich Felkerada47982016-01-20 02:58:29 +0000145 AS_CMD = $(CC_CMD)
Alex Dowad35b33122015-07-10 15:03:24 +0200146endif
147
Petr Hosek2f853dd2015-11-18 12:07:32 -0800148obj/%.o: $(srcdir)/%.s
Rich Felkerada47982016-01-20 02:58:29 +0000149 $(AS_CMD)
Rich Felker0b44a032011-02-12 00:22:29 -0500150
Rich Felker9f5eb772016-01-19 18:19:01 -0500151obj/%.o: $(srcdir)/%.S
Rich Felkerada47982016-01-20 02:58:29 +0000152 $(CC_CMD)
Rich Felker9f5eb772016-01-19 18:19:01 -0500153
Petr Hosek2f853dd2015-11-18 12:07:32 -0800154obj/%.o: $(srcdir)/%.c $(GENH) $(IMPH)
Rich Felkerada47982016-01-20 02:58:29 +0000155 $(CC_CMD)
Rich Felker0b44a032011-02-12 00:22:29 -0500156
Petr Hosek2f853dd2015-11-18 12:07:32 -0800157obj/%.lo: $(srcdir)/%.s
Rich Felkerada47982016-01-20 02:58:29 +0000158 $(AS_CMD)
Rich Felker0b44a032011-02-12 00:22:29 -0500159
Rich Felker9f5eb772016-01-19 18:19:01 -0500160obj/%.lo: $(srcdir)/%.S
Rich Felkerada47982016-01-20 02:58:29 +0000161 $(CC_CMD)
Rich Felker9f5eb772016-01-19 18:19:01 -0500162
Petr Hosek2f853dd2015-11-18 12:07:32 -0800163obj/%.lo: $(srcdir)/%.c $(GENH) $(IMPH)
Rich Felkerada47982016-01-20 02:58:29 +0000164 $(CC_CMD)
Rich Felker0b44a032011-02-12 00:22:29 -0500165
Rich Felker5552ce52016-01-25 19:29:55 -0500166lib/libc.so: $(LOBJS) $(LDSO_OBJS)
Rich Felker9514e702016-01-19 19:08:52 -0500167 $(CC) $(CFLAGS_ALL) $(LDFLAGS_ALL) -nostdlib -shared \
Rich Felker65498f22016-01-31 00:40:33 -0500168 -Wl,-e,_dlstart -o $@ $(LOBJS) $(LDSO_OBJS) $(LIBCC)
Rich Felker0b44a032011-02-12 00:22:29 -0500169
Rich Felker16191272016-01-25 19:57:38 -0500170lib/libc.a: $(AOBJS)
Rich Felker0b44a032011-02-12 00:22:29 -0500171 rm -f $@
Rich Felker16191272016-01-25 19:57:38 -0500172 $(AR) rc $@ $(AOBJS)
Rich Felker0b44a032011-02-12 00:22:29 -0500173 $(RANLIB) $@
174
Rich Felker4fd15952011-02-17 17:12:52 -0500175$(EMPTY_LIBS):
176 rm -f $@
Rich Felker0b44a032011-02-12 00:22:29 -0500177 $(AR) rc $@
178
Rich Felker63bcda42016-02-17 13:56:27 -0500179lib/%.o: obj/crt/$(ARCH)/%.o
180 cp $< $@
181
Petr Hosek2f853dd2015-11-18 12:07:32 -0800182lib/%.o: obj/crt/%.o
Rich Felker0b44a032011-02-12 00:22:29 -0500183 cp $< $@
184
Petr Hosek2f853dd2015-11-18 12:07:32 -0800185lib/musl-gcc.specs: $(srcdir)/tools/musl-gcc.specs.sh config.mak
Rich Felker58f430c2012-04-22 14:32:49 -0400186 sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
187
Petr Hosek2f853dd2015-11-18 12:07:32 -0800188obj/musl-gcc: config.mak
Shizb3cd7d12015-06-28 23:08:19 +0200189 printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
Rich Felker0b44a032011-02-12 00:22:29 -0500190 chmod +x $@
191
Petr Hosek2f853dd2015-11-18 12:07:32 -0800192obj/%-clang: $(srcdir)/tools/%-clang.in config.mak
Shizfb585452015-06-28 23:08:21 +0200193 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' $< > $@
194 chmod +x $@
195
Petr Hosek2f853dd2015-11-18 12:07:32 -0800196$(DESTDIR)$(bindir)/%: obj/%
Rich Felkere678fc62013-08-17 22:21:11 -0400197 $(INSTALL) -D $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500198
Rich Felker207c45d2011-06-27 21:38:11 -0400199$(DESTDIR)$(libdir)/%.so: lib/%.so
Rich Felkere678fc62013-08-17 22:21:11 -0400200 $(INSTALL) -D -m 755 $< $@
Rich Felker207c45d2011-06-27 21:38:11 -0400201
Rich Felkerec05a0b2011-06-23 22:13:47 -0400202$(DESTDIR)$(libdir)/%: lib/%
Rich Felkere678fc62013-08-17 22:21:11 -0400203 $(INSTALL) -D -m 644 $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500204
Petr Hosek2f853dd2015-11-18 12:07:32 -0800205$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/$(ARCH)/bits/%
Rich Felkere678fc62013-08-17 22:21:11 -0400206 $(INSTALL) -D -m 644 $< $@
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400207
Rich Felkerefdf04c2016-01-27 19:31:15 -0500208$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/generic/bits/%
209 $(INSTALL) -D -m 644 $< $@
210
Petr Hosek2f853dd2015-11-18 12:07:32 -0800211$(DESTDIR)$(includedir)/bits/%: obj/include/bits/%
212 $(INSTALL) -D -m 644 $< $@
213
214$(DESTDIR)$(includedir)/%: $(srcdir)/include/%
Rich Felkere678fc62013-08-17 22:21:11 -0400215 $(INSTALL) -D -m 644 $< $@
Rich Felkerec05a0b2011-06-23 22:13:47 -0400216
Rich Felker82fa6b42013-08-16 17:51:38 -0400217$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
Rich Felker9ff8ed42013-08-31 11:36:56 -0400218 $(INSTALL) -D -l $(libdir)/libc.so $@ || true
Rich Felkerf8e054f2012-05-04 21:54:57 -0400219
Rich Felker07809362012-08-17 19:32:24 -0400220install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
221
222install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
223
Petr Hosek2f853dd2015-11-18 12:07:32 -0800224install-tools: $(ALL_TOOLS:obj/%=$(DESTDIR)$(bindir)/%)
Rich Felker07809362012-08-17 19:32:24 -0400225
Rich Felker157e2842016-02-17 16:11:45 -0500226install: install-libs install-headers install-tools
227
Rich Felkerf8071072014-06-25 16:14:37 -0400228musl-git-%.tar.gz: .git
Petr Hosek2f853dd2015-11-18 12:07:32 -0800229 git --git-dir=$(srcdir)/.git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@)
Rich Felker07809362012-08-17 19:32:24 -0400230
Rich Felkerf8071072014-06-25 16:14:37 -0400231musl-%.tar.gz: .git
Petr Hosek2f853dd2015-11-18 12:07:32 -0800232 git --git-dir=$(srcdir)/.git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@)
Rich Felker07809362012-08-17 19:32:24 -0400233
Rich Felker157e2842016-02-17 16:11:45 -0500234endif
235
236clean:
237 rm -rf obj lib
238
239distclean: clean
240 rm -f config.mak
241
Rich Felker07809362012-08-17 19:32:24 -0400242.PHONY: all clean install install-libs install-headers install-tools