-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate
executable file
·269 lines (213 loc) · 6.63 KB
/
simulate
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env swift
import Foundation
extension String {
func sh() -> Int {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = ["sh", "-c", self]
task.launch()
task.waitUntilExit()
return Int(task.terminationStatus)
}
func shOutput() -> (status: Int, string: String) {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = ["sh", "-c", self]
let outputPipe = Pipe()
task.standardOutput = outputPipe
task.standardError = outputPipe
task.launch()
task.waitUntilExit()
let data = outputPipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return (status: Int(task.terminationStatus), string: output)
}
}
public enum DefileModes {
case read
case write
case append
}
public enum DefileError: Error {
case modeMismatch
case writeFailure
case noFileOpen
case null
}
extension String {
public func replacing(_ extensions: [String], with replacement: String) -> String {
var components = self.components(separatedBy: ".")
let last = components.count - 1
if extensions.contains(components[last]) {
components.remove(at: last)
}
components.append(replacement)
return components.joined(separator: ".")
}
}
public class Defile {
private var file: UnsafeMutablePointer<FILE>
private var mode: DefileModes
private var storage: [UInt8]
private var cOpen: Bool
public init?(_ path: String, mode: DefileModes = .read) {
var modeStr: String
switch(mode) {
case .read:
modeStr = "r"
case .write:
modeStr = "w"
case .append:
modeStr = "a"
}
guard let file = fopen(path, modeStr + "b")
else {
return nil
}
self.file = file
self.mode = mode
self.storage = []
cOpen = true;
if (mode == .read) {
var character = fgetc(file)
while character != EOF {
storage.append(UInt8(character))
character = fgetc(file)
}
fclose(file)
cOpen = false;
}
}
deinit {
close()
}
public func close() {
if cOpen {
fclose(file)
cOpen = false
}
}
static public func open(_ path: String, mode: DefileModes = .read) -> Defile? {
return Defile(path, mode: mode)
}
public var string: String? {
if mode != .read {
return nil
}
guard let convertible = String(data: Data(storage), encoding: .utf8)
else {
return nil
}
return convertible
}
public var lines: [String]? {
if mode != .read {
return nil
}
guard let result = self.string?.components(separatedBy: "\n")
else {
return nil
}
return result
}
public func write(bytes: [UInt8]) throws {
if mode == .read {
throw DefileError.modeMismatch
}
if !cOpen {
throw DefileError.noFileOpen
}
for byte in bytes {
if (fputc(Int32(byte), file) == EOF) {
throw DefileError.writeFailure
}
}
}
public func write(string: String) throws {
do {
try self.write(bytes: [UInt8](Array(string.utf8)))
}
catch {
throw error
}
}
}
if (CommandLine.arguments.count <= 1) {
print("Error: Folder for Swiftlog module not provided.")
exit(64)
}
let simulatable = CommandLine.arguments[1].trimmingCharacters(in: CharacterSet(charactersIn: "/"))
if ("mkdir -p Executables/".sh() != 0) {
print("Unable to create 'Executables' dump file.")
exit(-1)
}
guard let startup = Defile("\(simulatable)/Startup.swift") else {
print("Startup.swift not found in Swiftlog module.")
exit(-1)
}
guard let firstLine = startup.lines?[0] else {
print("File encoding failed.")
exit(-1)
}
if !firstLine.hasPrefix("///@SWIFTLOG: ") {
print("Swiftlog prefix not found in Startup.swift. (Note: must be space-delimited.)")
exit(-1)
}
let file = firstLine.components(separatedBy: "///@SWIFTLOG: ")[1]
chdir(simulatable)
var icarusVerilogResult = "iverilog -o ../Executables/main.vvp \(file)".sh()
if (icarusVerilogResult != 0) {
exit(Int32(icarusVerilogResult))
}
chdir("..")
var swiftFlags: String
var clangFlags: String
var linkerFlags: String
#if os(macOS)
print("Compiling for macOS.")
swiftFlags = "-Xcc -I/usr/local/include/ -Xlinker -undefined -Xlinker dynamic_lookup"
clangFlags = "-dynamiclib -undefined dynamic_lookup"
let xcodePath = "xcode-select --print-path".shOutput()
if (xcodePath.status != 0) {
print("Xcode not installed. Aborting.")
exit(-1)
}
linkerFlags = "-L/usr/lib/swift -rpath /usr/lib/swift -lswiftCore"
#elseif os(Linux)
print("Compiling for Linux.")
swiftFlags = ""
clangFlags = "-shared"
let swiftPath = "dirname $(dirname $(which swift))"
if (swiftPath.status != 0) {
print("Swift not in PATH. Aborting.")
exit(-1)
}
linkerFlags = "-L\(swiftPath.string.trimmingCharacters(in: .whitespacesAndNewlines))/lib/swift/linux -ldispatch -lFoundation -lswiftCore -lswiftGlibc -lswiftRemoteMirror -lswiftSwiftOnoneSupport -rpath \(swiftPath.string.trimmingCharacters(in: .whitespacesAndNewlines))/lib/swift/linux -fuse-ld=gold"
#else
print("Unsupported operating system.")
exit(-1)
#endif
guard let packageTemplate = Defile("Package.swiftlog") else {
print("Package.swiftlog not found. Aborting.")
exit(-1)
}
var lines = packageTemplate.lines!
lines.insert("let packageName = \"\(simulatable)\"", at: 2)
lines.insert("// Automatically generated by Swiftlog: Do not modify!", at: 2)
guard let output = Defile("Package.swift", mode: .write) else {
print("Could not open Package.swift for output.")
exit(-1)
}
try! output.write(string: lines.joined(separator: "\n"))
output.close()
if ("""
swift build \(swiftFlags) &&\
clang -fPIC \(clangFlags) \
.build/debug/Swiftlog.build/*.swift.o\
.build/debug/\(simulatable).build/*.swift.o\
.build/debug/VPIAssistant.build/VPIAssistant.c.o\
-o Executables/main.vpi \(linkerFlags)
""".sh() == 0
) {
exit(Int32("vvp -MExecutables/ -mmain Executables/main.vvp".sh()))
}