Alan
Alan

Reputation: 46883

Setting a variable in make

This is my makefile

file1:
    uglifyjs myfile1.js -c | gzip -c -9 > myfile1.min.js

file2:
    uglifyjs myfile2.js -c | gzip -c -9 > myfile2.min.js

How can I change my makefile to remove duplicate code:

file1:
    FILE=myfile1.js
    #How to call build target?

file2:
    FILE=myfile2.js

build:
    uglifyjs $(FILE).js -c | gzip -c -9 > $(FILE).min.js

I know I can use make build but is there another way to do this without invoking make recursively?

Upvotes: 1

Views: 96

Answers (2)

Chris Dodd
Chris Dodd

Reputation: 126478

Use a pattern rule to run the command, and then make your targets depend on the files you want:

file1: myfile1.min.js
file2: myfile2.min.js

%.min.js: %.js
        uglifyjs $< -c | gzip -c -9 >$@

The pattern rule tells make how to build a .min.js file from a .js file, and the other rules tell it to build specific files.

Upvotes: 1

MadScientist
MadScientist

Reputation: 101051

Use automatic variables:

file1 file2:
        uglifyjs [email protected] -c | gzip -c -9 > [email protected]

I don't know why you're using targets like file1 when the file you're actually building is myfile1.min.js. That's not a good makefile.

But, that's not the question you asked.

Upvotes: 1

Related Questions