Skip to content

Commit 28f9641

Browse files
authored
Merge pull request #79778 from etcwilde/ewilde/yo-dawg-heard-you-liked-swift
CMake: option to disable swift in swift
2 parents 1fc5a4e + ddaf003 commit 28f9641

21 files changed

+141
-70
lines changed

CMakeLists.txt

+58-46
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,24 @@ option(SWIFT_TOOLS_LD64_LTO_CODEGEN_ONLY_FOR_SUPPORTING_TARGETS
352352
debugging Swift)"
353353
FALSE)
354354

355-
set(BOOTSTRAPPING_MODE HOSTTOOLS CACHE STRING [=[
356-
How to build the swift compiler modules. Possible values are
357-
HOSTTOOLS: build with a pre-installed toolchain
358-
BOOTSTRAPPING: build with a 2-stage bootstrapping process
359-
BOOTSTRAPPING-WITH-HOSTLIBS: build with a 2-stage bootstrapping process,
360-
but the compiler links against the host system swift libs (macOS only)
361-
CROSSCOMPILE: cross-compiledwith a native host compiler, provided in
362-
`SWIFT_NATIVE_SWIFT_TOOLS_PATH` (non-Darwin only)
363-
CROSSCOMPILE-WITH-HOSTLIBS: build with a bootstrapping-with-hostlibs compiled
364-
compiler, provided in `SWIFT_NATIVE_SWIFT_TOOLS_PATH`
365-
]=])
355+
option(SWIFT_ENABLE_SWIFT_IN_SWIFT "Enable Swift sources in Swift compiler" ON)
356+
357+
if(SWIFT_ENABLE_SWIFT_IN_SWIFT)
358+
set(BOOTSTRAPPING_MODE HOSTTOOLS CACHE STRING [=[
359+
How to build the swift compiler modules. Possible values are
360+
HOSTTOOLS: build with a pre-installed toolchain
361+
BOOTSTRAPPING: build with a 2-stage bootstrapping process
362+
BOOTSTRAPPING-WITH-HOSTLIBS: build with a 2-stage bootstrapping process,
363+
but the compiler links against the host system swift libs (macOS only)
364+
CROSSCOMPILE: cross-compiledwith a native host compiler, provided in
365+
`SWIFT_NATIVE_SWIFT_TOOLS_PATH` (non-Darwin only)
366+
CROSSCOMPILE-WITH-HOSTLIBS: build with a bootstrapping-with-hostlibs compiled
367+
compiler, provided in `SWIFT_NATIVE_SWIFT_TOOLS_PATH`
368+
]=])
369+
else()
370+
set(BOOTSTRAPPING_MODE OFF)
371+
set(SWIFT_BUILD_SWIFT_SYNTAX OFF)
372+
endif()
366373

