CMAKE_OSX_ARCHITECTURES and separate files for certain architectures?

Hi there!

I found myself needing to compile a library today with M1 support. Googling a little, I found CMAKE_OSX_ARCHITECTURES. This worked great for most of our code!

But then I came across our libjpeg_turbo dependency - which uses different files depending on the architecture. This is for SIMD support. We have a couple others like this as well.

I googled around quite a bit but couldn’t really find a solution for this. The best I found is to use ifdef guards to only compile in the correct arch and just include all the files. I would rather not do that if I don’t have to, but that would definitely work.

Does anyone know if I’m missing something here?

1 Like

Multiarch targeting is more compiler magic than CMake magic. It just so happens that the compiler knows how to run itself twice for codegen and put it in a single object file. If both passes cannot handle the content of a single file, it needs to be conditional in some way. If you don’t want to modify the files directly, you can instead make a wrapper source and add it to the list of source files:

#ifdef __x86_64__
#include "jpeg-x86_64-bits.c"
#elif defined(__aarch64__)
#include "jpeg-aarch64-bits.c"
// Nothing special
1 Like

I was afraid of that but fully expected that answer :slight_smile:

Thank you for confirming my suspicions!