Swift Package Manager loses track of system library when adding an additional dependency

I'm trying to include a C library that depends on a 3rd, system (pkg-config) library. I have written a trivial test that makes use of the system library. This Package.swift runs the test successfully:

// swift-tools-version:4.2

import PackageDescription

let package = Package(
    name: "Qui",
    products: [
        .library(name: "Qui", targets: ["Qui"]),
    ],
    targets: [
        .systemLibrary(name: "Clibbtc", pkgConfig: "libbtc"),
        .target(name: "Cbitpay", dependencies: ["Clibbtc"]),
        .target(name: "Qui", dependencies: ["Clibbtc"]),
        .testTarget(name: "QuiTests", dependencies: ["Qui"]),
    ]
)

However, simply adding "Cbitpay", to the Qui dependencies causes the following error when running those same tests:

Linking ./.build/x86_64-apple-macosx10.10/debug/QuiPackageTests.xctest/Contents/MacOS/QuiPackageTests
ld: warning: Could not find auto-linked library 'libbtc'
Undefined symbols for architecture x86_64:
  "_sha256_Raw", referenced from:
      _btc_hash in key_utils.c.o
      _btc_hash_sngl_sha256 in key_utils.c.o
  "_utils_hex_to_uint8", referenced from:
      _genPubKeyFromPrivKeyRaw in key_utils.c.o
      _signMessageWithPrivateKey in key_utils.c.o
  "_utils_uint8_to_hex", referenced from:
      _generatePrivateKey in key_utils.c.o
      _signMessageWithPrivateKey in key_utils.c.o
ld: symbol(s) not found for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
error: terminated(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/joe/Development/Library/Qui/.build/debug.yaml test output:

This is likely the culprit. I am not entirely sure what will cause the linker to not find an auto-linked library, will have to investigate.

Right; again the weird thing is that by simply removing "Cbitpay" from the dependency list of Qui (as it is above), the libbtc library is found by the linker and the tests run successfully. That sole modification some how causes the linker to lose track of libbtc.

I've also tried removing Clibbtc from the Qui dependency list (since it's already included with Cbitpay, but that doesn't help either).

Could you please share the output of swift build --verbose?

While using this Package.swift:

// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Qui",
    products: [
        .library(name: "Qui", targets: ["Qui"]),
    ],
    targets: [
    	.systemLibrary(name: "Clibbtc", pkgConfig: "libbtc"),
    	.target(name: "Cbitpay", dependencies: ["Clibbtc"]),
    	.target(name: "Qui", dependencies: ["Cbitpay", "Clibbtc"]),
    	.testTarget(name: "QuiTests", dependencies: ["Qui"]),
    ]
)

[swift build] works without error, however the response for [swift test --verbose]:

