Makefile 7.2 KB

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