0

I am trying to create an automated process with Waf to optimize, minify, etc. the source files of a website based on the HTML5 boilerplate's ANT build script. Part of this includes running all the PNG's in the img directory through two utilities, optipng and advpng.

This is my current best attempt at these tasks:

def build(ctx):
    ctx(
        rule = '${OPTIPNG} -quiet -o5 -i 1 -out ${TGT} ${SRC}',
        source = 'img1.png',
        target = 'img1-opt.png'
    )

    ctx(
        rule = '${ADVPNG} -z -1 ${SRC}',
        source = SOMETHING,
        target = SOMETHING ELSE
    )

I first run optipng on img1, where my first problem arises. I would like the output file to have the same name as the input file. However, setting target to the same name results in Waf detecting a deadlock. So, I moved ahead by appending a suffix.

advpng is a bit strange in that it doesn't create a new output file: it modifies the input file in place. So, I now need a way to access the output of optipng, which now resides in the build's output directory.

What is the proper way of accomplishing this task?

Scott Colby
  • 1,370
  • 12
  • 25

1 Answers1

0

The first problem is addressed in part by the waf book section 6.3. That is about copying, so you have to tweak it. Part of section 11 on Providing arbitrary configuration files is somewhat relevant, as well.

You have to write some Python to resolve the target file in the build directory. This script works:

top = '.'
out = 'build'

def configure(ctx):
    pass

def build(ctx):
    txt = ctx.srcnode.find_node('text.txt')

    ctx(
        rule = 'cp ${SRC} ${TGT}',
        source = txt,
        target = txt.get_bld() # Or: ctx.bldnode.make_node('text.txt')
    )

    ctx(
        rule = 'wc ${SRC} > ${TGT}',
        source = 'text.txt', # Or: txt.get_bld(),
        target = 'count.txt'
    )

For the second step, waf seemed to resolve the literal source file name in the build directory, but you could also use the bld node from step 1 to resolve it explicitly.

Shawn Hoover
  • 726
  • 8
  • 9