From 018adc5afaedd6117bd41d544ad9d4680892bbde Mon Sep 17 00:00:00 2001 From: Butta Date: Fri, 15 Oct 2021 14:45:28 +0530 Subject: [PATCH] Port to Android --- Sources/SwiftDocC/Benchmark/Benchmark.swift | 2 ++ Sources/SwiftDocC/Benchmark/Metrics/PeakMemory.swift | 2 +- .../External Data/OutOfProcessReferenceResolver.swift | 2 +- Sources/SwiftDocC/Model/Rendering/RenderContext.swift | 4 ++-- .../FoundationExtensions/AutoreleasepoolShim.swift | 2 +- Sources/SwiftDocC/Utility/LMDB/LMDB.swift | 2 +- Sources/SwiftDocC/Utility/Synchronization.swift | 8 ++++---- .../Action/Actions/PreviewAction.swift | 4 ++-- .../SwiftDocCUtilities/Utility/DirectoryMonitor.swift | 2 +- Sources/SwiftDocCUtilities/Utility/Signal.swift | 2 ++ Sources/docc/main.swift | 2 +- .../SwiftDocCTests/Indexing/NavigatorIndexTests.swift | 2 +- Tests/SwiftDocCTests/Rendering/RoundTripCoding.swift | 2 +- Tests/SwiftDocCTests/Servers/FileServerTests.swift | 2 +- .../Servers/TopicRefenceSchemeTests.swift | 4 ++-- Tests/SwiftDocCTests/Utility/LMDBTests.swift | 2 +- .../DirectoryMonitorTests.swift | 10 +++++----- .../PreviewServer/PreviewServerTests.swift | 6 +++--- Tests/SwiftDocCUtilitiesTests/SignalTests.swift | 4 ++-- Tests/signal-test-app/main.swift | 2 +- 20 files changed, 35 insertions(+), 31 deletions(-) diff --git a/Sources/SwiftDocC/Benchmark/Benchmark.swift b/Sources/SwiftDocC/Benchmark/Benchmark.swift index 77376234e9..1b7f3f1e65 100644 --- a/Sources/SwiftDocC/Benchmark/Benchmark.swift +++ b/Sources/SwiftDocC/Benchmark/Benchmark.swift @@ -44,6 +44,8 @@ public class Benchmark: Encodable { public let platform = "iOS" #elseif os(Linux) public let platform = "Linux" + #elseif os(Android) + public let platform = "Android" #else public let platform = "unsupported" #endif diff --git a/Sources/SwiftDocC/Benchmark/Metrics/PeakMemory.swift b/Sources/SwiftDocC/Benchmark/Metrics/PeakMemory.swift index fcfee738e7..151cffe0e1 100644 --- a/Sources/SwiftDocC/Benchmark/Metrics/PeakMemory.swift +++ b/Sources/SwiftDocC/Benchmark/Metrics/PeakMemory.swift @@ -39,7 +39,7 @@ extension Benchmark { return Double(vmInfo.ledger_phys_footprint_peak) } - #elseif os(Linux) + #elseif os(Linux) || os(Android) private static func peakMemory() -> Double? { // On Linux we cannot use the Kernel framework, so we tap into the // kernel proc file system to read the vm peak reported in the process status. diff --git a/Sources/SwiftDocC/Infrastructure/External Data/OutOfProcessReferenceResolver.swift b/Sources/SwiftDocC/Infrastructure/External Data/OutOfProcessReferenceResolver.swift index 7a0ef0df52..19944e3dbb 100644 --- a/Sources/SwiftDocC/Infrastructure/External Data/OutOfProcessReferenceResolver.swift +++ b/Sources/SwiftDocC/Infrastructure/External Data/OutOfProcessReferenceResolver.swift @@ -400,7 +400,7 @@ private class LongRunningService: ExternalLinkResolving { /// This private class is only used by the ``OutOfProcessReferenceResolver`` and shouldn't be used for general communication with other processes. private class LongRunningProcess: ExternalLinkResolving { - #if os(macOS) || os(Linux) + #if os(macOS) || os(Linux) || os(Android) private let process: Process init(location: URL, errorOutputHandler: @escaping (String) -> Void) throws { diff --git a/Sources/SwiftDocC/Model/Rendering/RenderContext.swift b/Sources/SwiftDocC/Model/Rendering/RenderContext.swift index 7ecfc634ab..dab96eb84d 100644 --- a/Sources/SwiftDocC/Model/Rendering/RenderContext.swift +++ b/Sources/SwiftDocC/Model/Rendering/RenderContext.swift @@ -61,8 +61,8 @@ public struct RenderContext { ) } - #if os(macOS) || os(iOS) - // Concurrently render content on macOS & iOS + #if os(macOS) || os(iOS) || os(Android) + // Concurrently render content on macOS/iOS & Android let results: [(reference: ResolvedTopicReference, content: RenderReferenceStore.TopicContent)] = references.concurrentPerform { reference, results in results.append((reference, renderContentFor(reference))) } diff --git a/Sources/SwiftDocC/Utility/FoundationExtensions/AutoreleasepoolShim.swift b/Sources/SwiftDocC/Utility/FoundationExtensions/AutoreleasepoolShim.swift index e71ece64c5..9fc8de1760 100644 --- a/Sources/SwiftDocC/Utility/FoundationExtensions/AutoreleasepoolShim.swift +++ b/Sources/SwiftDocC/Utility/FoundationExtensions/AutoreleasepoolShim.swift @@ -8,7 +8,7 @@ See https://swift.org/CONTRIBUTORS.txt for Swift project authors */ -#if os(Linux) +#if os(Linux) || os(Android) /// A shim for Linux that runs the given block of code. /// /// The existence of this shim allows you the use of auto-release pools to optimize memory footprint on Darwin platforms while maintaining diff --git a/Sources/SwiftDocC/Utility/LMDB/LMDB.swift b/Sources/SwiftDocC/Utility/LMDB/LMDB.swift index 26876f8779..1e1a4289f4 100644 --- a/Sources/SwiftDocC/Utility/LMDB/LMDB.swift +++ b/Sources/SwiftDocC/Utility/LMDB/LMDB.swift @@ -97,7 +97,7 @@ extension String: LMDBData { } } -#if !os(Linux) +#if !os(Linux) && !os(Android) // This is required for macOS and Swift 4.2, for Linux the default implementation works as expected. extension Array: LMDBData where Element: FixedWidthInteger { diff --git a/Sources/SwiftDocC/Utility/Synchronization.swift b/Sources/SwiftDocC/Utility/Synchronization.swift index 725531d26b..1aefc9cf52 100644 --- a/Sources/SwiftDocC/Utility/Synchronization.swift +++ b/Sources/SwiftDocC/Utility/Synchronization.swift @@ -29,7 +29,7 @@ public class Synchronized { /// A lock type appropriate for the current platform. /// > Note: To avoid access race reports we manage the memory manually. var lock: UnsafeMutablePointer - #elseif os(Linux) + #elseif os(Linux) || os(Android) /// A lock type appropriate for the current platform. var lock: UnsafeMutablePointer #else @@ -44,7 +44,7 @@ public class Synchronized { #if os(macOS) || os(iOS) lock = UnsafeMutablePointer.allocate(capacity: 1) lock.initialize(to: os_unfair_lock()) - #elseif os(Linux) + #elseif os(Linux) || os(Android) lock = UnsafeMutablePointer.allocate(capacity: 1) lock.initialize(to: pthread_mutex_t()) pthread_mutex_init(lock, nil) @@ -66,7 +66,7 @@ public class Synchronized { #if os(macOS) || os(iOS) os_unfair_lock_lock(lock) defer { os_unfair_lock_unlock(lock) } - #elseif os(Linux) + #elseif os(Linux) || os(Android) pthread_mutex_lock(lock) defer { pthread_mutex_unlock(lock) } #else @@ -99,7 +99,7 @@ public extension Lock { #if os(macOS) || os(iOS) os_unfair_lock_lock(lock) defer { os_unfair_lock_unlock(lock) } - #elseif os(Linux) + #elseif os(Linux) || os(Android) pthread_mutex_lock(lock) defer { pthread_mutex_unlock(lock) } #else diff --git a/Sources/SwiftDocCUtilities/Action/Actions/PreviewAction.swift b/Sources/SwiftDocCUtilities/Action/Actions/PreviewAction.swift index 20d48d1624..edb1d9295c 100644 --- a/Sources/SwiftDocCUtilities/Action/Actions/PreviewAction.swift +++ b/Sources/SwiftDocCUtilities/Action/Actions/PreviewAction.swift @@ -183,7 +183,7 @@ public final class PreviewAction: Action, RecreatingContext { trapSignals() // Monitor the source folder if possible. - #if !os(Linux) + #if !os(Linux) && !os(Android) try watch() #endif // This will wait until the server is manually killed. @@ -225,7 +225,7 @@ public final class PreviewAction: Action, RecreatingContext { // Monitoring a source folder: Asynchronous output reading and file system events are supported only on macOS. -#if !os(Linux) +#if !os(Linux) && !os(Android) /// If needed, a retained directory monitor. fileprivate var monitor: DirectoryMonitor! = nil diff --git a/Sources/SwiftDocCUtilities/Utility/DirectoryMonitor.swift b/Sources/SwiftDocCUtilities/Utility/DirectoryMonitor.swift index e9ea3f7aa6..514b67d1ea 100644 --- a/Sources/SwiftDocCUtilities/Utility/DirectoryMonitor.swift +++ b/Sources/SwiftDocCUtilities/Utility/DirectoryMonitor.swift @@ -11,7 +11,7 @@ import Foundation import SwiftDocC -#if !os(Linux) +#if !os(Linux) && !os(Android) import Darwin /// A throttle object to filter events that come too fast. diff --git a/Sources/SwiftDocCUtilities/Utility/Signal.swift b/Sources/SwiftDocCUtilities/Utility/Signal.swift index bbcede8aa2..3d251cadf2 100644 --- a/Sources/SwiftDocCUtilities/Utility/Signal.swift +++ b/Sources/SwiftDocCUtilities/Utility/Signal.swift @@ -22,6 +22,8 @@ public struct Signal { #if os(Linux) // This is where we get to use a triple underscore in a method name. signalAction.__sigaction_handler = unsafeBitCast(callback, to: sigaction.__Unnamed_union___sigaction_handler.self) + #elseif os(Android) + signalAction.sa_handler = callback #else signalAction.__sigaction_u = unsafeBitCast(callback, to: __sigaction_u.self) #endif diff --git a/Sources/docc/main.swift b/Sources/docc/main.swift index 3020365f14..680e30bf42 100644 --- a/Sources/docc/main.swift +++ b/Sources/docc/main.swift @@ -8,7 +8,7 @@ See https://swift.org/CONTRIBUTORS.txt for Swift project authors */ -#if os(macOS) || os(Linux) +#if os(macOS) || os(Linux) || os(Android) import SwiftDocCUtilities Docc.main() diff --git a/Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift b/Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift index c9cef1aec8..efff648c34 100644 --- a/Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift +++ b/Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift @@ -947,7 +947,7 @@ Root } func testAvailabilityIndexCreation() throws { - #if !os(Linux) + #if !os(Linux) && !os(Android) let availabilityIndex = AvailabilityIndex() let macOS_10_14 = Platform(name: .macOS, version: Platform.Version(string: "10.14")!) diff --git a/Tests/SwiftDocCTests/Rendering/RoundTripCoding.swift b/Tests/SwiftDocCTests/Rendering/RoundTripCoding.swift index 3e3555ad7f..2edd96c1a6 100644 --- a/Tests/SwiftDocCTests/Rendering/RoundTripCoding.swift +++ b/Tests/SwiftDocCTests/Rendering/RoundTripCoding.swift @@ -50,7 +50,7 @@ func assertJSONRepresentation( var decoded: Value? = nil let encoding: String.Encoding - #if os(Linux) + #if os(Linux) || os(Android) // Work around a JSON decoding issue on Linux (SR-15035). encoding = .utf8 #else diff --git a/Tests/SwiftDocCTests/Servers/FileServerTests.swift b/Tests/SwiftDocCTests/Servers/FileServerTests.swift index f3c3fe2328..addd53979c 100644 --- a/Tests/SwiftDocCTests/Servers/FileServerTests.swift +++ b/Tests/SwiftDocCTests/Servers/FileServerTests.swift @@ -164,7 +164,7 @@ class FileServerTests: XCTestCase { (response, data) = fileServer.response(to: failingRequest) XCTAssertNil(data) // Initializing a URLResponse with `nil` as MIME type in Linux returns nil - #if os(Linux) + #if os(Linux) || os(Android) XCTAssertNil(response.mimeType) #else // Doing the same in macOS or iOS returns the default MIME type diff --git a/Tests/SwiftDocCTests/Servers/TopicRefenceSchemeTests.swift b/Tests/SwiftDocCTests/Servers/TopicRefenceSchemeTests.swift index 048fd6423a..07eef230f9 100644 --- a/Tests/SwiftDocCTests/Servers/TopicRefenceSchemeTests.swift +++ b/Tests/SwiftDocCTests/Servers/TopicRefenceSchemeTests.swift @@ -24,7 +24,7 @@ class TopicRefenceSchemeTests: XCTestCase { forResource: "TestBundle", withExtension: "docc", subdirectory: "Test Bundles")! func testTopicReferenceSchemeHandler() { - #if !os(Linux) + #if !os(Linux) && !os(Android) let topicSchemeHandler = DocumentationSchemeHandler(withTemplateURL: templateURL) let request = URLRequest(url: baseURL.appendingPathComponent("/images/figure1.jpg")) @@ -50,7 +50,7 @@ class TopicRefenceSchemeTests: XCTestCase { } func testSetData() { - #if !os(Linux) + #if !os(Linux) && !os(Android) let topicSchemeHandler = DocumentationSchemeHandler(withTemplateURL: templateURL) let data = "hello!".data(using: .utf8)! diff --git a/Tests/SwiftDocCTests/Utility/LMDBTests.swift b/Tests/SwiftDocCTests/Utility/LMDBTests.swift index e7f6221cc4..c7bcf492cf 100644 --- a/Tests/SwiftDocCTests/Utility/LMDBTests.swift +++ b/Tests/SwiftDocCTests/Utility/LMDBTests.swift @@ -240,7 +240,7 @@ final class SwiftLMDBTests: XCTestCase { } func testArrayOfInt() throws { -#if !os(Linux) +#if !os(Linux) && !os(Android) let database = try environment.openDatabase() var array: [UInt32] = [] diff --git a/Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift b/Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift index ca2bd6492f..004c51879d 100644 --- a/Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift +++ b/Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift @@ -11,7 +11,7 @@ import XCTest @testable import SwiftDocCUtilities -#if !os(Linux) +#if !os(Linux) && !os(Android) fileprivate extension NSNotification.Name { static let testNodeUpdated = NSNotification.Name(rawValue: "testNodeUpdated") static let testDirectoryReloaded = NSNotification.Name(rawValue: "testDirectoryReloaded") @@ -24,7 +24,7 @@ func fileURLsAreEqual(_ url1: URL, _ url2: URL) -> Bool { #endif class DirectoryMonitorTests: XCTestCase { - #if !os(Linux) + #if !os(Linux) && !os(Android) // - MARK: Directory watching test infra /// Method that automates setting up a directory monitor, setting up the relevant expectations for a test, @@ -118,7 +118,7 @@ class DirectoryMonitorTests: XCTestCase { /// Tests a succession of file system changes and verifies that they produce /// the expected monitor events. func testMonitorUpdates() throws { - #if !os(Linux) + #if !os(Linux) && !os(Android) // Create temp folder & sub-folder. let tempFolderURL = Foundation.URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString) @@ -171,7 +171,7 @@ class DirectoryMonitorTests: XCTestCase { } func testMonitorDoesNotTriggerUpdates() throws { - #if !os(Linux) + #if !os(Linux) && !os(Android) // Create temp folder & sub-folder. let tempFolderURL = Foundation.URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString) @@ -208,7 +208,7 @@ class DirectoryMonitorTests: XCTestCase { /// Tests a zero sum change aggregation triggers an event. func testMonitorZeroSumSizeChangesUpdates() throws { - #if !os(Linux) + #if !os(Linux) && !os(Android) // Create temp folder & sub-folder. let tempFolderURL = Foundation.URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString) diff --git a/Tests/SwiftDocCUtilitiesTests/PreviewServer/PreviewServerTests.swift b/Tests/SwiftDocCUtilitiesTests/PreviewServer/PreviewServerTests.swift index 5362555409..8cdc94d7c3 100644 --- a/Tests/SwiftDocCUtilitiesTests/PreviewServer/PreviewServerTests.swift +++ b/Tests/SwiftDocCUtilitiesTests/PreviewServer/PreviewServerTests.swift @@ -26,7 +26,7 @@ class PreviewServerTests: XCTestCase { ]) try tempFolder.write(to: URL(fileURLWithPath: NSTemporaryDirectory().appending(UUID().uuidString))) - let socketURL = URL(fileURLWithPath: "/var/tmp").appendingPathComponent(UUID().uuidString).appendingPathExtension("sock") + let socketURL = URL(fileURLWithPath: FileManager.default.temporaryDirectory.path).appendingPathComponent(UUID().uuidString).appendingPathExtension("sock") // Run test server var log = LogHandle.none @@ -106,7 +106,7 @@ class PreviewServerTests: XCTestCase { let tempFolder = try makeTempFolder() // Socket URL - let socketURL = URL(fileURLWithPath: "/var/tmp").appendingPathComponent(UUID().uuidString).appendingPathExtension("sock") + let socketURL = URL(fileURLWithPath: FileManager.default.temporaryDirectory.path).appendingPathComponent(UUID().uuidString).appendingPathExtension("sock") // Create the server var log = LogHandle.none @@ -153,7 +153,7 @@ class PreviewServerTests: XCTestCase { let tempFolder = try makeTempFolder() // Socket URL - let socketURL = URL(fileURLWithPath: "/var/tmp").appendingPathComponent(UUID().uuidString).appendingPathExtension("sock") + let socketURL = URL(fileURLWithPath: FileManager.default.temporaryDirectory.path).appendingPathComponent(UUID().uuidString).appendingPathExtension("sock") // Create the server var log = LogHandle.none diff --git a/Tests/SwiftDocCUtilitiesTests/SignalTests.swift b/Tests/SwiftDocCUtilitiesTests/SignalTests.swift index f0c006f664..564d0bc938 100644 --- a/Tests/SwiftDocCUtilitiesTests/SignalTests.swift +++ b/Tests/SwiftDocCUtilitiesTests/SignalTests.swift @@ -12,10 +12,10 @@ import XCTest @testable import SwiftDocCUtilities class SignalTests: XCTestCase { - #if os(macOS) || os(Linux) + #if os(macOS) || os(Linux) || os(Android) /// The path to the built products directory. private var productDirectory: URL { - #if !os(Linux) + #if !os(Linux) && !os(Android) guard let xcTestBundle = Bundle.allBundles.first(where: { bundle in bundle.bundleURL.pathExtension == "xctest" }) else { diff --git a/Tests/signal-test-app/main.swift b/Tests/signal-test-app/main.swift index c201857422..7e52b04086 100644 --- a/Tests/signal-test-app/main.swift +++ b/Tests/signal-test-app/main.swift @@ -13,7 +13,7 @@ // Check Tests/SwiftDocCUtilitiesTests/SignalTests.swift for more details. // -#if os(macOS) || os(Linux) +#if os(macOS) || os(Linux) || os(Android) import Foundation import SwiftDocCUtilities