[jamming] The right way to do
Craig Allsop
cjamallsop at gmail.com
Thu Nov 15 15:19:43 PST 2007
Hi.
You just need to change your thinking, I mean why call this rule
JpegToPng and restrict it to just one job? Why not call it
ProcessAnyType? The only difference might be the type of tool you use
to convert. Heres how..
You can set a variable 'on' your targets to determine the type of
'conversion tool' to use in your actions. You can go one step further
and set a TOOL on a given type of extension, i.e.
TOOL on .jpg = jpeg2png ;
SUFFIX on .jpg = .png ;
To enable these variables for a scope, call another rule under the
influence of the target with your desired variables. See below where I
call ProcessAnyWithSuffix under the influence of the suffix of the
source file which is the same target TOOL & SUFFIX was set on above.
rule ProcessAnyType files
{
local file ;
for file in [ FGristFiles $(files) ]
{
on $(file:S) ProcessAnyWithSuffix $(file) ;
}
}
Now with TOOL & SUFFIX in scope, we can generate the output name by
using the input but we also must set TOOL on the output target as this
is the target the actions will be under the influence of, jam does
this for you.
rule ProcessAnyWithSuffix in
{
local out = $(in:S=$(SUFFIX)) ;
SEARCH on $(in) = $(SEARCH_SOURCE) ;
MakeLocate $(out) : $(LOCATE_TARGET) ;
TOOL on $(out) = $(TOOL) ;
Depends $(out) : $(in) ;
Clean clean : $(out) ;
ProcessOne $(out) : $(in) ;
}
# this is executed "on $(<)"
actions ProcessOne
{
$(TOOL) $(<) $(>)
}
Now just set your tool & suffix, as many as you want for different
types of files.
ProcessAny pic1.jpg pic2.jpg pic3.jpg ;
You could even set a different output each type of conversion...
LOCATE_TARGET on .jpeg = [ FDirName $(TOP) pngfiles ] ;
If LOCATE_TARGET isn't set for a given extension the default
LOCATE_TARGET is used. Use this idea for other variables such as
OPTIONS for your tools. e.g.
actions ProcessOne
{
$(TOOL) $(OPTIONS) $(<) $(>)
}
You can set OPTIONS for a given extension type or perhaps OPTIONS on a
given single source file which might take preference over the
extension (and that over the global OPTIONS). To do this just add
another rule between ProcessAnyType & ProcessAnyWithSuffix this time
calling under the influence of the source.
actions ProcessAnyWithSource in
{
on $(in) ProcessAnyWithSuffix $(in) ;
}
Add this line to ProcessAnyWithSuffix:
OPTIONS on $(out) = $(OPTIONS) ;
Set your OPTIONS for a source like:
OPTIONS on [ FGristFiles mysource.jpg ] = /switch ;
If you don't like ProcessOne appearing for all different types of
conversions, then you could give all your tools their own actions just
by calling the action using the tool name, i.e. $(TOOL) $(out) : $(in)
from the ProcessAnyWithSuffix rule.
The possibilities are endless...
Craig.
On Nov 16, 2007 2:09 AM, Mildred <ml.mildred593 at online.fr> wrote:
> I'm writing Jam rules and I'm wondering if I do it right, because it
> seems there is a heavy skeleton I have to follow.
>
> For example, if someone asks me to write a rule for some program that
> converts jpeg files to png, I would write in my Jamrules:
>
> rule JpegToPng {
> local png = [ FGristFiles $(1) ] ;
> local jpeg = [ FGristFiles $(2) ] ;
> SEARCH on $(jpeg) = $(SEARCH_SOURCE) ;
> MakeLocate $(png) : $(LOCATE_TARGET) ;
> JpegToPng1 $(png) : $(jpeg) ;
> }
> rule JpegToPng1 png : jpeg {
> Depends $(png) : $(jpeg) ;
> Clear clear : $(png) ;
> }
> actions JpegToPng1 {
> jpeg2png '$(>)' '$(<)'
> }
>
> Isn't there a more effecient way to do that ? A way to avoid always
> writing the same thing at the beginning of my rules (that is the
> grist, the SEARCH and LOCATE variables that are always the same).
>
> How do you do ?
More information about the jamming
mailing list