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_BASE)_EXTRA_DEPS) $$($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 # EXTRA_DEPS List of extra dependencies to be added to each compiled file 370 # VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run 371 # RC_FLAGS flags for RC. 372 # EMBED_MANIFEST if true, embed manifest on Windows. 373 # MAPFILE mapfile 374 # REORDER reorder file 375 # USE_MAPFILE_FOR_SYMBOLS if true and this is a STATIC_BUILD, just copy the 376 # mapfile for the output symbols file 377 # CC the compiler to use, default is $(CC) 378 # LD the linker to use, default is $(LD) 379 # OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST, HIGHEST_JVM, SIZE 380 # DISABLED_WARNINGS_<toolchain> Disable the given warnings for the specified toolchain 381 # DISABLED_WARNINGS_C_<toolchain> Disable the given warnings for the specified toolchain 382 # when compiling C code 383 # DISABLED_WARNINGS_CXX_<toolchain> Disable the given warnings for the specified 384 # toolchain when compiling C++ code 385 # STRIP_SYMBOLS Set to false to override global strip policy and always leave 386 # symbols in the binary, if the toolchain allows for it 387 # DEBUG_SYMBOLS Set to false to disable generation of debug symbols 388 # COPY_DEBUG_SYMBOLS Set to false to override global setting of debug symbol copying 389 # ZIP_EXTERNAL_DEBUG_SYMBOLS Set to false to override global setting of debug symbol 390 # zipping 391 # STRIPFLAGS Optionally change the flags given to the strip command 392 # PRECOMPILED_HEADER Header file to use as precompiled header 393 # PRECOMPILED_HEADER_EXCLUDE List of source files that should not use PCH 394 SetupNativeCompilation = $(NamedParamsMacroTemplate) 395 define SetupNativeCompilationBody 396 397 # If type is unspecified, default to LIBRARY 398 ifeq ($$($1_TYPE), ) 399 $1_TYPE := LIBRARY 400 endif 401 402 # If we're doing a static build and producing a library 403 # force it to be a static library and remove the -l libraries 404 ifeq ($(STATIC_BUILD), true) 405 ifeq ($$($1_TYPE), LIBRARY) 406 $1_TYPE := STATIC_LIBRARY 407 endif 408 endif 409 410 ifeq ($$($1_TYPE), EXECUTABLE) 411 $1_PREFIX := 412 ifeq ($$($1_SUFFIX), ) 413 $1_SUFFIX := $(EXE_SUFFIX) 414 endif 415 else 416 $1_PREFIX := $(LIBRARY_PREFIX) 417 ifeq ($$($1_TYPE), LIBRARY) 418 ifeq ($$($1_SUFFIX), ) 419 $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX) 420 endif 421 else ifeq ($$($1_TYPE), STATIC_LIBRARY) 422 ifeq ($$($1_SUFFIX), ) 423 $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX) 424 endif 425 endif 426 endif 427 428 ifneq ($$($1_NAME), $(basename $$($1_NAME))) 429 $$(error NAME must not contain any directory path in $1) 430 endif 431 ifneq ($(findstring $$($1_SUFFIX), $$($1_NAME)), ) 432 $$(error NAME should be specified without suffix: $$($1_SUFFIX) in $1) 433 endif 434 ifneq ($(findstring $$($1_PREFIX), $$($1_NAME)), ) 435 $$(error NAME should be specified without prefix: $$($1_PREFIX) in $1) 436 endif 437 ifeq ($$($1_OUTPUT_DIR), ) 438 $$(error OUTPUT_DIR is missing in $1) 439 endif 440 ifneq ($$($1_MANIFEST), ) 441 ifeq ($$($1_MANIFEST_VERSION), ) 442 $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1) 443 endif 444 endif 445 446 $1_BASENAME := $$($1_PREFIX)$$($1_NAME)$$($1_SUFFIX) 447 $1_TARGET := $$($1_OUTPUT_DIR)/$$($1_BASENAME) 448 $1_NOSUFFIX := $$($1_PREFIX)$$($1_NAME) 449 $1_SAFE_NAME := $$(strip $$(subst /,_, $1)) 450 451 # Setup the toolchain to be used 452 $$(call SetIfEmpty, $1_TOOLCHAIN, TOOLCHAIN_DEFAULT) 453 $$(call SetIfEmpty, $1_CC, $$($$($1_TOOLCHAIN)_CC)) 454 $$(call SetIfEmpty, $1_CXX, $$($$($1_TOOLCHAIN)_CXX)) 455 $$(call SetIfEmpty, $1_LD, $$($$($1_TOOLCHAIN)_LD)) 456 $$(call SetIfEmpty, $1_AR, $$($$($1_TOOLCHAIN)_AR)) 457 $$(call SetIfEmpty, $1_AS, $$($$($1_TOOLCHAIN)_AS)) 458 $$(call SetIfEmpty, $1_MT, $$($$($1_TOOLCHAIN)_MT)) 459 $$(call SetIfEmpty, $1_RC, $$($$($1_TOOLCHAIN)_RC)) 460 $$(call SetIfEmpty, $1_OBJCOPY, $$($$($1_TOOLCHAIN)_OBJCOPY)) 461 $$(call SetIfEmpty, $1_STRIP, $$($$($1_TOOLCHAIN)_STRIP)) 462 $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_CFLAGS)) 463 $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$($$($1_TOOLCHAIN)_SYSROOT_LDFLAGS)) 464 465 # Make sure the dirs exist. 466 $$(call MakeDir, $$($1_OBJECT_DIR) $$($1_OUTPUT_DIR)) 467 $$(foreach d, $$($1_SRC), $$(if $$(wildcard $$d), , \ 468 $$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d))) 469 470 # Find all files in the source trees. Preserve order. 471 $1_SRCS := $$(foreach s, $$($1_SRC), $$(call CacheFind, $$(s))) 472 $1_SRCS := $$(filter $$(NATIVE_SOURCE_EXTENSIONS), $$($1_SRCS)) 473 # Extract the C/C++ files. 474 ifneq ($$($1_EXCLUDE_PATTERNS), ) 475 # We must not match the exclude pattern against the src root(s). 476 $1_SRCS_WITHOUT_ROOTS := $$($1_SRCS) 477 $$(foreach i, $$($1_SRC), $$(eval $1_SRCS_WITHOUT_ROOTS := $$(patsubst \ 478 $$i/%,%, $$($1_SRCS_WITHOUT_ROOTS)))) 479 $1_ALL_EXCLUDE_FILES := $$(call containing, $$($1_EXCLUDE_PATTERNS), \ 480 $$($1_SRCS_WITHOUT_ROOTS)) 481 endif 482 ifneq ($$($1_EXCLUDE_FILES), ) 483 $1_ALL_EXCLUDE_FILES += $$($1_EXCLUDE_FILES) 484 endif 485 ifneq ($$($1_ALL_EXCLUDE_FILES), ) 486 $1_EXCLUDE_FILES_PAT := $$($1_ALL_EXCLUDE_FILES) \ 487 $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$($1_ALL_EXCLUDE_FILES))) 488 $1_EXCLUDE_FILES_PAT := $$(addprefix %, $$($1_EXCLUDE_FILES_PAT)) 489 $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES_PAT), $$($1_SRCS)) 490 endif 491 ifneq ($$($1_INCLUDE_FILES), ) 492 $1_INCLUDE_FILES_PAT := $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$($1_INCLUDE_FILES))) 493 $1_SRCS := $$(filter $$($1_INCLUDE_FILES_PAT), $$($1_SRCS)) 494 endif 495 # There can be only a single bin dir root, no need to foreach over the roots. 496 $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX)) 497 # Now we have a list of all c/c++ files to compile: $$($1_SRCS) 498 # and we have a list of all existing object files: $$($1_BINS) 499 500 # Prepend the source/bin path to the filter expressions. Then do the filtering. 501 ifneq ($$($1_INCLUDES), ) 502 $1_SRC_INCLUDES := $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$(addsuffix /%, $$($1_INCLUDES)))) 503 $1_SRCS := $$(filter $$($1_SRC_INCLUDES), $$($1_SRCS)) 504 endif 505 ifneq ($$($1_EXCLUDES), ) 506 $1_SRC_EXCLUDES := $$(addsuffix /%, $$($1_EXCLUDES)) 507 $1_SRC_EXCLUDES += $$(foreach i, $$($1_SRC), $$(addprefix $$i/, $$(addsuffix /%, $$($1_EXCLUDES)))) 508 $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES), $$($1_SRCS)) 509 endif 510 511 $1_SRCS += $$($1_EXTRA_FILES) 512 513 ifeq ($$($1_SRCS), ) 514 $$(error No sources found for $1 when looking inside the dirs $$($1_SRC)) 515 endif 516 517 # Calculate the expected output from compiling the sources 518 $1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS))) 519 $1_EXPECTED_OBJS := $$(addprefix $$($1_OBJECT_DIR)/, $$($1_EXPECTED_OBJS_FILENAMES)) 520 # Are there too many object files on disk? Perhaps because some source file was removed? 521 $1_SUPERFLOUS_OBJS := $$(sort $$(filter-out $$($1_EXPECTED_OBJS), $$($1_BINS))) 522 # Clean out the superfluous object files. 523 ifneq ($$($1_SUPERFLUOUS_OBJS), ) 524 $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS)) 525 endif 526 # Sort to remove dupliates and provide a reproducable order on the input files to the linker. 527 $1_ALL_OBJS := $$(sort $$($1_EXPECTED_OBJS) $$($1_EXTRA_OBJECT_FILES)) 528 529 # Pickup extra OPENJDK_TARGET_OS_TYPE, OPENJDK_TARGET_OS, and/or OPENJDK_TARGET_OS plus 530 # OPENJDK_TARGET_CPU pair dependent variables for CFLAGS. 531 $1_EXTRA_CFLAGS := $$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS)) \ 532 $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)) 533 ifneq ($(DEBUG_LEVEL), release) 534 # Pickup extra debug dependent variables for CFLAGS 535 $1_EXTRA_CFLAGS += $$($1_CFLAGS_debug) 536 $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug) 537 $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug) 538 $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)_debug) 539 else 540 $1_EXTRA_CFLAGS += $$($1_CFLAGS_release) 541 $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release) 542 $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release) 543 $1_EXTRA_CFLAGS += $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)_release) 544 endif 545 546 # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS. 547 $1_EXTRA_CXXFLAGS := $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)) 548 ifneq ($(DEBUG_LEVEL), release) 549 # Pickup extra debug dependent variables for CXXFLAGS 550 $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_debug) 551 $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug) 552 $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug) 553 else 554 $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_release) 555 $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release) 556 $1_EXTRA_CXXFLAGS += $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release) 557 endif 558 559 # If no C++ flags are explicitly set, default to using the C flags. 560 # After that, we can set additional C++ flags that should not interfere 561 # with the mechanism for copying the C flags by default. 562 ifeq ($$($1_CXXFLAGS), ) 563 $1_CXXFLAGS := $$($1_CFLAGS) 564 endif 565 ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)), ) 566 $1_EXTRA_CXXFLAGS := $$($1_EXTRA_CFLAGS) 567 endif 568 569 ifeq ($(COMPILE_WITH_DEBUG_SYMBOLS), true) 570 $1_EXTRA_CFLAGS += $$(CFLAGS_DEBUG_SYMBOLS) 571 $1_EXTRA_CXXFLAGS += $$(CFLAGS_DEBUG_SYMBOLS) 572 endif 573 574 ifneq ($$($1_REORDER), ) 575 $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER) 576 $1_EXTRA_CXXFLAGS += $$(C_FLAG_REORDER) 577 endif 578 579 # Pass the library name for static JNI library naming 580 ifeq ($$($1_TYPE), STATIC_LIBRARY) 581 $1_EXTRA_CFLAGS += -DLIBRARY_NAME=$$($1_NAME) 582 $1_EXTRA_CXXFLAGS += -DLIBRARY_NAME=$$($1_NAME) 583 endif 584 585 # Pick up disabled warnings, if possible on this platform. 586 ifneq ($(DISABLE_WARNING_PREFIX), ) 587 $1_EXTRA_CFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), \ 588 $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)) \ 589 $$($1_DISABLED_WARNINGS_C_$(TOOLCHAIN_TYPE))) 590 $1_EXTRA_CXXFLAGS += $$(addprefix $(DISABLE_WARNING_PREFIX), \ 591 $$($1_DISABLED_WARNINGS_$(TOOLCHAIN_TYPE)) \ 592 $$($1_DISABLED_WARNINGS_CXX_$(TOOLCHAIN_TYPE))) 593 endif 594 595 # Check if warnings should be considered errors. 596 # Pick first binary and toolchain specific, then binary specific, then general setting. 597 ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), ) 598 ifeq ($$($1_WARNINGS_AS_ERRORS), ) 599 $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$(WARNINGS_AS_ERRORS) 600 else 601 $1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE) := $$($1_WARNINGS_AS_ERRORS) 602 endif 603 endif 604 605 ifeq ($$($1_WARNINGS_AS_ERRORS_$(TOOLCHAIN_TYPE)), true) 606 $1_EXTRA_CFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS) 607 $1_EXTRA_CXXFLAGS += $(CFLAGS_WARNINGS_ARE_ERRORS) 608 endif 609 610 ifeq (NONE, $$($1_OPTIMIZATION)) 611 $1_OPT_CFLAGS := $(C_O_FLAG_NONE) 612 $1_OPT_CXXFLAGS := $(CXX_O_FLAG_NONE) 613 else ifeq (LOW, $$($1_OPTIMIZATION)) 614 $1_OPT_CFLAGS := $(C_O_FLAG_NORM) 615 $1_OPT_CXXFLAGS := $(CXX_O_FLAG_NORM) 616 else ifeq (HIGH, $$($1_OPTIMIZATION)) 617 $1_OPT_CFLAGS := $(C_O_FLAG_HI) 618 $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HI) 619 else ifeq (HIGHEST, $$($1_OPTIMIZATION)) 620 $1_OPT_CFLAGS := $(C_O_FLAG_HIGHEST) 621 $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST) 622 else ifeq (HIGHEST_JVM, $$($1_OPTIMIZATION)) 623 $1_OPT_CFLAGS := $(C_O_FLAG_HIGHEST_JVM) 624 $1_OPT_CXXFLAGS := $(CXX_O_FLAG_HIGHEST_JVM) 625 else ifeq (SIZE, $$($1_OPTIMIZATION)) 626 $1_OPT_CFLAGS := $(C_O_FLAG_SIZE) 627 $1_OPT_CXXFLAGS := $(CXX_O_FLAG_SIZE) 628 else ifneq (, $$($1_OPTIMIZATION)) 629 $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION)) 630 endif 631 632 $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker 633 634 # Track variable changes for all variables that affect the compilation command 635 # lines for all object files in this setup. This includes at least all the 636 # variables used in the call to add_native_source below. 637 $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \ 638 $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_OPT_CFLAGS) $$($1_OPT_CXXFLAGS) \ 639 $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS) 640 $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \ 641 $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps) 642 643 ifneq ($$($1_PRECOMPILED_HEADER), ) 644 ifeq ($(USE_PRECOMPILED_HEADER), true) 645 ifeq ($(TOOLCHAIN_TYPE), microsoft) 646 $1_PCH_FILE := $$($1_OBJECT_DIR)/$1.pch 647 $1_GENERATED_PCH_SRC := $$($1_OBJECT_DIR)/$1_pch.cpp 648 $1_GENERATED_PCH_OBJ := $$($1_OBJECT_DIR)/$1_pch.obj 649 650 $$(eval $$(call SetupCompileNativeFile, $1_$$(notdir $$($1_GENERATED_PCH_SRC)), \ 651 FILE := $$($1_GENERATED_PCH_SRC), \ 652 BASE := $1, \ 653 EXTRA_CXXFLAGS := -Fp$$($1_PCH_FILE) -Yc$$(notdir $$($1_PRECOMPILED_HEADER)), \ 654 DISABLE_THIS_FILE_DEFINE := true, \ 655 )) 656 657 $1_USE_PCH_FLAGS := \ 658 -Fp$$($1_PCH_FILE) -Yu$$(notdir $$($1_PRECOMPILED_HEADER)) 659 660 $$($1_ALL_OBJS): $$($1_GENERATED_PCH_OBJ) 661 662 # Explicitly add the pch obj file first to ease comparing to old 663 # hotspot build. 664 $1_ALL_OBJS := $$($1_GENERATED_PCH_OBJ) $$($1_ALL_OBJS) 665 666 $$($1_GENERATED_PCH_SRC): 667 $(ECHO) "#include \"$$(notdir $$($1_PRECOMPILED_HEADER))\"" > $$@ 668 669 else ifneq ($(findstring $(TOOLCHAIN_TYPE), gcc clang), ) 670 ifeq ($(TOOLCHAIN_TYPE), gcc) 671 $1_PCH_FILE := $$($1_OBJECT_DIR)/precompiled/$$(notdir $$($1_PRECOMPILED_HEADER)).gch 672 $1_USE_PCH_FLAGS := -I$$($1_OBJECT_DIR)/precompiled 673 else ifeq ($(TOOLCHAIN_TYPE), clang) 674 $1_PCH_FILE := $$($1_OBJECT_DIR)/precompiled/$$(notdir $$($1_PRECOMPILED_HEADER)).pch 675 $1_USE_PCH_FLAGS := -include-pch $$($1_PCH_FILE) 676 endif 677 $1_PCH_DEP := $$($1_PCH_FILE).d 678 $1_PCH_DEP_TARGETS := $$($1_PCH_FILE).d.targets 679 680 -include $$($1_PCH_DEP) 681 -include $$($1_PCH_DEP_TARGETS) 682 683 $$($1_PCH_FILE): $$($1_PRECOMPILED_HEADER) $$($1_COMPILE_VARDEPS_FILE) 684 $$(call LogInfo, Generating precompiled header) 685 $$(call MakeDir, $$(@D)) 686 $$(call ExecuteWithLog, $$@, \ 687 $$($1_CC) $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \ 688 $$($1_OPT_CFLAGS) \ 689 -x c++-header -c $(C_FLAG_DEPS) $$($1_PCH_DEP) $$< -o $$@) 690 $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_PCH_DEP) > $$($1_PCH_DEP_TARGETS) 691 692 $$($1_ALL_OBJS): $$($1_PCH_FILE) 693 694 endif 695 endif 696 endif 697 698 # Now call SetupCompileNativeFile for each source file we are going to compile. 699 $$(foreach file, $$($1_SRCS), \ 700 $$(eval $$(call SetupCompileNativeFile, $1_$$(notdir $$(file)),\ 701 FILE := $$(file), \ 702 BASE := $1, \ 703 )) \ 704 ) 705 706 # Setup rule for printing progress info when compiling source files. 707 # This is a rough heuristic and may not always print accurate information. 708 $$($1_BUILD_INFO): $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE) 709 ifeq ($$(wildcard $$($1_TARGET)), ) 710 $(ECHO) 'Creating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET)) from $$(words \ 711 $$(filter-out %.vardeps, $$?)) file(s)' 712 else 713 $(ECHO) $$(strip 'Updating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET))' \ 714 $$(if $$(filter-out %.vardeps, $$?), \ 715 'due to $$(words $$(filter-out %.vardeps, $$?)) file(s)', \ 716 $$(if $$(filter %.vardeps, $$?), 'due to makefile changes'))) 717 endif 718 $(TOUCH) $$@ 719 720 # On windows we need to create a resource file 721 ifeq ($(OPENJDK_TARGET_OS), windows) 722 ifneq ($$($1_VERSIONINFO_RESOURCE), ) 723 $1_RES := $$($1_OBJECT_DIR)/$$($1_BASENAME).res 724 $1_RES_DEP := $$($1_RES).d 725 $1_RES_DEP_TARGETS := $$($1_RES).d.targets 726 -include $$($1_RES_DEP) 727 -include $$($1_RES_DEP_TARGETS) 728 729 $1_RES_VARDEPS := $$($1_RC) $$($1_RC_FLAGS) 730 $1_RES_VARDEPS_FILE := $$(call DependOnVariable, $1_RES_VARDEPS, \ 731 $$($1_RES).vardeps) 732 733 $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE) 734 $$(call LogInfo, Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$($1_BASENAME))) 735 $$(call MakeDir, $$(@D) $$($1_OBJECT_DIR)) 736 $$(call ExecuteWithLog, $$@, \ 737 $$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \ 738 $$($1_VERSIONINFO_RESOURCE) 2>&1 ) 739 # Windows RC compiler does not support -showIncludes, so we mis-use CL 740 # for this. Filter out RC specific arguments that are unknown to CL. 741 # For some unknown reason, in this case CL actually outputs the show 742 # includes to stderr so need to redirect it to hide the output from the 743 # main log. 744 $$(call ExecuteWithLog, $$($1_RES_DEP).obj, \ 745 $$($1_CC) $$(filter-out -l%, $$($1_RC_FLAGS)) \ 746 $$($1_SYSROOT_CFLAGS) -showIncludes -nologo -TC \ 747 $(CC_OUT_OPTION)$$($1_RES_DEP).obj -P -Fi$$($1_RES_DEP).pp \ 748 $$($1_VERSIONINFO_RESOURCE)) 2>&1 \ 749 | $(TR) -d '\r' | $(GREP) -v -e "^Note: including file:" \ 750 -e "^$$(notdir $$($1_VERSIONINFO_RESOURCE))$$$$" || test "$$$$?" = "1" ; \ 751 $(ECHO) $$($1_RES): \\ > $$($1_RES_DEP) ; \ 752 $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).obj.log >> $$($1_RES_DEP) ; \ 753 $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_RES_DEP) > $$($1_RES_DEP_TARGETS) 754 endif 755 endif 756 757 ifneq ($(DISABLE_MAPFILES), true) 758 $1_REAL_MAPFILE := $$($1_MAPFILE) 759 ifneq ($(OPENJDK_TARGET_OS), windows) 760 ifneq ($$($1_REORDER), ) 761 $1_REAL_MAPFILE := $$($1_OBJECT_DIR)/mapfile 762 763 $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER) 764 $$(call MakeDir, $$(@D)) 765 $$(CP) $$($1_MAPFILE) $$@.tmp 766 $$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp 767 $$(MV) $$@.tmp $$@ 768 endif 769 endif 770 endif 771 772 # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables 773 # for LDFLAGS and LIBS 774 $1_EXTRA_LDFLAGS := $$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS)) 775 $1_EXTRA_LIBS := $$($1_LIBS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LIBS_$(OPENJDK_TARGET_OS)) 776 ifneq ($$($1_REAL_MAPFILE), ) 777 $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE)) 778 endif 779 780 # Need to make sure TARGET is first on list 781 $1 := $$($1_TARGET) 782 783 ifneq ($$($1_COPY_DEBUG_SYMBOLS), false) 784 $1_COPY_DEBUG_SYMBOLS := $(COPY_DEBUG_SYMBOLS) 785 endif 786 787 ifneq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), false) 788 $1_ZIP_EXTERNAL_DEBUG_SYMBOLS := $(ZIP_EXTERNAL_DEBUG_SYMBOLS) 789 endif 790 791 ifeq ($$($1_COPY_DEBUG_SYMBOLS), true) 792 ifneq ($$($1_DEBUG_SYMBOLS), false) 793 # Only copy debug symbols for dynamic libraries and programs. 794 ifneq ($$($1_TYPE), STATIC_LIBRARY) 795 # Generate debuginfo files. 796 ifeq ($(OPENJDK_TARGET_OS), windows) 797 $1_EXTRA_LDFLAGS += -debug "-pdb:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb" \ 798 "-map:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map" 799 $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb \ 800 $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map 801 802 else ifneq ($(findstring $(OPENJDK_TARGET_OS), linux solaris), ) 803 $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).debuginfo 804 # Setup the command line creating debuginfo files, to be run after linking. 805 # It cannot be run separately since it updates the original target file 806 $1_CREATE_DEBUGINFO_CMDS := \ 807 $$($1_OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \ 808 $(CD) $$($1_OUTPUT_DIR) && \ 809 $$($1_OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET) 810 811 else ifeq ($(OPENJDK_TARGET_OS), macosx) 812 $1_DEBUGINFO_FILES := \ 813 $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist \ 814 $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) 815 $1_CREATE_DEBUGINFO_CMDS := \ 816 $(DSYMUTIL) --out $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM $$($1_TARGET) 817 endif # OPENJDK_TARGET_OS 818 819 # Since the link rule creates more than one file that we want to track, 820 # we have to use some tricks to get make to cooperate. To properly 821 # trigger downstream dependants of $$($1_DEBUGINFO_FILES), we must have 822 # a recipe in the rule below. To avoid rerunning the recipe every time 823 # have it touch the target. If a debuginfo file is deleted by something 824 # external, explicitly delete the TARGET to trigger a rebuild of both. 825 ifneq ($$(wildcard $$($1_DEBUGINFO_FILES)), $$($1_DEBUGINFO_FILES)) 826 $$(call LogDebug, Deleting $$($1_BASENAME) because debuginfo files are missing) 827 $$(shell $(RM) $$($1_TARGET)) 828 endif 829 $$($1_DEBUGINFO_FILES): $$($1_TARGET) 830 $$(if $$(CORRECT_FUNCTION_IN_RECIPE_EVALUATION), \ 831 $$(if $$(wildcard $$@), , $$(error $$@ was not created for $$<)) \ 832 ) 833 $(TOUCH) $$@ 834 835 $1 += $$($1_DEBUGINFO_FILES) 836 837 ifeq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), true) 838 $1_DEBUGINFO_ZIP := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).diz 839 $1 += $$($1_DEBUGINFO_ZIP) 840 841 # The dependency on TARGET is needed for debuginfo files 842 # to be rebuilt properly. 843 $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET) 844 $(CD) $$($1_OUTPUT_DIR) && \ 845 $(ZIPEXE) -q -r $$@ $$(subst $$($1_OUTPUT_DIR)/,, $$($1_DEBUGINFO_FILES)) 846 847 endif 848 endif # !STATIC_LIBRARY 849 endif # $1_DEBUG_SYMBOLS != false 850 endif # COPY_DEBUG_SYMBOLS 851 852 # Unless specifically set, stripping should only happen if symbols are also 853 # being copied. 854 $$(call SetIfEmpty, $1_STRIP_SYMBOLS, $$($1_COPY_DEBUG_SYMBOLS)) 855 856 ifneq ($$($1_STRIP_SYMBOLS), false) 857 ifneq ($$($1_STRIP), ) 858 # Default to using the global STRIPFLAGS. Allow for overriding with an empty value 859 $1_STRIPFLAGS ?= $(STRIPFLAGS) 860 $1_STRIP_CMD := $$($1_STRIP) $$($1_STRIPFLAGS) $$($1_TARGET) 861 endif 862 endif 863 864 ifeq ($$($1_TYPE), STATIC_LIBRARY) 865 $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LIBS) \ 866 $$($1_EXTRA_LIBS) 867 $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \ 868 $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps) 869 870 # Generating a static library, ie object file archive. 871 ifeq ($(STATIC_BUILD), true) 872 ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true) 873 STATIC_MAPFILE_DEP := $$($1_MAPFILE) 874 endif 875 endif 876 877 $$($1_TARGET): $$($1_ALL_OBJS) $$($1_RES) $$($1_VARDEPS_FILE) $$(STATIC_MAPFILE_DEP) 878 $$(call LogInfo, Building static library $$($1_BASENAME)) 879 $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \ 880 $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_ALL_OBJS) \ 881 $$($1_RES)) 882 ifeq ($(STATIC_BUILD), true) 883 ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true) 884 $(CP) $$($1_MAPFILE) $$(@D)/$$(basename $$(@F)).symbols 885 else 886 $(GetSymbols) 887 endif 888 endif 889 else 890 # A shared dynamic library or an executable binary has been specified 891 ifeq ($$($1_TYPE), LIBRARY) 892 # Generating a dynamic library. 893 $1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME)) 894 895 # Create loadmap on AIX. Helps in diagnosing some problems. 896 ifneq ($(COMPILER_BINDCMD_FILE_FLAG), ) 897 $1_EXTRA_LDFLAGS += $(COMPILER_BINDCMD_FILE_FLAG)$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).loadmap 898 endif 899 endif 900 901 ifeq ($(OPENJDK_TARGET_OS), windows) 902 ifeq ($$($1_EMBED_MANIFEST), true) 903 $1_EXTRA_LDFLAGS += -manifest:embed 904 endif 905 906 $1_IMPORT_LIBRARY := $$($1_OBJECT_DIR)/$$($1_NAME).lib 907 $1_EXTRA_LDFLAGS += "-implib:$$($1_IMPORT_LIBRARY)" 908 # To properly trigger downstream dependants of the import library, just as 909 # for debug files, we must have a recipe in the rule. To avoid rerunning 910 # the recipe every time have it touch the target. If an import library 911 # file is deleted by something external, explicitly delete the target to 912 # trigger a rebuild of both. 913 ifneq ($$(wildcard $$($1_IMPORT_LIBRARY)), $$($1_IMPORT_LIBRARY)) 914 $$(call LogDebug, Deleting $$($1_BASENAME) because import library is missing) 915 $$(shell $(RM) $$($1_TARGET)) 916 endif 917 $$($1_IMPORT_LIBRARY): $$($1_TARGET) 918 $$(if $$(CORRECT_FUNCTION_IN_RECIPE_EVALUATION), \ 919 $$(if $$(wildcard $$@), , $$(error $$@ was not created for $$<)) \ 920 ) 921 $(TOUCH) $$@ 922 endif 923 924 $1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \ 925 $$(GLOBAL_LIBS) $$($1_LIBS) $$($1_EXTRA_LIBS) $$($1_MT) \ 926 $$($1_CODESIGN) $$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION) \ 927 $$($1_STRIP_CMD) 928 $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \ 929 $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps) 930 931 $1_LD_OBJ_ARG := $$($1_ALL_OBJS) 932 933 # If there are many object files, use an @-file... 934 ifneq ($$(word 17, $$($1_ALL_OBJS)), ) 935 $1_OBJ_FILE_LIST := $$($1_OBJECT_DIR)/_$1_objectfilenames.txt 936 ifneq ($(COMPILER_COMMAND_FILE_FLAG), ) 937 $1_LD_OBJ_ARG := $(COMPILER_COMMAND_FILE_FLAG)$$($1_OBJ_FILE_LIST) 938 else 939 # ...except for toolchains which don't support them. 940 $1_LD_OBJ_ARG := `cat $$($1_OBJ_FILE_LIST)` 941 endif 942 endif 943 944 # Unfortunately the @-file trick does not work reliably when using clang. 945 # Clang does not propagate the @-file parameter to the ld sub process, but 946 # instead puts the full content on the command line. At least the llvm ld 947 # does not even support an @-file. 948 # 949 # When linking a large amount of object files, we risk hitting the limit 950 # of the command line length even on posix systems if the path length of 951 # the output dir is very long due to our use of absolute paths. To 952 # mitigate this, use paths relative to the output dir when linking over 953 # 500 files with clang and the output dir path is deep. 954 ifneq ($$(word 500, $$($1_ALL_OBJS)), ) 955 ifeq ($$(TOOLCHAIN_TYPE), clang) 956 # There is no strlen function in make, but checking path depth is a 957 # reasonable approximation. 958 ifneq ($$(word 10, $$(subst /, ,$$(OUTPUTDIR))), ) 959 $1_LINK_OBJS_RELATIVE := true 960 $1_ALL_OBJS_RELATIVE := $$(patsubst $$(OUTPUTDIR)/%, %, $$($1_ALL_OBJS)) 961 endif 962 endif 963 endif 964 965 $$($1_TARGET): $$($1_ALL_OBJS) $$($1_RES) $$($1_MANIFEST) \ 966 $$($1_REAL_MAPFILE) $$($1_VARDEPS_FILE) 967 ifneq ($$($1_OBJ_FILE_LIST), ) 968 ifeq ($$($1_LINK_OBJS_RELATIVE), true) 969 $$(eval $$(call ListPathsSafely, $1_ALL_OBJS_RELATIVE, $$($1_OBJ_FILE_LIST))) 970 else 971 $$(eval $$(call ListPathsSafely, $1_ALL_OBJS, $$($1_OBJ_FILE_LIST))) 972 endif 973 endif 974 # Keep as much as possible on one execution line for best performance 975 # on Windows 976 $$(call LogInfo, Linking $$($1_BASENAME)) 977 ifeq ($(OPENJDK_TARGET_OS), windows) 978 $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \ 979 $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \ 980 $(LD_OUT_OPTION)$$($1_TARGET) $$($1_LD_OBJ_ARG) $$($1_RES) $$(GLOBAL_LIBS) \ 981 $$($1_LIBS) $$($1_EXTRA_LIBS)) \ 982 | $(GREP) -v "^ Creating library .*\.lib and object .*\.exp" || \ 983 test "$$$$?" = "1" ; \ 984 $$($1_CREATE_DEBUGINFO_CMDS) 985 $$($1_STRIP_CMD) 986 else 987 $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \ 988 $$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \ 989 $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \ 990 $(LD_OUT_OPTION)$$($1_TARGET) $$($1_LD_OBJ_ARG) $$($1_RES) $$(GLOBAL_LIBS) \ 991 $$($1_LIBS) $$($1_EXTRA_LIBS)) ; \ 992 $$($1_CREATE_DEBUGINFO_CMDS) 993 $$($1_STRIP_CMD) 994 endif 995 ifeq ($(OPENJDK_TARGET_OS), windows) 996 ifneq ($$($1_MANIFEST), ) 997 $$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_NAME).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1 998 endif 999 endif 1000 # This only works if the openjdk_codesign identity is present on the system. Let 1001 # silently fail otherwise. 1002 ifneq ($(CODESIGN), ) 1003 ifneq ($$($1_CODESIGN), ) 1004 $(CODESIGN) -s openjdk_codesign $$@ 1005 endif 1006 endif 1007 endif 1008 endef 1009 1010 endif # _NATIVE_COMPILATION_GMK