swiftswift-package-managerapple-cryptokit

swift CryptoKit on Ubuntu


I am trying to compile a small swift program, "main.swift" to an executable on Ubuntu 18.08. I use the Swift Package Manager to manage my dependencies. In this very simple case I only have one dependency, namely this open-source CryptoKit. I have one swift file which just tries to import CryptoKit.

import Foundation
import CryptoKit
print("phew")

My Package.swift file looks like this:

// swift-tools-version:5.2

import PackageDescription

let package = Package(
    name: "decryp",
    dependencies: [
      .package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "1.0.1"))
    ],
    targets: [
        .target(
            name: "decryp",
            dependencies: ["swift-crypto"]
        ),
        .testTarget(
            name: "decrypTests",
            dependencies: ["decryp"]),
    ]
)

When I try to build the executable with swift build it fetches the repository, but then gives an error with a product not found. stdout from swift build:

Fetching https://github.com/apple/swift-crypto.git
Cloning https://github.com/apple/swift-crypto.git
Resolving https://github.com/apple/swift-crypto.git at 1.0.2
'decryp' /home/kah/decryp: error: product 'swift-crypto' not found. It is required by target 'decryp'.
warning: dependency 'swift-crypto' is not used by any target

Maybe I am missing something obvious? I am still a beginner in the swift world.


Solution

  • Looking at swift-crypto Package.swift gave a clue. The swift-crypto library name is "Crypto", so I tried with that instead which gave an error:

     error: dependency 'Crypto' in target 'decryp' requires explicit declaration; 
     reference the package in the target dependency with '.product(name: "Crypto", 
     package: "swift-crypto")'
    

    But that says what to do, so when I changed my Package.swift to the following and imported "Crypto" instead of "CryptoKit" everything works!

    // swift-tools-version:5.2
    
    import PackageDescription
    
    let package = Package(
        name: "decryp",
        dependencies: [
          .package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "1.0.1"))
        ],
        targets: [
            .target(
                name: "decryp",
                dependencies: [
                  .product(name: "Crypto", package: "swift-crypto")
    ]
            ),
            .testTarget(
                name: "decrypTests",
                dependencies: ["decryp"]),
        ]
    )
    

    Now I can send an encrypted message from my phone to a server and then decrypt it locally on my ubuntu laptop, thanks to the swift-crypto team!