common/makefiles/MakeBase.gmk
changeset 14111 2a82ecb35fc7
parent 13697 5262b00bc10c
child 14279 1c596d0f411c
equal deleted inserted replaced
14104:8d9d430b4244 14111:2a82ecb35fc7
    31 ################################################################
    31 ################################################################
    32 
    32 
    33 ifndef _MAKEBASE_GMK
    33 ifndef _MAKEBASE_GMK
    34 _MAKEBASE_GMK := 1
    34 _MAKEBASE_GMK := 1
    35 
    35 
    36 ifeq (,$(findstring 3.81,$(MAKE_VERSION)))
       
    37     ifeq (,$(findstring 3.82,$(MAKE_VERSION)))
       
    38         $(error This version of GNU Make is too low ($(MAKE_VERSION)). Please upgrade to 3.81 or newer.)
       
    39     endif
       
    40 endif
       
    41 
       
    42 ifneq (,$(CYGPATH))
       
    43     # Cygwin styff, if needed but most is going to be
       
    44     # done in configure!
       
    45     ifeq (yes,$(CHECK_FOR_VCINSTALLDIR))
       
    46         ifeq (,$(VCINSTALLDIR)) 
       
    47             $(info Could not find compiler paths!)
       
    48             $(info You probably configured the build from within a)
       
    49             $(info VS command prompt, but you are not using such a)
       
    50             $(info prompt right now.)
       
    51             $(info If you instead run configure from a plain cygwin shell, it)
       
    52             $(info will attempt to setup VS for you! Please try that.)
       
    53             $(error Giving up for now.)
       
    54         endif
       
    55     endif
       
    56 endif
       
    57 
       
    58 # The build times report is turned off by setting REPORT_BUILD_TIMES to nothing.
       
    59 # This is necessary for the target clean which will erase the
       
    60 # directories where the buildtimes are stored.
       
    61 REPORT_BUILD_TIMES=1
       
    62 # Store the build times in this directory.
       
    63 BUILDTIMESDIR=$(OUTPUT_ROOT)/tmp/buildtimes
       
    64 
       
    65 # Record starting time for build of a sub repository.
       
    66 define RecordStartTime
       
    67 $(MKDIR) -p $(BUILDTIMESDIR)
       
    68 $(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_start_$1
       
    69 $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$1_human_readable
       
    70 endef
       
    71 
       
    72 # Indicate that we started to build a sub repository and record starting time. 
       
    73 define MakeStart
       
    74 $(call RecordStartTime,$1)
       
    75 $(BUILD_LOG_WRAPPER) $(PRINTF) "\n\n%s\n%s\n##### %-60.60s #####\n%s\n\n" \
       
    76 "########################################################################" \
       
    77 "########################################################################" \
       
    78 "Entering $1 for target(s) $2" \
       
    79 "########################################################################"
       
    80 endef
       
    81 
       
    82 # Record ending time and calculate the difference and store it in a
       
    83 # easy to read format. Handles builds that cross midnight. Expects
       
    84 # that a build will never take 24 hours or more. 
       
    85 define RecordEndTime
       
    86 $(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_end_$1
       
    87 $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$1_human_readable
       
    88 $(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$1` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$1` $1 | \
       
    89   $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
       
    90              M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
       
    91   > $(BUILDTIMESDIR)/build_time_diff_$1
       
    92 endef
       
    93 
       
    94 # Check if the current target is the final target, as specified by
       
    95 # the user on the command line. If so, call PrintEndMessage.
       
    96 define CheckIfFinished
       
    97 $(if $(filter $@,$(MAKECMDGOALS)),$(call PrintEndMessage))
       
    98 # If no taget is given, "all" is default. Check for that, too.
       
    99 # At most one of the tests can be true.
       
   100 $(if $(MAKECMDGOALS),,$(if $(filter $@,all),$(call PrintEndMessage)))
       
   101 endef
       
   102 
       
   103 # Indicate that we are done.
       
   104 # Record ending time and print out the total time it took to build.
       
   105 define MakeFinish 
       
   106 $(if $(REPORT_BUILD_TIMES),$(call RecordEndTime,$1),) 
       
   107 $(BUILD_LOG_WRAPPER) $(PRINTF) "%s\n##### %-60.60s #####\n%s\n##### %-60.60s #####\n%s\n\n" \
       
   108 "########################################################################" \
       
   109 "Leaving $1 for target(s) $2" \
       
   110 "########################################################################" \
       
   111 $(if $(REPORT_BUILD_TIMES),"Build time `$(CAT) $(BUILDTIMESDIR)/build_time_diff_$1` for target(s) $2","") \
       
   112 "########################################################################"
       
   113 $(call CheckIfFinished)
       
   114 endef
       
   115 
       
   116 # Find all build_time_* files and print their contents in a list sorted
       
   117 # on the name of the sub repository.
       
   118 define ReportBuildTimes
       
   119 $(BUILD_LOG_WRAPPER) $(PRINTF) -- "-- Build times ----------\nTarget %s\nStart %s\nEnd   %s\n%s\n%s\n-------------------------\n" \
       
   120 "$1" \
       
   121 "`$(CAT) $(BUILDTIMESDIR)/build_time_start_TOTAL_human_readable`" \
       
   122 "`$(CAT) $(BUILDTIMESDIR)/build_time_end_TOTAL_human_readable`" \
       
   123 "`$(LS) $(BUILDTIMESDIR)/build_time_diff_* | $(GREP) -v _TOTAL | $(XARGS) $(CAT) | $(SORT) -k 2`" \
       
   124 "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_TOTAL`"
       
   125 endef
       
   126 
       
   127 define ResetTimers
       
   128 $$(shell $(MKDIR) -p $(BUILDTIMESDIR) &&  $(RM) $(BUILDTIMESDIR)/build_time_*)
       
   129 endef
       
   130 
       
   131 define StartTimer
       
   132 	$(call RecordStartTime,TOTAL)
       
   133 endef
       
   134 
       
   135 define StopTimer
       
   136 	$(if $(REPORT_BUILD_TIMES),$(call RecordEndTime,TOTAL) && $(call ReportBuildTimes,$1),)
       
   137 endef
       
   138 
       
   139 # Hook to be called as the very first thing when running a normal build
       
   140 define AtRootMakeStart
       
   141 	$(if $(findstring --jobserver,$(MAKEFLAGS)), $(error make -j is not supported, use make JOBS=n))
       
   142 	$(call PrintStartMessage)
       
   143 	$(call StartTimer)
       
   144 endef
       
   145 
       
   146 # Hook to be called as the very last thing for targets that are "top level" targets
       
   147 define AtRootMakeEnd
       
   148 	$(call StopTimer)
       
   149 	$(call CheckIfFinished)
       
   150 endef
       
   151 
       
   152 # If the variable that you want to send to stdout for piping into a file or otherwise,
    36 # If the variable that you want to send to stdout for piping into a file or otherwise,
   153 # is potentially long, for example the a list of file paths, eg a list of all package directories.
    37 # is potentially long, for example the a list of file paths, eg a list of all package directories.
   154 # Then you need to use ListPathsSafely, which optimistically splits the output into several shell
    38 # Then you need to use ListPathsSafely, which optimistically splits the output into several shell
   155 # calls as well as use compression on recurrent file paths segments, to get around the potential
    39 # calls as well as use compression on recurrent file paths segments, to get around the potential
   156 # command line length problem that exists in cygwin and other shells.
    40 # command line length problem that exists in cygwin and other shells.
   157 compress_pre:=$(strip $(shell cat $(SRC_ROOT)/common/makefiles/compress.pre))
    41 compress_pre:=$(strip $(shell $(CAT) $(SRC_ROOT)/common/makefiles/support/ListPathsSafely-pre-compress.incl))
   158 compress_post:=$(strip $(shell cat $(SRC_ROOT)/common/makefiles/compress.post))
    42 compress_post:=$(strip $(shell $(CAT) $(SRC_ROOT)/common/makefiles/support/ListPathsSafely-post-compress.incl))
   159 compress_paths=$(compress_pre)\
    43 compress_paths=$(compress_pre)\
   160 $(subst $(SRC_ROOT),X97,\
    44 $(subst $(SRC_ROOT),X97,\
   161 $(subst $(OUTPUT_ROOT),X98,\
    45 $(subst $(OUTPUT_ROOT),X98,\
   162 $(subst X,X00,\
    46 $(subst X,X00,\
   163 $(subst $(SPACE),\n,$(strip $1)))))\
    47 $(subst $(SPACE),\n,$(strip $1)))))\
   164 $(compress_post)
    48 $(compress_post)
   165 
    49 
   166 decompress_paths=sed -f $(SRC_ROOT)/common/makefiles/uncompress.sed -e 's|X99|\\n|g' \
    50 decompress_paths=$(SED) -f $(SRC_ROOT)/common/makefiles/support/ListPathsSafely-uncompress.sed -e 's|X99|\\n|g' \
   167 		     -e 's|X98|$(OUTPUT_ROOT)|g' -e 's|X97|$(SRC_ROOT)|g' \
    51 		     -e 's|X98|$(OUTPUT_ROOT)|g' -e 's|X97|$(SRC_ROOT)|g' \
   168 		     -e 's|X00|X|g' | tr '\n' '$2'
    52 		     -e 's|X00|X|g' | tr '\n' '$2'
   169 
    53 
   170 define ListPathsSafely_If
    54 define ListPathsSafely_If
   171     $(if $(word $3,$($1)),$(eval $1_LPS$3:=$(call compress_paths,$(wordlist $3,$4,$($1)))))
    55     $(if $(word $3,$($1)),$(eval $1_LPS$3:=$(call compress_paths,$(wordlist $3,$4,$($1)))))
   382 $(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME);\
   266 $(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME);\
   383 $(ECHO) $1/$(HGTIP_FILENAME)
   267 $(ECHO) $1/$(HGTIP_FILENAME)
   384 endef
   268 endef
   385 
   269 
   386 define SetupLogging
   270 define SetupLogging
   387     ifneq ($(findstring $(LOG),debug trace),)
   271     ifeq ($$(LOG), trace)
   388         # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
   272         # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
       
   273         # For each target executed, will print
       
   274         # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
       
   275         # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much (and causing a crash on Cygwin).
   389         OLD_SHELL:=$$(SHELL)
   276         OLD_SHELL:=$$(SHELL)
   390         SHELL = $$(warning Building $$@$$(if $$<, (from $$<))$(if $$?, ($$? newer)))$$(OLD_SHELL) -x
   277         WRAPPER_SHELL:=$$(OLD_SHELL) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(TIME),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(OLD_SHELL)
       
   278         SHELL=$$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL)
       
   279     endif
       
   280     # Never remove warning messages; this is just for completeness
       
   281     LOG_WARN= 
       
   282     ifneq ($$(findstring $$(LOG),info debug trace),)
       
   283         LOG_INFO=
       
   284     else
       
   285         LOG_INFO=> /dev/null
       
   286     endif
       
   287     ifneq ($$(findstring $$(LOG),debug trace),)
       
   288         LOG_DEBUG=
       
   289     else
       
   290         LOG_DEBUG=> /dev/null
       
   291     endif
       
   292     ifneq ($$(findstring $$(LOG),trace),)
       
   293         LOG_TRACE=
       
   294     else
       
   295         LOG_TRACE=> /dev/null
   391     endif
   296     endif
   392 endef
   297 endef
   393 
   298 
   394 # Make sure logging is setup for everyone that includes MakeBase.gmk.
   299 # Make sure logging is setup for everyone that includes MakeBase.gmk.
   395 $(eval $(call SetupLogging))
   300 $(eval $(call SetupLogging))
   396 
   301 
       
   302 # This is to be called by all SetupFoo macros
       
   303 define LogSetupMacroEntry
       
   304     $(if $(26),$(error Internal makefile error: Too many arguments to LogSetupMacroEntry, please update MakeBase.gmk))
       
   305     $(if $(findstring $(LOG),debug trace), $(info $1 $(foreach i,2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25,$(if $($i),$(NEWLINE) $(strip [$i] $($i))))))
       
   306 endef
       
   307 
       
   308 # Make directory without forking mkdir if not needed
       
   309 define MakeDir
       
   310     ifneq ($$(wildcard $1 $2 $3 $4 $5 $6 $7 $8 $9),$$(strip $1 $2 $3 $4 $5 $6 $7 $8 $9))
       
   311         $$(shell $(MKDIR) -p $1 $2 $3 $4 $5 $6 $7 $8 $9)
       
   312     endif
       
   313 endef
       
   314 
       
   315 ifeq ($(OPENJDK_TARGET_OS),solaris)
       
   316 # On Solaris, if the target is a symlink and exists, cp won't overwrite.
       
   317 define install-file
       
   318 	$(MKDIR) -p $(@D)
       
   319 	$(RM) '$@'
       
   320 	$(CP) -f -r -P '$<' '$(@D)'
       
   321 endef
       
   322 else ifeq ($(OPENJDK_TARGET_OS),macosx)
       
   323 define install-file
       
   324 	$(MKDIR) -p $(@D)
       
   325 	$(CP) -fpRP '$<' '$@'
       
   326 endef
       
   327 else
       
   328 define install-file
       
   329 	$(MKDIR) -p $(@D)
       
   330 	$(CP) -fP '$<' '$@'
       
   331 endef
       
   332 endif
       
   333 
   397 endif # _MAKEBASE_GMK
   334 endif # _MAKEBASE_GMK