Skip to content

Commit c92e5b4

Browse files
authored
Merge pull request #84574 from madsodgaard/android-availability
1 parent 4403625 commit c92e5b4

File tree

19 files changed

+189
-5
lines changed

19 files changed

+189
-5
lines changed

include/swift/AST/PlatformKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ AVAILABILITY_PLATFORM(macCatalystApplicationExtension, "application extensions f
3737
AVAILABILITY_PLATFORM(FreeBSD, "FreeBSD")
3838
AVAILABILITY_PLATFORM(OpenBSD, "OpenBSD")
3939
AVAILABILITY_PLATFORM(Windows, "Windows")
40+
AVAILABILITY_PLATFORM(Android, "Android")
4041

4142
#undef AVAILABILITY_PLATFORM

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,16 @@ FUNCTION(TSanInoutAccess, tsan, __tsan_external_write, C_CC, AlwaysAvailable,
21332133
EFFECT(RuntimeEffect::NoEffect),
21342134
UNKNOWN_MEMEFFECTS)
21352135

2136+
// int32 __isOSVersionAtLeast(uint32_t major, uint32_t minor, uint32_t patch);
2137+
// This a C builtin provided by compiler-rt.
2138+
FUNCTION(OSVersionAtLeast, libgcc, __isOSVersionAtLeast,
2139+
C_CC, AlwaysAvailable,
2140+
RETURNS(Int32Ty),
2141+
ARGS(Int32Ty, Int32Ty, Int32Ty),
2142+
ATTRS(NoUnwind),
2143+
EFFECT(RuntimeEffect::NoEffect),
2144+
UNKNOWN_MEMEFFECTS)
2145+
21362146
// int32 __isPlatformVersionAtLeast(uint32_t platform, uint32_t major,
21372147
// uint32_t minor, uint32_t patch);
21382148
// This a C builtin provided by compiler-rt.

lib/AST/PlatformKindUtils.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ swift::basePlatformForExtensionPlatform(PlatformKind Platform) {
118118
case PlatformKind::FreeBSD:
119119
case PlatformKind::OpenBSD:
120120
case PlatformKind::Windows:
121+
case PlatformKind::Android:
121122
case PlatformKind::none:
122123
return std::nullopt;
123124
}
@@ -164,6 +165,8 @@ static bool isPlatformActiveForTarget(PlatformKind Platform,
164165
return Target.isOSFreeBSD();
165166
case PlatformKind::Windows:
166167
return Target.isOSWindows();
168+
case PlatformKind::Android:
169+
return Target.isAndroid();
167170
case PlatformKind::none:
168171
llvm_unreachable("handled above");
169172
}
@@ -219,6 +222,10 @@ static PlatformKind platformForTriple(const llvm::Triple &triple,
219222
: PlatformKind::visionOS);
220223
}
221224

225+
if (triple.isAndroid()) {
226+
return PlatformKind::Android;
227+
}
228+
222229
return PlatformKind::none;
223230
}
224231

@@ -291,6 +298,8 @@ swift::tripleOSTypeForPlatform(PlatformKind platform) {
291298
return llvm::Triple::OpenBSD;
292299
case PlatformKind::Windows:
293300
return llvm::Triple::Win32;
301+
case PlatformKind::Android:
302+
return llvm::Triple::Linux;
294303
case PlatformKind::none:
295304
return std::nullopt;
296305
}
@@ -326,6 +335,7 @@ bool swift::isPlatformSPI(PlatformKind Platform) {
326335
case PlatformKind::OpenBSD:
327336
case PlatformKind::FreeBSD:
328337
case PlatformKind::Windows:
338+
case PlatformKind::Android:
329339
case PlatformKind::none:
330340
return false;
331341
}

lib/Basic/Platform.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ llvm::VersionTuple swift::getVersionForTriple(const llvm::Triple &triple) {
301301
return triple.getOSVersion();
302302
} else if (triple.isOSWindows()) {
303303
return triple.getOSVersion();
304+
} else if (triple.isAndroid()) {
305+
return triple.getEnvironmentVersion();
304306
}
305307
return llvm::VersionTuple(/*Major=*/0, /*Minor=*/0, /*Subminor=*/0);
306308
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,12 @@ void importer::getNormalInvocationArguments(
665665
});
666666
}
667667

