Skip to content

Commit f202411

Browse files
authored
Port to Android (#5)
1 parent c23fdeb commit f202411

File tree

20 files changed

+35
-31
lines changed

20 files changed

+35
-31
lines changed

Sources/SwiftDocC/Benchmark/Benchmark.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class Benchmark: Encodable {
4444
public let platform = "iOS"
4545
#elseif os(Linux)
4646
public let platform = "Linux"
47+
#elseif os(Android)
48+
public let platform = "Android"
4749
#else
4850
public let platform = "unsupported"
4951
#endif

Sources/SwiftDocC/Benchmark/Metrics/PeakMemory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension Benchmark {
3939
return Double(vmInfo.ledger_phys_footprint_peak)
4040
}
4141

42-
#elseif os(Linux)
42+
#elseif os(Linux) || os(Android)
4343
private static func peakMemory() -> Double? {
4444
// On Linux we cannot use the Kernel framework, so we tap into the
4545
// kernel proc file system to read the vm peak reported in the process status.

Sources/SwiftDocC/Infrastructure/External Data/OutOfProcessReferenceResolver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ private class LongRunningService: ExternalLinkResolving {
400400
/// This private class is only used by the ``OutOfProcessReferenceResolver`` and shouldn't be used for general communication with other processes.
401401
private class LongRunningProcess: ExternalLinkResolving {
402402

403-
#if os(macOS) || os(Linux)
403+
#if os(macOS) || os(Linux) || os(Android)
404404
private let process: Process
405405

406406
init(location: URL, errorOutputHandler: @escaping (String) -> Void) throws {

Sources/SwiftDocC/Model/Rendering/RenderContext.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public struct RenderContext {
6161
)
6262
}
6363

64-
#if os(macOS) || os(iOS)
65-
// Concurrently render content on macOS & iOS
64+
#if os(macOS) || os(iOS) || os(Android)
65+
// Concurrently render content on macOS/iOS & Android
6666
let results: [(reference: ResolvedTopicReference, content: RenderReferenceStore.TopicContent)] = references.concurrentPerform { reference, results in
6767
results.append((reference, renderContentFor(reference)))
6868
}

Sources/SwiftDocC/Utility/FoundationExtensions/AutoreleasepoolShim.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
#if os(Linux)
11+
#if os(Linux) || os(Android)
1212
/// A shim for Linux that runs the given block of code.
1313
///
1414
/// The existence of this shim allows you the use of auto-release pools to optimize memory footprint on Darwin platforms while maintaining

Sources/SwiftDocC/Utility/LMDB/LMDB.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extension String: LMDBData {
9797
}
9898
}
9999

100-
#if !os(Linux)
100+
#if !os(Linux) && !os(Android)
101101
// This is required for macOS and Swift 4.2, for Linux the default implementation works as expected.
102102
extension Array: LMDBData where Element: FixedWidthInteger {
103103

Sources/SwiftDocC/Utility/Synchronization.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Synchronized<Value> {
2929
/// A lock type appropriate for the current platform.
3030
/// > Note: To avoid access race reports we manage the memory manually.
3131
var lock: UnsafeMutablePointer<os_unfair_lock>
32-
#elseif os(Linux)
32+
#elseif os(Linux) || os(Android)
3333
/// A lock type appropriate for the current platform.
3434
var lock: UnsafeMutablePointer<pthread_mutex_t>
3535
#else
@@ -44,7 +44,7 @@ public class Synchronized<Value> {
4444
#if os(macOS) || os(iOS)
4545
lock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
4646
lock.initialize(to: os_unfair_lock())
47-
#elseif os(Linux)
47+
#elseif os(Linux) || os(Android)
4848
lock = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
4949
lock.initialize(to: pthread_mutex_t())
5050
pthread_mutex_init(lock, nil)
@@ -66,7 +66,7 @@ public class Synchronized<Value> {
6666
#if os(macOS) || os(iOS)
6767
os_unfair_lock_lock(lock)
6868
defer { os_unfair_lock_unlock(lock) }
69-
#elseif os(Linux)
69+
#elseif os(Linux) || os(Android)
7070
pthread_mutex_lock(lock)
7171
defer { pthread_mutex_unlock(lock) }
7272
#else
@@ -99,7 +99,7 @@ public extension Lock {
9999
#if os(macOS) || os(iOS)
100100
os_unfair_lock_lock(lock)
101101
defer { os_unfair_lock_unlock(lock) }
102-
#elseif os(Linux)
102+
#elseif os(Linux) || os(Android)
103103
pthread_mutex_lock(lock)
104104
defer { pthread_mutex_unlock(lock) }
105105
#else

Sources/SwiftDocCUtilities/Action/Actions/PreviewAction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public final class PreviewAction: Action, RecreatingContext {
183183
trapSignals()
184184

185185
// Monitor the source folder if possible.
186-
#if !os(Linux)
186+
#if !os(Linux) && !os(Android)
187187
try watch()
188188
#endif
189189
// This will wait until the server is manually killed.
@@ -225,7 +225,7 @@ public final class PreviewAction: Action, RecreatingContext {
225225

226226
// Monitoring a source folder: Asynchronous output reading and file system events are supported only on macOS.
227227

228-
#if !os(Linux)
228+
#if !os(Linux) && !os(Android)
229229
/// If needed, a retained directory monitor.
230230
fileprivate var monitor: DirectoryMonitor! = nil
231231

Sources/SwiftDocCUtilities/Utility/DirectoryMonitor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import Foundation
1212
import SwiftDocC
1313

14-
#if !os(Linux)
14+
#if !os(Linux) && !os(Android)
1515
import Darwin
1616

1717
/// A throttle object to filter events that come too fast.

Sources/SwiftDocCUtilities/Utility/Signal.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public struct Signal {
2222
#if os(Linux)
2323
// This is where we get to use a triple underscore in a method name.
2424
signalAction.__sigaction_handler = unsafeBitCast(callback, to: sigaction.__Unnamed_union___sigaction_handler.self)
25+
#elseif os(Android)
26+
signalAction.sa_handler = callback
2527
#else
2628
signalAction.__sigaction_u = unsafeBitCast(callback, to: __sigaction_u.self)
2729
#endif

Sources/docc/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
#if os(macOS) || os(Linux)
11+
#if os(macOS) || os(Linux) || os(Android)
1212
import SwiftDocCUtilities
1313

1414
Docc.main()

Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ Root
947947
}
948948

949949
func testAvailabilityIndexCreation() throws {
950-
#if !os(Linux)
950+
#if !os(Linux) && !os(Android)
951951
let availabilityIndex = AvailabilityIndex()
952952

953953
let macOS_10_14 = Platform(name: .macOS, version: Platform.Version(string: "10.14")!)

Tests/SwiftDocCTests/Rendering/RoundTripCoding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func assertJSONRepresentation<Value: Decodable & Equatable>(
5050
var decoded: Value? = nil
5151

5252
let encoding: String.Encoding
53-
#if os(Linux)
53+
#if os(Linux) || os(Android)
5454
// Work around a JSON decoding issue on Linux (SR-15035).
5555
encoding = .utf8
5656
#else

Tests/SwiftDocCTests/Servers/FileServerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class FileServerTests: XCTestCase {
164164
(response, data) = fileServer.response(to: failingRequest)
165165
XCTAssertNil(data)
166166
// Initializing a URLResponse with `nil` as MIME type in Linux returns nil
167-
#if os(Linux)
167+
#if os(Linux) || os(Android)
168168
XCTAssertNil(response.mimeType)
169169
#else
170170
// Doing the same in macOS or iOS returns the default MIME type

Tests/SwiftDocCTests/Servers/TopicRefenceSchemeTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TopicRefenceSchemeTests: XCTestCase {
2424
forResource: "TestBundle", withExtension: "docc", subdirectory: "Test Bundles")!
2525

2626
func testTopicReferenceSchemeHandler() {
27-
#if !os(Linux)
27+
#if !os(Linux) && !os(Android)
2828
let topicSchemeHandler = DocumentationSchemeHandler(withTemplateURL: templateURL)
2929

3030
let request = URLRequest(url: baseURL.appendingPathComponent("/images/figure1.jpg"))
@@ -50,7 +50,7 @@ class TopicRefenceSchemeTests: XCTestCase {
5050
}
5151

5252
func testSetData() {
53-
#if !os(Linux)
53+
#if !os(Linux) && !os(Android)
5454
let topicSchemeHandler = DocumentationSchemeHandler(withTemplateURL: templateURL)
5555

5656
let data = "hello!".data(using: .utf8)!

Tests/SwiftDocCTests/Utility/LMDBTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ final class SwiftLMDBTests: XCTestCase {
240240
}
241241

242242
func testArrayOfInt() throws {
243-
#if !os(Linux)
243+
#if !os(Linux) && !os(Android)
244244
let database = try environment.openDatabase()
245245

246246
var array: [UInt32] = []

Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import XCTest
1212
@testable import SwiftDocCUtilities
1313

14-
#if !os(Linux)
14+
#if !os(Linux) && !os(Android)
1515
fileprivate extension NSNotification.Name {
1616
static let testNodeUpdated = NSNotification.Name(rawValue: "testNodeUpdated")
1717
static let testDirectoryReloaded = NSNotification.Name(rawValue: "testDirectoryReloaded")
@@ -24,7 +24,7 @@ func fileURLsAreEqual(_ url1: URL, _ url2: URL) -> Bool {
2424
#endif
2525

2626
class DirectoryMonitorTests: XCTestCase {
27-
#if !os(Linux)
27+
#if !os(Linux) && !os(Android)
2828
// - MARK: Directory watching test infra
2929

3030
/// Method that automates setting up a directory monitor, setting up the relevant expectations for a test,
@@ -118,7 +118,7 @@ class DirectoryMonitorTests: XCTestCase {
118118
/// Tests a succession of file system changes and verifies that they produce
119119
/// the expected monitor events.
120120
func testMonitorUpdates() throws {
121-
#if !os(Linux)
121+
#if !os(Linux) && !os(Android)
122122

123123
// Create temp folder & sub-folder.
124124
let tempFolderURL = Foundation.URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
@@ -171,7 +171,7 @@ class DirectoryMonitorTests: XCTestCase {
171171
}
172172

173173
func testMonitorDoesNotTriggerUpdates() throws {
174-
#if !os(Linux)
174+
#if !os(Linux) && !os(Android)
175175

176176
// Create temp folder & sub-folder.
177177
let tempFolderURL = Foundation.URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
@@ -208,7 +208,7 @@ class DirectoryMonitorTests: XCTestCase {
208208

209209
/// Tests a zero sum change aggregation triggers an event.
210210
func testMonitorZeroSumSizeChangesUpdates() throws {
211-
#if !os(Linux)
211+
#if !os(Linux) && !os(Android)
212212

213213
// Create temp folder & sub-folder.
214214
let tempFolderURL = Foundation.URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)

Tests/SwiftDocCUtilitiesTests/PreviewServer/PreviewServerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class PreviewServerTests: XCTestCase {
2626
])
2727
try tempFolder.write(to: URL(fileURLWithPath: NSTemporaryDirectory().appending(UUID().uuidString)))
2828

29-
let socketURL = URL(fileURLWithPath: "/var/tmp").appendingPathComponent(UUID().uuidString).appendingPathExtension("sock")
29+
let socketURL = URL(fileURLWithPath: FileManager.default.temporaryDirectory.path).appendingPathComponent(UUID().uuidString).appendingPathExtension("sock")
3030

3131
// Run test server
3232
var log = LogHandle.none
@@ -106,7 +106,7 @@ class PreviewServerTests: XCTestCase {
106106
let tempFolder = try makeTempFolder()
107107

108108
// Socket URL
109-
let socketURL = URL(fileURLWithPath: "/var/tmp").appendingPathComponent(UUID().uuidString).appendingPathExtension("sock")
109+
let socketURL = URL(fileURLWithPath: FileManager.default.temporaryDirectory.path).appendingPathComponent(UUID().uuidString).appendingPathExtension("sock")
110110

111111
// Create the server
112112
var log = LogHandle.none
@@ -153,7 +153,7 @@ class PreviewServerTests: XCTestCase {
153153
let tempFolder = try makeTempFolder()
154154

155155
// Socket URL
156-
let socketURL = URL(fileURLWithPath: "/var/tmp").appendingPathComponent(UUID().uuidString).appendingPathExtension("sock")
156+
let socketURL = URL(fileURLWithPath: FileManager.default.temporaryDirectory.path).appendingPathComponent(UUID().uuidString).appendingPathExtension("sock")
157157

158158
// Create the server
159159
var log = LogHandle.none

Tests/SwiftDocCUtilitiesTests/SignalTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import XCTest
1212
@testable import SwiftDocCUtilities
1313

1414
class SignalTests: XCTestCase {
15-
#if os(macOS) || os(Linux)
15+
#if os(macOS) || os(Linux) || os(Android)
1616
/// The path to the built products directory.
1717
private var productDirectory: URL {
18-
#if !os(Linux)
18+
#if !os(Linux) && !os(Android)
1919
guard let xcTestBundle = Bundle.allBundles.first(where: { bundle in
2020
bundle.bundleURL.pathExtension == "xctest"
2121
}) else {

Tests/signal-test-app/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Check Tests/SwiftDocCUtilitiesTests/SignalTests.swift for more details.
1414
//
1515

16-
#if os(macOS) || os(Linux)
16+
#if os(macOS) || os(Linux) || os(Android)
1717

1818
import Foundation
1919
import SwiftDocCUtilities

0 commit comments

Comments
 (0)