Skip to content

Fix various linker paths, variables and dependencies related to architecture specific library paths #16194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 136 additions & 49 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,8 @@ function(_add_swift_lipo_target)
DEPENDS ${source_targets})
else()
# We don't know how to create fat binaries for other platforms.
add_custom_command_target(unused_var
COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${source_binaries}" "${LIPO_OUTPUT}"
CUSTOM_TARGET_NAME "${LIPO_TARGET}"
OUTPUT "${LIPO_OUTPUT}"
DEPENDS ${source_targets})
message(FATAL_ERROR
"It's not possible to build a universal binary for a non-MachO target")
endif()
endfunction()

Expand Down Expand Up @@ -1147,8 +1144,14 @@ function(_add_swift_library_single target name)
set(link_flags ${SWIFTLIB_SINGLE_LINK_FLAGS})
set(library_search_directories
"${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}")

if(IS_DARWIN)
# Since we lipo each arch together on Darwin, search in the common directory
list(APPEND library_search_directories
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
endif()


# Add variant-specific flags.
if(SWIFTLIB_SINGLE_TARGET_LIBRARY)
Expand Down Expand Up @@ -1289,8 +1292,12 @@ function(_add_swift_library_single target name)
COMPILE_FLAGS " ${c_compile_flags}")
set(library_search_directories
"${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}")
if(IS_DARWIN)
list(APPEND library_search_directories
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
endif()

swift_target_link_search_directories("${target_static}" "${library_search_directories}")
target_link_libraries("${target_static}" PRIVATE
${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES})
Expand Down Expand Up @@ -1592,6 +1599,7 @@ function(add_swift_library name)
continue()
endif()

is_darwin_based_sdk("${sdk}" IS_DARWIN)
set(THIN_INPUT_TARGETS)

# For each architecture supported by this SDK
Expand Down Expand Up @@ -1784,47 +1792,105 @@ function(add_swift_library name)
)

