CMake macOS Universal Binary build: How to link to a target for x86_64 only?

Probably more a general CMake question than a JUCE-specific one, but maybe relevant for others round here as well… I’m currently trying to build a macOS arm/x86_64 universal binary plugin with JUCE 6 for the first time. We use IPP a lot for our current plugins and now built some replacement solutions targeting arm.

So now, what I do is specifying

set(CMAKE_OSX_ARCHITECTURES arm64 x86_64) at the top of the CMakeLists and according to lipo -archs this generates a universal binary :tada: But I see a lot of linker warnings that the IPP libraries have only been linked to the x86_64 target – which is quite obvious. So now I’m wondering what’s the correct solution to only link to IPP for x86? I tried the following approach

add_library(ALL_IPP INTERFACE)

set(IPP_ROOT "/opt/intel/ipp")

target_include_directories(ALL_IPP INTERFACE "${IPP_ROOT}/include")

add_library(ippi    STATIC IMPORTED GLOBAL)
add_library(ipps    STATIC IMPORTED GLOBAL)
add_library(ippvm   STATIC IMPORTED GLOBAL)
add_library(ippcore STATIC IMPORTED GLOBAL)

set (IPP_LIB "${IPP_ROOT}/lib")

set_target_properties(ippi    PROPERTIES IMPORTED_LOCATION ${IPP_LIB}/libippi.a    CMAKE_OSX_ARCHITECTURES x86_64)
set_target_properties(ipps    PROPERTIES IMPORTED_LOCATION ${IPP_LIB}/libipps.a    CMAKE_OSX_ARCHITECTURES x86_64)
set_target_properties(ippvm   PROPERTIES IMPORTED_LOCATION ${IPP_LIB}/libippvm.a   CMAKE_OSX_ARCHITECTURES x86_64)
set_target_properties(ippcore PROPERTIES IMPORTED_LOCATION ${IPP_LIB}/libippcore.a CMAKE_OSX_ARCHITECTURES x86_64)

target_link_libraries(ALL_IPP INTERFACE ippi ipps ippvm ippcore)

# somewhere later
target_link_libraries(myPlugin PRIVATE ALL_IPP)

But I still get errors like

ld: warning: ignoring file /opt/intel/ipp/lib/libippi.a, building for macOS-arm64 but attempting to link with file built for unknown-x86_64

So that doesn’t seem to be the right approach. But what’s the right approach? :slight_smile:

Interested in this as well.

We had the issue trying to move an iOS project to cmake and didn’t found a way to setup different lib folder for each architecture like you can for debug/release

Alright, in the end I found a (stupid) workaround :wink: I created an empty dummy library project with arm64 set as target architecture only and join this with each of the ipp libraries needed by the lipo tool. Then I link to that libraries. Feels a bit hacky, but seems to work for now :smiley:

# Some library containing an empty function. Simple purpose is to have an empty arm64 lib to join with the real x86_64 libs
add_library(ippARMDummy ippARMDummy.c)
set_target_properties(ippARMDummy PROPERTIES OSX_ARCHITECTURES arm64)

set(IPP_ROOT "/opt/intel/ipp")
set(IPP_LIB "${IPP_ROOT}/lib")

        COMMAND lipo -create libippArmDummy.a ${IPP_LIB}/libippi.a -output libippiMultiarch.a
        DEPENDS ippARMDummy)

        COMMAND lipo -create libippArmDummy.a ${IPP_LIB}/libipps.a -output libippsMultiarch.a
        DEPENDS ippARMDummy)

        COMMAND lipo -create libippArmDummy.a ${IPP_LIB}/libippvm.a -output libippvmMultiarch.a
        DEPENDS ippARMDummy)

        COMMAND lipo -create libippArmDummy.a ${IPP_LIB}/libippcore.a -output libippcoreMultiarch.a
        DEPENDS ippARMDummy)

add_custom_target(ippMultiarch_All DEPENDS ippiMultiarch ippsMultiarch ippvmMultiarch ippcoreMultiarch)