NativeCompilation.gmk revision 1528:b42f35b0dbff
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  # If no C++ flags are explicitly set, default to using the C flags.
453  # After that, we can set additional C++ flags that should not interfere
454  # with the mechanism for copying the C flags by default.
455  ifeq ($$($1_CXXFLAGS),)
456    $1_CXXFLAGS:=$$($1_CFLAGS)
457  endif
458  ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),)
459    $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS)
460  endif
461
462  ifeq ($$($1_DEBUG_SYMBOLS), true)
463    ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
464      ifdef OPENJDK
465        # Always add debug symbols
466        $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
467        $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
468      else
469        # Programs don't get the debug symbols added in the old build. It's not clear if
470        # this is intentional.
471        ifeq ($$($1_PROGRAM),)
472          $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
473          $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
474        endif
475      endif
476    endif
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  # Check if warnings should be considered errors.
491  # Pick first binary and toolchain specific, then binary specific, then general setting.
492  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)),)
493    ifeq ($$($1_WARNINGS_AS_ERRORS),)
494      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$(WARNINGS_AS_ERRORS)
495    else
496      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$($1_WARNINGS_AS_ERRORS)
497    endif
498  endif
499
500  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), true)
501    $1_EXTRA_CFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
502    $1_EXTRA_CXXFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
503  endif
504
505  ifeq (NONE, $$($1_OPTIMIZATION))
506    $1_EXTRA_CFLAGS += $(C_O_FLAG_NONE)
507    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NONE)
508  else ifeq (LOW, $$($1_OPTIMIZATION))
509    $1_EXTRA_CFLAGS += $(C_O_FLAG_NORM)
510    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NORM)
511  else ifeq (HIGH, $$($1_OPTIMIZATION))
512    $1_EXTRA_CFLAGS += $(C_O_FLAG_HI)
513    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HI)
514  else ifeq (HIGHEST, $$($1_OPTIMIZATION))
515    $1_EXTRA_CFLAGS += $(C_O_FLAG_HIGHEST)
516    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HIGHEST)
517  else ifneq (, $$($1_OPTIMIZATION))
518    $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION))
519  endif
520
521  $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker
522
523  # Track variable changes for all variables that affect the compilation command
524  # lines for all object files in this setup. This includes at least all the
525  # variables used in the call to add_native_source below.
526  $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
527      $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) \
528      $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS) \
529      $$(foreach s, $$($1_SRCS), \
530          $$($1_$$(notdir $$s)_CFLAGS) $$($1_$$(notdir $$s)_CXXFLAGS))
531  $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \
532      $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps)
533
534  # Now call add_native_source for each source file we are going to compile.
535  $$(foreach p,$$($1_SRCS), \
536      $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \
537          $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS), \
538          $$($1_CC), \
539          $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_SYSROOT_CFLAGS), \
540          $$($1_CXX), $$($1_ASFLAGS))))
541
542  # Setup rule for printing progress info when compiling source files.
543  # This is a rough heuristic and may not always print accurate information.
544  $$($1_BUILD_INFO): $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE)
545        ifeq ($$(wildcard $$($1_TARGET)),)
546	  $(ECHO) 'Creating $$($1_BASENAME) from $$(words $$(filter-out %.vardeps, $$?)) file(s)'
547        else
548	  $(ECHO) $$(strip 'Updating $$($1_BASENAME)' \
549	      $$(if $$(filter-out %.vardeps, $$?), \
550	        'from $$(words $$(filter-out %.vardeps, $$?)) file(s)') \
551	      $$(if $$(filter %.vardeps, $$?), 'due to makefile changes'))
552        endif
553	$(TOUCH) $$@
554
555  # On windows we need to create a resource file
556  ifeq ($(OPENJDK_TARGET_OS), windows)
557    ifneq (,$$($1_VERSIONINFO_RESOURCE))
558      $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res
559      $1_RES_DEP:=$$($1_RES).d
560      $1_RES_DEP_TARGETS:=$$($1_RES).d.targets
561      -include $$($1_RES_DEP)
562      -include $$($1_RES_DEP_TARGETS)
563
564      $1_RES_VARDEPS := $$($1_RC) $$($1_RC_FLAGS)
565      $1_RES_VARDEPS_FILE := $$(call DependOnVariable, $1_RES_VARDEPS, \
566          $$($1_RES).vardeps)
567
568      $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE)
569		$(ECHO) $(LOG_INFO) "Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$(notdir $$($1_TARGET)))"
570		$$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \
571		    $$($1_VERSIONINFO_RESOURCE)
572                # Windows RC compiler does not support -showIncludes, so we mis-use CL for this.
573		$$($1_CC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) -showIncludes -nologo -TC \
574		    $(CC_OUT_OPTION)$$($1_RES_DEP).obj $$($1_VERSIONINFO_RESOURCE) > $$($1_RES_DEP).raw 2>&1 || exit 0
575		($(ECHO) $$($1_RES): \\ \
576		&& $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).raw) > $$($1_RES_DEP)
577		$(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_RES_DEP) > $$($1_RES_DEP_TARGETS)
578    endif
579  endif
580
581  # mapfile doesnt seem to be implemented on macosx (yet??)
582  ifneq ($(OPENJDK_TARGET_OS),macosx)
583    ifneq ($(OPENJDK_TARGET_OS),windows)
584      $1_REAL_MAPFILE:=$$($1_MAPFILE)
585      ifneq (,$$($1_REORDER))
586        $1_REAL_MAPFILE:=$$($1_OBJECT_DIR)/mapfile
587
588        $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER)
589		$$(MKDIR) -p $$(@D)
590		$$(CP) $$($1_MAPFILE) $$@.tmp
591		$$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp
592		$$(MV) $$@.tmp $$@
593      endif
594    endif
595  endif
596
597  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables
598  # for LDFLAGS and LDFLAGS_SUFFIX
599  $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS))
600  $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS))
601  ifneq (,$$($1_REAL_MAPFILE))
602    $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE))
603  endif
604
605  # Need to make sure TARGET is first on list
606  $1 := $$($1_TARGET)
607  ifeq ($$($1_STATIC_LIBRARY),)
608    ifeq ($$($1_DEBUG_SYMBOLS), true)
609      ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
610        ifneq ($(OPENJDK_TARGET_OS), macosx) # no MacOS X support yet
611          ifneq ($$($1_OUTPUT_DIR),$$($1_OBJECT_DIR))
612            # The dependency on TARGET is needed on windows for debuginfo files
613            # to be rebuilt properly.
614            $$($1_OUTPUT_DIR)/% : $$($1_OBJECT_DIR)/% $$($1_TARGET)
615		$(CP) $$< $$@
616          endif
617
618          # Generate debuginfo files.
619          ifeq ($(OPENJDK_TARGET_OS), windows)
620            $1_EXTRA_LDFLAGS += "-pdb:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb" \
621                "-map:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map"
622            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb \
623                $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map
624
625          else ifeq ($(OPENJDK_TARGET_OS), solaris)
626            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
627            # Setup the command line creating debuginfo files, to be run after linking.
628            # It cannot be run separately since it updates the original target file
629            #
630            # gobjcopy crashes on "empty" section headers with the SHF_ALLOC flag set.
631            # Use $(FIX_EMPTY_SEC_HDR_FLAGS) to clear the SHF_ALLOC flag (if set) from
632            # empty section headers until a fixed $(OBJCOPY) is available.
633            # An empty section header has sh_addr == 0 and sh_size == 0.
634            # This problem has only been seen on Solaris X64, but we call this tool
635            # on all Solaris builds just in case.
636            #
637            # $(OBJCOPY) --add-gnu-debuglink=... corrupts SUNW_* sections.
638            # Use $(ADD_GNU_DEBUGLINK) until a fixed $(OBJCOPY) is available.
639            $1_CREATE_DEBUGINFO_CMDS := \
640                $(FIX_EMPTY_SEC_HDR_FLAGS) $(LOG_INFO) $$($1_TARGET) $$(NEWLINE) \
641                $(OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
642                $(CD) $$($1_OUTPUT_DIR) && \
643                    $(ADD_GNU_DEBUGLINK) $(LOG_INFO) $$($1_DEBUGINFO_FILES) $$($1_TARGET)
644            $1_DEBUGINFO_EXTRA_DEPS := $(FIX_EMPTY_SEC_HDR_FLAGS) $(ADD_GNU_DEBUGLINK)
645
646          else ifeq ($(OPENJDK_TARGET_OS), linux)
647            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
648            # Setup the command line creating debuginfo files, to be run after linking.
649            # It cannot be run separately since it updates the original target file
650            $1_CREATE_DEBUGINFO_CMDS := \
651                $(OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
652                $(CD) $$($1_OUTPUT_DIR) && \
653                    $(OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET)
654
655          endif # No MacOS X support
656
657          # This dependency dance ensures that debug info files get rebuilt
658          # properly if deleted.
659          $$($1_TARGET): $$($1_DEBUGINFO_FILES)
660          $$($1_DEBUGINFO_FILES): $$($1_EXPECTED_OBJS)
661
662          ifeq ($(ZIP_DEBUGINFO_FILES), true)
663            $1_DEBUGINFO_ZIP := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz
664            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_ZIP))
665
666            # The dependency on TARGET is needed for debuginfo files
667            # to be rebuilt properly.
668            $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET)
669		$(CD) $$($1_OBJECT_DIR) \
670		&& $(ZIP) -q $$@ $$(notdir $$($1_DEBUGINFO_FILES))
671
672          else
673            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_FILES))
674          endif
675        endif
676      endif # !MacOS X
677    endif # $1_DEBUG_SYMBOLS
678  endif # !STATIC_LIBRARY
679
680  ifneq (,$$($1_LIBRARY))
681    # Generating a dynamic library.
682    $1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
683    ifeq ($(OPENJDK_TARGET_OS), windows)
684      $1_EXTRA_LDFLAGS += "-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib"
685    endif
686
687    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
688
689    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
690        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_CREATE_DEBUGINFO_CMDS)
691    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
692        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
693
694    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) \
695        $$($1_DEBUGINFO_EXTRA_DEPS) $$($1_VARDEPS_FILE)
696		$(ECHO) $(LOG_INFO) "Linking $$($1_BASENAME)"
697		$$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
698		    $(LD_OUT_OPTION)$$@ \
699		    $$($1_EXPECTED_OBJS) $$($1_RES) \
700		    $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX)
701		$$($1_CREATE_DEBUGINFO_CMDS)
702                # Touch target to make sure it has a later time stamp than the debug
703                # symbol files to avoid unnecessary relinking on rebuild.
704                ifeq ($(OPENJDK_TARGET_OS), windows)
705		  $(TOUCH) $$@
706                endif
707
708  endif
709
710  ifneq (,$$($1_STATIC_LIBRARY))
711    $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LDFLAGS_SUFFIX) \
712        $$($1_EXTRA_LDFLAGS_SUFFIX)
713    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
714        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
715
716    # Generating a static library, ie object file archive.
717    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_VARDEPS_FILE)
718	$(ECHO) $(LOG_INFO) "Archiving $$($1_STATIC_LIBRARY)"
719	$$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_EXPECTED_OBJS) \
720	    $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX)
721  endif
722
723  ifneq (,$$($1_PROGRAM))
724    # A executable binary has been specified, setup the target for it.
725    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
726
727    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
728        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_MT) \
729        $$($1_CODESIGN) $$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION)
730    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
731        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
732
733    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_MANIFEST) \
734        $$($1_DEBUGINFO_EXTRA_DEPS) $$($1_VARDEPS_FILE)
735		$(ECHO) $(LOG_INFO) "Linking executable $$($1_BASENAME)"
736		$$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
737		    $(EXE_OUT_OPTION)$$($1_TARGET) \
738		    $$($1_EXPECTED_OBJS) $$($1_RES) \
739		    $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX)
740                ifeq ($(OPENJDK_TARGET_OS), windows)
741                  ifneq ($$($1_MANIFEST), )
742		    $$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_PROGRAM).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1
743                  endif
744                endif
745                # This only works if the openjdk_codesign identity is present on the system. Let
746                # silently fail otherwise.
747                ifneq (,$(CODESIGN))
748                  ifneq (,$$($1_CODESIGN))
749		    $(CODESIGN) -s openjdk_codesign $$@
750                  endif
751                endif
752		$$($1_CREATE_DEBUGINFO_CMDS)
753                # Touch target to make sure it has a later time stamp than the debug
754                # symbol files to avoid unnecessary relinking on rebuild.
755                ifeq ($(OPENJDK_TARGET_OS), windows)
756		  $(TOUCH) $$@
757                endif
758
759  endif
760endef
761
762endif # _NATIVE_COMPILATION_GMK
763