1 #
  2 # Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
  3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4 #
  5 # This code is free software; you can redistribute it and/or modify it
  6 # under the terms of the GNU General Public License version 2 only, as
  7 # published by the Free Software Foundation.  Oracle designates this
  8 # particular file as subject to the "Classpath" exception as provided
  9 # by Oracle in the LICENSE file that accompanied this code.
 10 #
 11 # This code is distributed in the hope that it will be useful, but WITHOUT
 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14 # version 2 for more details (a copy is included in the LICENSE file that
 15 # accompanied this code).
 16 #
 17 # You should have received a copy of the GNU General Public License version
 18 # 2 along with this work; if not, write to the Free Software Foundation,
 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20 #
 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22 # or visit www.oracle.com if you need additional information or have any
 23 # questions.
 24 #
 25 
 26 ################################################################################
 27 # This file contains helper functions for Init.gmk.
 28 ################################################################################
 29 
 30 # Define basic logging setup
 31 BUILD_LOG := $(OUTPUTDIR)/build.log
 32 BUILD_PROFILE_LOG := $(OUTPUTDIR)/build-profile.log
 33 
 34 BUILD_LOG_PIPE := > >($(TEE) -a $(BUILD_LOG)) 2> >($(TEE) -a $(BUILD_LOG) >&2) && wait
 35 # Use this for simple echo/printf commands that are never expected to print
 36 # to stderr.
 37 BUILD_LOG_PIPE_SIMPLE := | $(TEE) -a $(BUILD_LOG)
 38 
 39 ifneq ($(CUSTOM_ROOT), )
 40   topdir = $(CUSTOM_ROOT)
 41 else
 42   topdir = $(TOPDIR)
 43 endif
 44 
 45 # Setup the build environment to match the requested specification on
 46 # level of reproducible builds
 47 define SetupReproducibleBuild
 48   ifeq ($$(SOURCE_DATE), updated)
 49     # For static values of SOURCE_DATE (not "updated"), these are set in spec.gmk
 50     export SOURCE_DATE_EPOCH := $$(shell $$(DATE) +"%s")
 51     export SOURCE_DATE_ISO_8601 := $$(call EpochToISO8601, $$(SOURCE_DATE_EPOCH))
 52   endif
 53 endef
 54 
 55 # Parse COMPARE_BUILD into COMPARE_BUILD_*
 56 # Syntax: COMPARE_BUILD=CONF=<configure options>:PATCH=<patch file>:
 57 #         MAKE=<make targets>:COMP_OPTS=<compare script options>:
 58 #         COMP_DIR=<compare script base dir>|<default>:
 59 #         FAIL=<bool>
 60 # If neither CONF or PATCH is given, assume <default> means CONF if it
 61 # begins with "--", otherwise assume it means PATCH.
 62 # MAKE and COMP_OPTS can only be used with CONF and/or PATCH specified.
 63 # If any value contains "+", it will be replaced by space.
 64 # FAIL can be set to false to have the return value of compare be ignored.
 65 define ParseCompareBuild
 66   ifneq ($$(COMPARE_BUILD), )
 67     COMPARE_BUILD_OUTPUTDIR := $(topdir)/build/compare-build/$(CONF_NAME)
 68     COMPARE_BUILD_FAIL := true
 69 
 70     ifneq ($$(findstring :, $$(COMPARE_BUILD)), )
 71       $$(foreach part, $$(subst :, , $$(COMPARE_BUILD)), \
 72         $$(if $$(filter PATCH=%, $$(part)), \
 73           $$(eval COMPARE_BUILD_PATCH = $$(strip $$(patsubst PATCH=%, %, $$(part)))) \
 74         ) \
 75         $$(if $$(filter CONF=%, $$(part)), \
 76           $$(eval COMPARE_BUILD_CONF = $$(strip $$(subst +, , $$(patsubst CONF=%, %, $$(part))))) \
 77         ) \
 78         $$(if $$(filter MAKE=%, $$(part)), \
 79           $$(eval COMPARE_BUILD_MAKE = $$(strip $$(subst +, , $$(patsubst MAKE=%, %, $$(part))))) \
 80         ) \
 81         $$(if $$(filter COMP_OPTS=%, $$(part)), \
 82           $$(eval COMPARE_BUILD_COMP_OPTS = $$(strip $$(subst +, , $$(patsubst COMP_OPTS=%, %, $$(part))))) \
 83         ) \
 84         $$(if $$(filter COMP_DIR=%, $$(part)), \
 85           $$(eval COMPARE_BUILD_COMP_DIR = $$(strip $$(subst +, , $$(patsubst COMP_DIR=%, %, $$(part))))) \
 86         ) \
 87         $$(if $$(filter FAIL=%, $$(part)), \
 88           $$(eval COMPARE_BUILD_FAIL = $$(strip $$(subst +, , $$(patsubst FAIL=%, %, $$(part))))) \
 89         ) \
 90         $$(if $$(filter NODRYRUN=%, $$(part)), \
 91           $$(eval COMPARE_BUILD_NODRYRUN = $$(strip $$(subst +, , $$(patsubst NODRYRUN=%, %, $$(part))))) \
 92         ) \
 93       )
 94     else
 95       # Separate handling for single field case, to allow for spaces in values.
 96       ifneq ($$(filter PATCH=%, $$(COMPARE_BUILD)), )
 97         COMPARE_BUILD_PATCH = $$(strip $$(patsubst PATCH=%, %, $$(COMPARE_BUILD)))
 98       else ifneq ($$(filter CONF=%, $$(COMPARE_BUILD)), )
 99         COMPARE_BUILD_CONF = $$(strip $$(subst +, , $$(patsubst CONF=%, %, $$(COMPARE_BUILD))))
