blob: df20f94a5c7129c34e92acf57310497eeedff9d7 [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 Felker4cd8b472015-11-02 16:58:14 -050026LDFLAGS =
27LDFLAGS_AUTO =
Rich Felker1b231452012-08-02 21:05:43 -040028LIBCC = -lgcc
Rich Felkerd76eafc2012-05-01 19:30:03 -040029CPPFLAGS =
Rich Felker4cd8b472015-11-02 16:58:14 -050030CFLAGS =
31CFLAGS_AUTO = -Os -pipe
Rich Felkerd76eafc2012-05-01 19:30:03 -040032CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc
33
34CFLAGS_ALL = $(CFLAGS_C99FSE)
Rich Felkerfcaec912012-07-11 02:44:14 -040035CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I./arch/$(ARCH) -I./src/internal -I./include
Rich Felker4cd8b472015-11-02 16:58:14 -050036CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS_AUTO) $(CFLAGS)
Rich Felkerd76eafc2012-05-01 19:30:03 -040037CFLAGS_ALL_STATIC = $(CFLAGS_ALL)
Rich Felker9f745742012-09-14 23:38:10 -040038CFLAGS_ALL_SHARED = $(CFLAGS_ALL) -fPIC -DSHARED
Rich Felkerd76eafc2012-05-01 19:30:03 -040039
Rich Felker4cd8b472015-11-02 16:58:14 -050040LDFLAGS_ALL = $(LDFLAGS_AUTO) $(LDFLAGS)
41
Rich Felker0b44a032011-02-12 00:22:29 -050042AR = $(CROSS_COMPILE)ar
43RANLIB = $(CROSS_COMPILE)ranlib
Rich Felkere678fc62013-08-17 22:21:11 -040044INSTALL = ./tools/install.sh
Rich Felker0b44a032011-02-12 00:22:29 -050045
Rich Felkerd66ab4f2013-07-01 13:43:43 -040046ARCH_INCLUDES = $(wildcard arch/$(ARCH)/bits/*.h)
47ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH) $(ARCH_INCLUDES:arch/$(ARCH)/%=include/%))
Rich Felker0b44a032011-02-12 00:22:29 -050048
Rich Felker41d51832011-02-24 16:37:21 -050049EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
Rich Felker0b44a032011-02-12 00:22:29 -050050EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
Rich Felkerdc031ee2015-05-26 03:37:41 -040051CRT_LIBS = lib/crt1.o lib/Scrt1.o lib/rcrt1.o lib/crti.o lib/crtn.o
Rich Felkerd76eafc2012-05-01 19:30:03 -040052STATIC_LIBS = lib/libc.a
Rich Felker207c45d2011-06-27 21:38:11 -040053SHARED_LIBS = lib/libc.so
Rich Felker58f430c2012-04-22 14:32:49 -040054TOOL_LIBS = lib/musl-gcc.specs
Rich Felkerd76eafc2012-05-01 19:30:03 -040055ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
Rich Felker0b44a032011-02-12 00:22:29 -050056ALL_TOOLS = tools/musl-gcc
57
Shizb3cd7d12015-06-28 23:08:19 +020058WRAPCC_GCC = gcc
Shizfb585452015-06-28 23:08:21 +020059WRAPCC_CLANG = clang
Shizb3cd7d12015-06-28 23:08:19 +020060
Rich Felker3e7f1862013-07-18 20:30:58 -040061LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
Rich Felker207c45d2011-06-27 21:38:11 -040062
Rich Felker0b44a032011-02-12 00:22:29 -050063-include config.mak
64
Rich Felker8d546812011-06-28 08:27:38 -040065all: $(ALL_LIBS) $(ALL_TOOLS)
Rich Felker0b44a032011-02-12 00:22:29 -050066
Rich Felker07809362012-08-17 19:32:24 -040067install: install-libs install-headers install-tools
Rich Felker0b44a032011-02-12 00:22:29 -050068
69clean:
70 rm -f crt/*.o
71 rm -f $(OBJS)
72 rm -f $(LOBJS)
Rich Felker127ab572011-02-17 17:57:26 -050073 rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
Rich Felker0b44a032011-02-12 00:22:29 -050074 rm -f $(ALL_TOOLS)
Rich Felker179ab5a2013-12-01 17:27:25 -050075 rm -f $(GENH) $(GENH_INT)
Rich Felker1355fdc2011-02-15 00:33:23 -050076 rm -f include/bits
77
Rich Felkerd76eafc2012-05-01 19:30:03 -040078distclean: clean
79 rm -f config.mak
80
Rich Felker1355fdc2011-02-15 00:33:23 -050081include/bits:
Rich Felkera36164c2011-02-17 15:15:03 -050082 @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
Rich Felker1355fdc2011-02-15 00:33:23 -050083 ln -sf ../arch/$(ARCH)/bits $@
84
Rich Felker9448b052013-07-22 11:22:36 -040085include/bits/alltypes.h.in: include/bits
Rich Felker0b44a032011-02-12 00:22:29 -050086
Rich Felker9448b052013-07-22 11:22:36 -040087include/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 > $@
Rich Felker0b44a032011-02-12 00:22:29 -050089
Rich Felkerae71a432013-12-04 18:00:19 -050090src/internal/version.h: $(wildcard VERSION .git)
Rich Felker179ab5a2013-12-01 17:27:25 -050091 printf '#define VERSION "%s"\n' "$$(sh tools/version.sh)" > $@
92
Rich Felker4f3a9282015-11-18 19:00:44 -050093src/internal/version.o src/internal/version.lo: src/internal/version.h
Rich Felker179ab5a2013-12-01 17:27:25 -050094
Rich Felker585ba142015-06-03 02:00:44 -040095crt/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 -040096
Rich Felker585ba142015-06-03 02:00:44 -040097crt/crt1.o crt/Scrt1.o crt/rcrt1.o src/ldso/dlstart.lo: $(wildcard arch/$(ARCH)/crt_arch.h)
98
99crt/rcrt1.o: src/ldso/dlstart.c
Rich Felkerc5e34da2013-07-26 01:49:14 -0400100
Rich Felkerbe76cdc2015-10-22 23:41:35 -0400101crt/Scrt1.o crt/rcrt1.o: CFLAGS_ALL += -fPIC
Rich Felkerc5e34da2013-07-26 01:49:14 -0400102
Rich Felkera80847d2013-07-22 21:22:04 -0400103OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=src/%))
104$(OPTIMIZE_SRCS:%.c=%.o) $(OPTIMIZE_SRCS:%.c=%.lo): CFLAGS += -O3
105
Rich Felker4a1f55e2013-08-01 17:12:23 -0400106MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
Rich Felkerbe76cdc2015-10-22 23:41:35 -0400107$(MEMOPS_SRCS:%.c=%.o) $(MEMOPS_SRCS:%.c=%.lo): CFLAGS_ALL += $(CFLAGS_MEMOPS)
Rich Felker4a1f55e2013-08-01 17:12:23 -0400108
Rich Felker1ef849c2015-04-13 20:13:10 -0400109NOSSP_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
Rich Felkerbe76cdc2015-10-22 23:41:35 -0400114$(NOSSP_SRCS:%.c=%.o) $(NOSSP_SRCS:%.c=%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP)
Rich Felker1ef849c2015-04-13 20:13:10 -0400115
Rich Felkerbe76cdc2015-10-22 23:41:35 -0400116$(CRT_LIBS:lib/%=crt/%): CFLAGS_ALL += -DCRT
Rich Felkerde2b67f2015-04-19 22:05:29 -0400117
Rich Felkerfb72a972013-08-14 02:50:25 -0400118# 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.
121define mkasmdep
122$(dir $(patsubst %/,%,$(dir $(1))))$(notdir $(1:.s=.o)): $(1)
123endef
124$(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s))))
125
Alex Dowad35b33122015-07-10 15:03:24 +0200126# Choose invocation of assembler to be used
127# $(1) is input file, $(2) is output file, $(3) is assembler flags
128ifeq ($(ADD_CFI),yes)
Alex Dowad0650a052015-10-02 13:32:32 +0200129 AS_CMD = LC_ALL=C awk -f tools/add-cfi.common.awk -f tools/add-cfi.$(ARCH).awk $< | $(CC) -x assembler -c -o $@ -
Alex Dowad35b33122015-07-10 15:03:24 +0200130else
131 AS_CMD = $(CC) -c -o $@ $<
132endif
133
Rich Felkerfb72a972013-08-14 02:50:25 -0400134%.o: $(ARCH)$(ASMSUBARCH)/%.sub
135 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<)
Rich Felker90d77722013-08-11 03:27:35 -0400136
Rich Felker0b44a032011-02-12 00:22:29 -0500137%.o: $(ARCH)/%.s
Alex Dowad35b33122015-07-10 15:03:24 +0200138 $(AS_CMD) $(CFLAGS_ALL_STATIC)
Rich Felker0b44a032011-02-12 00:22:29 -0500139
Rich Felkere7652392012-05-03 20:35:11 -0400140%.o: %.c $(GENH) $(IMPH)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400141 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500142
Rich Felkerfb72a972013-08-14 02:50:25 -0400143%.lo: $(ARCH)$(ASMSUBARCH)/%.sub
144 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<)
Rich Felker804e9942013-08-11 03:49:16 -0400145
Rich Felker0b44a032011-02-12 00:22:29 -0500146%.lo: $(ARCH)/%.s
Alex Dowad35b33122015-07-10 15:03:24 +0200147 $(AS_CMD) $(CFLAGS_ALL_SHARED)
Rich Felker0b44a032011-02-12 00:22:29 -0500148
Rich Felkere7652392012-05-03 20:35:11 -0400149%.lo: %.c $(GENH) $(IMPH)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400150 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500151
Rich Felker207c45d2011-06-27 21:38:11 -0400152lib/libc.so: $(LOBJS)
Rich Felker4cd8b472015-11-02 16:58:14 -0500153 $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS_ALL) -nostdlib -shared \
Rich Felker3fa2eb22014-06-20 00:25:12 -0400154 -Wl,-e,_dlstart -Wl,-Bsymbolic-functions \
Rich Felkerdfdc3372013-03-09 22:34:11 -0500155 -o $@ $(LOBJS) $(LIBCC)
Rich Felker0b44a032011-02-12 00:22:29 -0500156
Rich Felker4fd15952011-02-17 17:12:52 -0500157lib/libc.a: $(OBJS)
Rich Felker0b44a032011-02-12 00:22:29 -0500158 rm -f $@
159 $(AR) rc $@ $(OBJS)
160 $(RANLIB) $@
161
Rich Felker4fd15952011-02-17 17:12:52 -0500162$(EMPTY_LIBS):
163 rm -f $@
Rich Felker0b44a032011-02-12 00:22:29 -0500164 $(AR) rc $@
165
Rich Felker4fd15952011-02-17 17:12:52 -0500166lib/%.o: crt/%.o
Rich Felker0b44a032011-02-12 00:22:29 -0500167 cp $< $@
168
Rich Felker58f430c2012-04-22 14:32:49 -0400169lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
170 sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
171
172tools/musl-gcc: config.mak
Shizb3cd7d12015-06-28 23:08:19 +0200173 printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
Rich Felker0b44a032011-02-12 00:22:29 -0500174 chmod +x $@
175
Shizfb585452015-06-28 23:08:21 +0200176tools/%-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
Rich Felker0b44a032011-02-12 00:22:29 -0500180$(DESTDIR)$(bindir)/%: tools/%
Rich Felkere678fc62013-08-17 22:21:11 -0400181 $(INSTALL) -D $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500182
Rich Felker207c45d2011-06-27 21:38:11 -0400183$(DESTDIR)$(libdir)/%.so: lib/%.so
Rich Felkere678fc62013-08-17 22:21:11 -0400184 $(INSTALL) -D -m 755 $< $@
Rich Felker207c45d2011-06-27 21:38:11 -0400185
Rich Felkerec05a0b2011-06-23 22:13:47 -0400186$(DESTDIR)$(libdir)/%: lib/%
Rich Felkere678fc62013-08-17 22:21:11 -0400187 $(INSTALL) -D -m 644 $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500188
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400189$(DESTDIR)$(includedir)/bits/%: arch/$(ARCH)/bits/%
Rich Felkere678fc62013-08-17 22:21:11 -0400190 $(INSTALL) -D -m 644 $< $@
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400191
Rich Felkerec05a0b2011-06-23 22:13:47 -0400192$(DESTDIR)$(includedir)/%: include/%
Rich Felkere678fc62013-08-17 22:21:11 -0400193 $(INSTALL) -D -m 644 $< $@
Rich Felkerec05a0b2011-06-23 22:13:47 -0400194
Rich Felker82fa6b42013-08-16 17:51:38 -0400195$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
Rich Felker9ff8ed42013-08-31 11:36:56 -0400196 $(INSTALL) -D -l $(libdir)/libc.so $@ || true
Rich Felkerf8e054f2012-05-04 21:54:57 -0400197
Rich Felker07809362012-08-17 19:32:24 -0400198install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
199
200install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
201
202install-tools: $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%)
203
Rich Felkerf8071072014-06-25 16:14:37 -0400204musl-git-%.tar.gz: .git
205 git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@)
Rich Felker07809362012-08-17 19:32:24 -0400206
Rich Felkerf8071072014-06-25 16:14:37 -0400207musl-%.tar.gz: .git
208 git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@)
Rich Felker07809362012-08-17 19:32:24 -0400209
Rich Felker07809362012-08-17 19:32:24 -0400210.PHONY: all clean install install-libs install-headers install-tools