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 include MakeIncludeStart.gmk
 27 ifeq ($(INCLUDE), true)
 28 
 29 ################################################################################
 30 # This file contains helper functions for Main.gmk.
 31 ################################################################################
 32 
 33 # Setup make rules for creating a top-level target.
 34 # Parameter 1 is the name of the rule. This name is used as variable prefix.
 35 #
 36 # Remaining parameters are named arguments. These include:
 37 #   MAKEFILE the makefile to delegate to
 38 #   TARGET the makefile target
 39 #   ARGS arguments to the makefile
 40 #   DEPS the target(s) this new rule depends on
 41 #   DIR the directory of the makefile (defaults to $(TOPDIR)/make)
 42 #
 43 SetupTarget = $(NamedParamsMacroTemplate)
 44 define SetupTargetBody
 45   ifeq ($$($1_DIR), )
 46     $1_DIR := $(TOPDIR)/make
 47   endif
 48 
 49   $1:
 50 	+($(CD) $$($1_DIR) && $(MAKE) $(MAKE_ARGS) -f $$($1_MAKEFILE).gmk $$($1_TARGET) $$($1_ARGS))
 51 
 52   ALL_TARGETS += $1
 53 
 54   ifneq ($(DEPS), none)
 55     $1: $$($1_DEPS)
 56   endif
 57 endef
 58 
 59 define CleanDocs
 60 	@$(PRINTF) "Cleaning docs ..."
 61 	@$(ECHO) "" $(LOG_DEBUG)
 62 	$(RM) -r $(SUPPORT_OUTPUTDIR)/docs
 63 	$(RM) -r $(SUPPORT_OUTPUTDIR)/javadoc
 64 	$(RM) -r $(IMAGES_OUTPUTDIR)/docs
 65 	@$(ECHO) " done"
 66 endef
 67 
 68 # Cleans the dir given as $1
 69 define CleanDir
 70 	@$(PRINTF) "Cleaning %s build artifacts ..." "$(strip $1)"
 71 	@$(ECHO) "" $(LOG_DEBUG)
 72 	($(CD) $(OUTPUTDIR) && $(RM) -r $1)
 73 	@$(ECHO) " done"
 74 endef
 75 
 76 define CleanSupportDir
 77 	@$(PRINTF) "Cleaning %s build artifacts ..." "$(strip $1)"
 78 	@$(ECHO) "" $(LOG_DEBUG)
 79 	$(RM) -r $(SUPPORT_OUTPUTDIR)/$(strip $1)
 80 	@$(ECHO) " done"
 81 endef
 82 
 83 define CleanMakeSupportDir
 84 	@$(PRINTF) "Cleaning %s make support artifacts ..." "$(strip $1)"
 85 	@$(ECHO) "" $(LOG_DEBUG)
 86 	$(RM) -r $(MAKESUPPORT_OUTPUTDIR)/$(strip $1)
 87 	@$(ECHO) " done"
 88 endef
 89 
 90 define CleanTest
 91 	@$(PRINTF) "Cleaning test %s ..." "$(strip $1)"
 92 	@$(ECHO) "" $(LOG_DEBUG)
 93 	$(RM) -r $(SUPPORT_OUTPUTDIR)/test/$(strip $(subst -,/,$1))
 94         # Remove as much of the test directory structure as is empty
 95 	$(RMDIR) -p $(dir $(SUPPORT_OUTPUTDIR)/test/$(strip $(subst -,/,$1))) 2> /dev/null || true
 96 	@$(ECHO) " done"
 97 endef
 98 
 99 define Clean-gensrc
