Skip to content

Commit 1ca7644

Browse files
magicusjianglizhou
andcommitted
8339480: Build static-jdk image with a statically linked launcher
Co-authored-by: Magnus Ihse Bursie <[email protected]> Co-authored-by: Jiangli Zhou <[email protected]> Reviewed-by: dholmes, erikj, coleenp
1 parent d589baf commit 1ca7644

File tree

27 files changed

+473
-97
lines changed

27 files changed

+473
-97
lines changed

make/Images.gmk

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -281,27 +281,6 @@ else
281281
endif
282282
CMDS_TARGET_SUBDIR := bin
283283

284-
# Param 1 - dir to find debuginfo files in
285-
FindDebuginfoFiles = \
286-
$(wildcard $(addprefix $1/*, $(DEBUGINFO_SUFFIXES)) \
287-
$(addprefix $1/*/*, $(DEBUGINFO_SUFFIXES)) \
288-
$(addprefix $1/*/*/*, $(DEBUGINFO_SUFFIXES)))
289-
290-
# Pick the correct debug info files to copy, either zipped or not.
291-
ifeq ($(ZIP_EXTERNAL_DEBUG_SYMBOLS), true)
292-
DEBUGINFO_SUFFIXES += .diz
293-
else
294-
DEBUGINFO_SUFFIXES := .debuginfo .pdb .map
295-
# On Macosx, if debug symbols have not been zipped, find all files inside *.dSYM
296-
# dirs.
297-
ifeq ($(call isTargetOs, macosx), true)
298-
$(call FillFindCache, \
299-
$(SUPPORT_OUTPUTDIR)/modules_libs $(SUPPORT_OUTPUTDIR)/modules_cmds)
300-
FindDebuginfoFiles = \
301-
$(if $(wildcard $1), $(call containing, .dSYM/, $(call FindFiles, $1)))
302-
endif
303-
endif
304-
305284
# Param 1 - either JDK or JRE
306285
SetupCopyDebuginfo = \
307286
$(foreach m, $(ALL_$1_MODULES), \

make/Main.gmk

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,18 @@ $(eval $(call SetupTarget, symbols-image, \
454454
TARGET := symbols, \
455455
))
456456

457+
$(eval $(call SetupTarget, static-launcher, \
458+
MAKEFILE := StaticLibs, \
459+
TARGET := static-launcher, \
460+
DEPS := hotspot-static-libs static-libs, \
461+
))
462+
463+
$(eval $(call SetupTarget, static-jdk-image, \
464+
MAKEFILE := StaticLibs, \
465+
TARGET := static-jdk-image, \
466+
DEPS := static-exploded-image jdk-image, \
467+
))
468+
457469
$(eval $(call SetupTarget, static-libs-image, \
458470
MAKEFILE := StaticLibsImage, \
459471
TARGET := static-libs-image, \
@@ -1086,9 +1098,9 @@ else
10861098

10871099
symbols-image: $(LIBS_TARGETS) $(LAUNCHER_TARGETS)
10881100

1089-
static-libs-image: hotspot-static-libs $(STATIC_LIBS_TARGETS)
1101+
static-libs-image: hotspot-static-libs static-libs
10901102

1091-
static-libs-graal-image: $(STATIC_LIBS_TARGETS)
1103+
static-libs-graal-image: static-libs
10921104

10931105
bootcycle-images: jdk-image
10941106

@@ -1254,6 +1266,8 @@ ifeq ($(call isTargetOs, macosx), true)
12541266
legacy-images: mac-legacy-jre-bundle
12551267
endif
12561268

1269+
static-exploded-image: static-launcher exploded-image
1270+
12571271
# These targets build the various documentation images
12581272
docs-jdk-image: docs-jdk
12591273
docs-javase-image: docs-javase
@@ -1296,7 +1310,7 @@ endif
12961310
################################################################################
12971311

12981312
# all-images builds all our deliverables as images.
1299-
all-images: product-images test-image all-docs-images
1313+
all-images: product-images static-jdk-image test-image all-docs-images
13001314

13011315
# all-bundles packages all our deliverables as tar.gz bundles.
13021316
all-bundles: product-bundles test-bundles docs-bundles static-libs-bundles
@@ -1309,7 +1323,7 @@ ALL_TARGETS += buildtools hotspot hotspot-libs hotspot-static-libs \
13091323
create-buildjdk docs-jdk-api docs-javase-api docs-reference-api docs-jdk \
13101324
docs-javase docs-reference docs-javadoc mac-bundles product-images legacy-images \
13111325
docs-image docs-javase-image docs-reference-image all-docs-images \
1312-
docs-bundles all-docs-bundles test-image all-images \
1326+
docs-bundles all-docs-bundles test-image all-images static-exploded-image \
13131327
all-bundles
13141328

13151329
################################################################################

make/ModuleWrapper.gmk

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2014, 2024, 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
@@ -45,6 +45,23 @@ TARGETS :=
4545
# Include the file being wrapped.
4646
include $(MAKEFILE_PREFIX).gmk
4747

48+
ifeq ($(MAKEFILE_PREFIX), Lib)
49+
# We need to keep track of what libraries are generated/needed by this
50+
# module. This information is required when doing static linking, to know
51+
# which static library files to include. The variable $(MODULE)_INCLUDED_LIBS is
52+
# added to for each call to SetupJdkLibrary. The file module-included-libs.txt is then
53+
# read in StaticLibs.gmk.
54+
ifneq ($($(MODULE)_JDK_LIBS), )
55+
LIBLIST := $(SUPPORT_OUTPUTDIR)/modules_static-libs/$(MODULE)/module-included-libs.txt
56+
57+
$(LIBLIST): $(TARGETS)
58+
$(call MakeDir, $(@D))
59+
$(ECHO) $($(MODULE)_INCLUDED_LIBS) > $@
60+
61+
TARGETS += $(LIBLIST)
62+
endif
63+
endif
64+
4865
# Setup copy rules from the modules directories to the jdk image directory.
4966
ifeq ($(call isTargetOs, windows), true)
5067
TO_BIN_FILTER := %$(SHARED_LIBRARY_SUFFIX) %.diz %.pdb %.map

make/StaticLibs.gmk

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#
2+
# Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
#
5+
# This code is free software; you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License version 2 only, as
7+
# published by the Free Software Foundation. Oracle designates this
8+
# particular file as subject to the "Classpath" exception as provided
9+
# by Oracle in the LICENSE file that accompanied this code.
10+
#
11+
# This code is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
# version 2 for more details (a copy is included in the LICENSE file that
15+
# accompanied this code).
16+
#
17+
# You should have received a copy of the GNU General Public License version
18+
# 2 along with this work; if not, write to the Free Software Foundation,
19+
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
#
21+
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
# or visit www.oracle.com if you need additional information or have any
23+
# questions.
24+
#
25+
26+
default: all
27+
28+
include $(SPEC)
29+
include MakeBase.gmk
30+
31+
include CopyFiles.gmk
32+
include Modules.gmk
33+
include modules/LauncherCommon.gmk
34+
35+
################################################################################
36+
#
37+
# Create the static java launcher
38+
#
39+
################################################################################
40+
41+
STATIC_JDK_IMAGE_DIR := $(IMAGES_OUTPUTDIR)/static-jdk
42+
STATIC_LAUNCHER_OUTPUT_DIR := $(SUPPORT_OUTPUTDIR)/static-native/launcher
43+
HOTSPOT_STATIC_LIB_PATH := $(HOTSPOT_OUTPUTDIR)/*/libjvm/objs/static
44+
45+
ifneq ($(word 2, $(wildcard $(HOTSPOT_STATIC_LIB_PATH))), )
46+
$(error Cannot perform static linking when building more than one JVM library)
47+
endif
48+
49+
# Find all modules with static libraries
50+
STATIC_LIB_MODULES := $(patsubst $(SUPPORT_OUTPUTDIR)/modules_static-libs/%, \
51+
%, $(wildcard $(SUPPORT_OUTPUTDIR)/modules_static-libs/*))
52+
53+
# Filter out known broken libraries. This is a temporary measure until
54+
# proper support for these libraries can be provided.
55+
ifeq ($(call isTargetOs, linux), true)
56+
# libsplashscreen has a name conflict with libawt in the function
57+
# BitmapToYXBandedRectangles, so we exclude it for now.
58+
BROKEN_STATIC_LIBS += splashscreen
59+
else ifeq ($(call isTargetOs, macosx), true)
60+
# libosxsecurity has a name conflict with libosxapp in the function
61+
# JavaStringToNSString, so we exclude it for now.
62+
BROKEN_STATIC_LIBS += osxsecurity
63+
else ifeq ($(call isTargetOs, windows), true)
64+
# libsplashscreen has a name conflict with libawt in the function
65+
# BitmapToYXBandedRectangles, so we exclude it for now.
66+
BROKEN_STATIC_LIBS += splashscreen
67+
# libsspi_bridge has name conflicts with sunmscapi
68+
BROKEN_STATIC_LIBS += sspi_bridge
69+
# These libs define DllMain which conflict with Hotspot
70+
BROKEN_STATIC_LIBS += awt dt_shmem dt_socket
71+
# These libs are dependent on any of the above disabled libs
72+
BROKEN_STATIC_LIBS += fontmanager jawt lcms net nio
73+
endif
74+
75+
$(foreach module, $(STATIC_LIB_MODULES), \
76+
$(eval LIBS_$(module) := $(filter-out $(BROKEN_STATIC_LIBS), $(shell cat \
77+
$(SUPPORT_OUTPUTDIR)/modules_static-libs/$(module)/module-included-libs.txt))) \
78+
)
79+
80+
STATIC_LIB_FILES := $(foreach module, $(STATIC_LIB_MODULES), \
81+
$(foreach lib, $(LIBS_$(module)), \
82+
$(SUPPORT_OUTPUTDIR)/native/$(module)/lib$(lib)/static/$(LIBRARY_PREFIX)$(lib)$(STATIC_LIBRARY_SUFFIX)))
83+
84+
# Add Hotspot
85+
STATIC_LIB_FILES += $(wildcard $(HOTSPOT_STATIC_LIB_PATH)/$(LIBRARY_PREFIX)jvm$(STATIC_LIBRARY_SUFFIX))
86+
87+
# Figure out what external libraries are required to link these static JDK
88+
# libraries.
89+
LIB_FLAGS_FILES := $(addsuffix .lib-flags.txt, $(STATIC_LIB_FILES))
90+
91+
# Gather the lib flags from all individual libraries. There are many duplicates,
92+
# so sort and just keep unique instances. On macOS, a common pattern is
93+
# "-framework FooFramework", so we must make sure we keep the two words together.
94+
EXTERNAL_LIBS := $(strip $(shell $(CAT) $(LIB_FLAGS_FILES) | \
95+
$(SED) -e 's/-framework /-framework_/g' | $(TR) ' ' '\n' | $(SORT) -u | \
96+
$(SED) -e 's/-framework_/-framework /g'))
97+
98+
ifeq ($(call isTargetOs, macosx), true)
99+
STATIC_LIBS := $(addprefix -force_load$(SPACE), $(STATIC_LIB_FILES))
100+
STANDARD_LIBS += -lstdc++
101+
else ifeq ($(call isTargetOs, linux), true)
102+
STATIC_LIBS := -Wl,--export-dynamic -Wl,--whole-archive $(STATIC_LIB_FILES) -Wl,--no-whole-archive
103+
STANDARD_LIBS := -l:libstdc++.a
104+
else ifeq ($(call isTargetOs, windows), true)
105+
STATIC_LIBS := $(addprefix -wholearchive:, $(STATIC_LIB_FILES))
106+
else
107+
$(error Unsupported platform)
108+
endif
109+
110+
$(eval $(call SetupBuildLauncher, java, \
111+
CFLAGS := -DEXPAND_CLASSPATH_WILDCARDS -DENABLE_ARG_FILES, \
112+
EXTRA_RCFLAGS := $(JAVA_RCFLAGS), \
113+
VERSION_INFO_RESOURCE := $(JAVA_VERSION_INFO_RESOURCE), \
114+
OPTIMIZATION := HIGH, \
115+
STATIC_LAUNCHER := true, \
116+
LDFLAGS := $(LDFLAGS_STATIC_JDK), \
117+
LIBS := $(STATIC_LIBS) $(EXTERNAL_LIBS) $(STANDARD_LIBS), \
118+
OUTPUT_DIR := $(STATIC_LAUNCHER_OUTPUT_DIR), \
119+
OBJECT_DIR := $(STATIC_LAUNCHER_OUTPUT_DIR), \
120+
))
121+
122+
$(java): $(STATIC_LIB_FILES)
123+
124+
TARGETS += $(java)
125+
126+
JAVA_LAUNCHER := $(BUILD_LAUNCHER_java_TARGET)
127+
128+
static-launcher: $(java)
129+
130+
################################################################################
131+
#
132+
# Create the static-jdk image with the statically built java launcher
133+
#
134+
################################################################################
135+
136+
# Until we get proper support in jlink for generating an image with static
137+
# builds, we need to create the image ourselves. We base it on a normal
138+
# dynamically linked JDK image.
139+
140+
# All these files/dirs should be copied as-is
141+
JDK_IMAGE_COPY_FILES := $(addprefix $(JDK_IMAGE_DIR)/, conf demo include jmods \
142+
legal man/man1/java.1 release README)
143+
144+
# We need to copy some files from lib, but not the dynamic libraries themselves
145+
ALL_LIB_FILES := $(call FindFiles, $(JDK_IMAGE_DIR)/lib)
146+
147+
# Remove all dynamic libraries from the list
148+
JDK_IMAGE_COPY_LIB_FILES := $(filter-out %$(SHARED_LIBRARY_SUFFIX), $(ALL_LIB_FILES))
149+
# Remove all debug files from the list
150+
ifeq ($(call isTargetOs, macosx), true)
151+
JDK_IMAGE_COPY_LIB_FILES := $(call not-containing, .dSYM, $(JDK_IMAGE_COPY_LIB_FILES))
152+
else
153+
JDK_IMAGE_COPY_LIB_FILES := $(filter-out %.debuginfo %.pdb %.map, $(JDK_IMAGE_COPY_LIB_FILES))
154+
endif
155+
156+
static-jdk-info:
157+
$(call LogWarn, Creating static-jdk image)
158+
159+
$(eval $(call SetupCopyFiles, copy-from-jdk-image, \
160+
SRC := $(JDK_IMAGE_DIR), \
161+
DEST := $(STATIC_JDK_IMAGE_DIR), \
162+
FILES := $(call FindFiles, $(JDK_IMAGE_COPY_FILES)) \
163+
$(JDK_IMAGE_COPY_LIB_FILES), \
164+
))
165+
166+
TARGETS += $(copy-from-jdk-image)
167+
168+
$(copy-from-jdk-image): | static-jdk-info
169+
170+
$(eval $(call SetupCopyFiles, copy-static-launcher, \
171+
FILES := $(JAVA_LAUNCHER), \
172+
DEST := $(STATIC_JDK_IMAGE_DIR)/bin, \
173+
))
174+
175+
TARGETS += $(copy-static-launcher)
176+
177+
$(eval $(call SetupCopyFiles, copy-static-launcher-debuginfo, \
178+
SRC := $(STATIC_LAUNCHER_OUTPUT_DIR), \
179+
DEST := $(STATIC_JDK_IMAGE_DIR)/bin, \
180+
FILES := $(call FindDebuginfoFiles, $(STATIC_LAUNCHER_OUTPUT_DIR)), \
181+
))
182+
183+
TARGETS += $(copy-static-launcher-debuginfo)
184+
185+
static-jdk-image: $(copy-from-jdk-image) $(copy-static-launcher) $(copy-static-launcher-debuginfo)
186+
187+
TARGETS += static-jdk-image
188+
189+
all: $(TARGETS)
190+
191+
.PHONY: all static-launcher static-jdk-image

make/autoconf/flags-ldflags.m4

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,23 @@ AC_DEFUN([FLAGS_SETUP_LDFLAGS_CPU_DEP],
192192
# Export variables according to old definitions, prefix with $2 if present.
193193
LDFLAGS_JDK_COMMON="$BASIC_LDFLAGS $BASIC_LDFLAGS_JDK_ONLY \
194194
$OS_LDFLAGS $DEBUGLEVEL_LDFLAGS_JDK_ONLY ${$2EXTRA_LDFLAGS}"
195-
$2LDFLAGS_JDKLIB="$LDFLAGS_JDK_COMMON $BASIC_LDFLAGS_JDK_LIB_ONLY \
195+
$2LDFLAGS_JDKLIB="$LDFLAGS_JDK_COMMON \
196196
$SHARED_LIBRARY_FLAGS $REPRODUCIBLE_LDFLAGS $FILE_MACRO_LDFLAGS"
197197
$2LDFLAGS_JDKEXE="$LDFLAGS_JDK_COMMON $EXECUTABLE_LDFLAGS \
198198
${$1_CPU_EXECUTABLE_LDFLAGS} $REPRODUCIBLE_LDFLAGS $FILE_MACRO_LDFLAGS"
199199
200+
$2LDFLAGS_STATIC_JDK="$BASIC_LDFLAGS $BASIC_LDFLAGS_JVM_ONLY \
201+
$OS_LDFLAGS ${$2EXTRA_LDFLAGS} $REPRODUCIBLE_LDFLAGS $FILE_MACRO_LDFLAGS"
202+
200203
$2JVM_LDFLAGS="$BASIC_LDFLAGS $BASIC_LDFLAGS_JVM_ONLY $OS_LDFLAGS $OS_LDFLAGS_JVM_ONLY \
201-
$DEBUGLEVEL_LDFLAGS $DEBUGLEVEL_LDFLAGS_JVM_ONLY $BASIC_LDFLAGS_ONLYCXX \
204+
$DEBUGLEVEL_LDFLAGS $DEBUGLEVEL_LDFLAGS_JVM_ONLY \
202205
${$1_CPU_LDFLAGS} ${$1_CPU_LDFLAGS_JVM_ONLY} ${$2EXTRA_LDFLAGS} \
203206
$REPRODUCIBLE_LDFLAGS $FILE_MACRO_LDFLAGS"
204207
205208
AC_SUBST($2LDFLAGS_JDKLIB)
206209
AC_SUBST($2LDFLAGS_JDKEXE)
207210
211+
AC_SUBST($2LDFLAGS_STATIC_JDK)
212+
208213
AC_SUBST($2JVM_LDFLAGS)
209214
])

make/autoconf/jdk-options.m4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_CODE_COVERAGE],
369369
CXXFLAGS_JDKEXE="$CXXFLAGS_JDKEXE $GCOV_CFLAGS"
370370
LDFLAGS_JDKLIB="$LDFLAGS_JDKLIB $GCOV_LDFLAGS"
371371
LDFLAGS_JDKEXE="$LDFLAGS_JDKEXE $GCOV_LDFLAGS"
372+
LDFLAGS_STATIC_JDK="$LDFLAGS_STATIC_JDK $GCOV_LDFLAGS"
372373
])
373374
AC_SUBST(GCOV_ENABLED)
374375
@@ -463,6 +464,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_ADDRESS_SANITIZER],
463464
CXXFLAGS_JDKEXE="$CXXFLAGS_JDKEXE $ASAN_CFLAGS"
464465
LDFLAGS_JDKLIB="$LDFLAGS_JDKLIB $ASAN_LDFLAGS"
465466
LDFLAGS_JDKEXE="$LDFLAGS_JDKEXE $ASAN_LDFLAGS"
467+
LDFLAGS_STATIC_JDK="$LDFLAGS_STATIC_JDK $ASAN_LDFLAGS"
466468
])
467469
AC_SUBST(ASAN_ENABLED)
468470
])
@@ -496,6 +498,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_LEAK_SANITIZER],
496498
CXXFLAGS_JDKEXE="$CXXFLAGS_JDKEXE $LSAN_CFLAGS"
497499
LDFLAGS_JDKLIB="$LDFLAGS_JDKLIB $LSAN_LDFLAGS"
498500
LDFLAGS_JDKEXE="$LDFLAGS_JDKEXE $LSAN_LDFLAGS"
501+
LDFLAGS_STATIC_JDK="$LDFLAGS_STATIC_JDK $LSAN_LDFLAGS"
499502
])
500503
AC_SUBST(LSAN_ENABLED)
501504
])
@@ -538,6 +541,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_UNDEFINED_BEHAVIOR_SANITIZER],
538541
CXXFLAGS_JDKEXE="$CXXFLAGS_JDKEXE $UBSAN_CFLAGS"
539542
LDFLAGS_JDKLIB="$LDFLAGS_JDKLIB $UBSAN_LDFLAGS"
540543
LDFLAGS_JDKEXE="$LDFLAGS_JDKEXE $UBSAN_LDFLAGS"
544+
LDFLAGS_STATIC_JDK="$LDFLAGS_STATIC_JDK $UBSAN_LDFLAGS"
541545
])
542546
if test "x$UBSAN_ENABLED" = xfalse; then
543547
UBSAN_CFLAGS=""

make/autoconf/spec.gmk.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ LDFLAGS_JDKLIB := @LDFLAGS_JDKLIB@
559559
# LDFLAGS used to link the jdk native launchers (C-code)
560560
LDFLAGS_JDKEXE := @LDFLAGS_JDKEXE@
561561

562+
# LDFLAGS used to link the static jdk library
563+
LDFLAGS_STATIC_JDK := @LDFLAGS_STATIC_JDK@
564+
562565
# LDFLAGS specific to C++ linking.
563566
LDFLAGS_CXX_JDK := @LDFLAGS_CXX_JDK@
564567

0 commit comments

Comments
 (0)