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 # When you read this source. Remember that $(sort ...) has the side effect
  27 # of removing duplicates. It is actually this side effect that is
  28 # desired whenever sort is used below!
  29 
  30 ifndef _JAVA_COMPILATION_GMK
  31 _JAVA_COMPILATION_GMK := 1
  32 
  33 ifeq (,$(_MAKEBASE_GMK))
  34   $(error You must include MakeBase.gmk prior to including JavaCompilation.gmk)
  35 endif
  36 
  37 # Java compilation needs SetupJarArchive and/or SetupZipArchive, if we're
  38 # generating a jar file or a source zip.
  39 include JarArchive.gmk
  40 include ZipArchive.gmk
  41 
  42 # Setup make rules for defining a Java compiler, which is needed to compile
  43 # Java code. This rule generates no output.
  44 #
  45 # Parameter 1 is the name of the compiler definition. This name needs to be
  46 # passed to SetupJavaCompilation. This name is used as variable prefix.
  47 #
  48 # Remaining parameters are named arguments. These include:
  49 #   JVM:=The jvm used to run the javac command
  50 #   JAVAC:=The javac jar and bootstrap classpath changes, or just bin/javac if JVM is left out
  51 #   FLAGS:=Flags to be supplied to javac
  52 #   SERVER_DIR:=Use a javac server (-XDserver) and store the server related files here
  53 #   SERVER_JVM:=Use this JVM for the server. Defaults to the JVM above.
  54 #   DISABLE_SJAVAC:=Set to true if this setup does not support sjavac
  55 SetupJavaCompiler = $(NamedParamsMacroTemplate)
  56 define SetupJavaCompilerBody
  57   # The port file contains the tcp/ip on which the server listens
  58   # and the cookie necessary to talk to the server.
  59   $1_SJAVAC_PORTFILE:=$$($1_SERVER_DIR)/server.port
  60   # You can use a different JVM to run the background javac server.
  61   ifeq ($$($1_SERVER_JVM),)
  62     # It defaults to the same JVM that is used to start the javac command.
  63     $1_SERVER_JVM:=$$($1_JVM)
  64   endif
  65 endef
  66 
  67 define add_file_to_copy
  68   # param 1 = BUILD_MYPACKAGE
  69   # parma 2 = The source file to copy.
  70   $2_TARGET:=$2
  71   # Remove the source prefix.
  72   $$(foreach i,$$($1_SRC),$$(eval $$(call remove_string,$$i,$2_TARGET)))
  73   # To allow for automatic overrides, do not create a rule for a target file that
  74   # already has one
  75   ifneq ($$($1_COPY_$$($2_TARGET)), 1)
  76     $1_COPY_$$($2_TARGET) := 1
  77     # Now we can setup the dependency that will trigger the copying.
  78     $$($1_BIN)$$($1_MODULE_SUBDIR)$$($2_TARGET) : $2
  79         $$(call LogInfo, Copying $$(patsubst $(OUTPUTDIR)/%,%, $$@))
  80         $$(install-file)
  81         $(CHMOD) -f ug+w $$@
  82 
  83     # And do not forget this target
  84     $1_ALL_COPY_TARGETS += $$($1_BIN)$$($1_MODULE_SUBDIR)$$($2_TARGET)
  85   endif
  86 endef
  87 
  88 # This macro is used only for properties files that are to be
  89 # copied over to the classes directory in cleaned form:
  90 # Previously this was inconsistently done in different repositories.
  91 # This is the new clean standard. Though it is to be superseded by
  92 # a standard annotation processor from with sjavac.
  93 #
  94 # An empty echo ensures that the input to sed always ends with a newline.
  95 # Certain implementations (e.g. Solaris) will skip the last line without
  96 # it.
  97 #
  98 # The sed expression does this:
  99 # 1. Add a backslash before any :, = or ! that do not have a backslash already.
 100 # 2. Apply the file unicode2x.sed which does a whole bunch of \u00XX to \xXX
 101 #    conversions.
 102 # 3. Delete all lines starting with #.
 103 # 4. Delete empty lines.
 104 # 5. Append lines ending with \ with the next line.
 105 # 6. Remove leading and trailing white space. Note that tabs must be explicit
 106 #    as sed on macosx does not understand '\t'.
 107 # 7. Replace the first \= with just =.
 108 # 8. Finally it's all sorted to create a stable output.
 109 #
 110 # It is assumed that = is the character used for separating names and values.
 111 define add_file_to_clean
 112   # param 1 = BUILD_MYPACKAGE
 113   # parma 2 = The source file to copy and clean.
 114   $2_TARGET:=$2
 115   # Remove the source prefix.
 116   $$(foreach i,$$($1_SRC),$$(eval $$(call remove_string,$$i,$2_TARGET)))
 117   # Now we can setup the depency that will trigger the copying.
 118   # To allow for automatic overrides, do not create a rule for a target file that
 119   # already has one
 120   ifneq ($$($1_CLEAN_$$($2_TARGET)), 1)
 121     $1_CLEAN_$$($2_TARGET) := 1
 122     $$($1_BIN)$$($1_MODULE_SUBDIR)$$($2_TARGET) : $2
 123         $$(call LogInfo, Cleaning $$(patsubst $(OUTPUTDIR)/%,%, $$@))
 124         $$(call MakeTargetDir)
 125         export LC_ALL=C ; ( $(CAT) $$< && $(ECHO) "" ) \
 126             | $(SED) -e 's/\([^\\]\):/\1\\:/g' -e 's/\([^\\]\)=/\1\\=/g' \
 127                 -e 's/\([^\\]\)!/\1\\!/g' -e 's/^[      ]*#.*/#/g' \
 128             | $(SED) -f "$(TOPDIR)/make/common/support/unicode2x.sed" \
 129             | $(SED) -e '/^#/d' -e '/^$$$$/d' \
 130                 -e :a -e '/\\$$$$/N; s/\\\n//; ta' \
 131                 -e 's/^[        ]*//;s/[        ]*$$$$//' \
 132                 -e 's/\\=/=/' \
 133             | $(SORT) > $$@
 134         $(CHMOD) -f ug+w $$@
 135 
 136     # And do not forget this target
 137     $1_ALL_COPY_CLEAN_TARGETS += $$($1_BIN)$$($1_MODULE_SUBDIR)$$($2_TARGET)
 138   endif
 139 endef
 140 
 141 define remove_string
 142   $2 := $$(subst $1,,$$($2))
 143 endef
 144 
 145 # Setup make rules for compiling Java source code to class files and/or a
 146 # resulting jar file.
 147 #
 148 # Parameter 1 is the name of the rule. This name is used as variable prefix,
 149 # and the targets generated are listed in a variable by that name.
 150 #
 151 # The target for public API digest is returned in $1_API_TARGET.
 152 #
 153 # Remaining parameters are named arguments. These include:
 154 #   SETUP:=must point to a previously setup java compiler, for example: SETUP:=BOOTJAVAC
 155 #   JVM:=path to ..bin/java
 156 #   ADD_JAVAC_FLAGS:=javac flags to append to the default ones.
 157 #   SRC:=one or more directories to search for sources. The order of the source roots
 158 #        is significant. The first found file of a certain name has priority.
 159 #   BIN:=store classes here
 160 #   MODULE:=Name of module being compiled. If set, classes are put in BIN/MODULE.
 161 #   CLASSPATH:=a list of additional entries to set as classpath to javac
 162 #   INCLUDES:=myapp.foo means will only compile java files in myapp.foo or any of its sub-packages.
 163 #   EXCLUDES:=myapp.foo means will do not compile java files in myapp.foo or any of its sub-packages.
 164 #   COPY:=.prp means copy all prp files to the corresponding package in BIN.
 165 #   COPY_FILES:=myapp/foo/setting.txt means copy this file over to the package myapp/foo
 166 #   CLEAN:=.properties means copy and clean all properties file to the corresponding package in BIN.
 167 #   CLEAN_FILES:=myapp/foo/setting.txt means clean this file over to the package myapp/foo
 168 #   SRCZIP:=Create a src.zip based on the found sources and copied files.
 169 #   INCLUDE_FILES:="com/sun/SolarisFoobar.java" means only compile this file!
 170 #   EXCLUDE_FILES:="com/sun/SolarisFoobar.java" means do not compile this particular file!
 171 #       "SolarisFoobar.java" means do not compile SolarisFoobar, wherever it is found.
 172 #   EXTRA_FILES:=List of extra source files to include in compilation. Can be used to
 173 #       specify files that need to be generated by other rules first.
 174 #   HEADERS:=path to directory where all generated c-headers are written.
 175 #   DEPENDS:=Extra dependecy
 176 #   DISABLE_SJAVAC:=Explicitly disable the use of sjavac for this compilation unit.
 177 #   KEEP_DUPS:=Do not remove duplicate file names from different source roots.
 178 #   FAIL_NO_SRC:=Set to false to not fail the build if no source files are found,
 179 #        default is true.
 180 #   DEBUG_SYMBOLS:=Set to false to disable generation of debug symbols.
 181 #   CREATE_API_DIGEST:=Set to true to use a javac plugin to generate a public API
 182 #        hash which can be used for down stream dependencies to only rebuild
 183 #        when the API changes. Implicitly used in sjavac.
 184 #   KEEP_ALL_TRANSLATIONS:=Set to true to skip translation filtering
 185 SetupJavaCompilation = $(NamedParamsMacroTemplate)
 186 define SetupJavaCompilationBody
 187 
 188   # Verify arguments
 189   ifeq ($$($1_BIN),)
 190     $$(error Must specify BIN (in $1))
 191   endif
 192 
 193   # Extract the info from the java compiler setup.
 194   $1_JVM := $$($$($1_SETUP)_JVM)
 195   $1_JAVAC := $$($$($1_SETUP)_JAVAC)
 196   $1_FLAGS :=
 197   ifneq ($$($1_DEBUG_SYMBOLS), false)
 198     $1_FLAGS := -g
 199   endif
 200   $1_FLAGS += $$($$($1_SETUP)_FLAGS) $$($1_ADD_JAVAC_FLAGS) $(JAVAC_FLAGS)
 201   ifneq ($$($1_CLASSPATH), )
 202     $1_FLAGS += -cp $$(call PathList, $$($1_CLASSPATH))
 203   endif
 204   ifeq ($$($1_JAVAC),)
 205     $$(error The Java compilation $1 refers to a non-existant java compiler setup $$($1_SETUP))
 206   endif
 207   $1_SJAVAC_PORTFILE := $$($$($1_SETUP)_SJAVAC_PORTFILE)
 208   $1_SERVER_JVM := $$($$($1_SETUP)_SERVER_JVM)
 209   $1_DISABLE_SJAVAC := $$($$($1_SETUP)_DISABLE_SJAVAC)
 210 
 211   ifneq ($$($1_MODULE), )
 212     $1_MODULE_SUBDIR := /$$($1_MODULE)
 213   endif
 214 
 215   # Make sure the dirs exist, or that one of the EXTRA_FILES, that may not
 216   # exist yet, is in it.
 217   $$(foreach d, $$($1_SRC), \
 218     $$(if $$(wildcard $$d), , \
 219       $$(if $$(filter $$d%, $$($1_EXTRA_FILES)), , \
 220         $$(error SRC specified to SetupJavaCompilation $1 contains missing directory >$$d<) \
 221       ) \
 222     ) \
 223   )
 224   $$(call MakeDir,$$($1_BIN))
 225   # Order src files according to the order of the src dirs. Correct odering is
 226   # needed for correct overriding between different source roots.
 227   $1_ALL_SRC_RAW := $$(call FindFiles, $$($1_SRC))
 228   $1_ALL_SRCS := $$($1_EXTRA_FILES) \
 229       $$(foreach d, $$($1_SRC), $$(filter $$d%, $$($1_ALL_SRC_RAW)))
 230 
 231   # Extract the java files.
 232   $1_SRCS := $$(filter %.java, $$($1_ALL_SRCS))
 233 
 234   # Translate include/exclude into patterns
 235   ifneq ($$($1_EXCLUDE_FILES), )
 236     $1_EXCLUDE_PATTERN := $$(addprefix %, $$($1_EXCLUDE_FILES))
 237   endif
 238   ifneq ($$($1_INCLUDE_FILES), )
 239     $1_INCLUDE_PATTERN := $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$($1_INCLUDE_FILES)))
 240   endif
 241   ifneq ($$($1_EXCLUDES), )
 242     $1_EXCLUDE_PATTERN += $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$(addsuffix /%, $$($1_EXCLUDES))))
 243   endif
 244   ifneq ($$($1_INCLUDES), )
 245     $1_INCLUDE_PATTERN += $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$(addsuffix /%, $$($1_INCLUDES))))
 246   endif
 247 
 248   # Apply include/exclude patterns to java sources
 249   ifneq ($$($1_EXCLUDE_PATTERN), )
 250     $1_SRCS := $$(filter-out $$($1_EXCLUDE_PATTERN), $$($1_SRCS))
 251   endif
 252   ifneq ($$($1_INCLUDE_PATTERN), )
 253     $1_SRCS := $$(filter $$($1_INCLUDE_PATTERN) $$($1_EXTRA_FILES), $$($1_SRCS))
 254   endif
 255 
 256   ifneq ($$($1_KEEP_DUPS), true)
 257     # Remove duplicate source files by keeping the first found of each duplicate.
 258     # This allows for automatic overrides with custom or platform specific versions
 259     # source files.
 260     #
 261     # For the smart javac wrapper case, add each removed file to an extra exclude
 262     # file list to prevent sjavac from finding duplicate sources.
 263     $1_SRCS := $$(strip $$(foreach s, $$($1_SRCS), \
 264         $$(eval relative_src := $$(call remove-prefixes, $$($1_SRC), $$(s))) \
 265         $$(if $$($1_$$(relative_src)), \
 266           $$(eval $1_SJAVAC_EXCLUDE_FILES += $$(s)), \
 267           $$(eval $1_$$(relative_src) := 1) $$(s))))
 268   endif
 269 
 270   # Filter out any excluded translations
 271   ifneq ($$($1_KEEP_ALL_TRANSLATIONS), true)
 272     $1_SRCS := $$(call FilterExcludedTranslations, $$($1_SRCS), .java)
 273   endif
 274 
 275   ifeq ($$(strip $$($1_SRCS)), )
 276     ifneq ($$($1_FAIL_NO_SRC), false)
 277       $$(error No source files found for $1)
 278     endif
 279   else
 280 
 281     $1_SAFE_NAME := $$(strip $$(subst /,_, $1))
 282 
 283     # All files below META-INF are always copied.
 284     $1_ALL_COPIES := $$(filter $$(addsuffix /META-INF%,$$($1_SRC)),$$($1_ALL_SRCS))
 285     # Find all files to be copied from source to bin.
 286     ifneq (,$$($1_COPY)$$($1_COPY_FILES))
 287       # Search for all files to be copied.
 288       $1_ALL_COPIES += $$(filter $$(addprefix %,$$($1_COPY)),$$($1_ALL_SRCS))
 289       # Copy these explicitly
 290       $1_ALL_COPIES += $$($1_COPY_FILES)
 291     endif
 292     # Copy must also respect filters.
 293     ifneq (,$$($1_INCLUDE_PATTERN))
 294       $1_ALL_COPIES := $$(filter $$($1_INCLUDE_PATTERN),$$($1_ALL_COPIES))
 295     endif
 296     ifneq (,$$($1_EXCLUDE_PATTERN))
 297       $1_ALL_COPIES := $$(filter-out $$($1_EXCLUDE_PATTERN),$$($1_ALL_COPIES))
 298     endif
 299     # Filter out any excluded translations
 300     ifneq ($$($1_KEEP_ALL_TRANSLATIONS), true)
 301       $1_ALL_COPIES := $$(call FilterExcludedTranslations, $$($1_ALL_COPIES), .properties)
 302     endif
 303     ifneq (,$$($1_ALL_COPIES))
 304       # Yep, there are files to be copied!
 305       $1_ALL_COPY_TARGETS:=
 306           $$(foreach i,$$($1_ALL_COPIES),$$(eval $$(call add_file_to_copy,$1,$$i)))
 307       # Now we can depend on $$($1_ALL_COPY_TARGETS) to copy all files!
 308     endif
 309 
 310     # Find all property files to be copied and cleaned from source to bin.
 311     ifneq (,$$($1_CLEAN)$$($1_CLEAN_FILES))
 312       # Search for all files to be copied.
 313       $1_ALL_CLEANS := $$(filter $$(addprefix %,$$($1_CLEAN)),$$($1_ALL_SRCS))
 314       # Clean these explicitly
 315       $1_ALL_CLEANS += $$($1_CLEAN_FILES)
 316       # Copy and clean must also respect filters.
 317       ifneq (,$$($1_INCLUDE_PATTERN))
 318         $1_ALL_CLEANS := $$(filter $$($1_INCLUDE_PATTERN),$$($1_ALL_CLEANS))
 319       endif
 320       ifneq (,$$($1_EXCLUDE_PATTERN))
 321         $1_ALL_CLEANS := $$(filter-out $$($1_EXCLUDE_PATTERN),$$($1_ALL_CLEANS))
 322       endif
 323       # Filter out any excluded translations
 324       ifneq ($$($1_KEEP_ALL_TRANSLATIONS), true)
 325         $1_ALL_CLEANS := $$(call FilterExcludedTranslations, $$($1_ALL_CLEANS), .properties)
 326       endif
 327       ifneq (,$$($1_ALL_CLEANS))
 328         # Yep, there are files to be copied and cleaned!
 329         $1_ALL_COPY_CLEAN_TARGETS:=
 330             $$(foreach i,$$($1_ALL_CLEANS),$$(eval $$(call add_file_to_clean,$1,$$i)))
 331         # Now we can depend on $$($1_ALL_COPY_CLEAN_TARGETS) to copy all files!
 332       endif
 333     endif
 334 
 335     # Create a sed expression to remove the source roots and to replace / with .
 336     # and remove .java at the end.
 337     $1_REWRITE_INTO_CLASSES:=$$(foreach i,$$($1_SRC),-e 's|$$i/||g') -e 's|/|.|g' -e 's|.java$$$$||g'
 338 
 339     # Create SJAVAC variable from JAVAC variable. Expects $1_JAVAC to be
 340     # "bootclasspathprepend -cp .../javac.jar com.sun.tools.javac.Main"
 341     # and javac is simply replaced with sjavac.
 342     $1_SJAVAC:=$$(subst com.sun.tools.javac.Main,com.sun.tools.sjavac.Main,$$($1_JAVAC))
 343 
 344     # Set the $1_REMOTE to spawn a background javac server.
 345     $1_REMOTE:=--server:portfile=$$($1_SJAVAC_PORTFILE),id=$1,sjavac=$$(subst \
 346         $$(SPACE),%20,$$(subst $$(COMMA),%2C,$$(strip $$($1_SERVER_JVM) $$($1_SJAVAC))))
 347 
 348     $1_COMPILE_TARGET := $$($1_BIN)$$($1_MODULE_SUBDIR)/_the.$1_batch
 349     $1_API_TARGET := $$($1_BIN)$$($1_MODULE_SUBDIR)/_the.$1_pubapi
 350 
 351     ifeq ($$($1_DISABLE_SJAVAC)x$$(ENABLE_SJAVAC),xyes)
 352       # Using sjavac to compile.
 353 
 354       # Create the sjavac wrapper command line. Sjavac doesn't handle patterns that
 355       # match the absolute path, only the part inside each src dir. Instead -i and
 356       # -x flags apply only to the next -src arg on the command line.
 357       $1_EXCLUDE_FILES_ABS := $$(filter /%, $$($1_EXCLUDE_FILES)) $$($1_SJAVAC_EXCLUDE_FILES)
 358       $1_EXCLUDE_FILES_REL := $$(filter-out /%, $$($1_EXCLUDE_FILES))
 359       $1_SJAVAC_ARGS_STRING := $$(foreach s, $$(patsubst %/, %, $$($1_SRC)), \
 360           $$(addprefix -x ,$$(addsuffix /**,$$($1_EXCLUDES))) \
 361           $$(addprefix -i ,$$(addsuffix /**,$$($1_INCLUDES))) \
 362           $$(addprefix -x **,$$(strip $$($1_EXCLUDE_FILES_REL))) \
 363           $$(addprefix -i **,$$(strip $$($1_INCLUDE_FILES))) \
 364           $$(addprefix -x , $$(strip $$(patsubst $$(s)/%, %, $$(filter $$(s)/%, $$($1_EXCLUDE_FILES_ABS))))) \
 365           -src $$(s))
 366 
 367       ifneq ($$(word 20, $$($1_SJAVAC_ARGS_STRING)), )
 368         $1_SJAVAC_ARGS_FILE := $$($1_BIN)/_the.$1_args
 369         $1_SJAVAC_ARGS := @$$($1_SJAVAC_ARGS_FILE)
 370       else
 371         $1_SJAVAC_ARGS := $$($1_SJAVAC_ARGS_STRING)
 372       endif
 373 
 374 
 375       ifneq (,$$($1_HEADERS))
 376         $1_HEADERS_ARG := -h $$($1_HEADERS)
 377       endif
 378 
 379       $1_VARDEPS := $$($1_JVM) $$($1_SJAVAC) $$($1_SJAVAC_ARGS_STRING) $$($1_FLAGS) \
 380           $$($1_HEADERS_ARG) $$($1_BIN) $$($1_EXCLUDES) $$($1_INCLUDES) \
 381           $$($1_EXCLUDE_FILES) $$($1_INCLUDE_FILES)
 382       $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
 383           $$($1_BIN)$$($1_MODULE_SUBDIR)/_the.$1.vardeps)
 384 
 385       $$($1_COMPILE_TARGET): $$($1_SRCS) $$($1_DEPENDS) $$($1_VARDEPS_FILE)
 386                 $$(call MakeDir, $$(@D) $$(dir $$($1_SJAVAC_PORTFILE)))
 387                 $$(eval $$(call ListPathsSafely,$1_SRCS, $$@.tmp))
 388                 ifneq ($$($1_SJAVAC_ARGS_FILE), )
 389                   $$(eval $$(call ListPathsSafely,$1_SJAVAC_ARGS_STRING, $$($1_SJAVAC_ARGS_FILE)))
 390                 endif
 391                 $$(call LogWarn, Compiling $1)
 392                 $$(call ExecuteWithLog, $$($1_BIN)$$($1_MODULE_SUBDIR)/_the.$$($1_SAFE_NAME)_batch, \
 393                     $$($1_JVM) $$($1_SJAVAC) \
 394                         $$($1_REMOTE) \
 395                         -j 1 \
 396                         --permit-unidentified-artifacts \
 397                         --permit-sources-without-package \
 398                         --compare-found-sources $$@.tmp \
 399                         --log=$(LOG_LEVEL) \
 400                         --state-dir=$$($1_BIN)$$($1_MODULE_SUBDIR) \
 401                         $$($1_SJAVAC_ARGS) \
 402                         $$($1_FLAGS) \
 403                         $$($1_HEADERS_ARG) \
 404                         -d $$($1_BIN)) && \
 405                 $(MV) $$@.tmp $$@
 406                 # Create a pubapi file that only changes when the pubapi changes. Dependent
 407                 # compilations can use this file to only get recompiled when pubapi has changed.
 408                 # Grep returns 1 if no matching lines are found. Do not fail for this.
 409                 $(GREP) -e "^I" $$($1_BIN)$$($1_MODULE_SUBDIR)/javac_state \
 410                     > $$($1_API_TARGET).tmp || test "$$$$?" = "1"
 411                 if [ ! -f $$($1_API_TARGET) ] \
 412                     || [ "`$(DIFF) $$($1_API_TARGET) $$($1_API_TARGET).tmp`" != "" ]; then \
 413                   $(MV) $$($1_API_TARGET).tmp $$($1_API_TARGET); \
 414                 fi
 415 
 416     else
 417       # Using plain javac to batch compile everything.
 418 
 419       # When building in batch, put headers in a temp dir to filter out those that actually
 420       # changed before copying them to the real header dir.
 421       ifneq (,$$($1_HEADERS))
 422         $1_HEADERS_ARG := -h $$($1_HEADERS).$1.tmp
 423 
 424         $$($1_HEADERS)/_the.$1_headers: $$($1_COMPILE_TARGET)
 425                 $$(call MakeTargetDir)
 426                 if [ -d "$$($1_HEADERS).$1.tmp" ]; then \
 427                   for f in `$(CD) $$($1_HEADERS).$1.tmp && $(FIND) . -type f`; do \
 428                     if [ ! -f "$$($1_HEADERS)/$$$$f" ] \
 429                         || [ "`$(DIFF) $$($1_HEADERS)/$$$$f $$($1_HEADERS).$1.tmp/$$$$f`" != "" ]; then \
 430                       $(MKDIR) -p `$(DIRNAME) $$($1_HEADERS)/$$$$f`; \
 431                       $(CP) -f $$($1_HEADERS).$1.tmp/$$$$f $$($1_HEADERS)/$$$$f; \
 432                     fi; \
 433                   done; \
 434                 fi
 435                 $(RM) -r $$($1_HEADERS).$1.tmp
 436                 $(TOUCH) $$@
 437 
 438         $1_HEADER_TARGETS := $$($1_HEADERS)/_the.$1_headers
 439       endif
 440 
 441       $1_VARDEPS := $$($1_JVM) $$($1_JAVAC) $$($1_FLAGS) $$($1_BIN) \
 442           $$($1_HEADERS_ARG) $$($1_EXCLUDES) $$($1_INCLUDES) \
 443           $$($1_EXCLUDE_FILES) $$($1_INCLUDE_FILES)
 444       $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
 445           $$($1_BIN)$$($1_MODULE_SUBDIR)/_the.$1.vardeps)
 446 
 447       ifeq ($$($1_DISABLE_SJAVAC)x$(ENABLE_JAVAC_SERVER), xyes)
 448         $1_JAVAC_CMD := $$($1_SJAVAC) $$($1_REMOTE)
 449       else
 450         $1_JAVAC_CMD := $$($1_JAVAC)
 451       endif
 452 
 453       ifeq ($$($1_CREATE_API_DIGEST), true)
 454         $1_API_DIGEST_FLAGS := \
 455             -classpath $(BUILDTOOLS_OUTPUTDIR)/depend \
 456             -Xplugin:"depend $$($1_API_TARGET)" \
 457             #
 458 
 459         $1_EXTRA_DEPS := $(BUILDTOOLS_OUTPUTDIR)/depend/_the.COMPILE_DEPEND_batch
 460       endif
 461 
 462       # When not using sjavac, pass along all sources to javac using an @file.
 463       $$($1_COMPILE_TARGET): $$($1_SRCS) $$($1_DEPENDS) $$($1_VARDEPS_FILE) \
 464           $$($1_EXTRA_DEPS)
 465                 $$(call MakeDir, $$(@D))
 466                 $$(eval $$(call ListPathsSafely,$1_SRCS, $$@.tmp))
 467                 $$(call LogWarn, Compiling $$(words $$($1_SRCS)) files for $1)
 468                 $$(call ExecuteWithLog, $$($1_BIN)$$($1_MODULE_SUBDIR)/_the.$$($1_SAFE_NAME)_batch, \
 469                     $$($1_JVM) $$($1_JAVAC_CMD) $$($1_FLAGS) \
 470                         -implicit:none \
 471                         $$($1_API_DIGEST_FLAGS) \
 472                         -d $$($1_BIN) $$($1_HEADERS_ARG) @$$@.tmp) && \
 473                 $(MV) $$@.tmp $$@
 474     endif
 475 
 476     # Add all targets to main variable
 477     $1 := $$($1_ALL_COPY_TARGETS) $$($1_ALL_COPY_CLEAN_TARGETS) $$($1_COMPILE_TARGET) \
 478         $$($1_HEADER_TARGETS)
 479 
 480     # Check if a jar file was specified, then setup the rules for the jar.
 481     ifneq (,$$($1_JAR))
 482       # If no suffixes was explicitly set for this jar file.
 483       # Use class and the cleaned/copied properties file suffixes as the default
 484       # for the types of files to be put into the jar.
 485       ifeq (,$$($1_SUFFIXES))
 486         $1_SUFFIXES:=.class $$($1_CLEAN) $$($1_COPY)
 487       endif
 488 
 489       $$(eval $$(call SetupJarArchive, ARCHIVE_$1, \
 490           DEPENDENCIES:=$$($1), \
 491           SRCS:=$$($1_BIN)$$($1_MODULE_SUBDIR), \
 492           SUFFIXES:=$$($1_SUFFIXES), \
 493           EXCLUDE:=$$($1_EXCLUDES), \
 494           INCLUDES:=$$($1_INCLUDES), \
 495           EXTRA_FILES:=$$($1_ALL_COPY_TARGETS) $$($1_ALL_COPY_CLEAN_TARGETS), \
 496           JAR:=$$($1_JAR), \
 497           JARMAIN:=$$($1_JARMAIN), \
 498           MANIFEST:=$$($1_MANIFEST), \
 499           EXTRA_MANIFEST_ATTR:=$$($1_EXTRA_MANIFEST_ATTR), \
 500           JARINDEX:=$$($1_JARINDEX), \
 501           HEADERS:=$$($1_HEADERS), \
 502           SETUP:=$$($1_SETUP), \
 503       ))
 504 
 505       # Add jar to target list
 506       $1 += $$($1_JAR)
 507     endif
 508 
 509     # Check if a srczip was specified, then setup the rules for the srczip.
 510     ifneq (,$$($1_SRCZIP))
 511       $$(eval $$(call SetupZipArchive, ZIP_ARCHIVE_$1, \
 512           SRC:=$$($1_SRC), \
 513           ZIP:=$$($1_SRCZIP), \
 514           INCLUDES:=$$($1_INCLUDES), \
 515           EXCLUDES:=$$($1_EXCLUDES), \
 516           EXCLUDE_FILES:=$$($1_EXCLUDE_FILES)))
 517 
 518       # Add zip to target list
 519       $1 += $$($1_SRCZIP)
 520     endif
 521   endif # Source files found
 522 endef
 523 
 524 # Use this macro to find the correct target to depend on when the original
 525 # SetupJavaCompilation is declared in a different makefile, to avoid having
 526 # to declare and evaluate it again.
 527 # param 1 is for example BUILD_MYPACKAGE
 528 # param 2 is the output directory (BIN)
 529 SetupJavaCompilationCompileTarget = \
 530     $(strip $2)/_the.$(strip $1)_batch
 531 
 532 SetupJavaCompilationApiTarget = \
 533     $(strip $2)/_the.$(strip $1)_pubapi
 534 
 535 endif # _JAVA_COMPILATION_GMK