## Copyright (c) 2009  Openismus GmbH  <http://www.openismus.com/>
##
## This file is part of mm-common.
##
## mm-common is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation, either version 2 of the License,
## or (at your option) any later version.
##
## mm-common is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with mm-common.  If not, see <http://www.gnu.org/licenses/>.

## Parameters:  binding_name, wrap_init_flags
## Overrides:   codegen_srcdir, codegen_m4_srcdir, binding_outputdir,
##              gmmproc, gen_wrap_init, gmmproc_flags
## Files:       files_codegen_m4, files_defs, files_hg, files_ccg
## Output:      dist_noinst_DATA, gmmproc_dependencies, other_built_sources,
##              MAINTAINERCLEANFILES

# Location of the module's gmmproc support files.
codegen_srcdir ?= $(top_srcdir)/codegen

# Location of the module's gmmproc M4 files.
codegen_m4_srcdir ?= $(codegen_srcdir)/m4

# Destination directory of the generated source files.
binding_outputdir ?= $(if $(srcdir:.=),$(dir $(top_srcdir)/$(subdir)),../)$(binding_name)

# Additional built sources not generated by gmmproc.
other_built_sources = $(binding_outputdir)/wrap_init.cc

# Where to put the stamp files for the gmmproc output.
binding_stampdir = $(srcdir)/.stamps

# Lists of qualified file names relative to the current directory.
binding_relfiles_m4 = $(addprefix $(codegen_m4_srcdir)/,$(files_codegen_m4))
binding_relfiles_hg = $(addprefix $(srcdir)/,$(files_hg))
binding_stampfiles  = $(patsubst %.hg,$(binding_stampdir)/%.stamp,$(files_hg))

# Distributed code generation source files.
dist_noinst_DATA = $(files_defs) $(files_hg) $(files_ccg)

# Delete stamps on make maintainer-clean.  The other generated source
# files are deleted by the make rules for the output directory.
MAINTAINERCLEANFILES = $(binding_stampdir)/*.stamp

# Dependencies of the gmmproc code generator targets.
gmmproc_dependencies = $(binding_relfiles_m4) $(files_defs)

# Default command lines for running the code generators.
gmmproc       ?= $(PERL) -I"$(GMMPROC_DIR)/pm" -- "$(GMMPROC_DIR)/gmmproc"
gen_wrap_init ?= $(PERL) -- "$(GMMPROC_DIR)/generate_wrap_init.pl"
gmmproc_flags ?= -I $(codegen_m4_srcdir) --defs $(srcdir)

# Automatically created output directories.
binding_mkdirs = $(binding_stampdir) $(binding_outputdir)/private

# Show names of generated files in silent rules output.
mm_0_gen = @echo '  GEN    $(binding_outputdir)/{$*.cc,$*.h,private/$*_p.h}';
mm_v_gen = $(if $(filter 0,$(or $(V),$(AM_DEFAULT_VERBOSITY))),$(mm_0_gen))

# Declare the built sources main targets.
all-local: $(binding_stampfiles) $(other_built_sources)

# Create the output directories if they do not exist already.
$(binding_mkdirs):
	$(AM_V_at)$(MKDIR_P) $@

# Generate the wrap_init.cc file using generate_wrap_init.pl.
$(binding_outputdir)/wrap_init.cc: $(binding_relfiles_hg) $(srcdir)/Makefile.in
	$(AM_V_GEN)$(gen_wrap_init) $(wrap_init_flags) $(binding_relfiles_hg) >$@

# Run the gmmproc code generator to produce the C++ binding code.
$(binding_stampdir)/%.stamp: %.hg %.ccg $(gmmproc_dependencies) | $(binding_mkdirs)
	@: >$@
	$(mm_v_gen)$(gmmproc) $(gmmproc_flags) $* $(srcdir) $(binding_outputdir)

# Instruct GNU make to delete the targets of a rule after it failed, in
# order to avoid the complication of handling that situation manually.
.DELETE_ON_ERROR:
