1# 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 11srcdir = . 12exec_prefix = /usr/local 13bindir = $(exec_prefix)/bin 14 15prefix = /usr/local/musl 16includedir = $(prefix)/include 17libdir = $(prefix)/lib 18syslibdir = /lib 19 20SRC_DIRS = $(addprefix $(srcdir)/,src/* crt ldso $(COMPAT_SRC_DIRS)) 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))) 25BASE_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(BASE_SRCS))) 26ARCH_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(ARCH_SRCS))) 27REPLACED_OBJS = $(sort $(subst /$(ARCH)/,/,$(ARCH_OBJS))) 28ALL_OBJS = $(addprefix obj/, $(filter-out $(REPLACED_OBJS), $(sort $(BASE_OBJS) $(ARCH_OBJS)))) 29 30LIBC_OBJS = $(filter obj/src/%,$(ALL_OBJS)) $(filter obj/compat/%,$(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) 36GENH = obj/include/bits/alltypes.h obj/include/bits/syscall.h 37GENH_INT = obj/src/internal/version.h 38IMPH = $(addprefix $(srcdir)/, src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/locale_impl.h src/internal/libc.h) 39 40LDFLAGS = 41LDFLAGS_AUTO = 42LIBCC = -lgcc 43CPPFLAGS = 44CFLAGS = 45CFLAGS_AUTO = -Os -pipe 46CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc 47 48CFLAGS_ALL = $(CFLAGS_C99FSE) 49CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I$(srcdir)/arch/$(ARCH) -I$(srcdir)/arch/generic -Iobj/src/internal -I$(srcdir)/src/include -I$(srcdir)/src/internal -Iobj/include -I$(srcdir)/include 50CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS_AUTO) $(CFLAGS) 51 52LDFLAGS_ALL = $(LDFLAGS_AUTO) $(LDFLAGS) 53 54AR = $(CROSS_COMPILE)ar 55RANLIB = $(CROSS_COMPILE)ranlib 56INSTALL = $(srcdir)/tools/install.sh 57 58ARCH_INCLUDES = $(wildcard $(srcdir)/arch/$(ARCH)/bits/*.h) 59GENERIC_INCLUDES = $(wildcard $(srcdir)/arch/generic/bits/*.h) 60INCLUDES = $(wildcard $(srcdir)/include/*.h $(srcdir)/include/*/*.h) 61ALL_INCLUDES = $(sort $(INCLUDES:$(srcdir)/%=%) $(GENH:obj/%=%) $(ARCH_INCLUDES:$(srcdir)/arch/$(ARCH)/%=include/%) $(GENERIC_INCLUDES:$(srcdir)/arch/generic/%=include/%)) 62 63EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl 64EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a) 65CRT_LIBS = $(addprefix lib/,$(notdir $(CRT_OBJS))) 66STATIC_LIBS = lib/libc.a 67SHARED_LIBS = lib/libc.so 68TOOL_LIBS = lib/musl-gcc.specs 69ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS) 70ALL_TOOLS = obj/musl-gcc 71 72WRAPCC_GCC = gcc 73WRAPCC_CLANG = clang 74 75LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1 76 77-include config.mak 78-include $(srcdir)/arch/$(ARCH)/arch.mak 79 80ifeq ($(ARCH),) 81 82all: 83 @echo "Please set ARCH in config.mak before running make." 84 @exit 1 85 86else 87 88all: $(ALL_LIBS) $(ALL_TOOLS) 89 90OBJ_DIRS = $(sort $(patsubst %/,%,$(dir $(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(GENH) $(GENH_INT))) obj/include) 91 92$(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(ALL_OBJS:%.o=%.lo) $(GENH) $(GENH_INT): | $(OBJ_DIRS) 93 94$(OBJ_DIRS): 95 mkdir -p $@ 96 97obj/include/bits/alltypes.h: $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in $(srcdir)/tools/mkalltypes.sed 98 sed -f $(srcdir)/tools/mkalltypes.sed $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in > $@ 99 100obj/include/bits/syscall.h: $(srcdir)/arch/$(ARCH)/bits/syscall.h.in 101 cp $< $@ 102 sed -n -e s/__NR_/SYS_/p < $< >> $@ 103 104obj/src/internal/version.h: $(wildcard $(srcdir)/VERSION $(srcdir)/.git) 105 printf '#define VERSION "%s"\n' "$$(cd $(srcdir); sh tools/version.sh)" > $@ 106 107obj/src/internal/version.o obj/src/internal/version.lo: obj/src/internal/version.h 108 109obj/crt/rcrt1.o obj/ldso/dlstart.lo obj/ldso/dynlink.lo: $(srcdir)/src/internal/dynlink.h $(srcdir)/arch/$(ARCH)/reloc.h 110 111obj/crt/crt1.o obj/crt/scrt1.o obj/crt/rcrt1.o obj/ldso/dlstart.lo: $(srcdir)/arch/$(ARCH)/crt_arch.h 112 113obj/crt/rcrt1.o: $(srcdir)/ldso/dlstart.c 114 115obj/crt/Scrt1.o obj/crt/rcrt1.o: CFLAGS_ALL += -fPIC 116 117OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=$(srcdir)/src/%)) 118$(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.o) $(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.lo): CFLAGS += -O3 119 120MEMOPS_OBJS = $(filter %/memcpy.o %/memmove.o %/memcmp.o %/memset.o, $(LIBC_OBJS)) 121$(MEMOPS_OBJS) $(MEMOPS_OBJS:%.o=%.lo): CFLAGS_ALL += $(CFLAGS_MEMOPS) 122 123NOSSP_OBJS = $(CRT_OBJS) $(LDSO_OBJS) $(filter \ 124 %/__libc_start_main.o %/__init_tls.o %/__stack_chk_fail.o \ 125 %/__set_thread_area.o %/memset.o %/memcpy.o \ 126 , $(LIBC_OBJS)) 127$(NOSSP_OBJS) $(NOSSP_OBJS:%.o=%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP) 128 129$(CRT_OBJS): CFLAGS_ALL += -DCRT 130 131$(LOBJS) $(LDSO_OBJS): CFLAGS_ALL += -fPIC 132 133CC_CMD = $(CC) $(CFLAGS_ALL) -c -o $@ $< 134 135# Choose invocation of assembler to be used 136ifeq ($(ADD_CFI),yes) 137 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 $@ - 138else 139 AS_CMD = $(CC_CMD) 140endif 141 142obj/%.o: $(srcdir)/%.s 143 $(AS_CMD) 144 145obj/%.o: $(srcdir)/%.S 146 $(CC_CMD) 147 148obj/%.o: $(srcdir)/%.c $(GENH) $(IMPH) 149 $(CC_CMD) 150 151obj/%.lo: $(srcdir)/%.s 152 $(AS_CMD) 153 154obj/%.lo: $(srcdir)/%.S 155 $(CC_CMD) 156 157obj/%.lo: $(srcdir)/%.c $(GENH) $(IMPH) 158 $(CC_CMD) 159 160lib/libc.so: $(LOBJS) $(LDSO_OBJS) 161 $(CC) $(CFLAGS_ALL) $(LDFLAGS_ALL) -nostdlib -shared \ 162 -Wl,-e,_dlstart -o $@ $(LOBJS) $(LDSO_OBJS) $(LIBCC) 163 164lib/libc.a: $(AOBJS) 165 rm -f $@ 166 $(AR) rc $@ $(AOBJS) 167 $(RANLIB) $@ 168 169$(EMPTY_LIBS): 170 rm -f $@ 171 $(AR) rc $@ 172 173lib/%.o: obj/crt/$(ARCH)/%.o 174 cp $< $@ 175 176lib/%.o: obj/crt/%.o 177 cp $< $@ 178 179lib/musl-gcc.specs: $(srcdir)/tools/musl-gcc.specs.sh config.mak 180 sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@ 181 182obj/musl-gcc: config.mak 183 printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@ 184 chmod +x $@ 185 186obj/%-clang: $(srcdir)/tools/%-clang.in config.mak 187 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' $< > $@ 188 chmod +x $@ 189 190$(DESTDIR)$(bindir)/%: obj/% 191 $(INSTALL) -D $< $@ 192 193$(DESTDIR)$(libdir)/%.so: lib/%.so 194 $(INSTALL) -D -m 755 $< $@ 195 196$(DESTDIR)$(libdir)/%: lib/% 197 $(INSTALL) -D -m 644 $< $@ 198 199$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/$(ARCH)/bits/% 200 $(INSTALL) -D -m 644 $< $@ 201 202$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/generic/bits/% 203 $(INSTALL) -D -m 644 $< $@ 204 205$(DESTDIR)$(includedir)/bits/%: obj/include/bits/% 206 $(INSTALL) -D -m 644 $< $@ 207 208$(DESTDIR)$(includedir)/%: $(srcdir)/include/% 209 $(INSTALL) -D -m 644 $< $@ 210 211$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so 212 $(INSTALL) -D -r $(DESTDIR)$(libdir)/libc.so $@ || true 213 214install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),) 215 216install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%) 217 218install-tools: $(ALL_TOOLS:obj/%=$(DESTDIR)$(bindir)/%) 219 220install: install-libs install-headers install-tools 221 222musl-git-%.tar.gz: .git 223 git --git-dir=$(srcdir)/.git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@) 224 225musl-%.tar.gz: .git 226 git --git-dir=$(srcdir)/.git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@) 227 228endif 229 230clean: 231 rm -rf obj lib 232 233distclean: clean 234 rm -f config.mak 235 236.PHONY: all clean install install-libs install-headers install-tools 237