Skip to content

Commit 00a2e2f

Browse files
authored
Android: import new Android overlay in more places (#8115)
Also, fix one test and add the right path for `touch` in the plugin tests. @dschaefer2, I use this patch to build both natively on Android and [cross-compile for my Android CI](https://github.com/finagolfin/swift-android-sdk/blob/cb93bf31adeefba44ce8041a90f6c88f7241ae2d/swift-android-ci.patch#L114). It would be good to get it in before the upcoming 6.1 branch: should be easy to review as it touches nothing outside Android and makes simple changes for Android.
1 parent fae4928 commit 00a2e2f

File tree

12 files changed

+48
-8
lines changed

12 files changed

+48
-8
lines changed

Fixtures/Miscellaneous/PluginGeneratedResources/Plugins/Generator/plugin.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import PackagePlugin
22

3+
#if os(Android)
4+
let touchExe = "/system/bin/touch"
5+
#else
6+
let touchExe = "/usr/bin/touch"
7+
#endif
8+
39
@main
410
struct GeneratorPlugin: BuildToolPlugin {
511
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
612
return [
713
.prebuildCommand(
814
displayName: "Generating empty file",
9-
executable: .init("/usr/bin/touch"),
15+
executable: .init(touchExe),
1016
arguments: [context.pluginWorkDirectory.appending("best.txt")],
1117
outputFilesDirectory: context.pluginWorkDirectory
1218
)

Fixtures/Miscellaneous/Plugins/MySourceGenPlugin/Plugins/MySourceGenPrebuildPlugin/plugin.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import PackagePlugin
22

3+
#if os(Android)
4+
let touchExe = "/system/bin/touch"
5+
#else
6+
let touchExe = "/usr/bin/touch"
7+
#endif
8+
39
@main
410
struct MyPlugin: BuildToolPlugin {
511

@@ -15,7 +21,7 @@ struct MyPlugin: BuildToolPlugin {
1521
displayName:
1622
"Running prebuild command for target \(target.name)",
1723
executable:
18-
Path("/usr/bin/touch"),
24+
Path(touchExe),
1925
arguments:
2026
outputPaths.map{ $0.string },
2127
outputFilesDirectory:

Fixtures/Miscellaneous/Plugins/MySourceGenPluginUsingURLBasedAPI/Plugins/MySourceGenPrebuildPlugin/plugin.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import Foundation
22
import PackagePlugin
33

4+
#if os(Android)
5+
let touchExe = "/system/bin/touch"
6+
#else
7+
let touchExe = "/usr/bin/touch"
8+
#endif
9+
410
@main
511
struct MyPlugin: BuildToolPlugin {
612

@@ -16,7 +22,7 @@ struct MyPlugin: BuildToolPlugin {
1622
displayName:
1723
"Running prebuild command for target \(target.name)",
1824
executable:
19-
URL(fileURLWithPath: "/usr/bin/touch"),
25+
URL(fileURLWithPath: touchExe),
2026
arguments:
2127
outputPaths.map{ $0.path },
2228
outputFilesDirectory:

Sources/Basics/Cancellator.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Foundation
1515
import class TSCBasic.Thread
1616
#if canImport(WinSDK)
1717
import WinSDK
18+
#elseif canImport(Android)
19+
import Android
1820
#endif
1921

2022
public typealias CancellationHandler = @Sendable (DispatchTime) throws -> Void

Sources/Basics/Concurrency/AsyncProcess.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import Foundation
1414

1515
#if os(Windows)
1616
import TSCLibc
17+
#elseif canImport(Android)
18+
import Android
1719
#endif
1820

1921
#if os(Linux)

Sources/Basics/Environment/Environment.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import Musl
1919
#elseif os(Windows)
2020
import CRT
2121
import WinSDK
22-
#elseif canImport(Bionic)
23-
import Bionic
22+
#elseif canImport(Android)
23+
import Android
2424
#else
2525
import Darwin.C
2626
#endif

Sources/Commands/SwiftRunCommand.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import func TSCBasic.exec
2222

2323
import enum TSCUtility.Diagnostics
2424

25+
#if canImport(Android)
26+
import Android
27+
#endif
28+
2529
/// An enumeration of the errors that can be generated by the run tool.
2630
private enum RunError: Swift.Error {
2731
/// The package manifest has no executable product.

Sources/Commands/SwiftTestCommand.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import class TSCBasic.Thread
4242

4343
#if os(Windows)
4444
import WinSDK // for ERROR_NOT_FOUND
45+
#elseif canImport(Android)
46+
import Android
4547
#endif
4648

4749
private enum TestError: Swift.Error {
@@ -1484,7 +1486,7 @@ private extension Basics.Diagnostic {
14841486
/// it duplicates the definition of this constant in its own source. Any changes
14851487
/// to this constant in either package must be mirrored in the other.
14861488
private var EXIT_NO_TESTS_FOUND: CInt {
1487-
#if os(macOS) || os(Linux)
1489+
#if os(macOS) || os(Linux) || canImport(Android)
14881490
EX_UNAVAILABLE
14891491
#elseif os(Windows)
14901492
ERROR_NOT_FOUND

Sources/PackagePlugin/Plugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ internal func strerror(_ errno: CInt) -> String? {
3636
return String(decodingCString: baseAddress, as: UTF16.self)
3737
}
3838
}
39+
#elseif canImport(Android)
40+
import Android
3941
#endif
4042

4143
//

Sources/PackageRegistryCommand/PackageRegistryCommand+Auth.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ private func readpassword(_ prompt: String) throws -> String {
6363
return password
6464
}
6565
#else
66+
#if canImport(Android)
67+
import Android
68+
#endif
69+
6670
private func readpassword(_ prompt: String) throws -> String {
6771
let password: String
6872

Tests/BasicsTests/FileSystem/PathShimTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class WalkTests: XCTestCase {
3838
var expected: [AbsolutePath] = [
3939
"\(root)/usr",
4040
"\(root)/bin",
41-
"\(root)/xbin",
41+
"\(root)/etc",
4242
]
4343
#elseif os(Windows)
4444
let root = ProcessInfo.processInfo.environment["SystemRoot"]!

Tests/FunctionalTests/PluginTests.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,19 @@ final class PluginTests: XCTestCase {
208208
try localFileSystem.createDirectory(pluginSourceFile.parentDirectory, recursive: true)
209209
try localFileSystem.writeFileContents(pluginSourceFile, string: """
210210
import PackagePlugin
211+
#if os(Android)
212+
let touchExe = "/system/bin/touch"
213+
#else
214+
let touchExe = "/usr/bin/touch"
215+
#endif
216+
211217
@main
212218
struct Plugin: BuildToolPlugin {
213219
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
214220
return [
215221
.buildCommand(
216222
displayName: "empty",
217-
executable: .init("/usr/bin/touch"),
223+
executable: .init(touchExe),
218224
arguments: [context.pluginWorkDirectory.appending("best.txt")],
219225
inputFiles: [],
220226
outputFiles: []

0 commit comments

Comments
 (0)