Skip to content

Commit ec17253

Browse files
authored
Merge pull request swiftlang#68907 from al45tair/eng/PR-115278959-5.10
[Linux] Provide a statically linked swift-backtrace binary.
2 parents 381ba34 + e91f914 commit ec17253

File tree

5 files changed

+141
-12
lines changed

5 files changed

+141
-12
lines changed

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,8 +1530,14 @@ function(add_swift_target_library_single target name)
15301530
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
15311531
target_link_directories(${target_static} PRIVATE
15321532
${library_search_directories})
1533+
1534+
_list_add_string_suffix(
1535+
"${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES}"
1536+
"-static"
1537+
target_private_libs)
1538+
15331539
target_link_libraries("${target_static}" PRIVATE
1534-
${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES})
1540+
${target_private_libs})
15351541

15361542
# Force executables linker language to be CXX so that we do not link using the
15371543
# host toolchain swiftc.
@@ -2551,7 +2557,8 @@ endfunction()
25512557
# The Swift installation component that this executable belongs to.
25522558
# Defaults to never_install.
25532559
function(_add_swift_target_executable_single name)
2554-
set(options)
2560+
set(options
2561+
NOSWIFTRT)
25552562
set(single_parameter_options
25562563
ARCHITECTURE
25572564
SDK
@@ -2629,6 +2636,15 @@ function(_add_swift_target_executable_single name)
26292636
${SWIFTEXE_SINGLE_SOURCES}
26302637
${SWIFTEXE_SINGLE_EXTERNAL_SOURCES})
26312638

