blob: 7e634dadfcd01f9aa96b0f5662028b66d7529518 [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 Felkere7652392012-05-03 20:35:11 -040023IMPH = src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/libc.h
Rich Felker0b44a032011-02-12 00:22:29 -050024
Rich Felkerd76eafc2012-05-01 19:30:03 -040025LDFLAGS =
Rich Felker1b231452012-08-02 21:05:43 -040026LIBCC = -lgcc
Rich Felkerd76eafc2012-05-01 19:30:03 -040027CPPFLAGS =
28CFLAGS = -Os -pipe
29CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc
30
31CFLAGS_ALL = $(CFLAGS_C99FSE)
Rich Felkerfcaec912012-07-11 02:44:14 -040032CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I./arch/$(ARCH) -I./src/internal -I./include
Rich Felkerd76eafc2012-05-01 19:30:03 -040033CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS)
34CFLAGS_ALL_STATIC = $(CFLAGS_ALL)
Rich Felker9f745742012-09-14 23:38:10 -040035CFLAGS_ALL_SHARED = $(CFLAGS_ALL) -fPIC -DSHARED
Rich Felkerd76eafc2012-05-01 19:30:03 -040036
Rich Felker0b44a032011-02-12 00:22:29 -050037AR = $(CROSS_COMPILE)ar
38RANLIB = $(CROSS_COMPILE)ranlib
Rich Felker0b44a032011-02-12 00:22:29 -050039
Rich Felkerd66ab4f2013-07-01 13:43:43 -040040ARCH_INCLUDES = $(wildcard arch/$(ARCH)/bits/*.h)
41ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH) $(ARCH_INCLUDES:arch/$(ARCH)/%=include/%))
Rich Felker0b44a032011-02-12 00:22:29 -050042
Rich Felker41d51832011-02-24 16:37:21 -050043EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
Rich Felker0b44a032011-02-12 00:22:29 -050044EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
Rich Felker83023d12012-05-02 21:01:55 -040045CRT_LIBS = lib/crt1.o lib/Scrt1.o lib/crti.o lib/crtn.o
Rich Felkerd76eafc2012-05-01 19:30:03 -040046STATIC_LIBS = lib/libc.a
Rich Felker207c45d2011-06-27 21:38:11 -040047SHARED_LIBS = lib/libc.so
Rich Felker58f430c2012-04-22 14:32:49 -040048TOOL_LIBS = lib/musl-gcc.specs
Rich Felkerd76eafc2012-05-01 19:30:03 -040049ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
Rich Felker0b44a032011-02-12 00:22:29 -050050ALL_TOOLS = tools/musl-gcc
51
Rich Felker3e7f1862013-07-18 20:30:58 -040052LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
Rich Felker207c45d2011-06-27 21:38:11 -040053
Rich Felker0b44a032011-02-12 00:22:29 -050054-include config.mak
55
Rich Felker8d546812011-06-28 08:27:38 -040056all: $(ALL_LIBS) $(ALL_TOOLS)
Rich Felker0b44a032011-02-12 00:22:29 -050057
Rich Felker07809362012-08-17 19:32:24 -040058install: install-libs install-headers install-tools
Rich Felker0b44a032011-02-12 00:22:29 -050059
60clean:
61 rm -f crt/*.o
62 rm -f $(OBJS)
63 rm -f $(LOBJS)
Rich Felker127ab572011-02-17 17:57:26 -050064 rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
Rich Felker0b44a032011-02-12 00:22:29 -050065 rm -f $(ALL_TOOLS)
66 rm -f $(GENH)
Rich Felker1355fdc2011-02-15 00:33:23 -050067 rm -f include/bits
68
Rich Felkerd76eafc2012-05-01 19:30:03 -040069distclean: clean
70 rm -f config.mak
71
Rich Felker1355fdc2011-02-15 00:33:23 -050072include/bits:
Rich Felkera36164c2011-02-17 15:15:03 -050073 @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
Rich Felker1355fdc2011-02-15 00:33:23 -050074 ln -sf ../arch/$(ARCH)/bits $@
75
Rich Felker9448b052013-07-22 11:22:36 -040076include/bits/alltypes.h.in: include/bits
Rich Felker0b44a032011-02-12 00:22:29 -050077
Rich Felker9448b052013-07-22 11:22:36 -040078include/bits/alltypes.h: include/bits/alltypes.h.in include/alltypes.h.in tools/mkalltypes.sed
79 sed -f tools/mkalltypes.sed include/bits/alltypes.h.in include/alltypes.h.in > $@
Rich Felker0b44a032011-02-12 00:22:29 -050080
Rich Felkerd0437802012-08-05 12:32:20 -040081src/ldso/dynlink.lo: arch/$(ARCH)/reloc.h
82
Rich Felkerc5e34da2013-07-26 01:49:14 -040083crt/crt1.o crt/Scrt1.o: $(wildcard arch/$(ARCH)/crt_arch.h)
84
85crt/Scrt1.o: CFLAGS += -fPIC
86
Rich Felkera80847d2013-07-22 21:22:04 -040087OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=src/%))
88$(OPTIMIZE_SRCS:%.c=%.o) $(OPTIMIZE_SRCS:%.c=%.lo): CFLAGS += -O3
89
Rich Felker4a1f55e2013-08-01 17:12:23 -040090MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
91$(MEMOPS_SRCS:%.c=%.o) $(MEMOPS_SRCS:%.c=%.lo): CFLAGS += $(CFLAGS_MEMOPS)
92
Rich Felkerfb72a972013-08-14 02:50:25 -040093# This incantation ensures that changes to any subarch asm files will
94# force the corresponding object file to be rebuilt, even if the implicit
95# rule below goes indirectly through a .sub file.
96define mkasmdep
97$(dir $(patsubst %/,%,$(dir $(1))))$(notdir $(1:.s=.o)): $(1)
98endef
99$(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s))))
100
101%.o: $(ARCH)$(ASMSUBARCH)/%.sub
102 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<)
Rich Felker90d77722013-08-11 03:27:35 -0400103
Rich Felker0b44a032011-02-12 00:22:29 -0500104%.o: $(ARCH)/%.s
Rich Felkerd76eafc2012-05-01 19:30:03 -0400105 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500106
Rich Felkere7652392012-05-03 20:35:11 -0400107%.o: %.c $(GENH) $(IMPH)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400108 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500109
Rich Felkerfb72a972013-08-14 02:50:25 -0400110%.lo: $(ARCH)$(ASMSUBARCH)/%.sub
111 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<)
Rich Felker804e9942013-08-11 03:49:16 -0400112
Rich Felker0b44a032011-02-12 00:22:29 -0500113%.lo: $(ARCH)/%.s
Rich Felkerd76eafc2012-05-01 19:30:03 -0400114 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500115
Rich Felkere7652392012-05-03 20:35:11 -0400116%.lo: %.c $(GENH) $(IMPH)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400117 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500118
Rich Felker207c45d2011-06-27 21:38:11 -0400119lib/libc.so: $(LOBJS)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400120 $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS) -nostdlib -shared \
121 -Wl,-e,_start -Wl,-Bsymbolic-functions \
Rich Felkerdfdc3372013-03-09 22:34:11 -0500122 -o $@ $(LOBJS) $(LIBCC)
Rich Felker0b44a032011-02-12 00:22:29 -0500123
Rich Felker4fd15952011-02-17 17:12:52 -0500124lib/libc.a: $(OBJS)
Rich Felker0b44a032011-02-12 00:22:29 -0500125 rm -f $@
126 $(AR) rc $@ $(OBJS)
127 $(RANLIB) $@
128
Rich Felker4fd15952011-02-17 17:12:52 -0500129$(EMPTY_LIBS):
130 rm -f $@
Rich Felker0b44a032011-02-12 00:22:29 -0500131 $(AR) rc $@
132
Rich Felker4fd15952011-02-17 17:12:52 -0500133lib/%.o: crt/%.o
Rich Felker0b44a032011-02-12 00:22:29 -0500134 cp $< $@
135
Rich Felker58f430c2012-04-22 14:32:49 -0400136lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
137 sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
138
139tools/musl-gcc: config.mak
Rich Felker0b3e2252012-09-21 13:47:26 -0400140 printf '#!/bin/sh\nexec "$${REALGCC:-gcc}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
Rich Felker0b44a032011-02-12 00:22:29 -0500141 chmod +x $@
142
143$(DESTDIR)$(bindir)/%: tools/%
144 install -D $< $@
145
Rich Felker207c45d2011-06-27 21:38:11 -0400146$(DESTDIR)$(libdir)/%.so: lib/%.so
147 install -D -m 755 $< $@
148
Rich Felkerec05a0b2011-06-23 22:13:47 -0400149$(DESTDIR)$(libdir)/%: lib/%
Rich Felker0b44a032011-02-12 00:22:29 -0500150 install -D -m 644 $< $@
151
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400152$(DESTDIR)$(includedir)/bits/%: arch/$(ARCH)/bits/%
153 install -D -m 644 $< $@
154
Rich Felkerec05a0b2011-06-23 22:13:47 -0400155$(DESTDIR)$(includedir)/%: include/%
156 install -D -m 644 $< $@
157
Rich Felker82fa6b42013-08-16 17:51:38 -0400158$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
159 test -d $(DESTDIR)$(syslibdir) || install -d -m 755 $(DESTDIR)$(syslibdir) || true
160 { ln -sf $(libdir)/libc.so $@.tmp.$$$$ && mv -f $@.tmp.$$$$ $@ ; } || true
Rich Felkerf8e054f2012-05-04 21:54:57 -0400161
Rich Felker07809362012-08-17 19:32:24 -0400162install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
163
164install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
165
166install-tools: $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%)
167
168
169
Rich Felker0b44a032011-02-12 00:22:29 -0500170.PRECIOUS: $(CRT_LIBS:lib/%=crt/%)
171
Rich Felker07809362012-08-17 19:32:24 -0400172.PHONY: all clean install install-libs install-headers install-tools