Skip to content

Commit f6a1319

Browse files
authored
Merge pull request #68510 from kubamracek/embedded-stdlib-initial
[embedded] Build an initial embedded Swift standard library
2 parents 26653a7 + 89bdcb8 commit f6a1319

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1264
-255
lines changed

lib/Frontend/CompilerInvocation.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,15 @@ void CompilerInvocation::setDefaultBlocklistsIfNecessary() {
185185
}
186186

187187
static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
188-
llvm::Triple &Triple) {
188+
LangOptions &LangOpts) {
189+
llvm::Triple &Triple = LangOpts.Target;
189190
llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath);
190191

191192
StringRef LibSubDir = getPlatformNameForTriple(Triple);
192193
if (tripleIsMacCatalystEnvironment(Triple))
193194
LibSubDir = "maccatalyst";
195+
if (LangOpts.hasFeature(Feature::Embedded))
196+
LibSubDir = "embedded";
194197

195198
llvm::sys::path::append(LibPath, LibSubDir);
196199
SearchPathOpts.RuntimeLibraryPaths.clear();
@@ -294,7 +297,7 @@ setBridgingHeaderFromFrontendOptions(ClangImporterOptions &ImporterOpts,
294297

295298
void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
296299
SearchPathOpts.RuntimeResourcePath = Path.str();
297-
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
300+
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts);
298301
}
299302

300303
void CompilerInvocation::setTargetTriple(StringRef Triple) {
@@ -303,12 +306,12 @@ void CompilerInvocation::setTargetTriple(StringRef Triple) {
303306

304307
void CompilerInvocation::setTargetTriple(const llvm::Triple &Triple) {
305308
LangOpts.setTarget(Triple);
306-
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
309+
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts);
307310
}
308311

309312
void CompilerInvocation::setSDKPath(const std::string &Path) {
310313
SearchPathOpts.setSDKPath(Path);
311-
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
314+
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts);
312315
}
313316

314317
bool CompilerInvocation::setModuleAliasMap(std::vector<std::string> args,
@@ -1318,11 +1321,17 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
13181321

13191322
if (Opts.hasFeature(Feature::Embedded)) {
13201323
Opts.UnavailableDeclOptimizationMode = UnavailableDeclOptimization::Complete;
1324+
Opts.DisableImplicitStringProcessingModuleImport = true;
13211325

13221326
if (FrontendOpts.EnableLibraryEvolution) {
13231327
Diags.diagnose(SourceLoc(), diag::evolution_with_embedded);
13241328
HadError = true;
13251329
}
1330+
1331+
if (FrontendOpts.InputsAndOutputs.hasPrimaryInputs()) {
1332+
Diags.diagnose(SourceLoc(), diag::wmo_with_embedded);
1333+
HadError = true;
1334+
}
13261335
}
13271336

13281337
if (auto A = Args.getLastArg(OPT_checked_async_objc_bridging)) {
@@ -1340,15 +1349,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
13401349
}
13411350
}
13421351

1343-
// Is this the correct way to query for WMO?
1344-
bool isWMO =
1345-
Args.hasArg(OPT_wmo) ||
1346-
Args.hasArg(OPT_whole_module_optimization);
1347-
if (!isWMO && Opts.hasFeature(Feature::Embedded)) {
1348-
Diags.diagnose(SourceLoc(), diag::wmo_with_embedded);
1349-
HadError = true;
1350-
}
1351-
13521352
return HadError || UnsupportedOS || UnsupportedArch;
13531353
}
13541354

@@ -3098,7 +3098,7 @@ bool CompilerInvocation::parseArgs(
30983098
return true;
30993099
}
31003100

3101-
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
3101+
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts);
31023102
setDefaultPrebuiltCacheIfNecessary();
31033103
setDefaultBlocklistsIfNecessary();
31043104

