blob: 07336d27f0c8dfd3c68ebc53d66bd1342f415b97 [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
11exec_prefix = /usr/local
12bindir = $(exec_prefix)/bin
13
14prefix = /usr/local/musl
15includedir = $(prefix)/include
16libdir = $(prefix)/lib
Rich Felker207c45d2011-06-27 21:38:11 -040017syslibdir = /lib
Rich Felker0b44a032011-02-12 00:22:29 -050018
rofl0r158f7212013-02-06 02:11:49 +010019SRCS = $(sort $(wildcard src/*/*.c arch/$(ARCH)/src/*.c))
Rich Felker0b44a032011-02-12 00:22:29 -050020OBJS = $(SRCS:.c=.o)
21LOBJS = $(OBJS:.o=.lo)
22GENH = include/bits/alltypes.h
Rich Felker179ab5a2013-12-01 17:27:25 -050023GENH_INT = src/internal/version.h
Rich Felkere7652392012-05-03 20:35:11 -040024IMPH = src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/libc.h
Rich Felker0b44a032011-02-12 00:22:29 -050025
Rich Felkerd76eafc2012-05-01 19:30:03 -040026LDFLAGS =
Rich Felker1b231452012-08-02 21:05:43 -040027LIBCC = -lgcc
Rich Felkerd76eafc2012-05-01 19:30:03 -040028CPPFLAGS =
29CFLAGS = -Os -pipe
30CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc
31
32CFLAGS_ALL = $(CFLAGS_C99FSE)
Rich Felkerfcaec912012-07-11 02:44:14 -040033CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I./arch/$(ARCH) -I./src/internal -I./include
Rich Felkerd76eafc2012-05-01 19:30:03 -040034CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS)
35CFLAGS_ALL_STATIC = $(CFLAGS_ALL)
Rich Felker9f745742012-09-14 23:38:10 -040036CFLAGS_ALL_SHARED = $(CFLAGS_ALL) -fPIC -DSHARED
Rich Felkerd76eafc2012-05-01 19:30:03 -040037
Rich Felker0b44a032011-02-12 00:22:29 -050038AR = $(CROSS_COMPILE)ar
39RANLIB = $(CROSS_COMPILE)ranlib
Rich Felkere678fc62013-08-17 22:21:11 -040040INSTALL = ./tools/install.sh
Rich Felker0b44a032011-02-12 00:22:29 -050041
Rich Felkerd66ab4f2013-07-01 13:43:43 -040042ARCH_INCLUDES = $(wildcard arch/$(ARCH)/bits/*.h)
43ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH) $(ARCH_INCLUDES:arch/$(ARCH)/%=include/%))
Rich Felker0b44a032011-02-12 00:22:29 -050044
Rich Felker41d51832011-02-24 16:37:21 -050045EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
Rich Felker0b44a032011-02-12 00:22:29 -050046EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
Rich Felkerdc031ee2015-05-26 03:37:41 -040047CRT_LIBS = lib/crt1.o lib/Scrt1.o lib/rcrt1.o lib/crti.o lib/crtn.o
Rich Felkerd76eafc2012-05-01 19:30:03 -040048STATIC_LIBS = lib/libc.a
Rich Felker207c45d2011-06-27 21:38:11 -040049SHARED_LIBS = lib/libc.so
Rich Felker58f430c2012-04-22 14:32:49 -040050TOOL_LIBS = lib/musl-gcc.specs
Rich Felkerd76eafc2012-05-01 19:30:03 -040051ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
Rich Felker0b44a032011-02-12 00:22:29 -050052ALL_TOOLS = tools/musl-gcc
53
Shizb3cd7d12015-06-28 23:08:19 +020054WRAPCC_GCC = gcc
Shizfb585452015-06-28 23:08:21 +020055WRAPCC_CLANG = clang
Shizb3cd7d12015-06-28 23:08:19 +020056
Rich Felker3e7f1862013-07-18 20:30:58 -040057LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
Rich Felker207c45d2011-06-27 21:38:11 -040058
Rich Felker0b44a032011-02-12 00:22:29 -050059-include config.mak
60
Rich Felker8d546812011-06-28 08:27:38 -040061all: $(ALL_LIBS) $(ALL_TOOLS)
Rich Felker0b44a032011-02-12 00:22:29 -050062
Rich Felker07809362012-08-17 19:32:24 -040063install: install-libs install-headers install-tools
Rich Felker0b44a032011-02-12 00:22:29 -050064
65clean:
66 rm -f crt/*.o
67 rm -f $(OBJS)
68 rm -f $(LOBJS)
Rich Felker127ab572011-02-17 17:57:26 -050069 rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
Rich Felker0b44a032011-02-12 00:22:29 -050070 rm -f $(ALL_TOOLS)
Rich Felker179ab5a2013-12-01 17:27:25 -050071 rm -f $(GENH) $(GENH_INT)
Rich Felker1355fdc2011-02-15 00:33:23 -050072 rm -f include/bits
73
Rich Felkerd76eafc2012-05-01 19:30:03 -040074distclean: clean
75 rm -f config.mak
76
Rich Felker1355fdc2011-02-15 00:33:23 -050077include/bits:
Rich Felkera36164c2011-02-17 15:15:03 -050078 @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
Rich Felker1355fdc2011-02-15 00:33:23 -050079 ln -sf ../arch/$(ARCH)/bits $@
80
Rich Felker9448b052013-07-22 11:22:36 -040081include/bits/alltypes.h.in: include/bits
Rich Felker0b44a032011-02-12 00:22:29 -050082
Rich Felker9448b052013-07-22 11:22:36 -040083include/bits/alltypes.h: include/bits/alltypes.h.in include/alltypes.h.in tools/mkalltypes.sed
84 sed -f tools/mkalltypes.sed include/bits/alltypes.h.in include/alltypes.h.in > $@
Rich Felker0b44a032011-02-12 00:22:29 -050085
Rich Felkerae71a432013-12-04 18:00:19 -050086src/internal/version.h: $(wildcard VERSION .git)
Rich Felker179ab5a2013-12-01 17:27:25 -050087 printf '#define VERSION "%s"\n' "$$(sh tools/version.sh)" > $@
88
89src/internal/version.lo: src/internal/version.h
90
Rich Felker585ba142015-06-03 02:00:44 -040091crt/rcrt1.o src/ldso/dlstart.lo src/ldso/dynlink.lo: src/internal/dynlink.h arch/$(ARCH)/reloc.h
Rich Felkerd0437802012-08-05 12:32:20 -040092
Rich Felker585ba142015-06-03 02:00:44 -040093crt/crt1.o crt/Scrt1.o crt/rcrt1.o src/ldso/dlstart.lo: $(wildcard arch/$(ARCH)/crt_arch.h)
94
95crt/rcrt1.o: src/ldso/dlstart.c
Rich Felkerc5e34da2013-07-26 01:49:14 -040096
Rich Felkerdc031ee2015-05-26 03:37:41 -040097crt/Scrt1.o crt/rcrt1.o: CFLAGS += -fPIC
Rich Felkerc5e34da2013-07-26 01:49:14 -040098
Rich Felkera80847d2013-07-22 21:22:04 -040099OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=src/%))
100$(OPTIMIZE_SRCS:%.c=%.o) $(OPTIMIZE_SRCS:%.c=%.lo): CFLAGS += -O3
101
Rich Felker4a1f55e2013-08-01 17:12:23 -0400102MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
103$(MEMOPS_SRCS:%.c=%.o) $(MEMOPS_SRCS:%.c=%.lo): CFLAGS += $(CFLAGS_MEMOPS)
104
Rich Felker1ef849c2015-04-13 20:13:10 -0400105NOSSP_SRCS = $(wildcard crt/*.c) \
106 src/env/__libc_start_main.c src/env/__init_tls.c \
107 src/thread/__set_thread_area.c src/env/__stack_chk_fail.c \
108 src/string/memset.c src/string/memcpy.c \
109 src/ldso/dlstart.c src/ldso/dynlink.c
110$(NOSSP_SRCS:%.c=%.o) $(NOSSP_SRCS:%.c=%.lo): CFLAGS += $(CFLAGS_NOSSP)
111
Rich Felkered0c8242015-05-26 02:31:04 -0400112$(CRT_LIBS:lib/%=crt/%): CFLAGS += -DCRT
Rich Felkerde2b67f2015-04-19 22:05:29 -0400113
Rich Felkerfb72a972013-08-14 02:50:25 -0400114# This incantation ensures that changes to any subarch asm files will
115# force the corresponding object file to be rebuilt, even if the implicit
116# rule below goes indirectly through a .sub file.
117define mkasmdep
118$(dir $(patsubst %/,%,$(dir $(1))))$(notdir $(1:.s=.o)): $(1)
119endef
120$(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s))))
121
Alex Dowad35b33122015-07-10 15:03:24 +0200122# Choose invocation of assembler to be used
123# $(1) is input file, $(2) is output file, $(3) is assembler flags
124ifeq ($(ADD_CFI),yes)
125 AS_CMD = LC_ALL=C awk -f tools/add-cfi.$(ARCH).awk $< | $(CC) -x assembler -c -o $@ -
126else
127 AS_CMD = $(CC) -c -o $@ $<
128endif
129
Rich Felkerfb72a972013-08-14 02:50:25 -0400130%.o: $(ARCH)$(ASMSUBARCH)/%.sub
131 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<)
Rich Felker90d77722013-08-11 03:27:35 -0400132
Rich Felker0b44a032011-02-12 00:22:29 -0500133%.o: $(ARCH)/%.s
Alex Dowad35b33122015-07-10 15:03:24 +0200134 $(AS_CMD) $(CFLAGS_ALL_STATIC)
Rich Felker0b44a032011-02-12 00:22:29 -0500135
Rich Felkere7652392012-05-03 20:35:11 -0400136%.o: %.c $(GENH) $(IMPH)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400137 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500138
Rich Felkerfb72a972013-08-14 02:50:25 -0400139%.lo: $(ARCH)$(ASMSUBARCH)/%.sub
140 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<)
Rich Felker804e9942013-08-11 03:49:16 -0400141
Rich Felker0b44a032011-02-12 00:22:29 -0500142%.lo: $(ARCH)/%.s
Alex Dowad35b33122015-07-10 15:03:24 +0200143 $(AS_CMD) $(CFLAGS_ALL_SHARED)
Rich Felker0b44a032011-02-12 00:22:29 -0500144
Rich Felkere7652392012-05-03 20:35:11 -0400145%.lo: %.c $(GENH) $(IMPH)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400146 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500147
Rich Felker207c45d2011-06-27 21:38:11 -0400148lib/libc.so: $(LOBJS)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400149 $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS) -nostdlib -shared \
Rich Felker3fa2eb22014-06-20 00:25:12 -0400150 -Wl,-e,_dlstart -Wl,-Bsymbolic-functions \
Rich Felkerdfdc3372013-03-09 22:34:11 -0500151 -o $@ $(LOBJS) $(LIBCC)
Rich Felker0b44a032011-02-12 00:22:29 -0500152
Rich Felker4fd15952011-02-17 17:12:52 -0500153lib/libc.a: $(OBJS)
Rich Felker0b44a032011-02-12 00:22:29 -0500154 rm -f $@
155 $(AR) rc $@ $(OBJS)
156 $(RANLIB) $@
157
Rich Felker4fd15952011-02-17 17:12:52 -0500158$(EMPTY_LIBS):
159 rm -f $@
Rich Felker0b44a032011-02-12 00:22:29 -0500160 $(AR) rc $@
161
Rich Felker4fd15952011-02-17 17:12:52 -0500162lib/%.o: crt/%.o
Rich Felker0b44a032011-02-12 00:22:29 -0500163 cp $< $@
164
Rich Felker58f430c2012-04-22 14:32:49 -0400165lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
166 sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
167
168tools/musl-gcc: config.mak
Shizb3cd7d12015-06-28 23:08:19 +0200169 printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
Rich Felker0b44a032011-02-12 00:22:29 -0500170 chmod +x $@
171
Shizfb585452015-06-28 23:08:21 +0200172tools/%-clang: tools/%-clang.in config.mak
173 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' $< > $@
174 chmod +x $@
175
Rich Felker0b44a032011-02-12 00:22:29 -0500176$(DESTDIR)$(bindir)/%: tools/%
Rich Felkere678fc62013-08-17 22:21:11 -0400177 $(INSTALL) -D $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500178
Rich Felker207c45d2011-06-27 21:38:11 -0400179$(DESTDIR)$(libdir)/%.so: lib/%.so
Rich Felkere678fc62013-08-17 22:21:11 -0400180 $(INSTALL) -D -m 755 $< $@
Rich Felker207c45d2011-06-27 21:38:11 -0400181
Rich Felkerec05a0b2011-06-23 22:13:47 -0400182$(DESTDIR)$(libdir)/%: lib/%
Rich Felkere678fc62013-08-17 22:21:11 -0400183 $(INSTALL) -D -m 644 $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500184
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400185$(DESTDIR)$(includedir)/bits/%: arch/$(ARCH)/bits/%
Rich Felkere678fc62013-08-17 22:21:11 -0400186 $(INSTALL) -D -m 644 $< $@
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400187
Rich Felkerec05a0b2011-06-23 22:13:47 -0400188$(DESTDIR)$(includedir)/%: include/%
Rich Felkere678fc62013-08-17 22:21:11 -0400189 $(INSTALL) -D -m 644 $< $@
Rich Felkerec05a0b2011-06-23 22:13:47 -0400190
Rich Felker82fa6b42013-08-16 17:51:38 -0400191$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
Rich Felker9ff8ed42013-08-31 11:36:56 -0400192 $(INSTALL) -D -l $(libdir)/libc.so $@ || true
Rich Felkerf8e054f2012-05-04 21:54:57 -0400193
Rich Felker07809362012-08-17 19:32:24 -0400194install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
195
196install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
197
198install-tools: $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%)
199
Rich Felkerf8071072014-06-25 16:14:37 -0400200musl-git-%.tar.gz: .git
201 git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@)
Rich Felker07809362012-08-17 19:32:24 -0400202
Rich Felkerf8071072014-06-25 16:14:37 -0400203musl-%.tar.gz: .git
204 git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@)
Rich Felker07809362012-08-17 19:32:24 -0400205
Rich Felkera91ebdc2015-08-27 19:58:20 +0000206.SECONDARY:
Rich Felker0b44a032011-02-12 00:22:29 -0500207
Rich Felker07809362012-08-17 19:32:24 -0400208.PHONY: all clean install install-libs install-headers install-tools