100       else ifneq ($$(filter --%, $$(COMPARE_BUILD)), )
101         # Assume CONF if value begins with --
102         COMPARE_BUILD_CONF = $$(strip $$(subst +, , $$(COMPARE_BUILD)))
103       else
104         # Otherwise assume patch file
105         COMPARE_BUILD_PATCH = $$(strip $$(COMPARE_BUILD))
106       endif
107     endif
108     ifneq ($$(COMPARE_BUILD_PATCH), )
109       ifneq ($$(wildcard $$(topdir)/$$(COMPARE_BUILD_PATCH)), )
110         # Assume relative path, if file exists
111         COMPARE_BUILD_PATCH := $$(wildcard $$(topdir)/$$(COMPARE_BUILD_PATCH))
112       else ifeq ($$(wildcard $$(COMPARE_BUILD_PATCH)), )
113         $$(error Patch file $$(COMPARE_BUILD_PATCH) does not exist)
114       endif
115       ifneq ($$(COMPARE_BUILD_NODRYRUN), true)
116         PATCH_DRY_RUN := $$(shell cd $$(topdir) && $$(PATCH) --dry-run -p1 < $$(COMPARE_BUILD_PATCH) > /dev/null 2>&1 || $$(ECHO) FAILED)
117         ifeq ($$(PATCH_DRY_RUN), FAILED)
118           $$(error Patch file $$(COMPARE_BUILD_PATCH) does not apply cleanly)
119         endif
120       endif
121     endif
122     ifneq ($$(COMPARE_BUILD_FAIL), true)
123       COMPARE_BUILD_IGNORE_RESULT := || true
124     endif
125   endif
126 endef
127 
128 # Prepare for a comparison rebuild
129 define PrepareCompareBuild
130 	$(ECHO) "Preparing for comparison rebuild"
131         # Apply patch, if any
132 	$(if $(COMPARE_BUILD_PATCH), cd $(topdir) && $(PATCH) -p1 < $(COMPARE_BUILD_PATCH))
133         # Move the first build away temporarily
134 	$(RM) -r $(topdir)/build/.compare-build-temp
135 	$(MKDIR) -p $(topdir)/build/.compare-build-temp
136 	$(MV) $(OUTPUTDIR) $(topdir)/build/.compare-build-temp
137         # Restore an old compare-build, or create a new compare-build directory.
138 	if test -d $(COMPARE_BUILD_OUTPUTDIR); then \
139 	  $(MV) $(COMPARE_BUILD_OUTPUTDIR) $(OUTPUTDIR); \
140 	else \
141 	  $(MKDIR) -p $(OUTPUTDIR); \
142 	fi
143         # Re-run configure with the same arguments (and possibly some additional),
144         # must be done after patching.
145 	( cd $(CONFIGURE_START_DIR) && PATH="$(ORIGINAL_PATH)" \
146 	    $(BASH) $(topdir)/configure $(CONFIGURE_COMMAND_LINE) $(COMPARE_BUILD_CONF))
147 endef
148 
149 # Cleanup after a compare build
150 define CleanupCompareBuild
151         # If running with a COMPARE_BUILD patch, reverse-apply it, but continue
152         # even if that fails (can happen with removed files).
153 	$(if $(COMPARE_BUILD_PATCH), cd $(topdir) && $(PATCH) -R -p1 < $(COMPARE_BUILD_PATCH) || true)
154         # Move this build away and restore the original build
155 	$(MKDIR) -p $(topdir)/build/compare-build
156 	$(MV) $(OUTPUTDIR) $(COMPARE_BUILD_OUTPUTDIR)
157 	$(MV) $(topdir)/build/.compare-build-temp/$(CONF_NAME) $(OUTPUTDIR)
158 	$(RM) -r $(topdir)/build/.compare-build-temp
159 endef
160 
161 # Do the actual comparison of two builds
162 define CompareBuildDoComparison
163         # Compare first and second build. Ignore any error code from compare.sh.
164 	$(ECHO) "Comparing between comparison rebuild (this/new) and baseline (other/old)"
165 	$(if $(COMPARE_BUILD_COMP_DIR), \
166 	  +(cd $(COMPARE_BUILD_OUTPUTDIR) && ./compare.sh --diffs $(COMPARE_BUILD_COMP_OPTS) \
167 	      -2dirs $(COMPARE_BUILD_OUTPUTDIR)/$(COMPARE_BUILD_COMP_DIR) \
168 	      $(OUTPUTDIR)/$(COMPARE_BUILD_COMP_DIR) $(COMPARE_BUILD_IGNORE_RESULT)), \
169 	  +(cd $(COMPARE_BUILD_OUTPUTDIR) && ./compare.sh --diffs $(COMPARE_BUILD_COMP_OPTS) \
170 	      -o $(OUTPUTDIR) $(COMPARE_BUILD_IGNORE_RESULT)) \
171 	)
172 endef
173 
174 define PrintFailureReports
175 	$(if $(filter none, $(LOG_REPORT)), , \
176 	  $(RM) $(MAKESUPPORT_OUTPUTDIR)/failure-summary.log ; \
177 	  $(if $(wildcard $(MAKESUPPORT_OUTPUTDIR)/failure-logs/*.log), \
178 	    ( \
179 	      $(PRINTF) "\n=== Output from failing command(s) repeated here ===\n" ;  \
180 	      $(foreach logfile, $(sort $(wildcard $(MAKESUPPORT_OUTPUTDIR)/failure-logs/*.log)), \
181 	          $(PRINTF) "* For target $(notdir $(basename $(logfile))):\n" ; \
182 	          $(if $(filter all, $(LOG_REPORT)), \
183 	            $(GREP) -v -e "^Note: including file:" <  $(logfile) || true ; \
184 	          , \
185 	            ($(GREP) -v -e "^Note: including file:" <  $(logfile) || true); \
186 	            if test `$(WC) -l < $(logfile)` -gt 15; then \
187 	              $(ECHO) "   ... (rest of output omitted)" ; \
188 	            fi ; \
189 	          ) \
190 	      ) \
191 	      $(PRINTF) "\n* All command lines available in $(MAKESUPPORT_OUTPUTDIR)/failure-logs.\n" ; \
192 	      $(PRINTF) "=== End of repeated output ===\n" ; \
193 	    )  >> $(MAKESUPPORT_OUTPUTDIR)/failure-summary.log  \
194 	  ) \
195 	)
196 endef
197 
198 define PrintBuildLogFailures
199 	$(if $(filter none, $(LOG_REPORT)), , \
200 	  if $(GREP) -q "recipe for target .* failed" $(BUILD_LOG) 2> /dev/null; then  \
201 	    $(PRINTF) "\n=== Make failed targets repeated here ===\n" ; \
202 	    $(GREP) "recipe for target .* failed" $(BUILD_LOG) ; \
203 	    $(PRINTF) "=== End of repeated output ===\n" ; \
204 	    $(PRINTF) "\nHELP: Try searching the build log for the name of the first failed target.\n" ; \
205 	  else \
206 	    $(PRINTF) "\nNo indication of failed target found.\n" ; \
207 	    $(PRINTF) "HELP: Try searching the build log for '] Error'.\n" ; \
208 	  fi >> $(MAKESUPPORT_OUTPUTDIR)/failure-summary.log ; \
209 	  $(CAT) $(MAKESUPPORT_OUTPUTDIR)/failure-summary.log \
210 	)
211 endef
212 
213 define RotateLogFiles
214 	$(RM) $(BUILD_LOG).old 2> /dev/null && \
215 	$(MV) $(BUILD_LOG) $(BUILD_LOG).old 2> /dev/null || true
216 	$(if $(findstring true, $(LOG_PROFILE_TIMES_FILE)), \
217 	  $(RM) $(BUILD_PROFILE_LOG).old 2> /dev/null && \
218 	  $(MV) $(BUILD_PROFILE_LOG) $(BUILD_PROFILE_LOG).old 2> /dev/null || true \
219 	)
220 endef
221 
222 # Failure logs are only supported for "parallel" main targets, not the
223 # (trivial) sequential make targets (such as clean and reconfigure),
224 # since the failure-logs directory creation will conflict with clean.
225 # We also make sure the javatmp directory exists, which is needed if a java
226 # process (like javac) is using java.io.tmpdir.
227 define PrepareFailureLogs
228 	$(RM) -r $(MAKESUPPORT_OUTPUTDIR)/failure-logs 2> /dev/null && \
229 	$(MKDIR) -p $(MAKESUPPORT_OUTPUTDIR)/failure-logs
230 	$(MKDIR) -p $(JAVA_TMP_DIR)
231 	$(RM) $(MAKESUPPORT_OUTPUTDIR)/exit-with-error 2> /dev/null
232 endef
233 
234 # Remove any javac server logs and port files. This
235 # prevents a new make run to reuse the previous servers.
236 define PrepareJavacServer
237 	$(if $(JAVAC_SERVER_DIR), \
238 	  $(RM) -r $(JAVAC_SERVER_DIR) 2> /dev/null && \
239 	  $(MKDIR) -p $(JAVAC_SERVER_DIR) \
240 	)
241 endef
242 
243 define CleanupJavacServer
244 	[ -f $(JAVAC_SERVER_DIR)/server.port ] && $(ECHO) Stopping javac server && \
245 	    $(TOUCH) $(JAVAC_SERVER_DIR)/server.port.stop; true
246 endef
247 
248 ifeq ($(call isBuildOs, windows), true)
249   # On windows we need to synchronize with the javac server to be able to
250   # move or remove the build output directory. Since we have no proper
251   # synchronization process, wait for a while and hope it helps. This is only
252   # used by build comparisons.
253     define WaitForJavacServerFinish
254 	$(if $(JAVAC_SERVER_DIR), \
255 	  sleep 5 \
256 	)
257   endef
258 else
259   define WaitForJavacServerFinish
260   endef
261 endif
262 
263 ##############################################################################
264 # Functions for timers
265 ##############################################################################
266 
267 # Store the build times in this directory.
268 BUILDTIMESDIR = $(OUTPUTDIR)/make-support/build-times
269 
270 # Record starting time for build of a sub repository.
271 define RecordStartTime
272 	$(DATE) '+%Y %m %d %H %M %S' | $(AWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_start_$(strip $1) && \
273 	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable
274 endef
275 
276 # Record ending time and calculate the difference and store it in a
277 # easy to read format. Handles builds that cross midnight. Expects
278 # that a build will never take 24 hours or more.
279 define RecordEndTime
280 	$(DATE) '+%Y %m %d %H %M %S' | $(AWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)
281 	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable
282 	$(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \
283 	    $(AWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
284 	    M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
285 	    > $(BUILDTIMESDIR)/build_time_diff_$(strip $1)
286 endef
287 
288 define StartGlobalTimer
289 	$(RM) -r $(BUILDTIMESDIR) 2> /dev/null && \
290 	$(MKDIR) -p $(BUILDTIMESDIR) && \
291 	$(call RecordStartTime,TOTAL)
292 endef
293 
294 define StopGlobalTimer
295 	$(call RecordEndTime,TOTAL)
296 endef
297 
298 # Find all build_time_* files and print their contents in a list sorted
299 # on the name of the sub repository.
300 define ReportBuildTimes
301 	$(PRINTF) $(LOG_INFO) -- \
302 	    "----- Build times -------\nStart %s\nEnd   %s\n%s\n%s\n-------------------------\n" \
303 	    "`$(CAT) $(BUILDTIMESDIR)/build_time_start_TOTAL_human_readable`" \
304 	    "`$(CAT) $(BUILDTIMESDIR)/build_time_end_TOTAL_human_readable`" \
305 	    "`$(LS) $(BUILDTIMESDIR)/build_time_diff_* | $(GREP) -v _TOTAL | \
306 	    $(XARGS) $(CAT) | $(SORT) -k 2`" \
307 	    "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_TOTAL`" \
308 	    $(BUILD_LOG_PIPE_SIMPLE)
309 endef
310 
311 define ReportProfileTimes
312   $(if $(findstring true, $(LOG_PROFILE_TIMES_LOG)), \
313     [ ! -f $(BUILD_PROFILE_LOG) ] || \
314     { $(ECHO) Begin $(notdir $(BUILD_PROFILE_LOG)) && \
315       $(CAT) $(BUILD_PROFILE_LOG) && \
316       $(ECHO) End $(notdir $(BUILD_PROFILE_LOG)); \
317     } \
318     $(BUILD_LOG_PIPE_SIMPLE)
319   )
320 endef