From 960d0e30ee2b70ba3bb7ec076c16045afab01589 Mon Sep 17 00:00:00 2001 From: Simon Evans Date: Fri, 7 Aug 2020 09:15:52 +0100 Subject: [PATCH] Linux: Fix -static-executable and remove swiftImageInspectionShared - Remove references to swiftImageInspectionShared on Linux and dont have split libraries between static/non-static linked executables. - -static-executable now links correctly Linux. Note: swift::lookupSymbol() will not work on statically linked executables but this can be worked around by using the https://github.com/swift-server/swift-backtrace package. --- stdlib/public/core/CMakeLists.txt | 2 +- stdlib/public/runtime/CMakeLists.txt | 52 ----------------------- test/Driver/static-executable-linux.swift | 10 +++++ unittests/runtime/CMakeLists.txt | 7 --- utils/static-executable-args.lnk | 7 ++- 5 files changed, 14 insertions(+), 64 deletions(-) create mode 100644 test/Driver/static-executable-linux.swift diff --git a/stdlib/public/core/CMakeLists.txt b/stdlib/public/core/CMakeLists.txt index ad74e0e699039..7f8c8a62c2482 100644 --- a/stdlib/public/core/CMakeLists.txt +++ b/stdlib/public/core/CMakeLists.txt @@ -274,7 +274,7 @@ elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL FREEBSD) ${SWIFTLIB_DIR}/clang/lib/freebsd/libclang_rt.builtins-${SWIFT_PRIMARY_VARIANT_ARCH}.a) elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL LINUX) if(SWIFT_BUILD_STATIC_STDLIB) - list(APPEND swift_core_private_link_libraries swiftImageInspectionShared) + list(APPEND swift_core_private_link_libraries) endif() elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL WINDOWS) list(APPEND swift_core_private_link_libraries shell32;DbgHelp) diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index 4b51b04fec37b..56d5110ef94d8 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -81,48 +81,9 @@ list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/stdlib/inc set(sdk "${SWIFT_HOST_VARIANT_SDK}") if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX") - list(REMOVE_ITEM swift_runtime_sources ImageInspectionELF.cpp) set(static_binary_lnk_file_list) string(TOLOWER "${sdk}" lowercase_sdk) - # These two libraries are only used with the static swiftcore - add_swift_target_library(swiftImageInspectionShared STATIC - ImageInspectionELF.cpp - C_COMPILE_FLAGS ${swift_runtime_library_compile_flags} - LINK_FLAGS ${swift_runtime_linker_flags} - SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} - INSTALL_IN_COMPONENT stdlib) - - foreach(arch IN LISTS SWIFT_SDK_${sdk}_ARCHITECTURES) - set(FragileSupportLibrary swiftImageInspectionShared-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}) - set(LibraryLocation ${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${arch}) - add_custom_command_target(swift_image_inspection_${arch}_static - COMMAND - "${CMAKE_COMMAND}" -E copy $ ${LibraryLocation} - OUTPUT - "${LibraryLocation}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspectionShared${CMAKE_STATIC_LIBRARY_SUFFIX}" - DEPENDS - ${FragileSupportLibrary}) - add_dependencies(stdlib ${FragileSupportLibrary}) - swift_install_in_component(FILES $ - DESTINATION "lib/swift_static/${lowercase_sdk}/${arch}" - COMPONENT stdlib) - endforeach() - - set(FragileSupportLibraryPrimary swiftImageInspectionShared-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${SWIFT_PRIMARY_VARIANT_ARCH}) - set(LibraryLocationPrimary ${SWIFTSTATICLIB_DIR}/${lowercase_sdk}) - add_custom_command_target(swift_image_inspection_static_primary_arch - COMMAND - "${CMAKE_COMMAND}" -E copy $ ${LibraryLocationPrimary} - OUTPUT - "${LibraryLocationPrimary}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspectionShared${CMAKE_STATIC_LIBRARY_SUFFIX}" - DEPENDS - ${FragileSupportLibraryPrimary}) - add_dependencies(stdlib ${FragileSupportLibraryPrimary}) - swift_install_in_component(FILES $ - DESTINATION "lib/swift_static/${lowercase_sdk}" - COMPONENT stdlib) - # Generate the static-executable-args.lnk file used for ELF systems (eg linux) set(linkfile "${lowercase_sdk}/static-executable-args.lnk") add_custom_command_target(swift_static_binary_${sdk}_args @@ -140,18 +101,6 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX") DESTINATION "lib/swift_static/${lowercase_sdk}" COMPONENT stdlib) add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list}) - foreach(arch IN LISTS SWIFT_SDK_LINUX_ARCHITECTURES) - add_dependencies(static_binary_magic ${swift_image_inspection_${arch}_static}) - endforeach() - add_dependencies(static_binary_magic ${swift_image_inspection_static_primary_arch}) - add_dependencies(stdlib static_binary_magic) - - add_swift_target_library(swiftImageInspectionSharedObject OBJECT_LIBRARY - ImageInspectionELF.cpp - C_COMPILE_FLAGS ${swift_runtime_library_compile_flags} - LINK_FLAGS ${swift_runtime_linker_flags} - SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} - INSTALL_IN_COMPONENT never_install) endif() add_swift_target_library(swiftRuntime OBJECT_LIBRARY @@ -280,7 +229,6 @@ foreach(sdk ${SWIFT_CONFIGURED_SDKS}) -ldl -lpthread -lswiftCore --lswiftImageInspectionShared ${libicu_i18n_a} ${libicu_uc_a} ${libicu_data_a} diff --git a/test/Driver/static-executable-linux.swift b/test/Driver/static-executable-linux.swift new file mode 100644 index 0000000000000..8f0589aeccf36 --- /dev/null +++ b/test/Driver/static-executable-linux.swift @@ -0,0 +1,10 @@ +// Build a static executable "hello world" program +// REQUIRES: OS=linux-gnu +// REQUIRES: static_stdlib +print("hello world!") +// RUN: %empty-directory(%t) +// RUN: %target-swiftc_driver -static-executable -o %t/static-executable %s +// RUN: %t/static-executable | %FileCheck %s +// RUN: file %t/static-executable | %FileCheck %s --check-prefix=FILE +// CHECK: hello world! +// FILE: , statically linked, diff --git a/unittests/runtime/CMakeLists.txt b/unittests/runtime/CMakeLists.txt index 3ca27587f9e7b..33fa05245c412 100644 --- a/unittests/runtime/CMakeLists.txt +++ b/unittests/runtime/CMakeLists.txt @@ -28,12 +28,6 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND endif() endif() - set(swift_runtime_test_extra_libraries) - if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX") - list(APPEND swift_runtime_test_extra_libraries - $) - endif() - add_subdirectory(LongTests) set(PLATFORM_SOURCES) @@ -94,7 +88,6 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND PRIVATE swiftCore${SWIFT_PRIMARY_VARIANT_SUFFIX} ${PLATFORM_TARGET_LINK_LIBRARIES} - ${swift_runtime_test_extra_libraries} ) endif() diff --git a/utils/static-executable-args.lnk b/utils/static-executable-args.lnk index 98b814164c242..5e9d5c0c46014 100644 --- a/utils/static-executable-args.lnk +++ b/utils/static-executable-args.lnk @@ -1,12 +1,11 @@ -static -lswiftCore --lswiftImageInspectionShared -Xlinker ---defsym=__import_pthread_self=pthread_self +-undefined=pthread_self -Xlinker ---defsym=__import_pthread_once=pthread_once +-undefined=pthread_once -Xlinker ---defsym=__import_pthread_key_create=pthread_key_create +-undefined=pthread_key_create -lpthread -licui18nswift -licuucswift