1# -*- Makefile -*-
2# $Id: mpost.mk.in.in,v 1.4 2007/08/26 20:38:26 dan Exp $
3#
4# Makefile fragment for processing METAPOST files to produce postscript
5#
6# Copyright (c) 2007 Dan McMahill
7# All rights reserved.
8#
9# This code is derived from software written by Dan McMahill
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19# 3. All advertising materials mentioning features or use of this software
20#    must display the following acknowledgement:
21#        This product includes software developed by Dan McMahill
22#  4. The name of the author may not be used to endorse or promote products
23#     derived from this software without specific prior written permission.
24#
25#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32#  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35#  SUCH DAMAGE.
36#
37
38
39#######################################
40#
41# README:
42#
43# To use this makefile fragment, set the variable:
44#	MPOSTSRCS	= the METAPOST input files (.mp files)
45# in your top level Makefile.  You can also specify
46# MPOSTDIRS which is a list of directories containing
47# METAPOST files.  All mpost files in the specified directories
48# are added to MPOSTSRCS
49#
50# The following variables may be overridden in your top level Makefile
51#
52#  MPOST	  = the METAPOST executable
53#  MPOST_FLAGS	  = flags passed to ${MPOST}
54#
55#######################################
56
57
58# how to call METAPOST
59MPOST?=		@MPOST@
60MPOST_FLAGS?=
61
62#######################################
63#
64# Process MPOSTDIRS, adding to MPOSTSRCS
65#
66#######################################
67
68BMK:.if defined(MPOSTDIRS)
69BMK:.for __tmp__ in ${MPOSTDIRS}
70BMK:.if exists(${__tmp__})
71BMK:_MPOST_ADD_${__tmp__}!=	ls ${__tmp__}/*.mp
72BMK:MPOSTSRCS+=	${_MPOST_ADD_${__tmp__}}
73BMK:.endif
74BMK:.endfor
75BMK:.endif
76
77GMK:ifdef MPOSTDIRS
78GMK:MPOSTSRCS+= $(foreach __tmp__,$(MPOSTDIRS),$(wildcard $(__tmp__)/*.mp))
79GMK:endif
80
81#######################################
82#
83# Process proj_MPOSTDIRS, adding to proj_MPOSTSRCS
84#
85#######################################
86
87BMK:.for __name__ in ${NAME}
88BMK:.if defined(${__name__}_MPOSTDIRS)
89BMK:.for __tmp__ in ${${__name__}_MPOSTDIRS}
90BMK:.if exists(${__tmp__})
91BMK:_MPOST_ADD2_${__tmp__}!=	ls ${__tmp__}/*.mp
92BMK:${__name__}_MPOSTSRCS+=	${_MPOST_ADD2_${__tmp__}}
93BMK:.endif
94BMK:.endfor
95BMK:.endif
96BMK:.endfor
97
98GMK:define mpostdirs_template
99GMK:ifdef $(1)_MPOSTDIRS
100GMK:$(1)_MPOSTSRCS+= $(foreach __tmp2__,$($(1)_MPOSTDIRS),$(wildcard $(__tmp2__)/*.mp))
101GMK:endif
102GMK:endef
103GMK:$(foreach __tmp__,$(NAME),$(eval $(call mpostdirs_template,$(__tmp__))))
104
105#######################################
106#
107# Process proj_MPOSTSRCS to add to
108# proj_OTHER dependencies
109#
110#######################################
111
112
113MPOSTOBJS?=      $(MPOSTSRCS:.mp=.mpx)
114ALLMPOSTOBJS+=	$(MPOSTOBJS)
115
116BMK:.for __tmp__ in ${NAME}
117BMK:.if defined(${__tmp__}_MPOSTSRCS)
118BMK:${__tmp__}_MPOSTOBJS?=	${${__tmp__}_MPOSTSRCS:.mp=.mpx}
119BMK:${__tmp__}_OTHER+=	${${__tmp__}_MPOSTOBJS}
120BMK:ALLMPOSTOBJS+=	${${__tmp__}_MPOSTOBJS}
121BMK:.endif
122BMK:${__tmp__}_DIST+=	${${__tmp__}_MPOSTSRCS} ${MPOSTSRCS}
123BMK:.endfor
124
125GMK:# note, this had to be done in two steps (create proj_MPOSTOBJS,
126GMK:# and then add to proj_OTHER and ALLMPOSTOBJS) with GNU make 3.80
127GMK:# don't really know why.
128GMK:define mpostobjs_template
129GMK:ifdef $(1)_MPOSTSRCS
130GMK:$(1)_MPOSTOBJS?=	${$(1)_MPOSTSRCS:.mp=.mpx}
131GMK:endif
132GMK:$(1)_DIST+=		${$(1)_MPOSTSRCS}  ${MPOSTSRCS}
133GMK:endef
134GMK:$(foreach __tmp__,$(NAME),$(eval $(call mpostobjs_template,$(__tmp__))))
135
136GMK:define mpostdeps_template
137GMK:ifdef $(1)_MPOSTSRCS
138GMK:$(1)_OTHER+=	${$(1)_MPOSTOBJS}
139GMK:ALLMPOSTOBJS+=	${$(1)_MPOSTOBJS}
140GMK:endif
141GMK:endef
142GMK:$(foreach __tmp__,$(NAME),$(eval $(call mpostdeps_template,$(__tmp__))))
143
144
145#######################################
146#
147# Add the dependencies
148#
149#######################################
150
151
152OTHER+=	$(MPOSTOBJS)
153all::		$(ALLMPOSTOBJS)
154
155
156# we do this funny thing because neither BSD make nor GNU make
157# seem to like make conditionals in the middle of shell code
158BMK:.if defined(USE_PDFLATEX)
159GMK:ifdef USE_PDFLATEX
160_MPOST_PDF_REMOVE_=	$$fn
161BMK:.endif
162GMK:endif
163
164clean::
165BMK:.for __tmp__ in $(ALLMPOSTOBJS)
166BMK:	$(RM) -f $(__tmp__:Q)
167BMK:.endfor
168GMK:	$(RM) -f $(ALLMPOSTOBJS)
169	for mp in $(ALLMPOSTOBJS) ; do \
170		mpb=`basename $${mp} .mpx` ; \
171		for f in $${mpb}.ps $${mpb}.[0-9]* "" ; do \
172			if test "X$$f" != "X" ; then \
173				fn=`basename $$f .ps` ; \
174				fn=$${fn}.pdf ; \
175				$(RM) -f $$f ${_MPOST_PDF_REMOVE_}; \
176			fi ; \
177		done ; \
178	done
179
180#######################################
181#
182# The suffix rules
183#
184#######################################
185
186.SUFFIXES : .mp .mpx
187
188# if we are using pdflatex, then we probably need to convert our
189# postscript output to PDF
190
191.mp.mpx :
192BMK:	$(MPOST:Q) $(MPOST_FLAGS) $(MPOST_EPS_FLAGS) $*.mp
193GMK:	$(MPOST) $(MPOST_FLAGS) $(MPOST_EPS_FLAGS) $*.mp
194BMK:.if defined(MPOST_TWICE)
195GMK:ifdef MPOST_TWICE
196BMK:	$(MPOST:Q) $(MPOST_FLAGS) $(MPOST_EPS_FLAGS) $*.mp
197GMK:	$(MPOST) $(MPOST_FLAGS) $(MPOST_EPS_FLAGS) $*.mp
198BMK:.endif
199GMK:endif
200BMK:.if defined(USE_PDFLATEX)
201GMK:ifdef USE_PDFLATEX
202	for f in $*.ps $*.[0-9]* "" ; do \
203		if test "X$$f" != "X" ; then \
204			fn=`basename $$f .ps` ; \
205			fn=$${fn}.pdf ; \
206BMK:			${PS2PDF:Q} ${PS2PDF_FLAGS} $$f $$fn ; \
207GMK:			${PS2PDF} ${PS2PDF_FLAGS} $$f $$fn ; \
208		fi ; \
209	done
210BMK:.endif
211GMK:endif
212
213
214