NativeCompilation.gmk revision 1656:f2b50d16adc9
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        else
215          # The Visual Studio compiler lacks a feature for generating make dependencies, but by
216          # setting -showIncludes, all included files are printed. These are filtered out and
217          # parsed into make dependences.
218	  ($(call LogFailures, $$($1_$2_OBJ).log, $$($1_SAFE_NAME)_$$(notdir $2), \
219	      $$($1_$2_COMP) $$($1_$2_FLAGS) -showIncludes $$($1_$2_DEBUG_OUT_FLAGS) \
220	          $(CC_OUT_OPTION)$$($1_$2_OBJ) $2) ; echo $$$$? > $$($1_$2_DEP).exitvalue) \
221	      | $(TEE) $$($1_$2_DEP).raw | $(GREP) -v -e "^Note: including file:" \
222	          -e "^$(notdir $2)$$$$" || test "$$$$?" = "1" ; \
223	      exit `cat $$($1_$2_DEP).exitvalue`
224	  $(RM) $$($1_$2_DEP).exitvalue
225	  ($(ECHO) $$@: \\ \
226	  && $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_$2_DEP).raw) | $(SORT) -u > $$($1_$2_DEP)
227        endif
228        # Create a dependency target file from the dependency file.
229        # Solution suggested by http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
230        ifneq ($$($1_$2_DEP),)
231	  $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_$2_DEP) > $$($1_$2_DEP_TARGETS)
232        endif
233  endif
234endef
235
236# Setup make rules for creating a native binary (a shared library or an
237# executable).
238#
239# Parameter 1 is the name of the rule. This name is used as variable prefix,
240# and the targets generated are listed in a variable by that name.
241#
242# Remaining parameters are named arguments. These include:
243#   TOOLCHAIN Name of toolchain setup to use. Defaults to TOOLCHAIN_DEFAULT.
244#   SRC one or more directory roots to scan for C/C++ files.
245#   CFLAGS the compiler flags to be used, used both for C and C++.
246#   CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS.
247#   LDFLAGS the linker flags to be used, used both for C and C++.
248#   LDFLAGS_SUFFIX the linker flags to be added last on the commandline
249#       typically the libraries linked to.
250#   ARFLAGS the archiver flags to be used
251#   OBJECT_DIR the directory where we store the object files
252#   LIBRARY the resulting library file
253#   PROGRAM the resulting exec file
254#   INCLUDES only pick source from these directories
255#   EXCLUDES do not pick source from these directories
256#   INCLUDE_FILES only compile exactly these files!
257#   EXCLUDE_FILES with these names
258#   EXTRA_FILES List of extra files not in any of the SRC dirs
259#   VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run
260#   RC_FLAGS flags for RC.
261#   MAPFILE mapfile
262#   REORDER reorder file
263#   DEBUG_SYMBOLS add debug symbols (if configured on)
264#   CC the compiler to use, default is $(CC)
265#   LD the linker to use, default is $(LD)
266#   OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST
267#   DISABLED_WARNINGS_<toolchain> Disable the given warnings for the specified toolchain
268SetupNativeCompilation = $(NamedParamsMacroTemplate)
269define SetupNativeCompilationBody
270
271  ifneq (,$$($1_BIN))
272    $$(error BIN has been replaced with OBJECT_DIR)
273  endif
274
275  ifneq (,$$($1_LIB))
276    $$(error LIB has been replaced with LIBRARY)
277  endif
278
279  ifneq (,$$($1_EXE))
280    $$(error EXE has been replaced with PROGRAM)
281  endif
282
283  ifneq (,$$($1_LIBRARY))
284    ifeq (,$$($1_OUTPUT_DIR))
285      $$(error LIBRARY requires OUTPUT_DIR)
286    endif
287
288    ifneq ($$($1_LIBRARY),$(basename $$($1_LIBRARY)))
289      $$(error directory of LIBRARY should be specified using OUTPUT_DIR)
290    endif
291
292    ifneq (,$(findstring $(SHARED_LIBRARY_SUFFIX),$$($1_LIBRARY)))
293      $$(error LIBRARY should be specified without SHARED_LIBRARY_SUFFIX: $(SHARED_LIBRARY_SUFFIX))
294    endif
295
296    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_LIBRARY)))
297      $$(error LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
298    endif
299
300    ifeq ($$($1_SUFFIX), )
301      $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX)
302    endif
303
304    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_LIBRARY)$$($1_SUFFIX)
305    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
306    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_LIBRARY)
307  endif
308
309  ifneq (,$$($1_STATIC_LIBRARY))
310    ifeq (,$$($1_OUTPUT_DIR))
311      $$(error STATIC_LIBRARY requires OUTPUT_DIR)
312    endif
313
314    ifneq ($$($1_STATIC_LIBRARY),$(basename $$($1_STATIC_LIBRARY)))
315      $$(error directory of STATIC_LIBRARY should be specified using OUTPUT_DIR)
316    endif
317
318    ifneq (,$(findstring $(STATIC_LIBRARY_SUFFIX),$$($1_STATIC_LIBRARY)))
319      $$(error STATIC_LIBRARY should be specified without STATIC_LIBRARY_SUFFIX: $(STATIC_LIBRARY_SUFFIX))
320    endif
321
322    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_STATIC_LIBRARY)))
323      $$(error STATIC_LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
324    endif
325
326    ifeq ($$($1_SUFFIX), )
327      $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX)
328    endif
329
330    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)$$($1_SUFFIX)
331    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
332    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)
333  endif
334
335  ifneq (,$$($1_PROGRAM))
336    ifeq (,$$($1_OUTPUT_DIR))
337      $$(error PROGRAM requires OUTPUT_DIR)
338    endif
339
340    ifneq ($$($1_PROGRAM),$(basename $$($1_PROGRAM)))
341      $$(error directory of PROGRAM should be specified using OUTPUT_DIR)
342    endif
343
344    ifneq (,$(findstring $(EXE_SUFFIX),$$($1_PROGRAM)))
345      $$(error PROGRAM should be specified without EXE_SUFFIX: $(EXE_SUFFIX))
346    endif
347
348    ifeq ($$($1_SUFFIX), )
349      $1_SUFFIX := $(EXE_SUFFIX)
350    endif
351
352    $1_BASENAME:=$$($1_PROGRAM)$$($1_SUFFIX)
353    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
354    $1_NOSUFFIX:=$$($1_PROGRAM)
355  endif
356  $1_SAFE_NAME := $$(strip $$(subst /,_, $1))
357
358  ifeq (,$$($1_TARGET))
359    $$(error Neither PROGRAM, LIBRARY nor STATIC_LIBRARY has been specified for SetupNativeCompilation)
360  endif
361
362  # Setup the toolchain to be used
363  $$(call SetIfEmpty, $1_TOOLCHAIN, TOOLCHAIN_DEFAULT)
364  $$(call SetIfEmpty, $1_CC, $$($$($1_TOOLCHAIN)_CC))
365  $$(call SetIfEmpty, $1_CXX, $$($$($1_TOOLCHAIN)_CXX))
366  $$(call SetIfEmpty, $1_LD, $$($$($1_TOOLCHAIN)_LD))
367  $$(call SetIfEmpty, $1_AR, $$($$($1_TOOLCHAIN)_AR))
368  $$(call SetIfEmpty, $1_AS, $$($$($1_TOOLCHAIN)_AS))
369  $$(call SetIfEmpty, $1_MT, $$($$($1_TOOLCHAIN)_MT))
370  $$(call SetIfEmpty, $1_RC, $$($$($1_TOOLCHAIN)_RC))
371  $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_CFLAGS))
372  $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_LDFLAGS))
373
374  ifneq ($$($1_MANIFEST), )
375    ifeq ($$($1_MANIFEST_VERSION), )
376      $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1)
377    endif
378  endif
379
380  # Make sure the dirs exist.
381  $$(call MakeDir,$$($1_OBJECT_DIR) $$($1_OUTPUT_DIR))
382  $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),, \
383      $$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d)))
384
385  # Find all files in the source trees. Sort to remove duplicates.
386  $1_ALL_SRCS := $$(sort $$(call CacheFind,$$($1_SRC)))
387  # Extract the C/C++ files.
388  $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
389  $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
390  ifneq ($$($1_EXCLUDE_FILES),)
391    $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES))
392  endif
393  $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter $$(NATIVE_SOURCE_EXTENSIONS),$$($1_ALL_SRCS)))
394  ifneq (,$$(strip $$($1_INCLUDE_FILES)))
395    $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS))
396  endif
397  ifeq (,$$($1_SRCS))
398    $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
399  endif
400  # There can be only a single bin dir root, no need to foreach over the roots.
401  $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX))
402  # Now we have a list of all c/c++ files to compile: $$($1_SRCS)
403  # and we have a list of all existing object files: $$($1_BINS)
404
405  # Prepend the source/bin path to the filter expressions. Then do the filtering.
406  ifneq ($$($1_INCLUDES),)
407    $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
408    $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
409  endif
410  ifneq ($$($1_EXCLUDES),)
411    $1_SRC_EXCLUDES := $$(addsuffix /%,$$($1_EXCLUDES))
412    $1_SRC_EXCLUDES += $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
413    $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
414  endif
415
416  $1_SRCS += $$($1_EXTRA_FILES)
417
418  ifeq (,$$($1_SRCS))
419    $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
420  endif
421
422  # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides
423  # a reproducable order on the input files to the linker).
424  $1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS)))
425  $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_OBJECT_DIR)/,$$($1_EXPECTED_OBJS_FILENAMES)))
426  # Are there too many object files on disk? Perhaps because some source file was removed?
427  $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS)))
428  # Clean out the superfluous object files.
429  ifneq ($$($1_SUPERFLUOUS_OBJS),)
430    $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS))
431  endif
432
433  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CFLAGS.
434  $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS))
435  ifneq ($(DEBUG_LEVEL),release)
436    # Pickup extra debug dependent variables for CFLAGS
437    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug)
438    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
439    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug)
440  else
441    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release)
442    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
443    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release)
444  endif
445
446  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS.
447  $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS))
448  ifneq ($(DEBUG_LEVEL),release)
449    # Pickup extra debug dependent variables for CXXFLAGS
450    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug)
451    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
452    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug)
453  else
454    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release)
455    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
456    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release)
457  endif
458
459  # If no C++ flags are explicitly set, default to using the C flags.
460  # After that, we can set additional C++ flags that should not interfere
461  # with the mechanism for copying the C flags by default.
462  ifeq ($$($1_CXXFLAGS),)
463    $1_CXXFLAGS:=$$($1_CFLAGS)
464  endif
465  ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),)
466    $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS)
467  endif
468
469  ifeq ($$($1_DEBUG_SYMBOLS), true)
470    ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
471      ifdef OPENJDK
472        # Always add debug symbols
473        $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
474        $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
475      else
476        # Programs don't get the debug symbols added in the old build. It's not clear if
477        # this is intentional.
478        ifeq ($$($1_PROGRAM),)
479          $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
480          $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
481        endif
482      endif
483    endif
484  endif
485
486  ifneq (,$$($1_REORDER))
487    $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER)
488    $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER)
489  endif
490
491  # Pick up disabled warnings, if possible on this platform.
492  ifneq ($(DISABLE_WARNING_PREFIX),)
493    $1_EXTRA_CFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)))
494    $1_EXTRA_CXXFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)))
495  endif
496
497  # Check if warnings should be considered errors.
498  # Pick first binary and toolchain specific, then binary specific, then general setting.
499  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)),)
500    ifeq ($$($1_WARNINGS_AS_ERRORS),)
501      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$(WARNINGS_AS_ERRORS)
502    else
503      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$($1_WARNINGS_AS_ERRORS)
504    endif
505  endif
506
507  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), true)
508    $1_EXTRA_CFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
509    $1_EXTRA_CXXFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
510  endif
511
512  ifeq (NONE, $$($1_OPTIMIZATION))
513    $1_EXTRA_CFLAGS += $(C_O_FLAG_NONE)
514    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NONE)
515  else ifeq (LOW, $$($1_OPTIMIZATION))
516    $1_EXTRA_CFLAGS += $(C_O_FLAG_NORM)
517    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NORM)
518  else ifeq (HIGH, $$($1_OPTIMIZATION))
519    $1_EXTRA_CFLAGS += $(C_O_FLAG_HI)
520    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HI)
521  else ifeq (HIGHEST, $$($1_OPTIMIZATION))
522    $1_EXTRA_CFLAGS += $(C_O_FLAG_HIGHEST)
523    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HIGHEST)
524  else ifneq (, $$($1_OPTIMIZATION))
525    $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION))
526  endif
527
528  $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker
529
530  # Track variable changes for all variables that affect the compilation command
531  # lines for all object files in this setup. This includes at least all the
532  # variables used in the call to add_native_source below.
533  $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
534      $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) \
535      $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS) \
536      $$(foreach s, $$($1_SRCS), \
537          $$($1_$$(notdir $$s)_CFLAGS) $$($1_$$(notdir $$s)_CXXFLAGS))
538  $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \
539      $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps)
540
541  # Now call add_native_source for each source file we are going to compile.
542  $$(foreach p,$$($1_SRCS), \
543      $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \
544          $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS), \
545          $$($1_CC), \
546          $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_SYSROOT_CFLAGS), \
547          $$($1_CXX), $$($1_ASFLAGS))))
548
549  # Setup rule for printing progress info when compiling source files.
550  # This is a rough heuristic and may not always print accurate information.
551  $$($1_BUILD_INFO): $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE)
552        ifeq ($$(wildcard $$($1_TARGET)),)
553	  $(ECHO) 'Creating $$($1_BASENAME) from $$(words $$(filter-out %.vardeps, $$?)) file(s)'
554        else
555	  $(ECHO) $$(strip 'Updating $$($1_BASENAME)' \
556	      $$(if $$(filter-out %.vardeps, $$?), \
557	        'due to $$(words $$(filter-out %.vardeps, $$?)) file(s)', \
558	      $$(if $$(filter %.vardeps, $$?), 'due to makefile changes')))
559        endif
560	$(TOUCH) $$@
561
562  # On windows we need to create a resource file
563  ifeq ($(OPENJDK_TARGET_OS), windows)
564    ifneq (,$$($1_VERSIONINFO_RESOURCE))
565      $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res
566      $1_RES_DEP:=$$($1_RES).d
567      $1_RES_DEP_TARGETS:=$$($1_RES).d.targets
568      -include $$($1_RES_DEP)
569      -include $$($1_RES_DEP_TARGETS)
570
571      $1_RES_VARDEPS := $$($1_RC) $$($1_RC_FLAGS)
572      $1_RES_VARDEPS_FILE := $$(call DependOnVariable, $1_RES_VARDEPS, \
573          $$($1_RES).vardeps)
574
575      $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE)
576		$(ECHO) $(LOG_INFO) "Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$(notdir $$($1_TARGET)))"
577		$$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \
578		    $$($1_VERSIONINFO_RESOURCE)
579                # Windows RC compiler does not support -showIncludes, so we mis-use CL for this.
580		$$($1_CC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) -showIncludes -nologo -TC \
581		    $(CC_OUT_OPTION)$$($1_RES_DEP).obj $$($1_VERSIONINFO_RESOURCE) > $$($1_RES_DEP).raw 2>&1 || exit 0
582		($(ECHO) $$($1_RES): \\ \
583		&& $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).raw) > $$($1_RES_DEP)
584		$(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_RES_DEP) > $$($1_RES_DEP_TARGETS)
585    endif
586  endif
587
588  # mapfile doesnt seem to be implemented on macosx (yet??)
589  ifneq ($(OPENJDK_TARGET_OS),macosx)
590    ifneq ($(OPENJDK_TARGET_OS),windows)
591      $1_REAL_MAPFILE:=$$($1_MAPFILE)
592      ifneq (,$$($1_REORDER))
593        $1_REAL_MAPFILE:=$$($1_OBJECT_DIR)/mapfile
594
595        $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER)
596		$$(MKDIR) -p $$(@D)
597		$$(CP) $$($1_MAPFILE) $$@.tmp
598		$$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp
599		$$(MV) $$@.tmp $$@
600      endif
601    endif
602  endif
603
604  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables
605  # for LDFLAGS and LDFLAGS_SUFFIX
606  $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS))
607  $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS))
608  ifneq (,$$($1_REAL_MAPFILE))
609    $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE))
610  endif
611
612  # Need to make sure TARGET is first on list
613  $1 := $$($1_TARGET)
614  ifeq ($$($1_STATIC_LIBRARY),)
615    ifeq ($$($1_DEBUG_SYMBOLS), true)
616      ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
617        ifneq ($(OPENJDK_TARGET_OS), macosx) # no MacOS X support yet
618          ifneq ($$($1_OUTPUT_DIR),$$($1_OBJECT_DIR))
619            # The dependency on TARGET is needed on windows for debuginfo files
620            # to be rebuilt properly.
621            $$($1_OUTPUT_DIR)/% : $$($1_OBJECT_DIR)/% $$($1_TARGET)
622		$(CP) $$< $$@
623          endif
624
625          # Generate debuginfo files.
626          ifeq ($(OPENJDK_TARGET_OS), windows)
627            $1_EXTRA_LDFLAGS += "-pdb:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb" \
628                "-map:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map"
629            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb \
630                $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map
631
632          else ifneq ($(findstring $(OPENJDK_TARGET_OS), linux solaris), )
633            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
634            # Setup the command line creating debuginfo files, to be run after linking.
635            # It cannot be run separately since it updates the original target file
636            $1_CREATE_DEBUGINFO_CMDS := \
637                $(OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
638                $(CD) $$($1_OUTPUT_DIR) && \
639                    $(OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET)
640          endif # No MacOS X support
641
642          # This dependency dance ensures that debug info files get rebuilt
643          # properly if deleted.
644          $$($1_TARGET): $$($1_DEBUGINFO_FILES)
645          $$($1_DEBUGINFO_FILES): $$($1_EXPECTED_OBJS)
646
647          ifeq ($(ZIP_DEBUGINFO_FILES), true)
648            $1_DEBUGINFO_ZIP := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz
649            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_ZIP))
650
651            # The dependency on TARGET is needed for debuginfo files
652            # to be rebuilt properly.
653            $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET)
654		$(CD) $$($1_OBJECT_DIR) \
655		&& $(ZIP) -q $$@ $$(notdir $$($1_DEBUGINFO_FILES))
656
657          else
658            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_FILES))
659          endif
660        endif
661      endif # !MacOS X
662    endif # $1_DEBUG_SYMBOLS
663  endif # !STATIC_LIBRARY
664
665  ifneq (,$$($1_LIBRARY))
666    # Generating a dynamic library.
667    $1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
668    ifeq ($(OPENJDK_TARGET_OS), windows)
669      $1_EXTRA_LDFLAGS += "-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib"
670    endif
671
672    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
673
674    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
675        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_CREATE_DEBUGINFO_CMDS)
676    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
677        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
678
679    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) \
680        $$($1_VARDEPS_FILE)
681		$(ECHO) $(LOG_INFO) "Linking $$($1_BASENAME)"
682		$(call LogFailures, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link.log, $$($1_SAFE_NAME)_link, \
683		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
684		    $(LD_OUT_OPTION)$$@ \
685		    $$($1_EXPECTED_OBJS) $$($1_RES) \
686		    $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
687		$$($1_CREATE_DEBUGINFO_CMDS)
688                # Touch target to make sure it has a later time stamp than the debug
689                # symbol files to avoid unnecessary relinking on rebuild.
690                ifeq ($(OPENJDK_TARGET_OS), windows)
691		  $(TOUCH) $$@
692                endif
693
694  endif
695
696  ifneq (,$$($1_STATIC_LIBRARY))
697    $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LDFLAGS_SUFFIX) \
698        $$($1_EXTRA_LDFLAGS_SUFFIX)
699    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
700        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
701
702    # Generating a static library, ie object file archive.
703    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_VARDEPS_FILE)
704	$(ECHO) $(LOG_INFO) "Archiving $$($1_STATIC_LIBRARY)"
705	$(call LogFailures, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link.log, $$($1_SAFE_NAME)_link, \
706	    $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_EXPECTED_OBJS) \
707	        $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
708  endif
709
710  ifneq (,$$($1_PROGRAM))
711    # A executable binary has been specified, setup the target for it.
712    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
713
714    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
715        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_MT) \
716        $$($1_CODESIGN) $$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION)
717    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
718        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
719
720    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_MANIFEST) \
721        $$($1_VARDEPS_FILE)
722		$(ECHO) $(LOG_INFO) "Linking executable $$($1_BASENAME)"
723		$(call LogFailures, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link.log, $$($1_SAFE_NAME)_link, \
724		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
725		        $(EXE_OUT_OPTION)$$($1_TARGET) \
726		        $$($1_EXPECTED_OBJS) $$($1_RES) \
727		        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
728                ifeq ($(OPENJDK_TARGET_OS), windows)
729                  ifneq ($$($1_MANIFEST), )
730		    $$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_PROGRAM).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1
731                  endif
732                endif
733                # This only works if the openjdk_codesign identity is present on the system. Let
734                # silently fail otherwise.
735                ifneq (,$(CODESIGN))
736                  ifneq (,$$($1_CODESIGN))
737		    $(CODESIGN) -s openjdk_codesign $$@
738                  endif
739                endif
740		$$($1_CREATE_DEBUGINFO_CMDS)
741                # Touch target to make sure it has a later time stamp than the debug
742                # symbol files to avoid unnecessary relinking on rebuild.
743                ifeq ($(OPENJDK_TARGET_OS), windows)
744		  $(TOUCH) $$@
745                endif
746
747  endif
748endef
749
750endif # _NATIVE_COMPILATION_GMK
751