Is there a way to use a different Clang for C++ sources in a Swift package?

I have a package with some C++ sources that use OpenMP. As far as I know, the Clang bundled in macOS' toolchain does not support OpenMP. So, to compile the sources and link to libomp, I need to get a different Clang that supports it (e.g. from homebrew). The only things that I can do seem to be via Package.Target.CXXSetting, but it doesn't allow swapping in a different the Clang.

Is there a way for me to use a different Clang for the C++ sources than for the rest of the pakage? Or, are there some other ways to work around it?

You should build the package as part of CI and distribute the prebuilt library.

It is also pretty easy to make a better package manager script which only downloads the stuff you tell it (Like if you are using an Intel Mac, you don't need ARM64 simulator. And you probably don't need dSYMs for development, only for release) and then creates an xcframework from them.

1 Like

If you use the homebrew Swift, does it invoke the homebrew clang or the Xcode clang? If the latter, maybe you can reorder your PATH so the homebrew tools always come first.

1 Like

Thanks for this suggestion! I looked into first building a shared library using a different Clang, then maybe linking it to a dummy C++ source file to create a target. This does introduce at least 1 more step to the build process, but I guess the currently pitched package command plugin can simplify it?

While looking into it, I found a different approach of using homebrew's libomp and passing -Xpreprocessor -fopenmp to the compiler and -lomp to the linker. Because I'm a noob around C++, this feels easier to do, so I tried this first, and I can confirm that this works.

It looks like that the formula uses apple/llvm-project to build Swift, which from my understanding isn't different from how build-script does it(?). In any case, I will try it later and update this thread with what I find. I've been looking into what I can do with homebrew's clang and libomp, but forgot that it distributes swift too. Thanks for reminding me of it!

Terms of Service

Privacy Policy

Cookie Policy