if(NOT SWIFTLIB_OBJECT_LIBRARY)
# Add dependencies on the (not-yet-created) custom lipo target.
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
if (NOT "${DEP}" STREQUAL "icucore")
add_dependencies(${VARIANT_NAME}
"${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
# On Darwin we use lipo'd libraries for dependencies.
# On other targets without universal binaries we just install the target lib
if (IS_DARWIN)
# Add dependencies on the (not-yet-created) custom lipo target.
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
if (NOT "${DEP}" STREQUAL "icucore")
add_dependencies(${VARIANT_NAME}
"${DEP}-${SWIFT_SDK_{sdk}_LIB_SUBDIR}")
endif()
endforeach()

if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
# Add dependencies on the (not-yet-created) custom lipo target.
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
if (NOT "${DEP}" STREQUAL "icucore")
add_dependencies("${VARIANT_NAME}-static"
"${DEP}-${SWIFT_SDK_${SDK}_LIB_SUBDIR}-static")
endif()
endforeach()
endif()
endforeach()

if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
# Add dependencies on the (not-yet-created) custom lipo target.
list(APPEND THIN_INPUT_TARGETS ${VARIANT_NAME})
else()
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
if (NOT "${DEP}" STREQUAL "icucore")
add_dependencies("${VARIANT_NAME}-static"
"${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-static")
add_dependencies(${VARIANT_NAME}
"${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
endif()
endforeach()
endif()

# Note this thin library.
list(APPEND THIN_INPUT_TARGETS ${VARIANT_NAME})
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
foreach(DEP ${SWIFTLIB_LINK_LIBRARIES})
if (NOT "${DEP}" STREQUAL "icucore")
add_dependencies("${VARIANT_NAME}-static"
"${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}-static")
endif()
endforeach()
endif()

if(SWIFTLIB_TARGET_LIBRARY)
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
set(VARIANT_SUFFIX "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
if(TARGET "swift-stdlib${VARIANT_SUFFIX}" AND TARGET "swift-test-stdlib${VARIANT_SUFFIX}")
add_dependencies("swift-stdlib${VARIANT_SUFFIX}"
"${VARIANT_NAME}")
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
add_dependencies("swift-stdlib${VARIANT_SUFFIX}"
"${VARIANT_NAME}-static")
endif()

if((NOT "${name}" STREQUAL "swiftStdlibCollectionUnittest") AND
(NOT "${name}" STREQUAL "swiftStdlibUnicodeUnittest"))
add_dependencies("swift-test-stdlib${VARIANT_SUFFIX}"
"${VARIANT_NAME}")
if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC)
add_dependencies("swift-test-stdlib${VARIANT_SUFFIX}"
"${VARIANT_NAME}-static")
endif()
endif()
endif()

if(SWIFTLIB_SHARED)
set(resource_dir "swift")
set(file_permissions
OWNER_READ OWNER_WRITE
GROUP_READ
WORLD_READ)
set(install_libpath
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
else()
set(resource_dir "swift_static")
set(file_permissions
OWNER_READ OWNER_WRITE
GROUP_READ
WORLD_READ)
set(install_libpath
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()

swift_install_in_component("${SWIFTLIB_INSTALL_IN_COMPONENT}"
FILES "${install_libpath}"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}"
PERMISSIONS ${file_permissions})
endforeach()
endif()
endif()
endif()
endforeach()


if(NOT SWIFTLIB_OBJECT_LIBRARY)
# Only Darwin supports universal binaries.
if (NOT SWIFTLIB_OBJECT_LIBRARY AND IS_DARWIN)
# Determine the name of the universal library.
if(SWIFTLIB_SHARED)
if("${sdk}" STREQUAL "WINDOWS")
set(UNIVERSAL_LIBRARY_NAME
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${name}.dll")
else()
set(UNIVERSAL_LIBRARY_NAME
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
endif()
set(UNIVERSAL_LIBRARY_NAME
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
else()
if("${sdk}" STREQUAL "WINDOWS")
set(UNIVERSAL_LIBRARY_NAME
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${name}.lib")
else()
set(UNIVERSAL_LIBRARY_NAME
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()
set(UNIVERSAL_LIBRARY_NAME
"${SWIFTLIB_DIR}/${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()

set(lipo_target "${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
Expand Down Expand Up @@ -1866,6 +1932,7 @@ function(add_swift_library name)
WORLD_READ)
endif()

# On Darwin we install the lipo'd universal binary
swift_install_in_component("${SWIFTLIB_INSTALL_IN_COMPONENT}"
FILES "${UNIVERSAL_LIBRARY_NAME}"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}"
Expand Down Expand Up @@ -2039,9 +2106,16 @@ function(_add_swift_executable_single name)
set(c_compile_flags)
set(link_flags)

# Prepare linker search directories.
set(library_search_directories
# Prepare linker search directories. On Darwin, we want the fat library paths. On
# non-Darwin we want the architecture specific versions.
is_darwin_based_sdk("${SWIFTEXE_SINGLE_SDK}" IS_DARWIN)
if (IS_DARWIN)
set(library_search_directories
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}")
else()
set(library_search_directories
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}/${SWIFTEXE_SINGLE_ARCHITECTURE}")
endif()

# Add variant-specific flags.
_add_variant_c_compile_flags(
Expand All @@ -2067,7 +2141,6 @@ function(_add_swift_executable_single name)
list(APPEND link_flags "-Wl,-no_pie")
endif()

is_darwin_based_sdk("${SWIFTEXE_SINGLE_SDK}" IS_DARWIN)
if(IS_DARWIN)
list(APPEND link_flags
"-Xlinker" "-rpath"
Expand All @@ -2076,12 +2149,19 @@ function(_add_swift_executable_single name)

# Find the names of dependency library targets.
#
# We don't add the ${ARCH} to the target suffix because we want to link
# We don't add the ${ARCH} to the target suffix on Darwin because we want to link
# against fat libraries.
_list_add_string_suffix(
"${SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES}"
"-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}"
SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES_TARGETS)
if (IS_DARWIN)
_list_add_string_suffix(
"${SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES}"
"-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}"
SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES_TARGETS)
else()
_list_add_string_suffix(
"${SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES}"
"-${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}-${SWIFTEXE_SINGLE_ARCHITECTURE}"
SWIFTEXE_SINGLE_LINK_FAT_LIBRARIES_TARGETS)
endif()

handle_swift_sources(
dependency_target
Expand Down Expand Up @@ -2191,12 +2271,19 @@ function(add_swift_target_executable name)
add_dependencies("swift-test-stdlib${VARIANT_SUFFIX}" ${VARIANT_NAME})
endif()

# Don't add the ${arch} to the suffix. We want to link against fat
# On Darwin, don't add the ${arch} to the suffix. We want to link against fat
# libraries.
_list_add_string_suffix(
"${SWIFTEXE_TARGET_DEPENDS}"
"-${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
SWIFTEXE_TARGET_DEPENDS_with_suffix)
if (IS_DARWIN)
_list_add_string_suffix(
"${SWIFTEXE_TARGET_DEPENDS}"
"-${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
SWIFTEXE_TARGET_DEPENDS_with_suffix)
else()
_list_add_string_suffix(
"${SWIFTEXE_TARGET_DEPENDS}"
"${VARIANT_SUFFIX}"
SWIFTEXE_TARGET_DEPENDS_with_suffix)
endif()
_add_swift_executable_single(
${VARIANT_NAME}
${SWIFTEXE_TARGET_SOURCES}
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ class ToolChain {
/// relative to the compiler.
void getRuntimeLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
const llvm::opt::ArgList &args, bool shared) const;
void getRuntimeLibraryPathWithArch(SmallVectorImpl<char> &runtimeLibPath,
const llvm::opt::ArgList &args,
bool shared) const;

void addPathEnvironmentVariableIfNeeded(Job::EnvironmentVector &env,
const char *name,
Expand Down
8 changes: 8 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,14 @@ void ToolChain::getRuntimeLibraryPath(SmallVectorImpl<char> &runtimeLibPath,
getPlatformNameForTriple(getTriple()));
}

void ToolChain::getRuntimeLibraryPathWithArch(
SmallVectorImpl<char> &runtimeLibPath, const llvm::opt::ArgList &args,
bool shared) const {
getRuntimeLibraryPath(runtimeLibPath, args, shared);
llvm::sys::path::append(runtimeLibPath,
swift::getMajorArchitectureName(getTriple()));
}

bool ToolChain::sanitizerRuntimeLibExists(const ArgList &args,
StringRef sanitizerName,
bool shared) const {
Expand Down
15 changes: 9 additions & 6 deletions lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ toolchains::GenericUnix::constructInvocation(const InterpretJobAction &job,
InvocationInfo II = ToolChain::constructInvocation(job, context);

SmallString<128> runtimeLibraryPath;
getRuntimeLibraryPath(runtimeLibraryPath, context.Args,
/*Shared=*/true);
getRuntimeLibraryPathWithArch(runtimeLibraryPath, context.Args,
/*Shared=*/true);

addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "LD_LIBRARY_PATH",
":", options::OPT_L, context.Args,
Expand Down Expand Up @@ -205,10 +205,12 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
}

SmallString<128> SharedRuntimeLibPath;
getRuntimeLibraryPath(SharedRuntimeLibPath, context.Args, /*Shared=*/true);
getRuntimeLibraryPathWithArch(SharedRuntimeLibPath, context.Args,
/*Shared=*/true);

SmallString<128> StaticRuntimeLibPath;
getRuntimeLibraryPath(StaticRuntimeLibPath, context.Args, /*Shared=*/false);
getRuntimeLibraryPathWithArch(StaticRuntimeLibPath, context.Args,
/*Shared=*/false);

// Add the runtime library link path, which is platform-specific and found
// relative to the compiler.
Expand All @@ -222,8 +224,6 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
}

SmallString<128> swiftrtPath = SharedRuntimeLibPath;
llvm::sys::path::append(swiftrtPath,
swift::getMajorArchitectureName(getTriple()));
llvm::sys::path::append(swiftrtPath, "swiftrt.o");
Arguments.push_back(context.Args.MakeArgString(swiftrtPath));

Expand Down Expand Up @@ -260,6 +260,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));

SmallString<128> linkFilePath = StaticRuntimeLibPath;
llvm::sys::path::remove_filename(linkFilePath); // remove arch name
llvm::sys::path::append(linkFilePath, "static-executable-args.lnk");
auto linkFile = linkFilePath.str();

Expand All @@ -273,6 +274,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));

SmallString<128> linkFilePath = StaticRuntimeLibPath;
llvm::sys::path::remove_filename(linkFilePath); // remove arch name
llvm::sys::path::append(linkFilePath, "static-stdlib-args.lnk");
auto linkFile = linkFilePath.str();
if (llvm::sys::fs::is_regular_file(linkFile)) {
Expand Down Expand Up @@ -306,6 +308,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,

if (context.Args.hasArg(options::OPT_profile_generate)) {
SmallString<128> LibProfile(SharedRuntimeLibPath);
llvm::sys::path::remove_filename(LibProfile); // remove arch name
llvm::sys::path::remove_filename(LibProfile); // remove platform name
llvm::sys::path::append(LibProfile, "clang", "lib");

Expand Down
7 changes: 6 additions & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ static void updateRuntimeLibraryPath(SearchPathOptions &SearchPathOpts,
llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath);

llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
if (Triple.isOSDarwin()) {
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
}

llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
SearchPathOpts.RuntimeLibraryImportPath = LibPath.str();
if (!Triple.isOSDarwin()) {
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
}
}

void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
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_swift_library(swiftImageInspectionSharedObject OBJECT_LIBRARY TARGET_LIBRARY
ImageInspectionELF.cpp
Expand Down
9 changes: 8 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,16 @@ foreach(SDK ${SWIFT_SDKS})
list(APPEND test_dependencies "touch-covering-tests")
endif()

set(validation_test_dependencies
is_darwin_based_sdk("${SDK}" IS_DARWIN)
if(IS_DARWIN)
set(validation_test_dependencies
"swiftStdlibCollectionUnittest-${SWIFT_SDK_${SDK}_LIB_SUBDIR}"
"swiftStdlibUnicodeUnittest-${SWIFT_SDK_${SDK}_LIB_SUBDIR}")
else()
set(validation_test_dependencies
"swiftStdlibCollectionUnittest${VARIANT_SUFFIX}"
"swiftStdlibUnicodeUnittest${VARIANT_SUFFIX}")
endif()

set(command_upload_stdlib)
set(command_upload_swift_reflection_test)
Expand Down
Loading