blob: 7ac58d486c3ca803f07223c50c68dd0a218d17ba [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 Felkere678fc62013-08-17 22:21:11 -040039INSTALL = ./tools/install.sh
Rich Felker0b44a032011-02-12 00:22:29 -050040
Rich Felkerd66ab4f2013-07-01 13:43:43 -040041ARCH_INCLUDES = $(wildcard arch/$(ARCH)/bits/*.h)
42ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH) $(ARCH_INCLUDES:arch/$(ARCH)/%=include/%))
Rich Felker0b44a032011-02-12 00:22:29 -050043
Rich Felker41d51832011-02-24 16:37:21 -050044EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
Rich Felker0b44a032011-02-12 00:22:29 -050045EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
Rich Felker83023d12012-05-02 21:01:55 -040046CRT_LIBS = lib/crt1.o lib/Scrt1.o lib/crti.o lib/crtn.o
Rich Felkerd76eafc2012-05-01 19:30:03 -040047STATIC_LIBS = lib/libc.a
Rich Felker207c45d2011-06-27 21:38:11 -040048SHARED_LIBS = lib/libc.so
Rich Felker58f430c2012-04-22 14:32:49 -040049TOOL_LIBS = lib/musl-gcc.specs
Rich Felkerd76eafc2012-05-01 19:30:03 -040050ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
Rich Felker0b44a032011-02-12 00:22:29 -050051ALL_TOOLS = tools/musl-gcc
52
Rich Felker3e7f1862013-07-18 20:30:58 -040053LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
Rich Felker207c45d2011-06-27 21:38:11 -040054
Rich Felker0b44a032011-02-12 00:22:29 -050055-include config.mak
56
Rich Felker8d546812011-06-28 08:27:38 -040057all: $(ALL_LIBS) $(ALL_TOOLS)
Rich Felker0b44a032011-02-12 00:22:29 -050058
Rich Felker07809362012-08-17 19:32:24 -040059install: install-libs install-headers install-tools
Rich Felker0b44a032011-02-12 00:22:29 -050060
61clean:
62 rm -f crt/*.o
63 rm -f $(OBJS)
64 rm -f $(LOBJS)
Rich Felker127ab572011-02-17 17:57:26 -050065 rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
Rich Felker0b44a032011-02-12 00:22:29 -050066 rm -f $(ALL_TOOLS)
67 rm -f $(GENH)
Rich Felker1355fdc2011-02-15 00:33:23 -050068 rm -f include/bits
69
Rich Felkerd76eafc2012-05-01 19:30:03 -040070distclean: clean
71 rm -f config.mak
72
Rich Felker1355fdc2011-02-15 00:33:23 -050073include/bits:
Rich Felkera36164c2011-02-17 15:15:03 -050074 @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
Rich Felker1355fdc2011-02-15 00:33:23 -050075 ln -sf ../arch/$(ARCH)/bits $@
76
Rich Felker9448b052013-07-22 11:22:36 -040077include/bits/alltypes.h.in: include/bits
Rich Felker0b44a032011-02-12 00:22:29 -050078
Rich Felker9448b052013-07-22 11:22:36 -040079include/bits/alltypes.h: include/bits/alltypes.h.in include/alltypes.h.in tools/mkalltypes.sed
80 sed -f tools/mkalltypes.sed include/bits/alltypes.h.in include/alltypes.h.in > $@
Rich Felker0b44a032011-02-12 00:22:29 -050081
Rich Felkerd0437802012-08-05 12:32:20 -040082src/ldso/dynlink.lo: arch/$(ARCH)/reloc.h
83
Rich Felkerc5e34da2013-07-26 01:49:14 -040084crt/crt1.o crt/Scrt1.o: $(wildcard arch/$(ARCH)/crt_arch.h)
85
86crt/Scrt1.o: CFLAGS += -fPIC
87
Rich Felkera80847d2013-07-22 21:22:04 -040088OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=src/%))
89$(OPTIMIZE_SRCS:%.c=%.o) $(OPTIMIZE_SRCS:%.c=%.lo): CFLAGS += -O3
90
Rich Felker4a1f55e2013-08-01 17:12:23 -040091MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
92$(MEMOPS_SRCS:%.c=%.o) $(MEMOPS_SRCS:%.c=%.lo): CFLAGS += $(CFLAGS_MEMOPS)
93
Rich Felkerfb72a972013-08-14 02:50:25 -040094# This incantation ensures that changes to any subarch asm files will
95# force the corresponding object file to be rebuilt, even if the implicit
96# rule below goes indirectly through a .sub file.
97define mkasmdep
98$(dir $(patsubst %/,%,$(dir $(1))))$(notdir $(1:.s=.o)): $(1)
99endef
100$(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s))))
101
102%.o: $(ARCH)$(ASMSUBARCH)/%.sub
103 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<)
Rich Felker90d77722013-08-11 03:27:35 -0400104
Rich Felker0b44a032011-02-12 00:22:29 -0500105%.o: $(ARCH)/%.s
Rich Felkerd76eafc2012-05-01 19:30:03 -0400106 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500107
Rich Felkere7652392012-05-03 20:35:11 -0400108%.o: %.c $(GENH) $(IMPH)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400109 $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500110
Rich Felkerfb72a972013-08-14 02:50:25 -0400111%.lo: $(ARCH)$(ASMSUBARCH)/%.sub
112 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<)
Rich Felker804e9942013-08-11 03:49:16 -0400113
Rich Felker0b44a032011-02-12 00:22:29 -0500114%.lo: $(ARCH)/%.s
Rich Felkerd76eafc2012-05-01 19:30:03 -0400115 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500116
Rich Felkere7652392012-05-03 20:35:11 -0400117%.lo: %.c $(GENH) $(IMPH)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400118 $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
Rich Felker0b44a032011-02-12 00:22:29 -0500119
Rich Felker207c45d2011-06-27 21:38:11 -0400120lib/libc.so: $(LOBJS)
Rich Felkerd76eafc2012-05-01 19:30:03 -0400121 $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS) -nostdlib -shared \
122 -Wl,-e,_start -Wl,-Bsymbolic-functions \
Rich Felkerdfdc3372013-03-09 22:34:11 -0500123 -o $@ $(LOBJS) $(LIBCC)
Rich Felker0b44a032011-02-12 00:22:29 -0500124
Rich Felker4fd15952011-02-17 17:12:52 -0500125lib/libc.a: $(OBJS)
Rich Felker0b44a032011-02-12 00:22:29 -0500126 rm -f $@
127 $(AR) rc $@ $(OBJS)
128 $(RANLIB) $@
129
Rich Felker4fd15952011-02-17 17:12:52 -0500130$(EMPTY_LIBS):
131 rm -f $@
Rich Felker0b44a032011-02-12 00:22:29 -0500132 $(AR) rc $@
133
Rich Felker4fd15952011-02-17 17:12:52 -0500134lib/%.o: crt/%.o
Rich Felker0b44a032011-02-12 00:22:29 -0500135 cp $< $@
136
Rich Felker58f430c2012-04-22 14:32:49 -0400137lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
138 sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
139
140tools/musl-gcc: config.mak
Rich Felker0b3e2252012-09-21 13:47:26 -0400141 printf '#!/bin/sh\nexec "$${REALGCC:-gcc}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
Rich Felker0b44a032011-02-12 00:22:29 -0500142 chmod +x $@
143
144$(DESTDIR)$(bindir)/%: tools/%
Rich Felkere678fc62013-08-17 22:21:11 -0400145 $(INSTALL) -D $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500146
Rich Felker207c45d2011-06-27 21:38:11 -0400147$(DESTDIR)$(libdir)/%.so: lib/%.so
Rich Felkere678fc62013-08-17 22:21:11 -0400148 $(INSTALL) -D -m 755 $< $@
Rich Felker207c45d2011-06-27 21:38:11 -0400149
Rich Felkerec05a0b2011-06-23 22:13:47 -0400150$(DESTDIR)$(libdir)/%: lib/%
Rich Felkere678fc62013-08-17 22:21:11 -0400151 $(INSTALL) -D -m 644 $< $@
Rich Felker0b44a032011-02-12 00:22:29 -0500152
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400153$(DESTDIR)$(includedir)/bits/%: arch/$(ARCH)/bits/%
Rich Felkere678fc62013-08-17 22:21:11 -0400154 $(INSTALL) -D -m 644 $< $@
Rich Felkerd66ab4f2013-07-01 13:43:43 -0400155
Rich Felkerec05a0b2011-06-23 22:13:47 -0400156$(DESTDIR)$(includedir)/%: include/%
Rich Felkere678fc62013-08-17 22:21:11 -0400157 $(INSTALL) -D -m 644 $< $@
Rich Felkerec05a0b2011-06-23 22:13:47 -0400158
Rich Felker82fa6b42013-08-16 17:51:38 -0400159$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
Rich Felker9ff8ed42013-08-31 11:36:56 -0400160 $(INSTALL) -D -l $(libdir)/libc.so $@ || 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