2639+
# ELF and COFF need swiftrt
2640+
if(("${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF" OR
2641+
"${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "COFF")
2642+
AND NOT SWIFTEXE_SINGLE_NOSWIFTRT)
2643+
target_sources(${name}
2644+
PRIVATE
2645+
$<TARGET_OBJECTS:swiftImageRegistrationObject${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}-${SWIFTEXE_SINGLE_ARCHITECTURE}>)
2646+
endif()
2647+
26322648
add_dependencies_multiple_targets(
26332649
TARGETS "${name}"
26342650
DEPENDS
@@ -2692,6 +2708,24 @@ function(_add_swift_target_executable_single name)
26922708
set_target_properties(${name} PROPERTIES FOLDER "Swift executables")
26932709
endfunction()
26942710

2711+
# Conditionally append -static to a name, if that variant is a valid target
2712+
function(append_static name result_var_name)
2713+
cmake_parse_arguments(APPEND_TARGET
2714+
"STATIC_SWIFT_STDLIB"
2715+
""
2716+
""
2717+
${ARGN})
2718+
if(STATIC_SWIFT_STDLIB)
2719+
if(TARGET "${name}-static")
2720+
set("${result_var_name}" "${name}-static" PARENT_SCOPE)
2721+
else()
2722+
set("${result_var_name}" "${name}" PARENT_SCOPE)
2723+
endif()
2724+
else()
2725+
set("${result_var_name}" "${name}" PARENT_SCOPE)
2726+
endif()
2727+
endfunction()
2728+
26952729
# Add an executable for each target variant. Executables are given suffixes
26962730
# with the variant SDK and ARCH.
26972731
#
@@ -2700,7 +2734,9 @@ function(add_swift_target_executable name)
27002734
set(SWIFTEXE_options
27012735
EXCLUDE_FROM_ALL
27022736
BUILD_WITH_STDLIB
2703-
BUILD_WITH_LIBEXEC)
2737+
BUILD_WITH_LIBEXEC
2738+
PREFER_STATIC
2739+
NOSWIFTRT)
27042740
set(SWIFTEXE_single_parameter_options
27052741
INSTALL_IN_COMPONENT)
27062742
set(SWIFTEXE_multiple_parameter_options
@@ -2886,8 +2922,12 @@ function(add_swift_target_executable name)
28862922
list(APPEND swiftexe_module_dependency_targets
28872923
"swift${mod}${MODULE_VARIANT_SUFFIX}")
28882924

2889-
list(APPEND swiftexe_link_libraries_targets
2890-
"swift${mod}${VARIANT_SUFFIX}")
2925+
set(library_target "swift${mod}${VARIANT_SUFFIX}")
2926+
if(SWIFTEXE_TARGET_PREFER_STATIC AND TARGET "${library_target}-static")
2927+
set(library_target "${library_target}-static")
2928+
endif()
2929+
2930+
list(APPEND swiftexe_link_libraries_targets "${library_target}")
28912931
endforeach()
28922932

28932933
# Don't add the ${arch} to the suffix. We want to link against fat
@@ -2899,6 +2939,7 @@ function(add_swift_target_executable name)
28992939

29002940
_add_swift_target_executable_single(
29012941
${VARIANT_NAME}
2942+
${SWIFTEXE_TARGET_NOSWIFTRT_keyword}
29022943
${SWIFTEXE_TARGET_SOURCES}
29032944
DEPENDS
29042945
${SWIFTEXE_TARGET_DEPENDS_with_suffix}

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -905,11 +905,6 @@ function(_compile_swift_files
905905

906906
# First generate the obj dirs
907907
list(REMOVE_DUPLICATES dirs_to_create)
908-
add_custom_command_target(
909-
create_dirs_dependency_target
910-
COMMAND "${CMAKE_COMMAND}" -E make_directory ${dirs_to_create}
911-
OUTPUT ${dirs_to_create}
912-
COMMENT "Generating dirs for ${first_output}")
913908

914909
# Then we can compile both the object files and the swiftmodule files
915910
# in parallel in this target for the object file, and ...
@@ -939,6 +934,7 @@ function(_compile_swift_files
939934

940935
add_custom_command_target(
941936
dependency_target
937+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${dirs_to_create}
942938
COMMAND
943939
${set_environment_args}
944940
"$<TARGET_FILE:Python3::Interpreter>" "${line_directive_tool}" "@${file_path}" --

stdlib/public/Backtracing/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ add_swift_target_library(swift_Backtracing ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
7878

7979
SWIFT_MODULE_DEPENDS ${concurrency}
8080

81-
LINK_LIBRARIES ${swift_backtracing_link_libraries}
81+
PRIVATE_LINK_LIBRARIES ${swift_backtracing_link_libraries}
8282

8383
SWIFT_COMPILE_FLAGS
8484
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}

stdlib/public/libexec/swift-backtrace/CMakeLists.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ set(BACKTRACING_COMPILE_FLAGS
2626
"-Xcc;-I${SWIFT_SOURCE_DIR}/include"
2727
"-Xcc;-I${CMAKE_BINARY_DIR}/include")
2828

29-
add_swift_target_executable(swift-backtrace BUILD_WITH_LIBEXEC
29+
set(BACKTRACING_SOURCES
3030
main.swift
3131
AnsiColor.swift
3232
TargetMacOS.swift
3333
TargetLinux.swift
3434
Themes.swift
3535
Utils.swift
36+
)
37+
38+
39+
add_swift_target_executable(swift-backtrace BUILD_WITH_LIBEXEC
40+
${BACKTRACING_SOURCES}
3641

3742
SWIFT_MODULE_DEPENDS ${backtracing}
3843

@@ -47,3 +52,22 @@ add_swift_target_executable(swift-backtrace BUILD_WITH_LIBEXEC
4752

4853
TARGET_SDKS OSX LINUX)
4954

55+
if(SWIFT_BUILD_STATIC_STDLIB)
56+
add_swift_target_executable(swift-backtrace-static BUILD_WITH_LIBEXEC
57+
PREFER_STATIC
58+
59+
${BACKTRACING_SOURCES}
60+
61+
SWIFT_MODULE_DEPENDS ${backtracing}
62+
63+
SWIFT_MODULE_DEPENDS_OSX ${darwin}
64+
SWIFT_MODULE_DEPENDS_WINDOWS ${wincrt_sdk}
65+
SWIFT_MODULE_DEPENDS_LINUX ${glibc}
66+
67+
INSTALL_IN_COMPONENT libexec
68+
COMPILE_FLAGS
69+
${BACKTRACING_COMPILE_FLAGS}
70+
-parse-as-library
71+
72+
TARGET_SDKS LINUX)
73+
endif()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -parse-as-library %import-static-libdispatch -Onone -g -o %t/StaticBacktracer
3+
// RUN: %target-codesign %t/StaticBacktracer
4+
// RUN: /usr/bin/ldd %backtracer-static | %FileCheck %s --check-prefix LIBS
5+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no,swift-backtrace=%backtracer-static %target-run %t/StaticBacktracer 2>&1 || true) | %FileCheck %s
6+
7+
// UNSUPPORTED: use_os_stdlib
8+
// UNSUPPORTED: back_deployment_runtime
9+
// UNSUPPORTED: asan
10+
// REQUIRES: executable_test
11+
// REQUIRES: backtracing
12+
// REQUIRES: static_stdlib
13+
// REQUIRES: OS=linux-gnu
14+
15+
func level1() {
16+
level2()
17+
}
18+
19+
func level2() {
20+
level3()
21+
}
22+
23+
func level3() {
24+
level4()
25+
}
26+
27+
func level4() {
28+
level5()
29+
}
30+
31+
func level5() {
32+
print("About to crash")
33+
let ptr = UnsafeMutablePointer<Int>(bitPattern: 4)!
34+
ptr.pointee = 42
35+
}
36+
37+
@main
38+
struct StaticBacktracer {
39+
static func main() {
40+
level1()
41+
}
42+
}
43+
44+
// CHECK: *** Program crashed: Bad pointer dereference at 0x{{0+}}4 ***
45+
46+
// CHECK: Thread 0 {{(".*" )?}}crashed:
47+
48+
// CHECK: 0 0x{{[0-9a-f]+}} level5() + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift:34:15
49+
// CHECK-NEXT: 1 [ra] 0x{{[0-9a-f]+}} level4() + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift:28:3
50+
// CHECK-NEXT: 2 [ra] 0x{{[0-9a-f]+}} level3() + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift:24:3
51+
// CHECK-NEXT: 3 [ra] 0x{{[0-9a-f]+}} level2() + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift:20:3
52+
// CHECK-NEXT: 4 [ra] 0x{{[0-9a-f]+}} level1() + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift:16:3
53+
// CHECK-NEXT: 5 [ra] 0x{{[0-9a-f]+}} static StaticBacktracer.main() + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift:40:5
54+
// CHECK-NEXT: 6 [ra] [system] 0x{{[0-9a-f]+}} static StaticBacktracer.$main() + {{[0-9]+}} in StaticBacktracer at {{.*}}/<compiler-generated>
55+
// CHECK-NEXT: 7 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in StaticBacktracer at {{.*}}/StaticBacktracer.swift
56+
57+
// CHECK: Registers:
58+
59+
// CHECK: Images ({{[0-9]+}} omitted):
60+
61+
// CHECK: {{0x[0-9a-f]+}}–{{0x[0-9a-f]+}}{{ +}}{{([0-9a-f]+|<no build ID>)}}{{ +}}StaticBacktracer{{ +}}{{.*}}/StaticBacktracer
62+
63+
// .............................................................................
64+
65+
// We mustn't have any Swift libraries dynamically linked here; that's the
66+
// entire point.
67+
68+
// LIBS-NOT: libswift{{.*}}.so

0 commit comments

Comments
 (0)