@@ -3107,6 +3107,7 @@ bool CompilerInvocation::parseArgs(
31073107
setBridgingHeaderFromFrontendOptions(ClangImporterOpts, FrontendOpts);
31083108
if (LangOpts.hasFeature(Feature::Embedded)) {
31093109
IRGenOpts.InternalizeAtLink = true;
3110+
IRGenOpts.DisableLegacyTypeInfo = true;
31103111
}
31113112

31123113
return false;

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ bool CompilerInvocation::shouldImportSwiftONoneSupport() const {
10421042
return false;
10431043
if (getSILOptions().shouldOptimize())
10441044
return false;
1045+
if (LangOpts.hasFeature(Feature::Embedded))
1046+
return false;
10451047

10461048
// If we are not executing an action that has a dependency on
10471049
// SwiftOnoneSupport, don't load it.

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,9 @@ endfunction()
679679
# STATIC
680680
# Build a static library.
681681
#
682+
# ONLY_SWIFTMODULE
683+
# Do not build either static or shared, build just the .swiftmodule.
684+
#
682685
# SDK sdk
683686
# SDK to build for.
684687
#
@@ -744,6 +747,7 @@ function(add_swift_target_library_single target name)
744747
OBJECT_LIBRARY
745748
SHARED
746749
STATIC
750+
ONLY_SWIFTMODULE
747751
NO_LINK_NAME
748752
INSTALL_WITH_SHARED
749753
IS_FRAGILE)
@@ -760,6 +764,7 @@ function(add_swift_target_library_single target name)
760764
MACCATALYST_BUILD_FLAVOR
761765
BACK_DEPLOYMENT_LIBRARY
762766
ENABLE_LTO
767+
MODULE_DIR
763768
BOOTSTRAPPING)
764769
set(SWIFTLIB_SINGLE_multiple_parameter_options
765770
C_COMPILE_FLAGS
@@ -814,7 +819,8 @@ function(add_swift_target_library_single target name)
814819

815820
if(NOT SWIFTLIB_SINGLE_SHARED AND
816821
NOT SWIFTLIB_SINGLE_STATIC AND
817-
NOT SWIFTLIB_SINGLE_OBJECT_LIBRARY)
822+
NOT SWIFTLIB_SINGLE_OBJECT_LIBRARY AND
823+
NOT SWIFTLIB_SINGLE_ONLY_SWIFTMODULE)
818824
message(FATAL_ERROR
819825
"Either SHARED, STATIC, or OBJECT_LIBRARY must be specified")
820826
endif()
@@ -878,6 +884,8 @@ function(add_swift_target_library_single target name)
878884
set(libkind SHARED)
879885
elseif(SWIFTLIB_SINGLE_STATIC)
880886
set(libkind STATIC)
887+
elseif(SWIFTLIB_SINGLE_ONLY_SWIFTMODULE)
888+
set(libkind NONE)
881889
else()
882890
message(FATAL_ERROR
883891
"Either SHARED, STATIC, or OBJECT_LIBRARY must be specified")
@@ -953,11 +961,13 @@ function(add_swift_target_library_single target name)
953961
SDK ${SWIFTLIB_SINGLE_SDK}
954962
ARCHITECTURE ${SWIFTLIB_SINGLE_ARCHITECTURE}
955963
MODULE_NAME ${module_name}
964+
MODULE_DIR ${SWIFTLIB_SINGLE_MODULE_DIR}
956965
COMPILE_FLAGS ${SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS}
957966
${SWIFTLIB_SINGLE_IS_STDLIB_keyword}
958967
${SWIFTLIB_SINGLE_IS_STDLIB_CORE_keyword}
959968
${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword}
960969
${SWIFTLIB_SINGLE_IS_FRAGILE_keyword}
970+
${SWIFTLIB_SINGLE_ONLY_SWIFTMODULE_keyword}
961971
${embed_bitcode_arg}
962972
${SWIFTLIB_SINGLE_STATIC_keyword}
963973
${SWIFTLIB_SINGLE_NO_LINK_NAME_keyword}
@@ -1035,6 +1045,12 @@ function(add_swift_target_library_single target name)
10351045
${SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS_SHARED_ONLY})
10361046
endif()
10371047

