Skip to content

Commit 508864a

Browse files
Merge pull request #4631 from swiftwasm/main
[pull] swiftwasm from main
2 parents d906ecd + aff121b commit 508864a

File tree

95 files changed

+19458
-85
lines changed

Some content is hidden

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

95 files changed

+19458
-85
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ set(SWIFT_ANDROID_API_LEVEL "" CACHE STRING
315315

316316
set(SWIFT_ANDROID_NDK_PATH "" CACHE STRING
317317
"Path to the directory that contains the Android NDK tools that are executable on the build machine")
318-
set(SWIFT_ANDROID_NDK_CLANG_VERSION "12.0.8" CACHE STRING
318+
set(SWIFT_ANDROID_NDK_CLANG_VERSION "12.0.9" CACHE STRING
319319
"The Clang version to use when building for Android.")
320320
set(SWIFT_ANDROID_DEPLOY_DEVICE_PATH "" CACHE STRING
321321
"Path on an Android device where build products will be pushed. These are used when running the test suite against the device")

docs/Android.md

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Getting Started with Swift on Android
22

3-
The Swift stdlib can be compiled for Android armv7, x86_64, and aarch64 targets,
4-
which makes it possible to execute Swift code on a mobile device running
5-
Android or an emulator. This guide explains:
3+
The Swift standard library (stdlib) can be compiled for Android armv7, x86_64,
4+
and aarch64 targets, which makes it possible to execute Swift code on a mobile
5+
device running Android or an emulator. This guide explains:
66

77
1. How to run a simple "Hello, world" program on your Android device.
88
2. How to run the Swift test suite on an Android device.
@@ -12,7 +12,7 @@ bug using https://bugs.swift.org/.
1212

1313
## FAQ
1414

15-
Let's answer a few frequently asked questions right off the bat:
15+
Let's answer a frequently asked question right off the bat:
1616

1717
### Does this mean I can write Android applications in Swift?
1818

@@ -30,32 +30,44 @@ Swift-to-Java bridging.
3030
To follow along with this guide, you'll need:
3131

3232
1. A Linux environment capable of building Swift from source, preferably
33-
Ubuntu 18.04 or Ubuntu 16.04. Before attempting to build for Android,
33+
Ubuntu 20.04 or Ubuntu 18.04. Before attempting to build for Android,
3434
please make sure you are able to build for Linux by following the
3535
instructions in the Swift project README.
36-
2. The latest version of the Android NDK (r23b at the time of this writing),
36+
2. The latest build of the Swift compiler for your Linux distro, available at
37+
https://www.swift.org/download/ or sometimes your distro package manager.
38+
3. The latest version of the Android LTS NDK (r23c at the time of this writing),
3739
available to download here:
38-
https://developer.android.com/ndk/downloads/index.html.
39-
3. An Android device with remote debugging enabled or the emulator. We require
40+
https://developer.android.com/ndk/downloads
41+
4. An Android device with remote debugging enabled or the emulator. We require
4042
remote debugging in order to deploy built stdlib products to the device. You
4143
may turn on remote debugging by following the official instructions:
42-
https://developer.chrome.com/devtools/docs/remote-debugging.
44+
https://developer.chrome.com/devtools/docs/remote-debugging .
4345

4446
## "Hello, world" on Android
4547

46-
### 1. Building the Swift stdlib for Android
48+
### 1. Building the standalone Swift stdlib for Android
4749

48-
Enter your Swift directory, then run the build script, passing the path to the
49-
Android NDK:
50+
Enter your Swift directory, check out the tag corresponding to the prebuilt
51+
Swift compiler, then run the build script, passing the path to the Android NDK
52+
and the prebuilt Swift toolchain (add --skip-early-swift-driver if you already
53+
have a Swift toolchain in your path):
5054

5155
```
52-
$ NDK_PATH=path/to/android-ndk-r23b
56+
$ NDK_PATH=path/to/android-ndk-r23c
57+
$ SWIFT_PATH=path/to/swift-DEVELOPMENT-SNAPSHOT-2022-05-31-a-ubuntu20.04/usr/bin
58+
$ git checkout swift-DEVELOPMENT-SNAPSHOT-2022-05-31-a
5359
$ utils/build-script \
5460
-R \ # Build in ReleaseAssert mode.
5561
--android \ # Build for Android.
5662
--android-ndk $NDK_PATH \ # Path to an Android NDK.
57-
--android-arch armv7 \ # Optionally specify Android architecture, alternately aarch64 or x86_64
58-
--android-api-level 21 # The Android API level to target. Swift only supports 21 or greater.
63+
--android-arch aarch64 \ # Optionally specify Android architecture, alternately armv7 or x86_64
64+
--android-api-level 21 \ # The Android API level to target. Swift only supports 21 or greater.
65+
--stdlib-deployment-targets=android-aarch64 \ # Only cross-compile the stdlib for Android, ie don't build the native stdlib for Linux
66+
--native-swift-tools-path=$SWIFT_PATH \ # Path to your prebuilt Swift compiler
67+
--native-clang-tools-path=$SWIFT_PATH \ # Path to a prebuilt clang compiler, one comes with the Swift toolchain
68+
--build-swift-tools=0 \ # Don't build the Swift compiler and other host tools
69+
--build-llvm=0 \ # Don't build the LLVM libraries, but generate some CMake files needed by the Swift stdlib build
70+
--skip-build-cmark # Don't build the CommonMark library that's only needed by the Swift compiler
5971
```
6072

6173
### 2. Compiling `hello.swift` to run on an Android device
@@ -66,15 +78,16 @@ Create a simple Swift file named `hello.swift`:
6678
print("Hello, Android")
6779
```
6880

69-
Then use the built Swift compiler from the previous step to compile a Swift
81+
Then use the standalone Swift stdlib from the previous step to compile a Swift
7082
source file, targeting Android:
7183

7284
```
73-
$ NDK_PATH="path/to/android-ndk-r23b"
74-
$ build/Ninja-ReleaseAssert/swift-linux-x86_64/bin/swiftc \ # The Swift compiler built in the previous step
85+
$ NDK_PATH="path/to/android-ndk-r23c"
86+
$ SWIFT_PATH=path/to/swift-DEVELOPMENT-SNAPSHOT-2022-05-31-a-ubuntu20.04/usr/bin
87+
$ $SWIFT_PATH/swiftc \ # The prebuilt Swift compiler you downloaded
7588
# The location of the tools used to build Android binaries
7689
-tools-directory ${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/bin/ \
77-
-target armv7-unknown-linux-androideabi21 \ # Targeting Android armv7 at API 21
90+
-target aarch64-unknown-linux-android21 \ # Targeting Android aarch64 at API 21
7891
-sdk ${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/sysroot \ # The SDK is the Android unified sysroot and the resource-dir is where you just built the Swift stdlib.
7992
-resource-dir build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift
8093
hello.swift
@@ -111,12 +124,15 @@ $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswi
111124
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftGlibc.so /data/local/tmp
112125
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftSwiftOnoneSupport.so /data/local/tmp
113126
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftRemoteMirror.so /data/local/tmp
127+
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswift_Concurrency.so /data/local/tmp
128+
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libdispatch.so /data/local/tmp
129+
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libBlocksRuntime.so /data/local/tmp
114130
```
115131

116132
In addition, you'll also need to copy the Android NDK's libc++:
117133

118134
```
119-
$ adb push /path/to/android-ndk-r23b/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so /data/local/tmp
135+
$ adb push /path/to/android-ndk-r23c/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so /data/local/tmp
120136
```
121137

122138
Finally, you'll need to copy the `hello` executable you built in the
@@ -157,9 +173,12 @@ device. As in part three, you'll need to connect your Android device via USB:
157173
```
158174
$ utils/build-script \
159175
-R \ # Build in ReleaseAssert mode.
160-
-T \ # Run all tests, including on the Android device (add --host-test to only run Android tests on the linux host).
176+
-T \ # Run all tests, including on the Android device (add --host-test to only run Android tests on the Linux host).
161177
--android \ # Build for Android.
162-
--android-ndk ~/android-ndk-r23b \ # Path to an Android NDK.
163-
--android-arch armv7 \ # Optionally specify Android architecture, alternately aarch64
178+
--android-ndk ~/android-ndk-r23c \ # Path to an Android NDK.
179+
--android-arch aarch64 \ # Optionally specify Android architecture, alternately armv7
164180
--android-api-level 21
165181
```
182+
183+
This will build the Swift compiler and other host tools first, so expect a much
184+
longer build.

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,9 @@ WARNING(access_control_non_objc_open_member,none,
15241524

15251525
ERROR(invalid_decl_attribute,none,
15261526
"'%0' attribute cannot be applied to this declaration", (DeclAttribute))
1527+
ERROR(attr_invalid_on_decl_kind,none,
1528+
"'%0' attribute cannot be applied to %1 declarations",
1529+
(DeclAttribute, DescriptiveDeclKind))
15271530
ERROR(invalid_decl_modifier,none,
15281531
"%0 modifier cannot be applied to this declaration", (DeclAttribute))
15291532
ERROR(attribute_does_not_apply_to_type,none,

include/swift/SIL/SILNodes.def.rej

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3662,6 +3662,12 @@ void AttributeChecker::checkBackDeployAttrs(ArrayRef<BackDeployAttr *> Attrs) {
36623662
}
36633663
}
36643664

3665+
if (isa<DestructorDecl>(D) || isa<ConstructorDecl>(D)) {
3666+
diagnoseAndRemoveAttr(Attr, diag::attr_invalid_on_decl_kind, Attr,
3667+
D->getDescriptiveKind());
3668+
continue;
3669+
}
3670+
36653671
auto AtLoc = Attr->AtLoc;
36663672
auto Platform = Attr->Platform;
36673673

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,25 +1315,41 @@ WitnessChecker::lookupValueWitnesses(ValueDecl *req, bool *ignoringNames) {
13151315
auto reqName = req->createNameRef();
13161316
auto reqBaseName = reqName.withoutArgumentLabels();
13171317

1318-
if (req->isOperator()) {
1319-
// Operator lookup is always global.
1318+
// An operator function is the only kind of witness that requires global
1319+
// lookup. However, because global lookup doesn't enter local contexts,
1320+
// an additional, qualified lookup is warranted when the conforming type
1321+
// is declared in a local context.
1322+
const bool doUnqualifiedLookup = req->isOperator();
1323+
const bool doQualifiedLookup =
1324+
!req->isOperator() || DC->getParent()->getLocalContext();
1325+
1326+
if (doUnqualifiedLookup) {
13201327
auto lookup = TypeChecker::lookupUnqualified(DC->getModuleScopeContext(),
13211328
reqBaseName, SourceLoc(),
13221329
defaultUnqualifiedLookupOptions);
13231330
for (auto candidate : lookup) {
13241331
auto decl = candidate.getValueDecl();
1325-
if (swift::isMemberOperator(cast<FuncDecl>(decl), Adoptee)) {
1332+
if (!isa<ProtocolDecl>(decl->getDeclContext()) &&
1333+
swift::isMemberOperator(cast<FuncDecl>(decl), Adoptee)) {
13261334
witnesses.push_back(decl);
13271335
}
13281336
}
1329-
} else {
1330-
// Variable/function/subscript requirements.
1337+
}
1338+
1339+
if (doQualifiedLookup) {
13311340
auto *nominal = Adoptee->getAnyNominal();
13321341
nominal->synthesizeSemanticMembersIfNeeded(reqName.getFullName());
13331342

1343+
// Unqualified lookup would have already found candidates from protocol
1344+
// extensions, including those that match only by base name. Take care not
1345+
// to restate them in the resulting list, or else an otherwise valid
1346+
// conformance will become ambiguous.
1347+
const NLOptions options =
1348+
doUnqualifiedLookup ? NLOptions(0) : NL_ProtocolMembers;
1349+
13341350
SmallVector<ValueDecl *, 4> lookupResults;
13351351
bool addedAny = false;
1336-
DC->lookupQualified(nominal, reqName, NL_ProtocolMembers, lookupResults);
1352+
DC->lookupQualified(nominal, reqName, options, lookupResults);
13371353
for (auto *decl : lookupResults) {
13381354
if (!isa<ProtocolDecl>(decl->getDeclContext())) {
13391355
witnesses.push_back(decl);
@@ -1345,7 +1361,7 @@ WitnessChecker::lookupValueWitnesses(ValueDecl *req, bool *ignoringNames) {
13451361
// again using only the base name.
13461362
if (!addedAny && ignoringNames) {
13471363
lookupResults.clear();
1348-
DC->lookupQualified(nominal, reqBaseName, NL_ProtocolMembers, lookupResults);
1364+
DC->lookupQualified(nominal, reqBaseName, options, lookupResults);
13491365
for (auto *decl : lookupResults) {
13501366
if (!isa<ProtocolDecl>(decl->getDeclContext()))
13511367
witnesses.push_back(decl);
@@ -1358,6 +1374,10 @@ WitnessChecker::lookupValueWitnesses(ValueDecl *req, bool *ignoringNames) {
13581374
removeShadowedDecls(witnesses, DC);
13591375
}
13601376

1377+
assert(llvm::none_of(witnesses, [](ValueDecl *decl) {
1378+
return isa<ProtocolDecl>(decl->getDeclContext());
1379+
}));
1380+
13611381
return witnesses;
13621382
}
13631383

0 commit comments

Comments
 (0)