JavaCompilation.gmk revision 1247:da0c4139fe30
10SN/A#
21410Sihse# Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
30SN/A# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
40SN/A#
50SN/A# This code is free software; you can redistribute it and/or modify it
60SN/A# under the terms of the GNU General Public License version 2 only, as
7180SN/A# published by the Free Software Foundation.  Oracle designates this
80SN/A# particular file as subject to the "Classpath" exception as provided
9180SN/A# by Oracle in the LICENSE file that accompanied this code.
100SN/A#
110SN/A# This code is distributed in the hope that it will be useful, but WITHOUT
120SN/A# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
130SN/A# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
140SN/A# version 2 for more details (a copy is included in the LICENSE file that
150SN/A# accompanied this code).
160SN/A#
170SN/A# You should have received a copy of the GNU General Public License version
180SN/A# 2 along with this work; if not, write to the Free Software Foundation,
190SN/A# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
200SN/A#
21180SN/A# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22180SN/A# or visit www.oracle.com if you need additional information or have any
23180SN/A# questions.
240SN/A#
250SN/A
261410Sihse# This makefile is much simpler now that it can use the smart javac wrapper
271410Sihse# for dependency tracking between java packages and incremental compiles.
281410Sihse# It could be even more simple if we added support for incremental jar updates
291410Sihse# directly from the smart javac wrapper.
301410Sihse
311410Sihse# Cleaning/copying properties here is not a good solution. The properties
32910SN/A# should be cleaned/copied by a annotation processor in sjavac.
33910SN/A
341410Sihse# When you read this source. Remember that $(sort ...) has the side effect
355SN/A# of removing duplicates. It is actually this side effect that is
36910SN/A# desired whenever sort is used below!
371410Sihse
38910SN/Aifndef _JAVA_COMPILATION_GMK
39338SN/A_JAVA_COMPILATION_GMK := 1
401426Sihse
411410Sihseifeq (,$(_MAKEBASE_GMK))
421426Sihse  $(error You must include MakeBase.gmk prior to including JavaCompilation.gmk)
431410Sihseendif
441410Sihse
451426Sihse# Java compilation needs SetupZipArchive if we're generating a source zip.
461426Sihseinclude ZipArchive.gmk
47311SN/A
481426SihseFALSE_FIND_PATTERN:=-name FILE_NAME_THAT_DOESNT_EXIST
491426Sihse
501426Sihse# Setup make rules for defining a Java compiler, which is needed to compile
511410Sihse# Java code. This rule generates no output.
521426Sihse#
531651Sihse# Parameter 1 is the name of the compiler definition. This name needs to be
541651Sihse# passed to SetupJavaCompilation. This name is used as variable prefix.
551120SN/A#
561410Sihse# Remaining parameters are named arguments. These include:
571410Sihse#   JVM:=The jvm used to run the javac/javah command
581410Sihse#   JAVAC:=The javac jar and bootstrap classpath changes, or just bin/javac if JVM is left out
591426Sihse#   FLAGS:=Flags to be supplied to javac
601426Sihse#   SERVER_DIR:=Use a javac server (-XDserver) and store the server related files here
611426Sihse#   SERVER_JVM:=Use this JVM for the server. Defaults to the JVM above.
621426Sihsedefine SetupJavaCompiler
631426Sihse  $(if $(16),$(error Internal makefile error: Too many arguments to SetupJavaCompiler, please update JavaCompilation.gmk))
641426Sihse  $(call EvalDebugWrapper,$(strip $1),$(call SetupJavaCompilerInner,$(strip $1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15)))
651426Sihseendef
661426Sihse
671426Sihsedefine SetupJavaCompilerInner
681426Sihse  $(foreach i,2 3 4 5 6 7 8 9 10 11 12 13 14 15, $(if $($i),$1_$(strip $($i)))$(NEWLINE))
691426Sihse  $(call LogSetupMacroEntry,SetupJavaCompiler($1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15))
701426Sihse  $(if $(16),$(error Internal makefile error: Too many arguments to SetupJavaCompiler, please update JavaCompilation.gmk))
711426Sihse
721426Sihse  # The port file contains the tcp/ip on which the server listens
731426Sihse  # and the cookie necessary to talk to the server.
741426Sihse  $1_SJAVAC_PORTFILE:=$$($1_SERVER_DIR)/server.port
751426Sihse  # You can use a different JVM to run the background javac server.
761426Sihse  ifeq ($$($1_SERVER_JVM),)
771426Sihse    # It defaults to the same JVM that is used to start the javac command.
781426Sihse    $1_SERVER_JVM:=$$($1_JVM)
791426Sihse  endif
801426Sihseendef
811426Sihse
821426Sihse# Setup make rules for creating a jar archive.
831426Sihse#
841426Sihse# Parameter 1 is the name of the rule. This name is used as variable prefix,
851426Sihse# and the targets generated are listed in a variable by that name.
861426Sihse#
871426Sihse# Remaining parameters are named arguments. These include:
881426Sihse#   SRCS:=List of directories in where to find files to add to archive
891426Sihse#   SUFFIXES:=File suffixes to include in jar
901426Sihse#   INCLUDES:=List of directories/packages in SRCS that should be included
911426Sihse#   EXCLUDES:=List of directories/packages in SRCS that should be excluded
921426Sihse#   EXCLUDE_FILES:=List of files in SRCS that should be excluded
931426Sihse#   EXTRA_FILES:=List of files in SRCS that should be included regardless of suffix match.
941426Sihse#   JAR:=Jar file to create
951426Sihse#   MANIFEST:=Optional manifest file template.
961426Sihse#   JARMAIN:=Optional main class to add to manifest
971426Sihse#   JARINDEX:=true means generate the index in the jar file.
981426Sihse#   SKIP_METAINF:=Set to prevent contents of an META-INF directory to be automatically
991426Sihse#       added to the archive.
1001426Sihse#   EXTRA_MANIFEST_ATTR:=Extra attribute to add to manifest.
1011426Sihse#   CHECK_COMPRESS_JAR Check the COMPRESS_JAR variable
1021426Sihsedefine SetupArchive
1031426Sihse  $(if $(16),$(error Internal makefile error: Too many arguments to SetupArchive, please update JavaCompilation.gmk))
1041426Sihse  $(call EvalDebugWrapper,$(strip $1),$(call SetupArchiveInner,$(strip $1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15)))
1051426Sihseendef
1061426Sihse
1071426Sihsedefine SetupArchiveInner
1081426Sihse  # NOTE: $2 is dependencies, not a named argument!
1091426Sihse  $(foreach i,3 4 5 6 7 8 9 10 11 12 13 14 15, $(if $($i),$1_$(strip $($i)))$(NEWLINE))
1101426Sihse  $(call LogSetupMacroEntry,SetupArchive($1),<dependencies>,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15))
1111426Sihse  $(if $(findstring $(LOG_LEVEL),trace), $(info *[2] <dependencies> = $(strip $2)))
1121426Sihse  $(if $(16),$(error Internal makefile error: Too many arguments to SetupArchive, please update JavaCompilation.gmk))
1131426Sihse
1141426Sihse  $1_JARMAIN:=$(strip $$($1_JARMAIN))
1151426Sihse  $1_JARNAME:=$$(notdir $$($1_JAR))
1161426Sihse  $1_MANIFEST_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_manifest
1171426Sihse  $1_DELETESS_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletess
1181426Sihse  $1_DELETES_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletes
1191426Sihse  $1_BIN:=$$(dir $$($1_JAR))
1201426Sihse
1211426Sihse  ifeq (,$$($1_SUFFIXES))
1221426Sihse    # No suffix was set, default to classes.
1231426Sihse    $1_SUFFIXES:=.class
1241410Sihse  endif
1251120SN/A  # Convert suffixes to a find expression
1261426Sihse  $1_FIND_PATTERNS:=$(FALSE_FIND_PATTERN) $$(patsubst %,$(SPACE)-o$(SPACE)-name$(SPACE)$(DQUOTE)*%$(DQUOTE),$$($1_SUFFIXES))
1271426Sihse  # On windows, a lot of includes/excludes risk making the command line too long, so
1281426Sihse  # writing the grep patterns to files.
1291426Sihse  ifneq (,$$($1_INCLUDES))
1301426Sihse    $1_GREP_INCLUDE_PATTERNS:=$$(call EscapeDollar, \
1311426Sihse        $$(foreach src,$$($1_SRCS), $$(addprefix $$(src)/,$$($1_INCLUDES))))
1321426Sihse    # If there are a lot of include patterns, output to file to shorten command lines
1331410Sihse    ifeq ($$(word 20,$$($1_GREP_INCLUDE_PATTERNS)),)
1341426Sihse      $1_GREP_INCLUDES:=| $(GREP) $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_INCLUDE_PATTERNS))
1351426Sihse    else
1361426Sihse      $1_GREP_INCLUDE_OUTPUT:=$(RM) $$($1_BIN)/_the.$$($1_JARNAME)_include $$(NEWLINE) \
13727SN/A          $$(call ListPathsSafely,$1_GREP_INCLUDE_PATTERNS,\n, \
1381410Sihse          >> $$($1_BIN)/_the.$$($1_JARNAME)_include)
1391410Sihse      $1_GREP_INCLUDES:=| $(GREP) -f $$($1_BIN)/_the.$$($1_JARNAME)_include
1401701Sihse    endif
1411701Sihse  endif
1421701Sihse  ifneq (,$$($1_EXCLUDES)$$($1_EXCLUDE_FILES))
1431410Sihse    $1_GREP_EXCLUDE_PATTERNS:=$$(call EscapeDollar, \
1441410Sihse        $$(foreach src,$$($1_SRCS),$$(addprefix $$(src)/, \
1451410Sihse        $$($1_EXCLUDES) $$($1_EXCLUDE_FILES))))
1461410Sihse    # If there are a lot of include patterns, output to file to shorten command lines
1471410Sihse    ifeq ($$(word 20,$$($1_GREP_EXCLUDE_PATTERNS)),)
1481410Sihse      $1_GREP_EXCLUDES:=| $(GREP) -v $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_EXCLUDE_PATTERNS))
1491410Sihse    else
1501426Sihse      $1_GREP_EXCLUDE_OUTPUT=$(RM) $$($1_BIN)/_the.$$($1_JARNAME)_exclude $$(NEWLINE) \
1511426Sihse          $$(call ListPathsSafely,$1_GREP_EXCLUDE_PATTERNS,\n, \
1521410Sihse          >> $$($1_BIN)/_the.$$($1_JARNAME)_exclude)
1531410Sihse      $1_GREP_EXCLUDES:=| $(GREP) -v -f $$($1_BIN)/_the.$$($1_JARNAME)_exclude
1541410Sihse    endif
1551156SN/A  endif
1561120SN/A
1571426Sihse  # Check if this jar needs to have its index generated.
1581426Sihse  ifneq (,$$($1_JARINDEX))
1591426Sihse    $1_JARINDEX = (cd $$(dir $$@) && $(JAR) -i $$(notdir $$@))
1601426Sihse  else
1611410Sihse    $1_JARINDEX = true
16227SN/A  endif
1631426Sihse  # When this macro is run in the same makefile as the java compilation, dependencies are
1641426Sihse  # transfered in make variables. When the macro is run in a different makefile than the
1651426Sihse  # java compilation, the dependencies need to be found in the filesystem.
1661426Sihse  ifneq (,$2)
1671426Sihse    $1_DEPS:=$2
1681426Sihse  else
1691651Sihse    # Add all source roots to the find cache since we are likely going to run find
1701651Sihse    # on these more than once. The cache will only be updated if necessary.
1711915Sihse    $$(eval $$(call FillCacheFind, $$($1_FIND_LIST)))
1721651Sihse    $1_DEPS:=$$(filter $$(addprefix %,$$($1_SUFFIXES)), \
1731651Sihse        $$(call CacheFind,$$($1_SRCS)))
1741651Sihse    ifneq (,$$($1_GREP_INCLUDE_PATTERNS))
1751651Sihse      $1_DEPS:=$$(filter $$(addsuffix %,$$($1_GREP_INCLUDE_PATTERNS)),$$($1_DEPS))
1761426Sihse    endif
1771426Sihse    ifneq (,$$($1_GREP_EXCLUDE_PATTERNS))
1781426Sihse      $1_DEPS:=$$(filter-out $$(addsuffix %,$$($1_GREP_EXCLUDE_PATTERNS)),$$($1_DEPS))
1791426Sihse    endif
1801651Sihse    # Look for EXTRA_FILES in all SRCS dirs and as absolute paths.
1811426Sihse    $1_DEPS+=$$(wildcard $$(foreach src, $$($1_SRCS), \
1821410Sihse        $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES))
1831410Sihse    ifeq (,$$($1_SKIP_METAINF))
1841410Sihse      $1_DEPS+=$$(call CacheFind,$$(wildcard $$(addsuffix /META-INF,$$($1_SRCS))))
1851651Sihse    endif
1861410Sihse  endif
1871426Sihse  # The dependency list should never be empty
1881651Sihse  ifeq ($$(strip $$($1_DEPS)), )
1891651Sihse    $$(warning No dependencies found for $1)
1901651Sihse  endif
1911651Sihse
1921651Sihse  # Utility macros, to make the shell script receipt somewhat easier to decipher.
1931651Sihse
1941651Sihse  # Capture extra files is the same for both CAPTURE_CONTENTS and SCAPTURE_CONTENTS so
1951651Sihse  # only define it once to avoid duplication.
1961651Sihse  # The list of extra files might be long, so need to use ListPathsSafely to print
1971651Sihse  # them out to a separte file. Then process the contents of that file to rewrite
1981651Sihse  # into -C <dir> <file> lines.
1991651Sihse  # The EXTRA_FILES_RESOLVED varible must be set in the macro so that it's evaluated
2001651Sihse  # in the recipe when the files are guaranteed to exist.
2011651Sihse  $1_CAPTURE_EXTRA_FILES=\
2021410Sihse      $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra $$(NEWLINE) \
2031410Sihse      $$(eval $1_EXTRA_FILES_RESOLVED:=$$(call DoubleDollar, $$(call DoubleDollar, \
2041410Sihse          $$(wildcard $$(foreach src, $$($1_SRCS), \
2051410Sihse          $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES))))) \
2060SN/A      $$(if $$($1_EXTRA_FILES_RESOLVED), \
2071426Sihse        $$(call ListPathsSafely,$1_EXTRA_FILES_RESOLVED,\n, \
2081410Sihse            >> $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra) $$(NEWLINE) \
2091410Sihse        $(SED) $$(foreach src,$$($1_SRCS), -e 's|$$(src)/|-C $$(src) |g') \
2101410Sihse            $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra \
2111410Sihse            >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE))
2121410Sihse
2131410Sihse  # The capture contents macro finds all files (matching the patterns, typically
2141410Sihse  # .class and .prp) that are newer than the jar-file, ie the new content to be put into the jar.
2151410Sihse  # NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
2161410Sihse  $1_CAPTURE_CONTENTS=\
2171426Sihse      $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
2181426Sihse      $$(foreach src,$$($1_SRCS), \
2191426Sihse        $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) -a -newer $$@ $$($1_GREP_INCLUDES) \
2201426Sihse          $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
2211426Sihse        >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
2221426Sihse      $$($1_CAPTURE_EXTRA_FILES)
2231426Sihse
2241426Sihse  # The capture metainf macro finds all files below the META-INF directory that are newer than the jar-file.
2251651Sihse  ifeq (,$$($1_SKIP_METAINF))
2261651Sihse    $1_CAPTURE_METAINF =$$(foreach src,$$($1_SRCS), \
2271651Sihse        ( $(FIND) $$(src)/META-INF -type f -a -newer $$@ 2> /dev/null | $(SED) 's|$$(src)/|-C $$(src) |g' >> \
2281410Sihse        $$($1_BIN)/_the.$$($1_JARNAME)_contents ) $$(NEWLINE) )
2291862Serikj  endif
2301862Serikj  # The capture deletes macro finds all deleted files and concatenates them. The resulting file
2311410Sihse  # tells us what to remove from the jar-file.
2321410Sihse  $1_CAPTURE_DELETES=$$(foreach src,$$($1_SRCS),($(FIND) $$(src) -name _the.package.deleted -newer $$@ \
2331410Sihse      -exec $(SED) 's|$$(src)||g' \{\} >> $$($1_DELETES_FILE) \;) $$(NEWLINE))
2341410Sihse  # The update contents macro updates the jar file with the previously capture contents.
2351410Sihse  # Use 'wc -w' to see if the contents file is empty.
2361410Sihse  $1_UPDATE_CONTENTS=\
2371426Sihse      if [ "`$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'`" -gt "0" ]; then \
2381426Sihse        $(ECHO) "  updating" `$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'` files && \
2391426Sihse        $(JAR) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents; \
2401426Sihse      fi $$(NEWLINE)
2411426Sihse  # The s-variants of the above macros are used when the jar is created from scratch.
2421426Sihse  # NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
2431426Sihse  $1_SCAPTURE_CONTENTS=\
2441426Sihse      $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
2451426Sihse      $$(foreach src,$$($1_SRCS), \
2461426Sihse        $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) $$($1_GREP_INCLUDES) \
2471426Sihse            $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
2481426Sihse            >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
2491426Sihse      $$($1_CAPTURE_EXTRA_FILES)
2501327SN/A
2511628Sihse  ifeq (,$$($1_SKIP_METAINF))
2521628Sihse    $1_SCAPTURE_METAINF=$$(foreach src,$$($1_SRCS), \
2531628Sihse        ( $(FIND) $$(src)/META-INF -type f 2> /dev/null | $(SED) 's|$$(src)/|-C $$(src) |g' >> \
2541410Sihse        $$($1_BIN)/_the.$$($1_JARNAME)_contents) $$(NEWLINE) )
2551410Sihse  endif
2561410Sihse  $1_SUPDATE_CONTENTS=$(JAR) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)
2571410Sihse
2581410Sihse  # Use a slightly shorter name for logging, but with enough path to identify this jar.
2591410Sihse  $1_NAME:=$$(subst $$(OUTPUT_ROOT)/,,$$($1_JAR))
2601410Sihse
2611410Sihse  ifneq (,$$($1_CHECK_COMPRESS_JAR))
2621410Sihse    $1_JAR_CREATE_OPTIONS := c0fm
2631426Sihse    $1_JAR_UPDATE_OPTIONS := u0f
2641426Sihse    ifeq ($(COMPRESS_JARS), true)
2651426Sihse      $1_JAR_CREATE_OPTIONS := cfm
2661410Sihse      $1_JAR_UPDATE_OPTIONS := uf
2671651Sihse    endif
2681426Sihse  else
2691426Sihse    $1_JAR_CREATE_OPTIONS := cfm
2701410Sihse    $1_JAR_UPDATE_OPTIONS := uf
2711410Sihse  endif
2721410Sihse
2731410Sihse  # Here is the rule that creates/updates the jar file.
2741410Sihse  $$($1_JAR) : $$($1_DEPS)
2750SN/A	$(MKDIR) -p $$($1_BIN)
2761426Sihse	$$($1_GREP_INCLUDE_OUTPUT)
2771426Sihse	$$($1_GREP_EXCLUDE_OUTPUT)
2781426Sihse	$$(if $$($1_MANIFEST), \
2791623Sihse	  $(SED) -e "s#@@RELEASE@@#$(RELEASE)#" \
2801862Serikj	      -e "s#@@COMPANY_NAME@@#$(COMPANY_NAME)#" $$($1_MANIFEST) > $$($1_MANIFEST_FILE) \
2811426Sihse	, \
2821426Sihse	  $(RM) $$($1_MANIFEST_FILE) && $(TOUCH) $$($1_MANIFEST_FILE))
2831862Serikj	$$(if $$($1_JARMAIN),$(ECHO) "Main-Class: $$(strip $$($1_JARMAIN))" >> $$($1_MANIFEST_FILE))
2841426Sihse	$$(if $$($1_EXTRA_MANIFEST_ATTR),$(PRINTF) "$$($1_EXTRA_MANIFEST_ATTR)\n" >> $$($1_MANIFEST_FILE))
2851426Sihse	$$(if $$(wildcard $$@), \
2861426Sihse	  $(ECHO) Modifying $$($1_NAME) $$(NEWLINE) \
2871426Sihse	  $$($1_CAPTURE_CONTENTS) \
2881426Sihse	  $$($1_CAPTURE_METAINF) \
2891426Sihse	  $(RM) $$($1_DELETES_FILE) $$(NEWLINE) \
2901426Sihse	  $$($1_CAPTURE_DELETES) \
2911426Sihse	  $(CAT) $$($1_DELETES_FILE) > $$($1_DELETESS_FILE) $$(NEWLINE) \
2921862Serikj	  if [ -s $$($1_DELETESS_FILE) ]; then \
2931623Sihse	    $(ECHO) "  deleting" `$(WC) -l $$($1_DELETESS_FILE) | $(AWK) '{ print $$$$1 }'` files && \
2941862Serikj	    $(ZIP) -q -d $$@ `$(CAT) $$($1_DELETESS_FILE)` ; \
2951862Serikj	  fi $$(NEWLINE) \
2961862Serikj	  $$($1_UPDATE_CONTENTS) true $$(NEWLINE) \
2971862Serikj	  $$($1_JARINDEX) && true \
2981623Sihse	, \
2991623Sihse	  $(ECHO) Creating $$($1_NAME) && $(JAR) $$($1_JAR_CREATE_OPTIONS) $$@ $$($1_MANIFEST_FILE) $$(NEWLINE) \
3001623Sihse	  $$($1_SCAPTURE_CONTENTS) \
3011426Sihse	  $$($1_SCAPTURE_METAINF) \
3021426Sihse	  $$($1_SUPDATE_CONTENTS) \
3031426Sihse	  $$($1_JARINDEX) && true )
3041426Sihse
3051862Serikj  # Add jar to target list
3061410Sihse  $1 += $$($1_JAR)
3071410Sihseendef
3081623Sihse
3091745Sihse    $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
3101745Sihse  ifneq ($$($1_EXCLUDE_FILES),)
3111745Sihse    # Cannot precompute ZIP_EXCLUDE_FILES as it is dependent on which src root is being
3121745Sihse    # zipped at the moment.
3131745Sihse    $1_SRC_EXCLUDE_FILES := $$(addprefix %, $$($1_EXCLUDE_FILES)) $$($1_EXCLUDE_FILES)
3141623Sihse    $1_ALL_SRCS := $$(filter-out $$($1_SRC_EXCLUDE_FILES), $$($1_ALL_SRCS))
3151623Sihse  endif
3161651Sihsedefine add_file_to_copy
3171651Sihse  # param 1 = BUILD_MYPACKAGE
3181900Sihse  # parma 2 = The source file to copy.
3191745Sihse  $2_TARGET:=$2
3201651Sihse  # Remove the source prefix.
3211651Sihse  $$(foreach i,$$($1_SRC),$$(eval $$(call remove_string,$$i,$2_TARGET)))
3221926Serikj  # To allow for automatic overrides, do not create a rule for a target file that
3231745Sihse  # already has one
3241745Sihse  ifeq ($$(findstring $$($2_TARGET), $$($1_COPY_LIST)), )
3251651Sihse    $1_COPY_LIST += $$($2_TARGET)
3261623Sihse    # Now we can setup the depency that will trigger the copying.
3271410Sihse    $$($1_BIN)$$($2_TARGET) : $2
328	$(MKDIR) -p $$(@D)
329	$(CP) $$< $$@
330	$(CHMOD) -f ug+w $$@
331
332    # And do not forget this target
333    $1_ALL_COPY_TARGETS += $$($1_BIN)$$($2_TARGET)
334  endif
335endef
336
337
338# This macro is used only for properties files that are to be
339# copied over to the classes directory in cleaned form:
340# Previously this was inconsistently done in different repositories.
341# This is the new clean standard. Though it is to be superseded by
342# a standard annotation processor from with sjavac.
343#
344# The sed expression does this:
345# 1. Add a backslash before any :, = or ! that do not have a backslash already.
346# 2. Apply the file unicode2x.sed which does a whole bunch of \u00XX to \xXX
347#    conversions.
348# 3. Delete all lines starting with #.
349# 4. Delete empty lines.
350# 5. Append lines ending with \ with the next line.
351# 6. Remove leading and trailing white space. Note that tabs must be explicit
352#    as sed on macosx does not understand '\t'.
353# 7. Replace the first \= with just =.
354# 8. Finally it's all sorted to create a stable output.
355#
356# It is assumed that = is the character used for separating names and values.
357define add_file_to_clean
358  # param 1 = BUILD_MYPACKAGE
359  # parma 2 = The source file to copy and clean.
360  $2_TARGET:=$2
361  # Remove the source prefix.
362  $$(foreach i,$$($1_SRC),$$(eval $$(call remove_string,$$i,$2_TARGET)))
363  # Now we can setup the depency that will trigger the copying.
364  $$($1_BIN)$$($2_TARGET) : $2
365	$(MKDIR) -p $$(@D)
366	export LC_ALL=C ; $(CAT) $$< \
367	    | $(SED) -e 's/\([^\\]\):/\1\\:/g' -e 's/\([^\\]\)=/\1\\=/g' \
368	        -e 's/\([^\\]\)!/\1\\!/g' -e 's/#.*/#/g' \
369	    | $(SED) -f "$(SRC_ROOT)/make/common/support/unicode2x.sed" \
370	    | $(SED) -e '/^#/d' -e '/^$$$$/d' \
371	        -e :a -e '/\\$$$$/N; s/\\\n//; ta' \
372	        -e 's/^[ 	]*//;s/[ 	]*$$$$//' \
373	        -e 's/\\=/=/' \
374	    | $(SORT) > $$@
375	$(CHMOD) -f ug+w $$@
376
377  # And do not forget this target
378  $1_ALL_COPY_CLEAN_TARGETS += $$($1_BIN)$$($2_TARGET)
379endef
380
381define remove_string
382  $2 := $$(subst $1,,$$($2))
383endef
384
385# Setup make rules for compiling Java source code to class files and/or a
386# resulting jar file.
387#
388# Parameter 1 is the name of the rule. This name is used as variable prefix,
389# and the targets generated are listed in a variable by that name.
390#
391# Remaining parameters are named arguments. These include:
392#   SETUP:=must point to a previously setup java compiler, for example: SETUP:=BOOTJAVAC
393#   JVM:=path to ..bin/java
394#   ADD_JAVAC_FLAGS:=javac flags to append to the default ones.
395#   SRC:=one or more directories to search for sources
396#   BIN:=store classes here
397#   INCLUDES:=myapp.foo means will only compile java files in myapp.foo or any of its sub-packages.
398#   EXCLUDES:=myapp.foo means will do not compile java files in myapp.foo or any of its sub-packages.
399#   COPY:=.prp means copy all prp files to the corresponding package in BIN.
400#   COPY_FILES:=myapp/foo/setting.txt means copy this file over to the package myapp/foo
401#   CLEAN:=.properties means copy and clean all properties file to the corresponding package in BIN.
402#   CLEAN_FILES:=myapp/foo/setting.txt means clean this file over to the package myapp/foo
403#   SRCZIP:=Create a src.zip based on the found sources and copied files.
404#   INCLUDE_FILES:="com/sun/SolarisFoobar.java" means only compile this file!
405#   EXCLUDE_FILES:="com/sun/SolarisFoobar.java" means do not compile this particular file!
406#       "SolarisFoobar.java" means do not compile SolarisFoobar, wherever it is found.
407#   HEADERS:=path to directory where all generated c-headers are written.
408#   DEPENDS:=Extra dependecy
409#   DISABLE_SJAVAC:=Explicitly disable the use of sjavac for this compilation unit.
410define SetupJavaCompilation
411  $(if $(16),$(error Internal makefile error: Too many arguments to SetupJavaCompilation, please update JavaCompilation.gmk))
412  $(call EvalDebugWrapper,$(strip $1),$(call SetupJavaCompilationInner,$(strip $1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15)))
413endef
414
415define SetupJavaCompilationInner
416  $(foreach i,2 3 4 5 6 7 8 9 10 11 12 13 14 15, $(if $($i),$1_$(strip $($i)))$(NEWLINE))
417  $(call LogSetupMacroEntry,SetupJavaCompilation($1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15))
418  $(if $(16),$(error Internal makefile error: Too many arguments to SetupJavaCompilation, please update JavaCompilation.gmk))
419
420  # Extract the info from the java compiler setup.
421  $1_JVM := $$($$($1_SETUP)_JVM)
422  $1_JAVAC := $$($$($1_SETUP)_JAVAC)
423  $1_FLAGS := $$($$($1_SETUP)_FLAGS) $(JAVAC_FLAGS) $$($1_ADD_JAVAC_FLAGS)
424  ifeq ($$($1_JAVAC),)
425    $$(error The Java compilation $1 refers to a non-existant java compiler setup $$($1_SETUP))
426  endif
427  $1_SJAVAC_PORTFILE := $$($$($1_SETUP)_SJAVAC_PORTFILE)
428  $1_SERVER_JVM := $$($$($1_SETUP)_SERVER_JVM)
429
430  # Handle addons and overrides.
431  $1_SRC:=$$(call ADD_SRCS,$$($1_SRC))
432  # Make sure the dirs exist.
433  $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),,$$(error SRC specified to SetupJavaCompilation $1 contains missing directory $$d)))
434  $$(eval $$(call MakeDir,$$($1_BIN)))
435  # Add all source roots to the find cache since we are likely going to run find
436  # on these more than once. The cache will only be updated if necessary.
437  $$(eval $$(call FillCacheFind,$$($1_SRC)))
438  # Find all files in the source trees. Preserve order of source roots for overrides to
439  # work correctly. CacheFind does not preserve order so need to call it for each root.
440  $1_ALL_SRCS += $$(filter-out $(OVR_SRCS),$$(foreach s,$$($1_SRC),$$(call CacheFind,$$(s))))
441  # Extract the java files.
442  ifneq ($$($1_EXCLUDE_FILES),)
443    $1_EXCLUDE_FILES_PATTERN:=$$(addprefix %,$$($1_EXCLUDE_FILES))
444  endif
445  $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES_PATTERN),$$(filter %.java,$$($1_ALL_SRCS)))
446  ifneq ($$($1_INCLUDE_FILES),)
447    $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
448    $1_SRCS := $$(filter $$($1_INCLUDE_FILES), $$($1_SRCS))
449  endif
450
451  # Now we have a list of all java files to compile: $$($1_SRCS)
452
453  # Create the corresponding smart javac wrapper command line.
454  $1_SJAVAC_ARGS:=$$(addprefix -x ,$$(addsuffix /*,$$($1_EXCLUDES))) \
455      $$(addprefix -i ,$$(addsuffix /*,$$($1_INCLUDES))) \
456      $$(addprefix -xf *,$$(strip $$($1_EXCLUDE_FILES))) \
457      $$(addprefix -if *,$$(strip $$($1_INCLUDE_FILES))) \
458      -src "$$(subst $$(SPACE),$$(PATH_SEP),$$(strip $$($1_SRC)))"
459
460  # Prepend the source/bin path to the filter expressions.
461  ifneq ($$($1_INCLUDES),)
462    $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
463    $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
464  endif
465  ifneq ($$($1_EXCLUDES),)
466    $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
467    $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
468  endif
469
470  # All files below META-INF are always copied.
471  $1_ALL_COPIES := $$(filter $$(addsuffix /META-INF%,$$($1_SRC)),$$($1_ALL_SRCS))
472  # Find all files to be copied from source to bin.
473  ifneq (,$$($1_COPY)$$($1_COPY_FILES))
474    # Search for all files to be copied.
475    $1_ALL_COPIES += $$(filter $$(addprefix %,$$($1_COPY)),$$($1_ALL_SRCS))
476    # Copy these explicitly
477    $1_ALL_COPIES += $$($1_COPY_FILES)
478    # Copy must also respect filters.
479    ifneq (,$$($1_INCLUDES))
480      $1_ALL_COPIES := $$(filter $$($1_SRC_INCLUDES),$$($1_ALL_COPIES))
481    endif
482    ifneq (,$$($1_EXCLUDES))
483      $1_ALL_COPIES := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_ALL_COPIES))
484    endif
485    ifneq (,$$($1_EXCLUDE_FILES))
486      $1_ALL_COPIES := $$(filter-out $$($1_EXCLUDE_FILES_PATTERN),$$($1_ALL_COPIES))
487    endif
488  endif
489    ifneq (,$$($1_ALL_COPIES))
490      # Yep, there are files to be copied!
491      $1_ALL_COPY_TARGETS:=
492          $$(foreach i,$$($1_ALL_COPIES),$$(eval $$(call add_file_to_copy,$1,$$i)))
493      # Now we can depend on $$($1_ALL_COPY_TARGETS) to copy all files!
494    endif
495
496  # Find all property files to be copied and cleaned from source to bin.
497  ifneq (,$$($1_CLEAN)$$($1_CLEAN_FILES))
498    # Search for all files to be copied.
499    $1_ALL_CLEANS := $$(filter $$(addprefix %,$$($1_CLEAN)),$$($1_ALL_SRCS))
500    # Clean these explicitly
501    $1_ALL_CLEANS += $$($1_CLEAN_FILES)
502    # Copy and clean must also respect filters.
503    ifneq (,$$($1_INCLUDES))
504      $1_ALL_CLEANS := $$(filter $$($1_SRC_INCLUDES),$$($1_ALL_CLEANS))
505    endif
506    ifneq (,$$($1_EXCLUDES))
507      $1_ALL_CLEANS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_ALL_CLEANS))
508    endif
509    ifneq (,$$($1_EXCLUDE_FILES))
510      $1_ALL_CLEANS := $$(filter-out $$($1_EXCLUDE_FILES_PATTERN),$$($1_ALL_CLEANS))
511    endif
512    ifneq (,$$($1_ALL_CLEANS))
513      # Yep, there are files to be copied and cleaned!
514      $1_ALL_COPY_CLEAN_TARGETS:=
515          $$(foreach i,$$($1_ALL_CLEANS),$$(eval $$(call add_file_to_clean,$1,$$i)))
516      # Now we can depend on $$($1_ALL_COPY_CLEAN_TARGETS) to copy all files!
517    endif
518  endif
519
520  # Create a sed expression to remove the source roots and to replace / with .
521  # and remove .java at the end.
522  $1_REWRITE_INTO_CLASSES:=$$(foreach i,$$($1_SRC),-e 's|$$i/||g') -e 's|/|.|g' -e 's|.java$$$$||g'
523
524  ifeq ($$($1_DISABLE_SJAVAC)x$$(ENABLE_SJAVAC),xyes)
525    ifneq (,$$($1_HEADERS))
526      $1_HEADERS_ARG := -h $$($1_HEADERS)
527    endif
528
529    # Using sjavac to compile.
530    $1_COMPILE_TARGETS := $$($1_BIN)/_the.$1_batch
531
532    # Create SJAVAC variable form JAVAC variable. Expects $1_JAVAC to be
533    # "bootclasspathprepend -cp .../javac.jar com.sun.tools.javac.Main"
534    # and javac is simply replaced with sjavac.
535    $1_SJAVAC:=$$(subst com.sun.tools.javac.Main,com.sun.tools.sjavac.Main,$$($1_JAVAC))
536
537    # Set the $1_REMOTE to spawn a background javac server.
538    $1_REMOTE:=--server:portfile=$$($1_SJAVAC_PORTFILE),id=$1,sjavac=$$(subst $$(SPACE),%20,$$(subst $$(COMMA),%2C,$$(strip $$($1_SERVER_JVM) $$($1_SJAVAC))))
539
540    $$($1_BIN)/_the.$1_batch: $$($1_SRCS) $$($1_DEPENDS)
541	$(MKDIR) -p $$(@D)
542        # As a workaround for sjavac not tracking api changed from the classpath, force full
543        # recompile if an external dependency, which is something other than a source
544        # change, triggered this compilation.
545	$$(if $$(filter-out $$($1_SRCS), $$?), $(FIND) $$(@D) -name "*.class" $(FIND_DELETE))
546	$$(call ListPathsSafely,$1_SRCS,\n, >> $$($1_BIN)/_the.$1_batch.tmp)
547	$(ECHO) Compiling $1
548	($$($1_JVM) $$($1_SJAVAC) \
549	    $$($1_REMOTE) \
550	    -j 1 \
551	    --permit-unidentified-artifacts \
552	    --permit-sources-without-package \
553	    --compare-found-sources $$($1_BIN)/_the.$1_batch.tmp \
554	    --log=$(LOG_LEVEL) \
555	    $$($1_SJAVAC_ARGS) \
556	    $$($1_FLAGS) \
557	    $$($1_HEADERS_ARG) \
558	    -d $$($1_BIN) && \
559	$(MV) $$($1_BIN)/_the.$1_batch.tmp $$($1_BIN)/_the.$1_batch)
560        # Create a pubapi file that only changes when the pubapi changes. Dependent
561        # compilations can use this file to only get recompiled when pubapi has changed.
562        # Grep returns 1 if no matching lines are found. Do not fail for this.
563	$(GREP) -e "^I" $$($1_BIN)/javac_state > $$($1_BIN)/_the.$1_pubapi.tmp \
564	    || test "$$$$?" = "1"
565	if [ ! -f $$($1_BIN)/_the.$1_pubapi ] \
566	    || [ "`$(DIFF) $$($1_BIN)/_the.$1_pubapi $$($1_BIN)/_the.$1_pubapi.tmp`" != "" ]; then \
567	  $(MV) $$($1_BIN)/_the.$1_pubapi.tmp $$($1_BIN)/_the.$1_pubapi; \
568	fi
569
570  else
571    # Using plain javac to batch compile everything.
572    $1_COMPILE_TARGETS := $$($1_BIN)/_the.$1_batch
573
574    # When building in batch, put headers in a temp dir to filter out those that actually
575    # changed before copying them to the real header dir.
576    ifneq (,$$($1_HEADERS))
577      $1_HEADERS_ARG := -h $$($1_HEADERS).$1.tmp
578
579      $$($1_HEADERS)/_the.$1_headers: $$($1_BIN)/_the.$1_batch
580		$(MKDIR) -p $$(@D)
581		if [ -d "$$($1_HEADERS).$1.tmp" ]; then \
582		  for f in `ls $$($1_HEADERS).$1.tmp`; do \
583		    if [ ! -f "$$($1_HEADERS)/$$$$f" ] \
584		        || [ "`$(DIFF) $$($1_HEADERS)/$$$$f $$($1_HEADERS).$1.tmp/$$$$f`" != "" ]; then \
585		    $(CP) -f $$($1_HEADERS).$1.tmp/$$$$f $$($1_HEADERS)/$$$$f; \
586		  fi; \
587		  done; \
588		fi
589		$(RM) -r $$($1_HEADERS).$1.tmp
590		$(TOUCH) $$@
591
592      $1_HEADER_TARGETS := $$($1_HEADERS)/_the.$1_headers
593    endif
594
595    # When not using sjavac, pass along all sources to javac using an @file.
596    $$($1_BIN)/_the.$1_batch: $$($1_SRCS) $$($1_DEPENDS)
597	$(MKDIR) -p $$(@D)
598	$(RM) $$($1_BIN)/_the.$1_batch $$($1_BIN)/_the.$1_batch.tmp
599	$$(call ListPathsSafely,$1_SRCS,\n, >> $$($1_BIN)/_the.$1_batch.tmp)
600	$(ECHO) Compiling `$(WC) $$($1_BIN)/_the.$1_batch.tmp | $(TR) -s ' ' | $(CUT) -f 2 -d ' '` files for $1
601	($$($1_JVM) $$($1_JAVAC) $$($1_FLAGS) \
602	    -implicit:none \
603	    -d $$($1_BIN) $$($1_HEADERS_ARG) @$$($1_BIN)/_the.$1_batch.tmp && \
604	$(MV) $$($1_BIN)/_the.$1_batch.tmp $$($1_BIN)/_the.$1_batch)
605
606  endif
607
608  # Add all targets to main variable
609  $1 := $$($1_ALL_COPY_TARGETS) $$($1_ALL_COPY_CLEAN_TARGETS) $$($1_COMPILE_TARGETS) \
610      $$($1_HEADER_TARGETS)
611
612  # Check if a jar file was specified, then setup the rules for the jar.
613  ifneq (,$$($1_JAR))
614    # If no suffixes was explicitly set for this jar file.
615    # Use class and the cleaned/copied properties file suffixes as the default
616    # for the types of files to be put into the jar.
617    ifeq (,$$($1_SUFFIXES))
618      $1_SUFFIXES:=.class $$($1_CLEAN) $$($1_COPY)
619    endif
620
621    $$(eval $$(call SetupArchive,ARCHIVE_$1,$$($1), \
622        SRCS:=$$($1_BIN), \
623        SUFFIXES:=$$($1_SUFFIXES), \
624        EXCLUDE:=$$($1_EXCLUDES), \
625        INCLUDES:=$$($1_INCLUDES), \
626        EXTRA_FILES:=$$($1_ALL_COPY_TARGETS) $$($1_ALL_COPY_CLEAN_TARGETS), \
627        JAR:=$$($1_JAR), \
628        JARMAIN:=$$($1_JARMAIN), \
629        MANIFEST:=$$($1_MANIFEST), \
630        EXTRA_MANIFEST_ATTR:=$$($1_EXTRA_MANIFEST_ATTR), \
631        JARINDEX:=$$($1_JARINDEX), \
632        HEADERS:=$$($1_HEADERS), \
633        SETUP:=$$($1_SETUP)))
634
635    # Add jar to target list
636    $1 += $$($1_JAR)
637  endif
638
639  # Check if a srczip was specified, then setup the rules for the srczip.
640  ifneq (,$$($1_SRCZIP))
641    $$(eval $$(call SetupZipArchive,ARCHIVE_$1, \
642        SRC:=$$($1_SRC), \
643        ZIP:=$$($1_SRCZIP), \
644        INCLUDES:=$$($1_INCLUDES), \
645        EXCLUDES:=$$($1_EXCLUDES), \
646        EXCLUDE_FILES:=$$($1_EXCLUDE_FILES)))
647
648    # Add zip to target list
649    $1 += $$($1_SRCZIP)
650  endif
651endef
652
653# Use this macro to find the correct target to depend on when the original
654# SetupJavaCompilation is declared in a different makefile, to avoid having
655# to declare and evaluate it again.
656# param 1 is for example BUILD_MYPACKAGE
657# param 2 is the output directory (BIN)
658define SetupJavaCompilationCompileTarget
659  $(if $(findstring yes, $(ENABLE_SJAVAC)), $(strip $2)/_the.$(strip $1)_pubapi, \
660      $(strip $2)/_the.$(strip $1)_batch)
661endef
662endif
663