1048+
if (SWIFTLIB_SINGLE_ONLY_SWIFTMODULE)
1049+
add_custom_target("${target}"
1050+
DEPENDS "${swift_module_dependency_target}")
1051+
return()
1052+
endif()
1053+
10381054
add_library("${target}" ${libkind}
10391055
${SWIFTLIB_SINGLE_SOURCES}
10401056
${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ function(handle_swift_sources
4949
dependency_sibgen_target_out_var_name
5050
sourcesvar externalvar name)
5151
cmake_parse_arguments(SWIFTSOURCES
52-
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC;NO_LINK_NAME;IS_FRAGILE"
52+
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC;NO_LINK_NAME;IS_FRAGILE;ONLY_SWIFTMODULE"
5353
"SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR;BOOTSTRAPPING"
54-
"DEPENDS;COMPILE_FLAGS;MODULE_NAME;ENABLE_LTO"
54+
"DEPENDS;COMPILE_FLAGS;MODULE_NAME;MODULE_DIR;ENABLE_LTO"
5555
${ARGN})
5656
translate_flag(${SWIFTSOURCES_IS_MAIN} "IS_MAIN" IS_MAIN_arg)
5757
translate_flag(${SWIFTSOURCES_IS_STDLIB} "IS_STDLIB" IS_STDLIB_arg)
@@ -65,6 +65,7 @@ function(handle_swift_sources
6565
STATIC_arg)
6666
translate_flag(${SWIFTSOURCES_NO_LINK_NAME} "NO_LINK_NAME" NO_LINK_NAME_arg)
6767
translate_flag(${SWIFTSOURCES_IS_FRAGILE} "IS_FRAGILE" IS_FRAGILE_arg)
68+
translate_flag(${SWIFTSOURCES_ONLY_SWIFTMODULE} "ONLY_SWIFTMODULE" ONLY_SWIFTMODULE_arg)
6869
if(DEFINED SWIFTSOURCES_BOOTSTRAPPING)
6970
set(BOOTSTRAPPING_arg "BOOTSTRAPPING" ${SWIFTSOURCES_BOOTSTRAPPING})
7071
endif()
@@ -146,6 +147,7 @@ function(handle_swift_sources
146147
SDK ${SWIFTSOURCES_SDK}
147148
ARCHITECTURE ${SWIFTSOURCES_ARCHITECTURE}
148149
MODULE_NAME ${SWIFTSOURCES_MODULE_NAME}
150+
MODULE_DIR ${SWIFTSOURCES_MODULE_DIR}
149151
${IS_MAIN_arg}
150152
${IS_STDLIB_arg}
151153
${IS_STDLIB_CORE_arg}
@@ -154,6 +156,7 @@ function(handle_swift_sources
154156
${STATIC_arg}
155157
${BOOTSTRAPPING_arg}
156158
${IS_FRAGILE_arg}
159+
${ONLY_SWIFTMODULE_arg}
157160
INSTALL_IN_COMPONENT "${SWIFTSOURCES_INSTALL_IN_COMPONENT}"
158161
MACCATALYST_BUILD_FLAVOR "${SWIFTSOURCES_MACCATALYST_BUILD_FLAVOR}")
159162
set("${dependency_target_out_var_name}" "${dependency_target}" PARENT_SCOPE)
@@ -378,7 +381,7 @@ function(_compile_swift_files
378381
dependency_sib_target_out_var_name dependency_sibopt_target_out_var_name
379382
dependency_sibgen_target_out_var_name)
380383
cmake_parse_arguments(SWIFTFILE
381-
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC;IS_FRAGILE"
384+
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMBED_BITCODE;STATIC;IS_FRAGILE;ONLY_SWIFTMODULE"
382385
"OUTPUT;MODULE_NAME;INSTALL_IN_COMPONENT;MACCATALYST_BUILD_FLAVOR;BOOTSTRAPPING"
383386
"SOURCES;FLAGS;DEPENDS;SDK;ARCHITECTURE;OPT_FLAGS;MODULE_DIR"
384387
${ARGN})
@@ -903,13 +906,7 @@ function(_compile_swift_files
903906
endif()
904907
endif()
905908

906-
# First generate the obj dirs
907909
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}")
913910

914911
# Then we can compile both the object files and the swiftmodule files
915912
# in parallel in this target for the object file, and ...
@@ -940,8 +937,10 @@ function(_compile_swift_files
940937
set(copy_legacy_layouts_dep)
941938
endif()
942939

940+
if(NOT SWIFTFILE_ONLY_SWIFTMODULE)
943941
add_custom_command_target(
944942
dependency_target
943+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${dirs_to_create}
945944
COMMAND
946945
${set_environment_args}
947946
"$<TARGET_FILE:Python3::Interpreter>" "${line_directive_tool}" "@${file_path}" --
@@ -955,10 +954,10 @@ function(_compile_swift_files
955954
${swift_compiler_tool_dep}
956955
${source_files} ${SWIFTFILE_DEPENDS}
957956
${swift_ide_test_dependency}
958-
${create_dirs_dependency_target}
959957
${copy_legacy_layouts_dep}
960958
COMMENT "Compiling ${first_output}")
961959
set("${dependency_target_out_var_name}" "${dependency_target}" PARENT_SCOPE)
960+
endif()
962961

963962
# This is the target to generate:
964963
#
@@ -978,6 +977,7 @@ function(_compile_swift_files
978977
if (NOT SWIFTFILE_IS_MAIN)
979978
add_custom_command_target(
980979
module_dependency_target
980+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${dirs_to_create}
981981
COMMAND
982982
"${CMAKE_COMMAND}" "-E" "remove" "-f" ${module_outputs}
983983
COMMAND
@@ -997,7 +997,6 @@ function(_compile_swift_files
997997
${swift_compiler_tool_dep}
998998
${source_files} ${SWIFTFILE_DEPENDS}
999999
${swift_ide_test_dependency}
1000-
${create_dirs_dependency_target}
10011000
${copy_legacy_layouts_dep}
10021001
COMMENT "Generating ${module_file}")
10031002

@@ -1010,6 +1009,7 @@ function(_compile_swift_files
10101009
endif()
10111010
add_custom_command_target(
10121011
module_dependency_target_static
1012+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${dirs_to_create}
10131013
COMMAND
10141014
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
10151015
${specific_module_dir_static}
@@ -1026,7 +1026,6 @@ function(_compile_swift_files
10261026
${swift_compiler_tool_dep}
10271027
${source_files} ${SWIFTFILE_DEPENDS}
10281028
${swift_ide_test_dependency}
1029-
${create_dirs_dependency_target}
10301029
${copy_legacy_layouts_dep}
10311030
COMMENT "Generating ${module_file}")
10321031
set("${dependency_module_target_out_var_name}" "${module_dependency_target_static}" PARENT_SCOPE)
@@ -1084,6 +1083,7 @@ function(_compile_swift_files
10841083
# This is the target to generate the .sib files. It is not built by default.
10851084
add_custom_command_target(
10861085
sib_dependency_target
1086+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${dirs_to_create}
10871087
COMMAND
10881088
${set_environment_args}
10891089
"$<TARGET_FILE:Python3::Interpreter>" "${line_directive_tool}" "@${file_path}" --
@@ -1096,14 +1096,14 @@ function(_compile_swift_files
10961096
"${file_path}"
10971097
${swift_compiler_tool_dep}
10981098
${source_files} ${SWIFTFILE_DEPENDS}
1099-
${create_dirs_dependency_target}
11001099
${copy_legacy_layouts_dep}
11011100
COMMENT "Generating ${sib_file}"
11021101
EXCLUDE_FROM_ALL)
11031102
set("${dependency_sib_target_out_var_name}" "${sib_dependency_target}" PARENT_SCOPE)
11041103

11051104
add_custom_command_target(
11061105
sibopt_dependency_target
1106+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${dirs_to_create}
11071107
COMMAND
11081108
${set_environment_args}
11091109
"$<TARGET_FILE:Python3::Interpreter>" "${line_directive_tool}" "@${file_path}" --
@@ -1116,7 +1116,6 @@ function(_compile_swift_files
11161116
"${file_path}"
11171117
${swift_compiler_tool_dep}
11181118
${source_files} ${SWIFTFILE_DEPENDS}
1119-
${create_dirs_dependency_target}
11201119
${copy_legacy_layouts_dep}
11211120
COMMENT "Generating ${sibopt_file}"
11221121
EXCLUDE_FROM_ALL)
@@ -1125,6 +1124,7 @@ function(_compile_swift_files
11251124
# This is the target to generate the .sibgen files. It is not built by default.
11261125
add_custom_command_target(
11271126
sibgen_dependency_target
1127+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${dirs_to_create}
11281128
COMMAND
11291129
${set_environment_args}
11301130
"$<TARGET_FILE:Python3::Interpreter>" "${line_directive_tool}" "@${file_path}" --
@@ -1137,7 +1137,6 @@ function(_compile_swift_files
11371137
"${file_path}"
11381138
${swift_compiler_tool_dep}
11391139
${source_files} ${SWIFTFILE_DEPENDS}
1140-
${create_dirs_dependency_target}
11411140
${copy_legacy_layouts_dep}
11421141
COMMENT "Generating ${sibgen_file}"
11431142
EXCLUDE_FROM_ALL)

0 commit comments

Comments
 (0)