2
# Copyright (c) 2011, 2022, 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 _ZIP_ARCHIVE_GMK
29
# Depends on build tools for MakeZipReproducible
30
include ../ToolsJdk.gmk
32
ifeq (,$(_MAKEBASE_GMK))
33
$(error You must include MakeBase.gmk prior to including ZipArchive.gmk)
36
# Setup make rules for creating a zip archive.
38
# Parameter 1 is the name of the rule. This name is used as variable prefix,
39
# and the targets generated are listed in a variable by that name.
41
# Remaining parameters are named arguments. These include:
48
# EXCLUDE_PATTERNS - Patterns with at most one % wildcard matching filenames
50
# EXCLUDE_PATTERNS_$dir - Exclude patterns just like above but specific to one
54
# FOLLOW_SYMLINKS - Set to explicitly follow symlinks. Affects performance of
56
# ZIP_OPTIONS extra options to pass to zip
57
# REPRODUCIBLE set to false to disable the step that makes zip reproducible
59
SetupZipArchive = $(NamedParamsMacroTemplate)
60
define SetupZipArchiveBody
62
# Create a version $1_SRC with a guaranteed trailing slash
63
$1_SRC_SLASH := $$(addsuffix /, $$(patsubst %/, %, $$($1_SRC)))
65
# To avoid running find over too large sets of files, which causes make to crash
66
# on some configurations (cygwin), use INCLUDES and INCLUDE_FILES to build a set
67
# of directories to run find in, if available.
68
ifneq ($$($1_INCLUDES)$$($1_INCLUDE_FILES),)
69
$1_FIND_LIST := $$(wildcard $$(foreach s,$$($1_SRC_SLASH), \
70
$$(addprefix $$s,$$($1_INCLUDES) $$($1_INCLUDE_FILES))))
72
$1_FIND_LIST := $$($1_SRC_SLASH)
75
# Find all files in the source tree.
76
# If asked to, follow symlinks in this find since that is what zip does. To do
77
# this, we need to call ShellFindFiles directly.
78
ifeq ($$($1_FOLLOW_SYMLINKS), true)
79
$1_ALL_SRCS := $$(call not-containing,_the.,$$(call ShellFindFiles,$$($1_FIND_LIST), , -L))
81
$1_ALL_SRCS := $$(call not-containing,_the.,$$(call FindFiles,$$($1_FIND_LIST)))
84
# Filter on suffixes if set
85
ifneq ($$($1_SUFFIXES),)
86
$1_ALL_SRCS := $$(filter $$(addprefix %, $$($1_SUFFIXES)), $$($1_ALL_SRCS))
89
ifneq ($$($1_INCLUDES),)
90
ifneq ($$($1_SUFFIXES),)
91
$1_ZIP_INCLUDES := $$(foreach s,$$($1_SUFFIXES), \
92
$$(addprefix -i$(SPACE)$(DQUOTE),$$(addsuffix /*$$s$(DQUOTE),$$($1_INCLUDES))))
94
$1_ZIP_INCLUDES := $$(addprefix -i$(SPACE)$(DQUOTE),$$(addsuffix /*$(DQUOTE),$$($1_INCLUDES)))
97
ifneq ($$($1_SUFFIXES),)
98
$1_ZIP_INCLUDES := $$(foreach s,$$($1_SUFFIXES), \
99
$$(addprefix -i$(SPACE)$(DQUOTE),*$$s$(DQUOTE)))
102
ifneq ($$($1_INCLUDE_FILES),)
103
$1_ZIP_INCLUDES += $$(addprefix -i$(SPACE),$$($1_INCLUDE_FILES))
105
ifneq ($$($1_EXCLUDES),)
106
$1_ZIP_EXCLUDES := $$(addprefix -x$(SPACE)$(DQUOTE),$$(addsuffix /*$(DQUOTE),$$($1_EXCLUDES)))
107
$1_SRC_EXCLUDES := $$(foreach s,$$($1_SRC_SLASH),$$(addprefix $$s,$$(addsuffix /%,$$($1_EXCLUDES))))
108
$1_ALL_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_ALL_SRCS))
110
ifneq ($$($1_EXCLUDE_FILES),)
111
$1_SRC_EXCLUDE_FILES := $$(addprefix %, $$($1_EXCLUDE_FILES)) $$($1_EXCLUDE_FILES)
112
$1_ALL_SRCS := $$(filter-out $$($1_SRC_EXCLUDE_FILES), $$($1_ALL_SRCS))
113
$$(foreach s, $$($1_SRC_SLASH), \
114
$$(eval $1_ZIP_EXCLUDES_$$s += \
115
$$(addprefix -x$$(SPACE), $$(patsubst $$s%,%, $$($1_EXCLUDE_FILES))) \
119
ifneq ($$($1_EXCLUDE_PATTERNS), )
120
$1_ALL_SRCS := $$(filter-out $$($1_EXCLUDE_PATTERNS), $$($1_ALL_SRCS))
121
$1_ZIP_EXCLUDES += $$(addprefix -x$(SPACE), $$(subst %,\*,$$($1_EXCLUDE_PATTERNS)))
123
# Rewrite src dir specific exclude patterns to zip excludes
124
$$(foreach s, $$($1_SRC_SLASH), \
125
$$(if $$($1_EXCLUDE_PATTERNS_$$s), \
126
$$(eval $1_ZIP_EXCLUDES_$$s += \
127
$$(addprefix -x$$(SPACE), $$(subst %,\*,$$($1_EXCLUDE_PATTERNS_$$s))) \
132
ifeq ($$($1_REPRODUCIBLE), )
133
$1_REPRODUCIBLE := true
136
# Use a slightly shorter name for logging, but with enough path to identify this zip.
137
$1_NAME:=$$(subst $$(OUTPUTDIR)/,,$$($1_ZIP))
139
# Now $1_ALL_SRCS should contain all sources that are going to be put into the zip.
140
# I.e. the zip -i and -x options should match the filtering done in the makefile.
141
# Explicitly excluded files can be given with absolute path. The patsubst solution
142
# isn't perfect but the likelihood of an absolute path to match something in a src
144
# If zip has nothing to do, it returns 12 and would fail the build. Check for 12
145
# and only fail if it's not.
146
# For reproducible builds set the zip access & modify times to SOURCE_DATE_EPOCH
147
# by using a ziptmp folder to generate final zip from using MakeZipReproducible.
148
$$($1_ZIP) : $$($1_ALL_SRCS) $$($1_EXTRA_DEPS)
149
$$(call LogWarn, Updating $$($1_NAME))
150
$$(call MakeTargetDir)
151
# Find duplicate file names in the SRC and generate excludes for all
152
# instances that should not be included. Run this rather expensive
153
# calculation as part of the recipe to avoid running it when nothing
154
# needs to be rebuilt. The drawback is that we cannot exclude these
155
# files from the make prerequisites list, but the number of files is
156
# usually small so a very rare unnecessary rebuild is worth it.
157
# (The inner most foreach here is used instead of eval to declare a
159
$$(foreach root, $$($1_SRC_SLASH), \
160
$$(foreach file, $$(filter $$(root)%, $$($1_ALL_SRCS)), \
161
$$(foreach relfile, $$(patsubst $$(root)%, %, $$(file)), \
162
$$(if $$($1_relfiles_$$(call DoubleDollar, $$(relfile))), \
163
$$(eval $1_ZIP_EXCLUDES_$$(root) += -x $$(relfile)) \
165
$$(eval $1_relfiles_$$(call DoubleDollar, $$(relfile)) := 1) \
170
$$(foreach s,$$($1_SRC_SLASH), $$(call ExecuteWithLog, \
171
$$(SUPPORT_OUTPUTDIR)/zip/$$(patsubst $$(OUTPUTDIR)/%,%, $$@), \
172
(cd $$s && $(ZIPEXE) -qru $$($1_ZIP_OPTIONS) $$@ . \
173
$$($1_ZIP_INCLUDES) $$($1_ZIP_EXCLUDES) -x \*_the.\* \
174
$$($1_ZIP_EXCLUDES_$$s) \
175
|| test "$$$$?" = "12" \
178
ifeq ($$($1_REPRODUCIBLE), true)
179
$$(call ExecuteWithLog, \
180
$$(SUPPORT_OUTPUTDIR)/makezipreproducible/$$(patsubst $$(OUTPUTDIR)/%,%, $$@), \
181
($(RM) $$(SUPPORT_OUTPUTDIR)/ziptmp/$1/tmp.zip && \
182
$(MKDIR) -p $$(SUPPORT_OUTPUTDIR)/ziptmp/$1 && \
183
$(TOOL_MAKEZIPREPRODUCIBLE) -f $$(SUPPORT_OUTPUTDIR)/ziptmp/$1/tmp.zip \
184
-t $(SOURCE_DATE_EPOCH) $$@ && \
186
$(MV) $$(SUPPORT_OUTPUTDIR)/ziptmp/$1/tmp.zip $$@ \
191
# Add zip to target list
195
endif # _ZIP_ARCHIVE_GMK