How to compile Swift from the command line for distribution

I have one Swift script command line file (it uploads the contents of the menu bar with the process identifier).

To give an idea of ​​the APIs I use, here are a few relevant lines:

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)

The whole file is available here .

When I compile this with swiftc menu.swift, I can just run it on my system where Swift is installed.

When I transfer the executable to menusomeone who does not have Swift, they get the following error when starting through Terminal:

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

I suppose I need to statically link something, but I'm not sure. I cannot verify this easily since I do not have access to the macOS assembly without Swift.

swiftc, script, macOS?

+4
1

, -static-stdlib.

script , rpaths Swift, 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)
> 

, , .

Linux- - Swift script Swift

+1

Source: https://habr.com/ru/post/1675073/


All Articles