NativeCompilation.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# 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
30ifndef _NATIVE_COMPILATION_GMK
31_NATIVE_COMPILATION_GMK := 1
32
33ifeq (,$(_MAKEBASE_GMK))
34  $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk)
35endif
36
37################################################################################
38# Define a native toolchain configuration that can be used by
39# SetupNativeCompilation calls
40#
41# Parameter 1 is the name of the toolchain definition
42#
43# Remaining parameters are named arguments:
44#   EXTENDS - Optional parent definition to get defaults from
45#   CC - The C compiler
46#   CXX - The C++ compiler
47#   LD - The Linker
48#   AR - Static linker
49#   AS - Assembler
50#   MT - Windows MT tool
51#   RC - Windows RC tool
52#   SYSROOT_CFLAGS - Compiler flags for using the specific sysroot
53#   SYSROOT_LDFLAGS - Linker flags for using the specific sysroot
54DefineNativeToolchain = $(NamedParamsMacroTemplate)
55define DefineNativeToolchainBody
56  # If extending another definition, get default values from that,
57  # otherwise, nothing more needs to be done as variable assignments
58  # already happened in NamedParamsMacroTemplate.
59  ifneq ($$($1_EXTENDS), )
60    $$(call SetIfEmpty, $1_CC, $$($$($1_EXTENDS)_CC))
61    $$(call SetIfEmpty, $1_CXX, $$($$($1_EXTENDS)_CXX))
62    $$(call SetIfEmpty, $1_LD, $$($$($1_EXTENDS)_LD))
63    $$(call SetIfEmpty, $1_AR, $$($$($1_EXTENDS)_AR))
64    $$(call SetIfEmpty, $1_AS, $$($$($1_EXTENDS)_AS))
65    $$(call SetIfEmpty, $1_MT, $$($$($1_EXTENDS)_MT))
66    $$(call SetIfEmpty, $1_RC, $$($$($1_EXTENDS)_RC))
67    $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_EXTENDS)_SYSROOT_CFLAGS))
68    $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_EXTENDS)_SYSROOT_LDFLAGS))
69  endif
70endef
71
72# Create a default toolchain with the main compiler and linker
73$(eval $(call DefineNativeToolchain, TOOLCHAIN_DEFAULT, \
74    CC := $(CC), \
75    CXX := $(CXX), \
76    LD := $(LD), \
77    AR := $(AR), \
78    AS := $(AS), \
79    MT := $(MT), \
80    RC := $(RC), \
81    SYSROOT_CFLAGS := $(SYSROOT_CFLAGS), \
82    SYSROOT_LDFLAGS := $(SYSROOT_LDFLAGS), \
83))
84
85# Create a toolchain where linking is done with the C++ linker
86$(eval $(call DefineNativeToolchain, TOOLCHAIN_LINK_CXX, \
87    EXTENDS := TOOLCHAIN_DEFAULT, \
88    LD := $(LDCXX), \
89))
90
91# Create a toolchain with the BUILD compiler, used for build tools that
92# are to be run during the build.
93# The BUILD_SYSROOT_*FLAGS variables are empty for now.
94$(eval $(call DefineNativeToolchain, TOOLCHAIN_BUILD, \
95    EXTENDS := TOOLCHAIN_DEFAULT, \
96    CC := $(BUILD_CC), \
97    LD := $(BUILD_LD), \
98    SYSROOT_CFLAGS := $(BUILD_SYSROOT_CFLAGS), \
99    SYSROOT_LDFLAGS := $(BUILD_SYSROOT_LDFLAGS), \
100))
101
102################################################################################
103
104# Extensions of files handled by this macro.
105NATIVE_SOURCE_EXTENSIONS := %.s %.c %.cpp %.cc %.m %.mm
106
107# Replaces native source extensions with the object file extension in a string.
108# Param 1: the string containing source file names with extensions
109# The surrounding strip is needed to keep additional whitespace out
110define replace_with_obj_extension
111$(strip \
112  $(foreach extension, $(NATIVE_SOURCE_EXTENSIONS), \
113      $(patsubst $(extension),%$(OBJ_SUFFIX),$(filter $(extension),$1))) \
114)
115endef
116
117ifeq ($(OPENJDK_BUILD_OS_ENV), windows.cygwin)
118  UNIX_PATH_PREFIX := /cygdrive
119else ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys)
120  UNIX_PATH_PREFIX :=
121endif
122
123# This pattern is used to transform the output of the microsoft CL compiler
124# into a make syntax dependency file (.d)
125WINDOWS_SHOWINCLUDE_SED_PATTERN := \
126    -e '/^Note: including file:/!d' \
127    -e 's|Note: including file: *||' \
128    -e 's|\\|/|g' \
129    -e 's|^\([a-zA-Z]\):|$(UNIX_PATH_PREFIX)/\1|g' \
130    -e '\|$(TOPDIR)|I !d' \
131    -e 's|$$$$| \\|g' \
132    #
133
134# This pattern is used to transform a dependency file (.d) to a list
135# of make targets for dependent files (.d.targets)
136DEPENDENCY_TARGET_SED_PATTERN := \
137    -e 's/\#.*//' \
138    -e 's/^[^:]*: *//' \
139    -e 's/ *\\$$$$//' \
140    -e 's/^[	 ]*//' \
141    -e '/^$$$$/ d' \
142    -e 's/$$$$/ :/' \
143    #
144
145define add_native_source
146  # param 1 = BUILD_MYPACKAGE
147  # parma 2 = the source file name (..../alfa.c or .../beta.cpp)
148  # param 3 = the bin dir that stores all .o (.obj) and .d files.
149  # param 4 = the c flags to the compiler
150  # param 5 = the c compiler
151  # param 6 = the c++ flags to the compiler
152  # param 7 = the c++ compiler
153  # param 8 = the flags to the assembler
154
155  ifneq (,$$(filter %.c,$2))
156    # Compile as a C file
157    $1_$2_FLAGS=$(CFLAGS_CCACHE) $4 $$($1_$(notdir $2)_CFLAGS) -DTHIS_FILE='"$$(<F)"' -c
158    $1_$2_COMP=$5
159    $1_$2_DEP_FLAG:=$(C_FLAG_DEPS)
160  else ifneq (,$$(filter %.m,$2))
161    # Compile as an Objective-C file
162    $1_$2_FLAGS=-x objective-c $(CFLAGS_CCACHE) $4 $$($1_$(notdir $2)_CFLAGS) -DTHIS_FILE='"$$(<F)"' -c
163    $1_$2_COMP=$5
164    $1_$2_DEP_FLAG:=$(C_FLAG_DEPS)
165  else ifneq (,$$(filter %.s,$2))
166    # Compile as assembler file
167    $1_$2_FLAGS=$8 -DTHIS_FILE='"$$(<F)"'
168    $1_$2_COMP=$(AS)
169    $1_$2_DEP_FLAG:=
170  else ifneq (,$$(filter %.cpp,$2)$$(filter %.cc,$2)$$(filter %.mm,$2))
171    # Compile as a C++ or Objective-C++ file
172    $1_$2_FLAGS=$(CFLAGS_CCACHE) $6 $$($1_$(notdir $2)_CXXFLAGS) -DTHIS_FILE='"$$(<F)"' -c
173    $1_$2_COMP=$7
174    $1_$2_DEP_FLAG:=$(CXX_FLAG_DEPS)
175  else
176    $$(error Internal error in NativeCompilation.gmk: no compiler for file $2)
177  endif
178  # Generate the .o (.obj) file name and place it in the bin dir.
179  $1_$2_OBJ := $3/$$(call replace_with_obj_extension, $$(notdir $2))
180  # Only continue if this object file hasn't been processed already. This lets the first found
181  # source file override any other with the same name.
182  ifeq (,$$(findstring $$($1_$2_OBJ),$$($1_ALL_OBJS)))
183    $1_ALL_OBJS+=$$($1_$2_OBJ)
184    ifeq (,$$(filter %.s,$2))
185      # And this is the dependency file for this obj file.
186      $1_$2_DEP:=$$(patsubst %$(OBJ_SUFFIX),%.d,$$($1_$2_OBJ))
187      # The dependency target file lists all dependencies as empty targets
188      # to avoid make error "No rule to make target" for removed files
189      $1_$2_DEP_TARGETS:=$$(patsubst %$(OBJ_SUFFIX),%.d.targets,$$($1_$2_OBJ))
190
191      # Include previously generated dependency information. (if it exists)
192      -include $$($1_$2_DEP)
193      -include $$($1_$2_DEP_TARGETS)
194
195      ifeq ($(TOOLCHAIN_TYPE), microsoft)
196        $1_$2_DEBUG_OUT_FLAGS:=-Fd$$(patsubst %$(OBJ_SUFFIX),%.pdb,$$($1_$2_OBJ)) \
197            -Fm$$(patsubst %$(OBJ_SUFFIX),%.map,$$($1_$2_OBJ))
198      endif
199    endif
200
201    $$($1_$2_OBJ) : $2 $$($1_COMPILE_VARDEPS_FILE) | $$($1_BUILD_INFO)
202	$(ECHO) $(LOG_INFO) "Compiling $$(notdir $2) (for $$(notdir $$($1_TARGET)))"
203        ifneq ($(TOOLCHAIN_TYPE), microsoft)
204          ifeq ($(TOOLCHAIN_TYPE)$$(filter %.s,$2), solstudio)
205            # The Solaris studio compiler doesn't output the full path to the object file in the
206            # generated deps files. Fixing it with sed. If compiling assembly, don't try this.
207	    $(call LogFailures, $$($1_$2_OBJ).log, $$($1_SAFE_NAME)_$$(notdir $2), \
208	        $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP).tmp $(CC_OUT_OPTION)$$($1_$2_OBJ) $2)
209	    $(SED) 's|^$$(@F):|$$@:|' $$($1_$2_DEP).tmp > $$($1_$2_DEP)
210          else
211	    $(call LogFailures, $$($1_$2_OBJ).log, $$($1_SAFE_NAME)_$$(notdir $2), \
212	        $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP) $(CC_OUT_OPTION)$$($1_$2_OBJ) $2)
213          endif
214          # Create a dependency target file from the dependency file.
215          # Solution suggested by http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
216          ifneq ($$($1_$2_DEP),)
217	    $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_$2_DEP) > $$($1_$2_DEP_TARGETS)
218          endif
219        else
220          # The Visual Studio compiler lacks a feature for generating make dependencies, but by
221          # setting -showIncludes, all included files are printed. These are filtered out and
222          # parsed into make dependences.
223          # Keep as much as possible on one execution line for best performance on Windows
224	  ($(call LogFailures, $$($1_$2_OBJ).log, $$($1_SAFE_NAME)_$$(notdir $2), \
225	      $$($1_$2_COMP) $$($1_$2_FLAGS) -showIncludes $$($1_$2_DEBUG_OUT_FLAGS) \
226	          $(CC_OUT_OPTION)$$($1_$2_OBJ) $2) ; echo $$$$? > $$($1_$2_DEP).exitvalue) \
227	      | $(TEE) $$($1_$2_DEP).raw | $(GREP) -v -e "^Note: including file:" \
228	          -e "^$(notdir $2)$$$$" || test "$$$$?" = "1" ; \
229	      exit `cat $$($1_$2_DEP).exitvalue` ; \
230	  $(RM) $$($1_$2_DEP).exitvalue ; \\
231	  ($(ECHO) $$@: \\ ; \
232	  $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_$2_DEP).raw) | $(SORT) -u > $$($1_$2_DEP) ; \
233	  $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_$2_DEP) > $$($1_$2_DEP_TARGETS)
234        endif
235  endif
236endef
237
238# Setup make rules for creating a native binary (a shared library or an
239# executable).
240#
241# Parameter 1 is the name of the rule. This name is used as variable prefix,
242# and the targets generated are listed in a variable by that name.
243#
244# Remaining parameters are named arguments. These include:
245#   TOOLCHAIN Name of toolchain setup to use. Defaults to TOOLCHAIN_DEFAULT.
246#   SRC one or more directory roots to scan for C/C++ files.
247#   CFLAGS the compiler flags to be used, used both for C and C++.
248#   CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS.
249#   LDFLAGS the linker flags to be used, used both for C and C++.
250#   LDFLAGS_SUFFIX the linker flags to be added last on the commandline
251#       typically the libraries linked to.
252#   ARFLAGS the archiver flags to be used
253#   OBJECT_DIR the directory where we store the object files
254#   LIBRARY the resulting library file
255#   PROGRAM the resulting exec file
256#   INCLUDES only pick source from these directories
257#   EXCLUDES do not pick source from these directories
258#   INCLUDE_FILES only compile exactly these files!
259#   EXCLUDE_FILES with these names
260#   EXTRA_FILES List of extra files not in any of the SRC dirs
261#   VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run
262#   RC_FLAGS flags for RC.
263#   MAPFILE mapfile
264#   REORDER reorder file
265#   DEBUG_SYMBOLS add debug symbols (if configured on)
266#   CC the compiler to use, default is $(CC)
267#   LD the linker to use, default is $(LD)
268#   OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST
269#   DISABLED_WARNINGS_<toolchain> Disable the given warnings for the specified toolchain
270SetupNativeCompilation = $(NamedParamsMacroTemplate)
271define SetupNativeCompilationBody
272
273  ifneq (,$$($1_BIN))
274    $$(error BIN has been replaced with OBJECT_DIR)
275  endif
276
277  ifneq (,$$($1_LIB))
278    $$(error LIB has been replaced with LIBRARY)
279  endif
280
281  ifneq (,$$($1_EXE))
282    $$(error EXE has been replaced with PROGRAM)
283  endif
284
285  ifneq (,$$($1_LIBRARY))
286    ifeq (,$$($1_OUTPUT_DIR))
287      $$(error LIBRARY requires OUTPUT_DIR)
288    endif
289
290    ifneq ($$($1_LIBRARY),$(basename $$($1_LIBRARY)))
291      $$(error directory of LIBRARY should be specified using OUTPUT_DIR)
292    endif
293
294    ifneq (,$(findstring $(SHARED_LIBRARY_SUFFIX),$$($1_LIBRARY)))
295      $$(error LIBRARY should be specified without SHARED_LIBRARY_SUFFIX: $(SHARED_LIBRARY_SUFFIX))
296    endif
297
298    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_LIBRARY)))
299      $$(error LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
300    endif
301
302    ifeq ($$($1_SUFFIX), )
303      $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX)
304    endif
305
306    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_LIBRARY)$$($1_SUFFIX)
307    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
308    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_LIBRARY)
309  endif
310
311  ifneq (,$$($1_STATIC_LIBRARY))
312    ifeq (,$$($1_OUTPUT_DIR))
313      $$(error STATIC_LIBRARY requires OUTPUT_DIR)
314    endif
315
316    ifneq ($$($1_STATIC_LIBRARY),$(basename $$($1_STATIC_LIBRARY)))
317      $$(error directory of STATIC_LIBRARY should be specified using OUTPUT_DIR)
318    endif
319
320    ifneq (,$(findstring $(STATIC_LIBRARY_SUFFIX),$$($1_STATIC_LIBRARY)))
321      $$(error STATIC_LIBRARY should be specified without STATIC_LIBRARY_SUFFIX: $(STATIC_LIBRARY_SUFFIX))
322    endif
323
324    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_STATIC_LIBRARY)))
325      $$(error STATIC_LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
326    endif
327
328    ifeq ($$($1_SUFFIX), )
329      $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX)
330    endif
331
332    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)$$($1_SUFFIX)
333    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
334    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)
335  endif
336
337  ifneq (,$$($1_PROGRAM))
338    ifeq (,$$($1_OUTPUT_DIR))
339      $$(error PROGRAM requires OUTPUT_DIR)
340    endif
341
342    ifneq ($$($1_PROGRAM),$(basename $$($1_PROGRAM)))
343      $$(error directory of PROGRAM should be specified using OUTPUT_DIR)
344    endif
345
346    ifneq (,$(findstring $(EXE_SUFFIX),$$($1_PROGRAM)))
347      $$(error PROGRAM should be specified without EXE_SUFFIX: $(EXE_SUFFIX))
348    endif
349
350    ifeq ($$($1_SUFFIX), )
351      $1_SUFFIX := $(EXE_SUFFIX)
352    endif
353
354    $1_BASENAME:=$$($1_PROGRAM)$$($1_SUFFIX)
355    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
356    $1_NOSUFFIX:=$$($1_PROGRAM)
357  endif
358  $1_SAFE_NAME := $$(strip $$(subst /,_, $1))
359
360  ifeq (,$$($1_TARGET))
361    $$(error Neither PROGRAM, LIBRARY nor STATIC_LIBRARY has been specified for SetupNativeCompilation)
362  endif
363
364  # Setup the toolchain to be used
365  $$(call SetIfEmpty, $1_TOOLCHAIN, TOOLCHAIN_DEFAULT)
366  $$(call SetIfEmpty, $1_CC, $$($$($1_TOOLCHAIN)_CC))
367  $$(call SetIfEmpty, $1_CXX, $$($$($1_TOOLCHAIN)_CXX))
368  $$(call SetIfEmpty, $1_LD, $$($$($1_TOOLCHAIN)_LD))
369  $$(call SetIfEmpty, $1_AR, $$($$($1_TOOLCHAIN)_AR))
370  $$(call SetIfEmpty, $1_AS, $$($$($1_TOOLCHAIN)_AS))
371  $$(call SetIfEmpty, $1_MT, $$($$($1_TOOLCHAIN)_MT))
372  $$(call SetIfEmpty, $1_RC, $$($$($1_TOOLCHAIN)_RC))
373  $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_CFLAGS))
374  $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_LDFLAGS))
375
376  ifneq ($$($1_MANIFEST), )
377    ifeq ($$($1_MANIFEST_VERSION), )
378      $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1)
379    endif
380  endif
381
382  # Make sure the dirs exist.
383  $$(call MakeDir,$$($1_OBJECT_DIR) $$($1_OUTPUT_DIR))
384  $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),, \
385      $$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d)))
386
387  # Find all files in the source trees. Sort to remove duplicates.
388  $1_ALL_SRCS := $$(sort $$(call CacheFind,$$($1_SRC)))
389  # Extract the C/C++ files.
390  $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
391  $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
392  ifneq ($$($1_EXCLUDE_FILES),)
393    $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES))
394  endif
395  $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter $$(NATIVE_SOURCE_EXTENSIONS),$$($1_ALL_SRCS)))
396  ifneq (,$$(strip $$($1_INCLUDE_FILES)))
397    $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS))
398  endif
399  ifeq (,$$($1_SRCS))
400    $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
401  endif
402  # There can be only a single bin dir root, no need to foreach over the roots.
403  $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX))
404  # Now we have a list of all c/c++ files to compile: $$($1_SRCS)
405  # and we have a list of all existing object files: $$($1_BINS)
406
407  # Prepend the source/bin path to the filter expressions. Then do the filtering.
408  ifneq ($$($1_INCLUDES),)
409    $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
410    $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
411  endif
412  ifneq ($$($1_EXCLUDES),)
413    $1_SRC_EXCLUDES := $$(addsuffix /%,$$($1_EXCLUDES))
414    $1_SRC_EXCLUDES += $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
415    $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
416  endif
417
418  $1_SRCS += $$($1_EXTRA_FILES)
419
420  ifeq (,$$($1_SRCS))
421    $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
422  endif
423
424  # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides
425  # a reproducable order on the input files to the linker).
426  $1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS)))
427  $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_OBJECT_DIR)/,$$($1_EXPECTED_OBJS_FILENAMES)))
428  # Are there too many object files on disk? Perhaps because some source file was removed?
429  $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS)))
430  # Clean out the superfluous object files.
431  ifneq ($$($1_SUPERFLUOUS_OBJS),)
432    $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS))
433  endif
434
435  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CFLAGS.
436  $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS))
437  ifneq ($(DEBUG_LEVEL),release)
438    # Pickup extra debug dependent variables for CFLAGS
439    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug)
440    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
441    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug)
442  else
443    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release)
444    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
445    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release)
446  endif
447
448  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS.
449  $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS))
450  ifneq ($(DEBUG_LEVEL),release)
451    # Pickup extra debug dependent variables for CXXFLAGS
452    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug)
453    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
454    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug)
455  else
456    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release)
457    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
458    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release)
459  endif
460
461  # If no C++ flags are explicitly set, default to using the C flags.
462  # After that, we can set additional C++ flags that should not interfere
463  # with the mechanism for copying the C flags by default.
464  ifeq ($$($1_CXXFLAGS),)
465    $1_CXXFLAGS:=$$($1_CFLAGS)
466  endif
467  ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),)
468    $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS)
469  endif
470
471  ifeq ($$($1_DEBUG_SYMBOLS), true)
472    ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
473      ifdef OPENJDK
474        # Always add debug symbols
475        $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
476        $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
477      else
478        # Programs don't get the debug symbols added in the old build. It's not clear if
479        # this is intentional.
480        ifeq ($$($1_PROGRAM),)
481          $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
482          $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
483        endif
484      endif
485    endif
486  endif
487
488  ifneq (,$$($1_REORDER))
489    $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER)
490    $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER)
491  endif
492
493  # Pick up disabled warnings, if possible on this platform.
494  ifneq ($(DISABLE_WARNING_PREFIX),)
495    $1_EXTRA_CFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)))
496    $1_EXTRA_CXXFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)))
497  endif
498
499  # Check if warnings should be considered errors.
500  # Pick first binary and toolchain specific, then binary specific, then general setting.
501  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)),)
502    ifeq ($$($1_WARNINGS_AS_ERRORS),)
503      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$(WARNINGS_AS_ERRORS)
504    else
505      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$($1_WARNINGS_AS_ERRORS)
506    endif
507  endif
508
509  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), true)
510    $1_EXTRA_CFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
511    $1_EXTRA_CXXFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
512  endif
513
514  ifeq (NONE, $$($1_OPTIMIZATION))
515    $1_EXTRA_CFLAGS += $(C_O_FLAG_NONE)
516    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NONE)
517  else ifeq (LOW, $$($1_OPTIMIZATION))
518    $1_EXTRA_CFLAGS += $(C_O_FLAG_NORM)
519    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NORM)
520  else ifeq (HIGH, $$($1_OPTIMIZATION))
521    $1_EXTRA_CFLAGS += $(C_O_FLAG_HI)
522    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HI)
523  else ifeq (HIGHEST, $$($1_OPTIMIZATION))
524    $1_EXTRA_CFLAGS += $(C_O_FLAG_HIGHEST)
525    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HIGHEST)
526  else ifneq (, $$($1_OPTIMIZATION))
527    $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION))
528  endif
529
530  $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker
531
532  # Track variable changes for all variables that affect the compilation command
533  # lines for all object files in this setup. This includes at least all the
534  # variables used in the call to add_native_source below.
535  $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
536      $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) \
537      $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS) \
538      $$(foreach s, $$($1_SRCS), \
539          $$($1_$$(notdir $$s)_CFLAGS) $$($1_$$(notdir $$s)_CXXFLAGS))
540  $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \
541      $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps)
542
543  # Now call add_native_source for each source file we are going to compile.
544  $$(foreach p,$$($1_SRCS), \
545      $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \
546          $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS), \
547          $$($1_CC), \
548          $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_SYSROOT_CFLAGS), \
549          $$($1_CXX), $$($1_ASFLAGS))))
550
551  # Setup rule for printing progress info when compiling source files.
552  # This is a rough heuristic and may not always print accurate information.
553  $$($1_BUILD_INFO): $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE)
554        ifeq ($$(wildcard $$($1_TARGET)),)
555	  $(ECHO) 'Creating $$($1_BASENAME) from $$(words $$(filter-out %.vardeps, $$?)) file(s)'
556        else
557	  $(ECHO) $$(strip 'Updating $$($1_BASENAME)' \
558	      $$(if $$(filter-out %.vardeps, $$?), \
559	        'due to $$(words $$(filter-out %.vardeps, $$?)) file(s)', \
560	      $$(if $$(filter %.vardeps, $$?), 'due to makefile changes')))
561        endif
562	$(TOUCH) $$@
563
564  # On windows we need to create a resource file
565  ifeq ($(OPENJDK_TARGET_OS), windows)
566    ifneq (,$$($1_VERSIONINFO_RESOURCE))
567      $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res
568      $1_RES_DEP:=$$($1_RES).d
569      $1_RES_DEP_TARGETS:=$$($1_RES).d.targets
570      -include $$($1_RES_DEP)
571      -include $$($1_RES_DEP_TARGETS)
572
573      $1_RES_VARDEPS := $$($1_RC) $$($1_RC_FLAGS)
574      $1_RES_VARDEPS_FILE := $$(call DependOnVariable, $1_RES_VARDEPS, \
575          $$($1_RES).vardeps)
576
577      $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE)
578		$(ECHO) $(LOG_INFO) "Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$(notdir $$($1_TARGET)))"
579		$$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \
580		    $$($1_VERSIONINFO_RESOURCE)
581                # Windows RC compiler does not support -showIncludes, so we mis-use CL for this.
582		$$($1_CC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) -showIncludes -nologo -TC \
583		    $(CC_OUT_OPTION)$$($1_RES_DEP).obj $$($1_VERSIONINFO_RESOURCE) > $$($1_RES_DEP).raw 2>&1 || exit 0
584		($(ECHO) $$($1_RES): \\ \
585		&& $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).raw) > $$($1_RES_DEP)
586		$(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_RES_DEP) > $$($1_RES_DEP_TARGETS)
587    endif
588  endif
589
590  # mapfile doesnt seem to be implemented on macosx (yet??)
591  ifneq ($(OPENJDK_TARGET_OS),macosx)
592    ifneq ($(OPENJDK_TARGET_OS),windows)
593      $1_REAL_MAPFILE:=$$($1_MAPFILE)
594      ifneq (,$$($1_REORDER))
595        $1_REAL_MAPFILE:=$$($1_OBJECT_DIR)/mapfile
596
597        $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER)
598		$$(MKDIR) -p $$(@D)
599		$$(CP) $$($1_MAPFILE) $$@.tmp
600		$$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp
601		$$(MV) $$@.tmp $$@
602      endif
603    endif
604  endif
605
606  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables
607  # for LDFLAGS and LDFLAGS_SUFFIX
608  $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS))
609  $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS))
610  ifneq (,$$($1_REAL_MAPFILE))
611    $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE))
612  endif
613
614  # Need to make sure TARGET is first on list
615  $1 := $$($1_TARGET)
616  ifeq ($$($1_STATIC_LIBRARY),)
617    ifeq ($$($1_DEBUG_SYMBOLS), true)
618      ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
619        ifneq ($(OPENJDK_TARGET_OS), macosx) # no MacOS X support yet
620          ifneq ($$($1_OUTPUT_DIR),$$($1_OBJECT_DIR))
621            # The dependency on TARGET is needed on windows for debuginfo files
622            # to be rebuilt properly.
623            $$($1_OUTPUT_DIR)/% : $$($1_OBJECT_DIR)/% $$($1_TARGET)
624		$(CP) $$< $$@
625          endif
626
627          # Generate debuginfo files.
628          ifeq ($(OPENJDK_TARGET_OS), windows)
629            $1_EXTRA_LDFLAGS += "-pdb:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb" \
630                "-map:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map"
631            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb \
632                $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map
633            # No separate command is needed for debuginfo on windows, instead
634            # touch target to make sure it has a later time stamp than the debug
635            # symbol files to avoid unnecessary relinking on rebuild.
636            $1_CREATE_DEBUGINFO_CMDS := $(TOUCH) $$($1_TARGET)
637
638          else ifneq ($(findstring $(OPENJDK_TARGET_OS), linux solaris), )
639            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
640            # Setup the command line creating debuginfo files, to be run after linking.
641            # It cannot be run separately since it updates the original target file
642            $1_CREATE_DEBUGINFO_CMDS := \
643                $(OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
644                $(CD) $$($1_OUTPUT_DIR) && \
645                    $(OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET)
646          endif # No MacOS X support
647
648          # This dependency dance ensures that debug info files get rebuilt
649          # properly if deleted.
650          $$($1_TARGET): $$($1_DEBUGINFO_FILES)
651          $$($1_DEBUGINFO_FILES): $$($1_EXPECTED_OBJS)
652
653          ifeq ($(ZIP_DEBUGINFO_FILES), true)
654            $1_DEBUGINFO_ZIP := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz
655            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_ZIP))
656
657            # The dependency on TARGET is needed for debuginfo files
658            # to be rebuilt properly.
659            $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET)
660		$(CD) $$($1_OBJECT_DIR) \
661		&& $(ZIP) -q $$@ $$(notdir $$($1_DEBUGINFO_FILES))
662
663          else
664            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_FILES))
665          endif
666        endif
667      endif # !MacOS X
668    endif # $1_DEBUG_SYMBOLS
669  endif # !STATIC_LIBRARY
670
671  ifneq (,$$($1_LIBRARY))
672    # Generating a dynamic library.
673    $1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
674    ifeq ($(OPENJDK_TARGET_OS), windows)
675      $1_EXTRA_LDFLAGS += "-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib"
676    endif
677
678    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
679
680    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
681        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_CREATE_DEBUGINFO_CMDS)
682    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
683        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
684
685    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) \
686        $$($1_VARDEPS_FILE)
687		$(ECHO) $(LOG_INFO) "Linking $$($1_BASENAME)" ; \
688		$(call LogFailures, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link.log, $$($1_SAFE_NAME)_link, \
689		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
690		    $(LD_OUT_OPTION)$$@ \
691		    $$($1_EXPECTED_OBJS) $$($1_RES) \
692		    $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX)) ; \
693		$$($1_CREATE_DEBUGINFO_CMDS)
694
695  endif
696
697  ifneq (,$$($1_STATIC_LIBRARY))
698    $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LDFLAGS_SUFFIX) \
699        $$($1_EXTRA_LDFLAGS_SUFFIX)
700    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
701        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
702
703    # Generating a static library, ie object file archive.
704    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_VARDEPS_FILE)
705	$(ECHO) $(LOG_INFO) "Archiving $$($1_STATIC_LIBRARY)"
706	$(call LogFailures, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link.log, $$($1_SAFE_NAME)_link, \
707	    $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_EXPECTED_OBJS) \
708	        $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
709  endif
710
711  ifneq (,$$($1_PROGRAM))
712    # A executable binary has been specified, setup the target for it.
713    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
714
715    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
716        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_MT) \
717        $$($1_CODESIGN) $$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION)
718    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
719        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
720
721    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_MANIFEST) \
722        $$($1_VARDEPS_FILE)
723		$(ECHO) $(LOG_INFO) "Linking executable $$($1_BASENAME)" ; \
724		$(call LogFailures, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link.log, $$($1_SAFE_NAME)_link, \
725		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
726		        $(EXE_OUT_OPTION)$$($1_TARGET) \
727		        $$($1_EXPECTED_OBJS) $$($1_RES) \
728		        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
729                ifeq ($(OPENJDK_TARGET_OS), windows)
730                  ifneq ($$($1_MANIFEST), )
731		    $$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_PROGRAM).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1
732                  endif
733                endif
734                # This only works if the openjdk_codesign identity is present on the system. Let
735                # silently fail otherwise.
736                ifneq (,$(CODESIGN))
737                  ifneq (,$$($1_CODESIGN))
738		    $(CODESIGN) -s openjdk_codesign $$@
739                  endif
740                endif
741		$$($1_CREATE_DEBUGINFO_CMDS)
742
743  endif
744endef
745
746endif # _NATIVE_COMPILATION_GMK
747