11

I am trying to write a basic makefile that combines multiple js files into a single one and then does the same but compresses them.

So far I have this one that can make the compressed version fine.

# Set the source directory
srcdir = src/

# Create the list of modules
modules =   ${srcdir}core.js\
            ${srcdir}sizzle.js\
            ${srcdir}json2.js\
            ${srcdir}ajax.js\
            ${srcdir}attribute.js\
            ${srcdir}content.js\
            ${srcdir}cookie.js\
            ${srcdir}css.js\
            ${srcdir}event.js\
            ${srcdir}json.js\
            ${srcdir}location.js\
            ${srcdir}opacity.js\
            ${srcdir}ready.js\
            ${srcdir}size.js\
            ${srcdir}init.js

# Compress all of the modules into spark.js
spark.js: ${modules}
    java -jar yuicompressor.jar -o $@ $^

Does anyone know how I would go about adding an uncompressed version called spark-dev.js? I have been trying to use cat but I didn't get very far. This is my first makefile I have ever written.

EDIT I tried this code with cat

spark-dev.js: ${modules}
    cat $@ $^
Olical
  • 39,703
  • 12
  • 54
  • 77

1 Answers1

10

You were almost there :-) This should work:

spark-dev.js: ${modules}
    cat > $@ $^

Background: The function of cat is to (try to) open all the files listed on its command line, and dump the contents to stdout. The > $@ syntax is understood by the shell to mean "create the file $@, and connect this command's stdout to it", so now we end up with the contents of $^ combined together into $@.

slowdog
  • 6,076
  • 2
  • 27
  • 30
  • Hmm, stopped it printing the contents, but it is now throwing this error: 'makefile:23: *** missing separator. Stop.' – Olical Dec 10 '10 at 23:05
  • Make sure you have a tab character, not spaces, at the start of the line with the `cat` command. Copypasting makefile fragments in and out of browsers is a pain because the tabs tend to magically turn into spaces... – slowdog Dec 10 '10 at 23:08
  • Okay, now it is working, but only executing the first command, is there a way of executing both? – Olical Dec 10 '10 at 23:10
  • If you put the line `all: spark.js spark-dev.js` near the top of the makefile (before the other two rules), then `make` will build both output files by default. – slowdog Dec 10 '10 at 23:14