Makefile 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/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. LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
  46. -include config.mak
  47. all: $(ALL_LIBS) $(ALL_TOOLS)
  48. install: install-libs install-headers install-tools
  49. clean:
  50. rm -f crt/*.o
  51. rm -f $(OBJS)
  52. rm -f $(LOBJS)
  53. rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so
  54. rm -f $(ALL_TOOLS)
  55. rm -f $(GENH) $(GENH_INT)
  56. rm -f include/bits
  57. distclean: clean
  58. rm -f config.mak
  59. include/bits:
  60. @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
  61. ln -sf ../arch/$(ARCH)/bits $@
  62. include/bits/alltypes.h.in: include/bits
  63. include/bits/alltypes.h: include/bits/alltypes.h.in include/alltypes.h.in tools/mkalltypes.sed
  64. sed -f tools/mkalltypes.sed include/bits/alltypes.h.in include/alltypes.h.in > $@
  65. src/internal/version.h: $(wildcard VERSION .git)
  66. printf '#define VERSION "%s"\n' "$$(sh tools/version.sh)" > $@
  67. src/internal/version.lo: src/internal/version.h
  68. src/ldso/dlstart.lo src/ldso/dynlink.lo: src/internal/dynlink.h arch/$(ARCH)/reloc.h
  69. crt/crt1.o crt/Scrt1.o src/ldso/dlstart.lo: $(wildcard arch/$(ARCH)/crt_arch.h)
  70. crt/Scrt1.o: CFLAGS += -fPIC
  71. OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=src/%))
  72. $(OPTIMIZE_SRCS:%.c=%.o) $(OPTIMIZE_SRCS:%.c=%.lo): CFLAGS += -O3
  73. MEMOPS_SRCS = src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c
  74. $(MEMOPS_SRCS:%.c=%.o) $(MEMOPS_SRCS:%.c=%.lo): CFLAGS += $(CFLAGS_MEMOPS)
  75. NOSSP_SRCS = $(wildcard crt/*.c) \
  76. src/env/__libc_start_main.c src/env/__init_tls.c \
  77. src/thread/__set_thread_area.c src/env/__stack_chk_fail.c \
  78. src/string/memset.c src/string/memcpy.c \
  79. src/ldso/dlstart.c src/ldso/dynlink.c
  80. $(NOSSP_SRCS:%.c=%.o) $(NOSSP_SRCS:%.c=%.lo): CFLAGS += $(CFLAGS_NOSSP)
  81. $(CRT_LIBS): CFLAGS += -DCRT
  82. # This incantation ensures that changes to any subarch asm files will
  83. # force the corresponding object file to be rebuilt, even if the implicit
  84. # rule below goes indirectly through a .sub file.
  85. define mkasmdep
  86. $(dir $(patsubst %/,%,$(dir $(1))))$(notdir $(1:.s=.o)): $(1)
  87. endef
  88. $(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s))))
  89. %.o: $(ARCH)$(ASMSUBARCH)/%.sub
  90. $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<)
  91. %.o: $(ARCH)/%.s
  92. $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
  93. %.o: %.c $(GENH) $(IMPH)
  94. $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
  95. %.lo: $(ARCH)$(ASMSUBARCH)/%.sub
  96. $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<)
  97. %.lo: $(ARCH)/%.s
  98. $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
  99. %.lo: %.c $(GENH) $(IMPH)
  100. $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
  101. lib/libc.so: $(LOBJS)
  102. $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS) -nostdlib -shared \
  103. -Wl,-e,_dlstart -Wl,-Bsymbolic-functions \
  104. -o $@ $(LOBJS) $(LIBCC)
  105. lib/libc.a: $(OBJS)
  106. rm -f $@
  107. $(AR) rc $@ $(OBJS)
  108. $(RANLIB) $@
  109. $(EMPTY_LIBS):
  110. rm -f $@
  111. $(AR) rc $@
  112. lib/%.o: crt/%.o
  113. cp $< $@
  114. lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
  115. sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
  116. tools/musl-gcc: config.mak
  117. printf '#!/bin/sh\nexec "$${REALGCC:-gcc}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
  118. chmod +x $@
  119. $(DESTDIR)$(bindir)/%: tools/%
  120. $(INSTALL) -D $< $@
  121. $(DESTDIR)$(libdir)/%.so: lib/%.so
  122. $(INSTALL) -D -m 755 $< $@
  123. $(DESTDIR)$(libdir)/%: lib/%
  124. $(INSTALL) -D -m 644 $< $@
  125. $(DESTDIR)$(includedir)/bits/%: arch/$(ARCH)/bits/%
  126. $(INSTALL) -D -m 644 $< $@
  127. $(DESTDIR)$(includedir)/%: include/%
  128. $(INSTALL) -D -m 644 $< $@
  129. $(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
  130. $(INSTALL) -D -l $(libdir)/libc.so $@ || true
  131. install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
  132. install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
  133. install-tools: $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%)
  134. musl-git-%.tar.gz: .git
  135. git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@)
  136. musl-%.tar.gz: .git
  137. git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@)
  138. .PRECIOUS: $(CRT_LIBS:lib/%=crt/%)
  139. .PHONY: all clean install install-libs install-headers install-tools