X-Git-Url: https://git.llucax.com/software/makeit.git/blobdiff_plain/a4419536621943012477a48fc1fc5b1c730a2834..194c2379b4bd93f687a0f49cf7551af8724ac699:/Lib.mak diff --git a/Lib.mak b/Lib.mak index 259a659..db9b6a8 100644 --- a/Lib.mak +++ b/Lib.mak @@ -1,4 +1,3 @@ - ifndef Lib.mak.included Lib.mak.included := 1 @@ -7,12 +6,13 @@ Lib.mak.included := 1 # T should be the path to the top-level directory. # C should be the path to the current directory. -# Load top-level directory local configuration -sinclude $T/Config.mak - -# Verbosity flag (empty show nice messages, 1 be verbose) +# Verbosity flag (empty show nice messages, non-empty use make messages) +# When used internal, $V expand to @ is nice messages should be printed, this +# way it's easy to add $V in front of commands that should be silenced when +# displaying the nice messages. +override V := $(if $V,,@) # honour make -s flag -override V := $(if $(findstring s,$(MAKEFLAGS)),1,$V) +override V := $(if $(findstring s,$(MAKEFLAGS)),,$V) # Flavor (variant), should be one of "dbg", "opt" or "cov" F ?= opt @@ -50,11 +50,9 @@ B ?= $G/bin # Libraries directory L ?= $G/lib -# Includes directory -I ?= $G/include -# Generated includes directory -J ?= $G/geninc +# Includes directory +INCLUDE_DIR ?= $G/include # Functions @@ -73,16 +71,16 @@ find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell find $T/$C -name '*.$1')) abbr = $(addprefix $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\ $(subst $T,.,$(patsubst $T/%,%,$1))) -# Execute a command printing a nice message if $V is empty +# Execute a command printing a nice message if $V is @. # 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,,\ +vexec = $(if $V,\ echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \ $(call abbr,$(if $2,$(strip $2),$@))' ; )$1 -# Same as vexec but it silence the echo command (prepending a @). -exec = $(if $V,,@)$(call vexec,$1,$2,$3) +# 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 @@ -92,7 +90,7 @@ exec = $(if $V,,@)$(call vexec,$1,$2,$3) # variables from a rule. define compile $(if $(GCH),\ -@if test -f $O/$*.d; then \ +$Vif test -f $O/$*.d; then \ tmp=`mktemp`; \ h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \ grep -h '^#include <' $(call abbr,$<) $$h | sort -u > "$$tmp"; \ @@ -123,6 +121,13 @@ link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \ $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \ $(foreach obj,$(filter %.o,$^),$(obj))) +# Create a symbolic link to the project under the $(INCLUDE_DIR). The first +# argument is the name of symlink to create. The link is only created if it +# doesn't already exist. +symlink_include_dir = $(shell \ + test -L $(INCLUDE_DIR)/$1 \ + || ln -s $T/$C $(INCLUDE_DIR)/$1 ) + # Overrided flags ################## @@ -131,7 +136,7 @@ link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \ override CPPFLAGS += -Wall # Use the includes directories to search for includes -override CPPFLAGS += -I$I -I$J +override CPPFLAGS += -I$(INCLUDE_DIR) # Be standard compilant override CFLAGS += -std=c99 -pedantic @@ -161,17 +166,11 @@ override LDFLAGS += -pg --coverage endif -# Automatic rebuilding when flags or commands changes -###################################################### - -# Re-compile C files if one of this variables changes -COMPILE.c.FLAGS := $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH) - -# Re-compile C++ files if one of this variables changes -COMPILE.cpp.FLAGS := $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH) +# Automatic dependency handling +################################ -# Re-link binaries and libraries if one of this variables changes -LINK.o.FLAGS := $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH) +# These files are created during compilation. +sinclude $(shell test -d $O && find $O -name '*.d') # Default rules @@ -195,49 +194,68 @@ $L/%.so: $G/link-o-flags clean: $(call exec,$(RM) -r $D,$D) - -# Automatic dependency handling -################################ - -sinclude $(shell test -d $O && find $O -name '*.d') +# These rules use the "Secondary Expansion" GNU Make feature, to allow +# sub-makes to add values to the special variables $(all), after this makefile +# was read. +.SECONDEXPANSION: + +# Phony rule to make all the targets (sub-makefiles can append targets to build +# to the $(all) variable). +.PHONY: all +all: $$(all) # Create build directory structure ################################### -# Create a file with flags used to trigger rebuilding when they change. The -# first argument is the name of the file where to store the flags, the second -# are the flags and the third argument is a text to be displayed if the flags -# have changed. This should be used as a rule action or something where -# a shell script is expected. -gen_rebuild_flags = if test x"$2" != x"`cat $1 2>/dev/null`"; then \ - test -f $1 && echo "$3"; \ - echo "$2" > $1 ; fi -# Create $O, $B, $L, $I and $J directories and replicate the directory +# Create $O, $B, $L and $(INCLUDE_DIR) directories and replicate the directory # structure of the project into $O. Create one symlink "last" to the current -# build directory and another to use as include directory. It update the flags -# files to detect flag and/or compiler changes to force a rebuild. +# 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 $I $J; \ + mkdir -p $O $B $L $(INCLUDE_DIR); \ mkdir -p . $(addprefix $O,$(patsubst $T%,%,\ $(shell find $T -type d -not -path '$D*'))); \ + test -L $D/last || ln -s $F $D/last ) + + +# Automatic rebuilding when flags or commands changes +###################################################### + +# Re-compile C files if one of this variables changes +COMPILE.c.FLAGS += $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH) + +# Re-compile C++ files if one of this variables changes +COMPILE.cpp.FLAGS += $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH) + +# Re-link binaries and libraries if one of this variables changes +LINK.o.FLAGS += $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH) + +# Create a file with flags used to trigger rebuilding when they change. The +# first argument is the name of the file where to store the flags, the second +# are the flags and the third argument is a text to be displayed if the flags +# have changed. This should be used as a rule action or something where +# a shell script is expected. +gen_rebuild_flags = if test x"$2" != x"`cat $1 2>/dev/null`"; then \ + test -f $1 && echo "$3"; \ + echo "$2" > $1 ; fi + +# Create files containing the current flags to trigger a rebuild if they change +setup_flag_files__ := $(shell \ $(call gen_rebuild_flags,$G/compile-c-flags, \ $(COMPILE.c.FLAGS),C compiler or flags;); \ $(call gen_rebuild_flags,$G/compile-cpp-flags, \ $(COMPILE.cpp.FLAGS),C++ compiler or flags;); \ $(call gen_rebuild_flags,$G/link-o-flags, \ - $(LINK.o.FLAGS),linker or link flags;); \ - test -L $I/$P || ln -s $T $I/$P; \ - test -L $D/last || ln -s $F $D/last ) + $(LINK.o.FLAGS),linker or link flags;) ) # Print any generated message (if verbose) -$(if $V,,$(if $(setup_build_dir__), \ - $(info !! Something changed: $(setup_build_dir__) \ +$(if $V,$(if $(setup_flag_files__), \ + $(info !! Something changed: $(setup_flag_files__) \ re-building affected files...))) endif