Skip to content

[5.3] Properly compute resource folder when linking statically #33678

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

Merged
Merged
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
10 changes: 7 additions & 3 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ class CompilerInvocation {
bool parseArgs(ArrayRef<const char *> Args, DiagnosticEngine &Diags,
SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>>
*ConfigurationFileBuffers = nullptr,
StringRef workingDirectory = {});
StringRef workingDirectory = {},
StringRef mainExecutablePath = {});

/// Sets specific options based on the given serialized Swift binary data.
///
Expand Down Expand Up @@ -213,8 +214,11 @@ class CompilerInvocation {
/// Computes the runtime resource path relative to the given Swift
/// executable.
static void computeRuntimeResourcePathFromExecutablePath(
StringRef mainExecutablePath,
llvm::SmallString<128> &runtimeResourcePath);
StringRef mainExecutablePath, bool shared,
llvm::SmallVectorImpl<char> &runtimeResourcePath);

/// Appends `lib/swift[_static]` to the given path
static void appendSwiftLibDir(llvm::SmallVectorImpl<char> &path, bool shared);

void setSDKPath(const std::string &Path);

Expand Down
5 changes: 5 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ class FrontendOptions {
/// Indicates whether the action will immediately run code.
static bool isActionImmediate(ActionType);

/// Determines whether the static or shared resource folder is used.
/// When set to `true`, the default resource folder will be set to
/// '.../lib/swift', otherwise '.../lib/swift_static'.
bool UseSharedResourceFolder = true;

/// \return true if action only parses without doing other compilation steps.
static bool shouldActionOnlyParse(ActionType);

Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,8 @@ def target_sdk_version : Separate<["-"], "target-sdk-version">,
def target_variant_sdk_version : Separate<["-"], "target-variant-sdk-version">,
HelpText<"The version of target variant SDK used for compilation">;


def use_static_resource_dir
: Flag<["-"], "use-static-resource-dir">,
HelpText<"Use resources in the static resource directory">;
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]
7 changes: 7 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,13 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
commandLine.push_back(resourceDirArg->getValue());
}

if (Args.hasFlag(options::OPT_static_executable,
options::OPT_no_static_executable, false) ||
Args.hasFlag(options::OPT_static_stdlib, options::OPT_no_static_stdlib,
false)) {
commandLine.push_back("-use-static-resource-dir");
}

std::string executable = getSwiftProgramPath();

// FIXME: This bypasses mechanisms like -v and -###. (SR-12119)
Expand Down
22 changes: 12 additions & 10 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Option/Options.h"
#include "clang/Basic/Version.h"
#include "clang/Driver/Util.h"
Expand Down Expand Up @@ -523,6 +524,13 @@ ToolChain::constructInvocation(const CompileJobAction &job,
Arguments.push_back("-track-system-dependencies");
}

if (context.Args.hasFlag(options::OPT_static_executable,
options::OPT_no_static_executable, false) ||
context.Args.hasFlag(options::OPT_static_stdlib,
options::OPT_no_static_stdlib, false)) {
Arguments.push_back("-use-static-resource-dir");
}

