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