2
# Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
3
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
# This code is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 2 only, as
7
# published by the Free Software Foundation. Oracle designates this
8
# particular file as subject to the "Classpath" exception as provided
9
# by Oracle in the LICENSE file that accompanied this code.
11
# This code is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
# version 2 for more details (a copy is included in the LICENSE file that
15
# accompanied this code).
17
# You should have received a copy of the GNU General Public License version
18
# 2 along with this work; if not, write to the Free Software Foundation,
19
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
# or visit www.oracle.com if you need additional information or have any
26
################################################################################
27
# This file contains functionality related to linking a native binary;
28
# creating either a dynamic library, a static library or an executable.
30
################################################################################
32
# Find entitlements file for executable when signing on macosx. If no
33
# specialized file is found, returns the default file.
34
# This macro might be called from custom makefiles.
35
# $1 Executable to find entitlements file for.
36
ENTITLEMENTS_DIR := $(TOPDIR)/make/data/macosxsigning
37
ifeq ($(MACOSX_CODESIGN_MODE), debug)
38
CODESIGN_PLIST_SUFFIX := -debug
40
CODESIGN_PLIST_SUFFIX :=
42
DEFAULT_ENTITLEMENTS_FILE := $(ENTITLEMENTS_DIR)/default$(CODESIGN_PLIST_SUFFIX).plist
44
GetEntitlementsFile = \
45
$(foreach f, $(ENTITLEMENTS_DIR)/$(strip $(notdir $1))$(CODESIGN_PLIST_SUFFIX).plist, \
46
$(if $(wildcard $f), $f, $(DEFAULT_ENTITLEMENTS_FILE)) \
49
################################################################################
51
# Unless specifically set, stripping should only happen if symbols are also
53
$$(call SetIfEmpty, $1_STRIP_SYMBOLS, $$($1_COPY_DEBUG_SYMBOLS))
55
# Default to using the global STRIPFLAGS. Allow for overriding with an
57
$1_STRIPFLAGS ?= $(STRIPFLAGS)
60
################################################################################
61
define CreateLinkedResult
62
ifeq ($$($1_TYPE), STATIC_LIBRARY)
63
$$(eval $$(call CreateStaticLibrary,$1))
65
$$(eval $$(call CreateDynamicLibraryOrExecutable,$1))
69
################################################################################
70
define CreateStaticLibrary
71
# Include partial linking when building the static library with clang on linux
72
ifeq ($(call isTargetOs, linux), true)
73
ifneq ($(findstring $(TOOLCHAIN_TYPE), clang), )
74
$1_ENABLE_PARTIAL_LINKING := true
78
$1_VARDEPS := $$($1_AR) $$(ARFLAGS) $$($1_LIBS) \
80
ifeq ($$($1_ENABLE_PARTIAL_LINKING), true)
81
$1_VARDEPS += $$($1_LD) $$($1_SYSROOT_LDFLAGS)
83
$1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
84
$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
86
$1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_EXTRA_LINK_DEPS) $$($1_VARDEPS_FILE)
88
$1_AR_OBJ_ARG := $$($1_LD_OBJ_ARG)
89
# With clang on linux, partial linking is enabled and 'AR' takes the output
90
# object from the partial linking step.
91
ifeq ($$($1_ENABLE_PARTIAL_LINKING), true)
92
$1_TARGET_RELOCATABLE := $$($1_OBJECT_DIR)/$$($1_PREFIX)$$($1_NAME)_relocatable$(OBJ_SUFFIX)
93
$1_AR_OBJ_ARG := $$($1_TARGET_RELOCATABLE)
96
$$($1_TARGET): $$($1_TARGET_DEPS)
97
ifneq ($$($1_OBJ_FILE_LIST), )
98
ifeq ($$($1_LINK_OBJS_RELATIVE), true)
99
$$(eval $$(call ListPathsSafely, $1_ALL_OBJS_RELATIVE, $$($1_OBJ_FILE_LIST)))
101
$$(eval $$(call ListPathsSafely, $1_ALL_OBJS, $$($1_OBJ_FILE_LIST)))
104
$$(call LogInfo, Building static library $$($1_BASENAME))
105
$$(call MakeDir, $$($1_OUTPUT_DIR) $$($1_SYMBOLS_DIR))
106
# Do partial linking.
107
ifeq ($$($1_ENABLE_PARTIAL_LINKING), true)
108
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_partial_link, \
109
$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
110
$$($1_LD) $(LDFLAGS_CXX_PARTIAL_LINKING) $$($1_SYSROOT_LDFLAGS) \
111
-o $$($1_TARGET_RELOCATABLE) $$($1_LD_OBJ_ARG))
113
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_run_ar, \
114
$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
115
$$($1_AR) $$(ARFLAGS) -r -cs $$($1_TARGET) \
116
$$($1_AR_OBJ_ARG) $$($1_RES))
119
################################################################################
120
define CreateDynamicLibraryOrExecutable
121
# A shared dynamic library or an executable binary has been specified
122
ifeq ($$($1_TYPE), LIBRARY)
123
# Generating a dynamic library.
124
$1_EXTRA_LDFLAGS += $$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
127
ifeq ($(MACOSX_CODESIGN_MODE), hardened)
128
$1_CODESIGN_OPTS := "$(MACOSX_CODESIGN_IDENTITY)" --timestamp \
130
else ifeq ($(MACOSX_CODESIGN_MODE), debug)
131
$1_CODESIGN_OPTS := -
134
$1_VARDEPS := $$($1_LD) $$($1_SYSROOT_LDFLAGS) $$($1_LDFLAGS) \
135
$$($1_EXTRA_LDFLAGS) $$($1_LIBS) $$($1_EXTRA_LIBS) \
136
$$($1_DEBUGINFO_FILES) $$($1_STRIPFLAGS)
137
$1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
138
$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
140
$1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_EXTRA_LINK_DEPS) $$($1_VARDEPS_FILE)
142
$$($1_TARGET): $$($1_TARGET_DEPS)
143
ifneq ($$($1_OBJ_FILE_LIST), )
144
ifeq ($$($1_LINK_OBJS_RELATIVE), true)
145
$$(eval $$(call ListPathsSafely, $1_ALL_OBJS_RELATIVE, $$($1_OBJ_FILE_LIST)))
147
$$(eval $$(call ListPathsSafely, $1_ALL_OBJS, $$($1_OBJ_FILE_LIST)))
150
$$(call LogInfo, Linking $$($1_BASENAME))
151
$$(call MakeDir, $$($1_OUTPUT_DIR) $$($1_SYMBOLS_DIR))
152
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_run_ld, \
153
$$(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
154
$$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) \
155
$$($1_SYSROOT_LDFLAGS) -o $$($1_TARGET) $$($1_LD_OBJ_ARG) \
156
$$($1_LIBS) $$($1_EXTRA_LIBS))
157
ifeq ($$($1_CREATE_DEBUGINFO), true)
158
ifeq ($(call isTargetOs, linux), true)
159
# This cannot be run separately since it updates the original target
161
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_create_debuginfo, \
162
$$($1_OBJCOPY) --only-keep-debug $$($1_TARGET) $$($1_DEBUGINFO_FILES))
163
$$(CHMOD) -x $$($1_DEBUGINFO_FILES)
164
else ifeq ($(call isTargetOs, aix), true)
165
# AIX does not provide the equivalent of objcopy to extract debug
166
# symbols, so we copy unstripped library instead.
167
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_create_debuginfo, \
168
$(CP) $$($1_TARGET) $$($1_DEBUGINFO_FILES))
169
else ifeq ($(call isTargetOs, macosx), true)
170
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_create_debuginfo, \
171
$(DSYMUTIL) --out $$($1_SYMBOLS_DIR)/$$($1_BASENAME).dSYM $$($1_TARGET))
174
ifneq ($$($1_STRIP_SYMBOLS), false)
175
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_strip, \
176
$$($1_STRIP) $$($1_STRIPFLAGS) $$($1_TARGET))
178
ifeq ($$($1_CREATE_DEBUGINFO), true)
179
ifeq ($(call isTargetOs, linux), true)
180
# Run this after strip is called, since strip can sometimes mangle
181
# the embedded debuglink, which we want to avoid.
182
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_add_debuginfo_link, \
183
$(CD) $$($1_SYMBOLS_DIR) && \
184
$$($1_OBJCOPY) --add-gnu-debuglink=$$($1_DEBUGINFO_FILES) $$($1_TARGET))
187
ifneq ($(MACOSX_CODESIGN_MODE), disabled)
188
# Remove signature explicitly first to avoid warnings if the linker
189
# added a default adhoc signature.
190
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_codesign_clear, \
191
$(CODESIGN) --remove-signature $$@)
192
$$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_codesign_add, \
193
$(CODESIGN) -f -s $$($1_CODESIGN_OPTS) --entitlements \
194
$$(call GetEntitlementsFile, $$@) $$@)