## Copyright (c) 2001
## The gtkmm development team.
##
## **** Common rules for inclusion in Makefile.am ****
## Included from something/src/Makefile.am
##
## Used variable:	Example content:
##
## sublib_name		= gdkmm
## sublib_namespace     = Gdk

## files_defs		= gdk.defs gdk_pixbuf.defs

tools_dir	= $(top_srcdir)/tools
tools_dir_m4= $(top_srcdir)/tools/m4
tools_dir_pm= $(top_srcdir)/tools/pm
extra_defs_dir = $(top_builddir)/tools/extra_defs_gen

gensrc_destdir = $(srcdir)/../$(sublib_name)
stamp_dir = $(srcdir)/.stamps
stamp_plugin_dir = $(builddir)/.stamps
destdir_stamp_dir = $(gensrc_destdir)/.stamps

include $(tools_dir)/Makefile_list_of_sources.am_fragment
tools_m4	= $(files_tools_m4:%.m4=$(tools_dir_m4)/%.m4)
tools_plugin_m4	= $(files_tools_plugin_m4:%.m4=$(tools_dir_m4)/%.m4)
# tools_pm	= $(files_tools_pm:%.pm=$(tools_dir_pm)/%.pm)

include $(srcdir)/../src/Makefile_list_of_hg.am_fragment
files_all_ccg	= $(files_all_hg:%.hg=%.ccg)
files_all_plugin_ccg = $(files_all_plugin_hg:%.hg=%.ccg)
files_h		= $(files_all_hg:%.hg=$(gensrc_destdir)/%.h)
files_stamp	= $(files_all_hg:%.hg=$(stamp_dir)/stamp-%)
files_stamp_plugin = $(cppnames_plugin_all:%=$(stamp_plugin_dir)/stamp-%-plugin)
destdir_files_stamp = $(files_patched_hg:%.hg=$(destdir_stamp_dir)/stamp-%)

#Installed gmmproc stuff, from glibmm:
gmmproc_path = $(GMMPROC)
gmmproc_dir = $(GMMPROC_DIR)

# We use our own m4 and pm files as well as the ones installed by gtkmm:
# Our override m4 include seems to need to be before the default one.
gmmproc_args	= -I $(tools_dir_m4) --defs $(srcdir)
run_gmmproc	= $(gmmproc_path) $(gmmproc_args)

# The normal Glib::wrap() table initialization:
gen_wrap_init_in = $(tools_dir)/generate_wrap_init.pl.in
gen_wrap_init_path = $(tools_dir)/generate_wrap_init.pl
gen_wrap_init_args = --namespace=$(sublib_namespace) --parent_dir=$(sublib_parentdir)
run_gen_wrap_init  = $(PERL_PATH) $(gen_wrap_init_path) $(gen_wrap_init_args)

# The Gst::wrap() table initialization:
gen_gst_wrap_init_in = $(tools_dir)/generate_gst_wrap_init.pl.in
gen_gst_wrap_init_path = $(tools_dir)/generate_gst_wrap_init.pl
gen_gst_wrap_init_args = --namespace=$(sublib_namespace) --parent_dir=$(sublib_parentdir)
run_gen_gst_wrap_init = $(PERL_PATH) $(gen_gst_wrap_init_path) $(gen_gst_wrap_init_args)

# The generate_plugin_gmmproc_file utility
generate_plugin_gmmproc_file_path = $(extra_defs_dir)/generate_plugin_gmmproc_file
generate_plugin_gmmproc_file_args = --namespace=$(sublib_namespace) --main-defs=$(main_defs) --target=$(sublib_parentdir)
run_generate_plugin_gmmproc_file = $(generate_plugin_gmmproc_file_path) $(generate_plugin_gmmproc_file_args)

files_all_plugin_hg_with_path = $(patsubst %.hg,$(srcdir)/%.hg,\
				$(files_all_plugin_hg))
files_all_plugin_camel_hg_with_path = $(patsubst %,$(stamp_plugin_dir)/%.hg,\
				      $(cppnames_plugin_all))


EXTRA_DIST	= Makefile_list_of_hg.am_fragment $(files_defs) \
		  $(files_all_hg) $(files_all_ccg) $(files_all_plugin_hg) \
		  $(files_all_plugin_ccg)


if MAINTAINER_MODE
generate_plugin_dep = $(generate_plugin_gmmproc_file_path)
endif

# The source file from which the license header may be extracted and placed in
# plug-in generated .hg and .ccg files (use a file that probably will not
# change because it will be a plug-in generation dependency):
license_header_source_file = $(gensrc_destdir)/version.h

