Search code examples
makefilegnu-make

Makefile to compress all files in a directory


I've created a makefile with the following target:

assets/css/%.css.br: assets/css/%.css
     brotli $< $@

This works really well for compressing every css file in a directory and creating a .css.br file.

However, how can I call this target to compress every css file in that directory, I can only call these targets one-by-one.


Solution

  • Glob the names of the CSS files, derive the names of the corresponding compressed files using suffix substitution, and create a target that depends on all the compressed files:

    CSS_SRC        := $(wildcard assets/css/*.css)
    CSS_COMPRESSED := $(CSS_SRC:.css=.css.br)
    
    %.css.br: %.css
        brotli $< $@
    
    compress: $(CSS_COMPRESSED)
    

    Then

    $ make compress
    

    will compress each CSS file using your pattern rule.