NativeCompilation.gmk revision 2335:108814a7bae0
175584Sru#
275584Sru# Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3151497Sru# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4104862Sru#
575584Sru# This code is free software; you can redistribute it and/or modify it
675584Sru# under the terms of the GNU General Public License version 2 only, as
775584Sru# published by the Free Software Foundation.  Oracle designates this
875584Sru# particular file as subject to the "Classpath" exception as provided
975584Sru# by Oracle in the LICENSE file that accompanied this code.
1075584Sru#
1175584Sru# This code is distributed in the hope that it will be useful, but WITHOUT
1275584Sru# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1375584Sru# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1475584Sru# version 2 for more details (a copy is included in the LICENSE file that
1575584Sru# accompanied this code).
1675584Sru#
1775584Sru# You should have received a copy of the GNU General Public License version
1875584Sru# 2 along with this work; if not, write to the Free Software Foundation,
1975584Sru# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2075584Sru#
21151497Sru# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2275584Sru# or visit www.oracle.com if you need additional information or have any
2375584Sru# questions.
2475584Sru#
2575584Sru
2675584Sru# When you read this source. Remember that $(sort ...) has the side effect
2775584Sru# of removing duplicates. It is actually this side effect that is
2875584Sru# desired whenever sort is used below!
2975584Sru
3075584Sruifndef _NATIVE_COMPILATION_GMK
3175584Sru_NATIVE_COMPILATION_GMK := 1
3275584Sru
3375584Sruifeq (,$(_MAKEBASE_GMK))
34104862Sru  $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk)
3575584Sruendif
3675584Sru
3775584Sru################################################################################
3875584Sru# Create exported symbols file for static libraries
39104862Sru################################################################################
40104862Sru
41151497Sru# get the exported symbols from mapfiles and if there
42151497Sru# is no mapfile, get them from the archive
4379543Srudefine GetSymbols
4475584Sru  $(RM) $$(@D)/$$(basename $$(@F)).symbols; \
4575584Sru  if [ ! -z $$($1_MAPFILE) -a -e $$($1_MAPFILE) ]; then \
46104862Sru    $(ECHO) "Getting symbols from mapfile $$($1_MAPFILE)"; \
47104862Sru    $(AWK) '/global:/','/local:/' $$($1_MAPFILE) | \
4879543Sru        $(SED) -e 's/#.*//;s/global://;s/local://;s/\;//;s/^[ 	]*/_/;/^_$$$$/d' | \
4975584Sru        $(EGREP) -v "JNI_OnLoad|JNI_OnUnload|Agent_OnLoad|Agent_OnUnload|Agent_OnAttach" > \
5075584Sru        $$(@D)/$$(basename $$(@F)).symbols || true; \
5175584Sru    $(NM) $$($1_TARGET) | $(GREP)  " T " | \
5275584Sru        $(EGREP) "JNI_OnLoad|JNI_OnUnload|Agent_OnLoad|Agent_OnUnload|Agent_OnAttach" | \
5375584Sru        $(CUT) -d ' ' -f 3 >>  $$(@D)/$$(basename $$(@F)).symbols || true;\
5475584Sru  else \
5575584Sru    $(ECHO) "Getting symbols from nm"; \
5675584Sru    $(NM) -m $$($1_TARGET) | $(GREP)  "__TEXT" | \
5775584Sru        $(EGREP) -v "non-external|private extern|__TEXT,__eh_frame" | \
5875584Sru        $(SED) -e  's/.* //' > $$(@D)/$$(basename $$(@F)).symbols; \
5975584Sru  fi
6075584Sruendef
6175584Sru
6275584Sru################################################################################
63151497Sru# Define a native toolchain configuration that can be used by
64151497Sru# SetupNativeCompilation calls
65151497Sru#
66151497Sru# Parameter 1 is the name of the toolchain definition
67104862Sru#
68151497Sru# Remaining parameters are named arguments:
69151497Sru#   EXTENDS - Optional parent definition to get defaults from
70151497Sru#   CC - The C compiler
71151497Sru#   CXX - The C++ compiler
72151497Sru#   LD - The Linker
73151497Sru#   AR - Static linker
74151497Sru#   AS - Assembler
75151497Sru#   MT - Windows MT tool
76151497Sru#   RC - Windows RC tool
77151497Sru#   OBJCOPY - The objcopy tool for debug symbol handling
78151497Sru#   STRIP - The tool to use for stripping debug symbols
79151497Sru#   SYSROOT_CFLAGS - Compiler flags for using the specific sysroot
80151497Sru#   SYSROOT_LDFLAGS - Linker flags for using the specific sysroot
81151497SruDefineNativeToolchain = $(NamedParamsMacroTemplate)
82151497Srudefine DefineNativeToolchainBody
83151497Sru  # If extending another definition, get default values from that,
84104862Sru  # otherwise, nothing more needs to be done as variable assignments
85151497Sru  # already happened in NamedParamsMacroTemplate.
86114402Sru  ifneq ($$($1_EXTENDS), )
87114402Sru    $$(call SetIfEmpty, $1_CC, $$($$($1_EXTENDS)_CC))
88104862Sru    $$(call SetIfEmpty, $1_CXX, $$($$($1_EXTENDS)_CXX))
89114402Sru    $$(call SetIfEmpty, $1_LD, $$($$($1_EXTENDS)_LD))
90114402Sru    $$(call SetIfEmpty, $1_AR, $$($$($1_EXTENDS)_AR))
91114402Sru    $$(call SetIfEmpty, $1_AS, $$($$($1_EXTENDS)_AS))
92114402Sru    $$(call SetIfEmpty, $1_MT, $$($$($1_EXTENDS)_MT))
93114402Sru    $$(call SetIfEmpty, $1_RC, $$($$($1_EXTENDS)_RC))
94114402Sru    $$(call SetIfEmpty, $1_OBJCOPY, $$($$($1_EXTENDS)_OBJCOPY))
95114402Sru    $$(call SetIfEmpty, $1_STRIP, $$($$($1_EXTENDS)_STRIP))
9675584Sru    $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_EXTENDS)_SYSROOT_CFLAGS))
9779543Sru    $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_EXTENDS)_SYSROOT_LDFLAGS))
9875584Sru  endif
9975584Sruendef
10075584Sru
101151497Sru# Create a default toolchain with the main compiler and linker
102151497Sru$(eval $(call DefineNativeToolchain, TOOLCHAIN_DEFAULT, \
103151497Sru    CC := $(CC), \
104151497Sru    CXX := $(CXX), \
105151497Sru    LD := $(LD), \
106151497Sru    AR := $(AR), \
107104862Sru    AS := $(AS), \
108104862Sru    MT := $(MT), \
109151497Sru    RC := $(RC), \
110104862Sru    OBJCOPY := $(OBJCOPY), \
111104862Sru    STRIP := $(STRIP), \
112104862Sru    SYSROOT_CFLAGS := $(SYSROOT_CFLAGS), \
113104862Sru    SYSROOT_LDFLAGS := $(SYSROOT_LDFLAGS), \
114104862Sru))
115151497Sru
116151497Sru# Create a toolchain where linking is done with the C++ linker
117151497Sru$(eval $(call DefineNativeToolchain, TOOLCHAIN_LINK_CXX, \
118151497Sru    EXTENDS := TOOLCHAIN_DEFAULT, \
119151497Sru    LD := $(LDCXX), \
120151497Sru))
121151497Sru
122104862Sru# Create a toolchain with the BUILD compiler, used for build tools that
123104862Sru# are to be run during the build.
12475584Sru$(eval $(call DefineNativeToolchain, TOOLCHAIN_BUILD, \
12575584Sru    CC := $(BUILD_CC), \
12675584Sru    CXX := $(BUILD_CXX), \
12775584Sru    LD := $(BUILD_LD), \
12875584Sru    AR := $(BUILD_AR), \
12979543Sru    AS := $(BUILD_AS), \
13075584Sru    OBJCOPY := $(BUILD_OBJCOPY), \
131151497Sru    STRIP := $(BUILD_STRIP), \
13275584Sru    SYSROOT_CFLAGS := $(BUILD_SYSROOT_CFLAGS), \
133151497Sru    SYSROOT_LDFLAGS := $(BUILD_SYSROOT_LDFLAGS), \
13475584Sru))
13575584Sru
13675584Sru# BUILD toolchain with the C++ linker
13775584Sru$(eval $(call DefineNativeToolchain, TOOLCHAIN_BUILD_LINK_CXX, \
13875584Sru    EXTENDS := TOOLCHAIN_BUILD, \
13975584Sru    LD := $(BUILD_LDCXX), \
14075584Sru))
14175584Sru
14275584Sru################################################################################
14375584Sru
14475584Sru# Extensions of files handled by this macro.
14575584SruNATIVE_SOURCE_EXTENSIONS := %.s %.S %.c %.cpp %.cc %.m %.mm
14675584Sru
14775584Sru# Replaces native source extensions with the object file extension in a string.
14875584Sru# Param 1: the string containing source file names with extensions
14975584Sru# The surrounding strip is needed to keep additional whitespace out
15075584Srudefine replace_with_obj_extension
15175584Sru$(strip \
15275584Sru  $(foreach extension, $(NATIVE_SOURCE_EXTENSIONS), \
15375584Sru      $(patsubst $(extension),%$(OBJ_SUFFIX),$(filter $(extension),$1))) \
15475584Sru)
15575584Sruendef
15675584Sru
15775584Sruifeq ($(OPENJDK_BUILD_OS_ENV), windows.cygwin)
15875584Sru  UNIX_PATH_PREFIX := /cygdrive
15975584Sruelse ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys)
16075584Sru  UNIX_PATH_PREFIX :=
16175584Sruendif
16275584Sru
16375584Sru# This pattern is used to transform the output of the microsoft CL compiler
16475584Sru# into a make syntax dependency file (.d)
16575584SruWINDOWS_SHOWINCLUDE_SED_PATTERN := \
16675584Sru    -e '/^Note: including file:/!d' \
16775584Sru    -e 's|Note: including file: *||' \
16875584Sru    -e 's|\\|/|g' \
16975584Sru    -e 's|^\([a-zA-Z]\):|$(UNIX_PATH_PREFIX)/\1|g' \
17075584Sru    -e '\|$(TOPDIR)|I !d' \
17175584Sru    -e 's|$$$$| \\|g' \
17275584Sru    #
17375584Sru
174104862Sru# This pattern is used to transform a dependency file (.d) to a list
17575584Sru# of make targets for dependent files (.d.targets)
176114402SruDEPENDENCY_TARGET_SED_PATTERN := \
177114402Sru    -e 's/\#.*//' \
178114402Sru    -e 's/^[^:]*: *//' \
179114402Sru    -e 's/ *\\$$$$//' \
180114402Sru    -e 's/^[	 ]*//' \
181114402Sru    -e '/^$$$$/ d' \
18275584Sru    -e 's/$$$$/ :/' \
183114402Sru    #
184114402Sru
18575584Srudefine add_native_source
18675584Sru  # param 1 = BUILD_MYPACKAGE
18775584Sru  # parma 2 = the source file name (..../alfa.c or .../beta.cpp)
18875584Sru  # param 3 = the bin dir that stores all .o (.obj) and .d files.
18975584Sru  # param 4 = the c flags to the compiler
19075584Sru  # param 5 = the c compiler
19175584Sru  # param 6 = the c++ flags to the compiler
19275584Sru  # param 7 = the c++ compiler
19375584Sru  # param 8 = the flags to the assembler
19475584Sru  # param 9 = set to disable THIS_FILE
19575584Sru
19675584Sru  ifeq ($9, )
19775584Sru    $1_$2_THIS_FILE = -DTHIS_FILE='"$$(<F)"'
19875584Sru  endif
199114402Sru
20075584Sru  ifeq ($$($1_$(notdir $2)_OPTIMIZATION), )
20175584Sru    $1_$(notdir $2)_OPT_CFLAGS := $$($1_OPT_CFLAGS)
20275584Sru    $1_$(notdir $2)_OPT_CXXFLAGS := $$($1_OPT_CXXFLAGS)
20375584Sru  else
20475584Sru    ifeq (NONE, $$($1_$(notdir $2)_OPTIMIZATION))
20575584Sru      $1_$(notdir $2)_OPT_CFLAGS := $(C_O_FLAG_NONE)
20675584Sru      $1_$(notdir $2)_OPT_CXXFLAGS := $(CXX_O_FLAG_NONE)
20775584Sru    else ifeq (LOW, $$($1_$(notdir $2)_OPTIMIZATION))
20879847Sru      $1_$(notdir $2)_OPT_CFLAGS := $(C_O_FLAG_NORM)
20979847Sru      $1_$(notdir $2)_OPT_CXXFLAGS := $(CXX_O_FLAG_NORM)
21079847Sru    else ifeq (HIGH, $$($1_$(notdir $2)_OPTIMIZATION))
21179847Sru      $1_$(notdir $2)_OPT_CFLAGS := $(C_O_FLAG_HI)
21279847Sru      $1_$(notdir $2)_OPT_CXXFLAGS := $(CXX_O_FLAG_HI)
21379847Sru    else ifeq (HIGHEST, $$($1_$(notdir $2)_OPTIMIZATION))
21479847Sru      $1_$(notdir $2)_OPT_CFLAGS := $(C_O_FLAG_HIGHEST)
21579847Sru      $1_$(notdir $2)_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST)
21679847Sru    else ifeq (HIGHEST_JVM, $$($1_$(notdir $2)_OPTIMIZATION))
21779847Sru      $1_$(notdir $2)_OPT_CFLAGS := $(C_O_FLAG_HIGHEST_JVM)
21879847Sru      $1_$(notdir $2)_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST_JVM)
21979847Sru    else ifeq (SIZE, $$($1_$(notdir $2)_OPTIMIZATION))
22079847Sru      $1_$(notdir $2)_OPT_CFLAGS := $(C_O_FLAG_SIZE)
22179847Sru      $1_$(notdir $2)_OPT_CXXFLAGS := $(CXX_O_FLAG_SIZE)
22279847Sru    else
22379847Sru      $$(error Unknown value for OPTIMIZATION: $$($1_$(notdir $2)_OPTIMIZATION))
22479847Sru    endif
22579847Sru  endif
22679847Sru
22779847Sru  ifneq ($$($1_PRECOMPILED_HEADER), )
22879847Sru    ifeq ($$(filter $$(notdir $2), $$($1_PRECOMPILED_HEADER_EXCLUDE)), )
22979543Sru      $1_$2_USE_PCH_FLAGS := $$($1_USE_PCH_FLAGS)
23075584Sru    endif
23175584Sru  endif
23275584Sru
23379543Sru  ifneq (,$$(filter %.c,$2))
23475584Sru    # Compile as a C file
23575584Sru    $1_$2_FLAGS=$(CFLAGS_CCACHE) $$($1_$2_USE_PCH_FLAGS) $4 \
23675584Sru        $$($1_$(notdir $2)_OPT_CFLAGS) \
23775584Sru        $$($1_$(notdir $2)_CFLAGS) $$($1_$2_THIS_FILE) -c
238114402Sru    $1_$2_COMP=$5
239114402Sru    $1_$2_DEP_FLAG:=$(C_FLAG_DEPS)
240114402Sru  else ifneq (,$$(filter %.m,$2))
241114402Sru    # Compile as an Objective-C file
242114402Sru    $1_$2_FLAGS=-x objective-c $(CFLAGS_CCACHE) $$($1_$2_USE_PCH_FLAGS) $4 \
243114402Sru        $$($1_$(notdir $2)_OPT_CFLAGS) \
244114402Sru        $$($1_$(notdir $2)_CFLAGS) $$($1_$2_THIS_FILE) -c
245114402Sru    $1_$2_COMP=$5
246114402Sru    $1_$2_DEP_FLAG:=$(C_FLAG_DEPS)
247114402Sru  else ifneq (,$$(filter %.s %.S,$2))
248114402Sru    # Compile as assembler file
249114402Sru    $1_$2_FLAGS=$8
250114402Sru    $1_$2_COMP=$(AS)
251114402Sru    $1_$2_DEP_FLAG:=
252114402Sru  else ifneq (,$$(filter %.cpp,$2)$$(filter %.cc,$2)$$(filter %.mm,$2))
253114402Sru    # Compile as a C++ or Objective-C++ file
254114402Sru    $1_$2_FLAGS=$(CFLAGS_CCACHE) $$($1_$2_USE_PCH_FLAGS) $6 \
25579543Sru        $$($1_$(notdir $2)_OPT_CXXFLAGS) \
25675584Sru        $$($1_$(notdir $2)_CXXFLAGS) $$($1_$2_THIS_FILE) -c
257114402Sru    $1_$2_COMP=$7
258114402Sru    $1_$2_DEP_FLAG:=$(CXX_FLAG_DEPS)
259114402Sru  else
260114402Sru    $$(error Internal error in NativeCompilation.gmk: no compiler for file $2)
261114402Sru  endif
262114402Sru  # Generate the .o (.obj) file name and place it in the bin dir.
26375584Sru  $1_$2_OBJ := $3/$$(call replace_with_obj_extension, $$(notdir $2))
26475584Sru  # Only continue if this object file hasn't been processed already. This lets the first found
26575584Sru  # source file override any other with the same name.
266104862Sru  ifeq (,$$(findstring $$($1_$2_OBJ),$$($1_OBJS_SO_FAR)))
26775584Sru    $1_OBJS_SO_FAR+=$$($1_$2_OBJ)
26875584Sru    ifeq (,$$(filter %.s %.S,$2))
26975584Sru      # And this is the dependency file for this obj file.
27075584Sru      $1_$2_DEP:=$$(patsubst %$(OBJ_SUFFIX),%.d,$$($1_$2_OBJ))
27175584Sru      # The dependency target file lists all dependencies as empty targets
272114402Sru      # to avoid make error "No rule to make target" for removed files
27375584Sru      $1_$2_DEP_TARGETS:=$$(patsubst %$(OBJ_SUFFIX),%.d.targets,$$($1_$2_OBJ))
27475584Sru
27575584Sru      # Include previously generated dependency information. (if it exists)
27675584Sru      -include $$($1_$2_DEP)
27775584Sru      -include $$($1_$2_DEP_TARGETS)
27875584Sru
27975584Sru      ifeq ($(TOOLCHAIN_TYPE), microsoft)
28075584Sru        # To avoid name clashes between pdbs for objects and libs/execs, put
28175584Sru        # object pdbs in a separate subdir.
28279543Sru        $1_$2_DEBUG_OUT_FLAGS:=-Fd$$(strip $$(patsubst $$($1_OBJECT_DIR)/%, \
28375584Sru            $$($1_OBJECT_DIR)/pdb/%, $$(patsubst %$(OBJ_SUFFIX),%.pdb,$$($1_$2_OBJ))))
28475584Sru      endif
28575584Sru    endif
28675584Sru
28775584Sru    ifneq ($$(strip $$($1_$(notdir $2)_CFLAGS) $$($1_$(notdir $2)_CXXFLAGS) \
28879543Sru        $$($1_$(notdir $2)_OPTIMIZATION)), )
28975584Sru      $1_$2_VARDEPS := $$($1_$(notdir $2)_CFLAGS) $$($1_$(notdir $2)_CXXFLAGS) \
29075584Sru          $$($1_$(notdir $2)_OPT_CFLAGS) $$($1_$(notdir $2)_OPT_CXXFLAGS)
29175584Sru      $1_$2_VARDEPS_FILE := $$(call DependOnVariable, $1_$2_VARDEPS, \
292104862Sru          $$(patsubst %$(OBJ_SUFFIX),%.vardeps,$$($1_$2_OBJ)))
29375584Sru    endif
29475584Sru
29575584Sru    $$($1_$2_OBJ) : $2 $$($1_COMPILE_VARDEPS_FILE) $$($1_$2_VARDEPS_FILE) | $$($1_BUILD_INFO)
29675584Sru	$$(call LogInfo, Compiling $$(notdir $2) (for $$(notdir $$($1_TARGET))))
29775584Sru	$$(call MakeDir, $$(@D) $$(@D)/pdb)
29875584Sru        ifneq ($(TOOLCHAIN_TYPE), microsoft)
29975584Sru          ifeq ($(TOOLCHAIN_TYPE)$$(filter %.s,$2), solstudio)
30075584Sru            # The Solaris studio compiler doesn't output the full path to the object file in the
30175584Sru            # generated deps files. Fixing it with sed. If compiling assembly, don't try this.
30275584Sru	    $$(call ExecuteWithLog, $$@, \
30375584Sru	        $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP).tmp $(CC_OUT_OPTION)$$($1_$2_OBJ) $2)
30475584Sru	    $(SED) 's|^$$(@F):|$$@:|' $$($1_$2_DEP).tmp > $$($1_$2_DEP)
30575584Sru          else
30675584Sru	    $$(call ExecuteWithLog, $$@, \
30775584Sru	        $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP) $(CC_OUT_OPTION)$$($1_$2_OBJ) $2)
30875584Sru          endif
30975584Sru          # Create a dependency target file from the dependency file.
31075584Sru          # Solution suggested by http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
31175584Sru          ifneq ($$($1_$2_DEP),)
312114402Sru	    $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_$2_DEP) > $$($1_$2_DEP_TARGETS)
31375584Sru          endif
31475584Sru        else
31575584Sru          # The Visual Studio compiler lacks a feature for generating make dependencies, but by
31679543Sru          # setting -showIncludes, all included files are printed. These are filtered out and
31775584Sru          # parsed into make dependences.
318104862Sru          # Keep as much as possible on one execution line for best performance on Windows.
31975584Sru          # No need to save exit code from compilation since pipefail is always active on
32075584Sru          # Windows.
32175584Sru	  $$(call ExecuteWithLog, $$@, \
32275584Sru	      $$($1_$2_COMP) $$($1_$2_FLAGS) -showIncludes $$($1_$2_DEBUG_OUT_FLAGS) \
32375584Sru	          $(CC_OUT_OPTION)$$($1_$2_OBJ) $2) \
32475584Sru	      | $(GREP) -v -e "^Note: including file:" \
32575584Sru	          -e "^$(notdir $2)$$$$" || test "$$$$?" = "1" ; \
32675584Sru	  $(ECHO) $$@: \\ > $$($1_$2_DEP) ; \
32779543Sru	  $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_$2_OBJ).log \
32875584Sru	      | $(SORT) -u >> $$($1_$2_DEP) ; \
32975584Sru	  $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_$2_DEP) > $$($1_$2_DEP_TARGETS)
33075584Sru        endif
33175584Sru  endif
33275584Sruendef
33375584Sru
33475584Sru# Setup make rules for creating a native binary (a shared library or an
335151497Sru# executable).
336151497Sru#
33775584Sru# Parameter 1 is the name of the rule. This name is used as variable prefix,
33875584Sru# and the targets generated are listed in a variable by that name.
33975584Sru#
34075584Sru# Remaining parameters are named arguments. These include:
341114402Sru#   TOOLCHAIN Name of toolchain setup to use. Defaults to TOOLCHAIN_DEFAULT.
342104862Sru#   SRC one or more directory roots to scan for C/C++ files.
34375584Sru#   CFLAGS the compiler flags to be used, used both for C and C++.
34475584Sru#   CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS.
34575584Sru#   LDFLAGS the linker flags to be used, used both for C and C++.
34679543Sru#   LIBS the libraries to link to
34775584Sru#   ARFLAGS the archiver flags to be used
34875584Sru#   OBJECT_DIR the directory where we store the object files
34975584Sru#   OUTPUT_DIR the directory where the resulting binary is put
35075584Sru#   LIBRARY the resulting library file
35175584Sru#   PROGRAM the resulting exec file
35275584Sru#   INCLUDES only pick source from these directories
35375584Sru#   EXCLUDES do not pick source from these directories
35475584Sru#   INCLUDE_FILES only compile exactly these files!
35575584Sru#   EXCLUDE_FILES with these names
35675584Sru#   EXCLUDE_PATTERN exclude files matching any of these substrings
35775584Sru#   EXTRA_FILES List of extra files not in any of the SRC dirs
358114402Sru#   EXTRA_OBJECT_FILES List of extra object files to include when linking
359104862Sru#   VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run
36075584Sru#   RC_FLAGS flags for RC.
36175584Sru#   EMBED_MANIFEST if true, embed manifest on Windows.
36275584Sru#   MAPFILE mapfile
36379543Sru#   REORDER reorder file
36475584Sru#   USE_MAPFILE_FOR_SYMBOLS if true and this is a STATIC_BUILD, just copy the
36575584Sru#       mapfile for the output symbols file
36675584Sru#   CC the compiler to use, default is $(CC)
36775584Sru#   LD the linker to use, default is $(LD)
36875584Sru#   OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST, HIGHEST_JVM, SIZE
36979543Sru#   DISABLED_WARNINGS_<toolchain> Disable the given warnings for the specified toolchain
37075584Sru#   DISABLED_WARNINGS_C_<toolchain> Disable the given warnings for the specified toolchain
37175584Sru#       when compiling C code
372104862Sru#   DISABLED_WARNINGS_CXX_<toolchain> Disable the given warnings for the specified
37375584Sru#       toolchain when compiling C++ code
37475584Sru#   STRIP_SYMBOLS Set to true to strip the final binary if the toolchain allows for it
37579543Sru#   DEBUG_SYMBOLS Set to false to disable generation of debug symbols
37675584Sru#   COPY_DEBUG_SYMBOLS Set to false to override global setting of debug symbol copying
37775584Sru#   ZIP_EXTERNAL_DEBUG_SYMBOLS Set to false to override global setting of debug symbol
37875584Sru#       zipping
37975584Sru#   CFLAGS_DEBUG_SYMBOLS Overrides the default cflags for enabling debug symbols
38075584Sru#   CXXFLAGS_DEBUG_SYMBOLS Overrides the default cxxflags for enabling debug symbols
38179543Sru#   STRIPFLAGS Optionally change the flags given to the strip command
38275584Sru#   PRECOMPILED_HEADER Header file to use as precompiled header
38375584Sru#   PRECOMPILED_HEADER_EXCLUDE List of source files that should not use PCH
38475584SruSetupNativeCompilation = $(NamedParamsMacroTemplate)
38575584Srudefine SetupNativeCompilationBody
38675584Sru
38775584Sru  # If we're doing a static build and producing a library
38879543Sru  # force it to be a static library and remove the -l libraries
38975584Sru  ifeq ($(STATIC_BUILD), true)
39075584Sru    ifneq ($$($1_LIBRARY),)
39175584Sru      $1_STATIC_LIBRARY := $$($1_LIBRARY)
392104862Sru      $1_LIBRARY :=
393104862Sru    endif
39475584Sru  endif
39575584Sru
39675584Sru  ifneq (,$$($1_BIN))
39775584Sru    $$(error BIN has been replaced with OBJECT_DIR)
39879543Sru  endif
399151497Sru
400151497Sru  ifneq (,$$($1_LIB))
40175584Sru    $$(error LIB has been replaced with LIBRARY)
40275584Sru  endif
40375584Sru
40475584Sru  ifneq (,$$($1_EXE))
40575584Sru    $$(error EXE has been replaced with PROGRAM)
40675584Sru  endif
40775584Sru
40875584Sru  ifneq (,$$($1_LIBRARY))
40975584Sru    ifeq (,$$($1_OUTPUT_DIR))
41075584Sru      $$(error LIBRARY requires OUTPUT_DIR)
41175584Sru    endif
41275584Sru
41375584Sru    ifneq ($$($1_LIBRARY),$(basename $$($1_LIBRARY)))
41475584Sru      $$(error directory of LIBRARY should be specified using OUTPUT_DIR)
41575584Sru    endif
416114402Sru
417114402Sru    ifneq (,$(findstring $(SHARED_LIBRARY_SUFFIX),$$($1_LIBRARY)))
41875584Sru      $$(error LIBRARY should be specified without SHARED_LIBRARY_SUFFIX: $(SHARED_LIBRARY_SUFFIX))
41975584Sru    endif
42075584Sru
42175584Sru    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_LIBRARY)))
422104862Sru      $$(error LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
42375584Sru    endif
42475584Sru
42575584Sru    ifeq ($$($1_SUFFIX), )
42675584Sru      $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX)
427114402Sru    endif
428114402Sru
429114402Sru    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_LIBRARY)$$($1_SUFFIX)
430114402Sru    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
43175584Sru    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_LIBRARY)
43275584Sru  endif
43375584Sru
434104862Sru  ifneq (,$$($1_STATIC_LIBRARY))
435151497Sru    ifeq (,$$($1_OUTPUT_DIR))
43675584Sru      $$(error STATIC_LIBRARY requires OUTPUT_DIR)
437151497Sru    endif
438104862Sru
439104862Sru    ifneq ($$($1_STATIC_LIBRARY),$(basename $$($1_STATIC_LIBRARY)))
440104862Sru      $$(error directory of STATIC_LIBRARY should be specified using OUTPUT_DIR)
44179543Sru    endif
44275584Sru
44375584Sru    ifneq (,$(findstring $(STATIC_LIBRARY_SUFFIX),$$($1_STATIC_LIBRARY)))
44475584Sru      $$(error STATIC_LIBRARY should be specified without STATIC_LIBRARY_SUFFIX: $(STATIC_LIBRARY_SUFFIX))
44575584Sru    endif
44675584Sru
44775584Sru    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_STATIC_LIBRARY)))
44875584Sru      $$(error STATIC_LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
44975584Sru    endif
45075584Sru
45175584Sru    ifeq ($$($1_SUFFIX), )
45275584Sru      $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX)
45379543Sru    endif
45475584Sru
45575584Sru    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)$$($1_SUFFIX)
45675584Sru    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
45775584Sru    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)
45875584Sru  endif
459104862Sru
46075584Sru  ifneq (,$$($1_PROGRAM))
46175584Sru    ifeq (,$$($1_OUTPUT_DIR))
46275584Sru      $$(error PROGRAM requires OUTPUT_DIR)
46375584Sru    endif
46475584Sru
46575584Sru    ifneq ($$($1_PROGRAM),$(basename $$($1_PROGRAM)))
46675584Sru      $$(error directory of PROGRAM should be specified using OUTPUT_DIR)
46775584Sru    endif
46875584Sru
46979543Sru    ifneq (,$(findstring $(EXE_SUFFIX),$$($1_PROGRAM)))
47075584Sru      $$(error PROGRAM should be specified without EXE_SUFFIX: $(EXE_SUFFIX))
47175584Sru    endif
47275584Sru
47375584Sru    ifeq ($$($1_SUFFIX), )
474104862Sru      $1_SUFFIX := $(EXE_SUFFIX)
47575584Sru    endif
47675584Sru
47775584Sru    $1_BASENAME:=$$($1_PROGRAM)$$($1_SUFFIX)
47875584Sru    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
47975584Sru    $1_NOSUFFIX:=$$($1_PROGRAM)
48075584Sru  endif
48175584Sru  $1_SAFE_NAME := $$(strip $$(subst /,_, $1))
48279543Sru
48375584Sru  ifeq (,$$($1_TARGET))
484151497Sru    $$(error Neither PROGRAM, LIBRARY nor STATIC_LIBRARY has been specified for SetupNativeCompilation)
48575584Sru  endif
48675584Sru
48775584Sru  # Setup the toolchain to be used
48875584Sru  $$(call SetIfEmpty, $1_TOOLCHAIN, TOOLCHAIN_DEFAULT)
48975584Sru  $$(call SetIfEmpty, $1_CC, $$($$($1_TOOLCHAIN)_CC))
490151497Sru  $$(call SetIfEmpty, $1_CXX, $$($$($1_TOOLCHAIN)_CXX))
49175584Sru  $$(call SetIfEmpty, $1_LD, $$($$($1_TOOLCHAIN)_LD))
492151497Sru  $$(call SetIfEmpty, $1_AR, $$($$($1_TOOLCHAIN)_AR))
49375584Sru  $$(call SetIfEmpty, $1_AS, $$($$($1_TOOLCHAIN)_AS))
49475584Sru  $$(call SetIfEmpty, $1_MT, $$($$($1_TOOLCHAIN)_MT))
49575584Sru  $$(call SetIfEmpty, $1_RC, $$($$($1_TOOLCHAIN)_RC))
49679543Sru  $$(call SetIfEmpty, $1_OBJCOPY, $$($$($1_TOOLCHAIN)_OBJCOPY))
49775584Sru  $$(call SetIfEmpty, $1_STRIP, $$($$($1_TOOLCHAIN)_STRIP))
49875584Sru  $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_CFLAGS))
49975584Sru  $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_LDFLAGS))
50075584Sru
50175584Sru  ifneq ($$($1_MANIFEST), )
50275584Sru    ifeq ($$($1_MANIFEST_VERSION), )
50375584Sru      $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1)
50475584Sru    endif
50575584Sru  endif
506151497Sru
50775584Sru  # Make sure the dirs exist.
50875584Sru  $$(call MakeDir,$$($1_OBJECT_DIR) $$($1_OUTPUT_DIR))
50975584Sru  $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),, \
51079543Sru      $$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d)))
51175584Sru
51275584Sru  # Find all files in the source trees. Sort to remove duplicates.
51375584Sru  $1_SRCS := $$(sort $$(call CacheFind,$$($1_SRC)))
51475584Sru  $1_SRCS := $$(filter $$(NATIVE_SOURCE_EXTENSIONS), $$($1_SRCS))
51575584Sru  # Extract the C/C++ files.
51675584Sru  ifneq ($$($1_EXCLUDE_PATTERNS), )
51775584Sru    # We must not match the exclude pattern against the src root(s).
51875584Sru    $1_SRCS_WITHOUT_ROOTS := $$($1_SRCS)
51975584Sru    $$(foreach i,$$($1_SRC),$$(eval $1_SRCS_WITHOUT_ROOTS := $$(patsubst \
52075584Sru        $$i/%,%, $$($1_SRCS_WITHOUT_ROOTS))))
52175584Sru    $1_ALL_EXCLUDE_FILES :=  $$(call containing, $$($1_EXCLUDE_PATTERNS), \
52275584Sru        $$($1_SRCS_WITHOUT_ROOTS))
52375584Sru  endif
52479543Sru  ifneq ($$($1_EXCLUDE_FILES),)
52575584Sru    $1_ALL_EXCLUDE_FILES += $$($1_EXCLUDE_FILES)
52675584Sru  endif
52775584Sru  ifneq ($$($1_ALL_EXCLUDE_FILES),)
52875584Sru    $1_EXCLUDE_FILES_PAT := $$($1_ALL_EXCLUDE_FILES) \
52975584Sru        $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_ALL_EXCLUDE_FILES)))
53075584Sru    $1_EXCLUDE_FILES_PAT := $$(addprefix %,$$($1_EXCLUDE_FILES_PAT))
53175584Sru    $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES_PAT),$$($1_SRCS))
53275584Sru  endif
53375584Sru  ifneq ($$($1_INCLUDE_FILES), )
53475584Sru    $1_INCLUDE_FILES_PAT := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
53575584Sru    $1_SRCS := $$(filter $$($1_INCLUDE_FILES_PAT),$$($1_SRCS))
53675584Sru  endif
53775584Sru  # There can be only a single bin dir root, no need to foreach over the roots.
53879543Sru  $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX))
53975584Sru  # Now we have a list of all c/c++ files to compile: $$($1_SRCS)
54075584Sru  # and we have a list of all existing object files: $$($1_BINS)
54175584Sru
54275584Sru  # Prepend the source/bin path to the filter expressions. Then do the filtering.
54375584Sru  ifneq ($$($1_INCLUDES),)
54475584Sru    $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
54575584Sru    $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
54675584Sru  endif
54775584Sru  ifneq ($$($1_EXCLUDES),)
54875584Sru    $1_SRC_EXCLUDES := $$(addsuffix /%,$$($1_EXCLUDES))
54979543Sru    $1_SRC_EXCLUDES += $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
55075584Sru    $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
55175584Sru  endif
55275584Sru
55375584Sru  $1_SRCS += $$($1_EXTRA_FILES)
55475584Sru
55575584Sru  ifeq (,$$($1_SRCS))
55675584Sru    $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
55775584Sru  endif
55875584Sru
55975584Sru  # Calculate the expected output from compiling the sources
56079543Sru  $1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS)))
56175584Sru  $1_EXPECTED_OBJS := $$(addprefix $$($1_OBJECT_DIR)/,$$($1_EXPECTED_OBJS_FILENAMES))
56275584Sru  # Are there too many object files on disk? Perhaps because some source file was removed?
56375584Sru  $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS)))
56475584Sru  # Clean out the superfluous object files.
56575584Sru  ifneq ($$($1_SUPERFLUOUS_OBJS),)
56675584Sru    $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS))
56775584Sru  endif
56875584Sru  # Sort to remove dupliates and provide a reproducable order on the input files to the linker.
56975584Sru  $1_ALL_OBJS := $$(sort $$($1_EXPECTED_OBJS) $$($1_EXTRA_OBJECT_FILES))
57079543Sru
57175584Sru  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CFLAGS.
57275584Sru  $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS))
57375584Sru  ifneq ($(DEBUG_LEVEL),release)
57475584Sru    # Pickup extra debug dependent variables for CFLAGS
57575584Sru    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug)
57675584Sru    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
57775584Sru    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug)
57875584Sru  else
579104862Sru    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release)
580104862Sru    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
581104862Sru    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release)
582104862Sru  endif
583104862Sru
584104862Sru  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS.
585104862Sru  $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS))
586104862Sru  ifneq ($(DEBUG_LEVEL),release)
587104862Sru    # Pickup extra debug dependent variables for CXXFLAGS
588104862Sru    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug)
589104862Sru    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
590104862Sru    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug)
591104862Sru  else
592104862Sru    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release)
593104862Sru    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
594104862Sru    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release)
595104862Sru  endif
596104862Sru
597104862Sru  # If no C++ flags are explicitly set, default to using the C flags.
598104862Sru  # After that, we can set additional C++ flags that should not interfere
599104862Sru  # with the mechanism for copying the C flags by default.
600104862Sru  ifeq ($$($1_CXXFLAGS),)
601104862Sru    $1_CXXFLAGS:=$$($1_CFLAGS)
602104862Sru  endif
603104862Sru  ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),)
604104862Sru    $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS)
605104862Sru  endif
606104862Sru
607104862Sru  ifeq ($(COMPILE_WITH_DEBUG_SYMBOLS), true)
608104862Sru    $$(call SetIfEmpty, $1_CFLAGS_DEBUG_SYMBOLS, $(CFLAGS_DEBUG_SYMBOLS))
609104862Sru    $$(call SetIfEmpty, $1_CXXFLAGS_DEBUG_SYMBOLS, $(CXXFLAGS_DEBUG_SYMBOLS))
610104862Sru    $1_EXTRA_CFLAGS += $$($1_CFLAGS_DEBUG_SYMBOLS)
611104862Sru    $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_DEBUG_SYMBOLS)
612104862Sru  endif
613104862Sru
614104862Sru  ifneq (,$$($1_REORDER))
61579543Sru    $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER)
61679543Sru    $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER)
617104862Sru  endif
61879543Sru
61975584Sru  # Pass the library name for static JNI library naming
62075584Sru  ifneq ($$($1_STATIC_LIBRARY),)
62175584Sru    $1_EXTRA_CFLAGS += -DLIBRARY_NAME=$$($1_STATIC_LIBRARY)
62275584Sru    $1_EXTRA_CXXFLAGS += -DLIBRARY_NAME=$$($1_STATIC_LIBRARY)
623114402Sru  endif
624114402Sru
625114402Sru  # Pick up disabled warnings, if possible on this platform.
626114402Sru  ifneq ($(DISABLE_WARNING_PREFIX),)
627114402Sru    $1_EXTRA_CFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), \
628114402Sru        $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)) \
629114402Sru        $$($1_DISABLED_WARNINGS_C_$(TOOLCHAIN_TYPE)))
630114402Sru    $1_EXTRA_CXXFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), \
631104862Sru        $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)) \
63275584Sru        $$($1_DISABLED_WARNINGS_CXX_$(TOOLCHAIN_TYPE)))
633202358Sru  endif
634202358Sru
635202358Sru  # Check if warnings should be considered errors.
636202358Sru  # Pick first binary and toolchain specific, then binary specific, then general setting.
637202358Sru  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)),)
638202358Sru    ifeq ($$($1_WARNINGS_AS_ERRORS),)
639202358Sru      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$(WARNINGS_AS_ERRORS)
640202358Sru    else
641202358Sru      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$($1_WARNINGS_AS_ERRORS)
642202358Sru    endif
643202358Sru  endif
644202358Sru
64575584Sru  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), true)
64675584Sru    $1_EXTRA_CFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
64775584Sru    $1_EXTRA_CXXFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
64875584Sru  endif
64975584Sru
65075584Sru  ifeq (NONE, $$($1_OPTIMIZATION))
651    $1_OPT_CFLAGS := $(C_O_FLAG_NONE)
652    $1_OPT_CXXFLAGS := $(CXX_O_FLAG_NONE)
653  else ifeq (LOW, $$($1_OPTIMIZATION))
654    $1_OPT_CFLAGS := $(C_O_FLAG_NORM)
655    $1_OPT_CXXFLAGS := $(CXX_O_FLAG_NORM)
656  else ifeq (HIGH, $$($1_OPTIMIZATION))
657    $1_OPT_CFLAGS := $(C_O_FLAG_HI)
658    $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HI)
659  else ifeq (HIGHEST, $$($1_OPTIMIZATION))
660    $1_OPT_CFLAGS := $(C_O_FLAG_HIGHEST)
661    $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST)
662  else ifeq (HIGHEST_JVM, $$($1_OPTIMIZATION))
663    $1_OPT_CFLAGS := $(C_O_FLAG_HIGHEST_JVM)
664    $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST_JVM)
665  else ifeq (SIZE, $$($1_OPTIMIZATION))
666    $1_OPT_CFLAGS := $(C_O_FLAG_SIZE)
667    $1_OPT_CXXFLAGS := $(CXX_O_FLAG_SIZE)
668  else ifneq (, $$($1_OPTIMIZATION))
669    $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION))
670  endif
671
672  $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker
673
674  # Track variable changes for all variables that affect the compilation command
675  # lines for all object files in this setup. This includes at least all the
676  # variables used in the call to add_native_source below.
677  $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
678      $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_OPT_CFLAGS) $$($1_OPT_CXXFLAGS) \
679      $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS)
680  $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \
681      $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps)
682
683  ifneq ($$($1_PRECOMPILED_HEADER), )
684    ifeq ($(USE_PRECOMPILED_HEADER), 1)
685      ifeq ($(TOOLCHAIN_TYPE), microsoft)
686        $1_PCH_FILE := $$($1_OBJECT_DIR)/$1.pch
687        $1_GENERATED_PCH_SRC := $$($1_OBJECT_DIR)/$1_pch.cpp
688        $1_GENERATED_PCH_OBJ := $$($1_OBJECT_DIR)/$1_pch.obj
689
690        $$(eval $$(call add_native_source,$1,$$($1_GENERATED_PCH_SRC), \
691            $$($1_OBJECT_DIR),,, \
692            $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_SYSROOT_CFLAGS) \
693                -Fp$$($1_PCH_FILE) -Yc$$(notdir $$($1_PRECOMPILED_HEADER)), \
694            $$($1_CXX),,no_this_file))
695
696        $1_USE_PCH_FLAGS := \
697            -Fp$$($1_PCH_FILE) -Yu$$(notdir $$($1_PRECOMPILED_HEADER))
698
699        $$($1_ALL_OBJS): $$($1_GENERATED_PCH_OBJ)
700
701        # Explicitly add the pch obj file first to ease comparing to old
702        # hotspot build.
703        $1_ALL_OBJS := $$($1_GENERATED_PCH_OBJ) $$($1_ALL_OBJS)
704
705        $$($1_GENERATED_PCH_SRC):
706		$(ECHO) "#include \"$$(notdir $$($1_PRECOMPILED_HEADER))\"" > $$@
707
708      else ifneq ($(findstring $(TOOLCHAIN_TYPE), gcc clang), )
709        ifeq ($(TOOLCHAIN_TYPE), gcc)
710          $1_PCH_FILE := $$($1_OBJECT_DIR)/precompiled/$$(notdir $$($1_PRECOMPILED_HEADER)).gch
711          $1_USE_PCH_FLAGS := -I$$($1_OBJECT_DIR)/precompiled
712        else ifeq ($(TOOLCHAIN_TYPE), clang)
713          $1_PCH_FILE := $$($1_OBJECT_DIR)/precompiled/$$(notdir $$($1_PRECOMPILED_HEADER)).pch
714          $1_USE_PCH_FLAGS := -include-pch $$($1_PCH_FILE)
715        endif
716        $1_PCH_DEP := $$($1_PCH_FILE).d
717        $1_PCH_DEP_TARGETS := $$($1_PCH_FILE).d.targets
718
719        -include $$($1_PCH_DEP)
720        -include $$($1_PCH_DEP_TARGETS)
721
722        $$($1_PCH_FILE): $$($1_PRECOMPILED_HEADER) $$($1_COMPILE_VARDEPS_FILE)
723		$$(call LogInfo, Generating precompiled header)
724		$$(call MakeDir, $$(@D))
725		$$(call ExecuteWithLog, $$@, \
726		    $$($1_CC) $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
727		    $$($1_OPT_CFLAGS) \
728		    -x c++-header -c $(C_FLAG_DEPS) $$($1_PCH_DEP) $$< -o $$@)
729		$(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_PCH_DEP) > $$($1_PCH_DEP_TARGETS)
730
731        $$($1_ALL_OBJS): $$($1_PCH_FILE)
732
733      endif
734    endif
735  endif
736
737  # Now call add_native_source for each source file we are going to compile.
738  $$(foreach p,$$($1_SRCS), \
739      $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \
740          $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS), \
741          $$($1_CC), \
742          $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_SYSROOT_CFLAGS), \
743          $$($1_CXX), $$($1_ASFLAGS))))
744
745  # Setup rule for printing progress info when compiling source files.
746  # This is a rough heuristic and may not always print accurate information.
747  $$($1_BUILD_INFO): $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE)
748        ifeq ($$(wildcard $$($1_TARGET)),)
749	  $(ECHO) 'Creating $$(subst $$(BUILD_OUTPUT)/,,$$($1_TARGET)) from $$(words \
750	      $$(filter-out %.vardeps, $$?)) file(s)'
751        else
752	  $(ECHO) $$(strip 'Updating $$(subst $$(BUILD_OUTPUT)/,,$$($1_TARGET))' \
753	      $$(if $$(filter-out %.vardeps, $$?), \
754	        'due to $$(words $$(filter-out %.vardeps, $$?)) file(s)', \
755	      $$(if $$(filter %.vardeps, $$?), 'due to makefile changes')))
756        endif
757	$(TOUCH) $$@
758
759  # On windows we need to create a resource file
760  ifeq ($(OPENJDK_TARGET_OS), windows)
761    ifneq (,$$($1_VERSIONINFO_RESOURCE))
762      $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res
763      $1_RES_DEP:=$$($1_RES).d
764      $1_RES_DEP_TARGETS:=$$($1_RES).d.targets
765      -include $$($1_RES_DEP)
766      -include $$($1_RES_DEP_TARGETS)
767
768      $1_RES_VARDEPS := $$($1_RC) $$($1_RC_FLAGS)
769      $1_RES_VARDEPS_FILE := $$(call DependOnVariable, $1_RES_VARDEPS, \
770          $$($1_RES).vardeps)
771
772      $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE)
773		$$(call LogInfo, Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$(notdir $$($1_TARGET))))
774		$$(call MakeDir, $$(@D) $$($1_OBJECT_DIR))
775		$$(call ExecuteWithLog, $$@, \
776		    $$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \
777		    $$($1_VERSIONINFO_RESOURCE))
778                # Windows RC compiler does not support -showIncludes, so we mis-use CL
779                # for this. Filter out RC specific arguments that are unknown to CL.
780                # For some unknown reason, in this case CL actually outputs the show
781                # includes to stderr so need to redirect it to hide the output from the
782                # main log.
783		$$(call ExecuteWithLog, $$($1_RES_DEP).obj, \
784		    $$($1_CC) $$(filter-out -l%, $$($1_RC_FLAGS)) \
785		        $$($1_SYSROOT_CFLAGS) -showIncludes -nologo -TC \
786		        $(CC_OUT_OPTION)$$($1_RES_DEP).obj -P -Fi$$($1_RES_DEP).pp \
787		        $$($1_VERSIONINFO_RESOURCE)) 2>&1 \
788		    | $(GREP) -v -e "^Note: including file:" \
789		        -e "^$$(notdir $$($1_VERSIONINFO_RESOURCE))$$$$" || test "$$$$?" = "1" ; \
790		$(ECHO) $$($1_RES): \\ > $$($1_RES_DEP) ; \
791		$(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).obj.log >> $$($1_RES_DEP) ; \
792		$(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_RES_DEP) > $$($1_RES_DEP_TARGETS)
793    endif
794  endif
795
796  ifneq ($(DISABLE_MAPFILES),true)
797    $1_REAL_MAPFILE := $$($1_MAPFILE)
798    ifneq ($(OPENJDK_TARGET_OS),windows)
799      ifneq (,$$($1_REORDER))
800        $1_REAL_MAPFILE := $$($1_OBJECT_DIR)/mapfile
801
802        $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER)
803		$$(call MakeDir, $$(@D))
804		$$(CP) $$($1_MAPFILE) $$@.tmp
805		$$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp
806		$$(MV) $$@.tmp $$@
807      endif
808    endif
809  endif
810
811  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables
812  # for LDFLAGS and LIBS
813  $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS))
814  $1_EXTRA_LIBS:=$$($1_LIBS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LIBS_$(OPENJDK_TARGET_OS))
815  ifneq (,$$($1_REAL_MAPFILE))
816    $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE))
817  endif
818
819  # Need to make sure TARGET is first on list
820  $1 := $$($1_TARGET)
821
822  ifneq ($$($1_COPY_DEBUG_SYMBOLS), false)
823    $1_COPY_DEBUG_SYMBOLS := $(COPY_DEBUG_SYMBOLS)
824  endif
825
826  ifneq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), false)
827    $1_ZIP_EXTERNAL_DEBUG_SYMBOLS := $(ZIP_EXTERNAL_DEBUG_SYMBOLS)
828  endif
829
830  ifeq ($$($1_COPY_DEBUG_SYMBOLS), true)
831    ifneq ($$($1_DEBUG_SYMBOLS), false)
832      # Only copy debug symbols for dynamic libraries and programs.
833      ifeq ($$($1_STATIC_LIBRARY), )
834        # Generate debuginfo files.
835        ifeq ($(OPENJDK_TARGET_OS), windows)
836          $1_EXTRA_LDFLAGS += -debug "-pdb:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb" \
837              "-map:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map"
838          $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb \
839              $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map
840          # No separate command is needed for debuginfo on windows, instead
841          # touch target to make sure it has a later time stamp than the debug
842          # symbol files to avoid unnecessary relinking on rebuild.
843          $1_CREATE_DEBUGINFO_CMDS := $(TOUCH) $$($1_TARGET)
844
845        else ifneq ($(findstring $(OPENJDK_TARGET_OS), linux solaris), )
846          $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).debuginfo
847          # Setup the command line creating debuginfo files, to be run after linking.
848          # It cannot be run separately since it updates the original target file
849          $1_CREATE_DEBUGINFO_CMDS := \
850              $$($1_OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
851              $(CD) $$($1_OUTPUT_DIR) && \
852                  $$($1_OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET)
853
854        else ifeq ($(OPENJDK_TARGET_OS), macosx)
855          $1_DEBUGINFO_FILES := \
856              $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist \
857              $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME)
858          # On Macosx, the debuginfo generation doesn't touch the linked binary, but
859          # to avoid always relinking, touch it anyway to force a later timestamp than
860          # the dSYM files.
861          $1_CREATE_DEBUGINFO_CMDS := \
862              $(DSYMUTIL) --out $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM $$($1_TARGET) $$(NEWLINE) \
863              $(TOUCH) $$($1_TARGET)
864        endif # OPENJDK_TARGET_OS
865
866        $$($1_DEBUGINFO_FILES): $$($1_TARGET)
867
868        $1 += $$($1_DEBUGINFO_FILES)
869
870        ifeq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), true)
871          $1_DEBUGINFO_ZIP := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).diz
872          $1 += $$($1_DEBUGINFO_ZIP)
873
874          # The dependency on TARGET is needed for debuginfo files
875          # to be rebuilt properly.
876          $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET)
877		$(CD) $$($1_OUTPUT_DIR) && \
878		    $(ZIPEXE) -q -r $$@ $$(subst $$($1_OUTPUT_DIR)/,, $$($1_DEBUGINFO_FILES))
879
880        endif
881       endif # !STATIC_LIBRARY
882    endif # $1_DEBUG_SYMBOLS != false
883  endif # COPY_DEBUG_SYMBOLS
884
885  ifneq ($$($1_STRIP_SYMBOLS), false)
886    ifneq ($$($1_STRIP), )
887      # Default to using the global STRIPFLAGS. Allow for overriding with an empty value
888      $1_STRIPFLAGS ?= $(STRIPFLAGS)
889      $1_STRIP_CMD := $$($1_STRIP) $$($1_STRIPFLAGS) $$($1_TARGET)
890    endif
891  endif
892
893  ifneq (,$$($1_LIBRARY))
894    # Generating a dynamic library.
895    $1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
896    ifeq ($(OPENJDK_TARGET_OS), windows)
897      $1_EXTRA_LDFLAGS += "-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib"
898      # Create a rule for the import lib so that other rules may depend on it
899      $$($1_OBJECT_DIR)/$$($1_LIBRARY).lib: $$($1_TARGET)
900    endif
901
902    # Create loadmap on AIX. Helps in diagnosing some problems.
903    ifneq ($(COMPILER_BINDCMD_FILE_FLAG),)
904      $1_EXTRA_LDFLAGS += $(COMPILER_BINDCMD_FILE_FLAG)$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).loadmap
905    endif
906
907    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
908        $$($1_LIBS) $$($1_EXTRA_LIBS) $$($1_CREATE_DEBUGINFO_CMDS) \
909        $$($1_STRIP_CMD)
910    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
911        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
912
913    $1_LD_OBJ_ARG := $$($1_ALL_OBJS)
914
915    # If there are many object files, use an @-file...
916    ifneq ($$(word 17, $$($1_ALL_OBJS)), )
917      $1_OBJ_FILE_LIST := $$($1_OBJECT_DIR)/_$1_objectfilenames.txt
918      ifneq ($(COMPILER_COMMAND_FILE_FLAG),)
919        $1_LD_OBJ_ARG := $(COMPILER_COMMAND_FILE_FLAG)$$($1_OBJ_FILE_LIST)
920      else
921        # ...except for toolchains which don't support them.
922        $1_LD_OBJ_ARG := `cat $$($1_OBJ_FILE_LIST)`
923      endif
924    endif
925
926    $$($1_TARGET): $$($1_ALL_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) \
927        $$($1_VARDEPS_FILE)
928                ifneq ($$($1_OBJ_FILE_LIST), )
929		  $$(eval $$(call ListPathsSafely, $1_ALL_OBJS, $$($1_OBJ_FILE_LIST)))
930                endif
931                # Keep as much as possible on one execution line for best performance
932                # on Windows
933		$$(call LogInfo, Linking $$($1_BASENAME))
934                ifeq ($(OPENJDK_TARGET_OS), windows)
935		  $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
936		      $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
937		      $(LD_OUT_OPTION)$$@ $$($1_LD_OBJ_ARG) $$($1_RES) $$($1_LIBS) \
938		      $$($1_EXTRA_LIBS)) \
939		      | $(GREP) -v "^   Creating library .*\.lib and object .*\.exp" || \
940		      test "$$$$?" = "1" ; \
941		  $$($1_CREATE_DEBUGINFO_CMDS)
942		  $$($1_STRIP_CMD)
943                else
944		  $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
945		      $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
946		      $(LD_OUT_OPTION)$$@ $$($1_LD_OBJ_ARG) $$($1_RES) $$($1_LIBS) \
947		      $$($1_EXTRA_LIBS)) ; \
948		  $$($1_CREATE_DEBUGINFO_CMDS)
949		  $$($1_STRIP_CMD)
950                endif
951
952  endif
953
954  ifneq (,$$($1_STATIC_LIBRARY))
955    $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LIBS) \
956        $$($1_EXTRA_LIBS)
957    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
958        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
959
960    # Generating a static library, ie object file archive.
961    ifeq ($(STATIC_BUILD), true)
962      ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true)
963        STATIC_MAPFILE_DEP := $$($1_MAPFILE)
964      endif
965    endif
966
967    $$($1_TARGET): $$($1_ALL_OBJS) $$($1_RES) $$($1_VARDEPS_FILE) $$(STATIC_MAPFILE_DEP)
968	$$(call LogInfo, Archiving $$($1_STATIC_LIBRARY))
969	$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
970	    $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_ALL_OBJS) \
971	        $$($1_RES))
972        ifeq ($(STATIC_BUILD), true)
973          ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true)
974	    $(CP) $$($1_MAPFILE) $$(@D)/$$(basename $$(@F)).symbols
975          else
976	    $(GetSymbols)
977          endif
978        endif
979  endif
980
981  ifneq (,$$($1_PROGRAM))
982    # A executable binary has been specified, setup the target for it.
983    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
984        $$($1_LIBS) $$($1_EXTRA_LIBS) $$($1_MT) \
985        $$($1_CODESIGN) $$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION) \
986        $$($1_STRIP_CMD)
987    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
988        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
989
990    ifeq ($(OPENJDK_TARGET_OS), windows)
991      ifeq ($$($1_EMBED_MANIFEST), true)
992        $1_EXTRA_LDFLAGS += -manifest:embed
993      endif
994    endif
995
996    $$($1_TARGET): $$($1_ALL_OBJS) $$($1_RES) $$($1_MANIFEST) \
997        $$($1_VARDEPS_FILE)
998		$$(call LogInfo, Linking executable $$($1_BASENAME))
999		$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
1000		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
1001		        $(EXE_OUT_OPTION)$$($1_TARGET) \
1002		        $$($1_ALL_OBJS) $$($1_RES) \
1003		        $$($1_LIBS) $$($1_EXTRA_LIBS))
1004                ifeq ($(OPENJDK_TARGET_OS), windows)
1005                  ifneq ($$($1_MANIFEST), )
1006		    $$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_PROGRAM).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1
1007                  endif
1008                endif
1009                # This only works if the openjdk_codesign identity is present on the system. Let
1010                # silently fail otherwise.
1011                ifneq (,$(CODESIGN))
1012                  ifneq (,$$($1_CODESIGN))
1013		    $(CODESIGN) -s openjdk_codesign $$@
1014                  endif
1015                endif
1016		$$($1_CREATE_DEBUGINFO_CMDS)
1017		$$($1_STRIP_CMD)
1018
1019  endif
1020endef
1021
1022endif # _NATIVE_COMPILATION_GMK
1023