How to specify CFLAGS for a system library?

My question is probably redundant, but all I found on the subject were old posts.

I’m trying to use OpenLDAP in a Swift project of mine. To do this, I create a system library:

.systemLibrary(name: "COpenLDAP", providers: [.apt(["libldap2-dev"]), .brew(["openldap"])])

Everything is good on Linux. On macOS however, all function calls to the LDAP lib are marked deprecated. To fix this, I want to force the Swift compiler to use Homebrew’s installation of OpenLDAP instead of the system one. And this is where it starts to get fun.

OpenLDAP does not have a pkg-config file. So we cannot simple set the PKG_CONFIG_PATH variable! We have to set the -Xcc -I… -Xlinker -L options.

However, weirdly enough, the cSettings and linkerSettings arguments I can set for a “regular” target do not seem to be available for system library targets.

  1. Why is that? Is there something that can natively be done to force the search path of the headers and libs for a system library target?
  2. Am I correct in assuming I could theoretically autogenerate a .pc file for OpenLDAP in the Package.swift file and use this file w/ the pkgConfig argument of the .systemLibrary function?
  3. Is it possible to give an absolute path to the pkgConfig argument?
  4. If found, is the .pc file always preferred over the system libs?

Thank you for the help

It's not a perfect solution, but I will usually create a .pc file for libraries which don't have one (i.e. a custom lib I build and install myself).

This of course leaves something to be desired, since the .pc file will have to be included in the PKG_CONFIG_PATH manually for anyone who needs to use this package, so the swift package in itself is not fully encapsulated.

It would be really nice to be able to specify a .pc path explicitly in the Package.swift.

2 Likes