Skip to content

Commit ab0dbf8

Browse files
spevansslavapestov
authored andcommitted
SR-2280: swiftc -static-stdlib option fails on Linux (#5269)
- Link in static version of libicu if available in preference to the dynamic library when using the -static-stdlib option.
1 parent 5d8bfb0 commit ab0dbf8

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

lib/Driver/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ if(SWIFT_BUILD_STATIC_STDLIB)
3030
set(ICU_STATICLIB "TRUE")
3131
else()
3232
set(ICU_STATICLIB "FALSE")
33+
find_package(ICU REQUIRED COMPONENTS uc i18n)
34+
get_filename_component(ICU_UC_LIBDIR "${ICU_UC_LIBRARY}" DIRECTORY)
35+
get_filename_component(ICU_I18N_LIBDIR "${ICU_I18N_LIBRARY}" DIRECTORY)
3336
endif()
3437
set(linkfile "${lowercase_sdk}/static-stdlib-args.lnk")
3538
add_custom_command_target(swift_static_stdlib_${sdk}_args
@@ -38,6 +41,8 @@ if(SWIFT_BUILD_STATIC_STDLIB)
3841
"${sdk}"
3942
"${SWIFTSTATICLIB_DIR}/${linkfile}"
4043
"${ICU_STATICLIB}"
44+
"${ICU_UC_LIBDIR}"
45+
"${ICU_I18N_LIBDIR}"
4146
OUTPUT
4247
"${SWIFTSTATICLIB_DIR}/${linkfile}")
4348

utils/gen-static-stdlib-link-args

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,62 @@
22

33
#
44
# Generate the static-stdlib-args.lnk used by the -static-stdlib option for
5-
# 'GenericUnix' (eg linux) plaforms. If libicu is built locally then include
6-
# libicudata
5+
# 'GenericUnix' (eg linux) plaforms. Tries to find static .a files for libs
6+
# not normally installed by default (currently libicu)
7+
# If libicu is built locally then include libicudata
78
#
89

910
# SDK=$1
1011
OUTPUTFILE=$2
1112
ICU_STATIC_LIB=$3
13+
# libdirs from pkg-config
14+
ICU_UC_LIBDIR=$4
15+
ICU_I18N_LIBDIR=$5
1216

1317

14-
if [ "${ICU_STATIC_LIB}" == "TRUE" ]; then
15-
read -r -d '' ICU_LIBS <<EOF
18+
# Try and find the libicu .a library files to directly link in using the
19+
# fullpath to the files otherwise fullback to the default
20+
function find_static_iculibs {
21+
if [ -n "${ICU_I18N_LIBDIR}" ]; then
22+
LIBICU_I18N_A=${ICU_I18N_LIBDIR}/libicui18n.a
23+
fi
24+
25+
if [ -n "${ICU_UC_LIBDIR}" ]; then
26+
LIBICU_UC_A=${ICU_UC_LIBDIR}/libicuuc.a
27+
LIBICU_DATA_A=${ICU_UC_LIBDIR}/libicudata.a
28+
fi
29+
30+
if [ -f "${LIBICU_I18N_A}" ] && [ -f "${LIBICU_UC_A}" ] &&
31+
[ -f "${LIBICU_DATA_A}" ]; then
32+
read -d '' ICU_LIBS <<EOF
33+
$LIBICU_I18N_A
34+
$LIBICU_UC_A
35+
$LIBICU_DATA_A
36+
EOF
37+
else
38+
read -d '' ICU_LIBS <<EOF
1639
-licui18n
1740
-licuuc
18-
-licudata
1941
EOF
20-
else
21-
read -r -d '' ICU_LIBS <<EOF
42+
fi
43+
}
44+
45+
46+
# If libicu was compiled as part of the swift build then link the libs as normal
47+
function use_local_iculibs {
48+
read -d '' ICU_LIBS <<EOF
2249
-licui18n
2350
-licuuc
51+
-licudata
2452
EOF
25-
fi
26-
53+
}
2754

28-
if [ -z "${OUTPUTFILE}" ]; then
29-
echo $0: No outputfile specified
30-
exit 1
31-
fi
3255

56+
function write_linkfile {
57+
if [ -z "${OUTPUTFILE}" ]; then
58+
echo $0: No outputfile specified
59+
exit 1
60+
fi
3361
cat >$OUTPUTFILE <<EOF
3462
-ldl
3563
-lpthread
@@ -43,3 +71,12 @@ $ICU_LIBS
4371
-Xlinker
4472
ALL
4573
EOF
74+
}
75+
76+
77+
if [ "${ICU_STATIC_LIB}" == "TRUE" ]; then
78+
use_local_iculibs
79+
else
80+
find_static_iculibs
81+
fi
82+
write_linkfile

0 commit comments

Comments
 (0)