1 #
2 # Copyright (c) 2011, 2026, 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)/preview/$(strip $1)
111 $(RM) -r $(SUPPORT_OUTPUTDIR)/special_classes/$(strip $1)
112 $(ECHO) " done"
113 $(PRINTF) "Cleaning headers %s..." "$(if $1,for $(strip $1) )"
114 $(RM) -r $(SUPPORT_OUTPUTDIR)/headers/$(strip $1)
115 @$(ECHO) " done"
116 endef
117
118 define Clean-native
119 @$(PRINTF) "Cleaning native %s..." "$(if $1,for $(strip $1) )"
120 @$(ECHO) "" $(LOG_DEBUG)
121 $(RM) -r $(SUPPORT_OUTPUTDIR)/native/$(strip $1)
122 $(RM) -r $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)
123 $(RM) -r $(SUPPORT_OUTPUTDIR)/modules_cmds/$(strip $1)
124 @$(ECHO) " done"
125 endef
126
127 define Clean-include
128 @$(PRINTF) "Cleaning include %s..." "$(if $1,for $(strip $1) )"
129 @$(ECHO) "" $(LOG_DEBUG)
130 $(RM) -r $(SUPPORT_OUTPUTDIR)/modules_include/$(strip $1)
131 @$(ECHO) " done"
132 endef
133
134 define CleanModule
135 $(call Clean-gensrc, $1)
136 $(call Clean-java, $1)
137 $(call Clean-native, $1)
138 $(call Clean-include, $1)
139 endef
140
141 define AddTestDependency
142 test-$(strip $1): $2
143
144 exploded-test-$(strip $1): $2
145
146 ifneq ($(filter $(TEST), $1), )
147 TEST_DEPS += $2
148 endif
149 endef
150
151 ################################################################################
152
153 PHASE_MAKEDIRS += $(TOPDIR)/make
154
155 # Helper macro for DeclareRecipesForPhase
156 # Declare a recipe for calling the module and phase specific makefile.
157 # If there are multiple makefiles to call, create a rule for each topdir
158 # that contains a makefile with the target $module-$suffix-$repodir,
159 # (i.e: java.base-gensrc-src)
160 # Normally there is only one makefile, and the target will just be
161 # $module-$suffix
162 # Param 1: Name of list to add targets to
163 # Param 2: Module name
164 define DeclareRecipeForModuleMakefile
165 $2-$$($1_TARGET_SUFFIX):
166 +($(CD) $(TOPDIR)/make && $(MAKE) $(MAKE_ARGS) \
167 -f ModuleWrapper.gmk -I $$(TOPDIR)/make/common/modules \
168 $$(patsubst %,-I%/modules/$2,$$(PHASE_MAKEDIRS)) \
169 MODULE=$2 MAKEFILE_PREFIX=$$($1_FILE_PREFIX) $$($1_EXTRA_ARGS))
170
171 endef
172
173 # Helper macro for DeclareRecipesForPhase
174 # Param 1: Name of list to add targets to
175 # Param 2: Module name
176 define DeclareRecipesForPhaseAndModule
177 $1_$2_MAKEFILES := $$(strip $$(wildcard \
178 $$(addsuffix /modules/$2/$$($1_FILE_PREFIX).gmk, $$(PHASE_MAKEDIRS))))
179
180 # Only declare recipes if there are makefiles to call
181 ifneq ($$($1_$2_MAKEFILES), )
182 # Add the top dir specific target to target list regardless of if recipe
183 # generation is disabled.
184 ifeq ($$($1_MULTIPLE_MAKEFILES), true)
185 $$(foreach d, $$($1_$2_TOPDIRS), \
186 $$(eval $1 += $2-$$($1_TARGET_SUFFIX)-$$(notdir $$d)))
187 endif
188 ifeq ($(NO_RECIPES), )
189 $$(eval $$(call DeclareRecipeForModuleMakefile,$1,$2))
190 endif
191 $1 += $2-$$($1_TARGET_SUFFIX)
192 $1_MODULES += $2
193 endif
194 endef
195
196 # Declare recipes for a specific module and build phase if there are makefiles
197 # present for the specific combination.
198 # Param 1: Name of list to add targets to
199 # Named params:
200 # TARGET_SUFFIX : Suffix of target to create for recipe
201 # FILE_PREFIX : File prefix for this build phase
202 # CHECK_MODULES : List of modules to try
203 # MULTIPLE_MAKEFILES : Set to true to handle makefiles for the same module and
204 # phase in multiple repos
205 # EXTRA_ARGS : Add extra make args to each makefile call
206 # Exported variables:
207 # $1_MODULES : All modules that had rules generated
208 # $1_TARGETS : All targets generated
209 define DeclareRecipesForPhase
210 $(foreach i, 2 3 4 5 6 7 8, $(if $(strip $($i)),$(strip $1)_$(strip $($i)))$(NEWLINE))
211 $(if $(9), $(error Internal makefile error: Too many arguments to \
212 DeclareRecipesForPhase, please update MakeHelper.gmk))
213
214 $$(foreach m, $$($(strip $1)_CHECK_MODULES), \
215 $$(eval $$(call DeclareRecipesForPhaseAndModule,$(strip $1),$$m)))
216
217 $(strip $1)_TARGETS := $$($(strip $1))
218 endef
219
220 ################################################################################
221
222 endif # include guard
223 include MakeIncludeEnd.gmk