asyncplot / .makefiles / common.mk

## -*- mode: Makefile; fill-column: 80 -*-
##
## Common project-independent rules and flags.  Include this from your
## main Makefile *after* your default target (or later).

include $(makefiles_dir)/gmsl

# Common flags
SHELL = /bin/sh
warning_flags ?= -Wall

# Include platform specific stuff
include $(makefiles_dir)/platform.mk

# This passes the -MMD option directly to the preprocessor (-Wp does
# not pass it to the compiler).  This generates dependency files (*.d)
# with only yje source header information (-MD includes system
# headers).  Both gcc and intel compilers support this.
#generate_depends ?= -Wp,-MMD
generate_depends ?= -MMD

# These are the types of files that will generate .o object files.
o_exts ?= .c .cpp .f90 .F90 .cc
h_exts ?= .h .hpp

# Determines if $(build_dir) will have the same folder structure as $(src_dir).
flat_build ?= false

# All paths are relative to $(CURDIR)
_relative := true
_curr_dir := $(realpath $(CURDIR))
ifeq ($(_relative), true)
  _cleanpath = $(foreach _f,$(1),$(patsubst $(_curr_dir)/%,%,$(realpath $(_f))))
else
  _cleanpath = $(foreach _f,$(1),$(realpath $(_f)))
endif

src_dir ?= $(CURDIR)
build_dir ?= $(CURDIR)/build

# Clean up paths and make build directory.
src_dir := $(call _cleanpath,$(src_dir))

_msg := $(shell [ -d $(build_dir) ] || mkdir -p $(build_dir) || echo fail)
ifneq ($(_msg),)
  $(error Could not make build_dir="$(build_dir)")
endif

build_dir := $(call _cleanpath,$(build_dir))
obj_dir ?= $(build_dir)/obj
exe_dir ?= $(build_dir)

##############################################
# Here is the code that finds the sources etc.

# Find source directories if not specified
ifndef src_dirs
  src_dirs := $(shell find $(src_dir) -type d)
  src_dirs += $(main_dirs)
endif

