###############################################################################
# vlc (VideoLAN Client) common module Makefile
# (c)2001 VideoLAN
###############################################################################
# This file should be included by all module Makefiles
###############################################################################

#
# include the local Makefile
#
include Makefile

#
# Analyze the target we are asked to build
#
module_name := $(shell echo $(MAKECMDGOALS) | sed 's@.*/\(.*\)\..*@\1@')
suff := $(shell echo $(MAKECMDGOALS) | sed 's@.*\.@@' | tr so/a lo/o)

#
# Compiler flags
#
SRC_C := $(filter %.c,$($(module_name)_SOURCES))
SRC_CPP := $(filter %.cpp,$($(module_name)_SOURCES))
SRC_M := $(filter %.m,$($(module_name)_SOURCES))

ifeq (lo,$(suff))
extra_CFLAGS := $(plugins_CFLAGS) $($(module_name)_CFLAGS) \
                -DMODULE_NAME=$(module_name) -DMODULE_NAME_IS_$(module_name) \
                $($(module_name)_so_CFLAGS)
OBJ_ALL := $(SRC_C:%.c=%.lo.$(module_name)) $(SRC_CPP:%.cpp=%.lo.$(module_name))
else
extra_CFLAGS := $(builtins_CFLAGS) $($(module_name)_CFLAGS) \
                -DMODULE_NAME=$(module_name) -DMODULE_NAME_IS_$(module_name) \
                $($(module_name)_a_CFLAGS)
OBJ_ALL := $(SRC_C:%.c=%.o.$(module_name)) $(SRC_CPP:%.cpp=%.o.$(module_name)) \
		$(SRC_M:%.m=%.o.$(module_name))
endif

# 
# Standard dependencies
#
C_DEP := $(SRC_C:%.c=.dep/%.d)
CPP_DEP := $(SRC_CPP:%.cpp=.dep/%.dpp)
M_DEP := $(SRC_M:%.m=.dep/%.dm)

export

#
# Virtual targets
#
all:

clean:
#	rm -f $(PLUGIN_ALL) $(BUILTIN_ALL)
	rm -f *.o *.o.* *.lo *.lo.* *.obj *.moc *.moc.* *.bak
	rm -rf .dep

FORCE:

$(OBJ_ALL): ../../Makefile.modules ../../Makefile.dep ../../Makefile ../../Makefile.opts Makefile
$(OBJ_ALL): $(H_DEP:%=../../include/%)

$(C_DEP): %.d: FORCE
	@$(MAKE) -s --no-print-directory -f ../../Makefile.dep $@ CFLAGS="$(CFLAGS) $(extra_CFLAGS)"

$(CPP_DEP): %.dpp: FORCE
	@$(MAKE) -s --no-print-directory -f ../../Makefile.dep $@ CFLAGS="$(CFLAGS) $(extra_CFLAGS)"

$(M_DEP): %.dm: FORCE
	@$(MAKE) -s --no-print-directory -f ../../Makefile.dep $@ CFLAGS="$(CFLAGS) $(extra_CFLAGS)"

$(SRC_C:%.c=%.$(suff).$(module_name)): %.$(suff).$(module_name): .dep/%.d
$(SRC_C:%.c=%.$(suff).$(module_name)): %.$(suff).$(module_name): %.c
	$(CC) $(CFLAGS) $(extra_CFLAGS) -c $< -o $@

$(SRC_CPP:%.cpp=%.$(suff).$(module_name)): %.$(suff).$(module_name): .dep/%.dpp
$(SRC_CPP:%.cpp=%.$(suff).$(module_name)): %.$(suff).$(module_name): %.cpp
	$(CC) $(CFLAGS) $(extra_CFLAGS) -c $< -o $@

$(SRC_M:%.m=%.$(suff).$(module_name)): %.$(suff).$(module_name): .dep/%.dm
$(SRC_M:%.m=%.$(suff).$(module_name)): %.$(suff).$(module_name): %.m
	$(CC) $(CFLAGS) $(extra_CFLAGS) -c $< -o $@

# foo_CUSTOM lets us override all target rules for foo.so and foo.a
ifeq (,$($(module_name)_CUSTOM))
../$(module_name).so: $(EXTRA_DEP) $(OBJ_ALL)
	$(CC) $(OBJ_ALL) $(LDFLAGS) $(plugins_LDFLAGS) $($(module_name)_LDFLAGS) -o $@
	chmod a-x $@

../$(module_name).a: $(EXTRA_DEP) $(OBJ_ALL)
	rm -f $@
	$(AR) rc $@ $(OBJ_ALL)
	$(RANLIB) $@
endif

