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