CommonCrypto as a System Library Target

I am trying to create a Swift package that uses CommonCrypto as a system library target (cf. systemlibrary(name:path:pkgconfig:providers:)), but I cannot get it to work by just passing the name:

// swift-tools-version: 5.8

import PackageDescription

let package = Package(
    name: "MyPackage",
    defaultLocalization: "en",
    platforms: [
        .iOS(.v14)
    ],
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]),
    ],
    targets: [
        .target(
            name: "MyPackage",
            dependencies: ["CommonCrypto"]
        )
        .systemLibrary(
            name: "CommonCrypto"
        )
    ],
    swiftLanguageVersions: [.v5]
)

I have a module map looking like this

module CommonCrypto [system] {
  umbrella header "CommonCrypto.h"
  export *
  module * { export * }
}

and the following umbrella header

#pragma once
#include <CommonCrypto/CommonCrypto.h>

in Sources/CommonCrypto. However, I still cannot swift build the package:

error: cannot find 'kCCSuccess' in scope

What am I missing?

Semi off-topic: which CommonCrypto API/functionality is it that you wanna use that is not part of swift-crypto / CryptoKit?

PBKDF2 (from CommonKeyDerivation.h) and TDEA (from CommonCryptor.h).

And it's "gotta" instead of "wanna" :wink:

The macOS 14 and iOS 17 SDKs already have a "usr/include/CommonCrypto/module.modulemap" file:

module CommonCrypto [system] {
  umbrella header "CommonCrypto.h"
  export *
  module * { export * }
  
  module Error {
      header "CommonCryptoError.h"
      export *
  }
  
  module Random {
      header "CommonRandom.h"
      export *
  }
}

I can simply import CommonCrypto — without using a system library target.
Is this not the case for previous SDKs?

1 Like

Thanks, you're right; even though I am still on macOS 13 I can just import CommonCrypto m(

1 Like