forked from atlassian/IDZSwiftCommonCrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateCommonCryptoModule.swift
More file actions
executable file
·127 lines (106 loc) · 3.72 KB
/
GenerateCommonCryptoModule.swift
File metadata and controls
executable file
·127 lines (106 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import Foundation
let verbose = true
// MARK: - Exception Handling
let handler: @convention(c) (NSException) -> Void = {
exception in
print("FATAL EXCEPTION: \(exception)")
exit(1)
}
NSSetUncaughtExceptionHandler(handler)
// MARK: - Task Utilities
func runShellCommand(command: String) -> String? {
let args: [String] = command.characters.split { $0 == " " }.map(String.init)
let other = args[1..<args.count]
let outputPipe = Pipe()
let task = Process()
task.launchPath = args[0]
task.arguments = other.map { $0 }
task.standardOutput = outputPipe
task.launch()
task.waitUntilExit()
guard task.terminationStatus == 0 else { return nil }
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
return String(data:outputData, encoding: String.Encoding.utf8)
}
// MARK: - File System Utilities
func fileExists(filePath: String) -> Bool {
return FileManager.default.fileExists(atPath:filePath)
}
func mkdir(path: String) -> Bool {
do {
try FileManager.default.createDirectory(atPath:path, withIntermediateDirectories: true, attributes: nil)
return true
}
catch {
return false
}
}
// MARK: - String Utilities
func trim(s: String) -> String {
return s.trimmingCharacters(in: .whitespacesAndNewlines)
}
func trim(s: String?) -> String? {
return (s == nil) ? nil : (trim(s: s!) as String)
}
func reportError(message: String) -> Never {
print("ERROR: \(message)")
exit(1)
}
// MARK: GenerateCommonCryptoModule
enum SDK: String {
case iOS = "iphoneos",
iOSSimulator = "iphonesimulator",
watchOS = "watchos",
watchSimulator = "watchsimulator",
tvOS = "appletvos",
tvOSSimulator = "appletvsimulator",
MacOSX = "macosx"
static let all = [iOS, iOSSimulator, watchOS, watchSimulator, tvOS, tvOSSimulator, MacOSX]
}
print(CommandLine.arguments)
guard let sdk = SDK(rawValue: CommandLine.arguments[1])?.rawValue else { reportError(message:"SDK must be one of \(SDK.all.map { $0.rawValue })") }
guard let sdkVersion = trim(s:runShellCommand(command:"/usr/bin/xcrun --sdk \(sdk) --show-sdk-version")) else {
reportError(message:"ERROR: Failed to determine SDK version for \(sdk)")
}
guard let sdkPath = trim(s:runShellCommand(command:"/usr/bin/xcrun --sdk \(sdk) --show-sdk-path")) else {
reportError(message:"ERROR: Failed to determine SDK path for \(sdk)")
}
if verbose {
print("SDK: \(sdk)")
print("SDK Version: \(sdkVersion)")
print("SDK Path: \(sdkPath)")
}
let moduleDirectory: String
let moduleFileName: String
if CommandLine.arguments.count > 2 {
moduleDirectory = "\(CommandLine.arguments[2])/Frameworks/\(sdk)/CommonCrypto.framework"
moduleFileName = "module.map"
}
else {
moduleDirectory = "\(sdkPath)/System/Library/Frameworks/CommonCrypto.framework"
moduleFileName = "module.map"
if fileExists(filePath:moduleDirectory) {
reportError(message:"Module directory already exists at \(moduleDirectory).")
}
}
if !mkdir(path:moduleDirectory) {
reportError(message:"Failed to create module directory \(moduleDirectory)")
}
let headerDir = "\(sdkPath)/usr/include/CommonCrypto/"
let headerFile1 = "\(headerDir)/CommonCrypto.h"
let headerFile2 = "\(headerDir)/CommonRandom.h"
let moduleMapFile =
"module CommonCrypto [system] {\n" +
" header \"\(headerFile1)\"\n" +
" header \"\(headerFile2)\"\n" +
" export *\n" +
"}\n"
let moduleMapPath = "\(moduleDirectory)/\(moduleFileName)"
do {
try moduleMapFile.write(toFile:moduleMapPath, atomically: true, encoding:String.Encoding.utf8)
print("Successfully created module \(moduleMapPath)")
exit(0)
}
catch {
reportError(message:"Failed to write module map file to \(moduleMapPath)")
}