build: sanitize linking of tools and test programs
[ffmpeg.git] / library.mak
1 SRC_DIR := $(SRC_PATH)/lib$(NAME)
2
3 include $(SRC_PATH)/common.mak
4
5 LIBVERSION := $(lib$(NAME)_VERSION)
6 LIBMAJOR   := $(lib$(NAME)_VERSION_MAJOR)
7 INCINSTDIR := $(INCDIR)/lib$(NAME)
8 THIS_LIB   := $(SUBDIR)$($(CONFIG_SHARED:yes=S)LIBNAME)
9
10 all-$(CONFIG_STATIC): $(SUBDIR)$(LIBNAME)
11 all-$(CONFIG_SHARED): $(SUBDIR)$(SLIBNAME)
12
13 $(SUBDIR)%-test.o: $(SUBDIR)%-test.c
14         $(COMPILE_C)
15
16 $(SUBDIR)%-test.o: $(SUBDIR)%.c
17         $(COMPILE_C)
18
19 $(SUBDIR)x86/%.o: $(SUBDIR)x86/%.asm
20         $(DEPYASM) $(YASMFLAGS) -I $(<D)/ -M -o $@ $< > $(@:.o=.d)
21         $(YASM) $(YASMFLAGS) -I $(<D)/ -o $@ $<
22
23 $(OBJS) $(SUBDIR)%.h.o $(TESTOBJS): CPPFLAGS += -DHAVE_AV_CONFIG_H
24 $(TESTOBJS): CPPFLAGS += -DTEST
25
26 $(SUBDIR)$(LIBNAME): $(OBJS)
27         $(RM) $@
28         $(AR) $(ARFLAGS) $(AR_O) $^ $(EXTRAOBJS)
29         $(RANLIB) $@
30
31 install-headers: install-lib$(NAME)-headers install-lib$(NAME)-pkgconfig
32
33 install-libs-$(CONFIG_STATIC): install-lib$(NAME)-static
34 install-libs-$(CONFIG_SHARED): install-lib$(NAME)-shared
35
36 define RULES
37 $(EXAMPLES) $(TOOLS): LIB = $(FULLNAME:%=$(LD_LIB))
38 $(TESTPROGS):         LIB = $(SUBDIR)$(LIBNAME)
39
40 $(EXAMPLES) $(TESTPROGS) $(TOOLS): %$(EXESUF): %.o
41         $$(LD) $(LDFLAGS) $$(LD_O) $$(filter %.o,$$^) $$(LIB) $(FFEXTRALIBS) $$(ELIBS)
42
43 $(SUBDIR)$(SLIBNAME): $(SUBDIR)$(SLIBNAME_WITH_MAJOR)
44         $(Q)cd ./$(SUBDIR) && $(LN_S) $(SLIBNAME_WITH_MAJOR) $(SLIBNAME)
45
46 $(SUBDIR)$(SLIBNAME_WITH_MAJOR): $(OBJS) $(SUBDIR)lib$(NAME).ver $(DEP_LIBS)
47         $(SLIB_CREATE_DEF_CMD)
48         $$(LD) $(SHFLAGS) $(LDFLAGS) $$(LD_O) $$(filter %.o,$$^) $(FFEXTRALIBS) $(EXTRAOBJS)
49         $(SLIB_EXTRA_CMD)
50
51 clean::
52         $(RM) $(addprefix $(SUBDIR),*-example$(EXESUF) *-test$(EXESUF) $(CLEANFILES) $(CLEANSUFFIXES) $(LIBSUFFIXES)) \
53             $(CLEANSUFFIXES:%=$(SUBDIR)$(ARCH)/%) $(HOSTOBJS) $(HOSTPROGS)
54
55 distclean:: clean
56         $(RM) $(DISTCLEANSUFFIXES:%=$(SUBDIR)%) $(DISTCLEANSUFFIXES:%=$(SUBDIR)$(ARCH)/%)
57
58 install-lib$(NAME)-shared: $(SUBDIR)$(SLIBNAME)
59         $(Q)mkdir -p "$(SHLIBDIR)"
60         $$(INSTALL) -m 755 $$< "$(SHLIBDIR)/$(SLIB_INSTALL_NAME)"
61         $(Q)$(foreach F,$(SLIB_INSTALL_LINKS),cd "$(SHLIBDIR)" && $(LN_S) $(SLIB_INSTALL_NAME) $(F);)
62         $(if $(SLIB_INSTALL_EXTRA_SHLIB),$$(INSTALL) -m 644 $(SLIB_INSTALL_EXTRA_SHLIB:%=$(SUBDIR)%) "$(SHLIBDIR)")
63         $(if $(SLIB_INSTALL_EXTRA_LIB),$(Q)mkdir -p "$(LIBDIR)")
64         $(if $(SLIB_INSTALL_EXTRA_LIB),$$(INSTALL) -m 644 $(SLIB_INSTALL_EXTRA_LIB:%=$(SUBDIR)%) "$(LIBDIR)")
65
66 install-lib$(NAME)-static: $(SUBDIR)$(LIBNAME)
67         $(Q)mkdir -p "$(LIBDIR)"
68         $$(INSTALL) -m 644 $$< "$(LIBDIR)"
69         $(LIB_INSTALL_EXTRA_CMD)
70
71 install-lib$(NAME)-headers: $(addprefix $(SUBDIR),$(HEADERS) $(BUILT_HEADERS))
72         $(Q)mkdir -p "$(INCINSTDIR)"
73         $$(INSTALL) -m 644 $$^ "$(INCINSTDIR)"
74
75 install-lib$(NAME)-pkgconfig: $(SUBDIR)lib$(NAME).pc
76         $(Q)mkdir -p "$(LIBDIR)/pkgconfig"
77         $$(INSTALL) -m 644 $$^ "$(LIBDIR)/pkgconfig"
78
79 uninstall-libs::
80         -$(RM) "$(SHLIBDIR)/$(SLIBNAME_WITH_MAJOR)" \
81                "$(SHLIBDIR)/$(SLIBNAME)"            \
82                "$(SHLIBDIR)/$(SLIBNAME_WITH_VERSION)"
83         -$(RM) $(SLIB_INSTALL_EXTRA_SHLIB:%="$(SHLIBDIR)"%)
84         -$(RM) $(SLIB_INSTALL_EXTRA_LIB:%="$(LIBDIR)"%)
85         -$(RM) "$(LIBDIR)/$(LIBNAME)"
86
87 uninstall-headers::
88         $(RM) $(addprefix "$(INCINSTDIR)/",$(HEADERS) $(BUILT_HEADERS))
89         $(RM) "$(LIBDIR)/pkgconfig/lib$(NAME).pc"
90         -rmdir "$(INCINSTDIR)"
91 endef
92
93 $(eval $(RULES))
94
95 $(EXAMPLES) $(TOOLS): $(THIS_LIB) $(DEP_LIBS)
96 $(TESTPROGS): $(SUBDIR)$(LIBNAME) $(DEP_LIBS)
97
98 examples: $(EXAMPLES)
99 testprogs: $(TESTPROGS)