668+
if (triple.isAndroid()) {
669+
invocationArgStrs.insert(invocationArgStrs.end(), {
670+
"-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
671+
});
672+
}
673+
668674
if (triple.isOSWindows()) {
669675
switch (triple.getArch()) {
670676
default: llvm_unreachable("unsupported Windows architecture");
@@ -2568,6 +2574,10 @@ PlatformAvailability::PlatformAvailability(const LangOptions &langOpts)
25682574
deprecatedAsUnavailableMessage = "";
25692575
break;
25702576

2577+
case PlatformKind::Android:
2578+
deprecatedAsUnavailableMessage = "";
2579+
break;
2580+
25712581
case PlatformKind::none:
25722582
break;
25732583
}
@@ -2616,6 +2626,9 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const {
26162626
case PlatformKind::Windows:
26172627
return name == "windows";
26182628

2629+
case PlatformKind::Android:
2630+
return name == "android";
2631+
26192632
case PlatformKind::none:
26202633
return false;
26212634
}
@@ -2692,6 +2705,10 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
26922705
case PlatformKind::Windows:
26932706
// No deprecation filter on Windows
26942707
return false;
2708+
2709+
case PlatformKind::Android:
2710+
// The minimum Android API level supported by Swift is 21
2711+
return major <= 20;
26952712
}
26962713

26972714
llvm_unreachable("Unexpected platform");

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9911,6 +9911,7 @@ void ClangImporter::Implementation::importAttributes(
99119911
PlatformKind::watchOSApplicationExtension)
99129912
.Case("xros_app_extension",
99139913
PlatformKind::visionOSApplicationExtension)
9914+
.Case("android", PlatformKind::Android)
99149915
.Default(std::nullopt);
99159916
if (!platformK)
99169917
continue;

lib/IRGen/IRGenFunction.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,18 @@ llvm::Value *
307307
IRGenFunction::emitTargetOSVersionAtLeastCall(llvm::Value *major,
308308
llvm::Value *minor,
309309
llvm::Value *patch) {
310-
auto fn = IGM.getPlatformVersionAtLeastFunctionPointer();
310+
// compiler-rt in the NDK does not include __isPlatformVersionAtLeast
311+
// but only __isOSVersionAtLeast
312+
if (IGM.Triple.isAndroid()) {
313+
auto fn = IGM.getOSVersionAtLeastFunctionPointer();
314+
return Builder.CreateCall(fn, {major, minor, patch});
315+
} else {
316+
auto fn = IGM.getPlatformVersionAtLeastFunctionPointer();
311317

312-
llvm::Value *platformID =
313-
llvm::ConstantInt::get(IGM.Int32Ty, getBaseMachOPlatformID(IGM.Triple));
314-
return Builder.CreateCall(fn, {platformID, major, minor, patch});
318+
llvm::Value *platformID =
319+
llvm::ConstantInt::get(IGM.Int32Ty, getBaseMachOPlatformID(IGM.Triple));
320+
return Builder.CreateCall(fn, {platformID, major, minor, patch});
321+
}
315322
}
316323

317324
llvm::Value *

lib/IRGen/TBDGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver,
251251
llvm_unreachable("not used for this platform");
252252
case swift::PlatformKind::Windows:
253253
llvm_unreachable("not used for this platform");
254+
case swift::PlatformKind::Android:
255+
llvm_unreachable("not used for this platform");
254256
case swift::PlatformKind::iOS:
255257
case swift::PlatformKind::iOSApplicationExtension:
256258
if (target && target->isMacCatalystEnvironment())

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,9 @@ class DeclAndTypePrinter::Implementation
18301830
case PlatformKind::Windows:
18311831
plat = "windows";
18321832
break;
1833+
case PlatformKind::Android:
1834+
plat = "android";
1835+
break;
18331836
case PlatformKind::none:
18341837
llvm_unreachable("handled above");
18351838
}

lib/SymbolGraphGen/AvailabilityMixin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ StringRef getDomain(const SemanticAvailableAttr &AvAttr) {
6060
return { "OpenBSD" };
6161
case swift::PlatformKind::Windows:
6262
return { "Windows" };
63+
case swift::PlatformKind::Android:
64+
return { "Android" };
6365
case swift::PlatformKind::none:
6466
return { "*" };
6567
}

0 commit comments

Comments
 (0)