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