Search code examples
swiftxcodemacoscryptoswift

Build error while trying to build a Swift-based command line tool depending on a third-party framework: this target might include its own product


Context: I want to create a simple Swift command line tool using CryptoSwift I am relatively new to Xcode and Swift (and MacOS!).

Configuration:
- MacOS High Sierra 10.13.2
- Xcode: 9.2

Steps:

  1. I start Xcode
  2. Create a new project “Command Line tool” for MacOS
  3. Option for my project:
    • Product name: cryptodemo
    • Organisation Identifier: com.demo
    • Language Swift
  4. I create the project into ~/Documents
  5. Fill my main.swift with:

    import Foundation
    import CryptoSwift
    
    print("Hello, World!")
    
    let bytes:Array<UInt8> = [0x01, 0x02, 0x03]
    let digest = Digest.md5(bytes)
    
  6. Open the shell and go into ~/Documents/cryptodemo

  7. Add CryptoSwift as a submodule as defined by the project’s README using: git submodule add https://github.com/krzyzanowskim/CryptoSwift.git
  8. Open Finder and drag the CryptoSwift.xcodeproj file into my Xcode project
  9. In Xcode, I go into my project Build Phase

    • I added CryptoSwift as a target dependency
    • I added CryptoSwift.framework to Link Binaries with Libraries
    • I added CryptoSwift.framework to Copy Files with:
      • Destination: Framework
      • SubPath: (empty) enter image description here
  10. Then I build it. I have this error:

    Check dependencies
    
    Unable to run command 'PBXCp CryptoSwift.framework' - this target might include its own product.
    Unable to run command 'CodeSign A' - this target might include its own product.
    

    enter image description here

Here is the archive of the project cryptodemo.zip


Solution

  • The only way I managed to build a command line tool using a third-party Swift libraries is to use Swift Package Manager (SPM).

    Example of Swift project using SPM (that could be generated using swift package init --type executable):

    // swift-tools-version:4.0
    // The swift-tools-version declares the minimum version of Swift required to build this package.
    
    import PackageDescription
    
    let package = Package(
        name: “mytool”,
        dependencies: [
            .package(url: "https://github.com/myfreeweb/SwiftCBOR.git", .branch("master")),
        ],
        targets: [
            // Targets are the basic building blocks of a package. A target can define a module or a test suite.
            // Targets can depend on other targets in this package, and on products in packages which this package depends on.
            .target(
                name: “mytool”,
                dependencies: [ "SwiftCBOR" ]),
        ]
    )
    

    A Xcode project could be generated from this SPM using: swift package generate-xcodeproj