common.mak 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #
  2. # common bits used by all libraries
  3. #
  4. DEFAULT_X86ASMD=.dbg
  5. ifeq ($(DBG),1)
  6. X86ASMD=$(DEFAULT_X86ASMD)
  7. else
  8. X86ASMD=
  9. endif
  10. ifndef SUBDIR
  11. BIN2CEXE = ffbuild/bin2c$(HOSTEXESUF)
  12. BIN2C = $(BIN2CEXE)
  13. ifndef V
  14. Q = @
  15. ECHO = printf "$(1)\t%s\n" $(2)
  16. BRIEF = CC CXX OBJCC HOSTCC HOSTLD AS X86ASM AR LD STRIP CP WINDRES NVCC BIN2C
  17. SILENT = DEPCC DEPHOSTCC DEPAS DEPX86ASM RANLIB RM
  18. MSG = $@
  19. M = @$(call ECHO,$(TAG),$@);
  20. $(foreach VAR,$(BRIEF), \
  21. $(eval override $(VAR) = @$$(call ECHO,$(VAR),$$(MSG)); $($(VAR))))
  22. $(foreach VAR,$(SILENT),$(eval override $(VAR) = @$($(VAR))))
  23. $(eval INSTALL = @$(call ECHO,INSTALL,$$(^:$(SRC_DIR)/%=%)); $(INSTALL))
  24. endif
  25. # Prepend to a recursively expanded variable without making it simply expanded.
  26. PREPEND = $(eval $(1) = $(patsubst %,$$(%), $(2)) $(value $(1)))
  27. # NASM requires -I path terminated with /
  28. IFLAGS := -I. -I$(SRC_LINK)/
  29. CPPFLAGS := $(IFLAGS) $(CPPFLAGS)
  30. CFLAGS += $(ECFLAGS)
  31. CCFLAGS = $(CPPFLAGS) $(CFLAGS)
  32. OBJCFLAGS += $(EOBJCFLAGS)
  33. OBJCCFLAGS = $(CPPFLAGS) $(CFLAGS) $(OBJCFLAGS)
  34. ASFLAGS := $(CPPFLAGS) $(ASFLAGS)
  35. # Use PREPEND here so that later (target-dependent) additions to CPPFLAGS
  36. # end up in CXXFLAGS.
  37. $(call PREPEND,CXXFLAGS, CPPFLAGS CFLAGS)
  38. X86ASMFLAGS += $(IFLAGS:%=%/) -I$(<D)/ -Pconfig.asm
  39. HOSTCCFLAGS = $(IFLAGS) $(HOSTCPPFLAGS) $(HOSTCFLAGS)
  40. LDFLAGS := $(ALLFFLIBS:%=$(LD_PATH)lib%) $(LDFLAGS)
  41. define COMPILE
  42. $(call $(1)DEP,$(1))
  43. $($(1)) $($(1)FLAGS) $($(2)) $($(1)_DEPFLAGS) $($(1)_C) $($(1)_O) $(patsubst $(SRC_PATH)/%,$(SRC_LINK)/%,$<)
  44. endef
  45. COMPILE_C = $(call COMPILE,CC)
  46. COMPILE_CXX = $(call COMPILE,CXX)
  47. COMPILE_S = $(call COMPILE,AS)
  48. COMPILE_M = $(call COMPILE,OBJCC)
  49. COMPILE_X86ASM = $(call COMPILE,X86ASM)
  50. COMPILE_HOSTC = $(call COMPILE,HOSTCC)
  51. COMPILE_NVCC = $(call COMPILE,NVCC)
  52. COMPILE_MMI = $(call COMPILE,CC,MMIFLAGS)
  53. COMPILE_MSA = $(call COMPILE,CC,MSAFLAGS)
  54. COMPILE_LSX = $(call COMPILE,CC,LSXFLAGS)
  55. COMPILE_LASX = $(call COMPILE,CC,LASXFLAGS)
  56. %_mmi.o: %_mmi.c
  57. $(COMPILE_MMI)
  58. %_msa.o: %_msa.c
  59. $(COMPILE_MSA)
  60. %_lsx.o: %_lsx.c
  61. $(COMPILE_LSX)
  62. %_lasx.o: %_lasx.c
  63. $(COMPILE_LASX)
  64. %.o: %.c
  65. $(COMPILE_C)
  66. %.o: %.cpp
  67. $(COMPILE_CXX)
  68. %.o: %.m
  69. $(COMPILE_M)
  70. %.s: %.c
  71. $(CC) $(CCFLAGS) -S -o $@ $<
  72. %.o: %.S
  73. $(COMPILE_S)
  74. %_host.o: %.c
  75. $(COMPILE_HOSTC)
  76. %$(DEFAULT_X86ASMD).asm: %.asm
  77. $(DEPX86ASM) $(X86ASMFLAGS) -M -o $@ $< > $(@:.asm=.d)
  78. $(X86ASM) $(X86ASMFLAGS) -e $< | sed '/^%/d;/^$$/d;' > $@
  79. %.o: %.asm
  80. $(COMPILE_X86ASM)
  81. -$(if $(ASMSTRIPFLAGS), $(STRIP) $(ASMSTRIPFLAGS) $@)
  82. %.o: %.rc
  83. $(WINDRES) $(IFLAGS) $(foreach ARG,$(CC_DEPFLAGS),--preprocessor-arg "$(ARG)") -o $@ $<
  84. %.i: %.c
  85. $(CC) $(CCFLAGS) $(CC_E) $<
  86. %.h.c:
  87. $(Q)echo '#include "$*.h"' >$@
  88. $(BIN2CEXE): ffbuild/bin2c_host.o
  89. $(HOSTLD) $(HOSTLDFLAGS) $(HOSTLD_O) $^ $(HOSTEXTRALIBS)
  90. %.metal.air: %.metal
  91. $(METALCC) $< -o $@
  92. %.metallib: %.metal.air
  93. $(METALLIB) --split-module-without-linking $< -o $@
  94. %.metallib.c: %.metallib $(BIN2CEXE)
  95. $(BIN2C) $< $@ $(subst .,_,$(basename $(notdir $@)))
  96. %.ptx: %.cu $(SRC_PATH)/compat/cuda/cuda_runtime.h
  97. $(COMPILE_NVCC)
  98. ifdef CONFIG_PTX_COMPRESSION
  99. %.ptx.gz: TAG = GZIP
  100. %.ptx.gz: %.ptx
  101. $(M)gzip -c9 $(patsubst $(SRC_PATH)/%,$(SRC_LINK)/%,$<) >$@
  102. %.ptx.c: %.ptx.gz $(BIN2CEXE)
  103. $(BIN2C) $(patsubst $(SRC_PATH)/%,$(SRC_LINK)/%,$<) $@ $(subst .,_,$(basename $(notdir $@)))
  104. else
  105. %.ptx.c: %.ptx $(BIN2CEXE)
  106. $(BIN2C) $(patsubst $(SRC_PATH)/%,$(SRC_LINK)/%,$<) $@ $(subst .,_,$(basename $(notdir $@)))
  107. endif
  108. clean::
  109. $(RM) $(BIN2CEXE)
  110. %.c %.h %.pc %.ver %.version: TAG = GEN
  111. # Dummy rule to stop make trying to rebuild removed or renamed headers
  112. %.h %_template.c:
  113. @:
  114. # Disable suffix rules. Most of the builtin rules are suffix rules,
  115. # so this saves some time on slow systems.
  116. .SUFFIXES:
  117. # Do not delete intermediate files from chains of implicit rules
  118. $(OBJS):
  119. endif
  120. include $(SRC_PATH)/ffbuild/arch.mak
  121. OBJS += $(OBJS-yes)
  122. SLIBOBJS += $(SLIBOBJS-yes)
  123. SHLIBOBJS += $(SHLIBOBJS-yes)
  124. STLIBOBJS += $(STLIBOBJS-yes)
  125. FFLIBS := $($(NAME)_FFLIBS) $(FFLIBS-yes) $(FFLIBS)
  126. TESTPROGS += $(TESTPROGS-yes)
  127. LDLIBS = $(FFLIBS:%=%$(BUILDSUF))
  128. FFEXTRALIBS := $(LDLIBS:%=$(LD_LIB)) $(foreach lib,EXTRALIBS-$(NAME) $(FFLIBS:%=EXTRALIBS-%),$($(lib))) $(EXTRALIBS)
  129. OBJS := $(sort $(OBJS:%=$(SUBDIR)%))
  130. SLIBOBJS := $(sort $(SLIBOBJS:%=$(SUBDIR)%))
  131. SHLIBOBJS := $(sort $(SHLIBOBJS:%=$(SUBDIR)%))
  132. STLIBOBJS := $(sort $(STLIBOBJS:%=$(SUBDIR)%))
  133. TESTOBJS := $(TESTOBJS:%=$(SUBDIR)tests/%) $(TESTPROGS:%=$(SUBDIR)tests/%.o)
  134. TESTPROGS := $(TESTPROGS:%=$(SUBDIR)tests/%$(EXESUF))
  135. HOSTOBJS := $(HOSTPROGS:%=$(SUBDIR)%.o)
  136. HOSTPROGS := $(HOSTPROGS:%=$(SUBDIR)%$(HOSTEXESUF))
  137. TOOLS += $(TOOLS-yes)
  138. TOOLOBJS := $(TOOLS:%=tools/%.o)
  139. TOOLS := $(TOOLS:%=tools/%$(EXESUF))
  140. HEADERS += $(HEADERS-yes)
  141. PATH_LIBNAME = $(foreach NAME,$(1),lib$(NAME)/$($(2)LIBNAME))
  142. DEP_LIBS := $(foreach lib,$(FFLIBS),$(call PATH_LIBNAME,$(lib),$(CONFIG_SHARED:yes=S)))
  143. STATIC_DEP_LIBS := $(foreach lib,$(FFLIBS),$(call PATH_LIBNAME,$(lib)))
  144. SRC_DIR := $(SRC_PATH)/lib$(NAME)
  145. ALLHEADERS := $(subst $(SRC_DIR)/,$(SUBDIR),$(wildcard $(SRC_DIR)/*.h $(SRC_DIR)/$(ARCH)/*.h))
  146. SKIPHEADERS += $(ARCH_HEADERS:%=$(ARCH)/%) $(SKIPHEADERS-)
  147. SKIPHEADERS := $(SKIPHEADERS:%=$(SUBDIR)%)
  148. HOBJS = $(filter-out $(SKIPHEADERS:.h=.h.o),$(ALLHEADERS:.h=.h.o))
  149. PTXOBJS = $(filter %.ptx.o,$(OBJS))
  150. $(HOBJS): CCFLAGS += $(CFLAGS_HEADERS)
  151. checkheaders: $(HOBJS)
  152. .SECONDARY: $(HOBJS:.o=.c) $(PTXOBJS:.o=.c) $(PTXOBJS:.o=.gz) $(PTXOBJS:.o=)
  153. alltools: $(TOOLS)
  154. $(HOSTOBJS): %.o: %.c
  155. $(COMPILE_HOSTC)
  156. $(HOSTPROGS): %$(HOSTEXESUF): %.o
  157. $(HOSTLD) $(HOSTLDFLAGS) $(HOSTLD_O) $^ $(HOSTEXTRALIBS)
  158. $(OBJS): | $(sort $(dir $(OBJS)))
  159. $(HOBJS): | $(sort $(dir $(HOBJS)))
  160. $(HOSTOBJS): | $(sort $(dir $(HOSTOBJS)))
  161. $(SLIBOBJS): | $(sort $(dir $(SLIBOBJS)))
  162. $(SHLIBOBJS): | $(sort $(dir $(SHLIBOBJS)))
  163. $(STLIBOBJS): | $(sort $(dir $(STLIBOBJS)))
  164. $(TESTOBJS): | $(sort $(dir $(TESTOBJS)))
  165. $(TOOLOBJS): | tools
  166. OUTDIRS := $(OUTDIRS) $(dir $(OBJS) $(HOBJS) $(HOSTOBJS) $(SLIBOBJS) $(SHLIBOBJS) $(STLIBOBJS) $(TESTOBJS))
  167. CLEANSUFFIXES = *.d *.gcda *.gcno *.h.c *.ho *.map *.o *.pc *.ptx *.ptx.gz *.ptx.c *.ver *.version *$(DEFAULT_X86ASMD).asm *~ *.ilk *.pdb
  168. LIBSUFFIXES = *.a *.lib *.so *.so.* *.dylib *.dll *.def *.dll.a
  169. define RULES
  170. clean::
  171. $(RM) $(HOSTPROGS) $(TESTPROGS) $(TOOLS)
  172. endef
  173. $(eval $(RULES))
  174. -include $(wildcard $(OBJS:.o=.d) $(HOSTOBJS:.o=.d) $(TESTOBJS:.o=.d) $(HOBJS:.o=.d) $(SHLIBOBJS:.o=.d) $(STLIBOBJS:.o=.d) $(SLIBOBJS:.o=.d)) $(OBJS:.o=$(DEFAULT_X86ASMD).d)