1# -*- Makefile -*-
2# $Id: lgrind.mk.in.in,v 1.7 2010/09/21 12:14:58 dan Exp $
3#
4# Makefile fragment for processing lgrind files to produce postscript
5#
6# Copyright (c) 2005, 2010 Dan McMahill
7#
8# Copyright (c) 2005 Peter Bex
9# All rights reserved.
10#
11# This code is derived from software written by Dan McMahill
12#
13# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions
15# are met:
16# 1. Redistributions of source code must retain the above copyright
17#    notice, this list of conditions and the following disclaimer.
18# 2. Redistributions in binary form must reproduce the above copyright
19#    notice, this list of conditions and the following disclaimer in the
20#    documentation and/or other materials provided with the distribution.
21# 3. All advertising materials mentioning features or use of this software
22#    must display the following acknowledgement:
23#        This product includes software developed by Dan McMahill
24#  4. The name of the author may not be used to endorse or promote products
25#     derived from this software without specific prior written permission.
26#
27#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34#  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37#  SUCH DAMAGE.
38#
39
40
41#######################################
42#
43# TODO:
44# - Add a way to deal with specifying the language on a per-file
45#   or maybe per-directory basis.  Maybe per-directory is a good
46#   start.
47#
48# - Add support for files with no extension.  Need to make sure
49#   we don't have problems with CVS/ directories.  In fact, we
50#   need to make sure we don't have issues with foo.bar directories.
51#
52# - Add support for inline lgrind sources using the %(...)% notation
53#
54# README:
55#
56# To use this makefile fragment, set the variable:
57#	LGRINDSRCS	= the source files to process
58# in your top level Makefile.  You can also specify
59# LGRINDDIRS which is a list of directories containing
60# source files.  All source files in the specified directories
61# are added to LGRINDSRCS
62#
63# The following variables may be overridden in your top level Makefile
64#
65#  LGRIND	= the lgrind executable
66#  LGRIND_FLAGS	= flags passed to lgrind for exporting to tex
67#
68#######################################
69
70
71# how to call lgrind to convert source files to TeX
72LGRIND?=	@LGRIND@
73LGRIND_FLAGS?=	-i
74
75#######################################
76#
77# Process LGRINDDIRS, adding to LGRINDSRCS
78#
79#######################################
80
81# pick out $(LGRINDDIRS)/*.* except for *.tex for each
82# specified directory.  This is how we will
83# deal with basically any source code file type.  The
84# exception will be things like Makefiles which do not
85# have an extension usually.
86
87# While we're processing these directories, check for a directory
88# specific LGRIND_FLAGS (i.e. mydir_LGRIND_FLAGS).  If it is defined
89# then assign it to the file specific lgrind flags for each source
90# file if the source file does not already have its own file specific
91# flags
92
93BMK:.if defined(LGRINDDIRS)
94BMK:.for __tmp__ in ${LGRINDDIRS}
95BMK:.if exists(${__tmp__})
96BMK:_LGRIND_ADDALL_${__tmp__}!=	ls ${__tmp__}/*.*
97BMK:_LGRIND_ADD_${__tmp__}=	${_LGRIND_ADDALL_${__tmp__}:C/.*.tex$//g}
98BMK:LGRINDSRCS+=	${_LGRIND_ADD_${__tmp__}}
99BMK:.  if defined(${__tmp__}_LGRIND_FLAGS)
100BMK:.    for __tmp2__ in ${_LGRIND_ADD_${__tmp__}}
101BMK:${__tmp2__}_LGRIND_FLAGS?=	${${__tmp__}_LGRIND_FLAGS}
102BMK:.    endfor
103BMK:.  endif
104BMK:.endif
105BMK:.endfor
106BMK:.endif
107
108
109GMK:define lgrinddir_flag_template
110GMK:$(1)_LGRIND_FLAGS?=	$($(2)_LGRIND_FLAGS)
111GMK:endef
112
113GMK:define lgrindsrcs_add_template
114GMK:LGRINDSRCS+=	${_LGRIND_ADD_$(1)}
115GMK:ifdef $(1)_LGRIND_FLAGS
116GMK:$(foreach __tmp2__,$(_LGRIND_ADD_$(1)),$(eval $(call lgrinddir_flag_template,$(__tmp2__),$(1))))
117GMK:endif
118GMK:endef
119
120GMK:define lgrinddirs_template
121GMK:_LGRIND_ADD_$(1)=	$(filter-out %.tex,$(wildcard $(1)/*.*))
122GMK:endef
123GMK:$(foreach __tmp__,$(LGRINDDIRS),$(eval $(call lgrinddirs_template,$(__tmp__))))
124GMK:$(foreach __tmp__,$(LGRINDDIRS),$(eval $(call lgrindsrcs_add_template,$(__tmp__))))
125
126
127#######################################
128#
129# Process proj_LGRINDDIRS, adding to proj_LGRINDSRCS
130#
131#######################################
132
133BMK:.for __name__ in ${NAME}
134BMK:.if defined(${__name__}_LGRINDDIRS)
135BMK:.for __tmp__ in ${${__name__}_LGRINDDIRS}
136BMK:.if exists(${__tmp__})
137BMK:_LGRIND_ADDALL2_${__tmp__}!=ls ${__tmp__}/*.*
138BMK:_LGRIND_ADD2_${__tmp__}=	${_LGRIND_ADDALL2_${__tmp__}:C/.*.tex$//g}
139BMK:${__name__}_LGRINDSRCS+=	${_LGRIND_ADD2_${__tmp__}}
140BMK:.  if defined(${__tmp__}_LGRIND_FLAGS)
141BMK:.    for __tmp2__ in ${_LGRIND_ADD2_${__tmp__}}
142BMK:${__tmp2__}_LGRIND_FLAGS?=	${${__tmp__}_LGRIND_FLAGS}
143BMK:.    endfor
144BMK:.  endif
145BMK:.endif
146BMK:.endfor
147BMK:.endif
148BMK:${__name__}_DIST+=	${${__name__}_LGRINDSRCS} ${LGRINDSRCS}
149BMK:.endfor
150
151
152GMK:define proj_lgrindsrcs_add_template
153GMK:$(2)_LGRINDSRCS+=	${_LGRIND_ADD2_$(1)}
154GMK:ifdef $(1)_LGRIND_FLAGS
155GMK:$(foreach __tmp2__,$(_LGRIND_ADD2_$(1)),$(eval $(call lgrinddir_flag_template,$(__tmp2__),$(1))))
156GMK:endif
157GMK:endef
158
159GMK:define proj_lgrinddirs_template
160GMK:_LGRIND_ADD2_$(1)=	$(filter-out %.tex,$(wildcard $(1)/*.*))
161GMK:endef
162
163GMK:define proj_lgrind_template
164GMK:ifdef $(1)_LGRINDDIRS
165GMK:$(foreach __tmp2__,$($(1)_LGRINDDIRS),$(eval $(call proj_lgrinddirs_template,$(__tmp2__))))
166GMK:$(foreach __tmp2__,$($(1)_LGRINDDIRS),$(eval $(call proj_lgrindsrcs_add_template,$(__tmp2__),$(1))))
167GMK:endif
168GMK:$(1)_DIST+=	$($(1)_LGRINDSRCS) $(LGRINDSRCS)
169GMK:endef
170GMK:$(foreach __tmp__,$(NAME),$(eval $(call proj_lgrind_template,$(__tmp__))))
171
172
173#######################################
174#
175# Process proj_LGRINDSRCS to add to
176# prog_OTHER dependencies
177#
178#######################################
179
180LGRINDOBJS?=	$(LGRINDSRCS:=.tex)
181ALLLGRINDOBJS+=	$(LGRINDOBJS)
182
183BMK:.for __tmp__ in ${NAME}
184BMK:.if defined(${__tmp__}_LGRINDSRCS)
185BMK:${__tmp__}_LGRINDOBJS?=	${${__tmp__}_LGRINDSRCS:=.tex}
186BMK:${__tmp__}_OTHER+=	${${__tmp__}_LGRINDOBJS}
187BMK:ALLLGRINDOBJS+=	${${__tmp__}_LGRINDOBJS}
188BMK:.endif
189BMK:.endfor
190
191GMK:# note, this had to be done in two steps (create proj_LGRINDOBJS,
192GMK:# and then add to proj_OTHER and ALLLGRINDOBJS) with GNU make 3.80
193GMK:# don't really know why.
194GMK:define lgrindobjs_template
195GMK:ifdef $(1)_LGRINDSRCS
196GMK:$(1)_LGRINDOBJS?=	${$(1)_LGRINDSRCS:=.tex}
197GMK:endif
198GMK:endef
199GMK:$(foreach __tmp__,$(NAME),$(eval $(call lgrindobjs_template,$(__tmp__))))
200
201GMK:define lgrinddeps_template
202GMK:ifdef $(1)_LGRINDSRCS
203GMK:$(1)_OTHER+=	${$(1)_LGRINDOBJS}
204GMK:ALLLGRINDOBJS+=	${$(1)_LGRINDOBJS}
205GMK:endif
206GMK:endef
207GMK:$(foreach __tmp__,$(NAME),$(eval $(call lgrinddeps_template,$(__tmp__))))
208
209
210#######################################
211#
212# Add the dependencies
213#
214#######################################
215
216
217OTHER+=	$(LGRINDOBJS)
218all::		$(ALLLGRINDOBJS)
219
220clean::
221BMK:.for __tmp__ in $(ALLLGRINDOBJS)
222BMK:	$(RM) -f $(__tmp__:Q)
223BMK:.endfor
224GMK:	$(RM) -f $(ALLLGRINDOBJS)
225
226
227#######################################
228#
229# And add the build rules.
230#
231# Note that we do this as a loop with explicit
232# rules for each file rather than a suffix
233# rule because we want to work on any input
234# file that lgrind can deal with
235#
236#######################################
237
238BMK:.for __tmp__ in ${ALLLGRINDOBJS}
239BMK:${__tmp__}: ${__tmp__:.tex=}
240BMK:	$(LGRIND:Q) $(LGRIND_FLAGS) ${${__tmp__:.tex=}_LGRIND_FLAGS} \
241BMK:		-o $@ ${__tmp__:.tex=}
242BMK:.endfor
243
244GMK:define lgrindrun_template
245GMK:$(1): $(1:.tex=)
246GMK:	$(LGRIND) $(LGRIND_FLAGS) $($(1:.tex=)_LGRIND_FLAGS) -o $(1) $(1:.tex=)
247GMK:endef
248GMK:$(foreach __tmp__,$(ALLLGRINDOBJS),$(eval $(call lgrindrun_template,$(__tmp__))))
249
250