367374
option(BRIDGING_MODE [=[
368375
How swift-C++ bridging code is compiled:
@@ -939,41 +946,44 @@ set(SWIFT_MAIN_INCLUDE_DIR "${SWIFT_SOURCE_DIR}/include")
939946
set(SWIFT_SHIMS_INCLUDE_DIR "${SWIFT_SOURCE_DIR}/stdlib/public/SwiftShims")
940947
set(SWIFT_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/include")
941948

942-
if (NOT BOOTSTRAPPING_MODE)
949+
if (NOT BOOTSTRAPPING_MODE AND SWIFT_ENABLE_SWIFT_IN_SWIFT)
943950
message(FATAL_ERROR "turning off bootstrapping is not supported anymore")
944951
endif()
945952

946953
set(SWIFT_RUNTIME_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin")
947954
set(SWIFT_LIBRARY_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib")
948-
if("${SWIFT_NATIVE_SWIFT_TOOLS_PATH}" STREQUAL "")
949-
# This is the normal case. We are not cross-compiling.
950-
set(SWIFT_NATIVE_SWIFT_TOOLS_PATH "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
951-
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
952-
if(NOT SWIFT_EXEC_FOR_SWIFT_MODULES)
953-
message(WARNING "BOOTSTRAPPING set to OFF because no Swift compiler is defined")
954-
set(BOOTSTRAPPING_MODE "OFF")
955-
endif()
956-
elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
957-
# If cross-compiling, we don't have to bootstrap. We can just use the previously
958-
# built native swiftc to build the swift compiler modules.
959-
message(STATUS "Building swift modules with previously built tools instead of bootstrapping")
960-
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
961-
if(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
962-
set(BOOTSTRAPPING_MODE "CROSSCOMPILE-WITH-HOSTLIBS")
963-
elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING")
964-
set(BOOTSTRAPPING_MODE "CROSSCOMPILE")
965-
else()
966-
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
955+
956+
if(SWIFT_ENABLE_SWIFT_IN_SWIFT)
957+
if(NOT SWIFT_NATIVE_SWIFT_TOOLS_PATH)
958+
# This is the normal case. We are not cross-compiling.
959+
set(SWIFT_NATIVE_SWIFT_TOOLS_PATH "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
960+
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
961+
if(NOT SWIFT_EXEC_FOR_SWIFT_MODULES)
962+
message(WARNING "BOOTSTRAPPING set to OFF because no Swift compiler is defined")
963+
set(BOOTSTRAPPING_MODE "OFF")
964+
endif()
965+
elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
966+
# If cross-compiling, we don't have to bootstrap. We can just use the previously
967+
# built native swiftc to build the swift compiler modules.
968+
message(STATUS "Building swift modules with previously built tools instead of bootstrapping")
969+
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
970+
if(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
971+
set(BOOTSTRAPPING_MODE "CROSSCOMPILE-WITH-HOSTLIBS")
972+
elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING")
973+
set(BOOTSTRAPPING_MODE "CROSSCOMPILE")
974+
else()
975+
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
976+
endif()
977+
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX)
978+
# We are building using a pre-installed host toolchain but not bootstrapping
979+
# the Swift modules. This happens when building using 'build-tooling-libs'
980+
# where we haven't built a new Swift compiler. Use the Swift compiler from the
981+
# pre-installed host toolchain to build the Swift modules.
982+
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
967983
endif()
968-
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX)
969-
# We are building using a pre-installed host toolchain but not bootstrapping
970-
# the Swift modules. This happens when building using 'build-tooling-libs'
971-
# where we haven't built a new Swift compiler. Use the Swift compiler from the
972-
# pre-installed host toolchain to build the Swift modules.
973-
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
974984
endif()
975985

976-
if(SWIFT_INCLUDE_TOOLS AND SWIFT_BUILD_SWIFT_SYNTAX)
986+
if(SWIFT_INCLUDE_TOOLS AND SWIFT_BUILD_SWIFT_SYNTAX AND SWIFT_ENABLE_SWIFT_IN_SWIFT)
977987
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
978988
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
979989
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
@@ -1530,14 +1540,16 @@ if(SWIFT_INCLUDE_TOOLS)
15301540
# https://github.com/apple/swift/issues/48534
15311541
add_subdirectory(tools)
15321542

1533-
# Localization targets are configured in a way that assume the swift
1534-
# frontend is being built, so trying to include them for other builds
1535-
# (like stdlib) fail!
1536-
#
1537-
# Diagnostics information is only useful for the frontend compiler
1538-
# anyway, so let's only include it if the compiler is being built,
1539-
# which at the moment seems like if SWIFT_INCLUDE_TOOLS is defined.
1540-
add_subdirectory(localization)
1543+
if(SWIFT_NATIVE_SWIFT_TOOLS_PATH)
1544+
# Localization targets are configured in a way that assume the swift
1545+
# frontend is being built, so trying to include them for other builds
1546+
# (like stdlib) fail!
1547+
#
1548+
# Diagnostics information is only useful for the frontend compiler
1549+
# anyway, so let's only include it if the compiler is being built,
1550+
# which at the moment seems like if SWIFT_INCLUDE_TOOLS is defined.
1551+
add_subdirectory(localization)
1552+
endif()
15411553
endif()
15421554

15431555
add_subdirectory(utils)

lib/AST/ASTScope.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ void ASTScope::unqualifiedLookup(
148148
if (auto *s = SF->getASTContext().Stats)
149149
++s->getFrontendCounters().NumASTScopeLookups;
150150

151+
#if SWIFT_BUILD_SWIFT_SYNTAX
151152
// Perform validation of SwiftLexicalLookup if option
152153
// Feature::UnqualifiedLookupValidation is enabled and lookup was not
153154
// performed in a macro.
@@ -171,6 +172,9 @@ void ASTScope::unqualifiedLookup(
171172
} else {
172173
ASTScopeImpl::unqualifiedLookup(SF, loc, consumer);
173174
}
175+
#else
176+
ASTScopeImpl::unqualifiedLookup(SF, loc, consumer);
177+
#endif
174178
}
175179

176180
llvm::SmallVector<LabeledStmt *, 4> ASTScope::lookupLabeledStmts(

lib/Parse/ParseExpr.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -3474,6 +3474,20 @@ ParserResult<Expr> Parser::parseExprMacroExpansion(bool isExprBasic) {
34743474
if (!macroNameRef)
34753475
return status;
34763476

3477+
#if !SWIFT_BUILD_SWIFT_SYNTAX
3478+
// If we don't have swift-syntax and therefore have no support for macros,
3479+
// recognize #isolation as special and route it through
3480+
// CurrentContextIsolationExpr.
3481+
if (macroNameRef.getBaseName().userFacingName() == "isolation" &&
3482+
genericArgs.empty() &&
3483+
(!argList || argList->empty())) {
3484+
return makeParserResult(
3485+
status,
3486+
new (Context) CurrentContextIsolationExpr(
3487+
macroNameLoc.getStartLoc(), Type()));
3488+
}
3489+
#endif
3490+
34773491
return makeParserResult(
34783492
status,
34793493
MacroExpansionExpr::create(

lib/Sema/TypeChecker.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ std::pair<bool, bool> EvaluateIfConditionRequest::evaluate(
755755
Evaluator &evaluator, SourceFile *sourceFile, SourceRange conditionRange,
756756
bool shouldEvaluate
757757
) const {
758+
#if SWIFT_BUILD_SWIFT_SYNTAX
758759
// FIXME: When we migrate to SwiftParser, use the parsed syntax tree.
759760
ASTContext &ctx = sourceFile->getASTContext();
760761
auto &sourceMgr = ctx.SourceMgr;
@@ -775,4 +776,7 @@ std::pair<bool, bool> EvaluateIfConditionRequest::evaluate(
775776
bool isActive = (evalResult & 0x01) != 0;
776777
bool allowSyntaxErrors = (evalResult & 0x02) != 0;
777778
return std::pair(isActive, allowSyntaxErrors);
779+
#else
780+
llvm_unreachable("Must not be used in C++-only build");
781+
#endif
778782
}

stdlib/cmake/modules/SwiftSource.cmake

+10-3
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,12 @@ function(_compile_swift_files
856856
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
857857
set(HOST_EXECUTABLE_SUFFIX .exe)
858858
endif()
859-
if(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER)
859+
if(NOT SWIFT_ENABLE_SWIFT_IN_SWIFT)
860+
# This is only for bootstrapping purposes. The just-built Swift is very
861+
# limited and only built for the builder to build the next stages with
862+
# hosttools.
863+
set(swift_compiler_tool "${Swift_BINARY_DIR}/bin/swiftc")
864+
elseif(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER)
860865
if(SWIFT_PREBUILT_SWIFT)
861866
set(swift_compiler_tool "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc${HOST_EXECUTABLE_SUFFIX}")
862867
elseif(CMAKE_Swift_COMPILER)
@@ -886,8 +891,10 @@ function(_compile_swift_files
886891
# cross-compiling the compiler.
887892
list(APPEND swift_compiler_tool_dep "swift-frontend${target_suffix}")
888893

889-
# If we aren't cross compiling, also depend on SwiftMacros.
890-
list(APPEND swift_compiler_tool_dep SwiftMacros)
894+
if(SWIFT_ENABLE_SWIFT_IN_SWIFT)
895+
# If we aren't cross compiling, also depend on SwiftMacros.
896+
list(APPEND swift_compiler_tool_dep SwiftMacros)
897+
endif()
891898
endif()
892899

893900
# If there are more than one output files, we assume that they are specified

stdlib/public/core/Availability.swift

+8
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public func _stdlib_isOSVersionAtLeast(
5252
@_semantics("availability.osversion")
5353
@_effects(readnone)
5454
@_unavailableInEmbedded
55+
#if hasFeature(Macros)
5556
@_noLocks
57+
#endif
5658
public func _stdlib_isOSVersionAtLeast(
5759
_ major: Builtin.Word,
5860
_ minor: Builtin.Word,
@@ -65,7 +67,9 @@ public func _stdlib_isOSVersionAtLeast(
6567
@_semantics("availability.osversion")
6668
@_effects(readnone)
6769
@_alwaysEmitIntoClient
70+
#if hasFeature(Macros)
6871
@_noLocks
72+
#endif
6973
public func _stdlib_isOSVersionAtLeast_AEIC(
7074
_ major: Builtin.Word,
7175
_ minor: Builtin.Word,
@@ -110,7 +114,9 @@ public func _stdlib_isOSVersionAtLeast_AEIC(
110114
@_semantics("availability.osversion")
111115
@_effects(readnone)
112116
@available(macOS 10.15, iOS 13.0, *)
117+
#if hasFeature(Macros)
113118
@_noLocks
119+
#endif
114120
public func _stdlib_isVariantOSVersionAtLeast(
115121
_ major: Builtin.Word,
116122
_ minor: Builtin.Word,
@@ -153,7 +159,9 @@ public func _stdlib_isVariantOSVersionAtLeast(
153159
@_semantics("availability.osversion")
154160
@_effects(readnone)
155161
@_unavailableInEmbedded
162+
#if hasFeature(Macros)
156163
@_noLocks
164+
#endif
157165
public func _stdlib_isOSVersionAtLeastOrVariantVersionAtLeast(
158166
_ major: Builtin.Word,
159167
_ minor: Builtin.Word,

stdlib/public/core/CollectionAlgorithms.swift

+1
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ extension MutableCollection where Self: BidirectionalCollection {
441441
swapAt(lo, hi)
442442
formIndex(after: &lo)
443443
}
444+
fatalError()
444445
}
445446
}
446447

stdlib/public/core/Flatten.swift

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extension FlattenSequence.Iterator: IteratorProtocol {
8585
_inner = s!.makeIterator()
8686
}
8787
while true
88+
fatalError()
8889
}
8990
}
9091

stdlib/public/core/HashTable.swift

+2
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ extension _HashTable {
387387
return unsafe Bucket(word: word, bit: bit)
388388
}
389389
}
390+
fatalError()
390391
}
391392

392393
@inlinable
@@ -414,6 +415,7 @@ extension _HashTable {
414415
return unsafe Bucket(word: word, bit: bit)
415416
}
416417
}
418+
fatalError()
417419
}
418420
}
419421

stdlib/public/core/Join.swift

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ extension JoinedSequence.Iterator: IteratorProtocol {
125125
return nil
126126
}
127127
}
128+
fatalError()
128129
}
129130
}
130131

stdlib/public/core/KeyPath.swift

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ public class AnyKeyPath: _AppendKeyPath {
193193

194194
if optNextType == nil { return .some(offset) }
195195
}
196+
fatalError()
196197
}
197198
#else
198199
// compiler optimizes _storedInlineOffset into a direct offset computation,
@@ -271,6 +272,7 @@ extension AnyKeyPath: Hashable {
271272
return true
272273
}
273274
}
275+
fatalError()
274276
}
275277
}
276278
}
@@ -432,6 +434,7 @@ public class KeyPath<Root, Value>: PartialKeyPath<Root> {
432434
}
433435
}
434436
}
437+
fatalError()
435438
}
436439
}
437440
}

stdlib/public/core/ObjectIdentifier+DebugDescription.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if !$Embedded
13+
#if !$Embedded && hasFeature(Macros)
1414
@DebugDescription
1515
extension ObjectIdentifier {
1616
var lldbDescription: String {

stdlib/public/core/SequenceAlgorithms.swift

+2
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ extension Sequence {
332332
case (nil, nil): return true
333333
}
334334
}
335+
fatalError()
335336
}
336337
}
337338

