blob: 308ddaae582e443167ef87f687e48c26d1eb6fe1 [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 -0800116OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=$(srcdir)/src/%))
117$(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.o) $(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.lo): CFLAGS += -O3
Rich Felkera80847d2013-07-22 21:22:04 -0400118
Rich Felker4a1f55e2013-08-01 17:12:23 -0400119MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
Petr Hosek2f853dd2015-11-18 12:07:32 -0800120$(MEMOPS_SRCS:%.c=obj/%.o) $(MEMOPS_SRCS:%.c=obj/%.lo): CFLAGS_ALL += $(CFLAGS_MEMOPS)
Rich Felker4a1f55e2013-08-01 17:12:23 -0400121
Rich Felker1ef849c2015-04-13 20:13:10 -0400122NOSSP_SRCS = $(wildcard crt/*.c) \
123 src/env/__libc_start_main.c src/env/__init_tls.c \
Rich Felkerf9b8df42016-01-25 20:06:31 -0500124 src/env/__stack_chk_fail.c \
125 src/thread/__set_thread_area.c src/thread/$(ARCH)/__set_thread_area.c \
126 src/string/memset.c src/string/$(ARCH)/memset.c \
127 src/string/memcpy.c src/string/$(ARCH)/memcpy.c \
Rich Felker5552ce52016-01-25 19:29:55 -0500128 ldso/dlstart.c ldso/dynlink.c
Petr Hosek2f853dd2015-11-18 12:07:32 -0800129$(NOSSP_SRCS:%.c=obj/%.o) $(NOSSP_SRCS:%.c=obj/%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP)
Rich Felker1ef849c2015-04-13 20:13:10 -0400130
Rich Felker63bcda42016-02-17 13:56:27 -0500131$(CRT_OBJS): CFLAGS_ALL += -DCRT
Rich Felkerde2b67f2015-04-19 22:05:29 -0400132
Rich Felker5552ce52016-01-25 19:29:55 -0500133$(LOBJS) $(LDSO_OBJS): CFLAGS_ALL += -fPIC
Rich Felker9514e702016-01-19 19:08:52 -0500134
Rich Felkerada47982016-01-20 02:58:29 +0000135CC_CMD = $(CC) $(CFLAGS_ALL) -c -o $@ $<
136
Alex Dowad35b33122015-07-10 15:03:24 +0200137# Choose invocation of assembler to be used
Alex Dowad35b33122015-07-10 15:03:24 +0200138ifeq ($(ADD_CFI),yes)
Rich Felker9514e702016-01-19 19:08:52 -0500139 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 +0200140else
Rich Felkerada47982016-01-20 02:58:29 +0000141 AS_CMD = $(CC_CMD)
Alex Dowad35b33122015-07-10 15:03:24 +0200142endif
143
Petr Hosek2f853dd2015-11-18 12:07:32 -0800144obj/%.o: $(srcdir)/%.s
Rich Felkerada47982016-01-20 02:58:29 +0000145 $(AS_CMD)
Rich Felker0b44a032011-02-12 00:22:29 -0500146
Rich Felker9f5eb772016-01-19 18:19:01 -0500147obj/%.o: $(srcdir)/%.S
Rich Felkerada47982016-01-20 02:58:29 +0000148 $(CC_CMD)
Rich Felker9f5eb772016-01-19 18:19:01 -0500149
Petr Hosek2f853dd2015-11-18 12:07:32 -0800150obj/%.o: $(srcdir)/%.c $(GENH) $(IMPH)
Rich Felkerada47982016-01-20 02:58:29 +0000151 $(CC_CMD)
Rich Felker0b44a032011-02-12 00:22:29 -0500152
Petr Hosek2f853dd2015-11-18 12:07:32 -0800153obj/%.lo: $(srcdir)/%.s
Rich Felkerada47982016-01-20 02:58:29 +0000154 $(AS_CMD)
Rich Felker0b44a032011-02-12 00:22:29 -0500155
Rich Felker9f5eb772016-01-19 18:19:01 -0500156obj/%.lo: $(srcdir)/%.S
Rich Felkerada47982016-01-20 02:58:29 +0000157 $(CC_CMD)
Rich Felker9f5eb772016-01-19 18:19:01 -0500158
Petr Hosek2f853dd2015-11-18 12:07:32 -0800159obj/%.lo: $(srcdir)/%.c $(GENH) $(IMPH)
Rich Felkerada47982016-01-20 02:58:29 +0000160 $(CC_CMD)
Rich Felker0b44a032011-02-12 00:22:29 -0500161
Rich Felker5552ce52016-01-25 19:29:55 -0500162lib/libc.so: $(LOBJS) $(LDSO_OBJS)
Rich Felker9514e702016-01-19 19:08:52 -0500163 $(CC) $(CFLAGS_ALL) $(LDFLAGS_ALL) -nostdlib -shared \
Rich Felker65498f22016-01-31 00:40:33 -0500164 -Wl,-e,_dlstart -o $@ $(LOBJS) $(LDSO_OBJS) $(LIBCC)
Rich Felker0b44a032011-02-12 00:22:29 -0500165
Rich Felker16191272016-01-25 19:57:38 -0500166lib/libc.a: $(AOBJS)
Rich Felker0b44a032011-02-12 00:22:29 -0500167 rm -f $@
Rich Felker16191272016-01-25 19:57:38 -0500168 $(AR) rc $@ $(AOBJS)
Rich Felker0b44a032011-02-12 00:22:29 -0500169 $(RANLIB) $@
170
Rich Felker4fd15952011-02-17 17:12:52 -0500171$(EMPTY_LIBS):
172 rm -f $@
Rich Felker0b44a032011-02-12 00:22:29 -0500173 $(AR) rc $@
174
Rich Felker63bcda42016-02-17 13:56:27 -0500175lib/%.o: obj/crt/$(ARCH)/%.o
176 cp $< $@
177
Petr Hosek2f853dd2015-11-18 12:07:32 -0800178lib/%.o: obj/crt/%.o
Rich Felker0b44a032011-02-12 00:22:29 -0500179 cp $< $@
180
Petr Hosek2f853dd2015-11-18 12:07:32 -0800181lib/musl-gcc.specs: $(srcdir)/tools/musl-gcc.specs.sh config.mak
Rich Felker58f430c2012-04-22 14:32:49 -0400182 sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
183
Petr Hosek2f853dd2015-11-18 12:07:32 -0800184obj/musl-gcc: config.mak
Shizb3cd7d12015-06-28 23:08:19 +0200185 printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
Rich Felker0b44a032011-02-12 00:22:29 -0500186 chmod +x $@
187
Petr Hosek2f853dd2015-11-18 12:07:32 -0800188obj/%-clang: $(srcdir)/tools/%-clang.in config.mak
Shizfb585452015-06-28 23:08:21 +0200189 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' $< > $@
190 chmod +x $@
191
Petr Hosek2f853dd2015-11-18 12:07:32 -0800192$(DESTDIR)$(bindir)/%: obj/%
Rich Felkere678fc62013-08-17 22:21:11 -0400193 $(INSTALL) -D $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500194
Rich Felker207c45d2011-06-27 21:38:11 -0400195$(DESTDIR)$(libdir)/%.so: lib/%.so
Rich Felkere678fc62013-08-17 22:21:11 -0400196 $(INSTALL) -D -m 755 $< $@
Rich Felker207c45d2011-06-27 21:38:11 -0400197
Rich Felkerec05a0b2011-06-23 22:13:47 -0400198$(DESTDIR)$(libdir)/%: lib/%
Rich Felkere678fc62013-08-17 22:21:11 -0400199 $(INSTALL) -D -m 644 $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500200
Petr Hosek2f853dd2015-11-18 12:07:32 -0800201$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/$(ARCH)/bits/%
Rich Felkere678fc62013-08-17 22:21:11 -0400202 $(INSTALL) -D -m 644 $< $@
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400203
Rich Felkerefdf04c2016-01-27 19:31:15 -0500204$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/generic/bits/%
205 $(INSTALL) -D -m 644 $< $@
206
Petr Hosek2f853dd2015-11-18 12:07:32 -0800207$(DESTDIR)$(includedir)/bits/%: obj/include/bits/%
208 $(INSTALL) -D -m 644 $< $@
209
210$(DESTDIR)$(includedir)/%: $(srcdir)/include/%
Rich Felkere678fc62013-08-17 22:21:11 -0400211 $(INSTALL) -D -m 644 $< $@
Rich Felkerec05a0b2011-06-23 22:13:47 -0400212
Rich Felker82fa6b42013-08-16 17:51:38 -0400213$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
Rich Felker9ff8ed42013-08-31 11:36:56 -0400214 $(INSTALL) -D -l $(libdir)/libc.so $@ || true
Rich Felkerf8e054f2012-05-04 21:54:57 -0400215
Rich Felker07809362012-08-17 19:32:24 -0400216install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
217
218install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
219
Petr Hosek2f853dd2015-11-18 12:07:32 -0800220install-tools: $(ALL_TOOLS:obj/%=$(DESTDIR)$(bindir)/%)
Rich Felker07809362012-08-17 19:32:24 -0400221
Rich Felker157e2842016-02-17 16:11:45 -0500222install: install-libs install-headers install-tools
223
Rich Felkerf8071072014-06-25 16:14:37 -0400224musl-git-%.tar.gz: .git
Petr Hosek2f853dd2015-11-18 12:07:32 -0800225 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 -0400226
Rich Felkerf8071072014-06-25 16:14:37 -0400227musl-%.tar.gz: .git
Petr Hosek2f853dd2015-11-18 12:07:32 -0800228 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 -0400229
Rich Felker157e2842016-02-17 16:11:45 -0500230endif
231
232clean:
233 rm -rf obj lib
234
235distclean: clean
236 rm -f config.mak
237
Rich Felker07809362012-08-17 19:32:24 -0400238.PHONY: all clean install install-libs install-headers install-tools