xcrun --sdk macosx --show-sdk-path
xcrun --sdk macosx --show-sdk-platform-path
xcrun --find clang
xcrun --sdk macosx --find xctest
sandbox-exec -p '(version 1)
(deny default)
(import "system.sb")
(allow file-read*)
(allow process*)
(allow sysctl*)
(allow file-write*
    (regex #"^/private/var/tmp/org\.llvm\.clang.*")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/org\.llvm\.clang.*")
)
' /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc --driver-mode=swift -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4_2 -lPackageDescription -suppress-warnings -swift-version 4.2 -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4_2 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk /Users/joe/Development/Library/Qui/Package.swift -fileno 5
pkg-config --variable pc_path pkg-config
sandbox-exec -p '(version 1)
(deny default)
(import "system.sb")
(allow file-read*)
(allow process*)
(allow sysctl*)
(allow file-write*
    (regex #"^/private/var/tmp/org\.llvm\.clang.*")
    (regex #"^/private/var/tmp/ar.*")
    (regex #"^/private/var/tmp/xcrun.*")
    (regex #"^/private/var/tmp/.*\.(swift|c)-[0-9a-f]+\.autolink")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org\.llvm\.clang.*")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/ar.*")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/xcrun.*")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/.*\.(swift|c)-[0-9a-f]+\.autolink")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/ar.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/xcrun.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/.*\.(swift|c)-[0-9a-f]+\.autolink")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/ar.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/xcrun.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/.*\.(swift|c)-[0-9a-f]+\.autolink")
    (subpath "/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org.swift.swiftpm.joe")
    (subpath "/Users/joe/Development/Library/Qui/.build")
    (subpath "/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org.swift.swiftpm.tests-3")
)
' /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/joe/Development/Library/Qui/.build/debug.yaml test -v
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -L/Users/joe/Development/Library/LibBTC/Core/bitpay-c-keyutils/lib/libbtc/.libs -lbtc -g -L /Users/joe/Development/Library/Qui/.build/x86_64-apple-macosx10.10/debug -o /Users/joe/Development/Library/Qui/.build/x86_64-apple-macosx10.10/debug/QuiPackageTests.xctest/Contents/MacOS/QuiPackageTests -module-name QuiPackageTests -Xlinker -bundle @/Users/joe/Development/Library/Qui/.build/x86_64-apple-macosx10.10/debug/QuiPackageTests.product/Objects.LinkFileList
ld: warning: Could not find auto-linked library 'libbtc'
Undefined symbols for architecture x86_64:
  "_sha256_Raw", referenced from:
      _btc_hash in key_utils.c.o
      _btc_hash_sngl_sha256 in key_utils.c.o
  "_utils_hex_to_uint8", referenced from:
      _genPubKeyFromPrivKeyRaw in key_utils.c.o
      _signMessageWithPrivateKey in key_utils.c.o
  "_utils_uint8_to_hex", referenced from:
      _generatePrivateKey in key_utils.c.o
      _signMessageWithPrivateKey in key_utils.c.o
ld: symbol(s) not found for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
error: terminated(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/joe/Development/Library/Qui/.build/debug.yaml test -v output:

And by removing "Cbitpay" from the Qui dependencies:

// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Qui",
    products: [
        .library(name: "Qui", targets: ["Qui"]),
    ],
    targets: [
    	.systemLibrary(name: "Clibbtc", pkgConfig: "libbtc"),
    	.target(name: "Cbitpay", dependencies: ["Clibbtc"]),
    	.target(name: "Qui", dependencies: ["Clibbtc"]),
    	.testTarget(name: "QuiTests", dependencies: ["Qui"]),
    ]
)

Both [swift build] and [swift test] work. The results for [swift test --verbose]:

xcrun --sdk macosx --show-sdk-path
xcrun --sdk macosx --show-sdk-platform-path
xcrun --find clang
xcrun --sdk macosx --find xctest
sandbox-exec -p '(version 1)
(deny default)
(import "system.sb")
(allow file-read*)
(allow process*)
(allow sysctl*)
(allow file-write*
    (regex #"^/private/var/tmp/org\.llvm\.clang.*")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/org\.llvm\.clang.*")
)
' /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc --driver-mode=swift -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4_2 -lPackageDescription -suppress-warnings -swift-version 4.2 -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4_2 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk /Users/joe/Development/Library/Qui/Package.swift -fileno 5
pkg-config --variable pc_path pkg-config
sandbox-exec -p '(version 1)
(deny default)
(import "system.sb")
(allow file-read*)
(allow process*)
(allow sysctl*)
(allow file-write*
    (regex #"^/private/var/tmp/org\.llvm\.clang.*")
    (regex #"^/private/var/tmp/ar.*")
    (regex #"^/private/var/tmp/xcrun.*")
    (regex #"^/private/var/tmp/.*\.(swift|c)-[0-9a-f]+\.autolink")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org\.llvm\.clang.*")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/ar.*")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/xcrun.*")
    (regex #"^/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/.*\.(swift|c)-[0-9a-f]+\.autolink")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/ar.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/xcrun.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/.*\.(swift|c)-[0-9a-f]+\.autolink")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/ar.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/xcrun.*")
    (regex #"^/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/C/.*\.(swift|c)-[0-9a-f]+\.autolink")
    (subpath "/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org.swift.swiftpm.joe")
    (subpath "/Users/joe/Development/Library/Qui/.build")
    (subpath "/private/var/folders/rd/mzmyzp2x5rs12fwdggp93jcc0000gn/T/org.swift.swiftpm.tests-3")
)
' /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/joe/Development/Library/Qui/.build/debug.yaml test -v
/Applications/Xcode.app/Contents/Developer/usr/bin/xctest /Users/joe/Development/Library/Qui/.build/x86_64-apple-macosx10.10/debug/QuiPackageTests.xctest
Test Suite 'All tests' started at 2018-12-20 05:39:04.781
Test Suite 'QuiPackageTests.xctest' started at 2018-12-20 05:39:04.781
Test Suite 'QuiTests' started at 2018-12-20 05:39:04.781
Test Case '-[QuiTests.QuiTests testExample]' started.
0x00007ff914f33750
Test Case '-[QuiTests.QuiTests testExample]' passed (0.125 seconds).
Test Suite 'QuiTests' passed at 2018-12-20 05:39:04.906.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.125 (0.125) seconds
Test Suite 'QuiPackageTests.xctest' passed at 2018-12-20 05:39:04.906.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.125 (0.125) seconds
Test Suite 'All tests' passed at 2018-12-20 05:39:04.906.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.125 (0.125) seconds
1 Like

This is all on macOS, BTW.

Terms of Service

Privacy Policy

Cookie Policy