Compilation flag dependent modulemaps

I have a Swift package with a library product that has a C target to perform some number crunching. Depending on the OS and compilation flags, the library uses either Apple's Accelerate framework, Intel's Math Kernel Library, Integrated Performance Primitives and Threading Building Blocks or has its own implementation as a fallback.

Compiling this Package with Accelerate support is trivial. But compiling with MKL/IPP/TBB support requires an incredibly large amount of compilation flags, environment variables, etc. to be set. So I thought of using an LLVM modulemap file to make this process easier and use link statements in the modulemap file to achieve this. But by just using link statements in my modulemap, I lose platform independence. I can no longer choose, whether I want to link those libraries or not and if I try to compile on a system that doesn't have these libraries and headers, the compilation and linking will fail.

So I was wondering: Is there a way to use compilation flags (or even environment variables) as conditions in module maps? Can I for example use -Xcc -DENABLE_INTEL_LIBRARIES to conditionally link all the required libraries? Or is there another way to simplify this whole linking and compilation process?

Perhaps instead of conditions inside modulemaps you can have a modulemap per target platform?

1 Like

Thanks for the suggestion. Unfortunately I was not able to achieve the desired results using that method. As soon as I commanded clang to use a specific modulemap file using the -fmodule-map-file option, clang refused to compile giving an error that my module has been defined in two module map files

I think your best bet here is to define module maps for each implementation, and then have a second target that imports the right one for the current platform.

1 Like

That seems to work. Thank you.