Skip to content

Commit

Permalink
8326587: Separate out Microsoft toolchain linking
Browse files Browse the repository at this point in the history
Reviewed-by: erikj
  • Loading branch information
magicus committed Feb 26, 2024
1 parent 2d3c9c5 commit 3780ad3
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 107 deletions.
4 changes: 2 additions & 2 deletions make/Hsdis.gmk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2020, 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
Expand Down Expand Up @@ -122,8 +122,8 @@ ifeq ($(HSDIS_BACKEND), binutils)

TOOLCHAIN_TYPE := gcc
OPENJDK_TARGET_OS := linux
OPENJDK_TARGET_OS_TYPE := unix
CC_OUT_OPTION := -o$(SPACE)
LD_OUT_OPTION := -o$(SPACE)
GENDEPS_FLAGS := -MMD -MF
CFLAGS_DEBUG_SYMBOLS := -g
DISABLED_WARNINGS :=
Expand Down
3 changes: 2 additions & 1 deletion make/autoconf/buildjdk-spec.gmk.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2015, 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
Expand Down Expand Up @@ -40,6 +40,7 @@ LDCXX := @BUILD_LDCXX@
AS := @BUILD_AS@
NM := @BUILD_NM@
AR := @BUILD_AR@
LIB := @BUILD_LIB@
OBJCOPY := @BUILD_OBJCOPY@
STRIP := @BUILD_STRIP@
SYSROOT_CFLAGS := @BUILD_SYSROOT_CFLAGS@
Expand Down
4 changes: 2 additions & 2 deletions make/autoconf/flags-ldflags.m4
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2011, 2023, 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
Expand Down Expand Up @@ -93,7 +93,7 @@ AC_DEFUN([FLAGS_SETUP_LDFLAGS_HELPER],
BASIC_LDFLAGS_JVM_ONLY="-Wl,-lC_r -bbigtoc"
elif test "x$TOOLCHAIN_TYPE" = xmicrosoft; then
BASIC_LDFLAGS="-nologo -opt:ref"
BASIC_LDFLAGS="-opt:ref"
BASIC_LDFLAGS_JDK_ONLY="-incremental:no"
BASIC_LDFLAGS_JVM_ONLY="-opt:icf,8 -subsystem:windows"
fi
Expand Down
17 changes: 13 additions & 4 deletions make/autoconf/flags-other.m4
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2011, 2023, 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
Expand Down Expand Up @@ -33,16 +33,25 @@ AC_DEFUN([FLAGS_SETUP_ARFLAGS],
# FIXME: figure out if we should select AR flags depending on OS or toolchain.
if test "x$OPENJDK_TARGET_OS" = xaix; then
ARFLAGS="-X64"
elif test "x$OPENJDK_TARGET_OS" = xwindows; then
# lib.exe is used as AR to create static libraries.
ARFLAGS="-nologo -NODEFAULTLIB:MSVCRT"
else
ARFLAGS=""
fi
AC_SUBST(ARFLAGS)
])

AC_DEFUN([FLAGS_SETUP_LIBFLAGS],
[
# LIB is used to create static libraries on Windows
if test "x$OPENJDK_TARGET_OS" = xwindows; then
LIBFLAGS="-nodefaultlib:msvcrt"
else
LIBFLAGS=""
fi
AC_SUBST(LIBFLAGS)
])

