This file, 'phpc.mkh', is the 'header makefile' containing variables and macros used to process *.phpc source files.
# This file is:
# $(XMAKE_EXTENSIONS_DIR)/phpc.$(XMAKE_MAKEFILE_SUFFIX_HEADER)
### IMPORTANT:
# the command-line version of php binary must be
# specified in: XME_phpc_cmd
# this is built under <php_source_dir>/sapi/cli/php

### PRIVATE VARIABLES ##################################

# XME_phpc_require_once
# for backward compatability with PHP < 4.3.2
# works around PHP Bug #22775: Fatal error from require()
# exits with status=0
# $(1) required file; caller must supply quotes, if
# assigning output as a string
# $(2) error message
define XME_phpc_require_once
$$XME_phpc_required_file=$(1); \
$$XME_phpc_error_message=$(2);\
if (empty($$XME_phpc_error_message)){\
	$$XME_phpc_error_message="XME_phpc_require_once: failed \
	including file: \n";\
}\
$$success=include_once($(1)); \
if (!$$success){\
	trigger_error($$XME_phpc_error_message."    \
	".$$XME_phpc_required_file."\n", E_USER_ERROR); \
}
endef

# XME_phpc_cmd_options sets php.ini vars common to all
# macros:
# log_errors=1 - sends errors to error_log, or
# /dev/stderr
# error_log=  - empty value causes error_log() to use
# /dev/stderr;
#				inhibits prepending of date to error message
# display_errors=0 - don't want errors on STDOUT
# display_startup_errors=0 - we get errors on STDERR
# anyway with logging
define XME_phpc_cmd_options
$(if $(XMAKE_EXT_PHPC_PHP_INI),-c \
$(XMAKE_EXT_PHPC_PHP_INI)) \
-d log_errors=1 \
-d error_log= \
-d display_errors=0 \
-d display_startup_errors=0
endef

