# # Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License version 2 only, as # published by the Free Software Foundation. Oracle designates this # particular file as subject to the "Classpath" exception as provided # by Oracle in the LICENSE file that accompanied this code. # # This code is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # version 2 for more details (a copy is included in the LICENSE file that # accompanied this code). # # You should have received a copy of the GNU General Public License version # 2 along with this work; if not, write to the Free Software Foundation, # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. # # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA # or visit www.oracle.com if you need additional information or have any # questions. # ifndef _JAR_ARCHIVE_GMK _JAR_ARCHIVE_GMK := 1 ifeq ($(_MAKEBASE_GMK), ) $(error You must include MakeBase.gmk prior to including JarArchive.gmk) endif include MakeIO.gmk FALSE_FIND_PATTERN := -name FILE_NAME_THAT_DOESNT_EXIST # Setup make rules for creating a jar archive. # # Parameter 1 is the name of the rule. This name is used as variable prefix, # and the targets generated are listed in a variable by that name. # # Remaining parameters are named arguments. These include: # DEPENDENCIES List of dependencies for the jar target. If left empty, # dependencies are calculated automatically from the source files found. # For this to work, the source files must exist when the makefile is # parsed. # SRCS List of directories in where to find files to add to archive # BIN Directory where to store build control files # SUFFIXES File suffixes to include in jar # INCLUDES List of directories/packages in SRCS that should be included # EXCLUDES List of directories/packages in SRCS that should be excluded # EXCLUDE_FILES List of files in SRCS that should be excluded # EXTRA_FILES List of files in SRCS that should be included regardless of suffix match. # JAR Jar file to create # MANIFEST Optional manifest file template. # JARMAIN Optional main class to add to manifest # JARINDEX true means generate the index in the jar file. # SKIP_METAINF Set to prevent contents of an META-INF directory to be automatically # added to the archive. # EXTRA_MANIFEST_ATTR Extra attribute to add to manifest. # CHECK_COMPRESS_JAR Check the COMPRESS_JAR variable # JAR_CMD Optionally override the jar command to use when creating the archive. SetupJarArchive = $(NamedParamsMacroTemplate) define SetupJarArchiveBody $1_JARMAIN := $(strip $$($1_JARMAIN)) $1_JARNAME := $$(notdir $$($1_JAR)) $1_JAR_OUTPUT_DIR := $$(patsubst %/, %, $$(dir $$($1_JAR))) $$(call SetIfEmpty, $1_BIN, $$($1_JAR_OUTPUT_DIR)) $1_MANIFEST_FILE := $$($1_BIN)/_the.$$($1_JARNAME)_manifest $1_DELETESS_FILE := $$($1_BIN)/_the.$$($1_JARNAME)_deletess $1_DELETES_FILE := $$($1_BIN)/_the.$$($1_JARNAME)_deletes $$(call SetIfEmpty, $1_JAR_CMD, $$(JAR)) ifeq ($$($1_SUFFIXES), ) # No suffix was set, default to classes. $1_SUFFIXES := .class endif # Convert suffixes to a find expression $1_FIND_PATTERNS := $(FALSE_FIND_PATTERN) $$(patsubst %,$(SPACE)-o$(SPACE)-name$(SPACE)$(DQUOTE)*%$(DQUOTE),$$($1_SUFFIXES)) # On windows, a lot of includes/excludes risk making the command line too long, so # writing the grep patterns to files. # Grep returns 1 if nothing is matched. Do not fail the build for this. ifneq ($$($1_INCLUDES), ) $1_GREP_INCLUDE_PATTERNS := $$(call EscapeDollar, \ $$(foreach src,$$($1_SRCS), $$(addprefix $$(src)/,$$($1_INCLUDES)))) # If there are a lot of include patterns, output to file to shorten command lines ifeq ($$(word 20, $$($1_GREP_INCLUDE_PATTERNS)), ) $1_GREP_INCLUDES := | ( $(GREP) $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_INCLUDE_PATTERNS)) \ || test "$$$$?" = "1" ) else $1_GREP_INCLUDE_OUTPUT = \ $$(eval $$(call ListPathsSafely, $1_GREP_INCLUDE_PATTERNS, \ $$($1_BIN)/_the.$$($1_JARNAME)_include)) $1_GREP_INCLUDES := | ( $(GREP) -f $$($1_BIN)/_the.$$($1_JARNAME)_include \ || test "$$$$?" = "1" ) endif endif ifneq ($$($1_EXCLUDES)$$($1_EXCLUDE_FILES), ) $1_GREP_EXCLUDE_PATTERNS := $$(call EscapeDollar, \ $$(foreach src, $$($1_SRCS), $$(addprefix $$(src)/, \ $$($1_EXCLUDES) $$($1_EXCLUDE_FILES)))) # If there are a lot of include patterns, output to file to shorten command lines ifeq ($$(word 20, $$($1_GREP_EXCLUDE_PATTERNS)), ) $1_GREP_EXCLUDES := | ( $(GREP) -v $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_EXCLUDE_PATTERNS)) \ || test "$$$$?" = "1" ) else $1_GREP_EXCLUDE_OUTPUT = \ $$(eval $$(call ListPathsSafely, $1_GREP_EXCLUDE_PATTERNS, \ $$($1_BIN)/_the.$$($1_JARNAME)_exclude)) $1_GREP_EXCLUDES := | ( $(GREP) -v -f $$($1_BIN)/_the.$$($1_JARNAME)_exclude \ || test "$$$$?" = "1" ) endif endif # Check if this jar needs to have its index generated. ifneq ($$($1_JARINDEX), ) $1_JARINDEX = (cd $$(dir $$@) && $$($1_JAR_CMD) -i $$(notdir $$@)) else $1_JARINDEX = true endif # When this macro is run in the same makefile as the java compilation, dependencies are # transferred in make variables. When the macro is run in a different makefile than the # java compilation, the dependencies need to be found in the filesystem. $1_ORIG_DEPS := $$($1_DEPENDENCIES) ifeq ($$($1_DEPENDENCIES), ) # Add all source roots to the find cache since we are likely going to run find # on these more than once. The cache will only be updated if necessary. $$(call FillFindCache, $$($1_FIND_LIST)) $1_DEPENDENCIES := $$(filter $$(addprefix %, $$($1_SUFFIXES)), \ $$(call FindFiles, $$($1_SRCS))) ifneq ($$($1_GREP_INCLUDE_PATTERNS), ) $1_DEPENDENCIES := $$(filter $$(addsuffix %, $$($1_GREP_INCLUDE_PATTERNS)), $$($1_DEPENDENCIES)) endif ifneq ($$($1_GREP_EXCLUDE_PATTERNS), ) $1_DEPENDENCIES := $$(filter-out $$(addsuffix %, $$($1_GREP_EXCLUDE_PATTERNS)), $$($1_DEPENDENCIES)) endif # Look for EXTRA_FILES in all SRCS dirs and as absolute paths. $1_DEPENDENCIES += $$(wildcard $$(foreach src, $$($1_SRCS), \ $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES)) ifeq ($$($1_SKIP_METAINF), ) $1_DEPENDENCIES += $$(call FindFiles, $$(wildcard $$(addsuffix /META-INF, $$($1_SRCS)))) endif endif # The dependency list should never be empty ifeq ($$(strip $$($1_DEPENDENCIES)), ) $$(warning No dependencies found for $1) endif # Utility macros, to make the shell script receipt somewhat easier to decipher. # Capture extra files is the same for both CAPTURE_CONTENTS and SCAPTURE_CONTENTS so # only define it once to avoid duplication. # The list of extra files might be long, so need to use ListPathsSafely to print # them out to a separate file. Then process the contents of that file to rewrite # into -C