Makefile 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/dynlink.lo: arch/$(ARCH)/reloc.h
  69. crt/crt1.o crt/Scrt1.o: $(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. # This incantation ensures that changes to any subarch asm files will
  76. # force the corresponding object file to be rebuilt, even if the implicit
  77. # rule below goes indirectly through a .sub file.
  78. define mkasmdep
  79. $(dir $(patsubst %/,%,$(dir $(1))))$(notdir $(1:.s=.o)): $(1)
  80. endef
  81. $(foreach s,$(wildcard src/*/$(ARCH)*/*.s),$(eval $(call mkasmdep,$(s))))
  82. %.o: $(ARCH)$(ASMSUBARCH)/%.sub
  83. $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $(dir $<)$(shell cat $<)
  84. %.o: $(ARCH)/%.s
  85. $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
  86. %.o: %.c $(GENH) $(IMPH)
  87. $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
  88. %.lo: $(ARCH)$(ASMSUBARCH)/%.sub
  89. $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $(dir $<)$(shell cat $<)
  90. %.lo: $(ARCH)/%.s
  91. $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
  92. %.lo: %.c $(GENH) $(IMPH)
  93. $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
  94. lib/libc.so: $(LOBJS)
  95. $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS) -nostdlib -shared \
  96. -Wl,-e,_dlstart -Wl,-Bsymbolic-functions \
  97. -o $@ $(LOBJS) $(LIBCC)
  98. lib/libc.a: $(OBJS)
  99. rm -f $@
  100. $(AR) rc $@ $(OBJS)
  101. $(RANLIB) $@
  102. $(EMPTY_LIBS):
  103. rm -f $@
  104. $(AR) rc $@
  105. lib/%.o: crt/%.o
  106. cp $< $@
  107. lib/musl-gcc.specs: tools/musl-gcc.specs.sh config.mak
  108. sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
  109. tools/musl-gcc: config.mak
  110. printf '#!/bin/sh\nexec "$${REALGCC:-gcc}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
  111. chmod +x $@
  112. $(DESTDIR)$(bindir)/%: tools/%
  113. $(INSTALL) -D $< $@
  114. $(DESTDIR)$(libdir)/%.so: lib/%.so
  115. $(INSTALL) -D -m 755 $< $@
  116. $(DESTDIR)$(libdir)/%: lib/%
  117. $(INSTALL) -D -m 644 $< $@
  118. $(DESTDIR)$(includedir)/bits/%: arch/$(ARCH)/bits/%
  119. $(INSTALL) -D -m 644 $< $@
  120. $(DESTDIR)$(includedir)/%: include/%
  121. $(INSTALL) -D -m 644 $< $@
  122. $(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
  123. $(INSTALL) -D -l $(libdir)/libc.so $@ || true
  124. install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),)
  125. install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%)
  126. install-tools: $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%)
  127. musl-git-%.tar.gz: .git
  128. git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@)
  129. musl-%.tar.gz: .git
  130. git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@)
  131. .PRECIOUS: $(CRT_LIBS:lib/%=crt/%)
  132. .PHONY: all clean install install-libs install-headers install-tools