annotate make/common/JarArchive.gmk @ 48426:1c1118deeedb

Automatic merge with default
author mcimadamore
date Thu, 07 Dec 2017 22:05:06 +0100
parents b90a1b5b8142 a358ebcfacfb
children c5905f5d6d45
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
erikj@48285 59 # JAR_CMD:=Optionally override the jar command to use when creating the archive.
ihse@33438 60 SetupJarArchive = $(NamedParamsMacroTemplate)
ihse@33438 61 define SetupJarArchiveBody
ohair@12258 62
ihse@20363 63 $1_JARMAIN:=$(strip $$($1_JARMAIN))
ihse@20363 64 $1_JARNAME:=$$(notdir $$($1_JAR))
ihse@20363 65 $1_MANIFEST_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_manifest
ihse@20363 66 $1_DELETESS_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletess
ihse@20363 67 $1_DELETES_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletes
ihse@20363 68 $1_BIN:=$$(dir $$($1_JAR))
erikj@48285 69 $$(call SetIfEmpty, $1_JAR_CMD, $$(JAR))
erikj@12801 70
ihse@20363 71 ifeq (,$$($1_SUFFIXES))
ihse@20363 72 # No suffix was set, default to classes.
ihse@20363 73 $1_SUFFIXES:=.class
ihse@20363 74 endif
ihse@20363 75 # Convert suffixes to a find expression
ihse@20363 76 $1_FIND_PATTERNS:=$(FALSE_FIND_PATTERN) $$(patsubst %,$(SPACE)-o$(SPACE)-name$(SPACE)$(DQUOTE)*%$(DQUOTE),$$($1_SUFFIXES))
ihse@20363 77 # On windows, a lot of includes/excludes risk making the command line too long, so
ihse@20363 78 # writing the grep patterns to files.
erikj@28602 79 # Grep returns 1 if nothing is matched. Do not fail the build for this.
ihse@20363 80 ifneq (,$$($1_INCLUDES))
chegar@25854 81 $1_GREP_INCLUDE_PATTERNS:=$$(call EscapeDollar, \
chegar@25854 82 $$(foreach src,$$($1_SRCS), $$(addprefix $$(src)/,$$($1_INCLUDES))))
ihse@20363 83 # If there are a lot of include patterns, output to file to shorten command lines
ihse@20363 84 ifeq ($$(word 20,$$($1_GREP_INCLUDE_PATTERNS)),)
erikj@28602 85 $1_GREP_INCLUDES:=| ( $(GREP) $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_INCLUDE_PATTERNS)) \
erikj@28602 86 || test "$$$$?" = "1" )
ihse@20363 87 else
erikj@32807 88 $1_GREP_INCLUDE_OUTPUT = \
erikj@32807 89 $$(eval $$(call ListPathsSafely,$1_GREP_INCLUDE_PATTERNS, \
erikj@32807 90 $$($1_BIN)/_the.$$($1_JARNAME)_include))
erikj@28602 91 $1_GREP_INCLUDES:=| ( $(GREP) -f $$($1_BIN)/_the.$$($1_JARNAME)_include \
erikj@28602 92 || test "$$$$?" = "1" )
ohair@12258 93 endif
ihse@20363 94 endif
ihse@20363 95 ifneq (,$$($1_EXCLUDES)$$($1_EXCLUDE_FILES))
chegar@25854 96 $1_GREP_EXCLUDE_PATTERNS:=$$(call EscapeDollar, \
chegar@25854 97 $$(foreach src,$$($1_SRCS),$$(addprefix $$(src)/, \
chegar@25854 98 $$($1_EXCLUDES) $$($1_EXCLUDE_FILES))))
ihse@20363 99 # If there are a lot of include patterns, output to file to shorten command lines
ihse@20363 100 ifeq ($$(word 20,$$($1_GREP_EXCLUDE_PATTERNS)),)
erikj@28602 101 $1_GREP_EXCLUDES:=| ( $(GREP) -v $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_EXCLUDE_PATTERNS)) \
erikj@28602 102 || test "$$$$?" = "1" )
ihse@20363 103 else
erikj@32807 104 $1_GREP_EXCLUDE_OUTPUT = \
erikj@32807 105 $$(eval $$(call ListPathsSafely,$1_GREP_EXCLUDE_PATTERNS, \
erikj@32807 106 $$($1_BIN)/_the.$$($1_JARNAME)_exclude))
erikj@28602 107 $1_GREP_EXCLUDES:=| ( $(GREP) -v -f $$($1_BIN)/_the.$$($1_JARNAME)_exclude \
erikj@28602 108 || test "$$$$?" = "1" )
ohair@12258 109 endif
ihse@20363 110 endif
ihse@20363 111
ihse@20363 112 # Check if this jar needs to have its index generated.
ihse@20363 113 ifneq (,$$($1_JARINDEX))
erikj@48285 114 $1_JARINDEX = (cd $$(dir $$@) && $$($1_JAR_CMD) -i $$(notdir $$@))
ihse@20363 115 else
ihse@20363 116 $1_JARINDEX = true
ihse@20363 117 endif
ihse@20363 118 # When this macro is run in the same makefile as the java compilation, dependencies are
ihse@20363 119 # transfered in make variables. When the macro is run in a different makefile than the
ihse@20363 120 # java compilation, the dependencies need to be found in the filesystem.
ihse@32720 121 $1_ORIG_DEPS := $$($1_DEPENDENCIES)
erikj@29312 122 ifeq ($$($1_DEPENDENCIES), )
ihse@27595 123 # Add all source roots to the find cache since we are likely going to run find
erikj@23170 124 # on these more than once. The cache will only be updated if necessary.
erikj@23170 125 $$(eval $$(call FillCacheFind, $$($1_FIND_LIST)))
erikj@29312 126 $1_DEPENDENCIES:=$$(filter $$(addprefix %,$$($1_SUFFIXES)), \
ihse@20363 127 $$(call CacheFind,$$($1_SRCS)))
ihse@20363 128 ifneq (,$$($1_GREP_INCLUDE_PATTERNS))
erikj@29312 129 $1_DEPENDENCIES:=$$(filter $$(addsuffix %,$$($1_GREP_INCLUDE_PATTERNS)),$$($1_DEPENDENCIES))
ohair@12258 130 endif
ihse@20363 131 ifneq (,$$($1_GREP_EXCLUDE_PATTERNS))
erikj@29312 132 $1_DEPENDENCIES:=$$(filter-out $$(addsuffix %,$$($1_GREP_EXCLUDE_PATTERNS)),$$($1_DEPENDENCIES))
ihse@20363 133 endif
chegar@25854 134 # Look for EXTRA_FILES in all SRCS dirs and as absolute paths.
erikj@29312 135 $1_DEPENDENCIES+=$$(wildcard $$(foreach src, $$($1_SRCS), \
chegar@25854 136 $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES))
ihse@20363 137 ifeq (,$$($1_SKIP_METAINF))
erikj@29312 138 $1_DEPENDENCIES+=$$(call CacheFind,$$(wildcard $$(addsuffix /META-INF,$$($1_SRCS))))
ihse@20363 139 endif
ihse@20363 140 endif
erikj@23169 141 # The dependency list should never be empty
erikj@29312 142 ifeq ($$(strip $$($1_DEPENDENCIES)), )
erikj@23169 143 $$(warning No dependencies found for $1)
erikj@23169 144 endif
ohair@12258 145
ihse@20363 146 # Utility macros, to make the shell script receipt somewhat easier to decipher.
ihse@20363 147
chegar@25854 148 # Capture extra files is the same for both CAPTURE_CONTENTS and SCAPTURE_CONTENTS so
chegar@25854 149 # only define it once to avoid duplication.
chegar@25854 150 # The list of extra files might be long, so need to use ListPathsSafely to print
chegar@25854 151 # them out to a separte file. Then process the contents of that file to rewrite
chegar@25854 152 # into -C <dir> <file> lines.
chegar@25854 153 # The EXTRA_FILES_RESOLVED varible must be set in the macro so that it's evaluated
chegar@25854 154 # in the recipe when the files are guaranteed to exist.
chegar@25854 155 $1_CAPTURE_EXTRA_FILES=\
erikj@32807 156 $$(eval $1_EXTRA_FILES_RESOLVED:=$$(call DoubleDollar, \
chegar@25854 157 $$(wildcard $$(foreach src, $$($1_SRCS), \
erikj@32807 158 $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES)))) \
chegar@25854 159 $$(if $$($1_EXTRA_FILES_RESOLVED), \
erikj@32807 160 $$(eval $$(call ListPathsSafely,$1_EXTRA_FILES_RESOLVED, \
erikj@32807 161 $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra)) \
chegar@25854 162 $(SED) $$(foreach src,$$($1_SRCS), -e 's|$$(src)/|-C $$(src) |g') \
chegar@25854 163 $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra \
chegar@25854 164 >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE))
chegar@25854 165
ihse@20363 166 # The capture contents macro finds all files (matching the patterns, typically
ihse@20363 167 # .class and .prp) that are newer than the jar-file, ie the new content to be put into the jar.
kvn@22467 168 # NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
chegar@25854 169 $1_CAPTURE_CONTENTS=\
chegar@25854 170 $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
chegar@25854 171 $$(foreach src,$$($1_SRCS), \
chegar@25854 172 $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) -a -newer $$@ $$($1_GREP_INCLUDES) \
chegar@25854 173 $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
chegar@25854 174 >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
chegar@25854 175 $$($1_CAPTURE_EXTRA_FILES)
chegar@25854 176
ihse@20363 177 # The capture metainf macro finds all files below the META-INF directory that are newer than the jar-file.
erikj@28602 178 # Find returns non zero if the META-INF dir does not exist, ignore this.
ihse@20363 179 ifeq (,$$($1_SKIP_METAINF))
chegar@25854 180 $1_CAPTURE_METAINF =$$(foreach src,$$($1_SRCS), \
erikj@28602 181 ( ( $(FIND) $$(src)/META-INF -type f -a -newer $$@ 2> /dev/null || true ) \
erikj@28602 182 | $(SED) 's|$$(src)/|-C $$(src) |g' >> \
chegar@25854 183 $$($1_BIN)/_the.$$($1_JARNAME)_contents ) $$(NEWLINE) )
ihse@20363 184 endif
ihse@20363 185 # The capture deletes macro finds all deleted files and concatenates them. The resulting file
ihse@20363 186 # tells us what to remove from the jar-file.
chegar@25854 187 $1_CAPTURE_DELETES=$$(foreach src,$$($1_SRCS),($(FIND) $$(src) -name _the.package.deleted -newer $$@ \
chegar@25854 188 -exec $(SED) 's|$$(src)||g' \{\} >> $$($1_DELETES_FILE) \;) $$(NEWLINE))
ihse@20363 189 # The update contents macro updates the jar file with the previously capture contents.
kvn@22467 190 # Use 'wc -w' to see if the contents file is empty.
chegar@25854 191 $1_UPDATE_CONTENTS=\
chegar@25854 192 if [ "`$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'`" -gt "0" ]; then \
chegar@25854 193 $(ECHO) " updating" `$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'` files && \
erikj@48285 194 $$($1_JAR_CMD) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents; \
chegar@25854 195 fi $$(NEWLINE)
ihse@20363 196 # The s-variants of the above macros are used when the jar is created from scratch.
kvn@22467 197 # NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
chegar@25854 198 $1_SCAPTURE_CONTENTS=\
chegar@25854 199 $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
chegar@25854 200 $$(foreach src,$$($1_SRCS), \
chegar@25854 201 $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) $$($1_GREP_INCLUDES) \
chegar@25854 202 $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
chegar@25854 203 >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
chegar@25854 204 $$($1_CAPTURE_EXTRA_FILES)
ihse@20363 205
erikj@28602 206 # Find returns non zero if the META-INF dir does not exist, ignore this.
ihse@20363 207 ifeq (,$$($1_SKIP_METAINF))
ihse@20363 208 $1_SCAPTURE_METAINF=$$(foreach src,$$($1_SRCS), \
erikj@28602 209 ( ( $(FIND) $$(src)/META-INF -type f 2> /dev/null || true ) \
erikj@28602 210 | $(SED) 's|$$(src)/|-C $$(src) |g' >> \
chegar@25854 211 $$($1_BIN)/_the.$$($1_JARNAME)_contents) $$(NEWLINE) )
ihse@20363 212 endif
erikj@48285 213 $1_SUPDATE_CONTENTS=$$($1_JAR_CMD) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)
ihse@20363 214
ihse@20363 215 # Use a slightly shorter name for logging, but with enough path to identify this jar.
ihse@47275 216 $1_NAME:=$$(subst $$(OUTPUTDIR)/,,$$($1_JAR))
ihse@20363 217
ihse@20363 218 ifneq (,$$($1_CHECK_COMPRESS_JAR))
ihse@20363 219 $1_JAR_CREATE_OPTIONS := c0fm
ihse@20363 220 $1_JAR_UPDATE_OPTIONS := u0f
ihse@20363 221 ifeq ($(COMPRESS_JARS), true)
ihse@20363 222 $1_JAR_CREATE_OPTIONS := cfm
ihse@20363 223 $1_JAR_UPDATE_OPTIONS := uf
erikj@12801 224 endif
ihse@20363 225 else
ihse@20363 226 $1_JAR_CREATE_OPTIONS := cfm
ihse@20363 227 $1_JAR_UPDATE_OPTIONS := uf
ihse@20363 228 endif
erikj@12801 229
erikj@28600 230 # Include all variables of significance in the vardeps file
erikj@48285 231 $1_VARDEPS := $$($1_JAR_CMD) $$($1_JAR_CREATE_OPTIONS) $$($1_MANIFEST) \
ihse@32720 232 $$($1_JARMAIN) $$($1_EXTRA_MANIFEST_ATTR) $$($1_ORIG_DEPS) $$($1_SRCS) \
ihse@32720 233 $$($1_INCLUDES) $$($1_EXCLUDES) $$($1_EXCLUDE_FILES) $$($1_EXTRA_FILES)
erikj@28600 234 $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, $$(dir $$($1_JAR))_the.$$($1_JARNAME).vardeps)
erikj@28600 235
ihse@20363 236 # Here is the rule that creates/updates the jar file.
erikj@29312 237 $$($1_JAR) : $$($1_DEPENDENCIES) $$($1_MANIFEST) $$($1_VARDEPS_FILE)
ohair@12258 238 $(MKDIR) -p $$($1_BIN)
erikj@15051 239 $$($1_GREP_INCLUDE_OUTPUT)
erikj@15051 240 $$($1_GREP_EXCLUDE_OUTPUT)
erikj@28600 241 # If the vardeps file is part of the newer prereq list, it means that
erikj@28600 242 # either the jar file does not exist, or we need to recreate it from
erikj@30095 243 # from scratch anyway since a simple update will not catch all the
erikj@28600 244 # potential changes.
erikj@28600 245 $$(if $$(filter $$($1_VARDEPS_FILE) $$($1_MANIFEST), $$?), \
erikj@28600 246 $$(if $$($1_MANIFEST), \
ihse@41460 247 $(CP) $$($1_MANIFEST) $$($1_MANIFEST_FILE) $$(NEWLINE) \
erikj@28600 248 , \
erikj@28600 249 $(RM) $$($1_MANIFEST_FILE) && $(TOUCH) $$($1_MANIFEST_FILE) $$(NEWLINE)) \
erikj@28600 250 $$(if $$($1_JARMAIN), \
erikj@28600 251 $(ECHO) "Main-Class: $$(strip $$($1_JARMAIN))" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \
erikj@28600 252 $$(if $$($1_EXTRA_MANIFEST_ATTR), \
erikj@28600 253 $(PRINTF) "$$($1_EXTRA_MANIFEST_ATTR)\n" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \
erikj@28600 254 $(ECHO) Creating $$($1_NAME) $$(NEWLINE) \
erikj@48285 255 $$($1_JAR_CMD) $$($1_JAR_CREATE_OPTIONS) $$@ $$($1_MANIFEST_FILE) $$(NEWLINE) \
erikj@28600 256 $$($1_SCAPTURE_CONTENTS) \
erikj@28600 257 $$($1_SCAPTURE_METAINF) \
erikj@28600 258 $$($1_SUPDATE_CONTENTS) \
erikj@28600 259 $$($1_JARINDEX) && true \
ihse@20363 260 , \
ihse@20363 261 $(ECHO) Modifying $$($1_NAME) $$(NEWLINE) \
ihse@20363 262 $$($1_CAPTURE_CONTENTS) \
ihse@20363 263 $$($1_CAPTURE_METAINF) \
ihse@20363 264 $(RM) $$($1_DELETES_FILE) $$(NEWLINE) \
ihse@20363 265 $$($1_CAPTURE_DELETES) \
ihse@20363 266 $(CAT) $$($1_DELETES_FILE) > $$($1_DELETESS_FILE) $$(NEWLINE) \
ihse@20363 267 if [ -s $$($1_DELETESS_FILE) ]; then \
ihse@20363 268 $(ECHO) " deleting" `$(WC) -l $$($1_DELETESS_FILE) | $(AWK) '{ print $$$$1 }'` files && \
ihse@42136 269 $(ZIPEXE) -q -d $$@ `$(CAT) $$($1_DELETESS_FILE)` ; \
ihse@20363 270 fi $$(NEWLINE) \
ihse@20363 271 $$($1_UPDATE_CONTENTS) true $$(NEWLINE) \
ihse@20363 272 $$($1_JARINDEX) && true )
ohair@12258 273
erikj@22480 274 # Add jar to target list
erikj@22480 275 $1 += $$($1_JAR)
ohair@12258 276 endef
ohair@12258 277
ihse@33438 278 endif # _JAR_ARCHIVE_GMK