make/common/NativeCompilation.gmk
author ihse
Fri, 11 Sep 2015 10:05:49 +0200
changeset 32462 f236112ab3d0
parent 32344 0b288e0efcfa
child 32568 fc5205c29b31
permissions -rw-r--r--
8064808: Disable use of broken objcopy on Solaris, remove adhoc helper tools Reviewed-by: erikj

#
# Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

# When you read this source. Remember that $(sort ...) has the side effect
# of removing duplicates. It is actually this side effect that is
# desired whenever sort is used below!

ifndef _NATIVE_COMPILATION_GMK
_NATIVE_COMPILATION_GMK := 1

ifeq (,$(_MAKEBASE_GMK))
  $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk)
endif

################################################################################
# Define a native toolchain configuration that can be used by
# SetupNativeCompilation calls
#
# Parameter 1 is the name of the toolchain definition
#
# Remaining parameters are named arguments:
#   EXTENDS - Optional parent definition to get defaults from
#   CC - The C compiler
#   CXX - The C++ compiler
#   LD - The Linker
#   AR - Static linker
#   AS - Assembler
#   MT - Windows MT tool
#   RC - Windows RC tool
#   SYSROOT_CFLAGS - Compiler flags for using the specific sysroot
#   SYSROOT_LDFLAGS - Linker flags for using the specific sysroot
DefineNativeToolchain = $(NamedParamsMacroTemplate)
define DefineNativeToolchainBody
  # If extending another definition, get default values from that,
  # otherwise, nothing more needs to be done as variable assignments
  # already happened in NamedParamsMacroTemplate.
  ifneq ($$($1_EXTENDS), )
    $$(call SetIfEmpty, $1_CC, $$($$($1_EXTENDS)_CC))
    $$(call SetIfEmpty, $1_CXX, $$($$($1_EXTENDS)_CXX))
    $$(call SetIfEmpty, $1_LD, $$($$($1_EXTENDS)_LD))
    $$(call SetIfEmpty, $1_AR, $$($$($1_EXTENDS)_AR))
    $$(call SetIfEmpty, $1_AS, $$($$($1_EXTENDS)_AS))
    $$(call SetIfEmpty, $1_MT, $$($$($1_EXTENDS)_MT))
    $$(call SetIfEmpty, $1_RC, $$($$($1_EXTENDS)_RC))
    $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_EXTENDS)_SYSROOT_CFLAGS))
    $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_EXTENDS)_SYSROOT_LDFLAGS))
  endif
endef

# Create a default toolchain with the main compiler and linker
$(eval $(call DefineNativeToolchain, TOOLCHAIN_DEFAULT, \
    CC := $(CC), \
    CXX := $(CXX), \
    LD := $(LD), \
    AR := $(AR), \
    AS := $(AS), \
    MT := $(MT), \
    RC := $(RC), \
    SYSROOT_CFLAGS := $(SYSROOT_CFLAGS), \
    SYSROOT_LDFLAGS := $(SYSROOT_LDFLAGS), \
))

# Create a toolchain where linking is done with the C++ linker
$(eval $(call DefineNativeToolchain, TOOLCHAIN_LINK_CXX, \
    EXTENDS := TOOLCHAIN_DEFAULT, \
    LD := $(LDCXX), \
))

# Create a toolchain with the BUILD compiler, used for build tools that
# are to be run during the build.
# The BUILD_SYSROOT_*FLAGS variables are empty for now.
$(eval $(call DefineNativeToolchain, TOOLCHAIN_BUILD, \
    EXTENDS := TOOLCHAIN_DEFAULT, \
    CC := $(BUILD_CC), \
    LD := $(BUILD_LD), \
    SYSROOT_CFLAGS := $(BUILD_SYSROOT_CFLAGS), \
    SYSROOT_LDFLAGS := $(BUILD_SYSROOT_LDFLAGS), \
))

################################################################################

# Extensions of files handled by this macro.
NATIVE_SOURCE_EXTENSIONS := %.s %.c %.cpp %.m %.mm

# Replaces native source extensions with the object file extension in a string.
# Param 1: the string containing source file names with extensions
# The surrounding strip is needed to keep additional whitespace out
define replace_with_obj_extension
$(strip \
  $(foreach extension, $(NATIVE_SOURCE_EXTENSIONS), \
      $(patsubst $(extension),%$(OBJ_SUFFIX),$(filter $(extension),$1))) \
)
endef

