Skip to content

Commit 7e60a73

Browse files
authored
Merge pull request #33168 from drexin/wip-fix-resource-folder
Properly compute resource folder when linking statically
2 parents b50a2cf + 0850436 commit 7e60a73

File tree

14 files changed

+241
-29
lines changed

14 files changed

+241
-29
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ class CompilerInvocation {
128128
bool parseArgs(ArrayRef<const char *> Args, DiagnosticEngine &Diags,
129129
SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>>
130130
*ConfigurationFileBuffers = nullptr,
131-
StringRef workingDirectory = {});
131+
StringRef workingDirectory = {},
132+
StringRef mainExecutablePath = {});
132133

133134
/// Sets specific options based on the given serialized Swift binary data.
134135
///
@@ -213,8 +214,11 @@ class CompilerInvocation {
213214
/// Computes the runtime resource path relative to the given Swift
214215
/// executable.
215216
static void computeRuntimeResourcePathFromExecutablePath(
216-
StringRef mainExecutablePath,
217-
llvm::SmallString<128> &runtimeResourcePath);
217+
StringRef mainExecutablePath, bool shared,
218+
llvm::SmallVectorImpl<char> &runtimeResourcePath);
219+
220+
/// Appends `lib/swift[_static]` to the given path
221+
static void appendSwiftLibDir(llvm::SmallVectorImpl<char> &path, bool shared);
218222

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

include/swift/Frontend/FrontendOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ class FrontendOptions {
286286
/// -dump-scope-maps.
287287
SmallVector<std::pair<unsigned, unsigned>, 2> DumpScopeMapLocations;
288288

289+
/// Determines whether the static or shared resource folder is used.
290+
/// When set to `true`, the default resource folder will be set to
291+
/// '.../lib/swift', otherwise '.../lib/swift_static'.
292+
bool UseSharedResourceFolder = true;
293+
289294
/// \return true if action only parses without doing other compilation steps.
290295
static bool shouldActionOnlyParse(ActionType);
291296

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,4 +738,8 @@ def extra_clang_options_only : Flag<["-"], "only-use-extra-clang-opts">,
738738
def candidate_module_file
739739
: Separate<["-"], "candidate-module-file">, MetaVarName<"<path>">,
740740
HelpText<"Specify Swift module may be ready to use for an interface">;
741+
742+
def use_static_resource_dir
743+
: Flag<["-"], "use-static-resource-dir">,
744+
HelpText<"Use resources in the static resource directory">;
741745
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

lib/Driver/Driver.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,13 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
22662266
commandLine.push_back(resourceDirArg->getValue());
22672267
}
22682268

2269+
if (Args.hasFlag(options::OPT_static_executable,
2270+
options::OPT_no_static_executable, false) ||
2271+
Args.hasFlag(options::OPT_static_stdlib, options::OPT_no_static_stdlib,
2272+
false)) {
2273+
commandLine.push_back("-use-static-resource-dir");
2274+
}
2275+
22692276
std::string executable = getSwiftProgramPath();
22702277

22712278
// FIXME: This bypasses mechanisms like -v and -###. (SR-12119)

lib/Driver/ToolChains.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Driver/Compilation.h"
2323
#include "swift/Driver/Driver.h"
2424
#include "swift/Driver/Job.h"
25+
#include "swift/Frontend/Frontend.h"
2526
#include "swift/Option/Options.h"
2627
#include "clang/Basic/Version.h"
2728
#include "clang/Driver/Util.h"
@@ -537,6 +538,13 @@ ToolChain::constructInvocation(const CompileJobAction &job,
537538
Arguments.push_back("-track-system-dependencies");
538539
}
539540

