NativeCompilation.gmk revision 1623:c356ee0434a0
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          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_$$(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_$$(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_$$(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
357  ifeq (,$$($1_TARGET))
358    $$(error Neither PROGRAM, LIBRARY nor STATIC_LIBRARY has been specified for SetupNativeCompilation)
359  endif
360
361  # Setup the toolchain to be used
362  $$(call SetIfEmpty, $1_TOOLCHAIN, TOOLCHAIN_DEFAULT)
363  $$(call SetIfEmpty, $1_CC, $$($$($1_TOOLCHAIN)_CC))
364  $$(call SetIfEmpty, $1_CXX, $$($$($1_TOOLCHAIN)_CXX))
365  $$(call SetIfEmpty, $1_LD, $$($$($1_TOOLCHAIN)_LD))
366  $$(call SetIfEmpty, $1_AR, $$($$($1_TOOLCHAIN)_AR))
367  $$(call SetIfEmpty, $1_AS, $$($$($1_TOOLCHAIN)_AS))
368  $$(call SetIfEmpty, $1_MT, $$($$($1_TOOLCHAIN)_MT))
369  $$(call SetIfEmpty, $1_RC, $$($$($1_TOOLCHAIN)_RC))
370  $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_CFLAGS))
371  $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_LDFLAGS))
372
373  ifneq ($$($1_MANIFEST), )
374    ifeq ($$($1_MANIFEST_VERSION), )
375      $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1)
376    endif
377  endif
378
379  # Make sure the dirs exist.
380  $$(call MakeDir,$$($1_OBJECT_DIR) $$($1_OUTPUT_DIR))
381  $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),, \
382      $$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d)))
383
384  # Find all files in the source trees. Sort to remove duplicates.
385  $1_ALL_SRCS := $$(sort $$(call CacheFind,$$($1_SRC)))
386  # Extract the C/C++ files.
387  $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
388  $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
389  ifneq ($$($1_EXCLUDE_FILES),)
390    $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES))
391  endif
392  $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter $$(NATIVE_SOURCE_EXTENSIONS),$$($1_ALL_SRCS)))
393  ifneq (,$$(strip $$($1_INCLUDE_FILES)))
394    $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS))
395  endif
396  ifeq (,$$($1_SRCS))
397    $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
398  endif
399  # There can be only a single bin dir root, no need to foreach over the roots.
400  $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX))
401  # Now we have a list of all c/c++ files to compile: $$($1_SRCS)
402  # and we have a list of all existing object files: $$($1_BINS)
403
404  # Prepend the source/bin path to the filter expressions. Then do the filtering.
405  ifneq ($$($1_INCLUDES),)
406    $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
407    $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
408  endif
409  ifneq ($$($1_EXCLUDES),)
410    $1_SRC_EXCLUDES := $$(addsuffix /%,$$($1_EXCLUDES))
411    $1_SRC_EXCLUDES += $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
412    $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
413  endif
414
415  $1_SRCS += $$($1_EXTRA_FILES)
416
417  # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides
418  # a reproducable order on the input files to the linker).
419  $1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS)))
420  $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_OBJECT_DIR)/,$$($1_EXPECTED_OBJS_FILENAMES)))
421  # Are there too many object files on disk? Perhaps because some source file was removed?
422  $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS)))
423  # Clean out the superfluous object files.
424  ifneq ($$($1_SUPERFLUOUS_OBJS),)
425    $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS))
426  endif
427
428  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CFLAGS.
429  $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS))
430  ifneq ($(DEBUG_LEVEL),release)
431    # Pickup extra debug dependent variables for CFLAGS
432    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug)
433    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
434    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug)
435  else
436    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release)
437    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
438    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release)
439  endif
440
441  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS.
442  $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS))
443  ifneq ($(DEBUG_LEVEL),release)
444    # Pickup extra debug dependent variables for CXXFLAGS
445    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug)
446    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
447    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug)
448  else
449    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release)
450    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
451    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release)
452  endif
453
454  # If no C++ flags are explicitly set, default to using the C flags.
455  # After that, we can set additional C++ flags that should not interfere
456  # with the mechanism for copying the C flags by default.
457  ifeq ($$($1_CXXFLAGS),)
458    $1_CXXFLAGS:=$$($1_CFLAGS)
459  endif
460  ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),)
461    $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS)
462  endif
463
464  ifeq ($$($1_DEBUG_SYMBOLS), true)
465    ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
466      ifdef OPENJDK
467        # Always add debug symbols
468        $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
469        $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
470      else
471        # Programs don't get the debug symbols added in the old build. It's not clear if
472        # this is intentional.
473        ifeq ($$($1_PROGRAM),)
474          $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
475          $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
476        endif
477      endif
478    endif
479  endif
480
481  ifneq (,$$($1_REORDER))
482    $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER)
483    $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER)
484  endif
485
486  # Pick up disabled warnings, if possible on this platform.
487  ifneq ($(DISABLE_WARNING_PREFIX),)
488    $1_EXTRA_CFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)))
489    $1_EXTRA_CXXFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)))
490  endif
491
492  # Check if warnings should be considered errors.
493  # Pick first binary and toolchain specific, then binary specific, then general setting.
494  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)),)
495    ifeq ($$($1_WARNINGS_AS_ERRORS),)
496      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$(WARNINGS_AS_ERRORS)
497    else
498      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$($1_WARNINGS_AS_ERRORS)
499    endif
500  endif
501
502  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), true)
503    $1_EXTRA_CFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
504    $1_EXTRA_CXXFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
505  endif
506
507  ifeq (NONE, $$($1_OPTIMIZATION))
508    $1_EXTRA_CFLAGS += $(C_O_FLAG_NONE)
509    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NONE)
510  else ifeq (LOW, $$($1_OPTIMIZATION))
511    $1_EXTRA_CFLAGS += $(C_O_FLAG_NORM)
512    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NORM)
513  else ifeq (HIGH, $$($1_OPTIMIZATION))
514    $1_EXTRA_CFLAGS += $(C_O_FLAG_HI)
515    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HI)
516  else ifeq (HIGHEST, $$($1_OPTIMIZATION))
517    $1_EXTRA_CFLAGS += $(C_O_FLAG_HIGHEST)
518    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HIGHEST)
519  else ifneq (, $$($1_OPTIMIZATION))
520    $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION))
521  endif
522
523  $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker
524
525  # Track variable changes for all variables that affect the compilation command
526  # lines for all object files in this setup. This includes at least all the
527  # variables used in the call to add_native_source below.
528  $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
529      $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) \
530      $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS) \
531      $$(foreach s, $$($1_SRCS), \
532          $$($1_$$(notdir $$s)_CFLAGS) $$($1_$$(notdir $$s)_CXXFLAGS))
533  $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \
534      $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps)
535
536  # Now call add_native_source for each source file we are going to compile.
537  $$(foreach p,$$($1_SRCS), \
538      $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \
539          $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS), \
540          $$($1_CC), \
541          $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_SYSROOT_CFLAGS), \
542          $$($1_CXX), $$($1_ASFLAGS))))
543
544  # Setup rule for printing progress info when compiling source files.
545  # This is a rough heuristic and may not always print accurate information.
546  $$($1_BUILD_INFO): $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE)
547        ifeq ($$(wildcard $$($1_TARGET)),)
548	  $(ECHO) 'Creating $$($1_BASENAME) from $$(words $$(filter-out %.vardeps, $$?)) file(s)'
549        else
550	  $(ECHO) $$(strip 'Updating $$($1_BASENAME)' \
551	      $$(if $$(filter-out %.vardeps, $$?), \
552	        'from $$(words $$(filter-out %.vardeps, $$?)) file(s)') \
553	      $$(if $$(filter %.vardeps, $$?), 'due to makefile changes'))
554        endif
555	$(TOUCH) $$@
556
557  # On windows we need to create a resource file
558  ifeq ($(OPENJDK_TARGET_OS), windows)
559    ifneq (,$$($1_VERSIONINFO_RESOURCE))
560      $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res
561      $1_RES_DEP:=$$($1_RES).d
562      $1_RES_DEP_TARGETS:=$$($1_RES).d.targets
563      -include $$($1_RES_DEP)
564      -include $$($1_RES_DEP_TARGETS)
565
566      $1_RES_VARDEPS := $$($1_RC) $$($1_RC_FLAGS)
567      $1_RES_VARDEPS_FILE := $$(call DependOnVariable, $1_RES_VARDEPS, \
568          $$($1_RES).vardeps)
569
570      $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE)
571		$(ECHO) $(LOG_INFO) "Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$(notdir $$($1_TARGET)))"
572		$$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \
573		    $$($1_VERSIONINFO_RESOURCE)
574                # Windows RC compiler does not support -showIncludes, so we mis-use CL for this.
575		$$($1_CC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) -showIncludes -nologo -TC \
576		    $(CC_OUT_OPTION)$$($1_RES_DEP).obj $$($1_VERSIONINFO_RESOURCE) > $$($1_RES_DEP).raw 2>&1 || exit 0
577		($(ECHO) $$($1_RES): \\ \
578		&& $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).raw) > $$($1_RES_DEP)
579		$(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_RES_DEP) > $$($1_RES_DEP_TARGETS)
580    endif
581  endif
582
583  # mapfile doesnt seem to be implemented on macosx (yet??)
584  ifneq ($(OPENJDK_TARGET_OS),macosx)
585    ifneq ($(OPENJDK_TARGET_OS),windows)
586      $1_REAL_MAPFILE:=$$($1_MAPFILE)
587      ifneq (,$$($1_REORDER))
588        $1_REAL_MAPFILE:=$$($1_OBJECT_DIR)/mapfile
589
590        $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER)
591		$$(MKDIR) -p $$(@D)
592		$$(CP) $$($1_MAPFILE) $$@.tmp
593		$$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp
594		$$(MV) $$@.tmp $$@
595      endif
596    endif
597  endif
598
599  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables
600  # for LDFLAGS and LDFLAGS_SUFFIX
601  $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS))
602  $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS))
603  ifneq (,$$($1_REAL_MAPFILE))
604    $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE))
605  endif
606
607  # Need to make sure TARGET is first on list
608  $1 := $$($1_TARGET)
609  ifeq ($$($1_STATIC_LIBRARY),)
610    ifeq ($$($1_DEBUG_SYMBOLS), true)
611      ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
612        ifneq ($(OPENJDK_TARGET_OS), macosx) # no MacOS X support yet
613          ifneq ($$($1_OUTPUT_DIR),$$($1_OBJECT_DIR))
614            # The dependency on TARGET is needed on windows for debuginfo files
615            # to be rebuilt properly.
616            $$($1_OUTPUT_DIR)/% : $$($1_OBJECT_DIR)/% $$($1_TARGET)
617		$(CP) $$< $$@
618          endif
619
620          # Generate debuginfo files.
621          ifeq ($(OPENJDK_TARGET_OS), windows)
622            $1_EXTRA_LDFLAGS += "-pdb:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb" \
623                "-map:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map"
624            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb \
625                $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map
626
627          else ifeq ($(OPENJDK_TARGET_OS), solaris)
628            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
629            # Setup the command line creating debuginfo files, to be run after linking.
630            # It cannot be run separately since it updates the original target file
631            #
632            # gobjcopy crashes on "empty" section headers with the SHF_ALLOC flag set.
633            # Use $(FIX_EMPTY_SEC_HDR_FLAGS) to clear the SHF_ALLOC flag (if set) from
634            # empty section headers until a fixed $(OBJCOPY) is available.
635            # An empty section header has sh_addr == 0 and sh_size == 0.
636            # This problem has only been seen on Solaris X64, but we call this tool
637            # on all Solaris builds just in case.
638            #
639            # $(OBJCOPY) --add-gnu-debuglink=... corrupts SUNW_* sections.
640            # Use $(ADD_GNU_DEBUGLINK) until a fixed $(OBJCOPY) is available.
641            $1_CREATE_DEBUGINFO_CMDS := \
642                $(FIX_EMPTY_SEC_HDR_FLAGS) $(LOG_INFO) $$($1_TARGET) $$(NEWLINE) \
643                $(OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
644                $(CD) $$($1_OUTPUT_DIR) && \
645                    $(ADD_GNU_DEBUGLINK) $(LOG_INFO) $$($1_DEBUGINFO_FILES) $$($1_TARGET)
646            $1_DEBUGINFO_EXTRA_DEPS := $(FIX_EMPTY_SEC_HDR_FLAGS) $(ADD_GNU_DEBUGLINK)
647
648          else ifeq ($(OPENJDK_TARGET_OS), linux)
649            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
650            # Setup the command line creating debuginfo files, to be run after linking.
651            # It cannot be run separately since it updates the original target file
652            $1_CREATE_DEBUGINFO_CMDS := \
653                $(OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
654                $(CD) $$($1_OUTPUT_DIR) && \
655                    $(OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET)
656
657          endif # No MacOS X support
658
659          # This dependency dance ensures that debug info files get rebuilt
660          # properly if deleted.
661          $$($1_TARGET): $$($1_DEBUGINFO_FILES)
662          $$($1_DEBUGINFO_FILES): $$($1_EXPECTED_OBJS)
663
664          ifeq ($(ZIP_DEBUGINFO_FILES), true)
665            $1_DEBUGINFO_ZIP := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz
666            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_ZIP))
667
668            # The dependency on TARGET is needed for debuginfo files
669            # to be rebuilt properly.
670            $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET)
671		$(CD) $$($1_OBJECT_DIR) \
672		&& $(ZIP) -q $$@ $$(notdir $$($1_DEBUGINFO_FILES))
673
674          else
675            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_FILES))
676          endif
677        endif
678      endif # !MacOS X
679    endif # $1_DEBUG_SYMBOLS
680  endif # !STATIC_LIBRARY
681
682  ifneq (,$$($1_LIBRARY))
683    # Generating a dynamic library.
684    $1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
685    ifeq ($(OPENJDK_TARGET_OS), windows)
686      $1_EXTRA_LDFLAGS += "-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib"
687    endif
688
689    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
690
691    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
692        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_CREATE_DEBUGINFO_CMDS)
693    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
694        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
695
696    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) \
697        $$($1_DEBUGINFO_EXTRA_DEPS) $$($1_VARDEPS_FILE)
698		$(ECHO) $(LOG_INFO) "Linking $$($1_BASENAME)"
699		$(call LogFailures, $$($1_OBJECT_DIR)/$1_link.log, $1_link, \
700		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
701		    $(LD_OUT_OPTION)$$@ \
702		    $$($1_EXPECTED_OBJS) $$($1_RES) \
703		    $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
704		$$($1_CREATE_DEBUGINFO_CMDS)
705                # Touch target to make sure it has a later time stamp than the debug
706                # symbol files to avoid unnecessary relinking on rebuild.
707                ifeq ($(OPENJDK_TARGET_OS), windows)
708		  $(TOUCH) $$@
709                endif
710
711  endif
712
713  ifneq (,$$($1_STATIC_LIBRARY))
714    $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LDFLAGS_SUFFIX) \
715        $$($1_EXTRA_LDFLAGS_SUFFIX)
716    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
717        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
718
719    # Generating a static library, ie object file archive.
720    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_VARDEPS_FILE)
721	$(ECHO) $(LOG_INFO) "Archiving $$($1_STATIC_LIBRARY)"
722	$(call LogFailures, $$($1_OBJECT_DIR)/$1_link.log, $1_link, \
723	    $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_EXPECTED_OBJS) \
724	        $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
725  endif
726
727  ifneq (,$$($1_PROGRAM))
728    # A executable binary has been specified, setup the target for it.
729    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
730
731    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
732        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_MT) \
733        $$($1_CODESIGN) $$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION)
734    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
735        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
736
737    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_MANIFEST) \
738        $$($1_DEBUGINFO_EXTRA_DEPS) $$($1_VARDEPS_FILE)
739		$(ECHO) $(LOG_INFO) "Linking executable $$($1_BASENAME)"
740		$(call LogFailures, $$($1_OBJECT_DIR)/$1_link.log, $1_link, \
741		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
742		        $(EXE_OUT_OPTION)$$($1_TARGET) \
743		        $$($1_EXPECTED_OBJS) $$($1_RES) \
744		        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
745                ifeq ($(OPENJDK_TARGET_OS), windows)
746                  ifneq ($$($1_MANIFEST), )
747		    $$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_PROGRAM).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1
748                  endif
749                endif
750                # This only works if the openjdk_codesign identity is present on the system. Let
751                # silently fail otherwise.
752                ifneq (,$(CODESIGN))
753                  ifneq (,$$($1_CODESIGN))
754		    $(CODESIGN) -s openjdk_codesign $$@
755                  endif
756                endif
757		$$($1_CREATE_DEBUGINFO_CMDS)
758                # Touch target to make sure it has a later time stamp than the debug
759                # symbol files to avoid unnecessary relinking on rebuild.
760                ifeq ($(OPENJDK_TARGET_OS), windows)
761		  $(TOUCH) $$@
762                endif
763
764  endif
765endef
766
767endif # _NATIVE_COMPILATION_GMK
768