AC_DEFUN([FLAGS_SETUP_STRIPFLAGS],
[
## Setup strip.
Expand Down
15 changes: 2 additions & 13 deletions make/autoconf/flags.m4
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2011, 2023, 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
Expand Down Expand Up @@ -364,24 +364,12 @@ AC_DEFUN([FLAGS_SETUP_TOOLCHAIN_CONTROL],
if test "x$TOOLCHAIN_TYPE" = xmicrosoft; then
CC_OUT_OPTION=-Fo
LD_OUT_OPTION=-out:
AR_OUT_OPTION=-out:
else
# The option used to specify the target .o,.a or .so file.
# When compiling, how to specify the to be created object file.
CC_OUT_OPTION='-o$(SPACE)'
# When linking, how to specify the output
LD_OUT_OPTION='-o$(SPACE)'
# When archiving, how to specify the destination static archive.
if test "x$OPENJDK_TARGET_OS" = xmacosx; then
AR_OUT_OPTION='-r -cs$(SPACE)'
else
AR_OUT_OPTION='-rcs$(SPACE)'
fi
fi
AC_SUBST(CC_OUT_OPTION)
AC_SUBST(LD_OUT_OPTION)
AC_SUBST(AR_OUT_OPTION)
# Generate make dependency files
if test "x$TOOLCHAIN_TYPE" = xgcc; then
Expand Down Expand Up @@ -423,6 +411,7 @@ AC_DEFUN([FLAGS_SETUP_FLAGS],
FLAGS_SETUP_LDFLAGS
FLAGS_SETUP_ARFLAGS
FLAGS_SETUP_LIBFLAGS
FLAGS_SETUP_STRIPFLAGS
FLAGS_SETUP_RCFLAGS
FLAGS_SETUP_NMFLAGS
Expand Down
6 changes: 2 additions & 4 deletions make/autoconf/spec.gmk.template
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,6 @@ COMPILER_COMMAND_FILE_FLAG := @COMPILER_COMMAND_FILE_FLAG@
COMPILER_BINDCMD_FILE_FLAG := @COMPILER_BINDCMD_FILE_FLAG@

CC_OUT_OPTION := @CC_OUT_OPTION@
LD_OUT_OPTION := @LD_OUT_OPTION@
AR_OUT_OPTION := @AR_OUT_OPTION@

# Flags used for overriding the default opt setting for a C/C++ source file.
C_O_FLAG_HIGHEST_JVM := @C_O_FLAG_HIGHEST_JVM@
Expand Down Expand Up @@ -604,10 +602,10 @@ BUILD_SYSROOT_LDFLAGS := @BUILD_SYSROOT_LDFLAGS@

AS := @AS@

# AR is used to create a static library (is ar in unix, lib.exe in windows)
AR := @AR@
ARFLAGS := @ARFLAGS@

LIB := @LIB@
LIBFLAGS := @LIBFLAGS@
NM := @NM@
NMFLAGS := @NMFLAGS@
STRIP := @STRIP@
Expand Down
5 changes: 2 additions & 3 deletions make/autoconf/toolchain.m4
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,10 @@ AC_DEFUN_ONCE([TOOLCHAIN_DETECT_TOOLCHAIN_CORE],
AC_SUBST(AS)
#
# Setup the archiver (AR)
# Setup tools for creating static libraries (AR/LIB)
#
if test "x$TOOLCHAIN_TYPE" = xmicrosoft; then
# The corresponding ar tool is lib.exe (used to create static libraries)
UTIL_LOOKUP_TOOLCHAIN_PROGS(AR, lib)
UTIL_LOOKUP_TOOLCHAIN_PROGS(LIB, lib)
elif test "x$TOOLCHAIN_TYPE" = xgcc; then
UTIL_LOOKUP_TOOLCHAIN_PROGS(AR, ar gcc-ar)
else
Expand Down
16 changes: 13 additions & 3 deletions make/common/NativeCompilation.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ include native/CompileFile.gmk
include native/DebugSymbols.gmk
include native/Flags.gmk
include native/Link.gmk
include native/LinkMicrosoft.gmk
include native/Paths.gmk
include native/ToolchainDefinitions.gmk

Expand Down Expand Up @@ -72,7 +73,8 @@ include native/ToolchainDefinitions.gmk
# used both for C and C++.
# LIBS_<toolchain>_<OS> the libraries to link to for the specified target
# OS and toolchain, used both for C and C++.
# ARFLAGS the archiver flags to be used
# ARFLAGS the archiver flags to be used on unix platforms
# LIBFLAGS the flags for the lib tool used on windows
# 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
Expand Down Expand Up @@ -198,12 +200,20 @@ define SetupNativeCompilationBody

# Prepare for linking
$$(eval $$(call SetupLinkerFlags,$1))
$$(eval $$(call SetupLinking,$1))
ifneq ($(TOOLCHAIN_TYPE), microsoft)
$$(eval $$(call SetupLinking,$1))
else
$$(eval $$(call SetupLinkingMicrosoft,$1))
endif

$$(eval $$(call SetupObjectFileList,$1))

# Link the individually compiled files into a single unit
$$(eval $$(call CreateLinkedResult,$1))
ifneq ($(TOOLCHAIN_TYPE), microsoft)
$$(eval $$(call CreateLinkedResult,$1))
else
$$(eval $$(call CreateLinkedResultMicrosoft,$1))
endif

ifeq ($(GENERATE_COMPILE_COMMANDS_ONLY), true)
# Override all targets (this is a hack)
Expand Down
100 changes: 27 additions & 73 deletions make/common/native/Link.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ define SetupLinking
$$(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
# 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
endef

Expand All @@ -81,7 +80,7 @@ endef

################################################################################
define CreateStaticLibrary
# Include partial linking when building the static library with clang on linux.
# Include partial linking when building the static library with clang on linux
ifeq ($(call isTargetOs, linux), true)
ifneq ($(findstring $(TOOLCHAIN_TYPE), clang), )
$1_ENABLE_PARTIAL_LINKING := true
Expand All @@ -103,7 +102,8 @@ define CreateStaticLibrary
endif
endif

$1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_RES) $$($1_VARDEPS_FILE) $$(STATIC_MAPFILE_DEP)
$1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_RES) $$($1_VARDEPS_FILE) \
$$(STATIC_MAPFILE_DEP)

$1_AR_OBJ_ARG := $$($1_LD_OBJ_ARG)
# With clang on linux, partial linking is enabled and 'AR' takes the output
Expand All @@ -128,13 +128,13 @@ define CreateStaticLibrary
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_partial_link, \
$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
$$($1_LD) $(LDFLAGS_CXX_PARTIAL_LINKING) $$($1_SYSROOT_LDFLAGS) \
$(LD_OUT_OPTION)$$($1_TARGET_RELOCATABLE) \
-o $$($1_TARGET_RELOCATABLE) \
$$($1_LD_OBJ_ARG))
endif
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
$$($1_AR) $$(ARFLAGS) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_AR_OBJ_ARG) \
$$($1_RES))
$$($1_AR) $$(ARFLAGS) $$($1_ARFLAGS) -r -cs $$($1_TARGET) \
$$($1_AR_OBJ_ARG) $$($1_RES))
ifeq ($(STATIC_BUILD), true)
ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true)
$(CP) $$($1_MAPFILE) $$(@D)/$$(basename $$(@F)).symbols
Expand Down Expand Up @@ -174,39 +174,13 @@ define CreateDynamicLibraryOrExecutable
endif
endif