# Find sources: Search in $(src_dirs) for files with extensions in $(o_exts)
source_files ?= $(foreach                       \
  _d,$(src_dirs),$(foreach                      \
    _e,$(o_exts),$(wildcard $(_d)/*$(_e))))
source_files := $(call _cleanpath,$(source_files))

# Find headers: Search in $(src_dirs) for files with extensions in $(h_exts)
header_files ?= $(foreach                       \
  _d,$(src_dirs),$(foreach                      \
    _e,$(h_exts),$(wildcard $(_d)/*$(_e)))))
header_files := $(call _cleanpath,$(header_files))

# Add to $(include_dirs)
include_dirs += $(dir $(header_files))
include_dirs := $(call _cleanpath,$(include_dirs))
include_dirs := $(call uniq,$(include_dirs))

# Find source files for main (executables)
ifdef main_dirs
  # Find executables:
  main_source_files := $(strip $(foreach \
    _d,$(main_dirs),$(foreach \
      _e,$(o_exts),$(wildcard $(_d)/*$(_e)))))
endif

# Function to change paths to be in obj_dir
_to_obj_dir = $(addprefix $(obj_dir)/,$(foreach \
  _f,$(1),$(patsubst $(src_dir)/%,%,$(_f))))

# Create object files for main
_main_object_files := $(addsuffix .o,$(basename $(main_source_files)))
_main_object_files := $(call _to_obj_dir,$(_main_object_files))

targets += $(basename $(_main_object_files))

# Get all object files.
ifndef all_object_files
  all_object_files = $(_main_object_files)
  all_object_files += \
    $(call _to_obj_dir,$(addsuffix .o,$(basename $(source_files))))
endif

# Remove main object files.
object_files ?= $(filter-out $(_main_object_files),$(all_object_files))

# Provide support for a flat_build with VPATH 
# (all output at top level in$(build_dir)
ifeq ($(flat_build),true)
  obj_dir := $(build_dir)
  src_dir :=
  VPATH += $(realpath $(src_dirs)) $(realpath $(obj_dir))
  object_files := $(addprefix $(build_dir)/,$(notdir $(object_files)))
  all_object_files := $(addprefix $(build_dir)/,$(notdir $(all_object_files)))
  targets := $(addprefix $(build_dir)/,$(notdir $(targets)))
endif 

# Load generated dependency files
depend_files ?= $(all_object_files:.o=.d)

-include $(depend_files)

# Generic compiler options
DEBUG ?= false
MAKEOVERRIDES := $(filter-out DEBUG=%,$(MAKEOVERRIDES))
ifeq ($(DEBUG), true)
  debug_flags += -g
else
  optimization_flags += -O3
  ifeq ($(use_boost), true)
     optimization_flags += -DBOOST_DISABLE_ASSERTS
  endif 
  # -DNDEBUG Kenny uses!
endif

# Add flags that the user has defined.
CPPFLAGS += $(foreach _v,$(MAKEOVERRIDES),-D$(_v))

# Compiler flags.
CFLAGS += $(warning_flags) $(cwarning_flags)
CFLAGS += $(debug_flags) $(cdebug_flags)
CFLAGS += $(optimize_flags) $(coptimize_flags)

CXXFLAGS += $(warning_flags) $(cxxwarning_flags)
CXXFLAGS += $(debug_flags) $(cxxdebug_flags)
CXXFLAGS += $(optimize_flags) $(cxxoptimize_flags)

FFLAGS += $(warning_flags) $(fwarning_flags)
FFLAGS += $(debug_flags) $(fdebug_flags)
FFLAGS += $(optimize_flags) $(foptimize_flags)

CPPFLAGS += $(generate_depends)
CPPFLAGS += $(addprefix -I,$(include_dirs))
LDLIBS += $(addprefix -L,$(lib_dirs))
LDLIBS += $(addprefix -l,$(libraries))

# Use c++ for linking
LINK.o = $(LINK.cc)

####### Load all Pattern Rules

all : $(targets)

include $(makefiles_dir)/help.mk
include $(makefiles_dir)/rules.mk

####### Cleaning, Syntax checking, etc.
.PHONY : clean clean.build
clean.build:
	rm -rf $(build_dir) $(tmp_files)
clean: clean.build

.PHONY: check-syntax TAGS
# This is used for syntax checking in emacs.  Must define $(chk_sources)
chk_sources ?= $(source_files)
check-syntax:
	$(CXX) $(CPPFLAGS) -Wall -Wextra -pedantic -fsyntax-only $(chk_sources)

# Emacs etags file.
TAGS: 
	cd $(src_dir); find . \( -name \*.c -o \
                  -name \*.h -o \
                  -name \*.cpp -o \
                  -name \*.hpp \) | xargs etags

.PHONY: debug.build debug_makefile
debug.build:
	@echo generate_depends="$(generate_depends)"
	@echo o_exts="$(o_exts)"
	@echo h_exts="$(h_exts)"
	@echo flat_build="$(flat_build)"
	@echo VPATH="$(VPATH)"
	@echo src_dir="$(src_dir)"
	@echo build_dir="$(build_dir)"
	@echo obj_dir="$(obj_dir)"
	@echo src_dirs="$(src_dirs)"
	@echo source_files="$(source_files)"
	@echo header_files="$(header_files)"
	@echo include_dirs="$(include_dirs)"
	@echo main_dirs="$(main_dirs)"
	@echo main_source_files="$(main_source_files)"
	@echo _main_object_files="$(_main_object_files)"
	@echo targets="$(targets)"
	@echo all_object_files="$(all_object_files)"
	@echo object_files="$(object_files)"
	@echo depend_files="$(depend_files)"
	@echo debug_flags="$(debug_flags)"
	@echo optimization_flags="$(optimization_flags)"
	@echo warning_flags="$(warning_flags)"
	@echo MAKEOVERRIDES="$(MAKEOVERRIDES)"

debug_makefile: debug.build
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.