Skip to content

Commit f6866e7

Browse files
committed
[SR-648] Update -static-stdlib option on Linux with libicu changes
- Create a file of linker arguments instead of a hardcoded list in ToolChains.cpp for use by -static-stdlib option
1 parent 1daa3ee commit f6866e7

File tree

3 files changed

+83
-17
lines changed

3 files changed

+83
-17
lines changed

lib/Driver/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,33 @@ add_swift_library(swiftDriver STATIC
1919
DEPENDS SwiftOptions
2020
LINK_LIBRARIES swiftAST swiftBasic swiftFrontend swiftOption)
2121

22+
# Generate the static-stdlib-args.lnk file used by -static-stdlib option
23+
# for 'GenericUnix' (eg linux)
24+
if(SWIFT_BUILD_STATIC_STDLIB)
25+
set(static_stdlib_lnk_file_list)
26+
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
27+
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
28+
string(TOLOWER "${sdk}" lowercase_sdk)
29+
if(SWIFT_LINUX_ICU_STATICLIB)
30+
set(ICU_STATICLIB "TRUE")
31+
else()
32+
set(ICU_STATICLIB "FALSE")
33+
endif()
34+
set(linkfile "${lowercase_sdk}/static-stdlib-args.lnk")
35+
add_custom_command_target(swift_static_stdlib_${sdk}_args
36+
COMMAND
37+
"${SWIFT_SOURCE_DIR}/utils/gen-static-stdlib-link-args"
38+
"${sdk}"
39+
"${SWIFTSTATICLIB_DIR}/${linkfile}"
40+
"${ICU_STATICLIB}"
41+
OUTPUT
42+
"${SWIFTSTATICLIB_DIR}/${linkfile}")
43+
44+
list(APPEND static_stdlib_lnk_file_list ${swift_static_stdlib_${sdk}_args})
45+
swift_install_in_component(stdlib
46+
FILES "${SWIFTSTATICLIB_DIR}/${linkfile}"
47+
DESTINATION "lib/swift_static/${lowercase_sdk}")
48+
endif()
49+
endforeach()
50+
add_custom_target(swift_static_lnk_args ALL DEPENDS ${static_stdlib_lnk_file_list})
51+
endif()

lib/Driver/ToolChains.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,24 +1386,15 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
13861386
SmallString<128> StaticRuntimeLibPath;
13871387
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
13881388
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
1389-
// The following libraries are required to build a satisfactory
1390-
// static program
1391-
Arguments.push_back("-ldl");
1392-
Arguments.push_back("-lpthread");
1393-
Arguments.push_back("-lbsd");
1394-
Arguments.push_back("-licui18n");
1395-
Arguments.push_back("-licuuc");
1396-
// The runtime uses dlopen to look for the protocol conformances.
1397-
// Therefore, we need to ensure they appear in the dynamic table.
1398-
// This happens automatically for dynamically-linked programs, but
1399-
// in this case we have to take additional measures.
1400-
Arguments.push_back("-Xlinker");
1401-
Arguments.push_back("-export-dynamic");
1402-
Arguments.push_back("-Xlinker");
1403-
Arguments.push_back("--exclude-libs");
1404-
Arguments.push_back("-Xlinker");
1405-
Arguments.push_back("ALL");
14061389

1390+
SmallString<128> linkFilePath = StaticRuntimeLibPath;
1391+
llvm::sys::path::append(linkFilePath, "static-stdlib-args.lnk");
1392+
auto linkFile = linkFilePath.str();
1393+
if (llvm::sys::fs::is_regular_file(linkFile)) {
1394+
Arguments.push_back(context.Args.MakeArgString(Twine("@") + linkFile));
1395+
} else {
1396+
llvm::report_fatal_error(linkFile + " not found");
1397+
}
14071398
}
14081399
else {
14091400
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));

utils/gen-static-stdlib-link-args

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# 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
7+
#
8+
9+
SDK=$1
10+
OUTPUTFILE=$2
11+
ICU_STATIC_LIB=$3
12+
13+
14+
if [ "${ICU_STATIC_LIB}" == "TRUE" ]; then
15+
read -d '' ICU_LIBS <<EOF
16+
-licui18n
17+
-licuuc
18+
-licudata
19+
EOF
20+
else
21+
read -d '' ICU_LIBS <<EOF
22+
-licui18n
23+
-licuuc
24+
EOF
25+
fi
26+
27+
28+
if [ -z "${OUTPUTFILE}" ]; then
29+
echo $0: No outputfile specified
30+
exit 1
31+
fi
32+
33+
cat >$OUTPUTFILE <<EOF
34+
-ldl
35+
-lpthread
36+
-lswiftCore
37+
-lswiftImageInspectionShared
38+
$ICU_LIBS
39+
-Xlinker
40+
-export-dynamic
41+
-Xlinker
42+
--exclude-libs
43+
-Xlinker
44+
ALL
45+
EOF

0 commit comments

Comments
 (0)