ifeq ($(OPENJDK_BUILD_OS_ENV), windows.cygwin)
  UNIX_PATH_PREFIX := /cygdrive
else ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys)
  UNIX_PATH_PREFIX :=
endif

# This pattern is used to transform the output of the microsoft CL compiler
# into a make syntax dependency file (.d)
WINDOWS_SHOWINCLUDE_SED_PATTERN := \
    -e '/^Note: including file:/!d' \
    -e 's|Note: including file: *||' \
    -e 's|\\|/|g' \
    -e 's|^\([a-zA-Z]\):|$(UNIX_PATH_PREFIX)/\1|g' \
    -e '\|$(TOPDIR)|I !d' \
    -e 's|$$$$| \\|g' \
    #

# This pattern is used to transform a dependency file (.d) to a list
# of make targets for dependent files (.d.targets)
DEPENDENCY_TARGET_SED_PATTERN := \
    -e 's/\#.*//' \
    -e 's/^[^:]*: *//' \
    -e 's/ *\\$$$$//' \
    -e 's/^[	 ]*//' \
    -e '/^$$$$/ d' \
    -e 's/$$$$/ :/' \
    #

define add_native_source
  # param 1 = BUILD_MYPACKAGE
  # parma 2 = the source file name (..../alfa.c or .../beta.cpp)
  # param 3 = the bin dir that stores all .o (.obj) and .d files.
  # param 4 = the c flags to the compiler
  # param 5 = the c compiler
  # param 6 = the c++ flags to the compiler
  # param 7 = the c++ compiler
  # param 8 = the flags to the assembler

  ifneq (,$$(filter %.c,$2))
    # Compile as a C file
    $1_$2_FLAGS=$(CFLAGS_CCACHE) $4 $$($1_$(notdir $2)_CFLAGS) -DTHIS_FILE='"$$(<F)"' -c
    $1_$2_COMP=$5
    $1_$2_DEP_FLAG:=$(C_FLAG_DEPS)
  else ifneq (,$$(filter %.m,$2))
    # Compile as an Objective-C file
    $1_$2_FLAGS=-x objective-c $(CFLAGS_CCACHE) $4 $$($1_$(notdir $2)_CFLAGS) -DTHIS_FILE='"$$(<F)"' -c
    $1_$2_COMP=$5
    $1_$2_DEP_FLAG:=$(C_FLAG_DEPS)
  else ifneq (,$$(filter %.s,$2))
    # Compile as assembler file
    $1_$2_FLAGS=$8 -DTHIS_FILE='"$$(<F)"'
    $1_$2_COMP=$(AS)
    $1_$2_DEP_FLAG:=
  else ifneq (,$$(filter %.cpp,$2)$$(filter %.mm,$2))
    # Compile as a C++ or Objective-C++ file
    $1_$2_FLAGS=$(CFLAGS_CCACHE) $6 $$($1_$(notdir $2)_CXXFLAGS) -DTHIS_FILE='"$$(<F)"' -c
    $1_$2_COMP=$7
    $1_$2_DEP_FLAG:=$(CXX_FLAG_DEPS)
  else
    $$(error Internal error in NativeCompilation.gmk: no compiler for file $2)
  endif
  # Generate the .o (.obj) file name and place it in the bin dir.
  $1_$2_OBJ := $3/$$(call replace_with_obj_extension, $$(notdir $2))
  # Only continue if this object file hasn't been processed already. This lets the first found
  # source file override any other with the same name.
  ifeq (,$$(findstring $$($1_$2_OBJ),$$($1_ALL_OBJS)))
    $1_ALL_OBJS+=$$($1_$2_OBJ)
    ifeq (,$$(filter %.s,$2))
      # And this is the dependency file for this obj file.
      $1_$2_DEP:=$$(patsubst %$(OBJ_SUFFIX),%.d,$$($1_$2_OBJ))
      # The dependency target file lists all dependencies as empty targets
      # to avoid make error "No rule to make target" for removed files
      $1_$2_DEP_TARGETS:=$$(patsubst %$(OBJ_SUFFIX),%.d.targets,$$($1_$2_OBJ))

      # Include previously generated dependency information. (if it exists)
      -include $$($1_$2_DEP)
      -include $$($1_$2_DEP_TARGETS)

      ifeq ($(TOOLCHAIN_TYPE), microsoft)
        $1_$2_DEBUG_OUT_FLAGS:=-Fd$$(patsubst %$(OBJ_SUFFIX),%.pdb,$$($1_$2_OBJ)) \
            -Fm$$(patsubst %$(OBJ_SUFFIX),%.map,$$($1_$2_OBJ))
      endif
    endif

    $$($1_$2_OBJ) : $2 $$($1_COMPILE_VARDEPS_FILE) | $$($1_BUILD_INFO)
	$(ECHO) $(LOG_INFO) "Compiling $$(notdir $2) (for $$(notdir $$($1_TARGET)))"
        ifneq ($(TOOLCHAIN_TYPE), microsoft)
          ifeq ($(TOOLCHAIN_TYPE)$$(filter %.s,$2), solstudio)
            # The Solaris studio compiler doesn't output the full path to the object file in the
            # generated deps files. Fixing it with sed. If compiling assembly, don't try this.
	    $(call LogFailures, $$($1_$2_OBJ).log, $1_$$(notdir $2), \
	        $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP).tmp $(CC_OUT_OPTION)$$($1_$2_OBJ) $2)
	    $(SED) 's|^$$(@F):|$$@:|' $$($1_$2_DEP).tmp > $$($1_$2_DEP)
          else
	    $(call LogFailures, $$($1_$2_OBJ).log, $1_$$(notdir $2), \
	        $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP) $(CC_OUT_OPTION)$$($1_$2_OBJ) $2)
          endif
        else
          # The Visual Studio compiler lacks a feature for generating make dependencies, but by
          # setting -showIncludes, all included files are printed. These are filtered out and
          # parsed into make dependences.
	  ($(call LogFailures, $$($1_$2_OBJ).log, $1_$$(notdir $2), \
	      $$($1_$2_COMP) $$($1_$2_FLAGS) -showIncludes $$($1_$2_DEBUG_OUT_FLAGS) \
	          $(CC_OUT_OPTION)$$($1_$2_OBJ) $2) ; echo $$$$? > $$($1_$2_DEP).exitvalue) \
	      | $(TEE) $$($1_$2_DEP).raw | $(GREP) -v -e "^Note: including file:" \
	          -e "^$(notdir $2)$$$$" || test "$$$$?" = "1" ; \
	      exit `cat $$($1_$2_DEP).exitvalue`
	  $(RM) $$($1_$2_DEP).exitvalue
	  ($(ECHO) $$@: \\ \
	  && $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_$2_DEP).raw) | $(SORT) -u > $$($1_$2_DEP)
        endif
        # Create a dependency target file from the dependency file.
        # Solution suggested by http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
        ifneq ($$($1_$2_DEP),)
	  $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_$2_DEP) > $$($1_$2_DEP_TARGETS)
        endif
  endif
