how to wrap cassandra c++ driver in a swift module

354 Views Asked by At

I'm trying to write a server side swift application against the cassandra c++ driver (no swift native support as far as I know)

I was able to install the cassandra c++ locally on my mac like so:

 # Datastax C++ driver dependencies
   brew install libuv cmake
   brew install openssl
   brew link --force openssl

   # Install git if you dont have it
   brew install git

   # Retrieve the cpp 
   git clone https://github.com/datastax/cpp-driver.git --depth=1
   mkdir cpp-driver/build
   cd cpp-driver/build

   # Build with qualified path to OpenSSL location
   cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl/ -DOPENSSL_LIBRARIES=/usr/local/opt/openssl/lib ..
   make
   make install

verified with

 ls /usr/local/include/cassandra.h
-rw-r--r--  1 staff   323K Feb  2 14:26 /usr/local/include/cassandra.h

as well as :

pkg-config --list-all
...
cassandra             cassandra - A C/C++ client driver for Apache Cassandra and DataStax Products
...

I created a swift package using too many tutorials to count.

Mainly:

https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#requiring-system-libraries

and https://medium.com/shopify-mobile/wrapping-a-c-library-in-swift-part-1-6dd240070cef

but seems that the instructions are wrong per this thread ?

https://forums.swift.org/t/system-library-targets-package-has-unsupported-layout-modulemap/16614/6

currently my structure looks like this:

/Developer/cassandra-swift: tree
.
├── Package.swift
├── README.md
├── Sources
│   ├── cassandra
│   │   ├── cassandraumb.h
│   │   └── module.modulemap
│   └── cassandra-swift
│       └── cassandra_swift.swift
├── Tests
│   ├── LinuxMain.swift
│   └── cassandra-swiftTests
│       ├── XCTestManifests.swift
│       └── cassandra_swiftTests.swift

and the contents are:

cassandra-swift: cat Package.swift
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "cassandra-swift",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
          name: "CCassandra",
          targets: ["CCassandra"])
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    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.
        .systemLibrary(
                    name: "CCassandra",
                    path: "Sources/cassandra",
                    pkgConfig: "cassandra"
//                    providers: [
//                        .brew(["libgraphqlparser"]),
//                        .apt(["libgraphqlparser"]),
//                        ]
                    ),
//        .testTarget(
//            name: "cassandra-swiftTests",
//            dependencies: ["cassandra-swift"]),
    ]
)

//git clone https://github.com/datastax/cpp-driver.git --depth=1
//cd cpp-driver/
//ls
//mkdir build
//cd build/
//   cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl/ -DOPENSSL_LIBRARIES=/usr/local/opt/openssl/lib ..
//make
//make install
//less /usr/local/include/cassandra.h
//cat /usr/local/lib/pkgconfig/cassandra.pc


//-- Install configuration: ""
//-- Installing: /usr/local/include/cassandra.h
//-- Installing: /usr/local/include/dse.h
//-- Installing: /usr/local/lib/libcassandra.2.15.0.dylib
//-- Installing: /usr/local/lib/libcassandra.2.dylib
//-- Up-to-date: /usr/local/lib/libcassandra.dylib
//-- Installing: /usr/local/lib/pkgconfig/cassandra.pc
cassandra-swift: cat Sources/cassandra/module.modulemap
module CCassandra [system] {
    umbrella header  "cassandraump.h"
    header "/usr/local/include/cassandra.h"
    link "cassandra"
    export *
}
cat Sources/cassandra/cassandraumb.h
#import <c/cassandra.h>

I have a 2 test project. The first test project is a mac XCode application using the SPM integrated support and adding a git repo reference

the other project is a SPM generated project referencing the SPM package

Both don't work properly.

I've been playing around with this for hours.

I'd love for this to work locally in XCode for dev, as well as be portable (assuming through spm) to run in a swift docker container.

0

There are 0 best solutions below