Skip to content

Commit f1d1df3

Browse files
authored
Merge pull request swiftlang#22842 from brentdax/target-practice
Name platform-specific module files using a normalized target triple
2 parents 8521015 + 6476098 commit f1d1df3

File tree

11 files changed

+397
-109
lines changed

11 files changed

+397
-109
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ ERROR(cannot_return_value_from_void_func,none,
586586

587587
ERROR(sema_no_import,Fatal,
588588
"no such module '%0'", (StringRef))
589-
ERROR(sema_no_import_arch,Fatal,
590-
"could not find module '%0' for architecture '%1'; "
589+
ERROR(sema_no_import_target,Fatal,
590+
"could not find module '%0' for target '%1'; "
591591
"found: %2", (StringRef, StringRef, StringRef))
592592
ERROR(sema_no_import_repl,none,
593593
"no such module '%0'", (StringRef))

include/swift/Basic/Platform.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ namespace swift {
7474
///
7575
/// This is a stop-gap until full Triple support (ala Clang) exists within swiftc.
7676
StringRef getMajorArchitectureName(const llvm::Triple &triple);
77+
78+
/// Computes the normalized target triple used as the most preferred name for
79+
/// module loading.
80+
///
81+
/// For platforms with fat binaries, this canonicalizes architecture,
82+
/// vendor, and OS names, strips OS versions, and makes inferred environments
83+
/// explicit. For other platforms, it returns the unmodified triple.
84+
///
85+
/// The input triple should already be "normalized" in the sense that
86+
/// llvm::Triple::normalize() would not affect it.
87+
llvm::Triple getTargetSpecificModuleTriple(const llvm::Triple &triple);
7788
} // end namespace swift
7889

7990
#endif // SWIFT_BASIC_PLATFORM_H

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ class SerializedModuleLoaderBase : public ModuleLoader {
6868
/// to list the architectures that \e are present.
6969
///
7070
/// \returns true if an error diagnostic was emitted
71-
virtual bool maybeDiagnoseArchitectureMismatch(SourceLoc sourceLocation,
72-
StringRef moduleName,
73-
StringRef archName,
74-
StringRef directoryPath) {
71+
virtual bool maybeDiagnoseTargetMismatch(SourceLoc sourceLocation,
72+
StringRef moduleName,
73+
StringRef archName,
74+
StringRef directoryPath) {
7575
return false;
7676
}
7777

@@ -139,10 +139,10 @@ class SerializedModuleLoader : public SerializedModuleLoaderBase {
139139
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
140140
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) override;
141141

142-
bool maybeDiagnoseArchitectureMismatch(SourceLoc sourceLocation,
143-
StringRef moduleName,
144-
StringRef archName,
145-
StringRef directoryPath) override;
142+
bool maybeDiagnoseTargetMismatch(SourceLoc sourceLocation,
143+
StringRef moduleName,
144+
StringRef archName,
145+
StringRef directoryPath) override;
146146

147147
public:
148148
virtual ~SerializedModuleLoader();

lib/Basic/Platform.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Basic/Platform.h"
14+
#include "llvm/ADT/StringExtras.h"
15+
#include "llvm/ADT/StringSwitch.h"
1416
#include "llvm/ADT/Triple.h"
1517

1618
using namespace swift;
@@ -188,3 +190,126 @@ StringRef swift::getMajorArchitectureName(const llvm::Triple &Triple) {
188190
return Triple.getArchName();
189191
}
190192
}
193+
194+
// The code below is responsible for normalizing target triples into the form
195+
// used to name target-specific swiftmodule, swiftinterface, and swiftdoc files.
196+
// If two triples have incompatible ABIs or can be distinguished by Swift #if
197+
// declarations, they should normalize to different values.
198+
//
199+
// This code is only really used on platforms with toolchains supporting fat
200+
// binaries (a single binary containing multiple architectures). On these
201+
// platforms, this code should strip unnecessary details from target triple
202+
// components and map synonyms to canonical values. Even values which don't need
203+
// any special canonicalization should be documented here as comments.
204+
//
205+
// (Fallback behavior does not belong here; it should be implemented in code
206+
// that calls this function, most importantly in SerializedModuleLoaderBase.)
207+
//
208+
// If you're trying to refer to this code to understand how Swift behaves and
209+
// you're unfamiliar with LLVM internals, here's a cheat sheet for reading it:
210+
//
211+
// * llvm::Triple is the type for a target name. It's a bit of a misnomer,
212+
// because it can contain three or four values: arch-vendor-os[-environment].
213+
//
214+
// * In .Cases and .Case, the last argument is the value the arguments before it
215+
// map to. That is, `.Cases("bar", "baz", "foo")` will return "foo" if it sees
216+
// "bar" or "baz".
217+
//
218+
// * llvm::Optional is similar to a Swift Optional: it either contains a value
219+
// or represents the absence of one. `None` is equivalent to `nil`; leading
220+
// `*` is equivalent to trailing `!`; conversion to `bool` is a not-`None`
221+
// check.
222+
223+
static StringRef
224+
getArchForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
225+
auto tripleArchName = triple.getArchName();
226+
227+
return llvm::StringSwitch<StringRef>(tripleArchName)
228+
.Cases("arm64", "aarch64", "arm64")
229+
.Cases("x86_64", "amd64", "x86_64")
230+
.Cases("i386", "i486", "i586", "i686", "i786", "i886", "i986",
231+
"i386")
232+
.Cases("unknown", "", "unknown")
233+
// These values are also supported, but are handled by the default case below:
234+
// .Case ("armv7s", "armv7s")
235+
// .Case ("armv7k", "armv7k")
236+
// .Case ("armv7", "armv7")
237+
.Default(tripleArchName);
238+
}
239+
240+
static StringRef
241+
getVendorForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
242+
// We unconditionally normalize to "apple" because it's relatively common for
243+
// build systems to omit the vendor name or use an incorrect one like
244+
// "unknown". Most parts of the compiler ignore the vendor, so you might not
245+
// notice such a mistake.
246+
//
247+
// Please don't depend on this behavior--specify 'apple' if you're building
248+
// for an Apple platform.
249+
250+
assert(triple.isOSDarwin() &&
251+
"shouldn't normalize non-Darwin triple to 'apple'");
252+
253+
return "apple";
254+
}
255+
256+
static StringRef
257+
getOSForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
258+
auto tripleOSName = triple.getOSName();
259+
260+
// Truncate the OS name before the first digit. "Digit" here is ASCII '0'-'9'.
261+
auto tripleOSNameNoVersion = tripleOSName.take_until(llvm::isDigit);
262+
263+
return llvm::StringSwitch<StringRef>(tripleOSNameNoVersion)
264+
.Cases("macos", "macosx", "darwin", "macos")
265+
.Cases("unknown", "", "unknown")
266+
// These values are also supported, but are handled by the default case below:
267+
// .Case ("ios", "ios")
268+
// .Case ("tvos", "tvos")
269+
// .Case ("watchos", "watchos")
270+
.Default(tripleOSNameNoVersion);
271+
}
272+
273+
static Optional<StringRef>
274+
getEnvironmentForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
275+
auto tripleEnvironment = triple.getEnvironmentName();
276+
277+
// If the environment is empty, infer a "simulator" environment based on the
278+
// OS and architecture combination. This feature is deprecated and exists for
279+
// backwards compatibility only; build systems should pass the "simulator"
280+
// environment explicitly if they know they're building for a simulator.
281+
if (tripleEnvironment == "" && swift::tripleIsAnySimulator(triple))
282+
return StringRef("simulator");
283+
284+
return llvm::StringSwitch<Optional<StringRef>>(tripleEnvironment)
285+
.Cases("unknown", "", None)
286+
// These values are also supported, but are handled by the default case below:
287+
// .Case ("simulator", StringRef("simulator"))
288+
.Default(tripleEnvironment);
289+
}
290+
291+
llvm::Triple swift::getTargetSpecificModuleTriple(const llvm::Triple &triple) {
292+
// isOSDarwin() returns true for all Darwin-style OSes, including macOS, iOS,
293+
// etc.
294+
if (triple.isOSDarwin()) {
295+
StringRef newArch = getArchForAppleTargetSpecificModuleTriple(triple);
296+
297+
StringRef newVendor = getVendorForAppleTargetSpecificModuleTriple(triple);
298+
299+
StringRef newOS = getOSForAppleTargetSpecificModuleTriple(triple);
300+
301+
Optional<StringRef> newEnvironment =
302+
getEnvironmentForAppleTargetSpecificModuleTriple(triple);
303+
304+
if (!newEnvironment)
305+
// Generate an arch-vendor-os triple.
306+
return llvm::Triple(newArch, newVendor, newOS);
307+
308+
// Generate an arch-vendor-os-environment triple.
309+
return llvm::Triple(newArch, newVendor, newOS, *newEnvironment);
310+
}
311+
312+
// Other platforms get no normalization.
313+
return triple;
314+
}
315+

0 commit comments

Comments
 (0)