# honour make -s flag
override V := $(if $(findstring s,$(MAKEFLAGS)),,$V)
-# Flavor (variant), should be one of "dbg", "opt" or "cov"
-F ?= opt
+# If $V is non-empty, colored output is used if $(COLOR) is non-empty too
+COLOR ?= 1
+
+# ANSI color used for the command if $(COLOR) is non-empty
+# The color is composed with 2 numbers separated by ;
+# The first is the style. 00 is normal, 01 is bold, 04 is underline, 05 blinks,
+# 07 is reversed mode
+# The second is the color: 30 dark gray/black, 31 red, 32 green, 33 yellow, 34
+# blue, 35 magenta, 36 cyan and 37 white.
+# If empty, no special color is used.
+COLOR_CMD ?= 00;33
+
+# ANSI color used for the argument if $(COLOR) is non-empty
+# See COLOR_CMD comment for details.
+COLOR_ARG ?=
+
+# ANSI color used for the warnings if $(COLOR) is non-empty
+# See COLOR_CMD comment for details.
+COLOR_WARN ?= 00;36
+
+# ANSI color used for commands output if $(COLOR) is non-empty
+# See COLOR_CMD comment for details.
+COLOR_OUT ?= 00;31
+
+# Flavor (variant), can be defined by the user in Config.mak
+F ?= default
# Use C++ linker by default
LINKER := $(CXX)
$(if $V,--log-file=$<.valgrind.log) \
$(if $(VALGRIND_SUPP),--suppressions=$(VALGRIND_SUPP))
+# Command to generate Sphinx based documentation
+SPHINX ?= sphinx-build
+
+# Format to build using Sphinx (html, pickle, htmlhelp, latex, changes or
+# linkcheck; see sphinx-build docs for details)
+SPHINX_FORMAT ?= html
+
+# Paper size for Sphinx LaTeX output (a4, letter, etc.)
+SPHINX_PAPERSIZE ?= a4
+
+# Name of the build directory (to use when excluding some paths)
+BUILD_DIR_NAME ?= build
+
+# Directories to exclude from the build directory tree replication
+BUILD_DIR_EXCLUDE ?= $(BUILD_DIR_NAME) .git .hg .bzr _darcs .svn CVS
+
# Directories
##############
R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
# Base directory where to put variants (Variants Directory)
-VD ?= $T/build
+VD ?= $T/$(BUILD_DIR_NAME)
# Generated files top directory
G ?= $(VD)/$F
# The first argument is mandatory and it's the command to execute. The second
# and third arguments are optional and are the target name and command name to
# pretty print.
-vexec = $(if $V,\
- echo ' $(call abbr,$(if $3,$(strip $3),$(firstword $1))) \
- $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
+vexec_pc = $(if $1,\033[$1m%s\033[00m,%s)
+vexec_p = $(if $(COLOR), \
+ ' $(call vexec_pc,$(COLOR_CMD)) $(call vexec_pc,$(COLOR_ARG))\n$(if \
+ $(COLOR_OUT),\033[$(COLOR_OUT)m)', \
+ ' %s %s\n')
+vexec = $(if $V,printf $(vexec_p) \
+ '$(call abbr,$(if $3,$(strip $3),$(firstword $1)))' \
+ '$(call abbr,$(if $2,$(strip $2),$@))' ; )$1 \
+ $(if $V,$(if $(COLOR),$(if $(COLOR_OUT), \
+ ; r=$$? ; printf '\033[00m' ; exit $$r)))
# Same as vexec but it silence the echo command (prepending a @ if $V).
exec = $V$(call vexec,$1,$2,$3)
-# Compile a source file to an object, generating pre-compiled headers and
-# dependencies. The pre-compiled headers are generated only if the system
-# includes change. This function is designed to be used as a command in a rule.
-# It takes one argument only, the type of file to compile (typically "c" or
-# "cpp"). What to compile and the output files are built using the automatic
-# variables from a rule.
+# Compile a source file to an object, generating pre-compiled headers (if
+# $(GCH) is non-empty) and dependencies. The pre-compiled headers are generated
+# only if the system includes change. This function is designed to be used as
+# a command in a rule. The first argument is the type of file to compile
+# (typically "c" or "cpp"). What to compile and the output files are built
+# using the automatic variables from a rule. The second argument is the base
+# output directory (typically $O). You can add non-propagated object-specific
+# flags defining a variable with the name of the target followed with
+# ".EXTRA_FLAGS".
+#
+# XXX: The pre-compiled headers generation is not very useful if you include
+# local files using #include <...>, because the system headers detection
+# is a little simplistic now, it just parse the source file and all its
+# dependencies searching for lines starting with "#include <" and
+# extracting the included files from them.
define compile
$(if $(GCH),\
-$Vif test -f $O/$*.d; then \
+$Vif test -f $2/$*.d; then \
tmp=`mktemp`; \
- h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
+ h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $2/$*.d`; \
grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
- if diff -q -w "$O/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
+ if diff -q -w "$2/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
then \
rm "$$tmp"; \
else \
- mv "$$tmp" "$O/$*.$1.h"; \
- $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
- $O/$*.$1.h.gch); \
+ mv "$$tmp" "$2/$*.$1.h"; \
+ $(call vexec,$(COMPILE.$1) $($@.EXTRA_FLAGS) \
+ -o "$2/$*.$1.h.gch" "$2/$*.$1.h",$2/$*.$1.h.gch); \
fi \
else \
- touch "$O/$*.$1.h"; \
+ touch "$2/$*.$1.h"; \
fi \
)
-$(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
+$(call exec,$(COMPILE.$1) $($@.EXTRA_FLAGS) -o $@ -MMD -MP \
+ $(if $(GCH),-include $2/$*.$1.h) $<)
endef
# Link object files to build an executable. The objects files are taken from
# taken from the rule automatic variables. If an argument is provided, it's
# included in the link command line. The variable LINKER is used to link the
# executable; for example, if you want to link a C++ executable, you should use
-# LINKER := $(CXX).
-link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
+# LINKER := $(CXX). You can add non-propagated target-specific flags defining
+# a variable with the name of the target followed with ".EXTRA_FLAGS". You can
+# specify a non-propagated object-specific linker defining a variable with the
+# name of the target followed with ".LINKER".
+link = $(call exec,$(if $($@.LINKER),$($@.LINKER),$(LINKER)) \
+ $(LDFLAGS) $(TARGET_ARCH) $($@.EXTRA_FLAGS) -o $@ $1 \
$(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
$(foreach obj,$(filter %.o,$^),$(obj)))
# have changed (optional). This should be used as a rule action or something
# where a shell script is expected.
gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
- $(if $3,test -f $1 && echo "$3";) \
+ $(if $3,test -f $1 && echo "$(if $(COLOR),$(if $(COLOR_WARN),\
+ \033[$(COLOR_WARN)m$3\033[00m,$3),$3);";) \
echo "$2" > $1 ; fi)
# Include sub-directory's Build.mak. The only argument is a list of
endef
include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
-# Run a command through valgrind if $(VALGRIND) is non-empty. The first and
-# only argument is the command to. If $(VALGRIND) is empty, the command is
+# Run a command through valgrind if $(VALGRIND) is non-empty. The first
+# argument is the command to run. If $(VALGRIND) is empty, the command is
# executed as passed to this function. If valgrind is used, the
# $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
-# definition for extra options that can be passed as make variables.
+# definition for extra options that can be passed as make variables. The
+# second argument is the name of the command to print when $V is non-empty (if
+# omitted, the first word of the first argument is used).
valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
$(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
- $(firstword $1))
+ $(if $2,$2,$(firstword $1)))
# Overridden flags
##################
-# Warn about everything
-override CPPFLAGS += -Wall
-
# Use the includes directories to search for includes
override CPPFLAGS += -I$(INCLUDE_DIR)
# Let the program know where it will be installed
override CPPFLAGS += -DPREFIX=$(prefix)
-# Be standard compliant
-override CFLAGS += -std=c99 -pedantic
-override CXXFLAGS += -std=c++98 -pedantic
-
# Use the generated library directory to for libraries
-override LDFLAGS += -L$L -Wall
+override LDFLAGS += -L$L
# Make sure the generated libraries can be found
export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
-# Variant flags
-################
-
-ifeq ($F,dbg)
-override CPPFLAGS += -ggdb -DDEBUG
-endif
-
-ifeq ($F,opt)
-override CPPFLAGS += -O2 -DNDEBUG
-endif
-
-ifeq ($F,cov)
-override CPPFLAGS += -ggdb -pg --coverage
-override LDFLAGS += -pg --coverage
-endif
-
-
# Automatic dependency handling
################################
# Compile C objects
$O/%.o: $T/%.c $G/compile-c-flags
- $(call compile,c)
+ $(call compile,c,$O)
# Compile C++ objects
$O/%.o: $T/%.cpp $G/compile-cpp-flags
- $(call compile,cpp)
+ $(call compile,cpp,$O)
# Link binary programs
$B/%: $G/link-o-flags
$(call replace,$(PC_VARS),$*-PC-)
# Run doxygen to build the documentation. It expects the first prerequisite to
-# be the Doxyfile to use and the next prerequisites the input files. This rule
-# is a little restrictive, but you can always make your own if it doesn't fit
-# your needs ;)
+# be the Doxyfile to use and the next prerequisites the input files. You
+# can override Doxyfile configuration variables by defining a DOXYGEN_VARS
+# Make variable for this rule. For example, defining:
+# PROJECT_NAME := myproj
+# DOXYGEN_VARS := PROJECT_NAME
+# You can override Doxygen's PROJECT_NAME configuration option. Optionally, you
+# can define DOXYGEN_VARS_PREFIX too, to avoid polluting your Makefile with
+# Doxygen variables. For example:
+# DOXY.PROJECT_NAME := myproj
+# DOXYGEN_VARS_PREFIX := DOXY.
+# DOXYGEN_VARS := PROJECT_NAME
+# This rule might be still a little restrictive, but you can always make your
+# own if it doesn't fit your needs ;)
$D/%/doxygen-stamp:
$V mkdir -p $(@D)
$(call exec,(cat $<; \
echo "FULL_PATH_NAMES=YES"; \
- echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
- $(wordlist 2,$(words $^),$^))"; \
+ $(if $(filter INPUT,$(DOXYGEN_VARS)),,\
+ echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
+ $(wordlist 2,$(words $^),$^))";) \
echo "OUTPUT_DIRECTORY=$(@D)"; \
- echo "INCLUDE_PATH=$(INCLUDE_DIR)"; \
echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
- echo "QUIET=$(if $V,YES,NO)") | doxygen -,$(@D),doxygen)
+ echo "QUIET=$(if $V,YES,NO)"; \
+ $(foreach v,$(DOXYGEN_VARS),\
+ echo '$v=$($(DOXYGEN_VARS_PREFIX)$v)';) \
+ ) | doxygen -,$(@D),doxygen)
+ $V touch $@
+
+# Run Sphinx to build the documentation. It expects the variable SPHINX_DIR
+# to be set to the directory where the Sphinx's conf.py and reST source files
+# are. This rule is a little restrictive, but you can always make your own if
+# it doesn't fit your needs ;)
+$D/%/sphinx-stamp: $G/sphinx-flags
+ $V mkdir -p $(@D)/$(SPHINX_FORMAT)
+ $(call exec,$(SPHINX) $(if $V,-q) -b $(SPHINX_FORMAT) \
+ -d $(@D)/doctrees -D latex_paper_size=$(SPHINX_PAPERSIZE) \
+ $(SPHINX_DIR) $(@D)/$(SPHINX_FORMAT),$(@D),$(SPHINX))
$V touch $@
# Install binary programs
# Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
# directory structure of the project into $O. Create one symbolic link "last"
# to the current build directory.
-#
-# NOTE: the second mkdir can yield no arguments if the project don't have any
-# subdirectories, that's why the current directory "." is included, so it
-# won't show an error message in case of no subdirectories.
setup_build_dir__ := $(shell \
- mkdir -p $O $B $L $D $(INCLUDE_DIR); \
- mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
- $(shell find $T -type d -not -path '$(VD)*'))); \
- test -L $(VD)/last || ln -s $F $(VD)/last )
+ mkdir -p $O $B $L $D $(INCLUDE_DIR) $(addprefix $O,$(patsubst $T%,%,\
+ $(shell find $T -type d $(foreach d,$(BUILD_DIR_EXCLUDE), \
+ -not -path '$T/$d' -not -path '$T/$d/*' \
+ -not -path '$T/*/$d' -not -path '$T/*/$d/*')))); \
+ rm -f $(VD)/last && ln -s $F $(VD)/last )
# Automatic rebuilding when flags or commands changes
# Re-link binaries and libraries if one of this variables changes
LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
+# Re-build sphinx documentation if one of these variables changes
+SPHINX.FLAGS := $(call varcat,SPHINX SPHINX_FORMAT SPHINX_PAPERSIZE)
+
# Create files containing the current flags to trigger a rebuild if they change
setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
- $(COMPILE.c.FLAGS),C compiler or flags; )
+ $(COMPILE.c.FLAGS),C compiler)
+setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
+ $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler)
setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
- $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
+ $G/link-o-flags, $(LINK.o.FLAGS),linker)
setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
- $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
+ $G/sphinx-flags, $(SPHINX.FLAGS),sphinx)
# Print any generated message (if verbose)
$(if $V,$(if $(setup_flag_files__), \
- $(info !! Something changed: $(setup_flag_files__)re-building \
+ $(info !! Flags or commands changed:$(setup_flag_files__) re-building \
affected files...)))
endif