@@ -425,6 +426,7 @@ extension Sequence {
425426
return false
426427
}
427428
}
429+
fatalError()
428430
}
429431
}
430432

stdlib/public/core/StringBridge.swift

+2
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ extension StringProtocol {
752752

753753
public // SPI(Foundation)
754754
func _toUTF16Indices(_ range: Range<Int>) -> Range<Index> {
755+
#if hasFeature(Macros)
755756
if Self.self == String.self {
756757
let s = unsafe unsafeBitCast(self, to: String.self)
757758
return s.utf16._indexRange(for: range, from: s.startIndex)
@@ -760,6 +761,7 @@ extension StringProtocol {
760761
let s = unsafe unsafeBitCast(self, to: Substring.self)
761762
return s._slice._base.utf16._indexRange(for: range, from: s.startIndex)
762763
}
764+
#endif
763765
let lowerbound = _toUTF16Index(range.lowerBound)
764766
let upperbound = _toUTF16Index(range.upperBound)
765767
return unsafe Range(uncheckedBounds: (lower: lowerbound, upper: upperbound))

stdlib/public/core/StringComparison.swift

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ private func _findBoundary(
265265

266266
unsafe idx &-= _utf8ScalarLength(utf8, endingAt: idx)
267267
}
268+
fatalError()
268269
}
269270

270271
@frozen

stdlib/public/core/StringUTF16View.swift

+1
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ extension String.UTF16View {
945945

946946
readIdx &+= len
947947
}
948+
fatalError()
948949
}
949950
}
950951

stdlib/public/core/SwiftifyImport.swift

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public enum _SwiftifyInfo {
5858
///
5959
/// Parameter paramInfo: information about how the function uses the pointer passed to it. The
6060
/// safety of the generated wrapper function depends on this info being extensive and accurate.
61+
#if hasFeature(Macros)
6162
@attached(peer, names: overloaded)
6263
public macro _SwiftifyImport(_ paramInfo: _SwiftifyInfo..., typeMappings: [String: String] = [:]) =
6364
#externalMacro(module: "SwiftMacros", type: "SwiftifyImportMacro")
65+
#endif

stdlib/public/core/UTF16.swift

+1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ extension Unicode.UTF16 {
256256
return (utf16Count, utf16BitUnion < 0x80)
257257
}
258258
}
259+
fatalError()
259260
}
260261
}
261262

0 commit comments

Comments
 (0)