如何从命令行编译Swift以进行分发

时间:2017-04-18 17:55:34

标签: swift macos swift3

我有一个单独的文件Swift命令行脚本(它会在给定进程ID的情况下转储菜单栏的内容)。

要了解我正在使用的API,这里有几个相关的行:

import Foundation
import Cocoa

// ...
func getAttribute(element: AXUIElement, name: String) -> CFTypeRef? {
    var value: CFTypeRef? = nil
    AXUIElementCopyAttributeValue(element, name as CFString, &value)
    return value
}

// ...
var app: NSRunningApplication? = nil
if pid == -1 {
    app = NSWorkspace.shared().menuBarOwningApplication
}
else {
    app = NSRunningApplication(processIdentifier: pid)
}

// ...
let axApp = AXUIElementCreateApplication(app.processIdentifier)

整个文件可用here

当我使用swiftc menu.swift编译它时,我可以在安装了Swift的系统中运行它。

当我将menu可执行文件共享给没有Swift的人时,他们通过终端运行时会出现以下错误:

Code 6: dyld: Library not loaded: @rpath/libswiftAppKit.dylib
  Referenced from: ./menu
  Reason: image not found

我想我需要静态链接某些内容,但我不确定。我无法轻松测试,因为没有Swift,我无法访问macOS。

我如何使用swiftc以便我可以编译我的脚本以便它可以在任何macOS系统上运行?

1 个答案:

答案 0 :(得分:2)

您可以使用-static-stdlib仅使用标准库的情况解决此问题。

编译没有选项的脚本时,最终的可执行文件包含各种Swift标准库的rpath,您可以使用otool进行验证。

> swiftc menu.swift 
> otool -L menu
menu:
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.28.0)
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
    /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.75.0)
    /System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 48.0.0)
    /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.25.0)
    @rpath/libswiftAppKit.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftCore.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftCoreData.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftDarwin.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftDispatch.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftFoundation.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftIOKit.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftSwiftOnoneSupport.dylib (compatibility version 1.0.0, current version 800.0.63)
    @rpath/libswiftXPC.dylib (compatibility version 1.0.0, current version 800.0.63)    

使用-static-stdlib可确保标准库根据需要链接。

> swiftc -static-stdlib menu.swift 
> otool -L menu
menu:
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.28.0)
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.4.0)
    /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.25.0)
    /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.75.0)
    /System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 48.0.0)
    /System/Library/Frameworks/CoreData.framework/Versions/A/CoreData (compatibility version 1.0.0, current version 752.8.0)
    /System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1070.13.0)
> 

我仍然不知道如何链接第三方框架 - 但上述步骤解决了我原来的问题。

Related Linux Question - Compile Swift script with static Swift core library