1
0

Makefile 6.1 KB

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