1 #
   2 # Copyright (c) 2011, 2018, 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 
  30 ifndef _NATIVE_COMPILATION_GMK
  31 _NATIVE_COMPILATION_GMK := 1
  32 
  33 ifeq ($(_MAKEBASE_GMK), )
  34   $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk)
  35 endif
  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
  43 define 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
  60 endef
  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 #   OBJCOPY - The objcopy tool for debug symbol handling
  78 #   STRIP - The tool to use for stripping debug symbols
  79 #   SYSROOT_CFLAGS - Compiler flags for using the specific sysroot
  80 #   SYSROOT_LDFLAGS - Linker flags for using the specific sysroot
  81 DefineNativeToolchain = $(NamedParamsMacroTemplate)
  82 define DefineNativeToolchainBody
  83   # If extending another definition, get default values from that,
  84   # otherwise, nothing more needs to be done as variable assignments
  85   # already happened in NamedParamsMacroTemplate.
  86   ifneq ($$($1_EXTENDS), )
  87     $$(call SetIfEmpty, $1_CC, $$($$($1_EXTENDS)_CC))
  88     $$(call SetIfEmpty, $1_CXX, $$($$($1_EXTENDS)_CXX))
  89     $$(call SetIfEmpty, $1_LD, $$($$($1_EXTENDS)_LD))
  90     $$(call SetIfEmpty, $1_AR, $$($$($1_EXTENDS)_AR))
  91     $$(call SetIfEmpty, $1_AS, $$($$($1_EXTENDS)_AS))
  92     $$(call SetIfEmpty, $1_MT, $$($$($1_EXTENDS)_MT))
  93     $$(call SetIfEmpty, $1_RC, $$($$($1_EXTENDS)_RC))
  94     $$(call SetIfEmpty, $1_OBJCOPY, $$($$($1_EXTENDS)_OBJCOPY))
  95     $$(call SetIfEmpty, $1_STRIP, $$($$($1_EXTENDS)_STRIP))
  96     $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_EXTENDS)_SYSROOT_CFLAGS))
  97     $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_EXTENDS)_SYSROOT_LDFLAGS))
  98   endif
  99 endef
 100 
 101 # Create a default toolchain with the main compiler and linker
 102 $(eval $(call DefineNativeToolchain, TOOLCHAIN_DEFAULT, \
 103     CC := $(CC), \
 104     CXX := $(CXX), \
 105     LD := $(LD), \
 106     AR := $(AR), \
 107     AS := $(AS), \
 108     MT := $(MT), \
 109     RC := $(RC), \
 110     OBJCOPY := $(OBJCOPY), \
 111     STRIP := $(STRIP), \
 112     SYSROOT_CFLAGS := $(SYSROOT_CFLAGS), \
 113     SYSROOT_LDFLAGS := $(SYSROOT_LDFLAGS), \
 114 ))
 115 
 116 # Create a toolchain where linking is done with the C++ linker
 117 $(eval $(call DefineNativeToolchain, TOOLCHAIN_LINK_CXX, \
 118     EXTENDS := TOOLCHAIN_DEFAULT, \
 119     LD := $(LDCXX), \
 120 ))
 121 
 122 # Create a toolchain with the BUILD compiler, used for build tools that
 123 # are to be run during the build.
 124 $(eval $(call DefineNativeToolchain, TOOLCHAIN_BUILD, \
 125     CC := $(BUILD_CC), \
 126     CXX := $(BUILD_CXX), \
 127     LD := $(BUILD_LD), \
 128     AR := $(BUILD_AR), \
 129     AS := $(BUILD_AS), \
 130     OBJCOPY := $(BUILD_OBJCOPY), \
 131     STRIP := $(BUILD_STRIP), \
 132     SYSROOT_CFLAGS := $(BUILD_SYSROOT_CFLAGS), \
 133     SYSROOT_LDFLAGS := $(BUILD_SYSROOT_LDFLAGS), \
 134 ))
 135 
 136 # BUILD toolchain with the C++ linker
 137 $(eval $(call DefineNativeToolchain, TOOLCHAIN_BUILD_LINK_CXX, \
 138     EXTENDS := TOOLCHAIN_BUILD, \
 139     LD := $(BUILD_LDCXX), \
 140 ))
 141 
 142 ################################################################################
 143 
 144 # Extensions of files handled by this macro.
 145 NATIVE_SOURCE_EXTENSIONS := %.s %.S %.c %.cpp %.cc %.m %.mm
 146 
 147 # Replaces native source extensions with the object file extension in a string.
 148 # Param 1: the string containing source file names with extensions
 149 # The surrounding strip is needed to keep additional whitespace out
 150 define replace_with_obj_extension
 151 $(strip \
 152   $(foreach extension, $(NATIVE_SOURCE_EXTENSIONS), \
 153       $(patsubst $(extension),%$(OBJ_SUFFIX), $(filter $(extension), $1))) \
 154 )
 155 endef
 156 
 157 ifeq ($(OPENJDK_BUILD_OS_ENV), windows.cygwin)
 158   UNIX_PATH_PREFIX := /cygdrive
 159 else ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys)
 160   UNIX_PATH_PREFIX :=
 161 endif
 162 
 163 # This pattern is used to transform the output of the microsoft CL compiler
 164 # into a make syntax dependency file (.d)
 165 WINDOWS_SHOWINCLUDE_SED_PATTERN := \
 166     -e '/^Note: including file:/!d' \
 167     -e 's|Note: including file: *||' \
 168     -e 's|\r||g' \
 169     -e 's|\\|/|g' \
 170     -e 's|^\([a-zA-Z]\):|$(UNIX_PATH_PREFIX)/\1|g' \
 171     -e '\|$(TOPDIR)|I !d' \
 172     -e 's|$$$$| \\|g' \
 173     #
 174 
 175 # This pattern is used to transform a dependency file (.d) to a list
 176 # of make targets for dependent files (.d.targets)
 177 DEPENDENCY_TARGET_SED_PATTERN := \
 178     -e 's/\#.*//' \
 179     -e 's/^[^:]*: *//' \
 180     -e 's/ *\\$$$$//' \
 181     -e 's/^[     ]*//' \
 182     -e '/^$$$$/ d' \
 183     -e 's/$$$$/ :/' \
 184     #
 185 
 186 ################################################################################
 187 # Create the recipe needed to compile a single native source file.
 188 #
 189 # Parameter 1 is the name of the rule, based on the name of the library/
 190 # program being build and the name of the source code file, e.g.
 191 # BUILD_LIBFOO_fooMain.cpp.
 192 #
 193 # Remaining parameters are named arguments:
 194 #   FILE - The full path of the source file to compiler
 195 #   BASE - The name of the rule for the entire binary to build ($1)
 196 #   DISABLE_THIS_FILE_DEFINE - Set to true to disable the THIS_FILE define.
 197 #
 198 SetupCompileNativeFile = $(NamedParamsMacroTemplate)
 199 define SetupCompileNativeFileBody
 200   $1_FILENAME := $$(notdir $$($1_FILE))
 201 
 202   # The target file to be generated.
 203   $1_OBJ := $$($$($1_BASE)_OBJECT_DIR)/$$(call replace_with_obj_extension, \
 204       $$($1_FILENAME))
 205 
 206   # Only continue if this object file hasn't been processed already. This lets
 207   # the first found source file override any other with the same name.
 208   ifeq ($$(findstring $$($1_OBJ), $$($$($1_BASE)_OBJS_SO_FAR)), )
 209     $$($1_BASE)_OBJS_SO_FAR += $$($1_OBJ)
 210     # This is the definite source file to use for $1_FILENAME.
 211     $1_SRC_FILE := $$($1_FILE)
 212 
 213     ifneq ($$($1_DISABLE_THIS_FILE_DEFINE), true)
 214       $1_THIS_FILE = -DTHIS_FILE='"$$(<F)"'
 215     endif
 216 
 217     ifeq ($$($1_OPTIMIZATION), )
 218       $1_OPT_CFLAGS := $$($$($1_BASE)_OPT_CFLAGS)
 219       $1_OPT_CXXFLAGS := $$($$($1_BASE)_OPT_CXXFLAGS)
 220     else
 221       ifeq ($$($1_OPTIMIZATION), NONE)
 222         $1_OPT_CFLAGS := $(C_O_FLAG_NONE)
 223         $1_OPT_CXXFLAGS := $(CXX_O_FLAG_NONE)
 224       else ifeq ($$($1_OPTIMIZATION), LOW)
 225         $1_OPT_CFLAGS := $(C_O_FLAG_NORM)
 226         $1_OPT_CXXFLAGS := $(CXX_O_FLAG_NORM)
 227       else ifeq ($$($1_OPTIMIZATION), HIGH)
 228         $1_OPT_CFLAGS := $(C_O_FLAG_HI)
 229         $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HI)
 230       else ifeq ($$($1_OPTIMIZATION), HIGHEST)
 231         $1_OPT_CFLAGS := $(C_O_FLAG_HIGHEST)
 232         $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST)
 233       else ifeq ($$($1_OPTIMIZATION), HIGHEST_JVM)
 234         $1_OPT_CFLAGS := $(C_O_FLAG_HIGHEST_JVM)
 235         $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST_JVM)
 236       else ifeq ($$($1_OPTIMIZATION), SIZE)
 237         $1_OPT_CFLAGS := $(C_O_FLAG_SIZE)
 238         $1_OPT_CXXFLAGS := $(CXX_O_FLAG_SIZE)
 239       else
 240         $$(error Unknown value for file OPTIMIZATION: $$($1_OPTIMIZATION))
 241       endif
 242     endif
 243 
 244     ifneq ($$($$($1_BASE)_PRECOMPILED_HEADER), )
 245       ifeq ($$(filter $$($1_FILENAME), $$($$($1_BASE)_PRECOMPILED_HEADER_EXCLUDE)), )
 246         $1_USE_PCH_FLAGS := $$($$($1_BASE)_USE_PCH_FLAGS)
 247       endif
 248     endif
 249 
 250     $1_BASE_CFLAGS :=  $$($$($1_BASE)_CFLAGS) $$($$($1_BASE)_EXTRA_CFLAGS) \
 251         $$($$($1_BASE)_SYSROOT_CFLAGS)
 252     $1_BASE_CXXFLAGS := $$($$($1_BASE)_CXXFLAGS) $$($$($1_BASE)_EXTRA_CXXFLAGS) \
 253         $$($$($1_BASE)_SYSROOT_CFLAGS) $$($1_EXTRA_CXXFLAGS)
 254 
 255     ifneq ($$(filter %.c, $$($1_FILENAME)), )
 256       # Compile as a C file
 257       $1_FLAGS := $(CFLAGS_CCACHE) $$($1_USE_PCH_FLAGS) $$($1_BASE_CFLAGS) \
 258           $$($1_OPT_CFLAGS) $$($1_CFLAGS) $$($1_THIS_FILE) -c
 259       $1_COMPILER := $$($$($1_BASE)_CC)
 260       $1_DEP_FLAG := $(C_FLAG_DEPS)
 261     else ifneq ($$(filter %.m, $$($1_FILENAME)), )
 262       # Compile as an Objective-C file
 263       $1_FLAGS := -x objective-c $(CFLAGS_CCACHE) $$($1_USE_PCH_FLAGS) \
 264           $$($1_BASE_CFLAGS) $$($1_OPT_CFLAGS) $$($1_CFLAGS) $$($1_THIS_FILE) -c
 265       $1_COMPILER := $$($$($1_BASE)_CC)
 266       $1_DEP_FLAG := $(C_FLAG_DEPS)
 267     else ifneq ($$(filter %.s %.S, $$($1_FILENAME)), )
 268       # Compile as assembler file
 269       $1_FLAGS := $$($$($1_BASE)_ASFLAGS)
 270       $1_COMPILER := $(AS)
 271       $1_DEP_FLAG :=
 272     else ifneq ($$(filter %.cpp %.cc %.mm, $$($1_FILENAME)), )
 273       # Compile as a C++ or Objective-C++ file
 274       $1_FLAGS := $(CFLAGS_CCACHE) $$($1_USE_PCH_FLAGS) $$($1_BASE_CXXFLAGS) \
 275           $$($1_OPT_CXXFLAGS) $$($1_CXXFLAGS) $$($1_THIS_FILE) -c
 276       $1_COMPILER := $$($$($1_BASE)_CXX)
 277       $1_DEP_FLAG := $(CXX_FLAG_DEPS)
 278     else
 279       $$(error Internal error in NativeCompilation.gmk: no compiler for file $$($1_FILENAME))
 280     endif
 281 
 282     ifeq ($$(filter %.s %.S, $$($1_FILENAME)), )
 283       # And this is the dependency file for this obj file.
 284       $1_DEP := $$(patsubst %$(OBJ_SUFFIX),%.d,$$($1_OBJ))
 285       # The dependency target file lists all dependencies as empty targets to
 286       # avoid make error "No rule to make target" for removed files
 287       $1_DEP_TARGETS := $$(patsubst %$(OBJ_SUFFIX),%.d.targets,$$($1_OBJ))
 288 
 289       # Include previously generated dependency information. (if it exists)
 290       -include $$($1_DEP)
 291       -include $$($1_DEP_TARGETS)
 292     endif
 293 
 294     ifneq ($$(strip $$($1_CFLAGS) $$($1_CXXFLAGS) $$($1_OPTIMIZATION)), )
 295       $1_VARDEPS := $$($1_CFLAGS) $$($1_CXXFLAGS) $$($1_OPTIMIZATION)
 296       $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, $$($1_OBJ).vardeps)
 297     endif
 298 
 299     $$($1_OBJ): $$($1_SRC_FILE) $$($$($1_BASE)_COMPILE_VARDEPS_FILE) \
 300         $$($1_VARDEPS_FILE) | $$($$($1_BASE)_BUILD_INFO)
 301         $$(call LogInfo, Compiling $$($1_FILENAME) (for $$($$($1_BASE)_BASENAME)))
 302         $$(call MakeDir, $$(@D))
 303         ifneq ($(TOOLCHAIN_TYPE), microsoft)
 304           ifeq ($(TOOLCHAIN_TYPE)$$(filter %.s, $$($1_FILENAME)), solstudio)
 305             # The Solaris studio compiler doesn't output the full path to the
 306             # object file in the generated deps files. Fixing it with sed. If
 307             # compiling assembly, don't try this.
 308             $$(call ExecuteWithLog, $$@, \
 309                 $$($1_COMPILER) $$($1_FLAGS) $$($1_DEP_FLAG) $$($1_DEP).tmp \
 310                     $(CC_OUT_OPTION)$$($1_OBJ) $$($1_SRC_FILE))
 311             $(SED) 's|^$$(@F):|$$@:|' $$($1_DEP).tmp > $$($1_DEP)
 312           else
 313             $$(call ExecuteWithLog, $$@, \
 314                 $$($1_COMPILER) $$($1_FLAGS) $$($1_DEP_FLAG) $$($1_DEP) \
 315                     $(CC_OUT_OPTION)$$($1_OBJ) $$($1_SRC_FILE))
 316           endif
 317           # Create a dependency target file from the dependency file.
 318           # Solution suggested by http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
 319           ifneq ($$($1_DEP), )
 320             $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_DEP) > $$($1_DEP_TARGETS)
 321           endif
 322         else
 323           # The Visual Studio compiler lacks a feature for generating make
 324           # dependencies, but by setting -showIncludes, all included files are
 325           # printed. These are filtered out and parsed into make dependences.
 326           #
 327           # Keep as much as possible on one execution line for best performance
 328           # on Windows. No need to save exit code from compilation since
 329           # pipefail is always active on Windows.
 330           $$(call ExecuteWithLog, $$@, \
 331               $$($1_COMPILER) $$($1_FLAGS) -showIncludes \
 332                   $(CC_OUT_OPTION)$$($1_OBJ) $$($1_SRC_FILE)) \
 333               | $(TR) -d '\r' | $(GREP) -v -e "^Note: including file:" \
 334                   -e "^$$($1_FILENAME)$$$$" || test "$$$$?" = "1" ; \
 335           $(ECHO) $$@: \\ > $$($1_DEP) ; \
 336           $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_OBJ).log \
 337               | $(SORT) -u >> $$($1_DEP) ; \
 338           $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_DEP) > $$($1_DEP_TARGETS)
 339         endif
 340   endif
 341 endef
 342 
 343 # Setup make rules for creating a native binary (a shared library or an
 344 # executable).
 345 #
 346 # Parameter 1 is the name of the rule. This name is used as variable prefix,
 347 # and the targets generated are listed in a variable by that name.
 348 #
 349 # Remaining parameters are named arguments. These include:
 350 #   NAME The base name for the resulting binary, excluding decorations (like *.exe)
 351 #   TYPE Type of binary (EXECUTABLE, LIBRARY or STATIC_LIBRARY). Default is LIBRARY.
 352 #   SUFFIX Override the default suffix for the output file
 353 #   TOOLCHAIN Name of toolchain setup to use. Defaults to TOOLCHAIN_DEFAULT.
 354 #   SRC one or more directory roots to scan for C/C++ files.
 355 #   CFLAGS the compiler flags to be used, used both for C and C++.
 356 #   CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS.
 357 #   LDFLAGS the linker flags to be used, used both for C and C++.
 358 #   LIBS the libraries to link to
 359 #   ARFLAGS the archiver flags to be used
 360 #   OBJECT_DIR the directory where we store the object files
 361 #   OUTPUT_DIR the directory where the resulting binary is put
 362 #   INCLUDES only pick source from these directories
 363 #   EXCLUDES do not pick source from these directories
 364 #   INCLUDE_FILES only compile exactly these files!
 365 #   EXCLUDE_FILES with these names
 366 #   EXCLUDE_PATTERN exclude files matching any of these substrings
 367 #   EXTRA_FILES List of extra files not in any of the SRC dirs
 368 #   EXTRA_OBJECT_FILES List of extra object files to include when linking
 369 #   VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run
 370 #   RC_FLAGS flags for RC.
 371 #   EMBED_MANIFEST if true, embed manifest on Windows.
 372 #   MAPFILE mapfile
 373 #   REORDER reorder file
 374 #   USE_MAPFILE_FOR_SYMBOLS if true and this is a STATIC_BUILD, just copy the
 375 #       mapfile for the output symbols file
 376 #   CC the compiler to use, default is $(CC)
 377 #   LD the linker to use, default is $(LD)
 378 #   OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST, HIGHEST_JVM, SIZE
 379 #   DISABLED_WARNINGS_<toolchain> Disable the given warnings for the specified toolchain
 380 #   DISABLED_WARNINGS_C_<toolchain> Disable the given warnings for the specified toolchain
 381 #       when compiling C code
 382 #   DISABLED_WARNINGS_CXX_<toolchain> Disable the given warnings for the specified
 383 #       toolchain when compiling C++ code
 384 #   STRIP_SYMBOLS Set to false to override global strip policy and always leave
 385 #       symbols in the binary, if the toolchain allows for it
 386 #   DEBUG_SYMBOLS Set to false to disable generation of debug symbols
 387 #   COPY_DEBUG_SYMBOLS Set to false to override global setting of debug symbol copying
 388 #   ZIP_EXTERNAL_DEBUG_SYMBOLS Set to false to override global setting of debug symbol
 389 #       zipping
 390 #   STRIPFLAGS Optionally change the flags given to the strip command
 391 #   PRECOMPILED_HEADER Header file to use as precompiled header
 392 #   PRECOMPILED_HEADER_EXCLUDE List of source files that should not use PCH
 393 SetupNativeCompilation = $(NamedParamsMacroTemplate)
 394 define SetupNativeCompilationBody
 395 
 396   # If type is unspecified, default to LIBRARY
 397   ifeq ($$($1_TYPE), )
 398     $1_TYPE := LIBRARY
 399   endif
 400 
 401   # If we're doing a static build and producing a library
 402   # force it to be a static library and remove the -l libraries
 403   ifeq ($(STATIC_BUILD), true)
 404     ifeq ($$($1_TYPE), LIBRARY)
 405       $1_TYPE := STATIC_LIBRARY
 406     endif
 407   endif
 408 
 409   ifeq ($$($1_TYPE), EXECUTABLE)
 410     $1_PREFIX :=
 411     ifeq ($$($1_SUFFIX), )
 412       $1_SUFFIX := $(EXE_SUFFIX)
 413     endif
 414   else
 415     $1_PREFIX := $(LIBRARY_PREFIX)
 416     ifeq ($$($1_TYPE), LIBRARY)
 417       ifeq ($$($1_SUFFIX), )
 418         $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX)
 419       endif
 420     else ifeq ($$($1_TYPE), STATIC_LIBRARY)
 421       ifeq ($$($1_SUFFIX), )
 422         $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX)
 423       endif
 424     endif
 425   endif
 426 
 427   ifneq ($$($1_NAME), $(basename $$($1_NAME)))
 428     $$(error NAME must not contain any directory path in $1)
 429   endif
 430   ifneq ($(findstring $$($1_SUFFIX), $$($1_NAME)), )
 431     $$(error NAME should be specified without suffix: $$($1_SUFFIX) in $1)
 432   endif
 433   ifneq ($(findstring $$($1_PREFIX), $$($1_NAME)), )
 434     $$(error NAME should be specified without prefix: $$($1_PREFIX) in $1)
 435   endif
 436   ifeq ($$($1_OUTPUT_DIR), )
 437     $$(error OUTPUT_DIR is missing in $1)
 438   endif
 439   ifneq ($$($1_MANIFEST), )
 440     ifeq ($$($1_MANIFEST_VERSION), )
 441       $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1)
 442     endif
 443   endif
 444 
 445   $1_BASENAME := $$($1_PREFIX)$$($1_NAME)$$($1_SUFFIX)
 446   $1_TARGET := $$($1_OUTPUT_DIR)/$$($1_BASENAME)
 447   $1_NOSUFFIX := $$($1_PREFIX)$$($1_NAME)
 448   $1_SAFE_NAME := $$(strip $$(subst /,_, $1))
 449 
 450   # Setup the toolchain to be used
 451   $$(call SetIfEmpty, $1_TOOLCHAIN, TOOLCHAIN_DEFAULT)
 452   $$(call SetIfEmpty, $1_CC, $$($$($1_TOOLCHAIN)_CC))
 453   $$(call SetIfEmpty, $1_CXX, $$($$($1_TOOLCHAIN)_CXX))
 454   $$(call SetIfEmpty, $1_LD, $$($$($1_TOOLCHAIN)_LD))
 455   $$(call SetIfEmpty, $1_AR, $$($$($1_TOOLCHAIN)_AR))
 456   $$(call SetIfEmpty, $1_AS, $$($$($1_TOOLCHAIN)_AS))
 457   $$(call SetIfEmpty, $1_MT, $$($$($1_TOOLCHAIN)_MT))
 458   $$(call SetIfEmpty, $1_RC, $$($$($1_TOOLCHAIN)_RC))
 459   $$(call SetIfEmpty, $1_OBJCOPY, $$($$($1_TOOLCHAIN)_OBJCOPY))
 460   $$(call SetIfEmpty, $1_STRIP, $$($$($1_TOOLCHAIN)_STRIP))
 461   $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_CFLAGS))
 462   $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_LDFLAGS))
 463 
 464   # Make sure the dirs exist.
 465   $$(call MakeDir, $$($1_OBJECT_DIR) $$($1_OUTPUT_DIR))
 466   $$(foreach d, $$($1_SRC), $$(if $$(wildcard $$d), , \
 467       $$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d)))
 468 
 469   # Find all files in the source trees. Preserve order.
 470   $1_SRCS := $$(foreach s, $$($1_SRC), $$(call CacheFind, $$(s)))
 471   $1_SRCS := $$(filter $$(NATIVE_SOURCE_EXTENSIONS), $$($1_SRCS))
 472   # Extract the C/C++ files.
 473   ifneq ($$($1_EXCLUDE_PATTERNS), )
 474     # We must not match the exclude pattern against the src root(s).
 475     $1_SRCS_WITHOUT_ROOTS := $$($1_SRCS)
 476     $$(foreach i, $$($1_SRC), $$(eval $1_SRCS_WITHOUT_ROOTS := $$(patsubst \
 477         $$i/%,%, $$($1_SRCS_WITHOUT_ROOTS))))
 478     $1_ALL_EXCLUDE_FILES :=  $$(call containing, $$($1_EXCLUDE_PATTERNS), \
 479         $$($1_SRCS_WITHOUT_ROOTS))
 480   endif
 481   ifneq ($$($1_EXCLUDE_FILES), )
 482     $1_ALL_EXCLUDE_FILES += $$($1_EXCLUDE_FILES)
 483   endif
 484   ifneq ($$($1_ALL_EXCLUDE_FILES), )
 485     $1_EXCLUDE_FILES_PAT := $$($1_ALL_EXCLUDE_FILES) \
 486         $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$($1_ALL_EXCLUDE_FILES)))
 487     $1_EXCLUDE_FILES_PAT := $$(addprefix %, $$($1_EXCLUDE_FILES_PAT))
 488     $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES_PAT), $$($1_SRCS))
 489   endif
 490   ifneq ($$($1_INCLUDE_FILES), )
 491     $1_INCLUDE_FILES_PAT := $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$($1_INCLUDE_FILES)))
 492     $1_SRCS := $$(filter $$($1_INCLUDE_FILES_PAT), $$($1_SRCS))
 493   endif
 494   # There can be only a single bin dir root, no need to foreach over the roots.
 495   $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX))
 496   # Now we have a list of all c/c++ files to compile: $$($1_SRCS)
 497   # and we have a list of all existing object files: $$($1_BINS)
 498 
 499   # Prepend the source/bin path to the filter expressions. Then do the filtering.
 500   ifneq ($$($1_INCLUDES), )
 501     $1_SRC_INCLUDES := $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$(addsuffix /%, $$($1_INCLUDES))))
 502     $1_SRCS := $$(filter $$($1_SRC_INCLUDES), $$($1_SRCS))
 503   endif
 504   ifneq ($$($1_EXCLUDES), )
 505     $1_SRC_EXCLUDES := $$(addsuffix /%, $$($1_EXCLUDES))
 506     $1_SRC_EXCLUDES += $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$(addsuffix /%, $$($1_EXCLUDES))))
 507     $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES), $$($1_SRCS))
 508   endif
 509 
 510   $1_SRCS += $$($1_EXTRA_FILES)
 511 
 512   ifeq ($$($1_SRCS), )
 513     $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
 514   endif
 515 
 516   # Calculate the expected output from compiling the sources
 517   $1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS)))
 518   $1_EXPECTED_OBJS := $$(addprefix $$($1_OBJECT_DIR)/, $$($1_EXPECTED_OBJS_FILENAMES))
 519   # Are there too many object files on disk? Perhaps because some source file was removed?
 520   $1_SUPERFLOUS_OBJS := $$(sort $$(filter-out $$($1_EXPECTED_OBJS), $$($1_BINS)))
 521   # Clean out the superfluous object files.
 522   ifneq ($$($1_SUPERFLUOUS_OBJS), )
 523     $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS))
 524   endif
 525   # Sort to remove dupliates and provide a reproducable order on the input files to the linker.
 526   $1_ALL_OBJS := $$(sort $$($1_EXPECTED_OBJS) $$($1_EXTRA_OBJECT_FILES))
 527 
 528   # Pickup extra OPENJDK_TARGET_OS_TYPE, OPENJDK_TARGET_OS, and/or OPENJDK_TARGET_OS plus
 529   # OPENJDK_TARGET_CPU pair dependent variables for CFLAGS.
 530   $1_EXTRA_CFLAGS := $$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS)) \
 531       $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU))
 532   ifneq ($(DEBUG_LEVEL), release)
 533     # Pickup extra debug dependent variables for CFLAGS
 534     $1_EXTRA_CFLAGS += $$($1_CFLAGS_debug)
 535     $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
 536     $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug)
 537     $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)_debug)
 538   else
 539     $1_EXTRA_CFLAGS += $$($1_CFLAGS_release)
 540     $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
 541     $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release)
 542     $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)_release)
 543   endif
 544 
 545   # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS.
 546   $1_EXTRA_CXXFLAGS := $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS))
 547   ifneq ($(DEBUG_LEVEL), release)
 548     # Pickup extra debug dependent variables for CXXFLAGS
 549     $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_debug)
 550     $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
 551     $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug)
 552   else
 553     $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_release)
 554     $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
 555     $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release)
 556   endif
 557 
 558   # If no C++ flags are explicitly set, default to using the C flags.
 559   # After that, we can set additional C++ flags that should not interfere
 560   # with the mechanism for copying the C flags by default.
 561   ifeq ($$($1_CXXFLAGS), )
 562     $1_CXXFLAGS := $$($1_CFLAGS)
 563   endif
 564   ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)), )
 565     $1_EXTRA_CXXFLAGS := $$($1_EXTRA_CFLAGS)
 566   endif
 567 
 568   ifeq ($(COMPILE_WITH_DEBUG_SYMBOLS), true)
 569     $1_EXTRA_CFLAGS += $$(CFLAGS_DEBUG_SYMBOLS)
 570     $1_EXTRA_CXXFLAGS += $$(CFLAGS_DEBUG_SYMBOLS)
 571   endif
 572 
 573   ifneq ($$($1_REORDER), )
 574     $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER)
 575     $1_EXTRA_CXXFLAGS += $$(C_FLAG_REORDER)
 576   endif
 577 
 578   # Pass the library name for static JNI library naming
 579   ifeq ($$($1_TYPE), STATIC_LIBRARY)
 580     $1_EXTRA_CFLAGS += -DLIBRARY_NAME=$$($1_NAME)
 581     $1_EXTRA_CXXFLAGS += -DLIBRARY_NAME=$$($1_NAME)
 582   endif
 583 
 584   # Pick up disabled warnings, if possible on this platform.
 585   ifneq ($(DISABLE_WARNING_PREFIX), )
 586     $1_EXTRA_CFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), \
 587         $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)) \
 588         $$($1_DISABLED_WARNINGS_C_$(TOOLCHAIN_TYPE)))
 589     $1_EXTRA_CXXFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), \
 590         $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)) \
 591         $$($1_DISABLED_WARNINGS_CXX_$(TOOLCHAIN_TYPE)))
 592   endif
 593 
 594   # Check if warnings should be considered errors.
 595   # Pick first binary and toolchain specific, then binary specific, then general setting.
 596   ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), )
 597     ifeq ($$($1_WARNINGS_AS_ERRORS), )
 598       $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$(WARNINGS_AS_ERRORS)
 599     else
 600       $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$($1_WARNINGS_AS_ERRORS)
 601     endif
 602   endif
 603 
 604   ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), true)
 605     $1_EXTRA_CFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
 606     $1_EXTRA_CXXFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS)
 607   endif
 608 
 609   ifeq (NONE, $$($1_OPTIMIZATION))
 610     $1_OPT_CFLAGS := $(C_O_FLAG_NONE)
 611     $1_OPT_CXXFLAGS := $(CXX_O_FLAG_NONE)
 612   else ifeq (LOW, $$($1_OPTIMIZATION))
 613     $1_OPT_CFLAGS := $(C_O_FLAG_NORM)
 614     $1_OPT_CXXFLAGS := $(CXX_O_FLAG_NORM)
 615   else ifeq (HIGH, $$($1_OPTIMIZATION))
 616     $1_OPT_CFLAGS := $(C_O_FLAG_HI)
 617     $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HI)
 618   else ifeq (HIGHEST, $$($1_OPTIMIZATION))
 619     $1_OPT_CFLAGS := $(C_O_FLAG_HIGHEST)
 620     $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST)
 621   else ifeq (HIGHEST_JVM, $$($1_OPTIMIZATION))
 622     $1_OPT_CFLAGS := $(C_O_FLAG_HIGHEST_JVM)
 623     $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST_JVM)
 624   else ifeq (SIZE, $$($1_OPTIMIZATION))
 625     $1_OPT_CFLAGS := $(C_O_FLAG_SIZE)
 626     $1_OPT_CXXFLAGS := $(CXX_O_FLAG_SIZE)
 627   else ifneq (, $$($1_OPTIMIZATION))
 628     $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION))
 629   endif
 630 
 631   $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker
 632 
 633   # Track variable changes for all variables that affect the compilation command
 634   # lines for all object files in this setup. This includes at least all the
 635   # variables used in the call to add_native_source below.
 636   $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
 637       $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_OPT_CFLAGS) $$($1_OPT_CXXFLAGS) \
 638       $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS)
 639   $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \
 640       $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps)
 641 
 642   ifneq ($$($1_PRECOMPILED_HEADER), )
 643     ifeq ($(USE_PRECOMPILED_HEADER), true)
 644       ifeq ($(TOOLCHAIN_TYPE), microsoft)
 645         $1_PCH_FILE := $$($1_OBJECT_DIR)/$1.pch
 646         $1_GENERATED_PCH_SRC := $$($1_OBJECT_DIR)/$1_pch.cpp
 647         $1_GENERATED_PCH_OBJ := $$($1_OBJECT_DIR)/$1_pch.obj
 648 
 649         $$(eval $$(call SetupCompileNativeFile, $1_$$(notdir $$($1_GENERATED_PCH_SRC)), \
 650             FILE := $$($1_GENERATED_PCH_SRC), \
 651             BASE := $1, \
 652             EXTRA_CXXFLAGS := -Fp$$($1_PCH_FILE) -Yc$$(notdir $$($1_PRECOMPILED_HEADER)), \
 653             DISABLE_THIS_FILE_DEFINE := true, \
 654         ))
 655 
 656         $1_USE_PCH_FLAGS := \
 657             -Fp$$($1_PCH_FILE) -Yu$$(notdir $$($1_PRECOMPILED_HEADER))
 658 
 659         $$($1_ALL_OBJS): $$($1_GENERATED_PCH_OBJ)
 660 
 661         # Explicitly add the pch obj file first to ease comparing to old
 662         # hotspot build.
 663         $1_ALL_OBJS := $$($1_GENERATED_PCH_OBJ) $$($1_ALL_OBJS)
 664 
 665         $$($1_GENERATED_PCH_SRC):
 666                 $(ECHO) "#include \"$$(notdir $$($1_PRECOMPILED_HEADER))\"" > $$@
 667 
 668       else ifneq ($(findstring $(TOOLCHAIN_TYPE), gcc clang), )
 669         ifeq ($(TOOLCHAIN_TYPE), gcc)
 670           $1_PCH_FILE := $$($1_OBJECT_DIR)/precompiled/$$(notdir $$($1_PRECOMPILED_HEADER)).gch
 671           $1_USE_PCH_FLAGS := -I$$($1_OBJECT_DIR)/precompiled
 672         else ifeq ($(TOOLCHAIN_TYPE), clang)
 673           $1_PCH_FILE := $$($1_OBJECT_DIR)/precompiled/$$(notdir $$($1_PRECOMPILED_HEADER)).pch
 674           $1_USE_PCH_FLAGS := -include-pch $$($1_PCH_FILE)
 675         endif
 676         $1_PCH_DEP := $$($1_PCH_FILE).d
 677         $1_PCH_DEP_TARGETS := $$($1_PCH_FILE).d.targets
 678 
 679         -include $$($1_PCH_DEP)
 680         -include $$($1_PCH_DEP_TARGETS)
 681 
 682         $$($1_PCH_FILE): $$($1_PRECOMPILED_HEADER) $$($1_COMPILE_VARDEPS_FILE)
 683                 $$(call LogInfo, Generating precompiled header)
 684                 $$(call MakeDir, $$(@D))
 685                 $$(call ExecuteWithLog, $$@, \
 686                     $$($1_CC) $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
 687                     $$($1_OPT_CFLAGS) \
 688                     -x c++-header -c $(C_FLAG_DEPS) $$($1_PCH_DEP) $$< -o $$@)
 689                 $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_PCH_DEP) > $$($1_PCH_DEP_TARGETS)
 690 
 691         $$($1_ALL_OBJS): $$($1_PCH_FILE)
 692 
 693       endif
 694     endif
 695   endif
 696 
 697   # Now call SetupCompileNativeFile for each source file we are going to compile.
 698   $$(foreach file, $$($1_SRCS), \
 699       $$(eval $$(call SetupCompileNativeFile, $1_$$(notdir $$(file)),\
 700           FILE := $$(file), \
 701           BASE := $1, \
 702       )) \
 703   )
 704 
 705   # Setup rule for printing progress info when compiling source files.
 706   # This is a rough heuristic and may not always print accurate information.
 707   $$($1_BUILD_INFO): $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE)
 708         ifeq ($$(wildcard $$($1_TARGET)), )
 709           $(ECHO) 'Creating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET)) from $$(words \
 710               $$(filter-out %.vardeps, $$?)) file(s)'
 711         else
 712           $(ECHO) $$(strip 'Updating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET))' \
 713               $$(if $$(filter-out %.vardeps, $$?), \
 714                 'due to $$(words $$(filter-out %.vardeps, $$?)) file(s)', \
 715               $$(if $$(filter %.vardeps, $$?), 'due to makefile changes')))
 716         endif
 717         $(TOUCH) $$@
 718 
 719   # On windows we need to create a resource file
 720   ifeq ($(OPENJDK_TARGET_OS), windows)
 721     ifneq ($$($1_VERSIONINFO_RESOURCE), )
 722       $1_RES := $$($1_OBJECT_DIR)/$$($1_BASENAME).res
 723       $1_RES_DEP := $$($1_RES).d
 724       $1_RES_DEP_TARGETS := $$($1_RES).d.targets
 725       -include $$($1_RES_DEP)
 726       -include $$($1_RES_DEP_TARGETS)
 727 
 728       $1_RES_VARDEPS := $$($1_RC) $$($1_RC_FLAGS)
 729       $1_RES_VARDEPS_FILE := $$(call DependOnVariable, $1_RES_VARDEPS, \
 730           $$($1_RES).vardeps)
 731 
 732       $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE)
 733                 $$(call LogInfo, Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$($1_BASENAME)))
 734                 $$(call MakeDir, $$(@D) $$($1_OBJECT_DIR))
 735                 $$(call ExecuteWithLog, $$@, \
 736                     $$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \
 737                     $$($1_VERSIONINFO_RESOURCE) 2>&1 )
 738                 # Windows RC compiler does not support -showIncludes, so we mis-use CL
 739                 # for this. Filter out RC specific arguments that are unknown to CL.
 740                 # For some unknown reason, in this case CL actually outputs the show
 741                 # includes to stderr so need to redirect it to hide the output from the
 742                 # main log.
 743                 $$(call ExecuteWithLog, $$($1_RES_DEP).obj, \
 744                     $$($1_CC) $$(filter-out -l%, $$($1_RC_FLAGS)) \
 745                         $$($1_SYSROOT_CFLAGS) -showIncludes -nologo -TC \
 746                         $(CC_OUT_OPTION)$$($1_RES_DEP).obj -P -Fi$$($1_RES_DEP).pp \
 747                         $$($1_VERSIONINFO_RESOURCE)) 2>&1 \
 748                     | $(TR) -d '\r' | $(GREP) -v -e "^Note: including file:" \
 749                         -e "^$$(notdir $$($1_VERSIONINFO_RESOURCE))$$$$" || test "$$$$?" = "1" ; \
 750                 $(ECHO) $$($1_RES): \\ > $$($1_RES_DEP) ; \
 751                 $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).obj.log >> $$($1_RES_DEP) ; \
 752                 $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_RES_DEP) > $$($1_RES_DEP_TARGETS)
 753     endif
 754   endif
 755 
 756   ifneq ($(DISABLE_MAPFILES), true)
 757     $1_REAL_MAPFILE := $$($1_MAPFILE)
 758     ifneq ($(OPENJDK_TARGET_OS), windows)
 759       ifneq ($$($1_REORDER), )
 760         $1_REAL_MAPFILE := $$($1_OBJECT_DIR)/mapfile
 761 
 762         $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER)
 763                 $$(call MakeDir, $$(@D))
 764                 $$(CP) $$($1_MAPFILE) $$@.tmp
 765                 $$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp
 766                 $$(MV) $$@.tmp $$@
 767       endif
 768     endif
 769   endif
 770 
 771   # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables
 772   # for LDFLAGS and LIBS
 773   $1_EXTRA_LDFLAGS := $$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS))
 774   $1_EXTRA_LIBS := $$($1_LIBS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LIBS_$(OPENJDK_TARGET_OS))
 775   ifneq ($$($1_REAL_MAPFILE), )
 776     $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE))
 777   endif
 778 
 779   # Need to make sure TARGET is first on list
 780   $1 := $$($1_TARGET)
 781 
 782   ifneq ($$($1_COPY_DEBUG_SYMBOLS), false)
 783     $1_COPY_DEBUG_SYMBOLS := $(COPY_DEBUG_SYMBOLS)
 784   endif
 785 
 786   ifneq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), false)
 787     $1_ZIP_EXTERNAL_DEBUG_SYMBOLS := $(ZIP_EXTERNAL_DEBUG_SYMBOLS)
 788   endif
 789 
 790   ifeq ($$($1_COPY_DEBUG_SYMBOLS), true)
 791     ifneq ($$($1_DEBUG_SYMBOLS), false)
 792       # Only copy debug symbols for dynamic libraries and programs.
 793       ifneq ($$($1_TYPE), STATIC_LIBRARY)
 794         # Generate debuginfo files.
 795         ifeq ($(OPENJDK_TARGET_OS), windows)
 796           $1_EXTRA_LDFLAGS += -debug "-pdb:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb" \
 797               "-map:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map"
 798           $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb \
 799               $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map
 800 
 801         else ifneq ($(findstring $(OPENJDK_TARGET_OS), linux solaris), )
 802           $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).debuginfo
 803           # Setup the command line creating debuginfo files, to be run after linking.
 804           # It cannot be run separately since it updates the original target file
 805           $1_CREATE_DEBUGINFO_CMDS := \
 806               $$($1_OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \
 807               $(CD) $$($1_OUTPUT_DIR) && \
 808                   $$($1_OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET)
 809 
 810         else ifeq ($(OPENJDK_TARGET_OS), macosx)
 811           $1_DEBUGINFO_FILES := \
 812               $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist \
 813               $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME)
 814           $1_CREATE_DEBUGINFO_CMDS := \
 815               $(DSYMUTIL) --out $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM $$($1_TARGET)
 816         endif # OPENJDK_TARGET_OS
 817 
 818         # Since the link rule creates more than one file that we want to track,
 819         # we have to use some tricks to get make to cooperate. To properly
 820         # trigger downstream dependants of $$($1_DEBUGINFO_FILES), we must have
 821         # a recipe in the rule below. To avoid rerunning the recipe every time
 822         # have it touch the target. If a debuginfo file is deleted by something
 823         # external, explicitly delete the TARGET to trigger a rebuild of both.
 824         ifneq ($$(wildcard $$($1_DEBUGINFO_FILES)), $$($1_DEBUGINFO_FILES))
 825           $$(call LogDebug, Deleting $$($1_BASENAME) because debuginfo files are missing)
 826           $$(shell $(RM) $$($1_TARGET))
 827         endif
 828         $$($1_DEBUGINFO_FILES): $$($1_TARGET)
 829                 $$(if $$(CORRECT_FUNCTION_IN_RECIPE_EVALUATION), \
 830                   $$(if $$(wildcard $$@), , $$(error $$@ was not created for $$<)) \
 831                 )
 832                 $(TOUCH) $$@
 833 
 834         $1 += $$($1_DEBUGINFO_FILES)
 835 
 836         ifeq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), true)
 837           $1_DEBUGINFO_ZIP := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).diz
 838           $1 += $$($1_DEBUGINFO_ZIP)
 839 
 840           # The dependency on TARGET is needed for debuginfo files
 841           # to be rebuilt properly.
 842           $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET)
 843                 $(CD) $$($1_OUTPUT_DIR) && \
 844                     $(ZIPEXE) -q -r $$@ $$(subst $$($1_OUTPUT_DIR)/,, $$($1_DEBUGINFO_FILES))
 845 
 846         endif
 847        endif # !STATIC_LIBRARY
 848     endif # $1_DEBUG_SYMBOLS != false
 849   endif # COPY_DEBUG_SYMBOLS
 850 
 851   # Unless specifically set, stripping should only happen if symbols are also
 852   # being copied.
 853   $$(call SetIfEmpty, $1_STRIP_SYMBOLS, $$($1_COPY_DEBUG_SYMBOLS))
 854 
 855   ifneq ($$($1_STRIP_SYMBOLS), false)
 856     ifneq ($$($1_STRIP), )
 857       # Default to using the global STRIPFLAGS. Allow for overriding with an empty value
 858       $1_STRIPFLAGS ?= $(STRIPFLAGS)
 859       $1_STRIP_CMD := $$($1_STRIP) $$($1_STRIPFLAGS) $$($1_TARGET)
 860     endif
 861   endif
 862 
 863   ifeq ($$($1_TYPE), STATIC_LIBRARY)
 864     $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LIBS) \
 865         $$($1_EXTRA_LIBS)
 866     $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
 867         $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
 868 
 869     # Generating a static library, ie object file archive.
 870     ifeq ($(STATIC_BUILD), true)
 871       ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true)
 872         STATIC_MAPFILE_DEP := $$($1_MAPFILE)
 873       endif
 874     endif
 875 
 876     $$($1_TARGET): $$($1_ALL_OBJS) $$($1_RES) $$($1_VARDEPS_FILE) $$(STATIC_MAPFILE_DEP)
 877         $$(call LogInfo, Building static library $$($1_BASENAME))
 878         $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
 879             $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_ALL_OBJS) \
 880                 $$($1_RES))
 881         ifeq ($(STATIC_BUILD), true)
 882           ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true)
 883             $(CP) $$($1_MAPFILE) $$(@D)/$$(basename $$(@F)).symbols
 884           else
 885             $(GetSymbols)
 886           endif
 887         endif
 888   else
 889     # A shared dynamic library or an executable binary has been specified
 890     ifeq ($$($1_TYPE), LIBRARY)
 891       # Generating a dynamic library.
 892       $1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
 893 
 894       # Create loadmap on AIX. Helps in diagnosing some problems.
 895       ifneq ($(COMPILER_BINDCMD_FILE_FLAG), )
 896         $1_EXTRA_LDFLAGS += $(COMPILER_BINDCMD_FILE_FLAG)$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).loadmap
 897       endif
 898     endif
 899 
 900     ifeq ($(OPENJDK_TARGET_OS), windows)
 901       ifeq ($$($1_EMBED_MANIFEST), true)
 902         $1_EXTRA_LDFLAGS += -manifest:embed
 903       endif
 904 
 905       $1_IMPORT_LIBRARY := $$($1_OBJECT_DIR)/$$($1_NAME).lib
 906       $1_EXTRA_LDFLAGS += "-implib:$$($1_IMPORT_LIBRARY)"
 907       # To properly trigger downstream dependants of the import library, just as
 908       # for debug files, we must have a recipe in the rule. To avoid rerunning
 909       # the recipe every time have it touch the target. If an import library
 910       # file is deleted by something external, explicitly delete the target to
 911       # trigger a rebuild of both.
 912       ifneq ($$(wildcard $$($1_IMPORT_LIBRARY)), $$($1_IMPORT_LIBRARY))
 913         $$(call LogDebug, Deleting $$($1_BASENAME) because import library is missing)
 914         $$(shell $(RM) $$($1_TARGET))
 915       endif
 916       $$($1_IMPORT_LIBRARY): $$($1_TARGET)
 917                 $$(if $$(CORRECT_FUNCTION_IN_RECIPE_EVALUATION), \
 918                   $$(if $$(wildcard $$@), , $$(error $$@ was not created for $$<)) \
 919                 )
 920                 $(TOUCH) $$@
 921     endif
 922 
 923     $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
 924         $$(GLOBAL_LIBS) $$($1_LIBS) $$($1_EXTRA_LIBS) $$($1_MT) \
 925         $$($1_CODESIGN) $$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION) \
 926         $$($1_STRIP_CMD)
 927     $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
 928         $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
 929 
 930     $1_LD_OBJ_ARG := $$($1_ALL_OBJS)
 931 
 932     # If there are many object files, use an @-file...
 933     ifneq ($$(word 17, $$($1_ALL_OBJS)), )
 934       $1_OBJ_FILE_LIST := $$($1_OBJECT_DIR)/_$1_objectfilenames.txt
 935       ifneq ($(COMPILER_COMMAND_FILE_FLAG), )
 936         $1_LD_OBJ_ARG := $(COMPILER_COMMAND_FILE_FLAG)$$($1_OBJ_FILE_LIST)
 937       else
 938         # ...except for toolchains which don't support them.
 939         $1_LD_OBJ_ARG := `cat $$($1_OBJ_FILE_LIST)`
 940       endif
 941     endif
 942 
 943     # Unfortunately the @-file trick does not work reliably when using clang.
 944     # Clang does not propagate the @-file parameter to the ld sub process, but
 945     # instead puts the full content on the command line. At least the llvm ld
 946     # does not even support an @-file.
 947     #
 948     # When linking a large amount of object files, we risk hitting the limit
 949     # of the command line length even on posix systems if the path length of
 950     # the output dir is very long due to our use of absolute paths. To
 951     # mitigate this, use paths relative to the output dir when linking over
 952     # 500 files with clang and the output dir path is deep.
 953     ifneq ($$(word 500, $$($1_ALL_OBJS)), )
 954       ifeq ($$(TOOLCHAIN_TYPE), clang)
 955         # There is no strlen function in make, but checking path depth is a
 956         # reasonable approximation.
 957         ifneq ($$(word 10, $$(subst /, ,$$(OUTPUTDIR))), )
 958           $1_LINK_OBJS_RELATIVE := true
 959           $1_ALL_OBJS_RELATIVE := $$(patsubst $$(OUTPUTDIR)/%, %, $$($1_ALL_OBJS))
 960         endif
 961       endif
 962     endif
 963 
 964     $$($1_TARGET): $$($1_ALL_OBJS) $$($1_RES) $$($1_MANIFEST) \
 965         $$($1_REAL_MAPFILE) $$($1_VARDEPS_FILE)
 966                 ifneq ($$($1_OBJ_FILE_LIST), )
 967                   ifeq ($$($1_LINK_OBJS_RELATIVE), true)
 968                     $$(eval $$(call ListPathsSafely, $1_ALL_OBJS_RELATIVE, $$($1_OBJ_FILE_LIST)))
 969                   else
 970                     $$(eval $$(call ListPathsSafely, $1_ALL_OBJS, $$($1_OBJ_FILE_LIST)))
 971                   endif
 972                 endif
 973                 # Keep as much as possible on one execution line for best performance
 974                 # on Windows
 975                 $$(call LogInfo, Linking $$($1_BASENAME))
 976                 ifeq ($(OPENJDK_TARGET_OS), windows)
 977                   $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
 978                       $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
 979                           $(LD_OUT_OPTION)$$($1_TARGET) $$($1_LD_OBJ_ARG) $$($1_RES) $$(GLOBAL_LIBS) \
 980                           $$($1_LIBS) $$($1_EXTRA_LIBS)) \
 981                       | $(GREP) -v "^   Creating library .*\.lib and object .*\.exp" || \
 982                           test "$$$$?" = "1" ; \
 983                   $$($1_CREATE_DEBUGINFO_CMDS)
 984                   $$($1_STRIP_CMD)
 985                 else
 986                   $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
 987                       $$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
 988                       $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
 989                           $(LD_OUT_OPTION)$$($1_TARGET) $$($1_LD_OBJ_ARG) $$($1_RES) $$(GLOBAL_LIBS) \
 990                           $$($1_LIBS) $$($1_EXTRA_LIBS)) ; \
 991                   $$($1_CREATE_DEBUGINFO_CMDS)
 992                   $$($1_STRIP_CMD)
 993                 endif
 994                 ifeq ($(OPENJDK_TARGET_OS), windows)
 995                   ifneq ($$($1_MANIFEST), )
 996                     $$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_NAME).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1
 997                   endif
 998                 endif
 999                 # This only works if the openjdk_codesign identity is present on the system. Let
1000                 # silently fail otherwise.
1001                 ifneq ($(CODESIGN), )
1002                   ifneq ($$($1_CODESIGN), )
1003                     $(CODESIGN) -s openjdk_codesign $$@
1004                   endif
1005                 endif
1006   endif
1007 endef
1008 
1009 endif # _NATIVE_COMPILATION_GMK