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