I'm working on using a Makefile to help perform build steps for my JS in my application. The goal is to use Make's ability to only deal with modified files to prevent extraneous copying and such.

To build the JS you need to

  1. copy the original .js files from the main dev directory to a build directory
  2. minimize the copied file and create a -min.js version of the file
  3. generate a meta.js which has the information on the YUI modules in the built JS files for the combo loader.

So I've been working on the Makefile working backwards. meta.js depends on -min.js which depends on $builddir/b/*.js which is copied from %origdir/*.js

In looking through things it seems I should be able to use %.js in order to catch any matching files, but when I try this out I get:

  make: *** No rule to make target `bookie/static/js/build/b/%-min.js', needed by `bookie/static/js/build/b/meta.js'.  Stop.

This is what I'm working with, anyone know what I'm doing wrong? I want that when I make js it copies over the changed .js files, minifies them, and then regenerates the meta.js.

# Makefile to help automate tasks in bookie
WD := $(shell pwd)
PY := bin/python
BOOKIE_JS = bookie/static/js/bookie
JS_BUILD_PATH = bookie/static/js/build
JS_META_SCRIPT = $(PY) scripts/js/generate_meta.py

EXTENSION = $(WD)/extensions
CHROME_BUILD = $(EXTENSION)/chrome_ext/lib


# JAVASCRIPT
#
# Javascript tools for building out combo loader build directory, out meta.js,
# and syncing things over to the chrome extension directory.

.PHONY: js
js: $(JS_BUILD_PATH)/b/meta.js

$(JS_BUILD_PATH)/b/meta.js: $(JS_BUILD_PATH)/b/%-min.js
    echo "META"
    $(JS_META_SCRIPT) -n YUI_MODULES -s $(JS_BUILD_PATH)/b/ \
        -o $(JS_BUILD_PATH)/b/meta.js \
        -x -min.js$

$(JS_BUILD_PATH)/b/%-min.js: $(JS_BUILD_PATH)/b/$*.js
    echo "MIN"
    rm $(JS_BUILD_PATH)/b/meta.js || true
    scripts/js/jsmin_all.py $(JS_BUILD_PATH)/b

$(JS_BUILD_PATH)/b/%.js: $(BOOKIE_JS)/$*.js
    echo "Initial"
    cp $? $(JS_BUILD_PATH)/b/
    cp $? $(CHROME_BUILD)/