ifeq ($(call isTargetOs, windows), true)
ifeq ($$($1_EMBED_MANIFEST), true)
$1_EXTRA_LDFLAGS += -manifest:embed
endif

$1_IMPORT_LIBRARY := $$($1_OBJECT_DIR)/$$($1_NAME).lib
$1_EXTRA_LDFLAGS += "-implib:$$($1_IMPORT_LIBRARY)"
ifeq ($$($1_TYPE), LIBRARY)
# To properly trigger downstream dependants of the import library, just as
# for debug files, we must have a recipe in the rule. To avoid rerunning
# the recipe every time have it touch the target. If an import library
# file is deleted by something external, explicitly delete the target to
# trigger a rebuild of both.
ifneq ($$(wildcard $$($1_IMPORT_LIBRARY)), $$($1_IMPORT_LIBRARY))
$$(call LogDebug, Deleting $$($1_BASENAME) because import library is missing)
$$(shell $(RM) $$($1_TARGET))
endif
$$($1_IMPORT_LIBRARY): $$($1_TARGET)
$(TOUCH) $$@

$1 += $$($1_IMPORT_LIBRARY)
endif
endif

$1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
$$($1_LIBS) $$($1_EXTRA_LIBS) $$($1_MT) \
$$($1_CREATE_DEBUGINFO_CMDS) $$($1_MANIFEST_VERSION) \
$$($1_STRIP_CMD) $$($1_CREATE_DEBUGLINK_CMDS)
$1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) \
$$($1_EXTRA_LDFLAGS) $$($1_LIBS) $$($1_EXTRA_LIBS) \
$$($1_CREATE_DEBUGINFO_CMDS) $$($1_STRIP_CMD) $$($1_CREATE_DEBUGLINK_CMDS)
$1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)