100 	@$(PRINTF) "Cleaning gensrc %s..." "$(if $1,for $(strip $1) )"
101 	@$(ECHO) "" $(LOG_DEBUG)
102 	$(RM) -r $(SUPPORT_OUTPUTDIR)/gensrc/$(strip $1)
103 	@$(ECHO) " done"
104 endef
105 
106 define Clean-java
107 	@$(PRINTF) "Cleaning java %s..." "$(if $1,for $(strip $1) )"
108 	@$(ECHO) "" $(LOG_DEBUG)
109 	$(RM) -r $(JDK_OUTPUTDIR)/modules/$(strip $1)
110 	$(RM) -r $(SUPPORT_OUTPUTDIR)/special_classes/$(strip $1)
111 	$(ECHO) " done"
112 	$(PRINTF) "Cleaning headers %s..." "$(if $1,for $(strip $1) )"
113 	$(RM) -r $(SUPPORT_OUTPUTDIR)/headers/$(strip $1)
114 	@$(ECHO) " done"
115 endef
116 
117 define Clean-native
118 	@$(PRINTF) "Cleaning native %s..." "$(if $1,for $(strip $1) )"
119 	@$(ECHO) "" $(LOG_DEBUG)
120 	$(RM) -r $(SUPPORT_OUTPUTDIR)/native/$(strip $1)
121 	$(RM) -r $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)
122 	$(RM) -r $(SUPPORT_OUTPUTDIR)/modules_cmds/$(strip $1)
123 	@$(ECHO) " done"
124 endef
125 
126 define Clean-include
127 	@$(PRINTF) "Cleaning include %s..." "$(if $1,for $(strip $1) )"
128 	@$(ECHO) "" $(LOG_DEBUG)
129 	$(RM) -r $(SUPPORT_OUTPUTDIR)/modules_include/$(strip $1)
130 	@$(ECHO) " done"
131 endef
132 
133 define CleanModule
134   $(call Clean-gensrc, $1)
135   $(call Clean-java, $1)
136   $(call Clean-native, $1)
137   $(call Clean-include, $1)
138 endef
139 
140 define AddTestDependency
141   test-$(strip $1): $2
142 
143   exploded-test-$(strip $1): $2
144 
145   ifneq ($(filter $(TEST), $1), )
146     TEST_DEPS += $2
147   endif
148 endef
149 
150 ################################################################################
151 
152 PHASE_MAKEDIRS += $(TOPDIR)/make
153 
154 # Helper macro for DeclareRecipesForPhase
155 # Declare a recipe for calling the module and phase specific makefile.
156 # If there are multiple makefiles to call, create a rule for each topdir
157 # that contains a makefile with the target $module-$suffix-$repodir,
158 # (i.e: java.base-gensrc-src)
159 # Normally there is only one makefile, and the target will just be
160 # $module-$suffix
161 # Param 1: Name of list to add targets to
162 # Param 2: Module name
163 define DeclareRecipeForModuleMakefile
164   $2-$$($1_TARGET_SUFFIX):
165 	+($(CD) $(TOPDIR)/make && $(MAKE) $(MAKE_ARGS) \
166 	    -f ModuleWrapper.gmk -I $$(TOPDIR)/make/common/modules  \
167 	    $$(patsubst %,-I%/modules/$2,$$(PHASE_MAKEDIRS)) \
168 	    MODULE=$2 MAKEFILE_PREFIX=$$($1_FILE_PREFIX) $$($1_EXTRA_ARGS))
169 
170 endef
171 
172 # Helper macro for DeclareRecipesForPhase
173 # Param 1: Name of list to add targets to
174 # Param 2: Module name
175 define DeclareRecipesForPhaseAndModule
176   $1_$2_MAKEFILES := $$(strip $$(wildcard \
177       $$(addsuffix /modules/$2/$$($1_FILE_PREFIX).gmk, $$(PHASE_MAKEDIRS))))
178 
179   # Only declare recipes if there are makefiles to call
180   ifneq ($$($1_$2_MAKEFILES), )
181     # Add the top dir specific target to target list regardless of if recipe
182     # generation is disabled.
183     ifeq ($$($1_MULTIPLE_MAKEFILES), true)
184       $$(foreach d, $$($1_$2_TOPDIRS), \
185         $$(eval $1 += $2-$$($1_TARGET_SUFFIX)-$$(notdir $$d)))
186     endif
187     ifeq ($(NO_RECIPES), )
188       $$(eval $$(call DeclareRecipeForModuleMakefile,$1,$2))
189     endif
190     $1 += $2-$$($1_TARGET_SUFFIX)
191     $1_MODULES += $2
192   endif
193 endef
194 
195 # Declare recipes for a specific module and build phase if there are makefiles
196 # present for the specific combination.
197 # Param 1: Name of list to add targets to
198 # Named params:
199 # TARGET_SUFFIX : Suffix of target to create for recipe
200 # FILE_PREFIX : File prefix for this build phase
201 # CHECK_MODULES : List of modules to try
202 # MULTIPLE_MAKEFILES : Set to true to handle makefiles for the same module and
203 #                      phase in multiple repos
204 # EXTRA_ARGS : Add extra make args to each makefile call
205 # Exported variables:
206 # $1_MODULES : All modules that had rules generated
207 # $1_TARGETS : All targets generated
208 define DeclareRecipesForPhase
209   $(foreach i, 2 3 4 5 6 7 8, $(if $(strip $($i)),$(strip $1)_$(strip $($i)))$(NEWLINE))
210   $(if $(9), $(error Internal makefile error: Too many arguments to \
211       DeclareRecipesForPhase, please update MakeHelper.gmk))
212 
213   $$(foreach m, $$($(strip $1)_CHECK_MODULES), \
214       $$(eval $$(call DeclareRecipesForPhaseAndModule,$(strip $1),$$m)))
215 
216   $(strip $1)_TARGETS := $$($(strip $1))
217 endef
218 
219 ################################################################################
220 
221 endif # include guard
222 include MakeIncludeEnd.gmk