context.Args.AddLastArg(
Arguments,
options::
Expand Down Expand Up @@ -1256,24 +1264,18 @@ void ToolChain::getClangLibraryPath(const ArgList &Args,
void ToolChain::getResourceDirPath(SmallVectorImpl<char> &resourceDirPath,
const llvm::opt::ArgList &args,
bool shared) const {
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
// library link path and the standard library module import path don't
// need to be the same.
if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) {
StringRef value = A->getValue();
resourceDirPath.append(value.begin(), value.end());
} else if (!getTriple().isOSDarwin() && args.hasArg(options::OPT_sdk)) {
StringRef value = args.getLastArg(options::OPT_sdk)->getValue();
resourceDirPath.append(value.begin(), value.end());
llvm::sys::path::append(resourceDirPath, "usr", "lib",
shared ? "swift" : "swift_static");
llvm::sys::path::append(resourceDirPath, "usr");
CompilerInvocation::appendSwiftLibDir(resourceDirPath, shared);
} else {
auto programPath = getDriver().getSwiftProgramPath();
resourceDirPath.append(programPath.begin(), programPath.end());
llvm::sys::path::remove_filename(resourceDirPath); // remove /swift
llvm::sys::path::remove_filename(resourceDirPath); // remove /bin
llvm::sys::path::append(resourceDirPath, "lib",
shared ? "swift" : "swift_static");
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
programPath, shared, resourceDirPath);
}

StringRef libSubDir = getPlatformNameForTriple(getTriple());
Expand Down
1 change: 1 addition & 0 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ bool ArgsToFrontendOptionsConverter::convert(
Opts.EnableSourceImport |= Args.hasArg(OPT_enable_source_import);
Opts.ImportUnderlyingModule |= Args.hasArg(OPT_import_underlying_module);
Opts.EnableIncrementalDependencyVerifier |= Args.hasArg(OPT_verify_incremental_dependencies);
Opts.UseSharedResourceFolder = !Args.hasArg(OPT_use_static_resource_dir);

computeImportObjCHeaderOptions();
computeImplicitImportModuleNames();
Expand Down
26 changes: 19 additions & 7 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,25 @@ swift::CompilerInvocation::CompilerInvocation() {
}

void CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
StringRef mainExecutablePath, llvm::SmallString<128> &runtimeResourcePath) {
runtimeResourcePath.assign(mainExecutablePath);
StringRef mainExecutablePath, bool shared,
llvm::SmallVectorImpl<char> &runtimeResourcePath) {
runtimeResourcePath.append(mainExecutablePath.begin(),
mainExecutablePath.end());

llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /swift
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /bin
llvm::sys::path::append(runtimeResourcePath, "lib", "swift");
appendSwiftLibDir(runtimeResourcePath, shared);
}

void CompilerInvocation::appendSwiftLibDir(llvm::SmallVectorImpl<char> &path,
bool shared) {
llvm::sys::path::append(path, "lib", shared ? "swift" : "swift_static");
}

void CompilerInvocation::setMainExecutablePath(StringRef Path) {
llvm::SmallString<128> LibPath;
computeRuntimeResourcePathFromExecutablePath(Path, LibPath);
computeRuntimeResourcePathFromExecutablePath(
Path, FrontendOpts.UseSharedResourceFolder, LibPath);
setRuntimeResourcePath(LibPath.str());

llvm::SmallString<128> DiagnosticDocsPath(Path);
Expand Down Expand Up @@ -1597,11 +1606,10 @@ static bool ParseMigratorArgs(MigratorOptions &Opts,
}

bool CompilerInvocation::parseArgs(
ArrayRef<const char *> Args,
DiagnosticEngine &Diags,
ArrayRef<const char *> Args, DiagnosticEngine &Diags,
SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>>
*ConfigurationFileBuffers,
StringRef workingDirectory) {
StringRef workingDirectory, StringRef mainExecutablePath) {
using namespace options;

if (Args.empty())
Expand Down Expand Up @@ -1632,6 +1640,10 @@ bool CompilerInvocation::parseArgs(
return true;
}

if (!mainExecutablePath.empty()) {
setMainExecutablePath(mainExecutablePath);
}

ParseModuleInterfaceArgs(ModuleInterfaceOpts, ParsedArgs);
SaveModuleInterfaceArgs(ModuleInterfaceOpts, FrontendOpts, ParsedArgs, Diags);

Expand Down
9 changes: 5 additions & 4 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2078,17 +2078,18 @@ int swift::performFrontend(ArrayRef<const char *> Args,
}

CompilerInvocation Invocation;
std::string MainExecutablePath = llvm::sys::fs::getMainExecutable(Argv0,
MainAddr);
Invocation.setMainExecutablePath(MainExecutablePath);

SmallString<128> workingDirectory;
llvm::sys::fs::current_path(workingDirectory);

std::string MainExecutablePath =
llvm::sys::fs::getMainExecutable(Argv0, MainAddr);

// Parse arguments.
SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> configurationFileBuffers;
if (Invocation.parseArgs(Args, Instance->getDiags(),
&configurationFileBuffers, workingDirectory)) {
&configurationFileBuffers, workingDirectory,
MainExecutablePath)) {
return finishDiagProcessing(1, /*verifierEnabled*/ false);
}

Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,9 @@ function(_add_swift_target_library_single target name)
"${SWIFTLIB_SINGLE_multiple_parameter_options}"
${ARGN})

translate_flag(${SWIFTLIB_SINGLE_STATIC} "STATIC"
SWIFTLIB_SINGLE_STATIC_keyword)

# Determine macCatalyst build flavor
get_maccatalyst_build_flavor(maccatalyst_build_flavor
"${SWIFTLIB_SINGLE_SDK}" "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}")
Expand Down Expand Up @@ -757,6 +760,7 @@ function(_add_swift_target_library_single target name)
${SWIFTLIB_SINGLE_IS_STDLIB_CORE_keyword}
${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword}
${embed_bitcode_arg}
${SWIFTLIB_SINGLE_STATIC_keyword}
INSTALL_IN_COMPONENT "${SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT}"
MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}")
add_swift_source_group("${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}")
Expand Down
70 changes: 67 additions & 3 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function(handle_swift_sources
dependency_sibgen_target_out_var_name
sourcesvar externalvar name)
cmake_parse_arguments(SWIFTSOURCES
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE"
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
"SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR"
"DEPENDS;COMPILE_FLAGS;MODULE_NAME"
${ARGN})
Expand All @@ -41,6 +41,8 @@ function(handle_swift_sources
IS_SDK_OVERLAY_arg)
translate_flag(${SWIFTSOURCES_EMBED_BITCODE} "EMBED_BITCODE"
EMBED_BITCODE_arg)
translate_flag(${SWIFTSOURCES_STATIC} "STATIC"
STATIC_arg)

