Define a macro if string.h has memset_s

I'm adding a Package.json file to Signal's libsignal-protocol-c.

They use CMake, so I'm translating CMake to SwiftPM.

Thier CMakeLists.txt file uses the CMake check_symbol_exists command to define a macro named HAVE_MEMSET_S if memset_s is available in the system's libc string.h header file.

Is it possible to do that with Swift Package Manger?

On macOS, I ran

find /Applications/Xcode.app -name 'string.h' | xargs fgrep memset_s

and saw that the macOS, iOS, tvOS, and watchOS SDKs each has the following line in their string.h header file:

memset_s(void *__s, rsize_t __smax, int __c, rsize_t __n) __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0);

So, I'm not sure about Linux, but I guess I'll do the following for now:

let package = Package(
  name: "SignalProtocol",
  products: [.library(name: "SignalProtocol", targets: ["SignalProtocol"])],
  targets: [
    .target(
      name: "SignalProtocol",
      // ...
      cSettings: [.define("HAVE_MEMSET_S")]),
    // ...
  ]
)

You can use build setting conditions to set this per-platform, but I don't think there's a good way to determine this automatically.

Yeah, I saw that. OK. Thanks, @NeoNacho. I was also wondering if it's possible to define a macro if the system is big endian, but I guess not. Oh well.

I don't think there is. The main conceptual issue with a query like this is while the manifest can run arbitrary Swift code, it will be run on the host, so any sort of queries like that would give you answers about macOS/Linux, not necessarily the target platform you are building for.

Wow, I didn't know the manifest could run arbitrary Swift code. I initially figured it could, but then I got a parse error or something when I tried it. But I just tried it again, and it worked! That's so awesome! Thanks! :-) Oh yeah, good point. Hmm... maybe the check_symbol_exists and test_big_endian commands in libsignal-protocol-c's CMakeLists.txt file have the same issue. By the way, I just realized that I've seen you talking about the Swift Package Manager from Apple's WWDC videos. Great job! I thought you seemed familiar. Thanks for your contributions. :-) And thanks so much for answering my questions. SwiftPM is really cool!

1 Like