Question

I've got a rule that checks if a certain environment variable has been set:

check-env:
  ifndef NODE_ENV
    $(error NODE_ENV is undefined)
  endif

I then have stuff that depend on it (which should fail if NODE_ENV isn't set):

sql/schema.js: sql/schema.sql check-env
  ...

My issue is that check-env always causes a rebuild, what should I actually be doing to achieve what I want in a reasonably modular way?

Was it helpful?

Solution

Why are you doing this in the recipe of a target? Why not just put it out in the main part of your makefile?

ifndef NODE_ENV
  $(error NODE_ENV is undefined)
endif

If you really want to have this done through rules, your best bet (assuming you're using a "new-enough" version of GNU make) is to use order-only prerequisites like this:

check-env:
        $(if $(NODE_ENV),,$(error NODE_ENV is undefined))

sql/schema.js: sql/schema.sql | check-env
        ...

(note the pipe symbol |). See the manual for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top