annotate make/common/JarArchive.gmk @ 47290:b90a1b5b8142

manual merge
author mcimadamore
date Mon, 25 Sep 2017 16:38:40 +0100
parents 59bdd7243c52
children 1c1118deeedb
rev   line source
ohair@12258 1 #
ihse@41460 2 # Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
ohair@12258 3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@12258 4 #
ohair@12258 5 # This code is free software; you can redistribute it and/or modify it
ohair@12258 6 # under the terms of the GNU General Public License version 2 only, as
ohair@12258 7 # published by the Free Software Foundation. Oracle designates this
ohair@12258 8 # particular file as subject to the "Classpath" exception as provided
ohair@12258 9 # by Oracle in the LICENSE file that accompanied this code.
ohair@12258 10 #
ohair@12258 11 # This code is distributed in the hope that it will be useful, but WITHOUT
ohair@12258 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@12258 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@12258 14 # version 2 for more details (a copy is included in the LICENSE file that
ohair@12258 15 # accompanied this code).
ohair@12258 16 #
ohair@12258 17 # You should have received a copy of the GNU General Public License version
ohair@12258 18 # 2 along with this work; if not, write to the Free Software Foundation,
ohair@12258 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@12258 20 #
ohair@12258 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@12258 22 # or visit www.oracle.com if you need additional information or have any
ohair@12258 23 # questions.
ohair@12258 24 #
ohair@12258 25
ihse@33438 26 ifndef _JAR_ARCHIVE_GMK
ihse@33438 27 _JAR_ARCHIVE_GMK := 1
chegar@25854 28
ihse@20363 29 ifeq (,$(_MAKEBASE_GMK))
ihse@33438 30 $(error You must include MakeBase.gmk prior to including JarArchive.gmk)
ohair@12258 31 endif
ohair@12258 32
ohair@12258 33 FALSE_FIND_PATTERN:=-name FILE_NAME_THAT_DOESNT_EXIST
ohair@12258 34
ihse@27592 35 # Setup make rules for creating a jar archive.
ihse@27592 36 #
ihse@27592 37 # Parameter 1 is the name of the rule. This name is used as variable prefix,
ihse@27592 38 # and the targets generated are listed in a variable by that name.
ihse@27592 39 #
ihse@27592 40 # Remaining parameters are named arguments. These include:
erikj@29312 41 # DEPENDENCIES:=List of dependencies for the jar target. If left empty,
erikj@29312 42 # dependencies are calculated automatically from the source files found.
erikj@29312 43 # For this to work, the source files must exist when the makefile is
erikj@29312 44 # parsed.
ihse@27592 45 # SRCS:=List of directories in where to find files to add to archive
ihse@27592 46 # SUFFIXES:=File suffixes to include in jar
ihse@27592 47 # INCLUDES:=List of directories/packages in SRCS that should be included
ihse@27592 48 # EXCLUDES:=List of directories/packages in SRCS that should be excluded
ihse@27592 49 # EXCLUDE_FILES:=List of files in SRCS that should be excluded
ihse@27592 50 # EXTRA_FILES:=List of files in SRCS that should be included regardless of suffix match.
ihse@27592 51 # JAR:=Jar file to create
ihse@27592 52 # MANIFEST:=Optional manifest file template.
ihse@27592 53 # JARMAIN:=Optional main class to add to manifest
ihse@27592 54 # JARINDEX:=true means generate the index in the jar file.
ihse@27592 55 # SKIP_METAINF:=Set to prevent contents of an META-INF directory to be automatically
ihse@27592 56 # added to the archive.
ihse@27592 57 # EXTRA_MANIFEST_ATTR:=Extra attribute to add to manifest.
ihse@27592 58 # CHECK_COMPRESS_JAR Check the COMPRESS_JAR variable
ihse@33438 59 SetupJarArchive = $(NamedParamsMacroTemplate)
ihse@33438 60 define SetupJarArchiveBody
ohair@12258 61
ihse@20363 62 $1_JARMAIN:=$(strip $$($1_JARMAIN))
ihse@20363 63 $1_JARNAME:=$$(notdir $$($1_JAR))
ihse@20363 64 $1_MANIFEST_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_manifest
ihse@20363 65 $1_DELETESS_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletess
ihse@20363 66 $1_DELETES_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletes
ihse@20363 67 $1_BIN:=$$(dir $$($1_JAR))
erikj@12801 68
ihse@20363 69 ifeq (,$$($1_SUFFIXES))
ihse@20363 70 # No suffix was set, default to classes.
ihse@20363 71 $1_SUFFIXES:=.class
ihse@20363 72 endif
ihse@20363 73 # Convert suffixes to a find expression
ihse@20363 74 $1_FIND_PATTERNS:=$(FALSE_FIND_PATTERN) $$(patsubst %,$(SPACE)-o$(SPACE)-name$(SPACE)$(DQUOTE)*%$(DQUOTE),$$($1_SUFFIXES))
ihse@20363 75 # On windows, a lot of includes/excludes risk making the command line too long, so
ihse@20363 76 # writing the grep patterns to files.
erikj@28602 77 # Grep returns 1 if nothing is matched. Do not fail the build for this.
ihse@20363 78 ifneq (,$$($1_INCLUDES))
chegar@25854 79 $1_GREP_INCLUDE_PATTERNS:=$$(call EscapeDollar, \
chegar@25854 80 $$(foreach src,$$($1_SRCS), $$(addprefix $$(src)/,$$($1_INCLUDES))))
ihse@20363 81 # If there are a lot of include patterns, output to file to shorten command lines
ihse@20363 82 ifeq ($$(word 20,$$($1_GREP_INCLUDE_PATTERNS)),)
erikj@28602 83 $1_GREP_INCLUDES:=| ( $(GREP) $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_INCLUDE_PATTERNS)) \
erikj@28602 84 || test "$$$$?" = "1" )
ihse@20363 85 else
erikj@32807 86 $1_GREP_INCLUDE_OUTPUT = \
erikj@32807 87 $$(eval $$(call ListPathsSafely,$1_GREP_INCLUDE_PATTERNS, \
erikj@32807 88 $$($1_BIN)/_the.$$($1_JARNAME)_include))
erikj@28602 89 $1_GREP_INCLUDES:=| ( $(GREP) -f $$($1_BIN)/_the.$$($1_JARNAME)_include \
erikj@28602 90 || test "$$$$?" = "1" )
ohair@12258 91 endif
ihse@20363 92 endif
ihse@20363 93 ifneq (,$$($1_EXCLUDES)$$($1_EXCLUDE_FILES))
chegar@25854 94 $1_GREP_EXCLUDE_PATTERNS:=$$(call EscapeDollar, \
chegar@25854 95 $$(foreach src,$$($1_SRCS),$$(addprefix $$(src)/, \
chegar@25854 96 $$($1_EXCLUDES) $$($1_EXCLUDE_FILES))))
ihse@20363 97 # If there are a lot of include patterns, output to file to shorten command lines
ihse@20363 98 ifeq ($$(word 20,$$($1_GREP_EXCLUDE_PATTERNS)),)
erikj@28602 99 $1_GREP_EXCLUDES:=| ( $(GREP) -v $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_EXCLUDE_PATTERNS)) \
erikj@28602 100 || test "$$$$?" = "1" )
ihse@20363 101 else
erikj@32807 102 $1_GREP_EXCLUDE_OUTPUT = \
erikj@32807 103 $$(eval $$(call ListPathsSafely,$1_GREP_EXCLUDE_PATTERNS, \
erikj@32807 104 $$($1_BIN)/_the.$$($1_JARNAME)_exclude))
erikj@28602 105 $1_GREP_EXCLUDES:=| ( $(GREP) -v -f $$($1_BIN)/_the.$$($1_JARNAME)_exclude \
erikj@28602 106 || test "$$$$?" = "1" )
ohair@12258 107 endif
ihse@20363 108 endif
ihse@20363 109
ihse@20363 110 # Check if this jar needs to have its index generated.
ihse@20363 111 ifneq (,$$($1_JARINDEX))
ihse@20363 112 $1_JARINDEX = (cd $$(dir $$@) && $(JAR) -i $$(notdir $$@))
ihse@20363 113 else
ihse@20363 114 $1_JARINDEX = true
ihse@20363 115 endif
ihse@20363 116 # When this macro is run in the same makefile as the java compilation, dependencies are
ihse@20363 117 # transfered in make variables. When the macro is run in a different makefile than the
ihse@20363 118 # java compilation, the dependencies need to be found in the filesystem.
ihse@32720 119 $1_ORIG_DEPS := $$($1_DEPENDENCIES)
erikj@29312 120 ifeq ($$($1_DEPENDENCIES), )
ihse@27595 121 # Add all source roots to the find cache since we are likely going to run find
erikj@23170 122 # on these more than once. The cache will only be updated if necessary.
erikj@23170 123 $$(eval $$(call FillCacheFind, $$($1_FIND_LIST)))
erikj@29312 124 $1_DEPENDENCIES:=$$(filter $$(addprefix %,$$($1_SUFFIXES)), \
ihse@20363 125 $$(call CacheFind,$$($1_SRCS)))
ihse@20363 126 ifneq (,$$($1_GREP_INCLUDE_PATTERNS))
erikj@29312 127 $1_DEPENDENCIES:=$$(filter $$(addsuffix %,$$($1_GREP_INCLUDE_PATTERNS)),$$($1_DEPENDENCIES))
ohair@12258 128 endif
ihse@20363 129 ifneq (,$$($1_GREP_EXCLUDE_PATTERNS))
erikj@29312 130 $1_DEPENDENCIES:=$$(filter-out $$(addsuffix %,$$($1_GREP_EXCLUDE_PATTERNS)),$$($1_DEPENDENCIES))
ihse@20363 131 endif
chegar@25854 132 # Look for EXTRA_FILES in all SRCS dirs and as absolute paths.
erikj@29312 133 $1_DEPENDENCIES+=$$(wildcard $$(foreach src, $$($1_SRCS), \
chegar@25854 134 $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES))
ihse@20363 135 ifeq (,$$($1_SKIP_METAINF))
erikj@29312 136 $1_DEPENDENCIES+=$$(call CacheFind,$$(wildcard $$(addsuffix /META-INF,$$($1_SRCS))))
ihse@20363 137 endif
ihse@20363 138 endif
erikj@23169 139 # The dependency list should never be empty
erikj@29312 140 ifeq ($$(strip $$($1_DEPENDENCIES)), )
erikj@23169 141 $$(warning No dependencies found for $1)
erikj@23169 142 endif
ohair@12258 143
ihse@20363 144 # Utility macros, to make the shell script receipt somewhat easier to decipher.
ihse@20363 145
chegar@25854 146 # Capture extra files is the same for both CAPTURE_CONTENTS and SCAPTURE_CONTENTS so
chegar@25854 147 # only define it once to avoid duplication.
chegar@25854 148 # The list of extra files might be long, so need to use ListPathsSafely to print
chegar@25854 149 # them out to a separte file. Then process the contents of that file to rewrite
chegar@25854 150 # into -C <dir> <file> lines.
chegar@25854 151 # The EXTRA_FILES_RESOLVED varible must be set in the macro so that it's evaluated
chegar@25854 152 # in the recipe when the files are guaranteed to exist.
chegar@25854 153 $1_CAPTURE_EXTRA_FILES=\
erikj@32807 154 $$(eval $1_EXTRA_FILES_RESOLVED:=$$(call DoubleDollar, \
chegar@25854 155 $$(wildcard $$(foreach src, $$($1_SRCS), \
erikj@32807 156 $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES)))) \
chegar@25854 157 $$(if $$($1_EXTRA_FILES_RESOLVED), \
erikj@32807 158 $$(eval $$(call ListPathsSafely,$1_EXTRA_FILES_RESOLVED, \
erikj@32807 159 $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra)) \
chegar@25854 160 $(SED) $$(foreach src,$$($1_SRCS), -e 's|$$(src)/|-C $$(src) |g') \
chegar@25854 161 $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra \
chegar@25854 162 >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE))
chegar@25854 163
ihse@20363 164 # The capture contents macro finds all files (matching the patterns, typically
ihse@20363 165 # .class and .prp) that are newer than the jar-file, ie the new content to be put into the jar.
kvn@22467 166 # NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
chegar@25854 167 $1_CAPTURE_CONTENTS=\
chegar@25854 168 $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
chegar@25854 169 $$(foreach src,$$($1_SRCS), \
chegar@25854 170 $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) -a -newer $$@ $$($1_GREP_INCLUDES) \
chegar@25854 171 $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
chegar@25854 172 >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
chegar@25854 173 $$($1_CAPTURE_EXTRA_FILES)
chegar@25854 174
ihse@20363 175 # The capture metainf macro finds all files below the META-INF directory that are newer than the jar-file.
erikj@28602 176 # Find returns non zero if the META-INF dir does not exist, ignore this.
ihse@20363 177 ifeq (,$$($1_SKIP_METAINF))
chegar@25854 178 $1_CAPTURE_METAINF =$$(foreach src,$$($1_SRCS), \
erikj@28602 179 ( ( $(FIND) $$(src)/META-INF -type f -a -newer $$@ 2> /dev/null || true ) \
erikj@28602 180 | $(SED) 's|$$(src)/|-C $$(src) |g' >> \
chegar@25854 181 $$($1_BIN)/_the.$$($1_JARNAME)_contents ) $$(NEWLINE) )
ihse@20363 182 endif
ihse@20363 183 # The capture deletes macro finds all deleted files and concatenates them. The resulting file
ihse@20363 184 # tells us what to remove from the jar-file.
chegar@25854 185 $1_CAPTURE_DELETES=$$(foreach src,$$($1_SRCS),($(FIND) $$(src) -name _the.package.deleted -newer $$@ \
chegar@25854 186 -exec $(SED) 's|$$(src)||g' \{\} >> $$($1_DELETES_FILE) \;) $$(NEWLINE))
ihse@20363 187 # The update contents macro updates the jar file with the previously capture contents.
kvn@22467 188 # Use 'wc -w' to see if the contents file is empty.
chegar@25854 189 $1_UPDATE_CONTENTS=\
chegar@25854 190 if [ "`$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'`" -gt "0" ]; then \
chegar@25854 191 $(ECHO) " updating" `$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'` files && \
chegar@25854 192 $(JAR) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents; \
chegar@25854 193 fi $$(NEWLINE)
ihse@20363 194 # The s-variants of the above macros are used when the jar is created from scratch.
kvn@22467 195 # NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
chegar@25854 196 $1_SCAPTURE_CONTENTS=\
chegar@25854 197 $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
chegar@25854 198 $$(foreach src,$$($1_SRCS), \
chegar@25854 199 $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) $$($1_GREP_INCLUDES) \
chegar@25854 200 $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
chegar@25854 201 >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
chegar@25854 202 $$($1_CAPTURE_EXTRA_FILES)
ihse@20363 203
erikj@28602 204 # Find returns non zero if the META-INF dir does not exist, ignore this.
ihse@20363 205 ifeq (,$$($1_SKIP_METAINF))
ihse@20363 206 $1_SCAPTURE_METAINF=$$(foreach src,$$($1_SRCS), \
erikj@28602 207 ( ( $(FIND) $$(src)/META-INF -type f 2> /dev/null || true ) \
erikj@28602 208 | $(SED) 's|$$(src)/|-C $$(src) |g' >> \
chegar@25854 209 $$($1_BIN)/_the.$$($1_JARNAME)_contents) $$(NEWLINE) )
ihse@20363 210 endif
chegar@25854 211 $1_SUPDATE_CONTENTS=$(JAR) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)
ihse@20363 212
ihse@20363 213 # Use a slightly shorter name for logging, but with enough path to identify this jar.
mcimadamore@47290 214 $1_NAME:=$$(subst $$(OUTPUTDIR)/,,$$($1_JAR))
ihse@20363 215
ihse@20363 216 ifneq (,$$($1_CHECK_COMPRESS_JAR))
ihse@20363 217 $1_JAR_CREATE_OPTIONS := c0fm
ihse@20363 218 $1_JAR_UPDATE_OPTIONS := u0f
ihse@20363 219 ifeq ($(COMPRESS_JARS), true)
ihse@20363 220 $1_JAR_CREATE_OPTIONS := cfm
ihse@20363 221 $1_JAR_UPDATE_OPTIONS := uf
erikj@12801 222 endif
ihse@20363 223 else
ihse@20363 224 $1_JAR_CREATE_OPTIONS := cfm
ihse@20363 225 $1_JAR_UPDATE_OPTIONS := uf
ihse@20363 226 endif
erikj@12801 227
erikj@28600 228 # Include all variables of significance in the vardeps file
ihse@30534 229 $1_VARDEPS := $(JAR) $$($1_JAR_CREATE_OPTIONS) $$($1_MANIFEST) \
ihse@32720 230 $$($1_JARMAIN) $$($1_EXTRA_MANIFEST_ATTR) $$($1_ORIG_DEPS) $$($1_SRCS) \
ihse@32720 231 $$($1_INCLUDES) $$($1_EXCLUDES) $$($1_EXCLUDE_FILES) $$($1_EXTRA_FILES)
erikj@28600 232 $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, $$(dir $$($1_JAR))_the.$$($1_JARNAME).vardeps)
erikj@28600 233
ihse@20363 234 # Here is the rule that creates/updates the jar file.
erikj@29312 235 $$($1_JAR) : $$($1_DEPENDENCIES) $$($1_MANIFEST) $$($1_VARDEPS_FILE)
ohair@12258 236 $(MKDIR) -p $$($1_BIN)
erikj@15051 237 $$($1_GREP_INCLUDE_OUTPUT)
erikj@15051 238 $$($1_GREP_EXCLUDE_OUTPUT)
erikj@28600 239 # If the vardeps file is part of the newer prereq list, it means that
erikj@28600 240 # either the jar file does not exist, or we need to recreate it from
erikj@30095 241 # from scratch anyway since a simple update will not catch all the
erikj@28600 242 # potential changes.
erikj@28600 243 $$(if $$(filter $$($1_VARDEPS_FILE) $$($1_MANIFEST), $$?), \
erikj@28600 244 $$(if $$($1_MANIFEST), \
ihse@41460 245 $(CP) $$($1_MANIFEST) $$($1_MANIFEST_FILE) $$(NEWLINE) \
erikj@28600 246 , \
erikj@28600 247 $(RM) $$($1_MANIFEST_FILE) && $(TOUCH) $$($1_MANIFEST_FILE) $$(NEWLINE)) \
erikj@28600 248 $$(if $$($1_JARMAIN), \
erikj@28600 249 $(ECHO) "Main-Class: $$(strip $$($1_JARMAIN))" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \
erikj@28600 250 $$(if $$($1_EXTRA_MANIFEST_ATTR), \
erikj@28600 251 $(PRINTF) "$$($1_EXTRA_MANIFEST_ATTR)\n" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \
erikj@28600 252 $(ECHO) Creating $$($1_NAME) $$(NEWLINE) \
erikj@28600 253 $(JAR) $$($1_JAR_CREATE_OPTIONS) $$@ $$($1_MANIFEST_FILE) $$(NEWLINE) \
erikj@28600 254 $$($1_SCAPTURE_CONTENTS) \
erikj@28600 255 $$($1_SCAPTURE_METAINF) \
erikj@28600 256 $$($1_SUPDATE_CONTENTS) \
erikj@28600 257 $$($1_JARINDEX) && true \
ihse@20363 258 , \
ihse@20363 259 $(ECHO) Modifying $$($1_NAME) $$(NEWLINE) \
ihse@20363 260 $$($1_CAPTURE_CONTENTS) \
ihse@20363 261 $$($1_CAPTURE_METAINF) \
ihse@20363 262 $(RM) $$($1_DELETES_FILE) $$(NEWLINE) \
ihse@20363 263 $$($1_CAPTURE_DELETES) \
ihse@20363 264 $(CAT) $$($1_DELETES_FILE) > $$($1_DELETESS_FILE) $$(NEWLINE) \
ihse@20363 265 if [ -s $$($1_DELETESS_FILE) ]; then \
ihse@20363 266 $(ECHO) " deleting" `$(WC) -l $$($1_DELETESS_FILE) | $(AWK) '{ print $$$$1 }'` files && \
ihse@42136 267 $(ZIPEXE) -q -d $$@ `$(CAT) $$($1_DELETESS_FILE)` ; \
ihse@20363 268 fi $$(NEWLINE) \
ihse@20363 269 $$($1_UPDATE_CONTENTS) true $$(NEWLINE) \
ihse@20363 270 $$($1_JARINDEX) && true )
ohair@12258 271
erikj@22480 272 # Add jar to target list
erikj@22480 273 $1 += $$($1_JAR)
ohair@12258 274 endef
ohair@12258 275
ihse@33438 276 endif # _JAR_ARCHIVE_GMK