1
0

Makefile 7.3 KB

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