Skip to content

Commit 043bc71

Browse files
mikeashairspeedswift
authored andcommitted
[Tools] Remove Foundation dependency from swiftdt, which gets very complicated for building.
Add a shim header and corresponding module to stub out a couple of C types we need.
1 parent da5d2b4 commit 043bc71

File tree

7 files changed

+42
-11
lines changed

7 files changed

+42
-11
lines changed

tools/swiftdt/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_swift_target_executable(swiftdt
88

99
COMPILE_FLAGS
1010
-I${SWIFT_SOURCE_DIR}/include/swift/SwiftRemoteMirror
11+
-I${SWIFT_SOURCE_DIR}/tools/swiftdt
1112
LINK_LIBRARIES
1213
swiftRemoteMirror
1314
)

tools/swiftdt/Inspector.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import Foundation
1+
import Darwin
22
import SwiftRemoteMirror
3+
import SymbolicationShims
34

45
class Inspector {
56
let task: task_t

tools/swiftdt/SymbolicationShims.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
3+
struct CSTypeRef {
4+
uintptr_t a, b;
5+
};
6+
7+
struct Range {
8+
uintptr_t location, length;
9+
};

tools/swiftdt/main.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ func printUsage(args: inout ArraySlice<String>) {
9797

9898
let maxWidth = commands.map({ $0.name.count }).max() ?? 0
9999
for command in commands {
100-
let paddedName = command.name.padding(toLength: maxWidth,
101-
withPad: " ", startingAt: 0)
100+
var paddedName = command.name
101+
while paddedName.count < maxWidth {
102+
paddedName = " " + paddedName
103+
}
102104
print(" \(paddedName) - \(command.help)", to: &Std.err)
103105
}
104106
}

tools/swiftdt/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module SymbolicationShims {
2+
header "SymbolicationShims.h"
3+
export *
4+
}

tools/swiftdt/stdio.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Foundation
1+
import Darwin
22

33
enum Std {
44
struct File: TextOutputStream {

tools/swiftdt/symbolication.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import Foundation
1+
import Darwin
2+
import SymbolicationShims
23

34
private let symbolicationPath =
45
"/System/Library/PrivateFrameworks/Symbolication.framework/Symbolication"
56
private let symbolicationHandle = dlopen(symbolicationPath, RTLD_LAZY)!
67

78
private let coreSymbolicationPath =
8-
"/System/Library/PrivateFrameworks/Symbolication.framework/Symbolication"
9+
"/System/Library/PrivateFrameworks/CoreSymbolication.framework/CoreSymbolication"
910
private let coreSymbolicationHandle = dlopen(coreSymbolicationPath, RTLD_LAZY)!
1011

1112
private func symbol<T>(_ handle: UnsafeMutableRawPointer, _ name: String) -> T {
@@ -16,7 +17,7 @@ private func symbol<T>(_ handle: UnsafeMutableRawPointer, _ name: String) -> T {
1617
}
1718

1819
enum Sym {
19-
static let pidFromHint: @convention(c) (NSString) -> pid_t =
20+
static let pidFromHint: @convention(c) (AnyObject) -> pid_t =
2021
symbol(symbolicationHandle, "pidFromHint")
2122
static let CSSymbolicatorCreateWithTask: @convention(c) (task_t) -> CSTypeRef =
2223
symbol(coreSymbolicationHandle, "CSSymbolicatorCreateWithTask")
@@ -35,7 +36,7 @@ enum Sym {
3536
symbol(coreSymbolicationHandle, "CSSymbolGetMangledName")
3637
static let CSSymbolIsFunction: @convention(c) (CSTypeRef) -> CBool =
3738
symbol(coreSymbolicationHandle, "CSSymbolIsFunction")
38-
static let CSSymbolGetRange: @convention(c) (CSTypeRef) -> NSRange =
39+
static let CSSymbolGetRange: @convention(c) (CSTypeRef) -> Range =
3940
symbol(coreSymbolicationHandle, "CSSymbolGetRange")
4041
static let task_start_peeking: @convention(c) (task_t) -> kern_return_t =
4142
symbol(symbolicationHandle, "task_start_peeking")
@@ -50,12 +51,25 @@ enum Sym {
5051
symbol(symbolicationHandle, "task_stop_peeking")
5152
}
5253

53-
typealias CSTypeRef = NSRange
54+
private enum CF {
55+
static let path = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
56+
static let handle = dlopen(path, RTLD_LAZY)!
57+
static let stringCreateWithCString:
58+
@convention(c) (UnsafeRawPointer?, UnsafePointer<CChar>, UInt32) ->
59+
Unmanaged<AnyObject> = symbol(handle, "CFStringCreateWithCString")
60+
static let stringEncodingUTF8: UInt32 = 0x08000100
61+
}
62+
5463
typealias CSMachineTime = UInt64
5564
let kCSNow = CSMachineTime(Int64.max) + 1
5665

66+
private func withNSString<T>(_ str: String, call: (AnyObject) -> T) -> T {
67+
let cfstr = CF.stringCreateWithCString(nil, str, CF.stringEncodingUTF8)
68+
return call(cfstr.takeRetainedValue())
69+
}
70+
5771
func pidFromHint(_ hint: String) -> pid_t? {
58-
let result = Sym.pidFromHint(hint as NSString)
72+
let result = withNSString(hint, call: Sym.pidFromHint)
5973
return result == 0 ? nil : result
6074
}
6175

@@ -95,7 +109,7 @@ func CSSymbolIsFunction(_ sym: CSTypeRef) -> Bool {
95109
return Sym.CSSymbolIsFunction(sym)
96110
}
97111

98-
func CSSymbolGetRange(_ sym: CSTypeRef) -> NSRange {
112+
func CSSymbolGetRange(_ sym: CSTypeRef) -> Range {
99113
return Sym.CSSymbolGetRange(sym)
100114
}
101115

0 commit comments

Comments
 (0)