From: Olivier Scalbert on
Maxim Yegorushkin wrote:

> You can simplify this in many interesting ways using GNU Make. (I see no
> reason to use any other make).
>
> Something along these lines:
>
> # override with `make BUILD=release`
> # default to debug build
> BUILD := debug
>
> SRC_DIR = :src
>
> # using make's computed variables to select object and bin folders
> # depending on the build type
> OBJ_DIR.debug := objdbg
> OBJ_DIR.release : objrel
> OBJ_DIR := $(OBJ_DIR.$(BUILD))
> BIN_DIR.debug := bindbg
> BIN_DIR.release := binrel
> BIN_DIR := $(BIN_DIR.$(BUILD))
>
> # same for flags
> FLAGS.debug = -g -Wall -pedantic
> FLAGS.release = -g -O3 -Wall -pedantic # can have debuginfo with -O
> FLAGS := $(FLAGS.$(BUILD))
>
> all: $(BIN_DIR)/prog
>
> # how to build any .o from the corresponding.cc
> # the | order-only dependency is to create OBJ_DIR on demand
> $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cc | $(OBJ_DIR)
> g++ -c $(FLAGS) $< -o $@
>
> # the | order-only dependency is to create BIN_DIR on demand
> $(BIN_DIR)/prog: $(OBJ_DIR)/engine.o $(OBJ_DIR)/main.o | $(BIN_DIR)
> g++ $^ -o $@
>
> # how to create folders on demand
> $(OBJ_DIR) $(BIN_DIR):
> mkdir $@
>
> clean :
> rm -rf $(OBJ_DIR) $(BIN_DIR)
>
> .PHONY: clean
>
> This makefile builds or cleans only one type of the build depending on
> the invocation. To clean and build a debug build invoke make as:
>
> make clean
> make
>
> To clean and build a release build invoke as:
>
> make BUILD=release clean
> make BUILD=release
>

Thanks Maxim !
Olivier