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