.PHONY: clean_js
clean_js:
    rm -rf $(JS_BUILD_PATH)/* || true
share|improve this question
    
$* only works within recipes, not within dependencies, use % there – reinierpost Apr 3 '12 at 8:44
    
What is the $ doing there in -min.js$ ? – reinierpost Apr 3 '12 at 8:46
    
Sorry, I think the $ is left over from exeriments with matching only the -min files since both the -main and the non-min files are in that build directory. – Rick Apr 3 '12 at 10:56
up vote 0 down vote accepted

Make wildcards don't work the way you think. Let's start with this rule:

$(JS_BUILD_PATH)/b/%.js: $(BOOKIE_JS)/$*.js
    echo "Initial"
    cp $? $(JS_BUILD_PATH)/b/
    cp $? $(CHROME_BUILD)/

The % implies that it's a pattern rule, so Make expects a corresponding % in the prerequisite list. Instead it looks as if you're trying to use either the automatic variable $* (which can't be used in the prerequisite list) or the wildcard * (which can't be used without the wildcard command). Try it this way:

$(JS_BUILD_PATH)/b/%.js: $(BOOKIE_JS)/%.js
    echo "Initial"
    cp $? $(JS_BUILD_PATH)/b/
    cp $? $(CHROME_BUILD)/

This works, but only if you specify exactly which file(s) you want; it doesn't copy every .js file in the dev directory (more on this later).

Now for -min.js. I can't tell exactly how this rule works, but it looks as if you run a python script that acts on all *.js files in the directory at once. If that's correct, then it doesn't make sense to have a rule that makes foo-min.js from foo.js; there should be a rule to run the script, and we need a list of all the .js files it will need:

DEV_JS_FILES := $(wildcard $(BOOKIE_JS)/*.js)
BUILD_JS_FILES := $(patsubst $(BOOKIE_JS)/%.js,$(JS_BUILD_PATH)/b/%.js,$(DEV_JS_FILES))

.PHONY: min
min: $(BUILD_JS_FILES)
    echo "MIN"
    rm $(JS_BUILD_PATH)/b/meta.js || true
    scripts/js/jsmin_all.py $(JS_BUILD_PATH)/b

Finally, meta.js:

$(JS_BUILD_PATH)/b/meta.js: min
        echo "META"
        $(JS_META_SCRIPT) -n YUI_MODULES -s $(JS_BUILD_PATH)/b/ \
        -o $(JS_BUILD_PATH)/b/meta.js \
        -x -min.js$

EDIT:
I think I see what you're trying to do. There are a couple of ways to do it; the simplest is just to combine the three rules into one:

DEV_JS_FILES := $(wildcard $(BOOKIE_JS)/*.js)

$(JS_BUILD_PATH)/b/meta.js: $(DEV_JS_FILES)
    echo "Initial"
    cp $? $(JS_BUILD_PATH)/b/
    cp $? $(CHROME_BUILD)/
    echo "MIN"
    rm $(JS_BUILD_PATH)/b/meta.js || true
    scripts/js/jsmin_all.py $(JS_BUILD_PATH)/b
    rm $(addprefix $(JS_BUILD_PATH)/b/,$?)
    echo "META"
    $(JS_META_SCRIPT) -n YUI_MODULES -s $(JS_BUILD_PATH)/b/ \
  -o $(JS_BUILD_PATH)/b/meta.js \
  -x -min.js
    rm $(JS_BUILD_PATH)/b/*-min.js

This will act only on the .js files in dev that are newer than meta.js (or all of them if meta.js doesn't yet exist).

share|improve this answer
    
Thanks will try this out. You're correct in that the minify script is working on the whole directory. The goal was to get it to only work on the changed/updated files since the last Make run, I went wholesale with the whole directory because I've been having trouble getting the steps and the rules right. Sorry for not reverting that change before posting. – Rick Apr 3 '12 at 11:00

Your recipes are a bit difficult to understand because your utilities are not filters in standard Unix-style: they don't appear to take input filenames as arguments or read input from stdin, and don't appear to write output to stdout.

My guess is (based on the all in the filename) that your utilities convert all files in a particular directory. This is not something make can deal with gracefully: it is really designed to support rules that produce individual output files (the targets) from individual input files (the dependencies).

Pattern rules just let you specify such a rule for a whole category of files at once, but they don't specify processing a collection of files within a single rule. You appear to be trying to do just that.

I run into this make limitation all the time. As a workaround, you can use the directories the files are in as the targets and dependencies of your rules (if you make sure no other files will ever be created in there), or special marker files without content that you just use to mark completion of a rule. A proper solution would require a fundamental extension to the syntax and semantics of make rules.

share|improve this answer
    
Thanks, this is kind of what I was starting to think. I know I'm trying to shoe-horn in Make into the steps I need for 'building' my js files. I was hoping to use Make's ability to only deal with files that have changed since the last make run to prevent copying/minifying files that haven't changed. – Rick Apr 3 '12 at 10:58
    
You can: use the pattern rules to convert individual files, and a separate rule with a recipe that contains a for loop. Expressing the dependencies in that latter rule is the problem. The alternative is to use GNU make functions such as $(patsubst ...) to construct a list of targets from the list of source files on disk (see e.g. stackoverflow.com/questions/4971865/patsubst-on-makefile), but that's awkward, too. – reinierpost Apr 3 '12 at 11:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.