$1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_RES) $$($1_MANIFEST) \
$$($1_REAL_MAPFILE) $$($1_VARDEPS_FILE)
$1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_REAL_MAPFILE) $$($1_VARDEPS_FILE)

$$($1_TARGET): $$($1_TARGET_DEPS)
ifneq ($$($1_OBJ_FILE_LIST), )
Expand All @@ -216,47 +190,27 @@ define CreateDynamicLibraryOrExecutable
$$(eval $$(call ListPathsSafely, $1_ALL_OBJS, $$($1_OBJ_FILE_LIST)))
endif
endif
# Keep as much as possible on one execution line for best performance
# on Windows
$$(call LogInfo, Linking $$($1_BASENAME))
$$(call MakeDir, $$($1_OUTPUT_DIR) $$($1_SYMBOLS_DIR))
ifeq ($(call isTargetOs, windows), true)
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
$$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
$(LD_OUT_OPTION)$$($1_TARGET) $$($1_LD_OBJ_ARG) $$($1_RES) \
$$($1_LIBS) $$($1_EXTRA_LIBS)) \
| $(GREP) -v "^ Creating library .*\.lib and object .*\.exp" || \
test "$$$$?" = "1" ; \
$$($1_CREATE_DEBUGINFO_CMDS)
$$($1_STRIP_CMD)
$$($1_CREATE_DEBUGLINK_CMDS)
ifeq ($(call isBuildOsEnv, windows.wsl2), true)
$$(CHMOD) +x $$($1_TARGET)
endif
else
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
$$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
$$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $$($1_SYSROOT_LDFLAGS) \
$(LD_OUT_OPTION)$$($1_TARGET) $$($1_LD_OBJ_ARG) $$($1_RES) \
$$($1_LIBS) $$($1_EXTRA_LIBS)) ; \
$$($1_CREATE_DEBUGINFO_CMDS)
$$($1_STRIP_CMD)
$$($1_CREATE_DEBUGLINK_CMDS)
endif
ifeq ($(call isTargetOs, windows), true)
ifneq ($$($1_MANIFEST), )
$$($1_MT) -nologo -manifest $$($1_MANIFEST) -identity:"$$($1_NAME).exe, version=$$($1_MANIFEST_VERSION)" -outputresource:$$@;#1
endif
endif
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
$$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
$$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
$$($1_SYSROOT_LDFLAGS) -o $$($1_TARGET) $$($1_LD_OBJ_ARG) \
$$($1_LIBS) $$($1_EXTRA_LIBS))
$$($1_CREATE_DEBUGINFO_CMDS)
$$($1_STRIP_CMD)
$$($1_CREATE_DEBUGLINK_CMDS)
# On macosx, optionally run codesign on every binary.
# Remove signature explicitly first to avoid warnings if the linker
# added a default adhoc signature.
ifeq ($(MACOSX_CODESIGN_MODE), hardened)
$(CODESIGN) --remove-signature $$@
$(CODESIGN) -f -s "$(MACOSX_CODESIGN_IDENTITY)" --timestamp --options runtime \
--entitlements $$(call GetEntitlementsFile, $$@) $$@
$(CODESIGN) -f -s "$(MACOSX_CODESIGN_IDENTITY)" --timestamp \
--options runtime --entitlements \
$$(call GetEntitlementsFile, $$@) $$@
else ifeq ($(MACOSX_CODESIGN_MODE), debug)
$(CODESIGN) --remove-signature $$@
$(CODESIGN) -f -s - --entitlements $$(call GetEntitlementsFile, $$@) $$@
$(CODESIGN) -f -s - --entitlements \
$$(call GetEntitlementsFile, $$@) $$@
endif
endef
Loading

1 comment on commit 3780ad3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.