if(SWIFTSOURCES_IS_MAIN)
set(SWIFTSOURCES_INSTALL_IN_COMPONENT never_install)
Expand Down Expand Up @@ -278,13 +280,15 @@ endfunction()
# [MODULE_NAME] # The module name.
# [INSTALL_IN_COMPONENT] # Install produced files.
# [EMBED_BITCODE] # Embed LLVM bitcode into the .o files
# [STATIC] # Also write .swiftmodule etc. to static
# # resource folder
# )
function(_compile_swift_files
dependency_target_out_var_name dependency_module_target_out_var_name
dependency_sib_target_out_var_name dependency_sibopt_target_out_var_name
dependency_sibgen_target_out_var_name)
cmake_parse_arguments(SWIFTFILE
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE"
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
"OUTPUT;MODULE_NAME;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR"
"SOURCES;FLAGS;DEPENDS;SDK;ARCHITECTURE;OPT_FLAGS;MODULE_DIR"
${ARGN})
Expand Down Expand Up @@ -448,8 +452,11 @@ function(_compile_swift_files
endforeach()

set(module_file)
set(module_file_static)
set(module_doc_file)
set(module_doc_file_static)
set(interface_file)
set(interface_file_static)

if(NOT SWIFTFILE_IS_MAIN)
# Determine the directory where the module file should be placed.
Expand All @@ -464,17 +471,28 @@ function(_compile_swift_files
list(APPEND swift_flags "-parse-as-library")

set(module_base "${module_dir}/${SWIFTFILE_MODULE_NAME}")

set(module_dir_static "${SWIFTSTATICLIB_DIR}/${library_subdir}")
set(module_base_static "${module_dir_static}/${SWIFTFILE_MODULE_NAME}")

set(module_triple ${SWIFT_SDK_${library_subdir_sdk}_ARCH_${SWIFTFILE_ARCHITECTURE}_MODULE})
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS OR
SWIFTFILE_SDK STREQUAL "MACCATALYST")
set(specific_module_dir "${module_base}.swiftmodule")
set(module_base "${module_base}.swiftmodule/${module_triple}")

set(specific_module_dir_static "${module_base_static}.swiftmodule")
set(module_base_static "${module_base_static}.swiftmodule/${module_triple}")
else()
set(specific_module_dir)
set(specific_module_dir_static)
endif()
set(module_file "${module_base}.swiftmodule")
set(module_doc_file "${module_base}.swiftdoc")

set(module_file_static "${module_base_static}.swiftmodule")
set(module_doc_file_static "${module_base_static}.swiftdoc")

# FIXME: These don't really belong inside the swiftmodule, but there's not
# an obvious alternate place to put them.
set(sib_file "${module_base}.Onone.sib")
Expand All @@ -483,6 +501,7 @@ function(_compile_swift_files

if(SWIFT_ENABLE_MODULE_INTERFACES)
set(interface_file "${module_base}.swiftinterface")
set(interface_file_static "${module_base_static}.swiftinterface")
list(APPEND swift_module_flags
"-emit-module-interface-path" "${interface_file}")
endif()
Expand Down Expand Up @@ -510,10 +529,20 @@ function(_compile_swift_files
swift_install_in_component(DIRECTORY "${specific_module_dir}"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
if(SWIFTFILE_STATIC)
swift_install_in_component(DIRECTORY "${specific_module_dir_static}"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${library_subdir}"
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
endif()
else()
swift_install_in_component(FILES ${module_outputs}
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
if(SWIFTFILE_STATIC)
swift_install_in_component(FILES ${module_outputs}
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${library_subdir}"
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
endif()
endif()

# macCatalyst zippered module setup
Expand Down Expand Up @@ -563,8 +592,10 @@ function(_compile_swift_files
endif()

set(module_outputs "${module_file}" "${module_doc_file}")
set(module_outputs_static "${module_file_static}" "${module_doc_file_static}")
if(interface_file)
list(APPEND module_outputs "${interface_file}")
list(APPEND module_outputs_static "${interface_file_static}")
endif()

if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
Expand All @@ -573,10 +604,23 @@ function(_compile_swift_files
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}"
OPTIONAL
PATTERN "Project" EXCLUDE)

if(SWIFTFILE_STATIC)
swift_install_in_component(DIRECTORY "${specific_module_dir_static}"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${library_subdir}"
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}"
OPTIONAL
PATTERN "Project" EXCLUDE)
endif()
else()
swift_install_in_component(FILES ${module_outputs}
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
if(SWIFTFILE_STATIC)
swift_install_in_component(FILES ${module_outputs}
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${library_subdir}"
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
endif()
endif()

set(line_directive_tool "${SWIFT_SOURCE_DIR}/utils/line-directive")
Expand Down Expand Up @@ -758,7 +802,27 @@ function(_compile_swift_files
${swift_ide_test_dependency}
${create_dirs_dependency_target}
COMMENT "Generating ${module_file}")
set("${dependency_module_target_out_var_name}" "${module_dependency_target}" PARENT_SCOPE)

if(SWIFTFILE_STATIC)
add_custom_command_target(
module_dependency_target_static
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
${specific_module_dir_static}
COMMAND
"${CMAKE_COMMAND}" "-E" "copy" ${module_file} ${module_file_static}
COMMAND
"${CMAKE_COMMAND}" "-E" "copy" ${module_doc_file} ${module_doc_file_static}
COMMAND
"${CMAKE_COMMAND}" "-E" "copy" ${interface_file} ${interface_file_static}
OUTPUT ${module_outputs_static}
DEPENDS
"${module_dependency_target}"
COMMENT "Generating ${module_file}")
set("${dependency_module_target_out_var_name}" "${module_dependency_target_static}" PARENT_SCOPE)
else()
set("${dependency_module_target_out_var_name}" "${module_dependency_target}" PARENT_SCOPE)
endif()

# macCatalyst zippered swiftmodule
if(maccatalyst_build_flavor STREQUAL "zippered")
Expand Down
Loading