-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruncompiled.swift
executable file
·46 lines (34 loc) · 1.43 KB
/
runcompiled.swift
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
#!/usr/bin/env swift
import Foundation
let file = CommandLine.arguments.dropFirst(1).first ?? { fatalError("First argument should be file") }()
let showJIT = CommandLine.arguments.dropFirst(2).first.flatMap(NSString.init)?.boolValue ?? true
let executable = file.split(separator: ".").dropLast().joined()
guard file.hasSuffix(".swift") else { fatalError(" First arg is not a swift file") }
let fm = FileManager.default
guard !fm.fileExists(atPath: executable) else { fatalError("There is already a file at \(executable)") }
let compile = Process()
compile.executableURL = URL(fileURLWithPath: "/usr/bin/env")
compile.arguments = ["swiftc", file, "-o", executable, "-O", "-gnone", "-whole-module-optimization"]
try compile.run()
compile.waitUntilExit()
let jit = Process() // needs to be outside if for the signal C function
if showJIT {
print("--- JIT:")
jit.executableURL = URL(fileURLWithPath: file)
try? jit.run()
signal(SIGINT, { _ in jit.terminate() })
jit.waitUntilExit()
if jit.terminationReason != .exit { print(jit.terminationReason) }
}
print("--- compiled:")
let compiled = Process()
compiled.executableURL = URL(fileURLWithPath: executable)
try? compiled.run()
signal(SIGINT, { _ in compiled.terminate() })
compiled.waitUntilExit()
if compiled.terminationReason != .exit { print(compiled.terminationReason) }
try? fm.removeItem(atPath: executable)
func fatalError(_ msg: String) -> Never {
print(msg)
exit(1)
}