Compile a Single Swift File Against the Argument Parser Package (Without Using SwiftPM for the Executable)

Hello,

I'm having a terrible time figuring out how to compile a single .swift file using swiftc against a downloaded & locally-built copy of ArgumentParser, without using SPM for the executable.


For background on why I want to do this seemingly-strange thing:

I'm not a professional programmer, but rather an advanced AppleScript user, who's bumping his head against the ceiling of what is possible in ASObjC (namely: blocks, completion handlers).

For this, I have many small (1 - 20 line) compiled helper executables that I can run from AppleScript, which let me use the methods unavailable in ASObjC. I initially wrote these in Objective-C, but have transitioned to Swift. Right now, I am manually parsing the command-line arguments to these executables, but would really like to use swift-argument-parser.

These executables don't require any other 3rd-party packages, as they're only used to access macOS APIs. For several reasons, it's not currently feasible to have these executables be swift packages, nor is something like the excellent swift-sh the right solution.

I've done some extensive googling on the subject, but haven't found any workable solutions after many evenings spent on this. There's some mention of being able to bundle a SPM package into an XCFramework, but I wasn't able to get this to work either.


I've taken the following general steps:

  1. Download & build the ArgumentParser module.

    mkdir ~/Desktop/SPMDownload
    cd ~/Desktop/SPMDownload
    git clone https://github.com/apple/swift-argument-parser
    cd swift-argument-parser
    swift build -c release
    
  2. Import & use ArgumentParser in the desired source file, here ~/Desktop/example.swift:

    import ArgumentParser
    
    struct Greet: ParsableCommand {
        @Argument(help: "The thing to greet.")
        var name: String = "World"
    
        mutating func run() throws {
            print("Hello, \(name)!")
        }
    }
    
    Greet.main()
    
  3. Compile example.swift against ArgumentParser (not working):

    xcrun -sdk macosx swiftc -o ~/Desktop/example ~/Desktop/example.swift
    

    I've tried multiple command-line options here (both with & without trailing whitespace)...
    -I
    -L
    -l

    ...with various paths in the built ArgumentParser release folder located at...

    ~/Desktop/SPMDownload/swift-argument-parser/.build/release
    

    ...with no success.

What am I missing to be able to link example.swift against ArgumentParser?

I don't have a direct answer and someone here may have a more direct answer to your question.

But it might help to do the following to figure out how to do this on the command line:

  • Create a Swift package that has an executable target and product, includes your single file in its sources, and adds Argument Parser as a dependency.

  • Build the package with verbose logging and see how the package manager is invoking the compiler. (Or the build log if you do this in Xcode).

That should give you the incantation passed to the compiler.

1 Like

Thanks James – that's an excellent suggestion.

If I follow your suggestion & temporarily compile the example as a package:

mkdir example_package
cd example_package
swift package init --type tool
swift build -v -c release 2> ~/Desktop/example_package_log.txt

I get the following output to stderr from swift build:

