title: Makefile for release and debug targets
body: <p>I am trying to build a Makefile that can build both release and debug targets by specifying a target rather than a variable (such as <code>make debug=1</code>, not great) I have a condensed simplified example here that emulates what I am trying to achieve:</p>
  
  <pre><code>ifdef debug
      BINARY=my_binary_debug
      MODULE_1_BIN=abc_debug
      MODULE_2_BIN=xyz_debug
      export DBG=1
  else
      BINARY=my_binary
      MODULE_1_BIN=abc
      MODULE_2_BIN=xyz
  endif
  
  FLAG=something
  
  .PHONY: all debug clean
  
  all: bin/$(BINARY).bin
  
  bin/$(BINARY).bin: module_1/$(MODULE_1_BIN).bin module_2/$(MODULE_2_BIN).bin
      cat module_1/$(MODULE_1_BIN).bin $(FLAG) module_2/$(MODULE_2_BIN).bin &gt; $@
  
  module_1/$(MODULE_1_BIN).bin:
      $(MAKE) -C module_1
  
  module_2/$(MODULE_2_BIN).bin:
      $(MAKE) -C module_2
  
  
  clean:
      rm bin/*.bin
      $(MAKE) -C module_1 clean
      $(MAKE) -C module_2 clean
  </code></pre>
  
  <p>This example would have me use <code>make debug=1</code> but I don't really like it and feel this implementation could be better. One way is to use target specific variables , but I am not entirely sure how to use them when the dependencies also need to change their names when a debug target is built. Something like:</p>
  
  <pre><code>debug: export DBG:=1
  debug: bin/$(BINARY)_debug.bin
  debug: module_1/$(MODULE_1_BIN)_debug.bin
  debug: module_2/$(MODULE_2_BIN)_debug.bin
  
  bin/$(BINARY).bin bin/$(BINARY)_debug.bin: module_1/$(MODULE_1_BIN).bin module_2/$(MODULE_2_BIN).bin
          cat module_1/$(MODULE_1_BIN).bin module_2/$(MODULE_2_BIN).bin &gt; $@
  </code></pre>
  
  <p>Here the target will work alright as the <code>bin/$(BINARY).bin</code> target wont need to be built when <code>debug</code> is being built.But I am not sure how to handle the dependencies <code>module_1/$(MODULE_1_BIN).bin</code> and  <code>module_2/$(MODULE_2_BIN).bin</code> in the recipe when building <code>debug</code></p>