Makefile 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. exec_prefix = /usr/local
  11. bindir = $(exec_prefix)/bin
  12. prefix = /usr/local/musl
  13. includedir = $(prefix)/include
  14. libdir = $(prefix)/lib
  15. syslibdir = /lib
  16. SRCS = $(sort $(wildcard src/*/*.c arch/$(ARCH)/src/*.c))
  17. OBJS = $(SRCS:.c=.o)
  18. LOBJS = $(OBJS:.o=.lo)
  19. GENH = include/bits/alltypes.h
  20. GENH_INT = src/internal/version.h
  21. IMPH = src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/libc.h
  22. LDFLAGS =
  23. LDFLAGS_AUTO =
  24. LIBCC = -lgcc
  25. CPPFLAGS =
  26. CFLAGS =
  27. CFLAGS_AUTO = -Os -pipe
  28. CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc
  29. CFLAGS_ALL = $(CFLAGS_C99FSE)
  30. CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I./arch/$(ARCH) -I./src/internal -I./include
  31. CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS_AUTO) $(CFLAGS)
  32. CFLAGS_ALL_STATIC = $(CFLAGS_ALL)
  33. CFLAGS_ALL_SHARED = $(CFLAGS_ALL) -fPIC -DSHARED
  34. LDFLAGS_ALL = $(LDFLAGS_AUTO) $(LDFLAGS)
  35. AR = $(CROSS_COMPILE)ar
  36. RANLIB = $(CROSS_COMPILE)ranlib
  37. INSTALL = ./tools/install.sh
  38. ARCH_INCLUDES = $(wildcard arch/$(ARCH)/bits/*.h)
  39. ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH) $(ARCH_INCLUDES:arch/$(ARCH)/%=include/%))
  40. EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
  41. EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
  42. CRT_LIBS = lib/crt1.o lib/Scrt1.o lib/rcrt1.o lib/crti.o lib/crtn.o
  43. STATIC_LIBS = lib/libc.a
  44. SHARED_LIBS = lib/libc.so
  45. TOOL_LIBS = lib/musl-gcc.specs
  46. ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
  47. ALL_TOOLS = tools/musl-gcc
  48. WRAPCC_GCC = gcc
  49. WRAPCC_CLANG = clang
  50. LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
  51. -include config.mak
  52. all: $(ALL_LIBS) $(ALL_TOOLS)
  53. install: install-libs install-headers install-tools
  54. clean:
  55. rm -f crt/*.o
  56. rm -f $(OBJS)
  57. rm -f $(LOBJS)
  58. rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
  59. rm -f $(ALL_TOOLS)
  60. rm -f $(GENH) $(GENH_INT)
  61. rm -f include/bits
  62. distclean: clean
  63. rm -f config.mak
  64. include/bits:
  65. @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
  66. ln -sf ../arch/$(ARCH)/bits $@
  67. include/bits/alltypes.h.in: include/bits
  68. include/bits/alltypes.h: include/bits/alltypes.h.in include/alltypes.h.in tools/mkalltypes.sed
  69. sed -f tools/mkalltypes.sed include/bits/alltypes.h.in include/alltypes.h.in > $@
  70. src/internal/version.h: $(wildcard VERSION .git)
  71. printf '#define VERSION "%s"\n' "$$(sh tools/version.sh)" > $@
  72. src/internal/version.lo: src/internal/version.h
  73. crt/rcrt1.o src/ldso/dlstart.lo src/ldso/dynlink.lo: src/internal/dynlink.h arch/$(ARCH)/reloc.h
  74. crt/crt1.o crt/Scrt1.o crt/rcrt1.o src/ldso/dlstart.lo: $(wildcard arch/$(ARCH)/crt_arch.h)
  75. crt/rcrt1.o: src/ldso/dlstart.c
  76. crt/Scrt1.o crt/rcrt1.o: CFLAGS_ALL += -fPIC
  77. OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=src/%))
  78. $(OPTIMIZE_SRCS:%.c=%.o) $(OPTIMIZE_SRCS:%.c=%.lo): CFLAGS += -O3
  79. MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
  80. $(MEMOPS_SRCS:%.c=%.o) $(MEMOPS_SRCS:%.c=%.lo): CFLAGS_ALL += $(CFLAGS_MEMOPS)
  81. NOSSP_SRCS = $(wildcard crt/*.c) \
  82. src/env/__libc_start_main.c src/env/__init_tls.c \
  83. src/thread/__set_thread_area.c src/env/__stack_chk_fail.c \
  84. src/string/memset.c src/string/memcpy.c \
  85. src/ldso/dlstart.c src/ldso/dynlink.c
  86. $(NOSSP_SRCS:%.c=%.o) $(NOSSP_SRCS:%.c=%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP)
  87. $(CRT_LIBS:lib/%=crt/%): CFLAGS_ALL += -DCRT
  88. # This incantation ensures that changes to any subarch asm files will
  89. # force the corresponding object file to be rebuilt, even if the implicit
  90. # rule below goes indirectly through a .sub file.
  91. define mkasmdep
  92. $(dir $(patsubst %/,%,$(dir $(1))))$(notdir $(1:.s=.o)): $(1)
  93. endef
  94. $(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s))))
  95. # Choose invocation of assembler to be used
  96. # $(1) is input file, $(2) is output file, $(3) is assembler flags
  97. ifeq ($(ADD_CFI),yes)
  98. AS_CMD = LC_ALL=C awk -f tools/add-cfi.common.awk -f tools/add-cfi.$(ARCH).awk $< | $(CC) -x assembler -c -o $@ -
  99. else
  100. AS_CMD = $(CC) -c -o $@ $<
  101. endif
  102. %.o: $(ARCH)$(ASMSUBARCH)/%.sub
  103. $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<)
  104. %.o: $(ARCH)/%.s
  105. $(AS_CMD) $(CFLAGS_ALL_STATIC)
  106. %.o: %.c $(GENH) $(IMPH)
  107. $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
  108. %.lo: $(ARCH)$(ASMSUBARCH)/%.sub
  109. $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<)
  110. %.lo: $(ARCH)/%.s
  111. $(AS_CMD) $(CFLAGS_ALL_SHARED)
  112. %.lo: %.c $(GENH) $(IMPH)
  113. $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
  114. lib/libc.so: $(LOBJS)
  115. $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS_ALL) -nostdlib -shared \
  116. -Wl,-e,_dlstart -Wl,-Bsymbolic-functions \
  117. -o $@ $(LOBJS) $(LIBCC)
  118. lib/libc.a: $(OBJS)
  119. rm -f $@
  120. $(AR) rc $@ $(OBJS)
  121. $(RANLIB) $@
  122. $(EMPTY_LIBS):
  123. rm -f $@
  124. $(AR) rc $@
  125. lib/%.o: crt/%.o
  126. cp $< $@
  127. lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
  128. sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
  129. tools/musl-gcc: config.mak
  130. printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
  131. chmod +x $@
  132. tools/%-clang: tools/%-clang.in config.mak
  133. 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' $< > $@
  134. chmod +x $@
  135. $(DESTDIR)$(bindir)/%: tools/%
  136. $(INSTALL) -D $< $@
  137. $(DESTDIR)$(libdir)/%.so: lib/%.so
  138. $(INSTALL) -D -m 755 $< $@
  139. $(DESTDIR)$(libdir)/%: lib/%
  140. $(INSTALL) -D -m 644 $< $@
  141. $(DESTDIR)$(includedir)/bits/%: arch/$(ARCH)/bits/%
  142. $(INSTALL) -D -m 644 $< $@
  143. $(DESTDIR)$(includedir)/%: include/%
  144. $(INSTALL) -D -m 644 $< $@
  145. $(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
  146. $(INSTALL) -D -l $(libdir)/libc.so $@ || true
  147. install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
  148. install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
  149. install-tools: $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%)
  150. musl-git-%.tar.gz: .git
  151. git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@)
  152. musl-%.tar.gz: .git
  153. git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@)
  154. .PHONY: all clean install install-libs install-headers install-tools