541+
if (context.Args.hasFlag(options::OPT_static_executable,
542+
options::OPT_no_static_executable, false) ||
543+
context.Args.hasFlag(options::OPT_static_stdlib,
544+
options::OPT_no_static_stdlib, false)) {
545+
Arguments.push_back("-use-static-resource-dir");
546+
}
547+
540548
context.Args.AddLastArg(
541549
Arguments,
542550
options::
@@ -1270,24 +1278,18 @@ void ToolChain::getClangLibraryPath(const ArgList &Args,
12701278
void ToolChain::getResourceDirPath(SmallVectorImpl<char> &resourceDirPath,
12711279
const llvm::opt::ArgList &args,
12721280
bool shared) const {
1273-
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
1274-
// library link path and the standard library module import path don't
1275-
// need to be the same.
12761281
if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) {
12771282
StringRef value = A->getValue();
12781283
resourceDirPath.append(value.begin(), value.end());
12791284
} else if (!getTriple().isOSDarwin() && args.hasArg(options::OPT_sdk)) {
12801285
StringRef value = args.getLastArg(options::OPT_sdk)->getValue();
12811286
resourceDirPath.append(value.begin(), value.end());
1282-
llvm::sys::path::append(resourceDirPath, "usr", "lib",
1283-
shared ? "swift" : "swift_static");
1287+
llvm::sys::path::append(resourceDirPath, "usr");
1288+
CompilerInvocation::appendSwiftLibDir(resourceDirPath, shared);
12841289
} else {
12851290
auto programPath = getDriver().getSwiftProgramPath();
1286-
resourceDirPath.append(programPath.begin(), programPath.end());
1287-
llvm::sys::path::remove_filename(resourceDirPath); // remove /swift
1288-
llvm::sys::path::remove_filename(resourceDirPath); // remove /bin
1289-
llvm::sys::path::append(resourceDirPath, "lib",
1290-
shared ? "swift" : "swift_static");
1291+
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
1292+
programPath, shared, resourceDirPath);
12911293
}
12921294

12931295
StringRef libSubDir = getPlatformNameForTriple(getTriple());

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ bool ArgsToFrontendOptionsConverter::convert(
193193
Opts.EnableSourceImport |= Args.hasArg(OPT_enable_source_import);
194194
Opts.ImportUnderlyingModule |= Args.hasArg(OPT_import_underlying_module);
195195
Opts.EnableIncrementalDependencyVerifier |= Args.hasArg(OPT_verify_incremental_dependencies);
196+
Opts.UseSharedResourceFolder = !Args.hasArg(OPT_use_static_resource_dir);
196197

197198
computeImportObjCHeaderOptions();
198199
computeImplicitImportModuleNames();

lib/Frontend/CompilerInvocation.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,26 @@ swift::CompilerInvocation::CompilerInvocation() {
4444
}
4545

4646
void CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
47-
StringRef mainExecutablePath, llvm::SmallString<128> &runtimeResourcePath) {
48-
runtimeResourcePath.assign(mainExecutablePath);
47+
StringRef mainExecutablePath, bool shared,
48+
llvm::SmallVectorImpl<char> &runtimeResourcePath) {
49+
runtimeResourcePath.append(mainExecutablePath.begin(),
50+
mainExecutablePath.end());
51+
4952
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /swift
5053
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /bin
51-
llvm::sys::path::append(runtimeResourcePath, "lib", "swift");
54+
appendSwiftLibDir(runtimeResourcePath, shared);
55+
}
56+
57+
void CompilerInvocation::appendSwiftLibDir(llvm::SmallVectorImpl<char> &path,
58+
bool shared) {
59+
llvm::sys::path::append(path, "lib", shared ? "swift" : "swift_static");
5260
}
5361

5462
void CompilerInvocation::setMainExecutablePath(StringRef Path) {
5563
FrontendOpts.MainExecutablePath = Path.str();
5664
llvm::SmallString<128> LibPath;
57-
computeRuntimeResourcePathFromExecutablePath(Path, LibPath);
65+
computeRuntimeResourcePathFromExecutablePath(
66+
Path, FrontendOpts.UseSharedResourceFolder, LibPath);
5867
setRuntimeResourcePath(LibPath.str());
5968

6069
llvm::SmallString<128> DiagnosticDocsPath(Path);
@@ -1727,11 +1736,10 @@ static bool ParseMigratorArgs(MigratorOptions &Opts,
17271736
}
17281737

17291738
bool CompilerInvocation::parseArgs(
1730-
ArrayRef<const char *> Args,
1731-
DiagnosticEngine &Diags,
1739+
ArrayRef<const char *> Args, DiagnosticEngine &Diags,
17321740
SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>>
17331741
*ConfigurationFileBuffers,
1734-
StringRef workingDirectory) {
1742+
StringRef workingDirectory, StringRef mainExecutablePath) {
17351743
using namespace options;
17361744

17371745
if (Args.empty())
@@ -1762,6 +1770,10 @@ bool CompilerInvocation::parseArgs(
17621770
return true;
17631771
}
17641772

1773+
if (!mainExecutablePath.empty()) {
1774+
setMainExecutablePath(mainExecutablePath);
1775+
}
1776+
17651777
ParseModuleInterfaceArgs(ModuleInterfaceOpts, ParsedArgs);
17661778
SaveModuleInterfaceArgs(ModuleInterfaceOpts, FrontendOpts, ParsedArgs, Diags);
17671779

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,17 +2462,18 @@ int swift::performFrontend(ArrayRef<const char *> Args,
24622462
}
24632463

24642464
CompilerInvocation Invocation;
2465-
std::string MainExecutablePath = llvm::sys::fs::getMainExecutable(Argv0,
2466-
MainAddr);
2467-
Invocation.setMainExecutablePath(MainExecutablePath);
24682465

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

2469+
std::string MainExecutablePath =
2470+
llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
2471+
24722472
// Parse arguments.
24732473
SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> configurationFileBuffers;
24742474
if (Invocation.parseArgs(Args, Instance->getDiags(),
2475-
&configurationFileBuffers, workingDirectory)) {
2475+
&configurationFileBuffers, workingDirectory,
2476+
MainExecutablePath)) {
24762477
return finishDiagProcessing(1, /*verifierEnabled*/ false);
24772478
}
24782479

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,9 @@ function(_add_swift_target_library_single target name)
650650
"${SWIFTLIB_SINGLE_multiple_parameter_options}"
651651
${ARGN})
652652

653+
translate_flag(${SWIFTLIB_SINGLE_STATIC} "STATIC"
654+
SWIFTLIB_SINGLE_STATIC_keyword)
655+
653656
# Determine macCatalyst build flavor
654657
get_maccatalyst_build_flavor(maccatalyst_build_flavor
655658
"${SWIFTLIB_SINGLE_SDK}" "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}")
@@ -779,6 +782,7 @@ function(_add_swift_target_library_single target name)
779782
${SWIFTLIB_SINGLE_IS_STDLIB_CORE_keyword}
780783
${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword}
781784
${embed_bitcode_arg}
785+
${SWIFTLIB_SINGLE_STATIC_keyword}
782786
INSTALL_IN_COMPONENT "${SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT}"
783787
MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}")
784788
add_swift_source_group("${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}")

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function(handle_swift_sources
2929
dependency_sibgen_target_out_var_name
3030
sourcesvar externalvar name)
3131
cmake_parse_arguments(SWIFTSOURCES
32-
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE"
32+
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
3333
"SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR"
3434
"DEPENDS;COMPILE_FLAGS;MODULE_NAME"
3535
${ARGN})
@@ -41,6 +41,8 @@ function(handle_swift_sources
4141
IS_SDK_OVERLAY_arg)
4242
translate_flag(${SWIFTSOURCES_EMBED_BITCODE} "EMBED_BITCODE"
4343
EMBED_BITCODE_arg)
44+
translate_flag(${SWIFTSOURCES_STATIC} "STATIC"
45+
STATIC_arg)
4446

