From 2005bd29a62d32794b73fe95b95fb52e19e3509b Mon Sep 17 00:00:00 2001 From: Scott Kovatch Date: Tue, 8 May 2018 09:12:54 -0700 Subject: [PATCH 1/6] Updated for Swift 4/Xcode 9.3 --- ScriptWorker.xcodeproj/project.pbxproj | 38 ++++++++++++++----- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++ .../xcschemes/ScriptWorker.xcscheme | 2 +- ScriptWorker/PipeWriter.h | 2 +- ScriptWorker/PipeWriter.m | 2 +- ScriptWorker/ScriptTask.swift | 5 ++- swiftscript/SwiftScript.swift | 2 +- 7 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 ScriptWorker.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/ScriptWorker.xcodeproj/project.pbxproj b/ScriptWorker.xcodeproj/project.pbxproj index 926cea8..4e2d04e 100644 --- a/ScriptWorker.xcodeproj/project.pbxproj +++ b/ScriptWorker.xcodeproj/project.pbxproj @@ -379,16 +379,16 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0820; - LastUpgradeCheck = 0800; + LastUpgradeCheck = 0930; ORGANIZATIONNAME = "Stephen Marquis"; TargetAttributes = { AA514C971D6900BE00D563F2 = { CreatedOnToolsVersion = 7.3.1; - LastSwiftMigration = 0830; + LastSwiftMigration = 0930; }; AA514CA11D6900BE00D563F2 = { CreatedOnToolsVersion = 7.3.1; - LastSwiftMigration = 0800; + LastSwiftMigration = 0930; }; AA514CC81D6927C500D563F2 = { CreatedOnToolsVersion = 7.3.1; @@ -396,6 +396,7 @@ }; B15A76B81E9558EF00AEABA3 = { CreatedOnToolsVersion = 8.2.1; + LastSwiftMigration = 0930; ProvisioningStyle = Automatic; }; B15A76D91E959C9A00AEABA3 = { @@ -404,6 +405,7 @@ }; B1D5BD511DF2219000E93D97 = { CreatedOnToolsVersion = 8.1; + LastSwiftMigration = 0930; ProvisioningStyle = Automatic; }; }; @@ -558,14 +560,22 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -609,14 +619,22 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -663,7 +681,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -685,7 +703,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.BobDevil.ScriptWorker; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -697,6 +715,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.BobDevil.ScriptWorkerTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -708,6 +727,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.BobDevil.ScriptWorkerTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -743,7 +763,7 @@ CLANG_WARN_DOCUMENTATION_COMMENTS = YES; MACOSX_DEPLOYMENT_TARGET = 10.12; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -753,7 +773,7 @@ CLANG_WARN_DOCUMENTATION_COMMENTS = YES; MACOSX_DEPLOYMENT_TARGET = 10.12; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -792,7 +812,7 @@ CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_SUSPICIOUS_MOVES = YES; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -802,7 +822,7 @@ CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_SUSPICIOUS_MOVES = YES; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/ScriptWorker.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/ScriptWorker.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/ScriptWorker.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/ScriptWorker.xcodeproj/xcshareddata/xcschemes/ScriptWorker.xcscheme b/ScriptWorker.xcodeproj/xcshareddata/xcschemes/ScriptWorker.xcscheme index 9bd5d3e..1b536d8 100644 --- a/ScriptWorker.xcodeproj/xcshareddata/xcschemes/ScriptWorker.xcscheme +++ b/ScriptWorker.xcodeproj/xcshareddata/xcschemes/ScriptWorker.xcscheme @@ -1,6 +1,6 @@ ((Void) -> Void) { + private func setup(pipe: Pipe, stdout: Bool) -> (() -> Void) { let readHandle = pipe.fileHandleForReading let semaphore = DispatchSemaphore(value: 1) @@ -260,7 +260,8 @@ public class ScriptTask { semaphore.signal() } - return { [weak self] in + return { + [weak self] in // The 'readabilityHandler' for a file handle doesn't get triggered for EOF for whatever reason, so we clear out the readability handler and read the last available data when the task is done. semaphore.wait() readHandle.readabilityHandler = nil diff --git a/swiftscript/SwiftScript.swift b/swiftscript/SwiftScript.swift index 3d78bed..de14325 100644 --- a/swiftscript/SwiftScript.swift +++ b/swiftscript/SwiftScript.swift @@ -23,7 +23,7 @@ class SwiftScript { return nil } - var path = trimmedLine.substring(from: range.upperBound).trimmingCharacters(in: .whitespaces) + var path = String(trimmedLine[.. Date: Tue, 8 May 2018 09:21:40 -0700 Subject: [PATCH 2/6] Reversed a substring. --- swiftscript/SwiftScript.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swiftscript/SwiftScript.swift b/swiftscript/SwiftScript.swift index de14325..3b8c4f0 100644 --- a/swiftscript/SwiftScript.swift +++ b/swiftscript/SwiftScript.swift @@ -23,7 +23,7 @@ class SwiftScript { return nil } - var path = String(trimmedLine[.. Date: Thu, 20 Sep 2018 13:29:01 -0700 Subject: [PATCH 3/6] Need @escaping to make the compiler happy again. --- ScriptWorker/trap.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ScriptWorker/trap.swift b/ScriptWorker/trap.swift index 65ba5bc..1ce8f76 100755 --- a/ScriptWorker/trap.swift +++ b/ScriptWorker/trap.swift @@ -12,7 +12,7 @@ enum Signal: Int32 { case TERM = 15 } -func trap(signal: Signal, action: @convention(c) (Int32) -> ()) { +func trap(signal: Signal, action: @escaping @convention(c) (Int32) -> ()) { // From Swift, sigaction.init() collides with the Darwin.sigaction() function. // This local typealias allows us to disambiguate them. typealias SignalAction = sigaction From bfdc5cc023753af9ff78c3286076b792b4d4407d Mon Sep 17 00:00:00 2001 From: Scott Kovatch Date: Fri, 21 Sep 2018 13:25:07 -0700 Subject: [PATCH 4/6] Made sure the type of UnsafeMutablePointer matched what we got from workingDirectoryTemplate. --- swiftscript/main.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swiftscript/main.swift b/swiftscript/main.swift index c9cd650..8d3c106 100644 --- a/swiftscript/main.swift +++ b/swiftscript/main.swift @@ -38,7 +38,7 @@ func runExec(task: String, args: [String], overrideExecutablePath: String? = nil // Returns the path to the executable in the temporary directory func setup(file: URL, withAdditionalFiles additionalFiles: [URL], searchPaths: [URL]) -> URL { let workingDirectoryTemplate = NSTemporaryDirectory() + "/swiftscript-XXX" - guard let cstring = mkdtemp(UnsafeMutablePointer(mutating: workingDirectoryTemplate.cString(using: .utf8))) else { + guard let cstring = mkdtemp(UnsafeMutablePointer(mutating: workingDirectoryTemplate.cString(using: .utf8))) else { print("Failed to create temporary directory for compilation: errno \(errno)") exit(1) } From a9f81c56e40bcdbb7982b161ded58f6ad5c09441 Mon Sep 17 00:00:00 2001 From: Scott Kovatch Date: Fri, 21 Sep 2018 13:37:35 -0700 Subject: [PATCH 5/6] Pull UnsafeMutablePointer outside of the usage; seems to behave better. --- swiftscript/main.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/swiftscript/main.swift b/swiftscript/main.swift index 8d3c106..f695d3d 100644 --- a/swiftscript/main.swift +++ b/swiftscript/main.swift @@ -38,7 +38,9 @@ func runExec(task: String, args: [String], overrideExecutablePath: String? = nil // Returns the path to the executable in the temporary directory func setup(file: URL, withAdditionalFiles additionalFiles: [URL], searchPaths: [URL]) -> URL { let workingDirectoryTemplate = NSTemporaryDirectory() + "/swiftscript-XXX" - guard let cstring = mkdtemp(UnsafeMutablePointer(mutating: workingDirectoryTemplate.cString(using: .utf8))) else { + let workingDirCString = UnsafeMutablePointer(mutating: workingDirectoryTemplate.cString(using: .utf8)) + + guard let cstring = mkdtemp(workingDirCString) else { print("Failed to create temporary directory for compilation: errno \(errno)") exit(1) } From 14dbf01759d03f00609af7905ed64c0bfaea91fa Mon Sep 17 00:00:00 2001 From: Scott Kovatch Date: Fri, 21 Sep 2018 14:07:19 -0700 Subject: [PATCH 6/6] Trying again using withCString. --- swiftscript/main.swift | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/swiftscript/main.swift b/swiftscript/main.swift index f695d3d..0dbcf6a 100644 --- a/swiftscript/main.swift +++ b/swiftscript/main.swift @@ -38,30 +38,33 @@ func runExec(task: String, args: [String], overrideExecutablePath: String? = nil // Returns the path to the executable in the temporary directory func setup(file: URL, withAdditionalFiles additionalFiles: [URL], searchPaths: [URL]) -> URL { let workingDirectoryTemplate = NSTemporaryDirectory() + "/swiftscript-XXX" - let workingDirCString = UnsafeMutablePointer(mutating: workingDirectoryTemplate.cString(using: .utf8)) - - guard let cstring = mkdtemp(workingDirCString) else { - print("Failed to create temporary directory for compilation: errno \(errno)") - exit(1) - } - - let workingDirectory = URL(fileURLWithPath: String(cString: cstring)) - let swiftFiles = SwiftScript.swiftURLs(for: additionalFiles) + print("working dir template = \(workingDirectoryTemplate)") - let (mainFile, compiledFiles) = SwiftScript.setup(workingDirectory: workingDirectory, for: file, with: swiftFiles) + return workingDirectoryTemplate.withCString { workingDirCString in + guard let cstring = mkdtemp(UnsafeMutablePointer(mutating:workingDirCString)) else { + print("Failed to create temporary directory for compilation: errno \(errno)") + exit(1) + } + + let workingDirectory = URL(fileURLWithPath: String(cString: cstring)) - let process = Process() - process.launchPath = "/usr/bin/xcrun" + let swiftFiles = SwiftScript.swiftURLs(for: additionalFiles) - let executableURL = workingDirectory.appendingPathComponent(file.deletingPathExtension().lastPathComponent) + let (mainFile, compiledFiles) = SwiftScript.setup(workingDirectory: workingDirectory, for: file, with: swiftFiles) - let args = ["swiftc", "-o", executableURL.path] + SwiftScript.swiftArguments(for: mainFile, additionalFiles: compiledFiles, searchPaths: searchPaths) - process.arguments = args - process.launch() - process.waitUntilExit() + let process = Process() + process.launchPath = "/usr/bin/xcrun" + + let executableURL = workingDirectory.appendingPathComponent(file.deletingPathExtension().lastPathComponent) + + let args = ["swiftc", "-o", executableURL.path] + SwiftScript.swiftArguments(for: mainFile, additionalFiles: compiledFiles, searchPaths: searchPaths) + process.arguments = args + process.launch() + process.waitUntilExit() - return executableURL + return executableURL + } } guard CommandLine.arguments.count >= 2 else {