11#
2- # Copyright (c) 2011, 2022 , Oracle and/or its affiliates. All rights reserved.
2+ # Copyright (c) 2011, 2023 , Oracle and/or its affiliates. All rights reserved.
33# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44#
55# This code is free software; you can redistribute it and/or modify it
@@ -548,6 +548,7 @@ endef
548548# PRECOMPILED_HEADER Header file to use as precompiled header
549549# PRECOMPILED_HEADER_EXCLUDE List of source files that should not use PCH
550550# BUILD_INFO_LOG_MACRO Overrides log level of the build info log message, default LogWarn
551+ # STATIC_LIB_EXCLUDE_OBJS exclude objects that matches from static library
551552#
552553# After being called, some variables are exported from this macro, all prefixed
553554# with parameter 1 followed by a '_':
@@ -726,6 +727,12 @@ define SetupNativeCompilationBody
726727 endif
727728 # Sort to remove duplicates and provide a reproducible order on the input files to the linker.
728729 $1_ALL_OBJS := $$(sort $$($1_EXPECTED_OBJS) $$($1_EXTRA_OBJECT_FILES))
730+ ifeq ($(STATIC_LIBS), true)
731+ # Exclude the object files that match with $1_STATIC_LIB_EXCLUDE_OBJS.
732+ ifneq ($$($1_STATIC_LIB_EXCLUDE_OBJS), )
733+ $1_ALL_OBJS := $$(call not-containing, $$($1_STATIC_LIB_EXCLUDE_OBJS), $$($1_ALL_OBJS))
734+ endif
735+ endif
729736
730737 # Pickup extra OPENJDK_TARGET_OS_TYPE, OPENJDK_TARGET_OS, TOOLCHAIN_TYPE and
731738 # OPENJDK_TARGET_OS plus OPENJDK_TARGET_CPU pair dependent variables for CFLAGS.
@@ -1130,9 +1137,60 @@ define SetupNativeCompilationBody
11301137 endif
11311138 endif
11321139
1140+ $1_LD_OBJ_ARG := $$($1_ALL_OBJS)
1141+
1142+ # If there are many object files, use an @-file...
1143+ ifneq ($$(word 17, $$($1_ALL_OBJS)), )
1144+ $1_OBJ_FILE_LIST := $$($1_OBJECT_DIR)/_$1_objectfilenames.txt
1145+ ifneq ($(COMPILER_COMMAND_FILE_FLAG), )
1146+ $1_LD_OBJ_ARG := $(COMPILER_COMMAND_FILE_FLAG)$$($1_OBJ_FILE_LIST)
1147+ else
1148+ # ...except for toolchains which don't support them.
1149+ $1_LD_OBJ_ARG := `cat $$($1_OBJ_FILE_LIST)`
1150+ endif
1151+
1152+ # If we are building static library, 'AR' on macosx may not support @-file.
1153+ ifeq ($$($1_TYPE), STATIC_LIBRARY)
1154+ ifeq ($(call isTargetOs, macosx), true)
1155+ $1_LD_OBJ_ARG := `cat $$($1_OBJ_FILE_LIST)`
1156+ endif
1157+ endif
1158+ endif
1159+
1160+ # Unfortunately the @-file trick does not work reliably when using clang.
1161+ # Clang does not propagate the @-file parameter to the ld sub process, but
1162+ # instead puts the full content on the command line. At least the llvm ld
1163+ # does not even support an @-file.
1164+ #
1165+ # When linking a large amount of object files, we risk hitting the limit
1166+ # of the command line length even on posix systems if the path length of
1167+ # the output dir is very long due to our use of absolute paths. To
1168+ # mitigate this, use paths relative to the output dir when linking over
1169+ # 500 files with clang and the output dir path is deep.
1170+ ifneq ($$(word 500, $$($1_ALL_OBJS)), )
1171+ ifeq ($$(TOOLCHAIN_TYPE), clang)
1172+ # There is no strlen function in make, but checking path depth is a
1173+ # reasonable approximation.
1174+ ifneq ($$(word 10, $$(subst /, ,$$(OUTPUTDIR))), )
1175+ $1_LINK_OBJS_RELATIVE := true
1176+ $1_ALL_OBJS_RELATIVE := $$(patsubst $$(OUTPUTDIR)/%, %, $$($1_ALL_OBJS))
1177+ endif
1178+ endif
1179+ endif
1180+
11331181 ifeq ($$($1_TYPE), STATIC_LIBRARY)
1182+ # Include partial linking when building the static library with clang on linux.
1183+ ifeq ($(call isTargetOs, linux), true)
1184+ ifneq ($(findstring $(TOOLCHAIN_TYPE), clang), )
1185+ $1_ENABLE_PARTIAL_LINKING := true
1186+ endif
1187+ endif
1188+
11341189 $1_VARDEPS := $$($1_AR) $$(ARFLAGS) $$($1_ARFLAGS) $$($1_LIBS) \
11351190 $$($1_EXTRA_LIBS)
1191+ ifeq ($$($1_ENABLE_PARTIAL_LINKING), true)
1192+ $1_VARDEPS += $$($1_LD) $$($1_SYSROOT_LDFLAGS)
1193+ endif
11361194 $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
11371195 $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
11381196
@@ -1145,11 +1203,35 @@ define SetupNativeCompilationBody
11451203
11461204 $1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_RES) $$($1_VARDEPS_FILE) $$(STATIC_MAPFILE_DEP)
11471205
1206+ $1_AR_OBJ_ARG := $$($1_LD_OBJ_ARG)
1207+ # With clang on linux, partial linking is enabled and 'AR' takes the output
1208+ # object from the partial linking step.
1209+ ifeq ($$($1_ENABLE_PARTIAL_LINKING), true)
1210+ $1_TARGET_RELOCATABLE := $$($1_OBJECT_DIR)/$$($1_PREFIX)$$($1_NAME)_relocatable$(OBJ_SUFFIX)
1211+ $1_AR_OBJ_ARG := $$($1_TARGET_RELOCATABLE)
1212+ endif
1213+
11481214 $$($1_TARGET): $$($1_TARGET_DEPS)
1215+ ifneq ($$($1_OBJ_FILE_LIST), )
1216+ ifeq ($$($1_LINK_OBJS_RELATIVE), true)
1217+ $$(eval $$(call ListPathsSafely, $1_ALL_OBJS_RELATIVE, $$($1_OBJ_FILE_LIST)))
1218+ else
1219+ $$(eval $$(call ListPathsSafely, $1_ALL_OBJS, $$($1_OBJ_FILE_LIST)))
1220+ endif
1221+ endif
11491222 $$(call LogInfo, Building static library $$($1_BASENAME))
11501223 $$(call MakeDir, $$($1_OUTPUT_DIR) $$($1_SYMBOLS_DIR))
1224+ # Do partial linking.
1225+ ifeq ($$($1_ENABLE_PARTIAL_LINKING), true)
1226+ $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_partial_link, \
1227+ $(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
1228+ $$($1_LD) $(LDFLAGS_CXX_PARTIAL_LINKING) $$($1_SYSROOT_LDFLAGS) \
1229+ $(LD_OUT_OPTION)$$($1_TARGET_RELOCATABLE) \
1230+ $$($1_LD_OBJ_ARG))
1231+ endif
11511232 $$(call ExecuteWithLog, $$($1_OBJECT_DIR)/$$($1_SAFE_NAME)_link, \
1152- $$($1_AR) $$(ARFLAGS) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_ALL_OBJS) \
1233+ $(if $$($1_LINK_OBJS_RELATIVE), $$(CD) $$(OUTPUTDIR) ; ) \
1234+ $$($1_AR) $$(ARFLAGS) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_AR_OBJ_ARG) \
11531235 $$($1_RES))
11541236 ifeq ($(STATIC_BUILD), true)
11551237 ifeq ($$($1_USE_MAPFILE_FOR_SYMBOLS), true)
@@ -1201,40 +1283,6 @@ define SetupNativeCompilationBody
12011283 $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS, \
12021284 $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).vardeps)
12031285
1204- $1_LD_OBJ_ARG := $$($1_ALL_OBJS)
1205-
1206- # If there are many object files, use an @-file...
1207- ifneq ($$(word 17, $$($1_ALL_OBJS)), )
1208- $1_OBJ_FILE_LIST := $$($1_OBJECT_DIR)/_$1_objectfilenames.txt
1209- ifneq ($(COMPILER_COMMAND_FILE_FLAG), )
1210- $1_LD_OBJ_ARG := $(COMPILER_COMMAND_FILE_FLAG)$$($1_OBJ_FILE_LIST)
1211- else
1212- # ...except for toolchains which don't support them.
1213- $1_LD_OBJ_ARG := `cat $$($1_OBJ_FILE_LIST)`
1214- endif
1215- endif
1216-
1217- # Unfortunately the @-file trick does not work reliably when using clang.
1218- # Clang does not propagate the @-file parameter to the ld sub process, but
1219- # instead puts the full content on the command line. At least the llvm ld
1220- # does not even support an @-file.
1221- #
1222- # When linking a large amount of object files, we risk hitting the limit
1223- # of the command line length even on posix systems if the path length of
1224- # the output dir is very long due to our use of absolute paths. To
1225- # mitigate this, use paths relative to the output dir when linking over
1226- # 500 files with clang and the output dir path is deep.
1227- ifneq ($$(word 500, $$($1_ALL_OBJS)), )
1228- ifeq ($$(TOOLCHAIN_TYPE), clang)
1229- # There is no strlen function in make, but checking path depth is a
1230- # reasonable approximation.
1231- ifneq ($$(word 10, $$(subst /, ,$$(OUTPUTDIR))), )
1232- $1_LINK_OBJS_RELATIVE := true
1233- $1_ALL_OBJS_RELATIVE := $$(patsubst $$(OUTPUTDIR)/%, %, $$($1_ALL_OBJS))
1234- endif
1235- endif
1236- endif
1237-
12381286 $1_TARGET_DEPS := $$($1_ALL_OBJS) $$($1_RES) $$($1_MANIFEST) \
12391287 $$($1_REAL_MAPFILE) $$($1_VARDEPS_FILE)
12401288
0 commit comments