JarArchive.gmk revision 1726:426d96f4922f
1160814Ssimon# 2160814Ssimon# Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. 3160814Ssimon# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4160814Ssimon# 5160814Ssimon# This code is free software; you can redistribute it and/or modify it 6160814Ssimon# under the terms of the GNU General Public License version 2 only, as 7160814Ssimon# published by the Free Software Foundation. Oracle designates this 8160814Ssimon# particular file as subject to the "Classpath" exception as provided 9160814Ssimon# by Oracle in the LICENSE file that accompanied this code. 10280304Sjkim# 11160814Ssimon# This code is distributed in the hope that it will be useful, but WITHOUT 12160814Ssimon# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13160814Ssimon# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14160814Ssimon# version 2 for more details (a copy is included in the LICENSE file that 15160814Ssimon# accompanied this code). 16160814Ssimon# 17160814Ssimon# You should have received a copy of the GNU General Public License version 18160814Ssimon# 2 along with this work; if not, write to the Free Software Foundation, 19160814Ssimon# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20160814Ssimon# 21160814Ssimon# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22160814Ssimon# or visit www.oracle.com if you need additional information or have any 23160814Ssimon# questions. 24160814Ssimon# 25160814Ssimon 26160814Ssimonifndef _JAR_ARCHIVE_GMK 27160814Ssimon_JAR_ARCHIVE_GMK := 1 28160814Ssimon 29160814Ssimonifeq (,$(_MAKEBASE_GMK)) 30160814Ssimon $(error You must include MakeBase.gmk prior to including JarArchive.gmk) 31160814Ssimonendif 32160814Ssimon 33160814SsimonFALSE_FIND_PATTERN:=-name FILE_NAME_THAT_DOESNT_EXIST 34160814Ssimon 35160814Ssimon# Setup make rules for creating a jar archive. 36160814Ssimon# 37160814Ssimon# Parameter 1 is the name of the rule. This name is used as variable prefix, 38160814Ssimon# and the targets generated are listed in a variable by that name. 39160814Ssimon# 40160814Ssimon# Remaining parameters are named arguments. These include: 41160814Ssimon# DEPENDENCIES:=List of dependencies for the jar target. If left empty, 42160814Ssimon# dependencies are calculated automatically from the source files found. 43160814Ssimon# For this to work, the source files must exist when the makefile is 44160814Ssimon# parsed. 45160814Ssimon# SRCS:=List of directories in where to find files to add to archive 46160814Ssimon# SUFFIXES:=File suffixes to include in jar 47160814Ssimon# INCLUDES:=List of directories/packages in SRCS that should be included 48160814Ssimon# EXCLUDES:=List of directories/packages in SRCS that should be excluded 49160814Ssimon# EXCLUDE_FILES:=List of files in SRCS that should be excluded 50160814Ssimon# EXTRA_FILES:=List of files in SRCS that should be included regardless of suffix match. 51160814Ssimon# JAR:=Jar file to create 52160814Ssimon# MANIFEST:=Optional manifest file template. 53160814Ssimon# JARMAIN:=Optional main class to add to manifest 54160814Ssimon# JARINDEX:=true means generate the index in the jar file. 55160814Ssimon# SKIP_METAINF:=Set to prevent contents of an META-INF directory to be automatically 56160814Ssimon# added to the archive. 57160814Ssimon# EXTRA_MANIFEST_ATTR:=Extra attribute to add to manifest. 58160814Ssimon# CHECK_COMPRESS_JAR Check the COMPRESS_JAR variable 59160814SsimonSetupJarArchive = $(NamedParamsMacroTemplate) 60160814Ssimondefine SetupJarArchiveBody 61160814Ssimon 62160814Ssimon $1_JARMAIN:=$(strip $$($1_JARMAIN)) 63160814Ssimon $1_JARNAME:=$$(notdir $$($1_JAR)) 64160814Ssimon $1_MANIFEST_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_manifest 65280304Sjkim $1_DELETESS_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletess 66160814Ssimon $1_DELETES_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletes 67160814Ssimon $1_BIN:=$$(dir $$($1_JAR)) 68160814Ssimon 69160814Ssimon ifeq (,$$($1_SUFFIXES)) 70280304Sjkim # No suffix was set, default to classes. 71160814Ssimon $1_SUFFIXES:=.class 72280304Sjkim endif 73280304Sjkim # Convert suffixes to a find expression 74280304Sjkim $1_FIND_PATTERNS:=$(FALSE_FIND_PATTERN) $$(patsubst %,$(SPACE)-o$(SPACE)-name$(SPACE)$(DQUOTE)*%$(DQUOTE),$$($1_SUFFIXES)) 75280304Sjkim # On windows, a lot of includes/excludes risk making the command line too long, so 76280304Sjkim # writing the grep patterns to files. 77160814Ssimon # Grep returns 1 if nothing is matched. Do not fail the build for this. 78280304Sjkim ifneq (,$$($1_INCLUDES)) 79160814Ssimon $1_GREP_INCLUDE_PATTERNS:=$$(call EscapeDollar, \ 80160814Ssimon $$(foreach src,$$($1_SRCS), $$(addprefix $$(src)/,$$($1_INCLUDES)))) 81160814Ssimon # If there are a lot of include patterns, output to file to shorten command lines 82160814Ssimon ifeq ($$(word 20,$$($1_GREP_INCLUDE_PATTERNS)),) 83280304Sjkim $1_GREP_INCLUDES:=| ( $(GREP) $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_INCLUDE_PATTERNS)) \ 84280304Sjkim || test "$$$$?" = "1" ) 85160814Ssimon else 86160814Ssimon $1_GREP_INCLUDE_OUTPUT = \ 87280304Sjkim $$(eval $$(call ListPathsSafely,$1_GREP_INCLUDE_PATTERNS, \ 88280304Sjkim $$($1_BIN)/_the.$$($1_JARNAME)_include)) 89160814Ssimon $1_GREP_INCLUDES:=| ( $(GREP) -f $$($1_BIN)/_the.$$($1_JARNAME)_include \ 90280304Sjkim || test "$$$$?" = "1" ) 91160814Ssimon endif 92280304Sjkim endif 93160814Ssimon ifneq (,$$($1_EXCLUDES)$$($1_EXCLUDE_FILES)) 94280304Sjkim $1_GREP_EXCLUDE_PATTERNS:=$$(call EscapeDollar, \ 95280304Sjkim $$(foreach src,$$($1_SRCS),$$(addprefix $$(src)/, \ 96280304Sjkim $$($1_EXCLUDES) $$($1_EXCLUDE_FILES)))) 97280304Sjkim # If there are a lot of include patterns, output to file to shorten command lines 98280304Sjkim ifeq ($$(word 20,$$($1_GREP_EXCLUDE_PATTERNS)),) 99160814Ssimon $1_GREP_EXCLUDES:=| ( $(GREP) -v $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_EXCLUDE_PATTERNS)) \ 100160814Ssimon || test "$$$$?" = "1" ) 101280304Sjkim else 102280304Sjkim $1_GREP_EXCLUDE_OUTPUT = \ 103280304Sjkim $$(eval $$(call ListPathsSafely,$1_GREP_EXCLUDE_PATTERNS, \ 104280304Sjkim $$($1_BIN)/_the.$$($1_JARNAME)_exclude)) 105280304Sjkim $1_GREP_EXCLUDES:=| ( $(GREP) -v -f $$($1_BIN)/_the.$$($1_JARNAME)_exclude \ 106280304Sjkim || test "$$$$?" = "1" ) 107280304Sjkim endif 108160814Ssimon endif 109280304Sjkim 110280304Sjkim # Check if this jar needs to have its index generated. 111280304Sjkim ifneq (,$$($1_JARINDEX)) 112280304Sjkim $1_JARINDEX = (cd $$(dir $$@) && $(JAR) -i $$(notdir $$@)) 113280304Sjkim else 114280304Sjkim $1_JARINDEX = true 115160814Ssimon endif 116160814Ssimon # When this macro is run in the same makefile as the java compilation, dependencies are 117280304Sjkim # transfered in make variables. When the macro is run in a different makefile than the 118280304Sjkim # java compilation, the dependencies need to be found in the filesystem. 119160814Ssimon $1_ORIG_DEPS := $$($1_DEPENDENCIES) 120160814Ssimon ifeq ($$($1_DEPENDENCIES), ) 121280304Sjkim # Add all source roots to the find cache since we are likely going to run find 122160814Ssimon # on these more than once. The cache will only be updated if necessary. 123280304Sjkim $$(eval $$(call FillCacheFind, $$($1_FIND_LIST))) 124280304Sjkim $1_DEPENDENCIES:=$$(filter $$(addprefix %,$$($1_SUFFIXES)), \ 125160814Ssimon $$(call CacheFind,$$($1_SRCS))) 126160814Ssimon ifneq (,$$($1_GREP_INCLUDE_PATTERNS)) 127280304Sjkim $1_DEPENDENCIES:=$$(filter $$(addsuffix %,$$($1_GREP_INCLUDE_PATTERNS)),$$($1_DEPENDENCIES)) 128280304Sjkim endif 129160814Ssimon ifneq (,$$($1_GREP_EXCLUDE_PATTERNS)) 130160814Ssimon $1_DEPENDENCIES:=$$(filter-out $$(addsuffix %,$$($1_GREP_EXCLUDE_PATTERNS)),$$($1_DEPENDENCIES)) 131160814Ssimon endif 132160814Ssimon # Look for EXTRA_FILES in all SRCS dirs and as absolute paths. 133160814Ssimon $1_DEPENDENCIES+=$$(wildcard $$(foreach src, $$($1_SRCS), \ 134160814Ssimon $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES)) 135280304Sjkim ifeq (,$$($1_SKIP_METAINF)) 136160814Ssimon $1_DEPENDENCIES+=$$(call CacheFind,$$(wildcard $$(addsuffix /META-INF,$$($1_SRCS)))) 137160814Ssimon endif 138160814Ssimon endif 139280304Sjkim # The dependency list should never be empty 140160814Ssimon ifeq ($$(strip $$($1_DEPENDENCIES)), ) 141160814Ssimon $$(warning No dependencies found for $1) 142280304Sjkim endif 143280304Sjkim 144280304Sjkim # Utility macros, to make the shell script receipt somewhat easier to decipher. 145280304Sjkim 146280304Sjkim # Capture extra files is the same for both CAPTURE_CONTENTS and SCAPTURE_CONTENTS so 147280304Sjkim # only define it once to avoid duplication. 148160814Ssimon # The list of extra files might be long, so need to use ListPathsSafely to print 149280304Sjkim # them out to a separte file. Then process the contents of that file to rewrite 150280304Sjkim # into -C <dir> <file> lines. 151280304Sjkim # The EXTRA_FILES_RESOLVED varible must be set in the macro so that it's evaluated 152280304Sjkim # in the recipe when the files are guaranteed to exist. 153280304Sjkim $1_CAPTURE_EXTRA_FILES=\ 154280304Sjkim $$(eval $1_EXTRA_FILES_RESOLVED:=$$(call DoubleDollar, \ 155160814Ssimon $$(wildcard $$(foreach src, $$($1_SRCS), \ 156280304Sjkim $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES)))) \ 157280304Sjkim $$(if $$($1_EXTRA_FILES_RESOLVED), \ 158280304Sjkim $$(eval $$(call ListPathsSafely,$1_EXTRA_FILES_RESOLVED, \ 159280304Sjkim $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra)) \ 160160814Ssimon $(SED) $$(foreach src,$$($1_SRCS), -e 's|$$(src)/|-C $$(src) |g') \ 161280304Sjkim $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra \ 162280304Sjkim >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) 163280304Sjkim 164280304Sjkim # The capture contents macro finds all files (matching the patterns, typically 165280304Sjkim # .class and .prp) that are newer than the jar-file, ie the new content to be put into the jar. 166280304Sjkim # NOTICE: please leave the parentheses space separated otherwise the AIX build will break! 167280304Sjkim $1_CAPTURE_CONTENTS=\ 168280304Sjkim $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \ 169280304Sjkim $$(foreach src,$$($1_SRCS), \ 170280304Sjkim $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) -a -newer $$@ $$($1_GREP_INCLUDES) \ 171280304Sjkim $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \ 172160814Ssimon >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \ 173280304Sjkim $$($1_CAPTURE_EXTRA_FILES) 174160814Ssimon 175160814Ssimon # The capture metainf macro finds all files below the META-INF directory that are newer than the jar-file. 176160814Ssimon # Find returns non zero if the META-INF dir does not exist, ignore this. 177280304Sjkim ifeq (,$$($1_SKIP_METAINF)) 178160814Ssimon $1_CAPTURE_METAINF =$$(foreach src,$$($1_SRCS), \ 179280304Sjkim ( ( $(FIND) $$(src)/META-INF -type f -a -newer $$@ 2> /dev/null || true ) \ 180280304Sjkim | $(SED) 's|$$(src)/|-C $$(src) |g' >> \ 181280304Sjkim $$($1_BIN)/_the.$$($1_JARNAME)_contents ) $$(NEWLINE) ) 182280304Sjkim endif 183280304Sjkim # The capture deletes macro finds all deleted files and concatenates them. The resulting file 184280304Sjkim # tells us what to remove from the jar-file. 185280304Sjkim $1_CAPTURE_DELETES=$$(foreach src,$$($1_SRCS),($(FIND) $$(src) -name _the.package.deleted -newer $$@ \ 186280304Sjkim -exec $(SED) 's|$$(src)||g' \{\} >> $$($1_DELETES_FILE) \;) $$(NEWLINE)) 187160814Ssimon # The update contents macro updates the jar file with the previously capture contents. 188280304Sjkim # Use 'wc -w' to see if the contents file is empty. 189280304Sjkim $1_UPDATE_CONTENTS=\ 190280304Sjkim if [ "`$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'`" -gt "0" ]; then \ 191280304Sjkim $(ECHO) " updating" `$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'` files && \ 192280304Sjkim $(JAR) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents; \ 193280304Sjkim fi $$(NEWLINE) 194280304Sjkim # The s-variants of the above macros are used when the jar is created from scratch. 195280304Sjkim # NOTICE: please leave the parentheses space separated otherwise the AIX build will break! 196280304Sjkim $1_SCAPTURE_CONTENTS=\ 197280304Sjkim $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \ 198280304Sjkim $$(foreach src,$$($1_SRCS), \ 199280304Sjkim $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) $$($1_GREP_INCLUDES) \ 200280304Sjkim $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \ 201280304Sjkim >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \ 202280304Sjkim $$($1_CAPTURE_EXTRA_FILES) 203280304Sjkim 204280304Sjkim # Find returns non zero if the META-INF dir does not exist, ignore this. 205280304Sjkim ifeq (,$$($1_SKIP_METAINF)) 206160814Ssimon $1_SCAPTURE_METAINF=$$(foreach src,$$($1_SRCS), \ 207280304Sjkim ( ( $(FIND) $$(src)/META-INF -type f 2> /dev/null || true ) \ 208280304Sjkim | $(SED) 's|$$(src)/|-C $$(src) |g' >> \ 209280304Sjkim $$($1_BIN)/_the.$$($1_JARNAME)_contents) $$(NEWLINE) ) 210280304Sjkim endif 211280304Sjkim $1_SUPDATE_CONTENTS=$(JAR) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) 212280304Sjkim 213280304Sjkim # Use a slightly shorter name for logging, but with enough path to identify this jar. 214280304Sjkim $1_NAME:=$$(subst $$(OUTPUT_ROOT)/,,$$($1_JAR)) 215280304Sjkim 216160814Ssimon ifneq (,$$($1_CHECK_COMPRESS_JAR)) 217160814Ssimon $1_JAR_CREATE_OPTIONS := c0fm 218280304Sjkim $1_JAR_UPDATE_OPTIONS := u0f 219280304Sjkim ifeq ($(COMPRESS_JARS), true) 220280304Sjkim $1_JAR_CREATE_OPTIONS := cfm 221160814Ssimon $1_JAR_UPDATE_OPTIONS := uf 222160814Ssimon endif 223280304Sjkim else 224160814Ssimon $1_JAR_CREATE_OPTIONS := cfm 225160814Ssimon $1_JAR_UPDATE_OPTIONS := uf 226280304Sjkim endif 227160814Ssimon 228280304Sjkim # Include all variables of significance in the vardeps file 229280304Sjkim $1_VARDEPS := $(JAR) $$($1_JAR_CREATE_OPTIONS) $$($1_MANIFEST) \ 230280304Sjkim $$($1_JARMAIN) $$($1_EXTRA_MANIFEST_ATTR) $$($1_ORIG_DEPS) $$($1_SRCS) \ 231280304Sjkim $$($1_INCLUDES) $$($1_EXCLUDES) $$($1_EXCLUDE_FILES) $$($1_EXTRA_FILES) 232280304Sjkim $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, $$(dir $$($1_JAR))_the.$$($1_JARNAME).vardeps) 233280304Sjkim 234280304Sjkim # Here is the rule that creates/updates the jar file. 235280304Sjkim $$($1_JAR) : $$($1_DEPENDENCIES) $$($1_MANIFEST) $$($1_VARDEPS_FILE) 236280304Sjkim $(MKDIR) -p $$($1_BIN) 237280304Sjkim $$($1_GREP_INCLUDE_OUTPUT) 238280304Sjkim $$($1_GREP_EXCLUDE_OUTPUT) 239280304Sjkim # If the vardeps file is part of the newer prereq list, it means that 240280304Sjkim # either the jar file does not exist, or we need to recreate it from 241280304Sjkim # from scratch anyway since a simple update will not catch all the 242280304Sjkim # potential changes. 243280304Sjkim $$(if $$(filter $$($1_VARDEPS_FILE) $$($1_MANIFEST), $$?), \ 244280304Sjkim $$(if $$($1_MANIFEST), \ 245280304Sjkim $(SED) -e '$(DOLLAR)$(DOLLAR)a\' $$($1_MANIFEST) > $$($1_MANIFEST_FILE) $$(NEWLINE) \ 246280304Sjkim , \ 247280304Sjkim $(RM) $$($1_MANIFEST_FILE) && $(TOUCH) $$($1_MANIFEST_FILE) $$(NEWLINE)) \ 248280304Sjkim $$(if $$($1_JARMAIN), \ 249160814Ssimon $(ECHO) "Main-Class: $$(strip $$($1_JARMAIN))" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \ 250280304Sjkim $$(if $$($1_EXTRA_MANIFEST_ATTR), \ 251160814Ssimon $(PRINTF) "$$($1_EXTRA_MANIFEST_ATTR)\n" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \ 252280304Sjkim $(ECHO) Creating $$($1_NAME) $$(NEWLINE) \ 253280304Sjkim $(JAR) $$($1_JAR_CREATE_OPTIONS) $$@ $$($1_MANIFEST_FILE) $$(NEWLINE) \ 254280304Sjkim $$($1_SCAPTURE_CONTENTS) \ 255280304Sjkim $$($1_SCAPTURE_METAINF) \ 256280304Sjkim $$($1_SUPDATE_CONTENTS) \ 257280304Sjkim $$($1_JARINDEX) && true \ 258280304Sjkim , \ 259280304Sjkim $(ECHO) Modifying $$($1_NAME) $$(NEWLINE) \ 260280304Sjkim $$($1_CAPTURE_CONTENTS) \ 261280304Sjkim $$($1_CAPTURE_METAINF) \ 262160814Ssimon $(RM) $$($1_DELETES_FILE) $$(NEWLINE) \ 263160814Ssimon $$($1_CAPTURE_DELETES) \ 264280304Sjkim $(CAT) $$($1_DELETES_FILE) > $$($1_DELETESS_FILE) $$(NEWLINE) \ 265280304Sjkim if [ -s $$($1_DELETESS_FILE) ]; then \ 266280304Sjkim $(ECHO) " deleting" `$(WC) -l $$($1_DELETESS_FILE) | $(AWK) '{ print $$$$1 }'` files && \ 267280304Sjkim $(ZIP) -q -d $$@ `$(CAT) $$($1_DELETESS_FILE)` ; \ 268280304Sjkim fi $$(NEWLINE) \ 269280304Sjkim $$($1_UPDATE_CONTENTS) true $$(NEWLINE) \ 270280304Sjkim $$($1_JARINDEX) && true ) 271280304Sjkim 272280304Sjkim # Add jar to target list 273160814Ssimon $1 += $$($1_JAR) 274160814Ssimonendef 275280304Sjkim 276280304Sjkimendif # _JAR_ARCHIVE_GMK 277280304Sjkim