Build Log
warning: 'example_package': /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/Package.swift -target x86_64-apple-macosx13.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -vfsoverlay /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.zWJtz9/vfs.yaml -swift-version 5 -package-description-version 5.10.0 -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name main -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.m77zaY/Package-1.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.m77zaY/Package-1.o -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk --target=x86_64-apple-macosx13.0 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCompatibilityPacks.a -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -lPackageDescription -Xlinker -rpath -Xlinker /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.isRpud/example_package-manifest
Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: x86_64-apple-macosx13.0
Running resolver because the following dependencies were added: 'swift-argument-parser' (https://github.com/apple/swift-argument-parser.git)
Fetching https://github.com/apple/swift-argument-parser.git from cache
Fetched https://github.com/apple/swift-argument-parser.git from cache (0.22s)
Computing version for https://github.com/apple/swift-argument-parser.git
warning: 'swift-argument-parser': /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Package@swift-5.8.swift -target x86_64-apple-macosx13.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -vfsoverlay /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.smIlSf/vfs.yaml -swift-version 5 -package-description-version 5.8.0 -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name main -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.l4ZjrA/Package@swift-5.8-1.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.l4ZjrA/Package@swift-5.8-1.o -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk --target=x86_64-apple-macosx13.0 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCompatibilityPacks.a -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -lPackageDescription -Xlinker -rpath -Xlinker /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.QD1uPF/swift-argument-parser-manifest
Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: x86_64-apple-macosx13.0
Computed https://github.com/apple/swift-argument-parser.git at 1.4.0 (0.33s)
Creating working copy for https://github.com/apple/swift-argument-parser.git
Working copy of https://github.com/apple/swift-argument-parser.git resolved at 1.4.0
warning: 'swift-argument-parser': /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Package@swift-5.8.swift -target x86_64-apple-macosx13.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -vfsoverlay /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.wkOQjP/vfs.yaml -swift-version 5 -package-description-version 5.8.0 -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name main -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.2L848X/Package@swift-5.8-1.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.2L848X/Package@swift-5.8-1.o -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk --target=x86_64-apple-macosx13.0 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCompatibilityPacks.a -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -lPackageDescription -Xlinker -rpath -Xlinker /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.fpIpRS/swift-argument-parser-manifest
Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: x86_64-apple-macosx13.0
info: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/GenerateManualPlugin.swift /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/GenerateManualPluginError.swift /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/PackagePlugin+Helpers.swift -target x86_64-apple-macosx13.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/PluginAPI -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -g -swift-version 5 -package-description-version 5.8.0 -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -serialize-diagnostics-path /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/plugins/cache/GenerateManual.dia -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name GenerateManual -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -emit-module-doc-path /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManual-1.swiftdoc -emit-module-source-info-path /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManual-1.swiftsourceinfo -parse-as-library -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManual-1.swiftmodule -emit-abi-descriptor-path /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManual-1.abi.json
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/GenerateManualPlugin.swift /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/GenerateManualPluginError.swift /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/PackagePlugin+Helpers.swift -target x86_64-apple-macosx13.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/PluginAPI -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -g -swift-version 5 -package-description-version 5.8.0 -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -serialize-diagnostics-path /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/plugins/cache/GenerateManual.dia -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name GenerateManual -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -parse-as-library -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManualPlugin-1.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/GenerateManualPlugin.swift -primary-file /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/GenerateManualPluginError.swift /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/PackagePlugin+Helpers.swift -target x86_64-apple-macosx13.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/PluginAPI -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -g -swift-version 5 -package-description-version 5.8.0 -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -serialize-diagnostics-path /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/plugins/cache/GenerateManual.dia -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name GenerateManual -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -parse-as-library -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManualPluginError-1.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/GenerateManualPlugin.swift /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/GenerateManualPluginError.swift -primary-file /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/checkouts/swift-argument-parser/Plugins/GenerateManual/PackagePlugin+Helpers.swift -target x86_64-apple-macosx13.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/PluginAPI -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -g -swift-version 5 -package-description-version 5.8.0 -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -serialize-diagnostics-path /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/plugins/cache/GenerateManual.dia -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name GenerateManual -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -external-plugin-path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server' -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -parse-as-library -o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/PackagePlugin+Helpers-1.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -Wl,-add_ast_path,/var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManual-1.swiftmodule /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManualPlugin-1.o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/GenerateManualPluginError-1.o /var/folders/qb/v4207vy10dsgkg11hwtq3ynw0000gn/T/TemporaryDirectory.lmKJja/PackagePlugin+Helpers-1.o -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk --target=x86_64-apple-macosx13.0 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCompatibilityPacks.a -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/PluginAPI -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -lPackagePlugin -Xlinker -rpath -Xlinker /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/PluginAPI -o /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/plugins/cache/GenerateManual
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/dsymutil /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/plugins/cache/GenerateManual -o /Users/__USERNAME__/Desktop/SPMDownload/swift-argument-parser/example_package/.build/plugins/cache/GenerateManual.dSYM
Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: x86_64-apple-macosx13.0

Hopefully this can help someone direct me on how to use swiftc directly on example.swift – I'm unfortunately not much further ahead after reading through the above log. I think it's not helped by the fact that ArgumentParser is a more complicated package with multiple build products.

I have posted this as an issue on the GitHub page, located here:

I will update this post with any developments.

1 Like

To really make Swift packages useful in scripts (which I'd also have many uses for!), we will need new features to the compiler still. The most recent discussions I could find were:

But let me try to answer this question, should it help you forward somehow!

Looking at what swift build -c release --verbose does under swift-argument-parser package, I can tell that SwiftPM passes the *.o files via indirection to swiftc when compiling the example apps. (Search for Objects.LinkFileList in the output.)

Assuming the command doesn't grow too large, you can also pass the object files directly; it looks like you'll need both ArgumentParser.build/*.o and ArgumentParserToolInfo.build/*.o.

Having first built swift-argument-parser, I was able to get your example to build and run with this command:

$ xcrun -sdk macosx swiftc \
  -o example \
  example.swift \
  ~/Desktop/SPMDownload/swift-argument-parser/.build/release/ArgumentParser.build/*.o \
  ~/Desktop/SPMDownload/swift-argument-parser/.build/release/ArgumentParserToolInfo.build/*.o \
  -I ~/Desktop/SPMDownload/swift-argument-parser/.build/release/
$ ./example
Hello, World!

(I don't know what to make out of it, but would love to hear back if you do!)

Fantastic - that's works great! I can't thank you enough!!

This will let me use ArgumentParser for all my little command-line tools, and when SwiftPM/Swift Scripting is more mature, I can switch over to the "correct" way of doing things.

This was my last-ditch attempt at this after a couple of years of on & off failures – I really can't understate how much I appreciate you taking the time out of your day to solve this. You've made my week! :smiley:


If you don't mind, I do have a couple of follow-up questions, in case you know the answer off-hand:

  • Right now, I compile everything on an Intel machine & run the executable on Intel & ARM machines (using Rosetta 2). If I compile ArgumentParser for both architectures...

    swift build -c release --arch x86_64 --arch arm64
    

    ...the command-line invocation you've provided above no longer works. Any thoughts?

  • Do you know if this process is generally applicable (within limits) to other packages?

If those questions are out of scope, please don't trouble yourself about them – you've been hugely helpful with your previous answer. Thanks again!

It gets tricky now. The directory structure under .build/ becomes different when you build for multiple architectures. And for more package dependencies in general, you'd need even more out-of-band information to know which .o files to include in the build.

OTOH, if you just gave in and turned the script into a tiny Swift package, you could build it as such, without needing to clone and build swift-argument-parser separately. The steps needed are:

  1. mkdir example
    mv example.swift example/main.swift
    touch example/Package.swift
    
  2. Change the contents of example/Package.swift to:
    // swift-tools-version:5.10
    import PackageDescription
    let package = Package(
      name: "Example",
      products: [.executable(name: "example", targets: ["example"])],
      dependencies: [.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0")],
      targets: [
        .executableTarget(
          name: "example",
          dependencies: [.product(name: "ArgumentParser", package: "swift-argument-parser")],
          path: "."
        ),
      ]
    )
    
  3. Under example/, run swift build -c release --arch x86_64 --arch arm64.
  4. Find the binary file example under the directory returned by swift build -c release --arch x86_64 --arch arm64 --show-bin-path.