# This is to generate plugin .hg files.  The generated .hg file s piped through
# m4 using the macros in tools/m4/ctocpp_base.m4 which then produces the final
# .hg file.  Finally, the camel hg file is linked to the final .hg file to
# signal the successful generation of the .hg file.
$(files_all_plugin_camel_hg_with_path): $(generate_plugin_dep) \
	$(tools_plugin_m4) $(files_defs) $(license_header_source_file)
	$(eval hg_lowercase = $$(shell echo $$@ | tr [:upper:] [:lower:]))
	sed -n '1,/^$$/p' $(license_header_source_file) > \
		$(srcdir)/$(notdir $(hg_lowercase))
	$(run_generate_plugin_gmmproc_file) --hg \
		$(notdir $(subst .hg,,$@)) | $(M4) -I $(tools_dir_m4) >> \
		$(srcdir)/$(notdir $(hg_lowercase))
	ln -sf $(realpath $(srcdir))/$(notdir $(hg_lowercase)) $(builddir)/$@

# This is to generate plugin .ccg files.  The .ccg file is generated and then
# the camel ccg file is linked to the generated .ccg file to signal the
# successful generation of the .hg file.
$(files_all_plugin_camel_hg_with_path:.hg=.ccg): $(generate_plugin_dep) \
	$(tools_plugin_m4) $(files_defs) $(license_header_source_file)
	$(eval ccg_lowercase = $$(shell echo $$@ | tr '[:upper:]' '[:lower:]'))
	sed -n '1,/^$$/p' $(license_header_source_file) > \
		$(srcdir)/$(notdir $(ccg_lowercase))
	$(run_generate_plugin_gmmproc_file) --ccg \
		$(notdir $(subst .ccg,,$@)) >>  \
		$(srcdir)/$(notdir $(ccg_lowercase))
	ln -sf $(realpath $(srcdir))/$(notdir $(ccg_lowercase)) $(builddir)/$@

$(stamp_dir)/stamp-%: %.hg %.ccg $(tools_m4) $(files_defs)
	$(run_gmmproc) $(notdir $*) $(srcdir) $(gensrc_destdir)
	@echo 'timestamp' > $@

# This is to run the generated plug-in .hg and .ccg files through gmmproc
# (they're generated if necessary by the .../%.hg and .../%.ccg dependencies.
$(stamp_plugin_dir)/stamp-%-plugin: $(stamp_plugin_dir)/%.hg \
	$(stamp_plugin_dir)/%.ccg $(tools_m4) $(tools_plugin_m4) $(files_defs)
	$(eval plugin_name_lowercase = \
		$$(shell echo $$* | tr '[:upper:]' '[:lower:]'))
	$(run_gmmproc) $(plugin_name_lowercase) $(srcdir) $(gensrc_destdir)
	@echo 'timestamp' > $@

# This is to patch generated .h files in $(gensrc_destdir) (e.g. taglist.h).
$(destdir_stamp_dir)/stamp-%: $(gensrc_destdir)/%.h
	patch -Ns $(gensrc_destdir)/$*.h $(gensrc_destdir)/$*.h.patch || true
	rm -f $(gensrc_destdir)/$*.h.rej
	touch $@

files_hg_with_path	= $(patsubst %.hg,$(srcdir)/%.hg,$(files_all_hg))

$(gensrc_destdir)/wrap_init.cc: $(gen_wrap_init_in) $(files_hg_with_path) \
	$(files_all_plugin_hg_with_path)
	$(run_gen_wrap_init) $(files_all_hg:%.hg=$(srcdir)/%.hg) \
	  $(files_all_plugin_hg:%.hg=$(srcdir)/%.hg) > $@

$(gensrc_destdir)/gst_wrap_init.cc: $(gen_gst_wrap_init_in) \
	$(files_hg_with_path)
	$(run_gen_gst_wrap_init) $(files_all_hg:%.hg=$(srcdir)/%.hg) >$@

# This is to create the plugin .stamps directory (if builddir is different than
# srcdir)
$(stamp_plugin_dir):
	@(test -d $(stamp_plugin_dir) || mkdir $(stamp_plugin_dir))

create-stamp-dir: $(stamp_plugin_dir)
	@(test -d $(stamp_dir) || mkdir $(stamp_dir))
	@(test -d $(destdir_stamp_dir) || mkdir $(destdir_stamp_dir))

if MAINTAINER_MODE
all-local: create-stamp-dir $(files_stamp) $(files_stamp_plugin) \
	$(gensrc_destdir)/wrap_init.cc $(gensrc_destdir)/gst_wrap_init.cc \
	$(destdir_files_stamp)
endif

maintainer-clean-local:
	rm -rf $(stamp_dir) $(stamp_plugin_dir) $(destdir_stamp_dir) \
		$(files_all_plugin_hg) $(files_all_plugin_ccg)
