JavaCompilation.gmk revision 1660:216f63a10515
1#
2# Copyright (c) 2011, 2015, 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# This makefile is much simpler now that it can use the smart javac wrapper
27# for dependency tracking between java packages and incremental compiles.
28# It could be even more simple if we added support for incremental jar updates
29# directly from the smart javac wrapper.
30
31# Cleaning/copying properties here is not a good solution. The properties
32# should be cleaned/copied by a annotation processor in sjavac.
33
34# When you read this source. Remember that $(sort ...) has the side effect
35# of removing duplicates. It is actually this side effect that is
36# desired whenever sort is used below!
37
38ifndef _JAVA_COMPILATION_GMK
39_JAVA_COMPILATION_GMK := 1
40
41ifeq (,$(_MAKEBASE_GMK))
42  $(error You must include MakeBase.gmk prior to including JavaCompilation.gmk)
43endif
44
45# Java compilation needs SetupZipArchive if we're generating a source zip.
46include ZipArchive.gmk
47
48FALSE_FIND_PATTERN:=-name FILE_NAME_THAT_DOESNT_EXIST
49
50# Setup make rules for defining a Java compiler, which is needed to compile
51# Java code. This rule generates no output.
52#
53# Parameter 1 is the name of the compiler definition. This name needs to be
54# passed to SetupJavaCompilation. This name is used as variable prefix.
55#
56# Remaining parameters are named arguments. These include:
57#   JVM:=The jvm used to run the javac/javah command
58#   JAVAC:=The javac jar and bootstrap classpath changes, or just bin/javac if JVM is left out
59#   FLAGS:=Flags to be supplied to javac
60#   SERVER_DIR:=Use a javac server (-XDserver) and store the server related files here
61#   SERVER_JVM:=Use this JVM for the server. Defaults to the JVM above.
62SetupJavaCompiler = $(NamedParamsMacroTemplate)
63define SetupJavaCompilerBody
64  # The port file contains the tcp/ip on which the server listens
65  # and the cookie necessary to talk to the server.
66  $1_SJAVAC_PORTFILE:=$$($1_SERVER_DIR)/server.port
67  # You can use a different JVM to run the background javac server.
68  ifeq ($$($1_SERVER_JVM),)
69    # It defaults to the same JVM that is used to start the javac command.
70    $1_SERVER_JVM:=$$($1_JVM)
71  endif
72endef
73
74# Setup make rules for creating a jar archive.
75#
76# Parameter 1 is the name of the rule. This name is used as variable prefix,
77# and the targets generated are listed in a variable by that name.
78#
79# Remaining parameters are named arguments. These include:
80#   DEPENDENCIES:=List of dependencies for the jar target. If left empty,
81#       dependencies are calculated automatically from the source files found.
82#       For this to work, the source files must exist when the makefile is
83#       parsed.
84#   SRCS:=List of directories in where to find files to add to archive
85#   SUFFIXES:=File suffixes to include in jar
86#   INCLUDES:=List of directories/packages in SRCS that should be included
87#   EXCLUDES:=List of directories/packages in SRCS that should be excluded
88#   EXCLUDE_FILES:=List of files in SRCS that should be excluded
89#   EXTRA_FILES:=List of files in SRCS that should be included regardless of suffix match.
90#   JAR:=Jar file to create
91#   MANIFEST:=Optional manifest file template.
92#   JARMAIN:=Optional main class to add to manifest
93#   JARINDEX:=true means generate the index in the jar file.
94#   SKIP_METAINF:=Set to prevent contents of an META-INF directory to be automatically
95#       added to the archive.
96#   EXTRA_MANIFEST_ATTR:=Extra attribute to add to manifest.
97#   CHECK_COMPRESS_JAR Check the COMPRESS_JAR variable
98SetupArchive = $(NamedParamsMacroTemplate)
99define SetupArchiveBody
100
101  $1_JARMAIN:=$(strip $$($1_JARMAIN))
102  $1_JARNAME:=$$(notdir $$($1_JAR))
103  $1_MANIFEST_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_manifest
104  $1_DELETESS_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletess
105  $1_DELETES_FILE:=$$(dir $$($1_JAR))_the.$$($1_JARNAME)_deletes
106  $1_BIN:=$$(dir $$($1_JAR))
107
108  ifeq (,$$($1_SUFFIXES))
109    # No suffix was set, default to classes.
110    $1_SUFFIXES:=.class
111  endif
112  # Convert suffixes to a find expression
113  $1_FIND_PATTERNS:=$(FALSE_FIND_PATTERN) $$(patsubst %,$(SPACE)-o$(SPACE)-name$(SPACE)$(DQUOTE)*%$(DQUOTE),$$($1_SUFFIXES))
114  # On windows, a lot of includes/excludes risk making the command line too long, so
115  # writing the grep patterns to files.
116  # Grep returns 1 if nothing is matched. Do not fail the build for this.
117  ifneq (,$$($1_INCLUDES))
118    $1_GREP_INCLUDE_PATTERNS:=$$(call EscapeDollar, \
119        $$(foreach src,$$($1_SRCS), $$(addprefix $$(src)/,$$($1_INCLUDES))))
120    # If there are a lot of include patterns, output to file to shorten command lines
121    ifeq ($$(word 20,$$($1_GREP_INCLUDE_PATTERNS)),)
122      $1_GREP_INCLUDES:=| ( $(GREP) $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_INCLUDE_PATTERNS)) \
123          || test "$$$$?" = "1" )
124    else
125      $1_GREP_INCLUDE_OUTPUT = \
126          $$(eval $$(call ListPathsSafely,$1_GREP_INCLUDE_PATTERNS, \
127              $$($1_BIN)/_the.$$($1_JARNAME)_include))
128      $1_GREP_INCLUDES:=| ( $(GREP) -f $$($1_BIN)/_the.$$($1_JARNAME)_include \
129          || test "$$$$?" = "1" )
130    endif
131  endif
132  ifneq (,$$($1_EXCLUDES)$$($1_EXCLUDE_FILES))
133    $1_GREP_EXCLUDE_PATTERNS:=$$(call EscapeDollar, \
134        $$(foreach src,$$($1_SRCS),$$(addprefix $$(src)/, \
135        $$($1_EXCLUDES) $$($1_EXCLUDE_FILES))))
136    # If there are a lot of include patterns, output to file to shorten command lines
137    ifeq ($$(word 20,$$($1_GREP_EXCLUDE_PATTERNS)),)
138      $1_GREP_EXCLUDES:=| ( $(GREP) -v $$(patsubst %,$(SPACE)-e$(SPACE)$(DQUOTE)%$(DQUOTE),$$($1_GREP_EXCLUDE_PATTERNS)) \
139          || test "$$$$?" = "1" )
140    else
141      $1_GREP_EXCLUDE_OUTPUT = \
142          $$(eval $$(call ListPathsSafely,$1_GREP_EXCLUDE_PATTERNS, \
143              $$($1_BIN)/_the.$$($1_JARNAME)_exclude))
144      $1_GREP_EXCLUDES:=| ( $(GREP) -v -f $$($1_BIN)/_the.$$($1_JARNAME)_exclude \
145          || test "$$$$?" = "1" )
146    endif
147  endif
148
149  # Check if this jar needs to have its index generated.
150  ifneq (,$$($1_JARINDEX))
151    $1_JARINDEX = (cd $$(dir $$@) && $(JAR) -i $$(notdir $$@))
152  else
153    $1_JARINDEX = true
154  endif
155  # When this macro is run in the same makefile as the java compilation, dependencies are
156  # transfered in make variables. When the macro is run in a different makefile than the
157  # java compilation, the dependencies need to be found in the filesystem.
158  $1_ORIG_DEPS := $$($1_DEPENDENCIES)
159  ifeq ($$($1_DEPENDENCIES), )
160    # Add all source roots to the find cache since we are likely going to run find
161    # on these more than once. The cache will only be updated if necessary.
162    $$(eval $$(call FillCacheFind, $$($1_FIND_LIST)))
163    $1_DEPENDENCIES:=$$(filter $$(addprefix %,$$($1_SUFFIXES)), \
164        $$(call CacheFind,$$($1_SRCS)))
165    ifneq (,$$($1_GREP_INCLUDE_PATTERNS))
166      $1_DEPENDENCIES:=$$(filter $$(addsuffix %,$$($1_GREP_INCLUDE_PATTERNS)),$$($1_DEPENDENCIES))
167    endif
168    ifneq (,$$($1_GREP_EXCLUDE_PATTERNS))
169      $1_DEPENDENCIES:=$$(filter-out $$(addsuffix %,$$($1_GREP_EXCLUDE_PATTERNS)),$$($1_DEPENDENCIES))
170    endif
171    # Look for EXTRA_FILES in all SRCS dirs and as absolute paths.
172    $1_DEPENDENCIES+=$$(wildcard $$(foreach src, $$($1_SRCS), \
173        $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES))
174    ifeq (,$$($1_SKIP_METAINF))
175      $1_DEPENDENCIES+=$$(call CacheFind,$$(wildcard $$(addsuffix /META-INF,$$($1_SRCS))))
176    endif
177  endif
178  # The dependency list should never be empty
179  ifeq ($$(strip $$($1_DEPENDENCIES)), )
180    $$(warning No dependencies found for $1)
181  endif
182
183  # Utility macros, to make the shell script receipt somewhat easier to decipher.
184
185  # Capture extra files is the same for both CAPTURE_CONTENTS and SCAPTURE_CONTENTS so
186  # only define it once to avoid duplication.
187  # The list of extra files might be long, so need to use ListPathsSafely to print
188  # them out to a separte file. Then process the contents of that file to rewrite
189  # into -C <dir> <file> lines.
190  # The EXTRA_FILES_RESOLVED varible must be set in the macro so that it's evaluated
191  # in the recipe when the files are guaranteed to exist.
192  $1_CAPTURE_EXTRA_FILES=\
193      $$(eval $1_EXTRA_FILES_RESOLVED:=$$(call DoubleDollar, \
194          $$(wildcard $$(foreach src, $$($1_SRCS), \
195          $$(addprefix $$(src)/, $$($1_EXTRA_FILES))) $$($1_EXTRA_FILES)))) \
196      $$(if $$($1_EXTRA_FILES_RESOLVED), \
197        $$(eval $$(call ListPathsSafely,$1_EXTRA_FILES_RESOLVED, \
198            $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra)) \
199        $(SED) $$(foreach src,$$($1_SRCS), -e 's|$$(src)/|-C $$(src) |g') \
200            $$($1_BIN)/_the.$$($1_JARNAME)_contents.extra \
201            >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE))
202
203  # The capture contents macro finds all files (matching the patterns, typically
204  # .class and .prp) that are newer than the jar-file, ie the new content to be put into the jar.
205  # NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
206  $1_CAPTURE_CONTENTS=\
207      $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
208      $$(foreach src,$$($1_SRCS), \
209        $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) -a -newer $$@ $$($1_GREP_INCLUDES) \
210          $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
211        >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
212      $$($1_CAPTURE_EXTRA_FILES)
213
214  # The capture metainf macro finds all files below the META-INF directory that are newer than the jar-file.
215  # Find returns non zero if the META-INF dir does not exist, ignore this.
216  ifeq (,$$($1_SKIP_METAINF))
217    $1_CAPTURE_METAINF =$$(foreach src,$$($1_SRCS), \
218        ( ( $(FIND) $$(src)/META-INF -type f -a -newer $$@ 2> /dev/null || true ) \
219            | $(SED) 's|$$(src)/|-C $$(src) |g' >> \
220        $$($1_BIN)/_the.$$($1_JARNAME)_contents ) $$(NEWLINE) )
221  endif
222  # The capture deletes macro finds all deleted files and concatenates them. The resulting file
223  # tells us what to remove from the jar-file.
224  $1_CAPTURE_DELETES=$$(foreach src,$$($1_SRCS),($(FIND) $$(src) -name _the.package.deleted -newer $$@ \
225      -exec $(SED) 's|$$(src)||g' \{\} >> $$($1_DELETES_FILE) \;) $$(NEWLINE))
226  # The update contents macro updates the jar file with the previously capture contents.
227  # Use 'wc -w' to see if the contents file is empty.
228  $1_UPDATE_CONTENTS=\
229      if [ "`$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'`" -gt "0" ]; then \
230        $(ECHO) "  updating" `$(WC) -l $$($1_BIN)/_the.$$($1_JARNAME)_contents | $(AWK) '{ print $$$$1 }'` files && \
231        $(JAR) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents; \
232      fi $$(NEWLINE)
233  # The s-variants of the above macros are used when the jar is created from scratch.
234  # NOTICE: please leave the parentheses space separated otherwise the AIX build will break!
235  $1_SCAPTURE_CONTENTS=\
236      $(RM) $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE) \
237      $$(foreach src,$$($1_SRCS), \
238        $(FIND) $$(src) -type f -a \( $$($1_FIND_PATTERNS) \) $$($1_GREP_INCLUDES) \
239            $$($1_GREP_EXCLUDES) | $(SED) 's|$$(src)/|-C $$(src) |g' \
240            >> $$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)) \
241      $$($1_CAPTURE_EXTRA_FILES)
242
243  # Find returns non zero if the META-INF dir does not exist, ignore this.
244  ifeq (,$$($1_SKIP_METAINF))
245    $1_SCAPTURE_METAINF=$$(foreach src,$$($1_SRCS), \
246        ( ( $(FIND) $$(src)/META-INF -type f 2> /dev/null || true ) \
247            | $(SED) 's|$$(src)/|-C $$(src) |g' >> \
248        $$($1_BIN)/_the.$$($1_JARNAME)_contents) $$(NEWLINE) )
249  endif
250  $1_SUPDATE_CONTENTS=$(JAR) $$($1_JAR_UPDATE_OPTIONS) $$@ @$$($1_BIN)/_the.$$($1_JARNAME)_contents $$(NEWLINE)
251
252  # Use a slightly shorter name for logging, but with enough path to identify this jar.
253  $1_NAME:=$$(subst $$(OUTPUT_ROOT)/,,$$($1_JAR))
254
255  ifneq (,$$($1_CHECK_COMPRESS_JAR))
256    $1_JAR_CREATE_OPTIONS := c0fm
257    $1_JAR_UPDATE_OPTIONS := u0f
258    ifeq ($(COMPRESS_JARS), true)
259      $1_JAR_CREATE_OPTIONS := cfm
260      $1_JAR_UPDATE_OPTIONS := uf
261    endif
262  else
263    $1_JAR_CREATE_OPTIONS := cfm
264    $1_JAR_UPDATE_OPTIONS := uf
265  endif
266
267  # Include all variables of significance in the vardeps file
268  $1_VARDEPS := $(JAR) $$($1_JAR_CREATE_OPTIONS) $$($1_MANIFEST) \
269      $$($1_JARMAIN) $$($1_EXTRA_MANIFEST_ATTR) $$($1_ORIG_DEPS) $$($1_SRCS) \
270      $$($1_INCLUDES) $$($1_EXCLUDES) $$($1_EXCLUDE_FILES) $$($1_EXTRA_FILES)
271  $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, $$(dir $$($1_JAR))_the.$$($1_JARNAME).vardeps)
272
273  # Here is the rule that creates/updates the jar file.
274  $$($1_JAR) : $$($1_DEPENDENCIES) $$($1_MANIFEST) $$($1_VARDEPS_FILE)
275	$(MKDIR) -p $$($1_BIN)
276	$$($1_GREP_INCLUDE_OUTPUT)
277	$$($1_GREP_EXCLUDE_OUTPUT)
278        # If the vardeps file is part of the newer prereq list, it means that
279        # either the jar file does not exist, or we need to recreate it from
280        # from scratch anyway since a simple update will not catch all the
281        # potential changes.
282	$$(if $$(filter $$($1_VARDEPS_FILE) $$($1_MANIFEST), $$?), \
283	  $$(if $$($1_MANIFEST), \
284	    $(SED) -e '$(DOLLAR)$(DOLLAR)a\' $$($1_MANIFEST) > $$($1_MANIFEST_FILE) $$(NEWLINE) \
285	  , \
286	    $(RM) $$($1_MANIFEST_FILE) && $(TOUCH) $$($1_MANIFEST_FILE) $$(NEWLINE)) \
287	  $$(if $$($1_JARMAIN), \
288	    $(ECHO) "Main-Class: $$(strip $$($1_JARMAIN))" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \
289	  $$(if $$($1_EXTRA_MANIFEST_ATTR), \
290	    $(PRINTF) "$$($1_EXTRA_MANIFEST_ATTR)\n" >> $$($1_MANIFEST_FILE) $$(NEWLINE)) \
291	  $(ECHO) Creating $$($1_NAME) $$(NEWLINE) \
292	  $(JAR) $$($1_JAR_CREATE_OPTIONS) $$@ $$($1_MANIFEST_FILE) $$(NEWLINE) \
293	  $$($1_SCAPTURE_CONTENTS) \
294	  $$($1_SCAPTURE_METAINF) \
295	  $$($1_SUPDATE_CONTENTS) \
296	  $$($1_JARINDEX) && true \
297	, \
298	  $(ECHO) Modifying $$($1_NAME) $$(NEWLINE) \
299	  $$($1_CAPTURE_CONTENTS) \
300	  $$($1_CAPTURE_METAINF) \
301	  $(RM) $$($1_DELETES_FILE) $$(NEWLINE) \
302	  $$($1_CAPTURE_DELETES) \
303	  $(CAT) $$($1_DELETES_FILE) > $$($1_DELETESS_FILE) $$(NEWLINE) \
304	  if [ -s $$($1_DELETESS_FILE) ]; then \
305	    $(ECHO) "  deleting" `$(WC) -l $$($1_DELETESS_FILE) | $(AWK) '{ print $$$$1 }'` files && \
306	    $(ZIP) -q -d $$@ `$(CAT) $$($1_DELETESS_FILE)` ; \
307	  fi $$(NEWLINE) \
308	  $$($1_UPDATE_CONTENTS) true $$(NEWLINE) \
309	  $$($1_JARINDEX) && true )
310
311  # Add jar to target list
312  $1 += $$($1_JAR)
313endef
314
315
316define add_file_to_copy
317  # param 1 = BUILD_MYPACKAGE
318  # parma 2 = The source file to copy.
319  $2_TARGET:=$2
320  # Remove the source prefix.
321  $$(foreach i,$$($1_SRC),$$(eval $$(call remove_string,$$i,$2_TARGET)))
322  # To allow for automatic overrides, do not create a rule for a target file that
323  # already has one
324  ifneq ($$($1_COPY_$$($2_TARGET)), 1)
325    $1_COPY_$$($2_TARGET) := 1
326    # Now we can setup the depency that will trigger the copying.
327    $$($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# An empty echo ensures that the input to sed always ends with a newline.
345# Certain implementations (e.g. Solaris) will skip the last line without
346# it.
347#
348# The sed expression does this:
349# 1. Add a backslash before any :, = or ! that do not have a backslash already.
350# 2. Apply the file unicode2x.sed which does a whole bunch of \u00XX to \xXX
351#    conversions.
352# 3. Delete all lines starting with #.
353# 4. Delete empty lines.
354# 5. Append lines ending with \ with the next line.
355# 6. Remove leading and trailing white space. Note that tabs must be explicit
356#    as sed on macosx does not understand '\t'.
357# 7. Replace the first \= with just =.
358# 8. Finally it's all sorted to create a stable output.
359#
360# It is assumed that = is the character used for separating names and values.
361define add_file_to_clean
362  # param 1 = BUILD_MYPACKAGE
363  # parma 2 = The source file to copy and clean.
364  $2_TARGET:=$2
365  # Remove the source prefix.
366  $$(foreach i,$$($1_SRC),$$(eval $$(call remove_string,$$i,$2_TARGET)))
367  # Now we can setup the depency that will trigger the copying.
368  # To allow for automatic overrides, do not create a rule for a target file that
369  # already has one
370  ifneq ($$($1_CLEAN_$$($2_TARGET)), 1)
371    $1_CLEAN_$$($2_TARGET) := 1
372    $$($1_BIN)$$($1_MODULE_SUBDIR)$$($2_TARGET) : $2
373	$(MKDIR) -p $$(@D)
374	export LC_ALL=C ; ( $(CAT) $$< && $(ECHO) "" ) \
375	    | $(SED) -e 's/\([^\\]\):/\1\\:/g' -e 's/\([^\\]\)=/\1\\=/g' \
376	        -e 's/\([^\\]\)!/\1\\!/g' -e 's/^[ 	]*#.*/#/g' \
377	    | $(SED) -f "$(SRC_ROOT)/make/common/support/unicode2x.sed" \
378	    | $(SED) -e '/^#/d' -e '/^$$$$/d' \
379	        -e :a -e '/\\$$$$/N; s/\\\n//; ta' \
380	        -e 's/^[ 	]*//;s/[ 	]*$$$$//' \
381	        -e 's/\\=/=/' \
382	    | $(SORT) > $$@
383	$(CHMOD) -f ug+w $$@
384
385    # And do not forget this target
386    $1_ALL_COPY_CLEAN_TARGETS += $$($1_BIN)$$($2_TARGET)
387  endif
388endef
389
390define remove_string
391  $2 := $$(subst $1,,$$($2))
392endef
393
394# Setup make rules for compiling Java source code to class files and/or a
395# resulting jar file.
396#
397# Parameter 1 is the name of the rule. This name is used as variable prefix,
398# and the targets generated are listed in a variable by that name.
399#
400# Remaining parameters are named arguments. These include:
401#   SETUP:=must point to a previously setup java compiler, for example: SETUP:=BOOTJAVAC
402#   JVM:=path to ..bin/java
403#   ADD_JAVAC_FLAGS:=javac flags to append to the default ones.
404#   SRC:=one or more directories to search for sources. The order of the source roots
405#        is significant. The first found file of a certain name has priority.
406#   BIN:=store classes here
407#   CLASSPATH:=a list of additional entries to set as classpath to javac
408#   INCLUDES:=myapp.foo means will only compile java files in myapp.foo or any of its sub-packages.
409#   EXCLUDES:=myapp.foo means will do not compile java files in myapp.foo or any of its sub-packages.
410#   COPY:=.prp means copy all prp files to the corresponding package in BIN.
411#   COPY_FILES:=myapp/foo/setting.txt means copy this file over to the package myapp/foo
412#   CLEAN:=.properties means copy and clean all properties file to the corresponding package in BIN.
413#   CLEAN_FILES:=myapp/foo/setting.txt means clean this file over to the package myapp/foo
414#   SRCZIP:=Create a src.zip based on the found sources and copied files.
415#   INCLUDE_FILES:="com/sun/SolarisFoobar.java" means only compile this file!
416#   EXCLUDE_FILES:="com/sun/SolarisFoobar.java" means do not compile this particular file!
417#       "SolarisFoobar.java" means do not compile SolarisFoobar, wherever it is found.
418#   HEADERS:=path to directory where all generated c-headers are written.
419#   DEPENDS:=Extra dependecy
420#   DISABLE_SJAVAC:=Explicitly disable the use of sjavac for this compilation unit.
421#   KEEP_DUPS:=Do not remove duplicate file names from different source roots.
422SetupJavaCompilation = $(NamedParamsMacroTemplate)
423define SetupJavaCompilationBody
424
425  # Verify arguments
426  ifeq ($$($1_BIN),)
427    $$(error Must specify BIN (in $1))
428  endif
429
430  # Extract the info from the java compiler setup.
431  $1_JVM := $$($$($1_SETUP)_JVM)
432  $1_JAVAC := $$($$($1_SETUP)_JAVAC)
433  $1_FLAGS := $$($$($1_SETUP)_FLAGS) $(JAVAC_FLAGS) $$($1_ADD_JAVAC_FLAGS)
434  ifneq ($$($1_CLASSPATH), )
435    $1_FLAGS += -cp $$(call PathList, $$($1_CLASSPATH))
436  endif
437  ifeq ($$($1_JAVAC),)
438    $$(error The Java compilation $1 refers to a non-existant java compiler setup $$($1_SETUP))
439  endif
440  $1_SJAVAC_PORTFILE := $$($$($1_SETUP)_SJAVAC_PORTFILE)
441  $1_SERVER_JVM := $$($$($1_SETUP)_SERVER_JVM)
442
443  # Handle addons and overrides.
444  $1_SRC:=$$(call ADD_SRCS,$$($1_SRC))
445  # Make sure the dirs exist.
446  $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),,$$(error SRC specified to SetupJavaCompilation $1 contains missing directory >$$d<)))
447  $$(call MakeDir,$$($1_BIN))
448  # Add all source roots to the find cache since we are likely going to run find
449  # on these more than once. The cache will only be updated if necessary.
450  $$(eval $$(call FillCacheFind,$$($1_SRC)))
451  # Find all files in the source trees. Preserve order of source roots for overrides to
452  # work correctly. CacheFind does not preserve order so need to call it for each root.
453  $1_ALL_SRCS += $$(filter-out $(OVR_SRCS),$$(foreach s,$$($1_SRC),$$(call CacheFind,$$(s))))
454  # Extract the java files.
455  ifneq ($$($1_EXCLUDE_FILES),)
456    $1_EXCLUDE_FILES_PATTERN:=$$(addprefix %,$$($1_EXCLUDE_FILES))
457  endif
458  $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES_PATTERN),$$(filter %.java,$$($1_ALL_SRCS)))
459  ifneq ($$($1_INCLUDE_FILES),)
460    $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
461    $1_SRCS := $$(filter $$($1_INCLUDE_FILES), $$($1_SRCS))
462  endif
463
464  # Prepend the source/bin path to the filter expressions.
465  ifneq ($$($1_INCLUDES),)
466    $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
467    $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
468  endif
469  ifneq ($$($1_EXCLUDES),)
470    $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
471    $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
472  endif
473
474  ifneq ($$($1_KEEP_DUPS), true)
475    # Remove duplicate source files by keeping the first found of each duplicate.
476    # This allows for automatic overrides with custom or platform specific versions
477    # source files.
478    #
479    # For the smart javac wrapper case, add each removed file to an extra exclude
480    # file list to prevent sjavac from finding duplicate sources.
481    $1_SRCS := $$(strip $$(foreach s, $$($1_SRCS), \
482        $$(eval relative_src := $$(call remove-prefixes, $$($1_SRC), $$(s))) \
483        $$(if $$($1_$$(relative_src)), \
484          $$(eval $1_SJAVAC_EXCLUDE_FILES += $$(s)), \
485          $$(eval $1_$$(relative_src) := 1) $$(s))))
486  endif
487
488  ifeq ($$(strip $$($1_SRCS)), )
489    $$(error No source files found for $1)
490  endif
491
492  $1_SAFE_NAME := $$(strip $$(subst /,_, $1))
493
494  # Create the corresponding smart javac wrapper command line.
495  $1_SJAVAC_ARGS:=$$(addprefix -x ,$$(addsuffix /*,$$($1_EXCLUDES))) \
496      $$(addprefix -i ,$$(addsuffix /*,$$($1_INCLUDES))) \
497      $$(addprefix -xf *,$$(strip $$($1_EXCLUDE_FILES) $$($1_SJAVAC_EXCLUDE_FILES))) \
498      $$(addprefix -if *,$$(strip $$($1_INCLUDE_FILES))) \
499      -src $$(call PathList, $$($1_SRC))
500
501  # All files below META-INF are always copied.
502  $1_ALL_COPIES := $$(filter $$(addsuffix /META-INF%,$$($1_SRC)),$$($1_ALL_SRCS))
503  # Find all files to be copied from source to bin.
504  ifneq (,$$($1_COPY)$$($1_COPY_FILES))
505    # Search for all files to be copied.
506    $1_ALL_COPIES += $$(filter $$(addprefix %,$$($1_COPY)),$$($1_ALL_SRCS))
507    # Copy these explicitly
508    $1_ALL_COPIES += $$($1_COPY_FILES)
509  endif
510  # Copy must also respect filters.
511  ifneq (,$$($1_INCLUDES))
512    $1_ALL_COPIES := $$(filter $$($1_SRC_INCLUDES),$$($1_ALL_COPIES))
513  endif
514  ifneq (,$$($1_EXCLUDES))
515    $1_ALL_COPIES := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_ALL_COPIES))
516  endif
517  ifneq (,$$($1_EXCLUDE_FILES))
518    $1_ALL_COPIES := $$(filter-out $$($1_EXCLUDE_FILES_PATTERN),$$($1_ALL_COPIES))
519  endif
520  ifneq (,$$($1_ALL_COPIES))
521    # Yep, there are files to be copied!
522    $1_ALL_COPY_TARGETS:=
523        $$(foreach i,$$($1_ALL_COPIES),$$(eval $$(call add_file_to_copy,$1,$$i)))
524    # Now we can depend on $$($1_ALL_COPY_TARGETS) to copy all files!
525  endif
526
527  # Find all property files to be copied and cleaned from source to bin.
528  ifneq (,$$($1_CLEAN)$$($1_CLEAN_FILES))
529    # Search for all files to be copied.
530    $1_ALL_CLEANS := $$(filter $$(addprefix %,$$($1_CLEAN)),$$($1_ALL_SRCS))
531    # Clean these explicitly
532    $1_ALL_CLEANS += $$($1_CLEAN_FILES)
533    # Copy and clean must also respect filters.
534    ifneq (,$$($1_INCLUDES))
535      $1_ALL_CLEANS := $$(filter $$($1_SRC_INCLUDES),$$($1_ALL_CLEANS))
536    endif
537    ifneq (,$$($1_EXCLUDES))
538      $1_ALL_CLEANS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_ALL_CLEANS))
539    endif
540    ifneq (,$$($1_EXCLUDE_FILES))
541      $1_ALL_CLEANS := $$(filter-out $$($1_EXCLUDE_FILES_PATTERN),$$($1_ALL_CLEANS))
542    endif
543    ifneq (,$$($1_ALL_CLEANS))
544      # Yep, there are files to be copied and cleaned!
545      $1_ALL_COPY_CLEAN_TARGETS:=
546          $$(foreach i,$$($1_ALL_CLEANS),$$(eval $$(call add_file_to_clean,$1,$$i)))
547      # Now we can depend on $$($1_ALL_COPY_CLEAN_TARGETS) to copy all files!
548    endif
549  endif
550
551  # Create a sed expression to remove the source roots and to replace / with .
552  # and remove .java at the end.
553  $1_REWRITE_INTO_CLASSES:=$$(foreach i,$$($1_SRC),-e 's|$$i/||g') -e 's|/|.|g' -e 's|.java$$$$||g'
554
555  ifeq ($$($1_DISABLE_SJAVAC)x$$(ENABLE_SJAVAC),xyes)
556    ifneq (,$$($1_HEADERS))
557      $1_HEADERS_ARG := -h $$($1_HEADERS)
558    endif
559
560    # Using sjavac to compile.
561    $1_COMPILE_TARGETS := $$($1_BIN)/_the.$1_batch
562
563    # Create SJAVAC variable form JAVAC variable. Expects $1_JAVAC to be
564    # "bootclasspathprepend -cp .../javac.jar com.sun.tools.javac.Main"
565    # and javac is simply replaced with sjavac.
566    $1_SJAVAC:=$$(subst com.sun.tools.javac.Main,com.sun.tools.sjavac.Main,$$($1_JAVAC))
567
568    # Set the $1_REMOTE to spawn a background javac server.
569    $1_REMOTE:=--server:portfile=$$($1_SJAVAC_PORTFILE),id=$1,sjavac=$$(subst \
570        $$(SPACE),%20,$$(subst $$(COMMA),%2C,$$(strip $$($1_SERVER_JVM) $$($1_SJAVAC))))
571
572    $1_VARDEPS := $$($1_JVM) $$($1_SJAVAC) $$($1_SJAVAC_ARGS) $$($1_FLAGS) \
573        $$($1_HEADERS_ARG) $$($1_BIN) $$($1_EXCLUDES) $$($1_INCLUDES) \
574        $$($1_EXCLUDE_FILES) $$($1_INCLUDE_FILES)
575    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, $$($1_BIN)/_the.$1.vardeps)
576
577    $$($1_BIN)/_the.$1_batch: $$($1_SRCS) $$($1_DEPENDS) $$($1_VARDEPS_FILE)
578	$(MKDIR) -p $$(@D) $$(dir $$($1_SJAVAC_PORTFILE))
579	$$(eval $$(call ListPathsSafely,$1_SRCS, $$($1_BIN)/_the.$1_batch.tmp))
580	$(ECHO) Compiling $1
581	$(call LogFailures, $$($1_BIN)/_the.$$($1_SAFE_NAME)_batch.log, $$($1_SAFE_NAME), \
582	    $$($1_JVM) $$($1_SJAVAC) \
583	        $$($1_REMOTE) \
584	        -j 1 \
585	        --permit-unidentified-artifacts \
586	        --permit-sources-without-package \
587	        --compare-found-sources $$($1_BIN)/_the.$1_batch.tmp \
588	        --log=$(LOG_LEVEL) \
589	        --state-dir=$$($1_BIN) \
590	        $$($1_SJAVAC_ARGS) \
591	        $$($1_FLAGS) \
592	        $$($1_HEADERS_ARG) \
593	        -d $$($1_BIN)) && \
594	$(MV) $$($1_BIN)/_the.$1_batch.tmp $$($1_BIN)/_the.$1_batch
595        # Create a pubapi file that only changes when the pubapi changes. Dependent
596        # compilations can use this file to only get recompiled when pubapi has changed.
597        # Grep returns 1 if no matching lines are found. Do not fail for this.
598	$(GREP) -e "^I" $$($1_BIN)/javac_state > $$($1_BIN)/_the.$1_pubapi.tmp \
599	    || test "$$$$?" = "1"
600	if [ ! -f $$($1_BIN)/_the.$1_pubapi ] \
601	    || [ "`$(DIFF) $$($1_BIN)/_the.$1_pubapi $$($1_BIN)/_the.$1_pubapi.tmp`" != "" ]; then \
602	  $(MV) $$($1_BIN)/_the.$1_pubapi.tmp $$($1_BIN)/_the.$1_pubapi; \
603	fi
604
605  else
606    # Using plain javac to batch compile everything.
607    $1_COMPILE_TARGETS := $$($1_BIN)/_the.$1_batch
608
609    # When building in batch, put headers in a temp dir to filter out those that actually
610    # changed before copying them to the real header dir.
611    ifneq (,$$($1_HEADERS))
612      $1_HEADERS_ARG := -h $$($1_HEADERS).$1.tmp
613
614      $$($1_HEADERS)/_the.$1_headers: $$($1_BIN)/_the.$1_batch
615		$(MKDIR) -p $$(@D)
616		if [ -d "$$($1_HEADERS).$1.tmp" ]; then \
617		  for f in `ls $$($1_HEADERS).$1.tmp`; do \
618		    if [ ! -f "$$($1_HEADERS)/$$$$f" ] \
619		        || [ "`$(DIFF) $$($1_HEADERS)/$$$$f $$($1_HEADERS).$1.tmp/$$$$f`" != "" ]; then \
620		    $(CP) -f $$($1_HEADERS).$1.tmp/$$$$f $$($1_HEADERS)/$$$$f; \
621		  fi; \
622		  done; \
623		fi
624		$(RM) -r $$($1_HEADERS).$1.tmp
625		$(TOUCH) $$@
626
627      $1_HEADER_TARGETS := $$($1_HEADERS)/_the.$1_headers
628    endif
629
630    $1_VARDEPS := $$($1_JVM) $$($1_JAVAC) $$($1_FLAGS) $$($1_BIN) \
631        $$($1_HEADERS_ARG) $$($1_EXCLUDES) $$($1_INCLUDES) \
632        $$($1_EXCLUDE_FILES) $$($1_INCLUDE_FILES)
633    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, $$($1_BIN)/_the.$1.vardeps)
634
635    # When not using sjavac, pass along all sources to javac using an @file.
636    $$($1_BIN)/_the.$1_batch: $$($1_SRCS) $$($1_DEPENDS) $$($1_VARDEPS_FILE)
637	$(MKDIR) -p $$(@D)
638	$$(eval $$(call ListPathsSafely,$1_SRCS, $$($1_BIN)/_the.$1_batch.tmp))
639	$(ECHO) Compiling `$(WC) $$($1_BIN)/_the.$1_batch.tmp | $(TR) -s ' ' | $(CUT) -f 2 -d ' '` files for $1
640	$(call LogFailures, $$($1_BIN)/_the.$$($1_SAFE_NAME)_batch.log, $$($1_SAFE_NAME), \
641	    $$($1_JVM) $$($1_JAVAC) $$($1_FLAGS) \
642	        -implicit:none \
643	        -d $$($1_BIN) $$($1_HEADERS_ARG) @$$($1_BIN)/_the.$1_batch.tmp) && \
644	$(MV) $$($1_BIN)/_the.$1_batch.tmp $$($1_BIN)/_the.$1_batch
645  endif
646
647  # Add all targets to main variable
648  $1 := $$($1_ALL_COPY_TARGETS) $$($1_ALL_COPY_CLEAN_TARGETS) $$($1_COMPILE_TARGETS) \
649      $$($1_HEADER_TARGETS)
650
651  # Check if a jar file was specified, then setup the rules for the jar.
652  ifneq (,$$($1_JAR))
653    # If no suffixes was explicitly set for this jar file.
654    # Use class and the cleaned/copied properties file suffixes as the default
655    # for the types of files to be put into the jar.
656    ifeq (,$$($1_SUFFIXES))
657      $1_SUFFIXES:=.class $$($1_CLEAN) $$($1_COPY)
658    endif
659
660    $$(eval $$(call SetupArchive,ARCHIVE_$1, \
661        DEPENDENCIES:=$$($1), \
662        SRCS:=$$($1_BIN), \
663        SUFFIXES:=$$($1_SUFFIXES), \
664        EXCLUDE:=$$($1_EXCLUDES), \
665        INCLUDES:=$$($1_INCLUDES), \
666        EXTRA_FILES:=$$($1_ALL_COPY_TARGETS) $$($1_ALL_COPY_CLEAN_TARGETS), \
667        JAR:=$$($1_JAR), \
668        JARMAIN:=$$($1_JARMAIN), \
669        MANIFEST:=$$($1_MANIFEST), \
670        EXTRA_MANIFEST_ATTR:=$$($1_EXTRA_MANIFEST_ATTR), \
671        JARINDEX:=$$($1_JARINDEX), \
672        HEADERS:=$$($1_HEADERS), \
673        SETUP:=$$($1_SETUP)))
674
675    # Add jar to target list
676    $1 += $$($1_JAR)
677  endif
678
679  # Check if a srczip was specified, then setup the rules for the srczip.
680  ifneq (,$$($1_SRCZIP))
681    $$(eval $$(call SetupZipArchive,ARCHIVE_$1, \
682        SRC:=$$($1_SRC), \
683        ZIP:=$$($1_SRCZIP), \
684        INCLUDES:=$$($1_INCLUDES), \
685        EXCLUDES:=$$($1_EXCLUDES), \
686        EXCLUDE_FILES:=$$($1_EXCLUDE_FILES)))
687
688    # Add zip to target list
689    $1 += $$($1_SRCZIP)
690  endif
691endef
692
693# Use this macro to find the correct target to depend on when the original
694# SetupJavaCompilation is declared in a different makefile, to avoid having
695# to declare and evaluate it again.
696# param 1 is for example BUILD_MYPACKAGE
697# param 2 is the output directory (BIN)
698define SetupJavaCompilationCompileTarget
699  $(if $(findstring yes, $(ENABLE_SJAVAC)), $(strip $2)/_the.$(strip $1)_pubapi, \
700      $(strip $2)/_the.$(strip $1)_batch)
701endef
702
703endif # _JAVA_COMPILATION_GMK
704