endef

# Setup make rules for creating a native binary (a shared library or an
# executable).
#
# Parameter 1 is the name of the rule. This name is used as variable prefix,
# and the targets generated are listed in a variable by that name.
#
# Remaining parameters are named arguments. These include:
#   TOOLCHAIN Name of toolchain setup to use. Defaults to TOOLCHAIN_DEFAULT.
#   SRC one or more directory roots to scan for C/C++ files.
#   CFLAGS the compiler flags to be used, used both for C and C++.
#   CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS.
#   LDFLAGS the linker flags to be used, used both for C and C++.
#   LDFLAGS_SUFFIX the linker flags to be added last on the commandline
#       typically the libraries linked to.
#   ARFLAGS the archiver flags to be used
#   OBJECT_DIR the directory where we store the object files
#   LIBRARY the resulting library file
#   PROGRAM the resulting exec file
#   INCLUDES only pick source from these directories
#   EXCLUDES do not pick source from these directories
#   INCLUDE_FILES only compile exactly these files!
#   EXCLUDE_FILES with these names
#   EXTRA_FILES List of extra files not in any of the SRC dirs
#   VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run
#   RC_FLAGS flags for RC.
#   MAPFILE mapfile
#   REORDER reorder file
#   DEBUG_SYMBOLS add debug symbols (if configured on)
#   CC the compiler to use, default is $(CC)
#   LD the linker to use, default is $(LD)
#   OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST
#   DISABLED_WARNINGS_<toolchain> Disable the given warnings for the specified toolchain
SetupNativeCompilation = $(NamedParamsMacroTemplate)
define SetupNativeCompilationBody

  ifneq (,$$($1_BIN))
    $$(error BIN has been replaced with OBJECT_DIR)
  endif

  ifneq (,$$($1_LIB))
    $$(error LIB has been replaced with LIBRARY)
  endif

  ifneq (,$$($1_EXE))
    $$(error EXE has been replaced with PROGRAM)
  endif

  ifneq (,$$($1_LIBRARY))
    ifeq (,$$($1_OUTPUT_DIR))
      $$(error LIBRARY requires OUTPUT_DIR)
    endif

    ifneq ($$($1_LIBRARY),$(basename $$($1_LIBRARY)))
      $$(error directory of LIBRARY should be specified using OUTPUT_DIR)
    endif

    ifneq (,$(findstring $(SHARED_LIBRARY_SUFFIX),$$($1_LIBRARY)))
      $$(error LIBRARY should be specified without SHARED_LIBRARY_SUFFIX: $(SHARED_LIBRARY_SUFFIX))
    endif

    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_LIBRARY)))
      $$(error LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
    endif

    ifeq ($$($1_SUFFIX), )
      $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX)
    endif

    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_LIBRARY)$$($1_SUFFIX)
    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_LIBRARY)
  endif

  ifneq (,$$($1_STATIC_LIBRARY))
    ifeq (,$$($1_OUTPUT_DIR))
      $$(error STATIC_LIBRARY requires OUTPUT_DIR)
    endif

    ifneq ($$($1_STATIC_LIBRARY),$(basename $$($1_STATIC_LIBRARY)))
      $$(error directory of STATIC_LIBRARY should be specified using OUTPUT_DIR)
    endif

    ifneq (,$(findstring $(STATIC_LIBRARY_SUFFIX),$$($1_STATIC_LIBRARY)))
      $$(error STATIC_LIBRARY should be specified without STATIC_LIBRARY_SUFFIX: $(STATIC_LIBRARY_SUFFIX))
    endif

    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_STATIC_LIBRARY)))
      $$(error STATIC_LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
    endif

    ifeq ($$($1_SUFFIX), )
      $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX)
    endif

    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)$$($1_SUFFIX)
    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)
  endif

  ifneq (,$$($1_PROGRAM))
    ifeq (,$$($1_OUTPUT_DIR))
      $$(error PROGRAM requires OUTPUT_DIR)
    endif

    ifneq ($$($1_PROGRAM),$(basename $$($1_PROGRAM)))
      $$(error directory of PROGRAM should be specified using OUTPUT_DIR)
    endif

    ifneq (,$(findstring $(EXE_SUFFIX),$$($1_PROGRAM)))
      $$(error PROGRAM should be specified without EXE_SUFFIX: $(EXE_SUFFIX))
    endif

    ifeq ($$($1_SUFFIX), )
      $1_SUFFIX := $(EXE_SUFFIX)
    endif

    $1_BASENAME:=$$($1_PROGRAM)$$($1_SUFFIX)
    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
    $1_NOSUFFIX:=$$($1_PROGRAM)
  endif

  ifeq (,$$($1_TARGET))
    $$(error Neither PROGRAM, LIBRARY nor STATIC_LIBRARY has been specified for SetupNativeCompilation)
  endif

  # Setup the toolchain to be used
  $$(call SetIfEmpty, $1_TOOLCHAIN, TOOLCHAIN_DEFAULT)
  $$(call SetIfEmpty, $1_CC, $$($$($1_TOOLCHAIN)_CC))
  $$(call SetIfEmpty, $1_CXX, $$($$($1_TOOLCHAIN)_CXX))
  $$(call SetIfEmpty, $1_LD, $$($$($1_TOOLCHAIN)_LD))
  $$(call SetIfEmpty, $1_AR, $$($$($1_TOOLCHAIN)_AR))
  $$(call SetIfEmpty, $1_AS, $$($$($1_TOOLCHAIN)_AS))
  $$(call SetIfEmpty, $1_MT, $$($$($1_TOOLCHAIN)_MT))
  $$(call SetIfEmpty, $1_RC, $$($$($1_TOOLCHAIN)_RC))
  $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_CFLAGS))
  $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_LDFLAGS))

  ifneq ($$($1_MANIFEST), )
    ifeq ($$($1_MANIFEST_VERSION), )
      $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1)
    endif
  endif

  # Make sure the dirs exist.
  $$(call MakeDir,$$($1_OBJECT_DIR) $$($1_OUTPUT_DIR))
  $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),, \
      $$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d)))

  # Find all files in the source trees. Sort to remove duplicates.
  $1_ALL_SRCS := $$(sort $$(call CacheFind,$$($1_SRC)))
  # Extract the C/C++ files.
  $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
  $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
  ifneq ($$($1_EXCLUDE_FILES),)
    $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES))
  endif
  $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter $$(NATIVE_SOURCE_EXTENSIONS),$$($1_ALL_SRCS)))
  ifneq (,$$(strip $$($1_INCLUDE_FILES)))
    $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS))
  endif
  ifeq (,$$($1_SRCS))
    $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
  endif
  # There can be only a single bin dir root, no need to foreach over the roots.
  $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX))
  # Now we have a list of all c/c++ files to compile: $$($1_SRCS)
  # and we have a list of all existing object files: $$($1_BINS)

  # Prepend the source/bin path to the filter expressions. Then do the filtering.
  ifneq ($$($1_INCLUDES),)
    $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
    $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
  endif
  ifneq ($$($1_EXCLUDES),)
    $1_SRC_EXCLUDES := $$(addsuffix /%,$$($1_EXCLUDES))
    $1_SRC_EXCLUDES += $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
    $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
  endif

  $1_SRCS += $$($1_EXTRA_FILES)

  # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides
  # a reproducable order on the input files to the linker).
  $1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS)))
  $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_OBJECT_DIR)/,$$($1_EXPECTED_OBJS_FILENAMES)))
  # Are there too many object files on disk? Perhaps because some source file was removed?
  $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS)))
  # Clean out the superfluous object files.
  ifneq ($$($1_SUPERFLUOUS_OBJS),)
    $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS))
  endif

  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CFLAGS.
  $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS))
  ifneq ($(DEBUG_LEVEL),release)
    # Pickup extra debug dependent variables for CFLAGS
    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug)
    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug)
  else
    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release)
    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release)
  endif

  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS.
  $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS))
  ifneq ($(DEBUG_LEVEL),release)
    # Pickup extra debug dependent variables for CXXFLAGS
    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug)
    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug)
  else
    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release)
    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release)
  endif

  # If no C++ flags are explicitly set, default to using the C flags.
  # After that, we can set additional C++ flags that should not interfere
  # with the mechanism for copying the C flags by default.
  ifeq ($$($1_CXXFLAGS),)
    $1_CXXFLAGS:=$$($1_CFLAGS)
  endif
  ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),)
    $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS)
  endif

  ifeq ($$($1_DEBUG_SYMBOLS), true)
    ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
      ifdef OPENJDK
        # Always add debug symbols
        $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
        $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
      else
        # Programs don't get the debug symbols added in the old build. It's not clear if
        # this is intentional.
        ifeq ($$($1_PROGRAM),)
          $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
          $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
        endif
      endif
    endif
  endif

  ifneq (,$$($1_REORDER))
    $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER)
    $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER)
  endif

  # Pick up disabled warnings, if possible on this platform.
  ifneq ($(DISABLE_WARNING_PREFIX),)
    $1_EXTRA_CFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)))
    $1_EXTRA_CXXFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)))
  endif

  # Check if warnings should be considered errors.
  # Pick first binary and toolchain specific, then binary specific, then general setting.
  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)),)
    ifeq ($$($1_WARNINGS_AS_ERRORS),)
      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$(WARNINGS_AS_ERRORS)
    else
      $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$($1_WARNINGS_AS_ERRORS)
    endif
  endif

  ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), true)
    $1_EXTRA_CFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
    $1_EXTRA_CXXFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
  endif

  ifeq (NONE, $$($1_OPTIMIZATION))
    $1_EXTRA_CFLAGS += $(C_O_FLAG_NONE)
    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NONE)
  else ifeq (LOW, $$($1_OPTIMIZATION))
    $1_EXTRA_CFLAGS += $(C_O_FLAG_NORM)
    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NORM)
  else ifeq (HIGH, $$($1_OPTIMIZATION))
    $1_EXTRA_CFLAGS += $(C_O_FLAG_HI)
    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HI)
  else ifeq (HIGHEST, $$($1_OPTIMIZATION))
    $1_EXTRA_CFLAGS += $(C_O_FLAG_HIGHEST)
    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HIGHEST)
  else ifneq (, $$($1_OPTIMIZATION))
    $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION))
  endif

  $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker

  # Track variable changes for all variables that affect the compilation command
  # lines for all object files in this setup. This includes at least all the
  # variables used in the call to add_native_source below.
  $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
      $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) \
      $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS) \
      $$(foreach s, $$($1_SRCS), \
          $$($1_$$(notdir $$s)_CFLAGS) $$($1_$$(notdir $$s)_CXXFLAGS))
  $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \
      $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps)

  # Now call add_native_source for each source file we are going to compile.
  $$(foreach p,$$($1_SRCS), \
      $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \
          $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS), \
          $$($1_CC), \
          $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_SYSROOT_CFLAGS), \
          $$($1_CXX), $$($1_ASFLAGS))))

  # Setup rule for printing progress info when compiling source files.
  # This is a rough heuristic and may not always print accurate information.
  $$($1_BUILD_INFO): $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE)
        ifeq ($$(wildcard $$($1_TARGET)),)
	  $(ECHO) 'Creating $$($1_BASENAME) from $$(words $$(filter-out %.vardeps, $$?)) file(s)'
        else
	  $(ECHO) $$(strip 'Updating $$($1_BASENAME)' \
	      $$(if $$(filter-out %.vardeps, $$?), \
	        'from $$(words $$(filter-out %.vardeps, $$?)) file(s)') \
	      $$(if $$(filter %.vardeps, $$?), 'due to makefile changes'))
        endif
	$(TOUCH) $$@

  # On windows we need to create a resource file
  ifeq ($(OPENJDK_TARGET_OS), windows)
    ifneq (,$$($1_VERSIONINFO_RESOURCE))
      $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res
      $1_RES_DEP:=$$($1_RES).d
      $1_RES_DEP_TARGETS:=$$($1_RES).d.targets
      -include $$($1_RES_DEP)
      -include $$($1_RES_DEP_TARGETS)

      $1_RES_VARDEPS := $$($1_RC) $$($1_RC_FLAGS)
      $1_RES_VARDEPS_FILE := $$(call DependOnVariable, $1_RES_VARDEPS, \
          $$($1_RES).vardeps)

      $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE)
		$(ECHO) $(LOG_INFO) "Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$(notdir $$($1_TARGET)))"
		$$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \
		    $$($1_VERSIONINFO_RESOURCE)
                # Windows RC compiler does not support -showIncludes, so we mis-use CL for this.
		$$($1_CC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) -showIncludes -nologo -TC \
		    $(CC_OUT_OPTION)$$($1_RES_DEP).obj $$($1_VERSIONINFO_RESOURCE) > $$($1_RES_DEP).raw 2>&1 || exit 0
		($(ECHO) $$($1_RES): \\ \
		&& $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).raw) > $$($1_RES_DEP)
		$(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_RES_DEP) > $$($1_RES_DEP_TARGETS)
    endif
  endif

  # mapfile doesnt seem to be implemented on macosx (yet??)
  ifneq ($(OPENJDK_TARGET_OS),macosx)
    ifneq ($(OPENJDK_TARGET_OS),windows)
      $1_REAL_MAPFILE:=$$($1_MAPFILE)
      ifneq (,$$($1_REORDER))
        $1_REAL_MAPFILE:=$$($1_OBJECT_DIR)/mapfile

        $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER)
		$$(MKDIR) -p $$(@D)
		$$(CP) $$($1_MAPFILE) $$@.tmp
		$$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp
		$$(MV) $$@.tmp $$@
      endif
    endif
  endif

  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables
  # for LDFLAGS and LDFLAGS_SUFFIX
  $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS))
  $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS))
  ifneq (,$$($1_REAL_MAPFILE))
    $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE))
  endif

  # Need to make sure TARGET is first on list
  $1 := $$($1_TARGET)
  ifeq ($$($1_STATIC_LIBRARY),)
    ifeq ($$($1_DEBUG_SYMBOLS), true)
      ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
        ifneq ($(OPENJDK_TARGET_OS), macosx) # no MacOS X support yet
          ifneq ($$($1_OUTPUT_DIR),$$($1_OBJECT_DIR))
            # The dependency on TARGET is needed on windows for debuginfo files
            # to be rebuilt properly.
            $$($1_OUTPUT_DIR)/% : $$($1_OBJECT_DIR)/% $$($1_TARGET)
		$(CP) $$< $$@
          endif

          # Generate debuginfo files.
          ifeq ($(OPENJDK_TARGET_OS), windows)
            $1_EXTRA_LDFLAGS += "-pdb:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb" \
                "-map:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map"
            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb \
                $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map

          else ifneq ($(findstring $(OPENJDK_TARGET_OS), linux solaris), )
            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
            # Setup the command line creating debuginfo files, to be run after linking.
            # It cannot be run separately since it updates the original target file
            $1_CREATE_DEBUGINFO_CMDS := \
                $(OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
                $(CD) $$($1_OUTPUT_DIR) && \
                    $(OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET)
          endif # No MacOS X support

          # This dependency dance ensures that debug info files get rebuilt
          # properly if deleted.
          $$($1_TARGET): $$($1_DEBUGINFO_FILES)
          $$($1_DEBUGINFO_FILES): $$($1_EXPECTED_OBJS)

          ifeq ($(ZIP_DEBUGINFO_FILES), true)
            $1_DEBUGINFO_ZIP := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz
            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_ZIP))

            # The dependency on TARGET is needed for debuginfo files
            # to be rebuilt properly.
            $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET)
		$(CD) $$($1_OBJECT_DIR) \
		&& $(ZIP) -q $$@ $$(notdir $$($1_DEBUGINFO_FILES))

          else
            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_FILES))
          endif
        endif
      endif # !MacOS X
    endif # $1_DEBUG_SYMBOLS
  endif # !STATIC_LIBRARY

  ifneq (,$$($1_LIBRARY))
    # Generating a dynamic library.
    $1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
    ifeq ($(OPENJDK_TARGET_OS), windows)
      $1_EXTRA_LDFLAGS += "-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib"
    endif

    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)

    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_CREATE_DEBUGINFO_CMDS)
    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)

    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) \
        $$($1_VARDEPS_FILE)
		$(ECHO) $(LOG_INFO) "Linking $$($1_BASENAME)"
		$(call LogFailures, $$($1_OBJECT_DIR)/$1_link.log, $1_link, \
		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
		    $(LD_OUT_OPTION)$$@ \
		    $$($1_EXPECTED_OBJS) $$($1_RES) \
		    $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
		$$($1_CREATE_DEBUGINFO_CMDS)
                # Touch target to make sure it has a later time stamp than the debug
                # symbol files to avoid unnecessary relinking on rebuild.
                ifeq ($(OPENJDK_TARGET_OS), windows)
		  $(TOUCH) $$@
                endif

  endif

  ifneq (,$$($1_STATIC_LIBRARY))
    $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LDFLAGS_SUFFIX) \
        $$($1_EXTRA_LDFLAGS_SUFFIX)
    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)

    # Generating a static library, ie object file archive.
    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_VARDEPS_FILE)
	$(ECHO) $(LOG_INFO) "Archiving $$($1_STATIC_LIBRARY)"
	$(call LogFailures, $$($1_OBJECT_DIR)/$1_link.log, $1_link, \
	    $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_EXPECTED_OBJS) \
	        $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
  endif

  ifneq (,$$($1_PROGRAM))
    # A executable binary has been specified, setup the target for it.
    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)

    $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) $$($1_MT) \
        $$($1_CODESIGN) $$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION)
    $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
        $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)

    $$($1_TARGET): $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_MANIFEST) \
        $$($1_VARDEPS_FILE)
		$(ECHO) $(LOG_INFO) "Linking executable $$($1_BASENAME)"
		$(call LogFailures, $$($1_OBJECT_DIR)/$1_link.log, $1_link, \
		    $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
		        $(EXE_OUT_OPTION)$$($1_TARGET) \
		        $$($1_EXPECTED_OBJS) $$($1_RES) \
		        $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX))
                ifeq ($(OPENJDK_TARGET_OS), windows)
                  ifneq ($$($1_MANIFEST), )
		    $$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_PROGRAM).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1
                  endif
                endif
                # This only works if the openjdk_codesign identity is present on the system. Let
                # silently fail otherwise.
                ifneq (,$(CODESIGN))
                  ifneq (,$$($1_CODESIGN))
		    $(CODESIGN) -s openjdk_codesign $$@
                  endif
                endif
		$$($1_CREATE_DEBUGINFO_CMDS)
                # Touch target to make sure it has a later time stamp than the debug
                # symbol files to avoid unnecessary relinking on rebuild.
                ifeq ($(OPENJDK_TARGET_OS), windows)
		  $(TOUCH) $$@
                endif

  endif
endef

endif # _NATIVE_COMPILATION_GMK