From 093b8f8757aae373a318419b18348377c116b5c3 Mon Sep 17 00:00:00 2001 From: Christoph Langer Date: Fri, 7 Feb 2020 08:38:40 +0100 Subject: [PATCH 1/4] Backport 907f9ee7083df384a7433cb933522680e70dcb1f --- jdk/test/tools/launcher/TestHelper.java | 49 ++++++++- jdk/test/tools/launcher/VersionCheck.java | 35 +------ make/common/NativeCompilation.gmk | 115 ++++++++++++++++++++++ 3 files changed, 163 insertions(+), 36 deletions(-) diff --git a/jdk/test/tools/launcher/TestHelper.java b/jdk/test/tools/launcher/TestHelper.java index 40737320d52..06dab9d322f 100644 --- a/jdk/test/tools/launcher/TestHelper.java +++ b/jdk/test/tools/launcher/TestHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -103,10 +103,12 @@ public class TestHelper { // make a note of the golden default locale static final Locale DefaultLocale = Locale.getDefault(); - static final String JAVA_FILE_EXT = ".java"; - static final String CLASS_FILE_EXT = ".class"; - static final String JAR_FILE_EXT = ".jar"; - static final String EXE_FILE_EXT = ".exe"; + static final String JAVA_FILE_EXT = ".java"; + static final String CLASS_FILE_EXT = ".class"; + static final String JAR_FILE_EXT = ".jar"; + static final String EXE_FILE_EXT = ".exe"; + static final String MAC_DSYM_EXT = ".dsym"; + static final String NIX_DBGINFO_EXT = ".debuginfo"; static final String JLDEBUG_KEY = "_JAVA_LAUNCHER_DEBUG"; static final String EXPECTED_MARKER = "TRACER_MARKER:About to EXEC"; static final String TEST_PREFIX = "###TestError###: "; @@ -506,6 +508,43 @@ static boolean isEnglishLocale() { return Locale.getDefault().getLanguage().equals("en"); } + static class ToolFilter implements FileFilter { + final List exclude = new ArrayList<>(); + protected ToolFilter(String... exclude) { + for (String x : exclude) { + String str = x + ((isWindows) ? EXE_FILE_EXT : ""); + this.exclude.add(str.toLowerCase()); + } + } + + @Override + public boolean accept(File pathname) { + if (!pathname.isFile() || !pathname.canExecute()) { + return false; + } + String name = pathname.getName().toLowerCase(); + if (isWindows) { + if (!name.endsWith(EXE_FILE_EXT)) { + return false; + } + } else if (isMacOSX) { + if (name.endsWith(MAC_DSYM_EXT)) { + return false; + } + } else { + if (name.endsWith(NIX_DBGINFO_EXT)) { + return false; + } + } + for (String x : exclude) { + if (name.endsWith(x)) { + return false; + } + } + return true; + } + } + /* * A class to encapsulate the test results and stuff, with some ease * of use methods to check the test results. diff --git a/jdk/test/tools/launcher/VersionCheck.java b/jdk/test/tools/launcher/VersionCheck.java index c175e8dede7..8d8c1d4f0ad 100644 --- a/jdk/test/tools/launcher/VersionCheck.java +++ b/jdk/test/tools/launcher/VersionCheck.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,11 +31,12 @@ */ import java.io.File; -import java.io.FileFilter; -import java.util.Map; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; public class VersionCheck extends TestHelper { @@ -227,32 +228,4 @@ public static void main(String[] args) { throw new AssertionError("Some tests failed"); } } - - static class ToolFilter implements FileFilter { - final Iterable exclude ; - protected ToolFilter(String... exclude) { - List tlist = new ArrayList<>(); - this.exclude = tlist; - for (String x : exclude) { - String str = x + ((isWindows) ? EXE_FILE_EXT : ""); - tlist.add(str.toLowerCase()); - } - } - @Override - public boolean accept(File pathname) { - if (!pathname.isFile() || !pathname.canExecute()) { - return false; - } - String name = pathname.getName().toLowerCase(); - if (isWindows && !name.endsWith(EXE_FILE_EXT)) { - return false; - } - for (String x : exclude) { - if (name.endsWith(x)) { - return false; - } - } - return true; - } - } } diff --git a/make/common/NativeCompilation.gmk b/make/common/NativeCompilation.gmk index 009139fb7bf..3ead66b14c3 100644 --- a/make/common/NativeCompilation.gmk +++ b/make/common/NativeCompilation.gmk @@ -1,5 +1,9 @@ # +<<<<<<< HEAD # Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. +======= +# Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. +>>>>>>> 907f9ee7083 (8237192: Generate stripped/public pdbs on Windows for jdk images) # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -437,6 +441,7 @@ define SetupNativeCompilation # Need to make sure TARGET is first on list $1 := $$($1_TARGET) +<<<<<<< HEAD ifeq ($$($1_STATIC_LIBRARY),) ifneq ($$($1_DEBUG_SYMBOLS),) ifeq ($(ENABLE_DEBUG_SYMBOLS), true) @@ -451,6 +456,116 @@ define SetupNativeCompilation $(CP) $$< $$@ $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).diz : $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz $(CP) $$< $$@ +======= + + ifneq ($$($1_COPY_DEBUG_SYMBOLS), false) + $1_COPY_DEBUG_SYMBOLS := $(COPY_DEBUG_SYMBOLS) + endif + + ifneq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), false) + $1_ZIP_EXTERNAL_DEBUG_SYMBOLS := $(ZIP_EXTERNAL_DEBUG_SYMBOLS) + endif + + ifeq ($$($1_COPY_DEBUG_SYMBOLS), true) + ifneq ($$($1_DEBUG_SYMBOLS), false) + # Only copy debug symbols for dynamic libraries and programs. + ifneq ($$($1_TYPE), STATIC_LIBRARY) + # Generate debuginfo files. + ifeq ($(OPENJDK_TARGET_OS), windows) + $1_EXTRA_LDFLAGS += -debug "-pdb:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb" \ + "-map:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map" + ifeq ($(SHIP_DEBUG_SYMBOLS), public) + $1_EXTRA_LDFLAGS += "-pdbstripped:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).stripped.pdb" + endif + $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb \ + $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map + + else ifneq ($(findstring $(OPENJDK_TARGET_OS), linux solaris), ) + $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).debuginfo + # Setup the command line creating debuginfo files, to be run after linking. + # It cannot be run separately since it updates the original target file + $1_CREATE_DEBUGINFO_CMDS := \ + $$($1_OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \ + $(CD) $$($1_OUTPUT_DIR) && \ + $$($1_OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET) + + else ifeq ($(OPENJDK_TARGET_OS), macosx) + $1_DEBUGINFO_FILES := \ + $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist \ + $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) + $1_CREATE_DEBUGINFO_CMDS := \ + $(DSYMUTIL) --out $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM $$($1_TARGET) + endif # OPENJDK_TARGET_OS + + # Since the link rule creates more than one file that we want to track, + # we have to use some tricks to get make to cooperate. To properly + # trigger downstream dependants of $$($1_DEBUGINFO_FILES), we must have + # a recipe in the rule below. To avoid rerunning the recipe every time + # have it touch the target. If a debuginfo file is deleted by something + # external, explicitly delete the TARGET to trigger a rebuild of both. + ifneq ($$(wildcard $$($1_DEBUGINFO_FILES)), $$($1_DEBUGINFO_FILES)) + $$(call LogDebug, Deleting $$($1_BASENAME) because debuginfo files are missing) + $$(shell $(RM) $$($1_TARGET)) + endif + $$($1_DEBUGINFO_FILES): $$($1_TARGET) + $$(if $$(CORRECT_FUNCTION_IN_RECIPE_EVALUATION), \ + $$(if $$(wildcard $$@), , $$(error $$@ was not created for $$<)) \ + ) + $(TOUCH) $$@ + + $1 += $$($1_DEBUGINFO_FILES) + + ifeq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), true) + $1_DEBUGINFO_ZIP := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).diz + $1 += $$($1_DEBUGINFO_ZIP) + + # The dependency on TARGET is needed for debuginfo files + # to be rebuilt properly. + $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET) + $(CD) $$($1_OUTPUT_DIR) && \ + $(ZIPEXE) -q -r $$@ $$(subst $$($1_OUTPUT_DIR)/,, $$($1_DEBUGINFO_FILES)) + + endif + endif # !STATIC_LIBRARY + endif # $1_DEBUG_SYMBOLS != false + endif # COPY_DEBUG_SYMBOLS + + # Unless specifically set, stripping should only happen if symbols are also + # being copied. + $$(call SetIfEmpty, $1_STRIP_SYMBOLS, $$($1_COPY_DEBUG_SYMBOLS)) + + ifneq ($$($1_STRIP_SYMBOLS), false) + ifneq ($$($1_STRIP), ) + # Default to using the global STRIPFLAGS. Allow for overriding with an empty value + $1_STRIPFLAGS ?= $(STRIPFLAGS) + $1_STRIP_CMD := $$($1_STRIP) $$($1_STRIPFLAGS) $$($1_TARGET) + endif + endif + + ifeq ($$($1_TYPE), STATIC_LIBRARY) + $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LIBS) \ + $$($1_EXTRA_LIBS) + $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \ + $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps) + + # Generating a static library, ie object file archive. + ifeq ($(STATIC_BUILD), true) + ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true) + STATIC_MAPFILE_DEP := $$($1_MAPFILE) + endif + endif + + $1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_RES) $$($1_VARDEPS_FILE) $$(STATIC_MAPFILE_DEP) + + $$($1_TARGET): $$($1_TARGET_DEPS) + $$(call LogInfo, Building static library $$($1_BASENAME)) + $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \ + $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_ALL_OBJS) \ + $$($1_RES)) + ifeq ($(STATIC_BUILD), true) + ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true) + $(CP) $$($1_MAPFILE) $$(@D)/$$(basename $$(@F)).symbols +>>>>>>> 907f9ee7083 (8237192: Generate stripped/public pdbs on Windows for jdk images) else # The dependency on TARGET is needed on windows for debuginfo files # to be rebuilt properly. From cbe6bd05f9e8bfcef79273ac68391525f1f60087 Mon Sep 17 00:00:00 2001 From: sendaoYan Date: Sat, 1 Feb 2025 13:43:55 +0800 Subject: [PATCH 2/4] fix conflict make/common/NativeCompilation.gmk --- make/common/NativeCompilation.gmk | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/make/common/NativeCompilation.gmk b/make/common/NativeCompilation.gmk index 3ead66b14c3..e37e64d20db 100644 --- a/make/common/NativeCompilation.gmk +++ b/make/common/NativeCompilation.gmk @@ -1,9 +1,5 @@ # -<<<<<<< HEAD -# Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. -======= # Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. ->>>>>>> 907f9ee7083 (8237192: Generate stripped/public pdbs on Windows for jdk images) # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -441,22 +437,6 @@ define SetupNativeCompilation # Need to make sure TARGET is first on list $1 := $$($1_TARGET) -<<<<<<< HEAD - ifeq ($$($1_STATIC_LIBRARY),) - ifneq ($$($1_DEBUG_SYMBOLS),) - ifeq ($(ENABLE_DEBUG_SYMBOLS), true) - ifneq ($$($1_OUTPUT_DIR),$$($1_OBJECT_DIR)) - ifeq ($(OPENJDK_TARGET_OS), macosx) - # MacOS debug symbols are in %.dSYM directories - $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist : $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist - mkdir -p $$(@D) - $(CP) $$< $$@ - $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) : $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) - mkdir -p $$(@D) - $(CP) $$< $$@ - $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).diz : $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz - $(CP) $$< $$@ -======= ifneq ($$($1_COPY_DEBUG_SYMBOLS), false) $1_COPY_DEBUG_SYMBOLS := $(COPY_DEBUG_SYMBOLS) @@ -565,7 +545,6 @@ define SetupNativeCompilation ifeq ($(STATIC_BUILD), true) ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true) $(CP) $$($1_MAPFILE) $$(@D)/$$(basename $$(@F)).symbols ->>>>>>> 907f9ee7083 (8237192: Generate stripped/public pdbs on Windows for jdk images) else # The dependency on TARGET is needed on windows for debuginfo files # to be rebuilt properly. From 9400996cd90ac572e586fca19caa1dab87f153c5 Mon Sep 17 00:00:00 2001 From: sendaoYan Date: Sat, 1 Feb 2025 13:51:53 +0800 Subject: [PATCH 3/4] Drop change of make/common/NativeCompilation.gmk --- make/common/NativeCompilation.gmk | 948 +++++++++++------------------- 1 file changed, 333 insertions(+), 615 deletions(-) diff --git a/make/common/NativeCompilation.gmk b/make/common/NativeCompilation.gmk index e37e64d20db..66d07df3b78 100644 --- a/make/common/NativeCompilation.gmk +++ b/make/common/NativeCompilation.gmk @@ -1,5 +1,5 @@ # -# Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -23,672 +23,390 @@ # questions. # -# When you read this source. Remember that $(sort ...) has the side effect -# of removing duplicates. It is actually this side effect that is -# desired whenever sort is used below! +################################################################################ +# This is the top-level entry point for our native compilation and linking. +# It contains the SetupNativeCompilation macro, but is supported by helper +# macros in the make/common/native directory. +################################################################################ -ifeq (,$(_MAKEBASE_GMK)) +ifndef _NATIVE_COMPILATION_GMK +_NATIVE_COMPILATION_GMK := 1 + +ifeq ($(_MAKEBASE_GMK), ) $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk) endif -ifneq ($(TOOLCHAIN_TYPE), microsoft) - COMPILING_MSG=echo $(LOG_INFO) "Compiling $(notdir $1) (for $(notdir $2))" - LINKING_MSG=echo $(LOG_INFO) "Linking $1" - LINKING_EXE_MSG=echo $(LOG_INFO) "Linking executable $1" - ARCHIVING_MSG=echo $(LOG_INFO) "Archiving $1" -else - COMPILING_MSG= - LINKING_MSG= - LINKING_EXE_MSG= - ARCHIVING_MSG= -endif +include MakeIO.gmk +include native/CompileFile.gmk +include native/DebugSymbols.gmk +include native/Flags.gmk +include native/Link.gmk +include native/LinkMicrosoft.gmk +include native/Paths.gmk + +################################################################################ +# Setup make rules for creating a native binary (a shared library or an +# executable). +# +# Parameter 1 is the name of the rule. This name is used as variable prefix, +# and the targets generated are listed in a variable by that name. +# +# Remaining parameters are named arguments. These include: +# NAME The base name for the resulting binary, excluding decorations (like *.exe) +# TYPE Type of binary (EXECUTABLE, LIBRARY or STATIC_LIBRARY). Default is LIBRARY. +# SUFFIX Override the default suffix for the output file +# TARGET_TYPE The type to target, BUILD or TARGET. Defaults to TARGET. +# LINK_TYPE The language to use for the linker, C or C++. Defaults to C. +# SRC one or more directory roots to scan for C/C++ files. +# CFLAGS the compiler flags to be used, used both for C and C++. +# CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS. +# LDFLAGS the linker flags to be used, used both for C and C++. +# LDFLAGS_ the linker flags to be used for the specified toolchain, +# used both for C and C++. +# LDFLAGS_ the linker flags to be used for the specified target OS, +# used both for C and C++. +# LDFLAGS__ the linker flags to be used for the specified +# toolchain and target OS, used both for C and C++. +# LIBS the libraries to link to +# LIBS_ the libraries to link to for the specified target OS, +# used both for C and C++. +# LIBS_ the libraries to link to for the specified toolchain, +# used both for C and C++. +# LIBS__ the libraries to link to for the specified target +# OS and toolchain, used both for C and C++. +# OBJECT_DIR the directory where we store the object files +# OUTPUT_DIR the directory where the resulting binary is put +# SYMBOLS_DIR the directory where the debug symbols are put, defaults to OUTPUT_DIR +# INCLUDES only pick source from these directories +# EXCLUDES do not pick source from these directories +# INCLUDE_FILES only compile exactly these files! +# EXCLUDE_FILES with these names +# EXCLUDE_PATTERN exclude files matching any of these substrings +# EXTRA_FILES List of extra files not in any of the SRC dirs +# EXTRA_OBJECT_FILES List of extra object files to include when linking +# EXTRA_DEPS List of extra dependencies to be added to each compiled file +# EXTRA_LINK_DEPS List of extra dependencies to be added to the link stage +# VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run +# RCFLAGS flags for RC. +# EMBED_MANIFEST if true, embed manifest on Windows. +# CC the C compiler to use +# CXX the C++ compiler to use +# LD the Linker to use +# AR the static linker to use +# LIB the Windows lib tool to use for creating static libraries +# AS the assembler to use +# MT the Windows MT tool to use +# RC the Windows RC tool to use +# OBJCOPY the objcopy tool for debug symbol handling +# STRIP the tool to use for stripping debug symbols +# SYSROOT_CFLAGS the compiler flags for using the specific sysroot +# SYSROOT_LDFLAGS the linker flags for using the specific sysroot +# OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST, HIGHEST_JVM, SIZE +# DISABLED_WARNINGS_ Disable the given warnings for the specified toolchain +# DISABLED_WARNINGS__ Disable the given warnings for the specified +# toolchain and target OS +# DISABLED_WARNINGS_C_ Disable the given warnings for the specified toolchain +# when compiling C code +# DISABLED_WARNINGS_C__ Disable the given warnings for the specified +# toolchain and target OS when compiling C code +# DISABLED_WARNINGS_CXX_ Disable the given warnings for the specified +# toolchain when compiling C++ code +# DISABLED_WARNINGS_CXX__ Disable the given warnings for the specified +# toolchain and target OS when compiling C++ code +# DISABLED_WARNINGS__ Disable the given warnings for the specified +# toolchain when compiling the file specified by filename +# DISABLED_WARNINGS___ Disable the given warnings for the specified +# toolchain and target OS when compiling the file specified by filename +# STRIP_SYMBOLS Set to false to override global strip policy and always leave +# symbols in the binary, if the toolchain allows for it +# DEBUG_SYMBOLS Set to false to disable generation of debug symbols +# COPY_DEBUG_SYMBOLS Set to false to override global setting of debug symbol copying +# ZIP_EXTERNAL_DEBUG_SYMBOLS Set to false to override global setting of debug symbol +# zipping +# STRIPFLAGS Optionally change the flags given to the strip command +# PRECOMPILED_HEADER Header file to use as precompiled header +# PRECOMPILED_HEADER_EXCLUDE List of source files that should not use PCH +# BUILD_INFO_LOG_MACRO Overrides log level of the build info log message, default LogWarn +# STATIC_LIB_EXCLUDE_OBJS exclude objects that matches from static library +# +# After being called, some variables are exported from this macro, all prefixed +# with parameter 1 followed by a '_': +# TARGET The library or executable created by the macro +# TARGET_DEPS All prerequisites for the target calculated by the macro +# ALL_OBJS All object files +# IMPORT_LIBRARY The import library created for a shared library on Windows +# +SetupNativeCompilation = $(NamedParamsMacroTemplate) +define SetupNativeCompilationBody + # When reading this code, note that macros named Setup are just setting + # variables, and macros called Create are setting up rules to create + # files. Macros starting with any other verb are more complicated, and can do + # all of the above, and also call directly to the shell. -ifeq ($(OPENJDK_BUILD_OS_ENV), windows.cygwin) - UNIX_PATH_PREFIX := /cygdrive -else ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys) - UNIX_PATH_PREFIX := -endif + ### + ### Prepare for compilation and linking + ### -define add_native_source - # param 1 = BUILD_MYPACKAGE - # parma 2 = the source file name (..../alfa.c or .../beta.cpp) - # param 3 = the bin dir that stores all .o (.obj) and .d files. - # param 4 = the c flags to the compiler - # param 5 = the c compiler - # param 6 = the c++ flags to the compiler - # param 7 = the c++ compiler - # param 8 = the flags to the assembler - - ifneq (,$$(filter %.c,$2)) - # Compile as a C file - $1_$2_FLAGS=$4 $$($1_$(notdir $2)_CFLAGS) -DTHIS_FILE='"$$( $$($1_$2_DEP) - else - $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP) $(CC_OUT_OPTION)$$($1_$2_OBJ) $2 - endif - endif - # The Visual Studio compiler lacks a feature for generating make dependencies, but by - # setting -showIncludes, all included files are printed. These are filtered out and - # parsed into make dependences. - ifeq ($(TOOLCHAIN_TYPE), microsoft) - ($$($1_$2_COMP) $$($1_$2_FLAGS) -showIncludes $$($1_$2_DEBUG_OUT_FLAGS) \ - $(CC_OUT_OPTION)$$($1_$2_OBJ) $2 ; echo $$$$? > $$($1_$2_DEP).exitvalue) \ - | $(TEE) $$($1_$2_DEP).raw | $(GREP) -v "^Note: including file:" \ - && exit `cat $$($1_$2_DEP).exitvalue` - $(RM) $$($1_$2_DEP).exitvalue - ($(ECHO) $$@: \\ \ - && $(SED) -e '/^Note: including file:/!d' \ - -e 's|Note: including file: *||' \ - -e 's|\r||g' \ - -e 's|\\|/|g' \ - -e 's|^\([a-zA-Z]\):|$(UNIX_PATH_PREFIX)/\1|g' \ - -e '/$(subst /,\/,$(TOPDIR))/!d' \ - -e 's|$$$$| \\|g' \ - $$($1_$2_DEP).raw) > $$($1_$2_DEP) - endif - endif -endef + # Setup variables for the rest of this macro to work with + $$(eval $$(call SetupBasicVariables,$1)) -define SetupNativeCompilation - # param 1 is for example BUILD_MYPACKAGE - # param 2,3,4,5,6,7,8 are named args. - # SRC one or more directory roots to scan for C/C++ files. - # LANG C or C++ - # CFLAGS the compiler flags to be used, used both for C and C++. - # CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS. - # LDFLAGS the linker flags to be used, used both for C and C++. - # LDFLAGS_SUFFIX the linker flags to be added last on the commandline - # typically the libraries linked to. - # ARFLAGS the archiver flags to be used - # OBJECT_DIR the directory where we store the object files - # LIBRARY the resulting library file - # PROGRAM the resulting exec file - # INCLUDES only pick source from these directories - # EXCLUDES do not pick source from these directories - # INCLUDE_FILES only compile exactly these files! - # EXCLUDE_FILES with these names - # VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run - # RC_FLAGS flags for RC. - # MAPFILE mapfile - # REORDER reorder file - # DEBUG_SYMBOLS add debug symbols (if configured on) - # CC the compiler to use, default is $(CC) - # LDEXE the linker to use for linking executables, default is $(LDEXE) - # OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST - $(foreach i,2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26, $(if $($i),$1_$(strip $($i)))$(NEWLINE)) - $(call LogSetupMacroEntry,SetupNativeCompilation($1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15),$(16),$(17),$(18),$(19),$(20),$(21),$(22),$(23),$(24),$(25),$(26)) - $(if $(27),$(error Internal makefile error: Too many arguments to SetupNativeCompilation, please update NativeCompilation.gmk)) - - ifneq (,$$($1_BIN)) - $$(error BIN has been replaced with OBJECT_DIR) - endif + # Setup the toolchain to be used + $$(eval $$(call SetupToolchain,$1)) - ifneq (,$$($1_LIB)) - $$(error LIB has been replaced with LIBRARY) - endif + # Find all source files to compile and determine the output object file names + $$(eval $$(call SetupSourceFiles,$1)) + $$(eval $$(call SetupOutputFiles,$1)) - ifneq (,$$($1_EXE)) - $$(error EXE has been replaced with PROGRAM) - endif + # Setup CFLAGS/CXXFLAGS based on warnings, optimizations, extra flags etc. + $$(eval $$(call SetupCompilerFlags,$1)) - ifneq (,$$($1_LIBRARY)) - ifeq (,$$($1_OUTPUT_DIR)) - $$(error LIBRARY requires OUTPUT_DIR) - endif + # Machinery needed for the build to function properly + $$(eval $$(call SetupBuildSystemSupport,$1)) - ifneq ($$($1_LIBRARY),$(basename $$($1_LIBRARY))) - $$(error directory of LIBRARY should be specified using OUTPUT_DIR) - endif + $$(eval $$(call RemoveSuperfluousOutputFiles,$1)) - ifneq (,$(findstring $(SHARED_LIBRARY_SUFFIX),$$($1_LIBRARY))) - $$(error LIBRARY should be specified without SHARED_LIBRARY_SUFFIX: $(SHARED_LIBRARY_SUFFIX)) - endif + # Need to make sure TARGET is first on list before starting to create files + $1 := $$($1_TARGET) - ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_LIBRARY))) - $$(error LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX)) - endif + # Have make print information about the library when we start compiling + $$(eval $$(call PrintStartInfo,$1)) - $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_LIBRARY)$(SHARED_LIBRARY_SUFFIX) - $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME) - $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_LIBRARY) - endif + ### + ### Compile all native source code files + ### - ifneq (,$$($1_STATIC_LIBRARY)) - ifeq (,$$($1_OUTPUT_DIR)) - $$(error STATIC_LIBRARY requires OUTPUT_DIR) - endif + # Create a PCH, if requested + $$(eval $$(call CreatePrecompiledHeader,$1)) - ifneq ($$($1_STATIC_LIBRARY),$(basename $$($1_STATIC_LIBRARY))) - $$(error directory of STATIC_LIBRARY should be specified using OUTPUT_DIR) - endif + # Now call CreateCompiledNativeFile for each source file we are going to compile. + $$(foreach file, $$($1_SRCS), \ + $$(eval $$(call CreateCompiledNativeFile,$1_$$(notdir $$(file)), \ + FILE := $$(file), \ + BASE := $1, \ + )) \ + ) - ifneq (,$(findstring $(STATIC_LIBRARY_SUFFIX),$$($1_STATIC_LIBRARY))) - $$(error STATIC_LIBRARY should be specified without STATIC_LIBRARY_SUFFIX: $(STATIC_LIBRARY_SUFFIX)) - endif - - ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_STATIC_LIBRARY))) - $$(error STATIC_LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX)) - endif - - $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)$(STATIC_LIBRARY_SUFFIX) - $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME) - $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY) + ifeq ($(call isTargetOs, windows), true) + # On windows we need to create a resource file + $$(eval $$(call CreateWindowsResourceFile,$1)) endif - ifneq (,$$($1_PROGRAM)) - ifeq (,$$($1_OUTPUT_DIR)) - $$(error PROGRAM requires OUTPUT_DIR) - endif + # Setup a library-wide dependency file from individual object file dependency + # files, and import it in the makefile. + $$(eval $$(call CreateDependencyFile,$1)) + $$(eval $$(call ImportDependencyFile,$1)) - ifneq ($$($1_PROGRAM),$(basename $$($1_PROGRAM))) - $$(error directory of PROGRAM should be specified using OUTPUT_DIR) - endif + ### + ### Link the object files into a native output library/executable + ### - ifneq (,$(findstring $(EXE_SUFFIX),$$($1_PROGRAM))) - $$(error PROGRAM should be specified without EXE_SUFFIX: $(EXE_SUFFIX)) - endif + # Handle native debug symbols + $$(eval $$(call CreateDebugSymbols,$1)) - $1_BASENAME:=$$($1_PROGRAM)$(EXE_SUFFIX) - $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME) - $1_NOSUFFIX:=$$($1_PROGRAM) + # Prepare for linking + $$(eval $$(call SetupLinkerFlags,$1)) + ifneq ($(TOOLCHAIN_TYPE), microsoft) + $$(eval $$(call SetupLinking,$1)) endif - ifeq (,$$($1_TARGET)) - $$(error Neither PROGRAM, LIBRARY nor STATIC_LIBRARY has been specified for SetupNativeCompilation) - endif + $$(eval $$(call SetupObjectFileList,$1)) - ifeq (,$$($1_LANG)) - $$(error You have to specify LANG for native compilation $1) - endif - ifeq (C,$$($1_LANG)) - ifeq ($$($1_LDEXE),) - $1_LDEXE:=$(LDEXE) - endif - $1_LD:=$(LD) + # Link the individually compiled files into a single unit + ifneq ($(TOOLCHAIN_TYPE), microsoft) + $$(eval $$(call CreateLinkedResult,$1)) else - ifeq (C++,$$($1_LANG)) - $1_LD:=$(LDCXX) - $1_LDEXE:=$(LDEXECXX) - else - $$(error Unknown native language $$($1_LANG) for $1) - endif - endif - - ifeq ($$($1_CC),) - $1_CC:=$(CC) + $$(eval $$(call CreateLinkedResultMicrosoft,$1)) endif - ifeq ($$($1_STRIP_POLICY),) - $1_STRIP_POLICY:=$$(STRIP_POLICY) + ifeq ($(GENERATE_COMPILE_COMMANDS_ONLY), true) + # Override all targets (this is a hack) + $1 := $$($1_ALL_OBJS_JSON) $$($1_LDFLAGS_FILE) endif +endef - # Make sure the dirs exist. - $$(eval $$(call MakeDir,$$($1_OBJECT_DIR) $$($1_OUTPUT_DIR))) - $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),,$$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d))) - - # Find all files in the source trees. Sort to remove duplicates. - $1_ALL_SRCS := $$(sort $$(call CacheFind,$$($1_SRC))) - # Extract the C/C++ files. - $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES))) - $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES))) - ifneq ($$($1_EXCLUDE_FILES),) - $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES)) - endif - $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter %.s %.c %.cpp %.m,$$($1_ALL_SRCS))) - ifneq (,$$(strip $$($1_INCLUDE_FILES))) - $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS)) +################################################################################ +# Verify that user passed arguments are valid +define VerifyArguments + ifeq ($$($1_NAME), ) + $$(error NAME must not be empty in $1) endif - ifeq (,$$($1_SRCS)) - $$(error No sources found for $1 when looking inside the dirs $$($1_SRC)) + ifneq ($$($1_NAME), $(basename $$($1_NAME))) + $$(error NAME must not contain any directory path in $1) endif - # There can be only a single bin dir root, no need to foreach over the roots. - $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX)) - # Now we have a list of all c/c++ files to compile: $$($1_SRCS) - # and we have a list of all existing object files: $$($1_BINS) - - # Prepend the source/bin path to the filter expressions. Then do the filtering. - ifneq ($$($1_INCLUDES),) - $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES)))) - $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS)) + ifneq ($(findstring $$($1_SUFFIX), $$($1_NAME)), ) + $$(error NAME should be specified without suffix: $$($1_SUFFIX) in $1) endif - ifneq ($$($1_EXCLUDES),) - $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES)))) - $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS)) + ifneq ($(findstring $$($1_PREFIX), $$($1_NAME)), ) + $$(error NAME should be specified without prefix: $$($1_PREFIX) in $1) endif - - # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides - # a reproducable order on the input files to the linker). - $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_OBJECT_DIR)/,$$(patsubst %.cpp,%$(OBJ_SUFFIX),$$(patsubst %.c,%$(OBJ_SUFFIX),$$(patsubst %.m,%$(OBJ_SUFFIX),$$(patsubst %.s,%$(OBJ_SUFFIX),$$(notdir $$($1_SRCS)))))))) - # Are there too many object files on disk? Perhaps because some source file was removed? - $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS))) - # Clean out the superfluous object files. - ifneq ($$($1_SUPERFLUOUS_OBJS),) - $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS)) + ifeq ($$($1_OUTPUT_DIR), ) + $$(error OUTPUT_DIR is missing in $1) endif - - # Pickup extra OPENJDK_TARGET_OS_API, OPENJDK_TARGET_OS, and/or OPENJDK_TARGET_OS plus - # OPENJDK_TARGET_CPU pair dependent variables for CFLAGS. - $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_API)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS)) \ - $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)) - ifneq ($(DEBUG_LEVEL),release) - # Pickup extra debug dependent variables for CFLAGS - $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug) - $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_API)_debug) - $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug) - $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)_debug) - else - $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release) - $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_API)_release) - $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release) - $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)_release) - endif - - # Pickup extra OPENJDK_TARGET_OS_API and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS. - $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_API)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)) - ifneq ($(DEBUG_LEVEL),release) - # Pickup extra debug dependent variables for CXXFLAGS - $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug) - $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_API)_debug) - $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug) - else - $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release) - $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_API)_release) - $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release) - endif - - ifeq ($$($1_CXXFLAGS),) - $1_CXXFLAGS:=$$($1_CFLAGS) - endif - ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),) - $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS) - endif - - ifneq (,$$($1_DEBUG_SYMBOLS)) - ifeq ($(ENABLE_DEBUG_SYMBOLS), true) - ifdef OPENJDK - # Always add debug symbols - $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS) - $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS) - else - # Programs don't get the debug symbols added in the old build. It's not clear if - # this is intentional. - ifeq ($$($1_PROGRAM),) - $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS) - $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS) - endif - endif + ifneq ($$($1_MANIFEST), ) + ifeq ($$($1_MANIFEST_VERSION), ) + $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1) endif endif +endef - ifneq (,$$($1_REORDER)) - $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER) - $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER) - endif - - ifeq (NONE, $$($1_OPTIMIZATION)) - $1_EXTRA_CFLAGS += $(C_O_FLAG_NONE) - $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NONE) - else ifeq (LOW, $$($1_OPTIMIZATION)) - $1_EXTRA_CFLAGS += $(C_O_FLAG_NORM) - $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NORM) - else ifeq (HIGH, $$($1_OPTIMIZATION)) - $1_EXTRA_CFLAGS += $(C_O_FLAG_HI) - $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HI) - else ifeq (HIGHEST, $$($1_OPTIMIZATION)) - $1_EXTRA_CFLAGS += $(C_O_FLAG_HIGHEST) - $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HIGHEST) - else ifneq (, $$($1_OPTIMIZATION)) - $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION)) - endif - - # Add sys root specific cflags last - $1_EXTRA_CFLAGS += $(SYSROOT_CFLAGS) - $1_EXTRA_CXXFLAGS += $(SYSROOT_CFLAGS) - - # Now call add_native_source for each source file we are going to compile. - $$(foreach p,$$($1_SRCS), \ - $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \ - $$($1_CFLAGS) $$($1_EXTRA_CFLAGS),$$($1_CC), \ - $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS),$(CXX),$$($1_ASFLAGS)))) - - # On windows we need to create a resource file - ifeq ($(OPENJDK_TARGET_OS), windows) - ifneq (,$$($1_VERSIONINFO_RESOURCE)) - $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res - $$($1_RES): $$($1_VERSIONINFO_RESOURCE) - $(RC) $$($1_RC_FLAGS) $(CC_OUT_OPTION)$$@ $$($1_VERSIONINFO_RESOURCE) +################################################################################ +# Setup basic variables +define SetupBasicVariables + # If type is unspecified, default to LIBRARY + ifeq ($$($1_TYPE), ) + $1_TYPE := LIBRARY + endif + + # STATIC_LIBS is set from Main.gmk when building static versions of certain + # native libraries. + ifeq ($(STATIC_LIBS), true) + $1_TYPE := STATIC_LIBRARY + # The static versions need to be redirected to different output dirs, both + # to not interfere with the main build as well as to not end up inside the + # jmods. + $1_OBJECT_DIR := $$($1_OBJECT_DIR)/static + $1_OUTPUT_DIR := $$($1_OBJECT_DIR) + endif + + ifeq ($$($1_TYPE), EXECUTABLE) + $1_PREFIX := + ifeq ($$($1_SUFFIX), ) + $1_SUFFIX := $(EXECUTABLE_SUFFIX) endif - ifneq (,$$($1_MANIFEST)) - $1_GEN_MANIFEST:=$$($1_OBJECT_DIR)/$$($1_PROGRAM).manifest - IMVERSIONVALUE:=$(JDK_MINOR_VERSION).$(JDK_MICRO_VERSION).$(JDK_UPDATE_VERSION).$(COOKED_BUILD_NUMBER) - $$($1_GEN_MANIFEST): $$($1_MANIFEST) - $(SED) 's%IMVERSION%$$(IMVERSIONVALUE)%g;s%PROGRAM%$$($1_PROGRAM)%g' $$< > $$@ - endif - endif - - # mapfile doesnt seem to be implemented on macosx (yet??) - ifneq ($(OPENJDK_TARGET_OS),macosx) - ifneq ($(OPENJDK_TARGET_OS),windows) - $1_REAL_MAPFILE:=$$($1_MAPFILE) - ifneq (,$$($1_REORDER)) - $1_REAL_MAPFILE:=$$($1_OBJECT_DIR)/mapfile - - $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER) - $$(MKDIR) -p $$(@D) - $$(CP) $$($1_MAPFILE) $$@.tmp - $$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp - $$(MV) $$@.tmp $$@ + else + $1_PREFIX := $(LIBRARY_PREFIX) + ifeq ($$($1_TYPE), LIBRARY) + ifeq ($$($1_SUFFIX), ) + $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX) + endif + else ifeq ($$($1_TYPE), STATIC_LIBRARY) + ifeq ($$($1_SUFFIX), ) + $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX) endif endif endif - # Pickup extra OPENJDK_TARGET_OS_API and/or OPENJDK_TARGET_OS dependent variables - # for LDFLAGS and LDFLAGS_SUFFIX - $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_API)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS)) - $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS_API)) $$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS)) - ifneq (,$$($1_REAL_MAPFILE)) - $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE)) - endif - - $1_EXTRA_LDFLAGS += $(SYSROOT_LDFLAGS) - - # Need to make sure TARGET is first on list - $1 := $$($1_TARGET) - - ifneq ($$($1_COPY_DEBUG_SYMBOLS), false) - $1_COPY_DEBUG_SYMBOLS := $(COPY_DEBUG_SYMBOLS) - endif - - ifneq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), false) - $1_ZIP_EXTERNAL_DEBUG_SYMBOLS := $(ZIP_EXTERNAL_DEBUG_SYMBOLS) - endif - - ifeq ($$($1_COPY_DEBUG_SYMBOLS), true) - ifneq ($$($1_DEBUG_SYMBOLS), false) - # Only copy debug symbols for dynamic libraries and programs. - ifneq ($$($1_TYPE), STATIC_LIBRARY) - # Generate debuginfo files. - ifeq ($(OPENJDK_TARGET_OS), windows) - $1_EXTRA_LDFLAGS += -debug "-pdb:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb" \ - "-map:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map" - ifeq ($(SHIP_DEBUG_SYMBOLS), public) - $1_EXTRA_LDFLAGS += "-pdbstripped:$$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).stripped.pdb" - endif - $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).pdb \ - $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).map - - else ifneq ($(findstring $(OPENJDK_TARGET_OS), linux solaris), ) - $1_DEBUGINFO_FILES := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).debuginfo - # Setup the command line creating debuginfo files, to be run after linking. - # It cannot be run separately since it updates the original target file - $1_CREATE_DEBUGINFO_CMDS := \ - $$($1_OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES) $$(NEWLINE) \ - $(CD) $$($1_OUTPUT_DIR) && \ - $$($1_OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET) - - else ifeq ($(OPENJDK_TARGET_OS), macosx) - $1_DEBUGINFO_FILES := \ - $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist \ - $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) - $1_CREATE_DEBUGINFO_CMDS := \ - $(DSYMUTIL) --out $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM $$($1_TARGET) - endif # OPENJDK_TARGET_OS - - # Since the link rule creates more than one file that we want to track, - # we have to use some tricks to get make to cooperate. To properly - # trigger downstream dependants of $$($1_DEBUGINFO_FILES), we must have - # a recipe in the rule below. To avoid rerunning the recipe every time - # have it touch the target. If a debuginfo file is deleted by something - # external, explicitly delete the TARGET to trigger a rebuild of both. - ifneq ($$(wildcard $$($1_DEBUGINFO_FILES)), $$($1_DEBUGINFO_FILES)) - $$(call LogDebug, Deleting $$($1_BASENAME) because debuginfo files are missing) - $$(shell $(RM) $$($1_TARGET)) - endif - $$($1_DEBUGINFO_FILES): $$($1_TARGET) - $$(if $$(CORRECT_FUNCTION_IN_RECIPE_EVALUATION), \ - $$(if $$(wildcard $$@), , $$(error $$@ was not created for $$<)) \ - ) - $(TOUCH) $$@ - - $1 += $$($1_DEBUGINFO_FILES) - - ifeq ($$($1_ZIP_EXTERNAL_DEBUG_SYMBOLS), true) - $1_DEBUGINFO_ZIP := $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).diz - $1 += $$($1_DEBUGINFO_ZIP) - - # The dependency on TARGET is needed for debuginfo files - # to be rebuilt properly. - $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET) - $(CD) $$($1_OUTPUT_DIR) && \ - $(ZIPEXE) -q -r $$@ $$(subst $$($1_OUTPUT_DIR)/,, $$($1_DEBUGINFO_FILES)) + $1_BASENAME := $$($1_PREFIX)$$($1_NAME)$$($1_SUFFIX) + $1_TARGET := $$($1_OUTPUT_DIR)/$$($1_BASENAME) + $1_NOSUFFIX := $$($1_PREFIX)$$($1_NAME) + $1_SAFE_NAME := $$(strip $$(subst /,_, $1)) + $1_UNIQUE_NAME = $$($1_TYPE)_$$(subst /,_,$$(patsubst $$(OUTPUTDIR)/%/,%,$$(dir $$($1_OBJECT_DIR))))_$$($1_NOSUFFIX) +endef - endif - endif # !STATIC_LIBRARY - endif # $1_DEBUG_SYMBOLS != false - endif # COPY_DEBUG_SYMBOLS - - # Unless specifically set, stripping should only happen if symbols are also - # being copied. - $$(call SetIfEmpty, $1_STRIP_SYMBOLS, $$($1_COPY_DEBUG_SYMBOLS)) - - ifneq ($$($1_STRIP_SYMBOLS), false) - ifneq ($$($1_STRIP), ) - # Default to using the global STRIPFLAGS. Allow for overriding with an empty value - $1_STRIPFLAGS ?= $(STRIPFLAGS) - $1_STRIP_CMD := $$($1_STRIP) $$($1_STRIPFLAGS) $$($1_TARGET) +################################################################################ +# Setup the toolchain variables +define SetupToolchain + ifeq ($$($1_TARGET_TYPE), BUILD) + $$(call SetIfEmpty, $1_CC, $$(BUILD_CC)) + $$(call SetIfEmpty, $1_CXX, $$(BUILD_CXX)) + $$(call SetIfEmpty, $1_AR, $$(BUILD_AR)) + $$(call SetIfEmpty, $1_LIB, $$(BUILD_LIB)) + $$(call SetIfEmpty, $1_AS, $$(BUILD_AS)) + $$(call SetIfEmpty, $1_OBJCOPY, $$(BUILD_OBJCOPY)) + $$(call SetIfEmpty, $1_STRIP, $$(BUILD_STRIP)) + $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$(BUILD_SYSROOT_CFLAGS)) + $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$(BUILD_SYSROOT_LDFLAGS)) + ifeq ($$($1_LINK_TYPE), C++) + $$(call SetIfEmpty, $1_LD, $$(BUILD_LDCXX)) + else + $$(call SetIfEmpty, $1_LD, $$(BUILD_LD)) endif - endif - - ifeq ($$($1_TYPE), STATIC_LIBRARY) - $1_VARDEPS := $$($1_AR) $$($1_ARFLAGS) $$($1_LIBS) \ - $$($1_EXTRA_LIBS) - $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \ - $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps) - - # Generating a static library, ie object file archive. - ifeq ($(STATIC_BUILD), true) - ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true) - STATIC_MAPFILE_DEP := $$($1_MAPFILE) - endif + else + $$(call SetIfEmpty, $1_CC, $$(CC)) + $$(call SetIfEmpty, $1_CXX, $$(CXX)) + $$(call SetIfEmpty, $1_AR, $$(AR)) + $$(call SetIfEmpty, $1_LIB, $$(LIB)) + $$(call SetIfEmpty, $1_AS, $$(AS)) + $$(call SetIfEmpty, $1_MT, $$(MT)) + $$(call SetIfEmpty, $1_RC, $$(RC)) + $$(call SetIfEmpty, $1_OBJCOPY, $$(OBJCOPY)) + $$(call SetIfEmpty, $1_STRIP, $$(STRIP)) + $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$(SYSROOT_CFLAGS)) + $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$(SYSROOT_LDFLAGS)) + ifeq ($$($1_LINK_TYPE), C++) + $$(call SetIfEmpty, $1_LD, $$(LDCXX)) + else + $$(call SetIfEmpty, $1_LD, $$(LD)) endif + endif +endef - $1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_RES) $$($1_VARDEPS_FILE) $$(STATIC_MAPFILE_DEP) - - $$($1_TARGET): $$($1_TARGET_DEPS) - $$(call LogInfo, Building static library $$($1_BASENAME)) - $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \ - $$($1_AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_ALL_OBJS) \ - $$($1_RES)) - ifeq ($(STATIC_BUILD), true) - ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true) - $(CP) $$($1_MAPFILE) $$(@D)/$$(basename $$(@F)).symbols - else - # The dependency on TARGET is needed on windows for debuginfo files - # to be rebuilt properly. - $$($1_OUTPUT_DIR)/% : $$($1_OBJECT_DIR)/% $$($1_TARGET) - $(CP) $$< $$@ - endif - endif - - # Generate debuginfo files. - ifeq ($(OPENJDK_TARGET_OS), windows) - ifneq ($$($1_STRIP_POLICY), no_strip) - $1_EXTRA_LDFLAGS += "-pdb:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb" \ - "-map:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map" - $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb \ - $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map - - # This dependency dance ensures that windows debug info files get rebuilt - # properly if deleted. - $$($1_TARGET): $$($1_DEBUGINFO_FILES) - $$($1_DEBUGINFO_FILES): $$($1_EXPECTED_OBJS) - endif - else ifeq ($(OPENJDK_TARGET_OS), solaris) - ifneq ($$($1_STRIP_POLICY), no_strip) - $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo - # gobjcopy crashes on "empty" section headers with the SHF_ALLOC flag set. - # Use $(FIX_EMPTY_SEC_HDR_FLAGS) to clear the SHF_ALLOC flag (if set) from - # empty section headers until a fixed $(OBJCOPY) is available. - # An empty section header has sh_addr == 0 and sh_size == 0. - # This problem has only been seen on Solaris X64, but we call this tool - # on all Solaris builds just in case. - # - # $(OBJCOPY) --add-gnu-debuglink=... corrupts SUNW_* sections. - # Use $(ADD_GNU_DEBUGLINK) until a fixed $(OBJCOPY) is available. - $$($1_DEBUGINFO_FILES): $$($1_TARGET) \ - $(FIX_EMPTY_SEC_HDR_FLAGS) $(ADD_GNU_DEBUGLINK) - $(RM) $$@ - $(FIX_EMPTY_SEC_HDR_FLAGS) $(LOG_INFO) $$< - $(OBJCOPY) --only-keep-debug $$< $$@ - $(CD) $$(@D) && $(ADD_GNU_DEBUGLINK) $(LOG_INFO) $$(@F) $$< - $(TOUCH) $$@ - endif - else ifeq ($(OPENJDK_TARGET_OS), linux) - ifneq ($$($1_STRIP_POLICY), no_strip) - $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo - $$($1_DEBUGINFO_FILES): $$($1_TARGET) - $(RM) $$@ - $(OBJCOPY) --only-keep-debug $$< $$@ - $(CD) $$(@D) && $(OBJCOPY) --add-gnu-debuglink=$$(@F) $$< - $(TOUCH) $$@ - endif - else ifeq ($(OPENJDK_TARGET_OS), aix) - ifneq ($$($1_STRIP_POLICY), no_strip) - # AIX does not provide the equivalent of OBJCOPY to extract debug symbols, - # so we copy the compiled object with symbols to the .debuginfo file, which - # happens prior to the STRIP on the original target object file. - $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo - $$($1_DEBUGINFO_FILES): $$($1_TARGET) - $(RM) $$@ - $(CP) $$< $$@ - $(TOUCH) $$@ - endif - else ifeq ($(OPENJDK_TARGET_OS), macosx) - ifneq ($$($1_STRIP_POLICY), no_strip) - $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist \ - $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) - $$($1_DEBUGINFO_FILES): $$($1_TARGET) - $(DSYMUTIL) --out $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM $$< - endif - endif +################################################################################ +# Setup machinery needed by the build system +define SetupBuildSystemSupport + # Track variable changes for all variables that affect the compilation command + # lines for all object files in this setup. This includes at least all the + # variables used in the call to add_native_source below. + $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \ + $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_OPT_CFLAGS) $$($1_OPT_CXXFLAGS) \ + $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS) + $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \ + $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps) +endef - ifeq ($(ZIP_DEBUGINFO_FILES), true) - ifneq ($$($1_STRIP_POLICY), no_strip) - $1_DEBUGINFO_ZIP := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz - $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_ZIP)) - - # The dependency on TARGET is needed on windows for debuginfo files - # to be rebuilt properly. - $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET) - $(CD) $$($1_OBJECT_DIR) \ - && $(ZIP) -q $$@ $$(subst $$($1_OBJECT_DIR)/,,$$($1_DEBUGINFO_FILES)) - endif +################################################################################ +# Have make print information about the library when we start compiling +define PrintStartInfo + # Setup rule for printing progress info when compiling source files. + # This is a rough heuristic and may not always print accurate information. + # The $1_BUILD_INFO and $1_BUILD_INFO_DEPS variables are used in + # TestFilesCompilation.gmk. + $$(call SetIfEmpty, $1_BUILD_INFO_LOG_MACRO, LogWarn) + $1_BUILD_INFO_DEPS := $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE) + $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker + + $$($1_BUILD_INFO): $$($1_BUILD_INFO_DEPS) + ifeq ($$(wildcard $$($1_TARGET)), ) + $$(call $$($1_BUILD_INFO_LOG_MACRO), \ + Creating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET)) from $$(words \ + $$(filter-out %.vardeps, $$?)) file(s)) else - ifneq ($$($1_STRIP_POLICY), no_strip) - $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_FILES)) - endif + $$(call $$($1_BUILD_INFO_LOG_MACRO), \ + $$(strip Updating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET)) \ + $$(if $$(filter-out %.vardeps, $$?), \ + due to $$(words $$(filter-out %.vardeps, $$?)) file(s), \ + $$(if $$(filter %.vardeps, $$?), due to makefile changes)))) endif - endif # ENABLE_DEBUG_SYMBOLS - endif # $1_DEBUG_SYMBOLS - endif # !STATIC_LIBRARY - - ifneq (,$$($1_LIBRARY)) - # Generating a dynamic library. - $1_EXTRA_LDFLAGS+=$$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME)) - ifeq ($(OPENJDK_TARGET_OS), windows) - $1_EXTRA_LDFLAGS+="-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib" - endif - - $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX) - - $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) - $$(call LINKING_MSG,$$($1_BASENAME)) - $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(LD_OUT_OPTION)$$@ \ - $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) \ - $$($1_EXTRA_LDFLAGS_SUFFIX) - # Touch target to make sure it has a later time stamp than the debug - # symbol files to avoid unnecessary relinking on rebuild. - ifeq ($(OPENJDK_TARGET_OS), windows) - $(TOUCH) $$@ - endif - - endif - - ifneq (,$$($1_STATIC_LIBRARY)) - # Generating a static library, ie object file archive. - $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES) - $$(call ARCHIVING_MSG,$$($1_LIBRARY)) - $(AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_EXPECTED_OBJS) \ - $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) - endif + $(TOUCH) $$@ +endef - ifneq (,$$($1_PROGRAM)) - # A executable binary has been specified, setup the target for it. - $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX) - - $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_GEN_MANIFEST) - $$(call LINKING_EXE_MSG,$$($1_BASENAME)) - $$($1_LDEXE) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(EXE_OUT_OPTION)$$($1_TARGET) \ - $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) \ - $$($1_EXTRA_LDFLAGS_SUFFIX) - ifneq (,$$($1_GEN_MANIFEST)) - $(MT) -nologo -manifest $$($1_GEN_MANIFEST) -outputresource:$$@;#1 - endif - # This only works if the openjdk_codesign identity is present on the system. Let - # silently fail otherwise. - ifneq (,$(CODESIGN)) - ifneq (,$$($1_CODESIGN)) - $(CODESIGN) -s openjdk_codesign $$@ - endif - endif - # Touch target to make sure it has a later time stamp than the debug - # symbol files to avoid unnecessary relinking on rebuild. - ifeq ($(OPENJDK_TARGET_OS), windows) - $(TOUCH) $$@ - endif +################################################################################ +# Setup a library-wide dependency file from individual object file dependency +# files +define CreateDependencyFile + # Create a rule to collect all the individual make dependency files into a + # single makefile. + $1_DEPS_FILE := $$($1_OBJECT_DIR)/$1.d + + $$($1_DEPS_FILE): $$($1_ALL_OBJS) $$($1_RES) + $(RM) $$@ + # CD into dir to reduce risk of hitting command length limits, which + # could otherwise happen if TOPDIR is a very long path. + $(CD) $$($1_OBJECT_DIR) && $(CAT) *.d > $$@.tmp + $(CD) $$($1_OBJECT_DIR) && $(CAT) *.d.targets | $(SORT) -u >> $$@.tmp + # After generating the file, which happens after all objects have been + # compiled, copy it to .old extension. On the next make invocation, this + # .old file will be included by make. + $(CP) $$@.tmp $$@.old + $(MV) $$@.tmp $$@ + + $1 += $$($1_DEPS_FILE) +endef +################################################################################ +# Import the dependency file into the makefile +define ImportDependencyFile + # The include must be on the .old file, which represents the state from the + # previous invocation of make. The file being included must not have a rule + # defined for it as otherwise make will think it has to run the rule before + # being able to include the file, which would be wrong since we specifically + # need the file as it was generated by a previous make invocation. + ifneq ($$(wildcard $$($1_DEPS_FILE).old), ) + $1_DEPS_FILE_LOADED := true + -include $$($1_DEPS_FILE).old endif endef + +endif # _NATIVE_COMPILATION_GMK From c20969a6a6145be40cb4ed957a30e12bdcf692dc Mon Sep 17 00:00:00 2001 From: sendaoYan Date: Sat, 1 Feb 2025 14:03:03 +0800 Subject: [PATCH 4/4] Drop change of make/common/NativeCompilation.gmk --- make/common/NativeCompilation.gmk | 854 ++++++++++++++++++------------ 1 file changed, 521 insertions(+), 333 deletions(-) diff --git a/make/common/NativeCompilation.gmk b/make/common/NativeCompilation.gmk index 66d07df3b78..009139fb7bf 100644 --- a/make/common/NativeCompilation.gmk +++ b/make/common/NativeCompilation.gmk @@ -1,5 +1,5 @@ # -# Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -23,390 +23,578 @@ # questions. # -################################################################################ -# This is the top-level entry point for our native compilation and linking. -# It contains the SetupNativeCompilation macro, but is supported by helper -# macros in the make/common/native directory. -################################################################################ +# When you read this source. Remember that $(sort ...) has the side effect +# of removing duplicates. It is actually this side effect that is +# desired whenever sort is used below! -ifndef _NATIVE_COMPILATION_GMK -_NATIVE_COMPILATION_GMK := 1 - -ifeq ($(_MAKEBASE_GMK), ) +ifeq (,$(_MAKEBASE_GMK)) $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk) endif -include MakeIO.gmk -include native/CompileFile.gmk -include native/DebugSymbols.gmk -include native/Flags.gmk -include native/Link.gmk -include native/LinkMicrosoft.gmk -include native/Paths.gmk - -################################################################################ -# Setup make rules for creating a native binary (a shared library or an -# executable). -# -# Parameter 1 is the name of the rule. This name is used as variable prefix, -# and the targets generated are listed in a variable by that name. -# -# Remaining parameters are named arguments. These include: -# NAME The base name for the resulting binary, excluding decorations (like *.exe) -# TYPE Type of binary (EXECUTABLE, LIBRARY or STATIC_LIBRARY). Default is LIBRARY. -# SUFFIX Override the default suffix for the output file -# TARGET_TYPE The type to target, BUILD or TARGET. Defaults to TARGET. -# LINK_TYPE The language to use for the linker, C or C++. Defaults to C. -# SRC one or more directory roots to scan for C/C++ files. -# CFLAGS the compiler flags to be used, used both for C and C++. -# CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS. -# LDFLAGS the linker flags to be used, used both for C and C++. -# LDFLAGS_ the linker flags to be used for the specified toolchain, -# used both for C and C++. -# LDFLAGS_ the linker flags to be used for the specified target OS, -# used both for C and C++. -# LDFLAGS__ the linker flags to be used for the specified -# toolchain and target OS, used both for C and C++. -# LIBS the libraries to link to -# LIBS_ the libraries to link to for the specified target OS, -# used both for C and C++. -# LIBS_ the libraries to link to for the specified toolchain, -# used both for C and C++. -# LIBS__ the libraries to link to for the specified target -# OS and toolchain, used both for C and C++. -# OBJECT_DIR the directory where we store the object files -# OUTPUT_DIR the directory where the resulting binary is put -# SYMBOLS_DIR the directory where the debug symbols are put, defaults to OUTPUT_DIR -# INCLUDES only pick source from these directories -# EXCLUDES do not pick source from these directories -# INCLUDE_FILES only compile exactly these files! -# EXCLUDE_FILES with these names -# EXCLUDE_PATTERN exclude files matching any of these substrings -# EXTRA_FILES List of extra files not in any of the SRC dirs -# EXTRA_OBJECT_FILES List of extra object files to include when linking -# EXTRA_DEPS List of extra dependencies to be added to each compiled file -# EXTRA_LINK_DEPS List of extra dependencies to be added to the link stage -# VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run -# RCFLAGS flags for RC. -# EMBED_MANIFEST if true, embed manifest on Windows. -# CC the C compiler to use -# CXX the C++ compiler to use -# LD the Linker to use -# AR the static linker to use -# LIB the Windows lib tool to use for creating static libraries -# AS the assembler to use -# MT the Windows MT tool to use -# RC the Windows RC tool to use -# OBJCOPY the objcopy tool for debug symbol handling -# STRIP the tool to use for stripping debug symbols -# SYSROOT_CFLAGS the compiler flags for using the specific sysroot -# SYSROOT_LDFLAGS the linker flags for using the specific sysroot -# OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST, HIGHEST_JVM, SIZE -# DISABLED_WARNINGS_ Disable the given warnings for the specified toolchain -# DISABLED_WARNINGS__ Disable the given warnings for the specified -# toolchain and target OS -# DISABLED_WARNINGS_C_ Disable the given warnings for the specified toolchain -# when compiling C code -# DISABLED_WARNINGS_C__ Disable the given warnings for the specified -# toolchain and target OS when compiling C code -# DISABLED_WARNINGS_CXX_ Disable the given warnings for the specified -# toolchain when compiling C++ code -# DISABLED_WARNINGS_CXX__ Disable the given warnings for the specified -# toolchain and target OS when compiling C++ code -# DISABLED_WARNINGS__ Disable the given warnings for the specified -# toolchain when compiling the file specified by filename -# DISABLED_WARNINGS___ Disable the given warnings for the specified -# toolchain and target OS when compiling the file specified by filename -# STRIP_SYMBOLS Set to false to override global strip policy and always leave -# symbols in the binary, if the toolchain allows for it -# DEBUG_SYMBOLS Set to false to disable generation of debug symbols -# COPY_DEBUG_SYMBOLS Set to false to override global setting of debug symbol copying -# ZIP_EXTERNAL_DEBUG_SYMBOLS Set to false to override global setting of debug symbol -# zipping -# STRIPFLAGS Optionally change the flags given to the strip command -# PRECOMPILED_HEADER Header file to use as precompiled header -# PRECOMPILED_HEADER_EXCLUDE List of source files that should not use PCH -# BUILD_INFO_LOG_MACRO Overrides log level of the build info log message, default LogWarn -# STATIC_LIB_EXCLUDE_OBJS exclude objects that matches from static library -# -# After being called, some variables are exported from this macro, all prefixed -# with parameter 1 followed by a '_': -# TARGET The library or executable created by the macro -# TARGET_DEPS All prerequisites for the target calculated by the macro -# ALL_OBJS All object files -# IMPORT_LIBRARY The import library created for a shared library on Windows -# -SetupNativeCompilation = $(NamedParamsMacroTemplate) -define SetupNativeCompilationBody - # When reading this code, note that macros named Setup are just setting - # variables, and macros called Create are setting up rules to create - # files. Macros starting with any other verb are more complicated, and can do - # all of the above, and also call directly to the shell. +ifneq ($(TOOLCHAIN_TYPE), microsoft) + COMPILING_MSG=echo $(LOG_INFO) "Compiling $(notdir $1) (for $(notdir $2))" + LINKING_MSG=echo $(LOG_INFO) "Linking $1" + LINKING_EXE_MSG=echo $(LOG_INFO) "Linking executable $1" + ARCHIVING_MSG=echo $(LOG_INFO) "Archiving $1" +else + COMPILING_MSG= + LINKING_MSG= + LINKING_EXE_MSG= + ARCHIVING_MSG= +endif - ### - ### Prepare for compilation and linking - ### +ifeq ($(OPENJDK_BUILD_OS_ENV), windows.cygwin) + UNIX_PATH_PREFIX := /cygdrive +else ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys) + UNIX_PATH_PREFIX := +endif - $$(eval $$(call VerifyArguments,$1)) +define add_native_source + # param 1 = BUILD_MYPACKAGE + # parma 2 = the source file name (..../alfa.c or .../beta.cpp) + # param 3 = the bin dir that stores all .o (.obj) and .d files. + # param 4 = the c flags to the compiler + # param 5 = the c compiler + # param 6 = the c++ flags to the compiler + # param 7 = the c++ compiler + # param 8 = the flags to the assembler + + ifneq (,$$(filter %.c,$2)) + # Compile as a C file + $1_$2_FLAGS=$4 $$($1_$(notdir $2)_CFLAGS) -DTHIS_FILE='"$$( $$($1_$2_DEP) + else + $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP) $(CC_OUT_OPTION)$$($1_$2_OBJ) $2 + endif + endif + # The Visual Studio compiler lacks a feature for generating make dependencies, but by + # setting -showIncludes, all included files are printed. These are filtered out and + # parsed into make dependences. + ifeq ($(TOOLCHAIN_TYPE), microsoft) + ($$($1_$2_COMP) $$($1_$2_FLAGS) -showIncludes $$($1_$2_DEBUG_OUT_FLAGS) \ + $(CC_OUT_OPTION)$$($1_$2_OBJ) $2 ; echo $$$$? > $$($1_$2_DEP).exitvalue) \ + | $(TEE) $$($1_$2_DEP).raw | $(GREP) -v "^Note: including file:" \ + && exit `cat $$($1_$2_DEP).exitvalue` + $(RM) $$($1_$2_DEP).exitvalue + ($(ECHO) $$@: \\ \ + && $(SED) -e '/^Note: including file:/!d' \ + -e 's|Note: including file: *||' \ + -e 's|\r||g' \ + -e 's|\\|/|g' \ + -e 's|^\([a-zA-Z]\):|$(UNIX_PATH_PREFIX)/\1|g' \ + -e '/$(subst /,\/,$(TOPDIR))/!d' \ + -e 's|$$$$| \\|g' \ + $$($1_$2_DEP).raw) > $$($1_$2_DEP) + endif + endif +endef - # Setup the toolchain to be used - $$(eval $$(call SetupToolchain,$1)) +define SetupNativeCompilation + # param 1 is for example BUILD_MYPACKAGE + # param 2,3,4,5,6,7,8 are named args. + # SRC one or more directory roots to scan for C/C++ files. + # LANG C or C++ + # CFLAGS the compiler flags to be used, used both for C and C++. + # CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS. + # LDFLAGS the linker flags to be used, used both for C and C++. + # LDFLAGS_SUFFIX the linker flags to be added last on the commandline + # typically the libraries linked to. + # ARFLAGS the archiver flags to be used + # OBJECT_DIR the directory where we store the object files + # LIBRARY the resulting library file + # PROGRAM the resulting exec file + # INCLUDES only pick source from these directories + # EXCLUDES do not pick source from these directories + # INCLUDE_FILES only compile exactly these files! + # EXCLUDE_FILES with these names + # VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run + # RC_FLAGS flags for RC. + # MAPFILE mapfile + # REORDER reorder file + # DEBUG_SYMBOLS add debug symbols (if configured on) + # CC the compiler to use, default is $(CC) + # LDEXE the linker to use for linking executables, default is $(LDEXE) + # OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST + $(foreach i,2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26, $(if $($i),$1_$(strip $($i)))$(NEWLINE)) + $(call LogSetupMacroEntry,SetupNativeCompilation($1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15),$(16),$(17),$(18),$(19),$(20),$(21),$(22),$(23),$(24),$(25),$(26)) + $(if $(27),$(error Internal makefile error: Too many arguments to SetupNativeCompilation, please update NativeCompilation.gmk)) + + ifneq (,$$($1_BIN)) + $$(error BIN has been replaced with OBJECT_DIR) + endif - # Find all source files to compile and determine the output object file names - $$(eval $$(call SetupSourceFiles,$1)) - $$(eval $$(call SetupOutputFiles,$1)) + ifneq (,$$($1_LIB)) + $$(error LIB has been replaced with LIBRARY) + endif - # Setup CFLAGS/CXXFLAGS based on warnings, optimizations, extra flags etc. - $$(eval $$(call SetupCompilerFlags,$1)) + ifneq (,$$($1_EXE)) + $$(error EXE has been replaced with PROGRAM) + endif - # Machinery needed for the build to function properly - $$(eval $$(call SetupBuildSystemSupport,$1)) + ifneq (,$$($1_LIBRARY)) + ifeq (,$$($1_OUTPUT_DIR)) + $$(error LIBRARY requires OUTPUT_DIR) + endif - $$(eval $$(call RemoveSuperfluousOutputFiles,$1)) + ifneq ($$($1_LIBRARY),$(basename $$($1_LIBRARY))) + $$(error directory of LIBRARY should be specified using OUTPUT_DIR) + endif - # Need to make sure TARGET is first on list before starting to create files - $1 := $$($1_TARGET) + ifneq (,$(findstring $(SHARED_LIBRARY_SUFFIX),$$($1_LIBRARY))) + $$(error LIBRARY should be specified without SHARED_LIBRARY_SUFFIX: $(SHARED_LIBRARY_SUFFIX)) + endif - # Have make print information about the library when we start compiling - $$(eval $$(call PrintStartInfo,$1)) + ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_LIBRARY))) + $$(error LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX)) + endif + + $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_LIBRARY)$(SHARED_LIBRARY_SUFFIX) + $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME) + $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_LIBRARY) + endif - ### - ### Compile all native source code files - ### + ifneq (,$$($1_STATIC_LIBRARY)) + ifeq (,$$($1_OUTPUT_DIR)) + $$(error STATIC_LIBRARY requires OUTPUT_DIR) + endif - # Create a PCH, if requested - $$(eval $$(call CreatePrecompiledHeader,$1)) + ifneq ($$($1_STATIC_LIBRARY),$(basename $$($1_STATIC_LIBRARY))) + $$(error directory of STATIC_LIBRARY should be specified using OUTPUT_DIR) + endif - # Now call CreateCompiledNativeFile for each source file we are going to compile. - $$(foreach file, $$($1_SRCS), \ - $$(eval $$(call CreateCompiledNativeFile,$1_$$(notdir $$(file)), \ - FILE := $$(file), \ - BASE := $1, \ - )) \ - ) + ifneq (,$(findstring $(STATIC_LIBRARY_SUFFIX),$$($1_STATIC_LIBRARY))) + $$(error STATIC_LIBRARY should be specified without STATIC_LIBRARY_SUFFIX: $(STATIC_LIBRARY_SUFFIX)) + endif - ifeq ($(call isTargetOs, windows), true) - # On windows we need to create a resource file - $$(eval $$(call CreateWindowsResourceFile,$1)) + ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_STATIC_LIBRARY))) + $$(error STATIC_LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX)) + endif + + $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)$(STATIC_LIBRARY_SUFFIX) + $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME) + $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY) endif - # Setup a library-wide dependency file from individual object file dependency - # files, and import it in the makefile. - $$(eval $$(call CreateDependencyFile,$1)) - $$(eval $$(call ImportDependencyFile,$1)) + ifneq (,$$($1_PROGRAM)) + ifeq (,$$($1_OUTPUT_DIR)) + $$(error PROGRAM requires OUTPUT_DIR) + endif - ### - ### Link the object files into a native output library/executable - ### + ifneq ($$($1_PROGRAM),$(basename $$($1_PROGRAM))) + $$(error directory of PROGRAM should be specified using OUTPUT_DIR) + endif - # Handle native debug symbols - $$(eval $$(call CreateDebugSymbols,$1)) + ifneq (,$(findstring $(EXE_SUFFIX),$$($1_PROGRAM))) + $$(error PROGRAM should be specified without EXE_SUFFIX: $(EXE_SUFFIX)) + endif - # Prepare for linking - $$(eval $$(call SetupLinkerFlags,$1)) - ifneq ($(TOOLCHAIN_TYPE), microsoft) - $$(eval $$(call SetupLinking,$1)) + $1_BASENAME:=$$($1_PROGRAM)$(EXE_SUFFIX) + $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME) + $1_NOSUFFIX:=$$($1_PROGRAM) endif - $$(eval $$(call SetupObjectFileList,$1)) + ifeq (,$$($1_TARGET)) + $$(error Neither PROGRAM, LIBRARY nor STATIC_LIBRARY has been specified for SetupNativeCompilation) + endif - # Link the individually compiled files into a single unit - ifneq ($(TOOLCHAIN_TYPE), microsoft) - $$(eval $$(call CreateLinkedResult,$1)) + ifeq (,$$($1_LANG)) + $$(error You have to specify LANG for native compilation $1) + endif + ifeq (C,$$($1_LANG)) + ifeq ($$($1_LDEXE),) + $1_LDEXE:=$(LDEXE) + endif + $1_LD:=$(LD) else - $$(eval $$(call CreateLinkedResultMicrosoft,$1)) + ifeq (C++,$$($1_LANG)) + $1_LD:=$(LDCXX) + $1_LDEXE:=$(LDEXECXX) + else + $$(error Unknown native language $$($1_LANG) for $1) + endif endif - ifeq ($(GENERATE_COMPILE_COMMANDS_ONLY), true) - # Override all targets (this is a hack) - $1 := $$($1_ALL_OBJS_JSON) $$($1_LDFLAGS_FILE) + ifeq ($$($1_CC),) + $1_CC:=$(CC) endif -endef -################################################################################ -# Verify that user passed arguments are valid -define VerifyArguments - ifeq ($$($1_NAME), ) - $$(error NAME must not be empty in $1) + ifeq ($$($1_STRIP_POLICY),) + $1_STRIP_POLICY:=$$(STRIP_POLICY) endif - ifneq ($$($1_NAME), $(basename $$($1_NAME))) - $$(error NAME must not contain any directory path in $1) + + # Make sure the dirs exist. + $$(eval $$(call MakeDir,$$($1_OBJECT_DIR) $$($1_OUTPUT_DIR))) + $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),,$$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d))) + + # Find all files in the source trees. Sort to remove duplicates. + $1_ALL_SRCS := $$(sort $$(call CacheFind,$$($1_SRC))) + # Extract the C/C++ files. + $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES))) + $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES))) + ifneq ($$($1_EXCLUDE_FILES),) + $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES)) endif - ifneq ($(findstring $$($1_SUFFIX), $$($1_NAME)), ) - $$(error NAME should be specified without suffix: $$($1_SUFFIX) in $1) + $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter %.s %.c %.cpp %.m,$$($1_ALL_SRCS))) + ifneq (,$$(strip $$($1_INCLUDE_FILES))) + $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS)) endif - ifneq ($(findstring $$($1_PREFIX), $$($1_NAME)), ) - $$(error NAME should be specified without prefix: $$($1_PREFIX) in $1) + ifeq (,$$($1_SRCS)) + $$(error No sources found for $1 when looking inside the dirs $$($1_SRC)) endif - ifeq ($$($1_OUTPUT_DIR), ) - $$(error OUTPUT_DIR is missing in $1) + # There can be only a single bin dir root, no need to foreach over the roots. + $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX)) + # Now we have a list of all c/c++ files to compile: $$($1_SRCS) + # and we have a list of all existing object files: $$($1_BINS) + + # Prepend the source/bin path to the filter expressions. Then do the filtering. + ifneq ($$($1_INCLUDES),) + $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES)))) + $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS)) endif - ifneq ($$($1_MANIFEST), ) - ifeq ($$($1_MANIFEST_VERSION), ) - $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1) - endif + ifneq ($$($1_EXCLUDES),) + $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES)))) + $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS)) endif -endef -################################################################################ -# Setup basic variables -define SetupBasicVariables - # If type is unspecified, default to LIBRARY - ifeq ($$($1_TYPE), ) - $1_TYPE := LIBRARY - endif - - # STATIC_LIBS is set from Main.gmk when building static versions of certain - # native libraries. - ifeq ($(STATIC_LIBS), true) - $1_TYPE := STATIC_LIBRARY - # The static versions need to be redirected to different output dirs, both - # to not interfere with the main build as well as to not end up inside the - # jmods. - $1_OBJECT_DIR := $$($1_OBJECT_DIR)/static - $1_OUTPUT_DIR := $$($1_OBJECT_DIR) - endif - - ifeq ($$($1_TYPE), EXECUTABLE) - $1_PREFIX := - ifeq ($$($1_SUFFIX), ) - $1_SUFFIX := $(EXECUTABLE_SUFFIX) - endif + # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides + # a reproducable order on the input files to the linker). + $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_OBJECT_DIR)/,$$(patsubst %.cpp,%$(OBJ_SUFFIX),$$(patsubst %.c,%$(OBJ_SUFFIX),$$(patsubst %.m,%$(OBJ_SUFFIX),$$(patsubst %.s,%$(OBJ_SUFFIX),$$(notdir $$($1_SRCS)))))))) + # Are there too many object files on disk? Perhaps because some source file was removed? + $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS))) + # Clean out the superfluous object files. + ifneq ($$($1_SUPERFLUOUS_OBJS),) + $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS)) + endif + + # Pickup extra OPENJDK_TARGET_OS_API, OPENJDK_TARGET_OS, and/or OPENJDK_TARGET_OS plus + # OPENJDK_TARGET_CPU pair dependent variables for CFLAGS. + $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_API)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS)) \ + $$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)) + ifneq ($(DEBUG_LEVEL),release) + # Pickup extra debug dependent variables for CFLAGS + $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug) + $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_API)_debug) + $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug) + $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)_debug) else - $1_PREFIX := $(LIBRARY_PREFIX) - ifeq ($$($1_TYPE), LIBRARY) - ifeq ($$($1_SUFFIX), ) - $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX) - endif - else ifeq ($$($1_TYPE), STATIC_LIBRARY) - ifeq ($$($1_SUFFIX), ) - $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX) + $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release) + $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_API)_release) + $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release) + $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_$(OPENJDK_TARGET_CPU)_release) + endif + + # Pickup extra OPENJDK_TARGET_OS_API and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS. + $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_API)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)) + ifneq ($(DEBUG_LEVEL),release) + # Pickup extra debug dependent variables for CXXFLAGS + $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug) + $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_API)_debug) + $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug) + else + $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release) + $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_API)_release) + $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release) + endif + + ifeq ($$($1_CXXFLAGS),) + $1_CXXFLAGS:=$$($1_CFLAGS) + endif + ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),) + $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS) + endif + + ifneq (,$$($1_DEBUG_SYMBOLS)) + ifeq ($(ENABLE_DEBUG_SYMBOLS), true) + ifdef OPENJDK + # Always add debug symbols + $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS) + $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS) + else + # Programs don't get the debug symbols added in the old build. It's not clear if + # this is intentional. + ifeq ($$($1_PROGRAM),) + $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS) + $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS) + endif endif endif endif - $1_BASENAME := $$($1_PREFIX)$$($1_NAME)$$($1_SUFFIX) - $1_TARGET := $$($1_OUTPUT_DIR)/$$($1_BASENAME) - $1_NOSUFFIX := $$($1_PREFIX)$$($1_NAME) - $1_SAFE_NAME := $$(strip $$(subst /,_, $1)) - $1_UNIQUE_NAME = $$($1_TYPE)_$$(subst /,_,$$(patsubst $$(OUTPUTDIR)/%/,%,$$(dir $$($1_OBJECT_DIR))))_$$($1_NOSUFFIX) -endef + ifneq (,$$($1_REORDER)) + $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER) + $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER) + endif -################################################################################ -# Setup the toolchain variables -define SetupToolchain - ifeq ($$($1_TARGET_TYPE), BUILD) - $$(call SetIfEmpty, $1_CC, $$(BUILD_CC)) - $$(call SetIfEmpty, $1_CXX, $$(BUILD_CXX)) - $$(call SetIfEmpty, $1_AR, $$(BUILD_AR)) - $$(call SetIfEmpty, $1_LIB, $$(BUILD_LIB)) - $$(call SetIfEmpty, $1_AS, $$(BUILD_AS)) - $$(call SetIfEmpty, $1_OBJCOPY, $$(BUILD_OBJCOPY)) - $$(call SetIfEmpty, $1_STRIP, $$(BUILD_STRIP)) - $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$(BUILD_SYSROOT_CFLAGS)) - $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$(BUILD_SYSROOT_LDFLAGS)) - ifeq ($$($1_LINK_TYPE), C++) - $$(call SetIfEmpty, $1_LD, $$(BUILD_LDCXX)) - else - $$(call SetIfEmpty, $1_LD, $$(BUILD_LD)) + ifeq (NONE, $$($1_OPTIMIZATION)) + $1_EXTRA_CFLAGS += $(C_O_FLAG_NONE) + $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NONE) + else ifeq (LOW, $$($1_OPTIMIZATION)) + $1_EXTRA_CFLAGS += $(C_O_FLAG_NORM) + $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NORM) + else ifeq (HIGH, $$($1_OPTIMIZATION)) + $1_EXTRA_CFLAGS += $(C_O_FLAG_HI) + $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HI) + else ifeq (HIGHEST, $$($1_OPTIMIZATION)) + $1_EXTRA_CFLAGS += $(C_O_FLAG_HIGHEST) + $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HIGHEST) + else ifneq (, $$($1_OPTIMIZATION)) + $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION)) + endif + + # Add sys root specific cflags last + $1_EXTRA_CFLAGS += $(SYSROOT_CFLAGS) + $1_EXTRA_CXXFLAGS += $(SYSROOT_CFLAGS) + + # Now call add_native_source for each source file we are going to compile. + $$(foreach p,$$($1_SRCS), \ + $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \ + $$($1_CFLAGS) $$($1_EXTRA_CFLAGS),$$($1_CC), \ + $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS),$(CXX),$$($1_ASFLAGS)))) + + # On windows we need to create a resource file + ifeq ($(OPENJDK_TARGET_OS), windows) + ifneq (,$$($1_VERSIONINFO_RESOURCE)) + $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res + $$($1_RES): $$($1_VERSIONINFO_RESOURCE) + $(RC) $$($1_RC_FLAGS) $(CC_OUT_OPTION)$$@ $$($1_VERSIONINFO_RESOURCE) endif - else - $$(call SetIfEmpty, $1_CC, $$(CC)) - $$(call SetIfEmpty, $1_CXX, $$(CXX)) - $$(call SetIfEmpty, $1_AR, $$(AR)) - $$(call SetIfEmpty, $1_LIB, $$(LIB)) - $$(call SetIfEmpty, $1_AS, $$(AS)) - $$(call SetIfEmpty, $1_MT, $$(MT)) - $$(call SetIfEmpty, $1_RC, $$(RC)) - $$(call SetIfEmpty, $1_OBJCOPY, $$(OBJCOPY)) - $$(call SetIfEmpty, $1_STRIP, $$(STRIP)) - $$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$(SYSROOT_CFLAGS)) - $$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$(SYSROOT_LDFLAGS)) - ifeq ($$($1_LINK_TYPE), C++) - $$(call SetIfEmpty, $1_LD, $$(LDCXX)) - else - $$(call SetIfEmpty, $1_LD, $$(LD)) + ifneq (,$$($1_MANIFEST)) + $1_GEN_MANIFEST:=$$($1_OBJECT_DIR)/$$($1_PROGRAM).manifest + IMVERSIONVALUE:=$(JDK_MINOR_VERSION).$(JDK_MICRO_VERSION).$(JDK_UPDATE_VERSION).$(COOKED_BUILD_NUMBER) + $$($1_GEN_MANIFEST): $$($1_MANIFEST) + $(SED) 's%IMVERSION%$$(IMVERSIONVALUE)%g;s%PROGRAM%$$($1_PROGRAM)%g' $$< > $$@ endif endif -endef -################################################################################ -# Setup machinery needed by the build system -define SetupBuildSystemSupport - # Track variable changes for all variables that affect the compilation command - # lines for all object files in this setup. This includes at least all the - # variables used in the call to add_native_source below. - $1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \ - $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_OPT_CFLAGS) $$($1_OPT_CXXFLAGS) \ - $$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS) - $1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \ - $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps) -endef + # mapfile doesnt seem to be implemented on macosx (yet??) + ifneq ($(OPENJDK_TARGET_OS),macosx) + ifneq ($(OPENJDK_TARGET_OS),windows) + $1_REAL_MAPFILE:=$$($1_MAPFILE) + ifneq (,$$($1_REORDER)) + $1_REAL_MAPFILE:=$$($1_OBJECT_DIR)/mapfile + + $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER) + $$(MKDIR) -p $$(@D) + $$(CP) $$($1_MAPFILE) $$@.tmp + $$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp + $$(MV) $$@.tmp $$@ + endif + endif + endif + + # Pickup extra OPENJDK_TARGET_OS_API and/or OPENJDK_TARGET_OS dependent variables + # for LDFLAGS and LDFLAGS_SUFFIX + $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_API)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS)) + $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS_API)) $$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS)) + ifneq (,$$($1_REAL_MAPFILE)) + $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE)) + endif -################################################################################ -# Have make print information about the library when we start compiling -define PrintStartInfo - # Setup rule for printing progress info when compiling source files. - # This is a rough heuristic and may not always print accurate information. - # The $1_BUILD_INFO and $1_BUILD_INFO_DEPS variables are used in - # TestFilesCompilation.gmk. - $$(call SetIfEmpty, $1_BUILD_INFO_LOG_MACRO, LogWarn) - $1_BUILD_INFO_DEPS := $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE) - $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker - - $$($1_BUILD_INFO): $$($1_BUILD_INFO_DEPS) - ifeq ($$(wildcard $$($1_TARGET)), ) - $$(call $$($1_BUILD_INFO_LOG_MACRO), \ - Creating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET)) from $$(words \ - $$(filter-out %.vardeps, $$?)) file(s)) + $1_EXTRA_LDFLAGS += $(SYSROOT_LDFLAGS) + + # Need to make sure TARGET is first on list + $1 := $$($1_TARGET) + ifeq ($$($1_STATIC_LIBRARY),) + ifneq ($$($1_DEBUG_SYMBOLS),) + ifeq ($(ENABLE_DEBUG_SYMBOLS), true) + ifneq ($$($1_OUTPUT_DIR),$$($1_OBJECT_DIR)) + ifeq ($(OPENJDK_TARGET_OS), macosx) + # MacOS debug symbols are in %.dSYM directories + $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist : $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist + mkdir -p $$(@D) + $(CP) $$< $$@ + $$($1_OUTPUT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) : $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) + mkdir -p $$(@D) + $(CP) $$< $$@ + $$($1_OUTPUT_DIR)/$$($1_NOSUFFIX).diz : $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz + $(CP) $$< $$@ + else + # The dependency on TARGET is needed on windows for debuginfo files + # to be rebuilt properly. + $$($1_OUTPUT_DIR)/% : $$($1_OBJECT_DIR)/% $$($1_TARGET) + $(CP) $$< $$@ + endif + endif + + # Generate debuginfo files. + ifeq ($(OPENJDK_TARGET_OS), windows) + ifneq ($$($1_STRIP_POLICY), no_strip) + $1_EXTRA_LDFLAGS += "-pdb:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb" \ + "-map:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map" + $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb \ + $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map + + # This dependency dance ensures that windows debug info files get rebuilt + # properly if deleted. + $$($1_TARGET): $$($1_DEBUGINFO_FILES) + $$($1_DEBUGINFO_FILES): $$($1_EXPECTED_OBJS) + endif + else ifeq ($(OPENJDK_TARGET_OS), solaris) + ifneq ($$($1_STRIP_POLICY), no_strip) + $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo + # gobjcopy crashes on "empty" section headers with the SHF_ALLOC flag set. + # Use $(FIX_EMPTY_SEC_HDR_FLAGS) to clear the SHF_ALLOC flag (if set) from + # empty section headers until a fixed $(OBJCOPY) is available. + # An empty section header has sh_addr == 0 and sh_size == 0. + # This problem has only been seen on Solaris X64, but we call this tool + # on all Solaris builds just in case. + # + # $(OBJCOPY) --add-gnu-debuglink=... corrupts SUNW_* sections. + # Use $(ADD_GNU_DEBUGLINK) until a fixed $(OBJCOPY) is available. + $$($1_DEBUGINFO_FILES): $$($1_TARGET) \ + $(FIX_EMPTY_SEC_HDR_FLAGS) $(ADD_GNU_DEBUGLINK) + $(RM) $$@ + $(FIX_EMPTY_SEC_HDR_FLAGS) $(LOG_INFO) $$< + $(OBJCOPY) --only-keep-debug $$< $$@ + $(CD) $$(@D) && $(ADD_GNU_DEBUGLINK) $(LOG_INFO) $$(@F) $$< + $(TOUCH) $$@ + endif + else ifeq ($(OPENJDK_TARGET_OS), linux) + ifneq ($$($1_STRIP_POLICY), no_strip) + $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo + $$($1_DEBUGINFO_FILES): $$($1_TARGET) + $(RM) $$@ + $(OBJCOPY) --only-keep-debug $$< $$@ + $(CD) $$(@D) && $(OBJCOPY) --add-gnu-debuglink=$$(@F) $$< + $(TOUCH) $$@ + endif + else ifeq ($(OPENJDK_TARGET_OS), aix) + ifneq ($$($1_STRIP_POLICY), no_strip) + # AIX does not provide the equivalent of OBJCOPY to extract debug symbols, + # so we copy the compiled object with symbols to the .debuginfo file, which + # happens prior to the STRIP on the original target object file. + $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo + $$($1_DEBUGINFO_FILES): $$($1_TARGET) + $(RM) $$@ + $(CP) $$< $$@ + $(TOUCH) $$@ + endif + else ifeq ($(OPENJDK_TARGET_OS), macosx) + ifneq ($$($1_STRIP_POLICY), no_strip) + $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM/Contents/Info.plist \ + $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM/Contents/Resources/DWARF/$$($1_BASENAME) + $$($1_DEBUGINFO_FILES): $$($1_TARGET) + $(DSYMUTIL) --out $$($1_OBJECT_DIR)/$$($1_BASENAME).dSYM $$< + endif + endif + + ifeq ($(ZIP_DEBUGINFO_FILES), true) + ifneq ($$($1_STRIP_POLICY), no_strip) + $1_DEBUGINFO_ZIP := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz + $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_ZIP)) + + # The dependency on TARGET is needed on windows for debuginfo files + # to be rebuilt properly. + $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET) + $(CD) $$($1_OBJECT_DIR) \ + && $(ZIP) -q $$@ $$(subst $$($1_OBJECT_DIR)/,,$$($1_DEBUGINFO_FILES)) + endif else - $$(call $$($1_BUILD_INFO_LOG_MACRO), \ - $$(strip Updating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET)) \ - $$(if $$(filter-out %.vardeps, $$?), \ - due to $$(words $$(filter-out %.vardeps, $$?)) file(s), \ - $$(if $$(filter %.vardeps, $$?), due to makefile changes)))) + ifneq ($$($1_STRIP_POLICY), no_strip) + $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_FILES)) + endif endif - $(TOUCH) $$@ -endef + endif # ENABLE_DEBUG_SYMBOLS + endif # $1_DEBUG_SYMBOLS + endif # !STATIC_LIBRARY + + ifneq (,$$($1_LIBRARY)) + # Generating a dynamic library. + $1_EXTRA_LDFLAGS+=$$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME)) + ifeq ($(OPENJDK_TARGET_OS), windows) + $1_EXTRA_LDFLAGS+="-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib" + endif -################################################################################ -# Setup a library-wide dependency file from individual object file dependency -# files -define CreateDependencyFile - # Create a rule to collect all the individual make dependency files into a - # single makefile. - $1_DEPS_FILE := $$($1_OBJECT_DIR)/$1.d - - $$($1_DEPS_FILE): $$($1_ALL_OBJS) $$($1_RES) - $(RM) $$@ - # CD into dir to reduce risk of hitting command length limits, which - # could otherwise happen if TOPDIR is a very long path. - $(CD) $$($1_OBJECT_DIR) && $(CAT) *.d > $$@.tmp - $(CD) $$($1_OBJECT_DIR) && $(CAT) *.d.targets | $(SORT) -u >> $$@.tmp - # After generating the file, which happens after all objects have been - # compiled, copy it to .old extension. On the next make invocation, this - # .old file will be included by make. - $(CP) $$@.tmp $$@.old - $(MV) $$@.tmp $$@ - - $1 += $$($1_DEPS_FILE) -endef + $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX) + + $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) + $$(call LINKING_MSG,$$($1_BASENAME)) + $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(LD_OUT_OPTION)$$@ \ + $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) \ + $$($1_EXTRA_LDFLAGS_SUFFIX) + # Touch target to make sure it has a later time stamp than the debug + # symbol files to avoid unnecessary relinking on rebuild. + ifeq ($(OPENJDK_TARGET_OS), windows) + $(TOUCH) $$@ + endif -################################################################################ -# Import the dependency file into the makefile -define ImportDependencyFile - # The include must be on the .old file, which represents the state from the - # previous invocation of make. The file being included must not have a rule - # defined for it as otherwise make will think it has to run the rule before - # being able to include the file, which would be wrong since we specifically - # need the file as it was generated by a previous make invocation. - ifneq ($$(wildcard $$($1_DEPS_FILE).old), ) - $1_DEPS_FILE_LOADED := true - -include $$($1_DEPS_FILE).old endif -endef -endif # _NATIVE_COMPILATION_GMK + ifneq (,$$($1_STATIC_LIBRARY)) + # Generating a static library, ie object file archive. + $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES) + $$(call ARCHIVING_MSG,$$($1_LIBRARY)) + $(AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_EXPECTED_OBJS) \ + $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) + endif + + ifneq (,$$($1_PROGRAM)) + # A executable binary has been specified, setup the target for it. + $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX) + + $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_GEN_MANIFEST) + $$(call LINKING_EXE_MSG,$$($1_BASENAME)) + $$($1_LDEXE) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(EXE_OUT_OPTION)$$($1_TARGET) \ + $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) \ + $$($1_EXTRA_LDFLAGS_SUFFIX) + ifneq (,$$($1_GEN_MANIFEST)) + $(MT) -nologo -manifest $$($1_GEN_MANIFEST) -outputresource:$$@;#1 + endif + # This only works if the openjdk_codesign identity is present on the system. Let + # silently fail otherwise. + ifneq (,$(CODESIGN)) + ifneq (,$$($1_CODESIGN)) + $(CODESIGN) -s openjdk_codesign $$@ + endif + endif + # Touch target to make sure it has a later time stamp than the debug + # symbol files to avoid unnecessary relinking on rebuild. + ifeq ($(OPENJDK_TARGET_OS), windows) + $(TOUCH) $$@ + endif + + endif +endef