Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make target which depends on an environment variable

Tags:

makefile

build

I work on a web app whose Makefile contains the following:

dist/index.html: src/templates/index.html
    @bin/insert-scripts $< --output $@

bin/insert-scripts replaces <--scripts--> in the provided file with one of the following:

  • a number of script tags (for jQuery, Underscore, etc.) when $ENV is "development", or
  • a single script tag (pointing to the concatenated, minified file) when $ENV is "production".

The problem is that if one builds dist/index.html in one mode ("development", say), and then builds it again in the other mode without touching the dependency, make will say there's nothing to be done. What I would like to be able to do is to make $ENV a dependency of dist/index.html:

dist/index.html: src/templates/index.html $ENV
    @bin/insert-scripts $< --output $@

This won't work, of course, so I considered having a file named ENV which contains either "development" or "production". This file would become a dependency:

dist/index.html: src/templates/index.html ENV
    @bin/insert-scripts $< --output $@

Rather than setting an environment variable, one would set the content of the ENV file. This seems a little clunky, but at least accurately represents the dependency tree.

What is the best way to handle this situation?

like image 424
davidchambers Avatar asked Feb 12 '13 19:02

davidchambers


1 Answers

If you absolutely have to enforce rebuilding for changed environments, you can always use a tag file for the build environment:

.PHONY: always-rebuild

environment : always-rebuild
   echo $ENV > [email protected]
   diff --quiet $@ [email protected] || cp [email protected] $@
   rm -f [email protected]

dist/index.html : src/templates/index.html environment

The diffing ensures that environment is always re-built (<= checked), but only touched when the relevant environment variables changed.

like image 141
thiton Avatar answered Sep 23 '22 12:09

thiton