4547
if(SWIFTSOURCES_IS_MAIN)
4648
set(SWIFTSOURCES_INSTALL_IN_COMPONENT never_install)
@@ -281,13 +283,15 @@ endfunction()
281283
# [MODULE_NAME] # The module name.
282284
# [INSTALL_IN_COMPONENT] # Install produced files.
283285
# [EMBED_BITCODE] # Embed LLVM bitcode into the .o files
286+
# [STATIC] # Also write .swiftmodule etc. to static
287+
# # resource folder
284288
# )
285289
function(_compile_swift_files
286290
dependency_target_out_var_name dependency_module_target_out_var_name
287291
dependency_sib_target_out_var_name dependency_sibopt_target_out_var_name
288292
dependency_sibgen_target_out_var_name)
289293
cmake_parse_arguments(SWIFTFILE
290-
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE"
294+
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC"
291295
"OUTPUT;MODULE_NAME;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR"
292296
"SOURCES;FLAGS;DEPENDS;SDK;ARCHITECTURE;OPT_FLAGS;MODULE_DIR"
293297
${ARGN})
@@ -450,8 +454,11 @@ function(_compile_swift_files
450454
endforeach()
451455

452456
set(module_file)
457+
set(module_file_static)
453458
set(module_doc_file)
459+
set(module_doc_file_static)
454460
set(interface_file)
461+
set(interface_file_static)
455462

456463
if(NOT SWIFTFILE_IS_MAIN)
457464
# Determine the directory where the module file should be placed.
@@ -466,17 +473,28 @@ function(_compile_swift_files
466473
list(APPEND swift_flags "-parse-as-library")
467474

468475
set(module_base "${module_dir}/${SWIFTFILE_MODULE_NAME}")
476+
477+
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${library_subdir}")
478+
set(module_base_static "${module_dir_static}/${SWIFTFILE_MODULE_NAME}")
479+
469480
set(module_triple ${SWIFT_SDK_${library_subdir_sdk}_ARCH_${SWIFTFILE_ARCHITECTURE}_MODULE})
470481
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS OR
471482
SWIFTFILE_SDK STREQUAL "MACCATALYST")
472483
set(specific_module_dir "${module_base}.swiftmodule")
473484
set(module_base "${module_base}.swiftmodule/${module_triple}")
485+
486+
set(specific_module_dir_static "${module_base_static}.swiftmodule")
487+
set(module_base_static "${module_base_static}.swiftmodule/${module_triple}")
474488
else()
475489
set(specific_module_dir)
490+
set(specific_module_dir_static)
476491
endif()
477492
set(module_file "${module_base}.swiftmodule")
478493
set(module_doc_file "${module_base}.swiftdoc")
479494

495+
set(module_file_static "${module_base_static}.swiftmodule")
496+
set(module_doc_file_static "${module_base_static}.swiftdoc")
497+
480498
# FIXME: These don't really belong inside the swiftmodule, but there's not
481499
# an obvious alternate place to put them.
482500
set(sib_file "${module_base}.Onone.sib")
@@ -485,6 +503,7 @@ function(_compile_swift_files
485503

486504
if(SWIFT_ENABLE_MODULE_INTERFACES)
487505
set(interface_file "${module_base}.swiftinterface")
506+
set(interface_file_static "${module_base_static}.swiftinterface")
488507
list(APPEND swift_module_flags
489508
"-emit-module-interface-path" "${interface_file}")
490509
endif()
@@ -512,10 +531,20 @@ function(_compile_swift_files
512531
swift_install_in_component(DIRECTORY "${specific_module_dir}"
513532
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
514533
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
534+
if(SWIFTFILE_STATIC)
535+
swift_install_in_component(DIRECTORY "${specific_module_dir_static}"
536+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${library_subdir}"
537+
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
538+
endif()
515539
else()
516540
swift_install_in_component(FILES ${module_outputs}
517541
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
518542
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
543+
if(SWIFTFILE_STATIC)
544+
swift_install_in_component(FILES ${module_outputs}
545+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${library_subdir}"
546+
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
547+
endif()
519548
endif()
520549

521550
# macCatalyst zippered module setup
@@ -565,8 +594,10 @@ function(_compile_swift_files
565594
endif()
566595

567596
set(module_outputs "${module_file}" "${module_doc_file}")
597+
set(module_outputs_static "${module_file_static}" "${module_doc_file_static}")
568598
if(interface_file)
569599
list(APPEND module_outputs "${interface_file}")
600+
list(APPEND module_outputs_static "${interface_file_static}")
570601
endif()
571602

572603
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
@@ -575,10 +606,23 @@ function(_compile_swift_files
575606
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}"
576607
OPTIONAL
577608
PATTERN "Project" EXCLUDE)
609+
610+
if(SWIFTFILE_STATIC)
611+
swift_install_in_component(DIRECTORY "${specific_module_dir_static}"
612+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${library_subdir}"
613+
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}"
614+
OPTIONAL
615+
PATTERN "Project" EXCLUDE)
616+
endif()
578617
else()
579618
swift_install_in_component(FILES ${module_outputs}
580619
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
581620
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
621+
if(SWIFTFILE_STATIC)
622+
swift_install_in_component(FILES ${module_outputs}
623+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift_static/${library_subdir}"
624+
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}")
625+
endif()
582626
endif()
583627

584628
set(line_directive_tool "${SWIFT_SOURCE_DIR}/utils/line-directive")
@@ -760,7 +804,27 @@ function(_compile_swift_files
760804
${swift_ide_test_dependency}
761805
${create_dirs_dependency_target}
762806
COMMENT "Generating ${module_file}")
763-
set("${dependency_module_target_out_var_name}" "${module_dependency_target}" PARENT_SCOPE)
807+
808+
if(SWIFTFILE_STATIC)
809+
add_custom_command_target(
810+
module_dependency_target_static
811+
COMMAND
812+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
813+
${specific_module_dir_static}
814+
COMMAND
815+
"${CMAKE_COMMAND}" "-E" "copy" ${module_file} ${module_file_static}
816+
COMMAND
817+
"${CMAKE_COMMAND}" "-E" "copy" ${module_doc_file} ${module_doc_file_static}
818+
COMMAND
819+
"${CMAKE_COMMAND}" "-E" "copy" ${interface_file} ${interface_file_static}
820+
OUTPUT ${module_outputs_static}
821+
DEPENDS
822+
"${module_dependency_target}"
823+
COMMENT "Generating ${module_file}")
824+
set("${dependency_module_target_out_var_name}" "${module_dependency_target_static}" PARENT_SCOPE)
825+
else()
826+
set("${dependency_module_target_out_var_name}" "${module_dependency_target}" PARENT_SCOPE)
827+
endif()
764828

765829
# macCatalyst zippered swiftmodule
766830
if(maccatalyst_build_flavor STREQUAL "zippered")

0 commit comments

Comments
 (0)