make/common/MakeBase.gmk
changeset 29662 78c47f0002c3
parent 29312 f3f859137ce6
child 29861 cb5d893c3282
equal deleted inserted replaced
29565:34ead0aa3d9d 29662:78c47f0002c3
    29 #
    29 #
    30 ################################################################
    30 ################################################################
    31 
    31 
    32 ifndef _MAKEBASE_GMK
    32 ifndef _MAKEBASE_GMK
    33 _MAKEBASE_GMK := 1
    33 _MAKEBASE_GMK := 1
       
    34 
       
    35 ifeq ($(wildcard $(SPEC)),)
       
    36   $(error MakeBase.gmk needs SPEC set to a proper spec.gmk)
       
    37 endif
       
    38 
       
    39 
       
    40 ##############################
       
    41 # Functions
       
    42 ##############################
       
    43 
       
    44 
       
    45 ### Functions for timers
       
    46 
       
    47 # Store the build times in this directory.
       
    48 BUILDTIMESDIR=$(OUTPUT_ROOT)/make-support/build-times
       
    49 
       
    50 # Record starting time for build of a sub repository.
       
    51 define RecordStartTime
       
    52 	$(MKDIR) -p $(BUILDTIMESDIR)
       
    53 	$(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)
       
    54 	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable
       
    55 endef
       
    56 
       
    57 # Record ending time and calculate the difference and store it in a
       
    58 # easy to read format. Handles builds that cross midnight. Expects
       
    59 # that a build will never take 24 hours or more.
       
    60 define RecordEndTime
       
    61 	$(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)
       
    62 	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable
       
    63 	$(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \
       
    64 	    $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
       
    65 	    M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
       
    66 	    > $(BUILDTIMESDIR)/build_time_diff_$(strip $1)
       
    67 endef
       
    68 
       
    69 # Hook to be called when starting to execute a top-level target
       
    70 define TargetEnter
       
    71 	$(PRINTF) "## Starting $(patsubst %-only,%,$@)\n"
       
    72 	$(call RecordStartTime,$(patsubst %-only,%,$@))
       
    73 endef
       
    74 
       
    75 # Hook to be called when finish executing a top-level target
       
    76 define TargetExit
       
    77 	$(call RecordEndTime,$(patsubst %-only,%,$@))
       
    78 	$(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \
       
    79 	    "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`"
       
    80 endef
    34 
    81 
    35 ################################################################################
    82 ################################################################################
    36 # This macro translates $ into \$ to protect the $ from expansion in the shell.
    83 # This macro translates $ into \$ to protect the $ from expansion in the shell.
    37 # To make this macro resilient against already escaped strings, first remove
    84 # To make this macro resilient against already escaped strings, first remove
    38 # any present escapes before escaping so that no double escapes are added.
    85 # any present escapes before escaping so that no double escapes are added.
   339 	$(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME); \
   386 	$(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME); \
   340 	$(ECHO) $1/$(HGTIP_FILENAME)
   387 	$(ECHO) $1/$(HGTIP_FILENAME)
   341 endef
   388 endef
   342 
   389 
   343 define SetupLogging
   390 define SetupLogging
   344   ifeq ($$(LOG_LEVEL),trace)
   391   ifeq ($$(LOG_LEVEL), trace)
   345     # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
   392     # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
   346     # For each target executed, will print
   393     # For each target executed, will print
   347     # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
   394     # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
   348     # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
   395     # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
   349     # (and causing a crash on Cygwin).
   396     # (and causing a crash on Cygwin).
   350     # Default shell seems to always be /bin/sh. Must override with bash to get this to work on Solaris.
   397     # Default shell seems to always be /bin/sh. Must override with bash to get this to work on Solaris.
   351     # Only use time if it's GNU time which supports format and output file.
   398     # Only use time if it's GNU time which supports format and output file.
   352     WRAPPER_SHELL:=$$(BASH) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(findstring yes,$$(IS_GNU_TIME)),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(SHELL)
   399     WRAPPER_SHELL := $$(BASH) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(findstring yes,$$(IS_GNU_TIME)),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(SHELL)
   353     SHELL=$$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL)
   400     SHELL := $$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL)
   354   endif
   401   endif
   355   # Never remove warning messages; this is just for completeness
   402   # Never remove warning messages; this is just for completeness
   356   LOG_WARN=
   403   LOG_WARN :=
   357   ifneq ($$(findstring $$(LOG_LEVEL),info debug trace),)
   404   ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),)
   358     LOG_INFO=
   405     LOG_INFO :=
   359   else
   406   else
   360     LOG_INFO=> /dev/null
   407     LOG_INFO := > /dev/null
   361   endif
   408   endif
   362   ifneq ($$(findstring $$(LOG_LEVEL),debug trace),)
   409   ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
   363     LOG_DEBUG=
   410     LOG_DEBUG :=
   364   else
   411   else
   365     LOG_DEBUG=> /dev/null
   412     LOG_DEBUG := > /dev/null
   366   endif
   413   endif
   367   ifneq ($$(findstring $$(LOG_LEVEL),trace),)
   414   ifneq ($$(findstring $$(LOG_LEVEL), trace),)
   368     LOG_TRACE=
   415     LOG_TRACE :=
   369   else
   416   else
   370     LOG_TRACE=> /dev/null
   417     LOG_TRACE := > /dev/null
   371   endif
   418   endif
   372 endef
   419 endef
   373 
   420 
   374 # Make sure logging is setup for everyone that includes MakeBase.gmk.
   421 # Make sure logging is setup for everyone that includes MakeBase.gmk.
   375 $(eval $(call SetupLogging))
   422 $(eval $(call SetupLogging))