#---*- Makefile -*-------------------------------------------------------------
#$Author: antanas $
#$Date: 2023-05-15 19:28:46 +0300 (Mon, 15 May 2023) $
#$Revision: 9580 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.1/makefiles/Makelocal-YAPP-module $
#------------------------------------------------------------------------------
#*
#  Builds a Perl module from a Yapp grammar file.
#**

# The following variables should be predefined in the Makeconf file:
# ${PKG_LIB_DIR}
#                   Name of the local Perl library directory in which
#                   the assembled Perl module should be placed.
# ${PKG_PATH}
#                   Relative path inside the local Perl library directory
#                   in which the assembled Perl module should be placed.
#                   If not defined, the relative path is determined
#                   from the ${PKG_PREFIX} variable value.
# ${PKG_PREFIX}
#                   Prefix of the Perl package name. For example, if
#                   the desired package name is 'X::Y::Z', then the 
#                   'X::Y::' value should be passed. Please note, that
#                   the 'Z' portion of the package name is determined
#                   automatically and is equal to the name of the
#                   current working directory.

PKG_LIB_DIR ?= lib
PKG_PREFIX  ?=
PKG_PATH    ?= ${patsubst %/,%, ${subst ::,/,${PKG_PREFIX}}}

PKG_NAME := ${notdir $(shell pwd)}.pm
PM_FILE  := ${PKG_NAME:%.pm=${PKG_LIB_DIR}/${PKG_PATH}/%.pm}

VFILE = .version

# The '^\#' is used instead of '^#' to circumvent a bug in older versions of
# Make (e.g GNU Make 4.2.1). However, the '^\#' sequence also causes a warning
# about a stray backslash in newer versions of grep (e.g. GNU grep 3.8) unless
# the -P option is used. Alternatively, the following Perl program may be used:
## VERSION := $(shell perl -n -e 'print unless /^\#/;' $(VFILE))
VERSION := $(shell grep -v -P '^\#' $(VFILE))

.PRECIOUS: ${PM_FILE}
.PHONY: all build install cleanAll cleanAll-YAPP-module

all: build

build: ${PM_FILE}

${PKG_LIB_DIR}/${PKG_PATH}/%.pm: grammar.yp
	yapp -v -m ${PKG_PREFIX}$* -o - $< | sed 's/@VERSION@/${VERSION}/' > $@

install: ${PM_FILE}
	mkdir --parents ${PREFIX}/lib/perl5/${PKG_PATH}
	install --mode 644 $< ${PREFIX}/lib/perl5/${PKG_PATH}

cleanAll distclean: cleanAll-YAPP-module

cleanAll-YAPP-module:
	rm -f ${PM_FILE} *.output
