2
# Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
3
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26
ifndef _JAR_ARCHIVE_GMK
29
ifeq (,$(_MAKEBASE_GMK))
30
$(error You must include MakeBase.gmk prior to including JarArchive.gmk)
35
FALSE_FIND_PATTERN:=-name FILE_NAME_THAT_DOESNT_EXIST
37
# Setup make rules for creating a jar archive.
39
# Parameter 1 is the name of the rule. This name is used as variable prefix,
40
# and the targets generated are listed in a variable by that name.
42
# Remaining parameters are named arguments. These include:
43
# DEPENDENCIES:=List of dependencies for the jar target. If left empty,
44
# dependencies are calculated automatically from the source files found.
45
# For this to work, the source files must exist when the makefile is
47
# SRCS:=List of directories in where to find files to add to archive
48
# BIN:=Directory where to store build control files
49
# SUFFIXES:=File suffixes to include in jar
50
# INCLUDES:=List of directories/packages in SRCS that should be included
51
# EXCLUDES:=List of directories/packages in SRCS that should be excluded
52
# EXCLUDE_FILES:=List of files in SRCS that should be excluded
53
# EXTRA_FILES:=List of files in SRCS that should be included regardless of suffix match.
54
# JAR:=Jar file to create
55
# MANIFEST:=Optional manifest file template.
56
# JARMAIN:=Optional main class to add to manifest
57
# JARINDEX:=true means generate the index in the jar file.
58
# SKIP_METAINF:=Set to prevent contents of an META-INF directory to be automatically
59
# added to the archive.
60
# EXTRA_MANIFEST_ATTR:=Extra attribute to add to manifest.
61
# CHECK_COMPRESS_JAR Check the COMPRESS_JAR variable
62
# JAR_CMD:=Optionally override the jar command to use when creating the archive.
63
SetupJarArchive = $(NamedParamsMacroTemplate)
64
define SetupJarArchiveBody
66
$1_JARMAIN:=$(strip $$($1_JARMAIN))
67
$1_JARNAME:=$$(notdir $$($1_JAR))
68
$1_JAR_OUTPUT_DIR := $$(patsubst %/, %, $$(dir $$($1_JAR)))
69
$$(call SetIfEmpty, $1_BIN, $$($1_JAR_OUTPUT_DIR))
70
$1_MANIFEST_FILE:=$$($1_BIN)/_the.$$($1_JARNAME)_manifest
71
$1_DELETESS_FILE:=$$($1_BIN)/_the.$$($1_JARNAME)_deletess
72
$1_DELETES_FILE:=$$($1_BIN)/_the.$$($1_JARNAME)_deletes
73
$$(call SetIfEmpty, $1_JAR_CMD, $$(JAR))
75
ifeq (,$$($1_SUFFIXES))
76
# No suffix was set, default to classes.
79
# Convert suffixes to a find expression
80
$1_FIND_PATTERNS:=$(FALSE_FIND_PATTERN) $$(patsubst %,$(SPACE)-o$(SPACE)-name$(SPACE)$(DQUOTE)*%$(DQUOTE),$$($1_SUFFIXES))
81
# On windows, a lot of includes/excludes risk making the command line too long, so
82
# writing the grep patterns to files.
83
# Grep returns 1 if nothing is matched. Do not fail the build for this.
84
ifneq (,$$($1_INCLUDES))
85
$1_GREP_INCLUDE_PATTERNS:=$$(call EscapeDollar, \
86
$$(foreach src,$$($1_SRCS), $$(addprefix $$(src)/,$$($1_INCLUDES))))
87
# If there are a lot of include patterns, output to file to shorten command lines
88
ifeq ($$(word 20,$$($1_GREP_INCLUDE_PATTERNS)),)
89
$1_GREP_INCLUDES:=| ( $(GREP) $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_INCLUDE_PATTERNS)) \
90
|| test "$$$$?" = "1" )
92
$1_GREP_INCLUDE_OUTPUT = \
93
$$(eval $$(call ListPathsSafely,$1_GREP_INCLUDE_PATTERNS, \
94
$$($1_BIN)/_the.$$($1_JARNAME)_include))
95
$1_GREP_INCLUDES:=| ( $(GREP) -f $$($1_BIN)/_the.$$($1_JARNAME)_include \
96
|| test "$$$$?" = "1" )
99
ifneq (,$$($1_EXCLUDES)$$($1_EXCLUDE_FILES))
100
$1_GREP_EXCLUDE_PATTERNS:=$$(call EscapeDollar, \
101
$$(foreach src,$$($1_SRCS),$$(addprefix $$(src)/, \
102
$$($1_EXCLUDES) $$($1_EXCLUDE_FILES))))
103
# If there are a lot of include patterns, output to file to shorten command lines
104
ifeq ($$(word 20,$$($1_GREP_EXCLUDE_PATTERNS)),)
105
$1_GREP_EXCLUDES:=| ( $(GREP) -v $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_EXCLUDE_PATTERNS)) \
106
|| test "$$$$?" = "1" )
108
$1_GREP_EXCLUDE_OUTPUT = \
109
$$(eval $$(call ListPathsSafely,$1_GREP_EXCLUDE_PATTERNS, \
110
$$($1_BIN)/_the.$$($1_JARNAME)_exclude))
111
$1_GREP_EXCLUDES:=| ( $(GREP) -v -f $$($1_BIN)/_the.$$($1_JARNAME)_exclude \
112
|| test "$$$$?" = "1" )
116
# Check if this jar needs to have its index generated.
117
ifneq (,$$($1_JARINDEX))
118
$1_JARINDEX = (cd $$(dir $$@) && $$($1_JAR_CMD) -i $$(notdir $$@))
122
# When this macro is run in the same makefile as the java compilation, dependencies are
123
# transferred in make variables. When the macro is run in a different makefile than the
124
# java compilation, the dependencies need to be found in the filesystem.
125
$1_ORIG_DEPS := $$($1_DEPENDENCIES)
126
ifeq ($$($1_DEPENDENCIES), )
127
# Add all source roots to the find cache since we are likely going to run find
128
# on these more than once. The cache will only be updated if necessary.
129
$$(call FillFindCache, $$($1_FIND_LIST))
130
$1_DEPENDENCIES:=$$(filter $$(addprefix %,$$($1_SUFFIXES)), \
131
$$(call FindFiles,$$($1_SRCS)))
132
ifneq (,$$($1_GREP_INCLUDE_PATTERNS))
133
$1_DEPENDENCIES:=$$(filter $$(addsuffix %,$$($1_GREP_INCLUDE_PATTERNS)),$$($1_DEPENDENCIES))
135
ifneq (,$$($1_GREP_EXCLUDE_PATTERNS))
136
$1_DEPENDENCIES:=$$(filter-out $$(addsuffix %,$$($1_GREP_EXCLUDE_PATTERNS)),$$($1_DEPENDENCIES))
138
# Look for EXTRA_FILES in all SRCS dirs and as absolute paths.
139
$1_DEPENDENCIES+=$$(wildcard $$(foreach src, $$($1_SRCS), \
140
$$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES))
141
ifeq (,$$($1_SKIP_METAINF))
142
$1_DEPENDENCIES+=$$(call FindFiles,$$(wildcard $$(addsuffix /META-INF,$$($1_SRCS))))
145
# The dependency list should never be empty
146
ifeq ($$(strip $$($1_DEPENDENCIES)), )
147
$$(warning No dependencies found for $1)
150
# Utility macros, to make the shell script receipt somewhat easier to decipher.
152
# Capture extra files is the same for both CAPTURE_CONTENTS and SCAPTURE_CONTENTS so
153
# only define it once to avoid duplication.
154
# The list of extra files might be long, so need to use ListPathsSafely to print
155
# them out to a separate file. Then process the contents of that file to rewrite
156
# into -C <dir> <file> lines.
157
# The EXTRA_FILES_RESOLVED variable must be set in the macro so that it's evaluated
158
# in the recipe when the files are guaranteed to exist.
159
$1_CAPTURE_EXTRA_FILES=\
160
$$(eval $1_EXTRA_FILES_RESOLVED:=$$(call DoubleDollar, \
161
$$(wildcard $$(foreach src, $$($1_SRCS), \
162
$$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES)))) \
163
$$(if $$($1_EXTRA_FILES_RESOLVED), \
164
$$(eval $$(call ListPathsSafely,$1_EXTRA_FILES_RESOLVED, \
165
$$($1_BIN)/_the.$$($1_JARNAME)_contents.extra)) \
166
$(SED) $$(foreach src,$$($1_SRCS), -e 's|$$(src)/|-C $$(src) |g') \
167
$$($1_BIN)/_the.$$($1_JARNAME)_contents.extra \
168
>> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE))
170
# The capture contents macro finds all files (matching the patterns, typically
171
# .class and .prp) that are newer than the jar-file, ie the new content to be put into the jar.
172
# NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
173
$1_CAPTURE_CONTENTS=\
174
$(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
175
$$(foreach src,$$($1_SRCS), \
176
$(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) -a -newer $$@ $$($1_GREP_INCLUDES) \
177
$$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
178
>> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
179
$$($1_CAPTURE_EXTRA_FILES)
181
# The capture metainf macro finds all files below the META-INF directory that are newer than the jar-file.
182
# Find returns non zero if the META-INF dir does not exist, ignore this.
183
ifeq (,$$($1_SKIP_METAINF))
184
$1_CAPTURE_METAINF =$$(foreach src,$$($1_SRCS), \
185
( ( $(FIND) $$(src)/META-INF -type f -a -newer $$@ 2> /dev/null || true ) \
186
| $(SED) 's|$$(src)/|-C $$(src) |g' >> \
187
$$($1_BIN)/_the.$$($1_JARNAME)_contents ) $$(NEWLINE) )
189
# The capture deletes macro finds all deleted files and concatenates them. The resulting file
190
# tells us what to remove from the jar-file.
191
$1_CAPTURE_DELETES=$$(foreach src,$$($1_SRCS),($(FIND) $$(src) -name _the.package.deleted -newer $$@ \
192
-exec $(SED) 's|$$(src)||g' \{\} >> $$($1_DELETES_FILE) \;) $$(NEWLINE))
193
# The update contents macro updates the jar file with the previously capture contents.
194
# Use 'wc -w' to see if the contents file is empty.
196
if [ "`$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'`" -gt "0" ]; then \
197
$(ECHO) " updating" `$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'` files && \
198
$(SORT) $$($1_BIN)/_the.$$($1_JARNAME)_contents > $$($1_BIN)/_the.$$($1_JARNAME)_contents_sorted && \
199
$$($1_JAR_CMD) --update $$($1_JAR_OPTIONS) --file $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents_sorted; \
201
# The s-variants of the above macros are used when the jar is created from scratch.
202
# NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
203
$1_SCAPTURE_CONTENTS=\
204
$(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
205
$$(foreach src,$$($1_SRCS), \
206
$(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) $$($1_GREP_INCLUDES) \
207
$$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
208
>> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
209
$$($1_CAPTURE_EXTRA_FILES)
211
# Find returns non zero if the META-INF dir does not exist, ignore this.
212
ifeq (,$$($1_SKIP_METAINF))
213
$1_SCAPTURE_METAINF=$$(foreach src,$$($1_SRCS), \
214
( ( $(FIND) $$(src)/META-INF -type f 2> /dev/null || true ) \
215
| $(SED) 's|$$(src)/|-C $$(src) |g' >> \
216
$$($1_BIN)/_the.$$($1_JARNAME)_contents) $$(NEWLINE) )
218
$1_SUPDATE_CONTENTS=\
219
$(SORT) $$($1_BIN)/_the.$$($1_JARNAME)_contents > $$($1_BIN)/_the.$$($1_JARNAME)_contents_sorted && \
220
$$($1_JAR_CMD) --update $$($1_JAR_OPTIONS) --file $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents_sorted $$(NEWLINE)
222
# Use a slightly shorter name for logging, but with enough path to identify this jar.
223
$1_NAME:=$$(subst $$(OUTPUTDIR)/,,$$($1_JAR))
225
# If reproducible build and the boot jdk jar supports --date option
226
# then specify the --date using SOURCE_DATE in ISO-8601
228
ifeq ($$(BOOT_JDK_JAR_SUPPORTS_DATE), true)
229
$1_JAR_OPTIONS += --date $(SOURCE_DATE_ISO_8601)
231
ifneq (,$$($1_CHECK_COMPRESS_JAR))
232
ifneq ($(COMPRESS_JARS), true)
233
$1_JAR_OPTIONS += --no-compress
237
# Include all variables of significance in the vardeps file
238
$1_VARDEPS := $$($1_JAR_CMD) $$($1_JAR_OPTIONS) $$($1_MANIFEST) \
239
$$($1_JARMAIN) $$($1_EXTRA_MANIFEST_ATTR) $$($1_ORIG_DEPS) $$($1_SRCS) \
240
$$($1_INCLUDES) $$($1_EXCLUDES) $$($1_EXCLUDE_FILES) $$($1_EXTRA_FILES)
241
$1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, $$($1_BIN)/_the.$$($1_JARNAME).vardeps)
243
# Here is the rule that creates/updates the jar file.
244
$$($1_JAR) : $$($1_DEPENDENCIES) $$($1_MANIFEST) $$($1_VARDEPS_FILE)
245
$$(call MakeTargetDir)
246
$$(call MakeDir, $$($1_BIN))
247
$$($1_GREP_INCLUDE_OUTPUT)
248
$$($1_GREP_EXCLUDE_OUTPUT)
249
# If the vardeps file is part of the newer prereq list, it means that
250
# either the jar file does not exist, or we need to recreate it from
251
# from scratch anyway since a simple update will not catch all the
253
$$(if $$(filter $$($1_VARDEPS_FILE) $$($1_MANIFEST), $$?), \
254
$$(if $$($1_MANIFEST), \
255
$(CP) $$($1_MANIFEST) $$($1_MANIFEST_FILE) $$(NEWLINE) \
257
$(RM) $$($1_MANIFEST_FILE) && $(TOUCH) $$($1_MANIFEST_FILE) $$(NEWLINE)) \
258
$$(if $$($1_JARMAIN), \
259
$(ECHO) "Main-Class: $$(strip $$($1_JARMAIN))" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \
260
$$(if $$($1_EXTRA_MANIFEST_ATTR), \
261
$(PRINTF) "$$($1_EXTRA_MANIFEST_ATTR)\n" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \
262
$(ECHO) Creating $$($1_NAME) $$(NEWLINE) \
263
$$($1_JAR_CMD) --create $$($1_JAR_OPTIONS) --file $$@ --manifest $$($1_MANIFEST_FILE) $$(NEWLINE) \
264
$$($1_SCAPTURE_CONTENTS) \
265
$$($1_SCAPTURE_METAINF) \
266
$$($1_SUPDATE_CONTENTS) \
267
$$($1_JARINDEX) && true \
269
$(ECHO) Modifying $$($1_NAME) $$(NEWLINE) \
270
$$($1_CAPTURE_CONTENTS) \
271
$$($1_CAPTURE_METAINF) \
272
$(RM) $$($1_DELETES_FILE) $$(NEWLINE) \
273
$$($1_CAPTURE_DELETES) \
274
$(CAT) $$($1_DELETES_FILE) > $$($1_DELETESS_FILE) $$(NEWLINE) \
275
if [ -s $$($1_DELETESS_FILE) ]; then \
276
$(ECHO) " deleting" `$(WC) -l $$($1_DELETESS_FILE) | $(AWK) '{ print $$$$1 }'` files && \
277
$(ZIPEXE) -q -d $$@ `$(CAT) $$($1_DELETESS_FILE)` ; \
279
$$($1_UPDATE_CONTENTS) true $$(NEWLINE) \
280
$$($1_JARINDEX) && true )
282
# Add jar to target list
286
endif # _JAR_ARCHIVE_GMK