0

I try to write a RabbitMQ wrapper in Swift (CRabbitMQ), using the rabbitmq-c library from Brew.

To tell Swift who to include and link files, I specified the pkg-config file librabbitmq in package.swift:

let package = Package(
    name: "swift-rabbitmq",
    platforms: [
        .macOS(.v12)
    ],
    products: [
        .library(
            name: "CRabbitMQ",
            targets: [
                "CRabbitMQ"
            ])
    ],
    dependencies: [
        
    ],
    targets: [
        .systemLibrary(
            name: "CRabbitMQ", pkgConfig: "librabbitmq", providers: [
                .brewItem(["rabbitmq-c"])
            ]),
        .testTarget(
            name: "TestsCRabbitMQ",
            dependencies: [
                .target(name: "CRabbitMQ")
            ])
    ]
)

Through command line, pkg-config give me the right path:

$ pkg-config --cflags librabbitmq
-I/opt/homebrew/Cellar/rabbitmq-c/0.11.0/include -I/opt/homebrew/Cellar/openssl@3/3.0.5/include

(note: I also installed openssl@3).

My modulemap file is the following:

module CRabbitMQ [system] {
    header "rabbitmq.h"
    link "rabbitmq"
    export *
}

and the rabbitmq.h header:

#ifndef rabbitmq_h
#define rabbitmq_h

#import <amqp.h>
#import <amqp_framing.h>
#import <amqp_ssl_socket.h>
#import <amqp_tcp_socket.h>

#endif /* rabbitmq_h */

However, when I tried to compile my project, Swift tell me that the headers specified inside the rabbitmq.h header are not found.

Through command line, same, but when I specify the include path and the library path with linker arguments:

swift test -Xcc -I/opt/homebrew/Cellar/rabbitmq-c/0.11.0/include -Xlinker -L/opt/homebrew/Cellar/rabbitmq-c/0.11.0/lib -Xlinker -lrabbitmq

It works!

I don't understand why the flags inside the librabbitmq.pc pkg-config file are not used by swift.

Any ideas ?

The full repo is available on my GitHub: https://github.com/ugocottin/swift-rabbitmq

ugocottin
  • 64
  • 8

1 Answers1

0

The problem came from the librabbitmq.pc file provided by the rabbitmq-c library. The file contain the CFlags property instead of the Cflags property.

SwiftPM doesn't use the pkg-config program, but a PkgConfig class that must have the same behaviour than the pkg-config program.

Solutions:

  1. Fix the property CFlags inside the librabbitmq.pc.
  2. Enhance SwiftPM PkgConfig with errors messages, and ensure that the PkgConfig class have the same behaviour than the pkg-config program.
ugocottin
  • 64
  • 8