# XME_phpc_highlight_files_command
# $(1) - source file
# $(2) - output file
define XME_phpc_highlight_files_command
outfile="$(2)"; \
$(XME_phpc_cmd) \
$(XME_phpc_cmd_options) \
-r '\
$(call XME_phpc_require_once,\
getenv("XMAKE_HOME")."/config/XMExtensions/phpc.inc",\
"XME_phpc_highlight_files_command: failed including file: \
\n") \
$$XMAKE_XME_PHPC_ERROR_REPORTING_MASK = \
getenv("XMAKE_XME_PHPC_ERROR_REPORTING_MASK"); \
error_reporting($$XMAKE_XME_PHPC_ERROR_REPORTING_MASK); \
set_error_handler("phpc_error_handler"); \
highlight_file("$(1)"); \
' \
1>$(XMAKE_TMP_DIR)/XME_phpc_highlight_files_command.$$$$ \
\
|| \
{ echo '*** FAILURE (XME_phpc_highlight_files_command): \
processing source file: ' >&2; \
echo '$(1)' >&2; \
if test -s \
"$(XMAKE_TMP_DIR)/XME_phpc_highlight_files_command.$$$$"; \
then \
echo 'Standard Output follows:' >&2; \
cat \
$(XMAKE_TMP_DIR)/XME_phpc_highlight_files_command.$$$$ \
>&2; \
fi; \
rm -f $(XMAKE_TMP_DIR)/*.$$$$ 2>/dev/null; exit 1; \
}; \
cat \
$(XMAKE_TMP_DIR)/XME_phpc_highlight_files_command.$$$$ \
1>"$$outfile" 2>/dev/null; \
rm -f $(XMAKE_TMP_DIR)/*.$$$$ 2>/dev/null
endef
### PUBLIC VARIABLES & MACROS
# ##################################

# XME_phpc_cmd
# Command for generating output files from source files
# the command-line version of php binary must be
# specified in: XME_phpc_cmd
# this is built under <php_source_dir>/sapi/cli/php
XME_phpc_cmd:=$(XMAKE_XME_PHPC)

# XME_phpc_dependCmd
# Command to generate a list of dependencies from a
# source file (including the source file)
# $(1) - source file
# we suppress errors since we only want to get
# dependencies at this point:
# $$XMAKE_XME_PHPC_FATAL_ERROR_MASK=0;
# error_reporting(0);
# errors will show up again when we try to make the
# output file or run phpc.lint
define XME_phpc_dependCmd
cd $(dir $(1)); \
export \
XMAKE_XME_phpc_result=$(XMAKE_TMP_DIR)/phpc_dependCmd_result.$$$$; \
\
$(XME_phpc_cmd) \
$(XME_phpc_cmd_options) \
-r '\
$(call XME_phpc_require_once,\
getenv("XMAKE_HOME")."/config/XMExtensions/phpc.inc",\
"XME_phpc_dependCmd: failed including file: \n") \
$$XMAKE_XME_PHPC_FATAL_ERROR_MASK=0; \
error_reporting(0); \
phpc_setenv( "$(1)"); \
set_error_handler("phpc_error_handler"); \
$(call XME_phpc_require_once,\
"$(1)",\
"XME_phpc_dependCmd: failed including source file: \n") \
$$XMAKE_XME_phpc_result = \
getenv("XMAKE_XME_phpc_result"); \
$$files = phpc_get_prerequisites();\
$$str = join(" ",$$files); \
$$fp  = @fopen( $$XMAKE_XME_phpc_result, "w"); \
fputs( $$fp, $$str ); \
' \
1>$(XMAKE_TMP_DIR)/phpc_dependCmd_out.$$$$ \
|| \
{ echo '*** FAILURE (XME_phpc_dependCmd): processing \
source file: ' >&2; \
echo '$(1)' >&2; \
if test -s "$(XMAKE_TMP_DIR)/phpc_dependCmd_out.$$$$"; \
then \
echo 'Standard Output follows:' >&2; \
cat $(XMAKE_TMP_DIR)/phpc_dependCmd_out.$$$$ >&2 ; \
fi; \
rm -f $(XMAKE_TMP_DIR)/*.$$$$ 2>/dev/null; exit 1; \
}; \
cat $$XMAKE_XME_phpc_result 2>/dev/null; \
rm -f $(XMAKE_TMP_DIR)/*.$$$$ 2>/dev/null;
endef

# XME_phpc_lintCmd
# $(1) - name of variable set by the calling shell:
# 'XMAKE_XME_phpc_source'
# MUST NOT CALL: ini_set( "display_startup_errors","On");
# ini_set( "display_errors","On");
# FOR SOME REASON IT CAUSES ERROR OUTPUT NOT TO GET
# WRITTEN TO OUTPUT FILE
define XME_phpc_lintCmd
$(XME_phpc_cmd) \
$(XME_phpc_cmd_options) \
-r '\
$(call XME_phpc_require_once,\
getenv("XMAKE_HOME")."/config/XMExtensions/phpc.inc",\
"XME_phpc_lintCmd: failed including file: \n") \
$$XMAKE_XME_PHPC_FATAL_ERROR_MASK=$(XMAKE_XME_PHPC_LINT_FATAL_ERROR_MASK); \
\
phpc_setenv( "$(1)"); \
set_error_handler("phpc_error_handler"); \
$$$(1)=getenv("$(1)"); \
$(call XME_phpc_require_once,\
$$$(1),\
"XME_phpc_lintCmd: failed including source file: \n") \
' \
1>$(XMAKE_TMP_DIR)/XME_phpc_lintCmd.$$$$ \
|| \
{ echo '*** FAILURE (XME_phpc_lintCmd): processing source \
file: ' >&2; \
echo "$$$(1)" >&2; \
if test -s "$(XMAKE_TMP_DIR)/XME_phpc_lintCmd.$$$$"; then \
\
echo 'Standard Output follows:' >&2; \
cat $(XMAKE_TMP_DIR)/XME_phpc_lintCmd.$$$$ >&2; \
fi; \
rm -f $(XMAKE_TMP_DIR)/*.$$$$ 2>/dev/null; exit 1; \
}; \
rm -f $(XMAKE_TMP_DIR)/*.$$$$ 2>/dev/null
endef

# add phpc.inc to XM_finalTargets
# Otherwise it would be outside XMAKE_PROJECT_DIR and
# become a phony target,
# This would force rebuilding all .phpc files which
# included it
XM_finalTargets+=$(XMAKE_EXTENSIONS_DIR)/phpc.inc

# XME_phpc_dependencies
# Called from DEPENDENCY MAKEFILES rule
# $(1) - source file: never affects dependencies for this
# file type, may for other types
# $(2) - options: additional prerequisites
# Generates a list of prerequisites of ALL files of this
# type
# Additional prerequisites can be added using
# XME_explicitRule
define XME_phpc_dependencies
$(call XME_phpc_dependCmd,$(1)) $(2)
endef

# XME_phpc_outfileMap
# $(1) - source file names
# returns the names of the output files for valid source
# file names in $(1);
define XME_phpc_outfileMap
$(patsubst %.phpc,%,$(filter %.phpc,$(1)))
endef

# XME_phpc_outfileMacro
# Used to create output files from sources
# Called from XME_patternRules
# $(1) - source file; required
# $(2) - PHP source code; optional
# $(3) - output file; optional: defaults to $(call
# XME_phpc_outfileMap,$(1))
# TO CREATE A CUSTOM RULE: $(php_code) will be passed to
# ALL .phpc files
# EXAMPLE: set globals to simulate a server environment
# $(eval $(call
# XME_patternRule,phpc,$(prerequisites),$(php_code)))
# COMMAND RULE USAGE:
# $(targets): $(prerequisites)
#	@$(call
# XM_php_outfileMacro,$(source_file),$@,$(php_code))
define XME_phpc_outfileMacro
outfile="$(call XME_outfile,phpc,$(1),$(3))"; \
cd $(dir $(1)); \
$(XME_phpc_cmd) \
$(XME_phpc_cmd_options) \
-r '\
$(call XME_phpc_require_once,\
getenv("XMAKE_HOME")."/config/XMExtensions/phpc.inc",\
"phpc_outfileMacro: failed including file: \n") \
$$XMAKE_XME_PHPC_ERROR_REPORTING_MASK = \
getenv("XMAKE_XME_PHPC_ERROR_REPORTING_MASK"); \
error_reporting($$XMAKE_XME_PHPC_ERROR_REPORTING_MASK); \
phpc_setenv( "$(1)"); \
set_error_handler("phpc_error_handler"); \
/* execute any PHP code passed in: */ \
$(2) \
$(call XME_phpc_require_once,\
"$(1)",\
"XM_phpc_outfileMacro: failed including source file: \n") \
\
' 1>$(XMAKE_TMP_DIR)/phpc_outfileMacro.$$$$ \
|| \
{ echo '*** FAILURE (XME_phpc_outfileMacro): processing \
source file: ' >&2; \
echo '$(1)' >&2; \
if test -s "$(XMAKE_TMP_DIR)/phpc_outfileMacro.$$$$"; \
then \
echo 'Standard Output follows:' >&2; \
cat $(XMAKE_TMP_DIR)/phpc_outfileMacro.$$$$ >&2; \
fi; \
rm -f $(XMAKE_TMP_DIR)/*.$$$$ 2>/dev/null; exit 1; \
}; \
cat $(XMAKE_TMP_DIR)/phpc_outfileMacro.$$$$ 1>"$$outfile" \
2>/dev/null; \
rm -f $(XMAKE_TMP_DIR)/*.$$$$ 2>/dev/null
endef

# add manually created files to XM_finalTargets
XM_finalTargets+=$(XME_phpc_cmd)

# XME_phpc_highlight_files
# static rule for creating syntax highlighted files from
# PHP sources
# $(1) - source files
# $(2) - output file suffix: appended to source file:
# $(1)$(2)
# each output file is added to XM_outfiles
# USAGE: $(eval $(call
# XME_phpc_highlight_files,$(sources),.html))
define XME_phpc_highlight_files
_XME_phpc_highlighted_files:=$(patsubst %,%$(2),$(1))
$(call XM_externalTargetsHandler,$(1))
$(call \
XM_append,XM_outfiles,$$(_XME_phpc_highlighted_files))

$$(_XME_phpc_highlighted_files): %$(2) : % \
$$(XM_makefiles) $$(XME_phpc_makefiles)
	@$(XM_silent) || echo 'file: \
	$(XMAKE_EXTENSIONS_DIR)/phpc.mkh'
	@$(XM_silent) || echo 'rule: XME_phpc_highlight_files'
	@$(XM_silent) || echo 'target: $$@'
	@$$(call XME_phpc_highlight_files_command,$$<,$$@)
endef

# XME_phpc_highlight_file
# explicit rule for creating a single syntax highlighted
# file from PHP source
# $(1) - source file
# $(2) - output file
# each output file is added to XM_outfiles
# USAGE: $(eval $(call
# XME_phpc_highlight_file,$(source),$(outfile)))
define XME_phpc_highlight_file
$(call XM_externalTargetsHandler,$(1))
$(call XM_append,XM_outfiles,$(2))

$(2): $(1) $$(XM_makefiles) $$(XME_phpc_makefiles)
	@$(XM_silent) || echo 'file: \
	$(XMAKE_EXTENSIONS_DIR)/phpc.mkh'
	@$(XM_silent) || echo 'rule: XME_phpc_highlight_file'
	@$(XM_silent) || echo 'target: $$@'
	@$$(call XME_phpc_highlight_files_command,$$<,$$@)
endef