# common bits used by all libraries
#
-all: # make "all" default target
+# first so "all" becomes default target
+all: all-yes
ifndef SUBDIR
-vpath %.c $(SRC_DIR)
-vpath %.h $(SRC_DIR)
-vpath %.S $(SRC_DIR)
+vpath %.c $(SRC_DIR)
+vpath %.h $(SRC_DIR)
+vpath %.S $(SRC_DIR)
+vpath %.asm $(SRC_DIR)
+vpath %.v $(SRC_DIR)
+
+ifeq ($(SRC_DIR),$(SRC_PATH_BARE))
+BUILD_ROOT_REL = .
+else
+BUILD_ROOT_REL = ..
+endif
+
+ifndef V
+Q = @
+ECHO = printf "$(1)\t%s\n" $(2)
+BRIEF = CC AS YASM AR LD HOSTCC STRIP CP
+SILENT = DEPCC YASMDEP RM RANLIB
+MSG = $@
+M = @$(call ECHO,$(TAG),$@);
+$(foreach VAR,$(BRIEF), \
+ $(eval override $(VAR) = @$$(call ECHO,$(VAR),$$(MSG)); $($(VAR))))
+$(foreach VAR,$(SILENT),$(eval override $(VAR) = @$($(VAR))))
+$(eval INSTALL = @$(call ECHO,INSTALL,$$(^:$(SRC_DIR)/%=%)); $(INSTALL))
+endif
-ALLFFLIBS = avcodec avdevice avfilter avformat avutil postproc swscale
+ALLFFLIBS = avcodec avcore avdevice avfilter avformat avutil postproc swscale
-CFLAGS = -DHAVE_AV_CONFIG_H -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE \
- -D_ISOC9X_SOURCE -I$(BUILD_ROOT) -I$(SRC_PATH) \
- $(addprefix -I$(SRC_PATH)/lib,$(ALLFFLIBS)) $(OPTFLAGS)
+IFLAGS := -I$(BUILD_ROOT_REL) -I$(SRC_PATH)
+CPPFLAGS := $(IFLAGS) $(CPPFLAGS)
+CFLAGS += $(ECFLAGS)
+YASMFLAGS += $(IFLAGS) -Pconfig.asm
+
+HOSTCFLAGS += $(IFLAGS)
%.o: %.c
- $(CC) $(CFLAGS) $(LIBOBJFLAGS) -c -o $@ $<
+ $(CCDEP)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(CC_DEPFLAGS) -c $(CC_O) $<
%.o: %.S
- $(CC) $(CFLAGS) $(LIBOBJFLAGS) -c -o $@ $<
+ $(ASDEP)
+ $(AS) $(CPPFLAGS) $(ASFLAGS) $(AS_DEPFLAGS) -c -o $@ $<
%.ho: %.h
- $(CC) $(CFLAGS) $(LIBOBJFLAGS) -Wno-unused -c -o $@ -x c $<
+ $(CC) $(CPPFLAGS) $(CFLAGS) -Wno-unused -c -o $@ -x c $<
+
+%$(EXESUF): %.c
-%.d: %.c
- $(DEPEND_CMD) > $@
+%.ver: %.v
+ $(Q)sed 's/$$MAJOR/$($(basename $(@F))_VERSION_MAJOR)/' $^ > $@
-%.d: %.S
- $(DEPEND_CMD) > $@
+%.c %.h: TAG = GEN
-%.d: %.cpp
- $(DEPEND_CMD) > $@
+# Dummy rule to stop make trying to rebuild removed or renamed headers
+%.h:
+ @:
install: install-libs install-headers
+install-libs: install-libs-yes
uninstall: uninstall-libs uninstall-headers
-.PHONY: all depend dep clean distclean install* uninstall* tests
-endif
+.PHONY: all depend dep *clean install* uninstall* examples testprogs
-CFLAGS += $(CFLAGS-yes)
-OBJS += $(OBJS-yes)
-ASM_OBJS += $(ASM_OBJS-yes)
-CPP_OBJS += $(CPP_OBJS-yes)
-FFLIBS := $(FFLIBS-yes) $(FFLIBS)
-TESTS += $(TESTS-yes)
+# Disable suffix rules. Most of the builtin rules are suffix rules,
+# so this saves some time on slow systems.
+.SUFFIXES:
-FFEXTRALIBS := $(addprefix -l,$(addsuffix $(BUILDSUF),$(FFLIBS))) $(EXTRALIBS)
-FFLDFLAGS := $(addprefix -L$(BUILD_ROOT)/lib,$(FFLIBS)) $(LDFLAGS)
-
-SRCS := $(OBJS:.o=.c) $(ASM_OBJS:.o=.S) $(CPP_OBJS:.o=.cpp)
-OBJS := $(OBJS) $(ASM_OBJS) $(CPP_OBJS)
-
-SRCS := $(addprefix $(SUBDIR),$(SRCS))
-OBJS := $(addprefix $(SUBDIR),$(OBJS))
-TESTS := $(addprefix $(SUBDIR),$(TESTS))
+# Do not delete intermediate files from chains of implicit rules
+$(OBJS):
+endif
-ALLHEADERS := $(subst $(SRC_DIR)/,$(SUBDIR),$(wildcard $(SRC_DIR)/*.h))
-checkheaders: $(filter-out %_template.ho,$(ALLHEADERS:.h=.ho))
+OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
-DEPS := $(OBJS:.o=.d)
-depend dep: $(DEPS)
+CFLAGS += $(CFLAGS-yes)
+OBJS += $(OBJS-yes)
+FFLIBS := $(FFLIBS-yes) $(FFLIBS)
+TESTPROGS += $(TESTPROGS-yes)
-CLEANFILES += *.o *~ *.a *.lib *.so *.so.* *.dylib *.dll \
- *.def *.dll.a *.exp *.ho *.map
+FFEXTRALIBS := $(addprefix -l,$(addsuffix $(BUILDSUF),$(FFLIBS))) $(EXTRALIBS)
+FFLDFLAGS := $(addprefix -L$(BUILD_ROOT)/lib,$(ALLFFLIBS)) $(LDFLAGS)
-define RULES
-$(SUBDIR)%: $(SUBDIR)%.o $(LIBNAME)
- $(CC) $(FFLDFLAGS) -o $$@ $$^ $(FFEXTRALIBS)
+EXAMPLES := $(addprefix $(SUBDIR),$(addsuffix -example$(EXESUF),$(EXAMPLES)))
+OBJS := $(addprefix $(SUBDIR),$(sort $(OBJS)))
+TESTOBJS := $(addprefix $(SUBDIR),$(TESTOBJS))
+TESTPROGS := $(addprefix $(SUBDIR),$(addsuffix -test$(EXESUF),$(TESTPROGS)))
+HOSTOBJS := $(addprefix $(SUBDIR),$(addsuffix .o,$(HOSTPROGS)))
+HOSTPROGS := $(addprefix $(SUBDIR),$(addsuffix $(HOSTEXESUF),$(HOSTPROGS)))
-$(SUBDIR)%-test$(EXESUF): $(SUBDIR)%.c $(LIBNAME)
- $(CC) $(CFLAGS) $(FFLDFLAGS) -DTEST -o $$@ $$^ $(FFEXTRALIBS)
+DEP_LIBS := $(foreach NAME,$(FFLIBS),$(BUILD_ROOT_REL)/lib$(NAME)/$($(CONFIG_SHARED:yes=S)LIBNAME))
-clean::
- rm -f $(TESTS) $(addprefix $(SUBDIR),$(CLEANFILES))
+ALLHEADERS := $(subst $(SRC_DIR)/,$(SUBDIR),$(wildcard $(SRC_DIR)/*.h $(SRC_DIR)/$(ARCH)/*.h))
+SKIPHEADERS += $(addprefix $(ARCH)/,$(ARCH_HEADERS))
+SKIPHEADERS := $(addprefix $(SUBDIR),$(SKIPHEADERS-) $(SKIPHEADERS))
+checkheaders: $(filter-out $(SKIPHEADERS:.h=.ho),$(ALLHEADERS:.h=.ho))
-distclean:: clean
- rm -f $(DEPS)
-endef
+$(HOSTOBJS): %.o: %.c
+ $(HOSTCC) $(HOSTCFLAGS) -c -o $@ $<
-$(eval $(RULES))
+$(HOSTPROGS): %$(HOSTEXESUF): %.o
+ $(HOSTCC) $(HOSTLDFLAGS) -o $@ $< $(HOSTLIBS)
-tests: $(TESTS)
+CLEANSUFFIXES = *.d *.o *~ *.ho *.map *.ver
+DISTCLEANSUFFIXES = *.pc
+LIBSUFFIXES = *.a *.lib *.so *.so.* *.dylib *.dll *.def *.dll.a *.exp
--include $(DEPS)
+-include $(wildcard $(OBJS:.o=.d))