We have a project with a CMake file. It builds some open-source libraries our project needs, and copies shared object files for some other closed-source libraries out of known locations. This currently works on Windows.
I have a branch where I am trying to add support for a particular shared library on Mac. I am seeing a weird CMake problem where the TARGET_SONAME_FILE property for the shared library target mysteriously drops the filename and sets itself to the directory only. The build, following the incorrect TARGET_SONAME_FILE, then turns out wrong.
If you want to test this yourself, check out this repository, branch mac-spatializer-test-2, commit 189bfe8, git submodule update --init, download Steam Audio aka Phonon, and run (paths adjusted for your own system)
mkdir build
(cd build && cmake -DLOVR_ENABLE_PHYSICS=NO -DCMAKE_BUILD_TYPE=Debug -DLOVR_USE_STEAM_AUDIO=1 -DLOVR_STEAM_AUDIO_PATH=/Users/mcc/Downloads/spatializers/steamaudio_api ..)
(cd build && cmake --build .)
...but that's a little complicated to build so I'll just describe the problem.
Early in our CMake file, if LOVR_USE_STEAM_AUDIO and APPLE are both set, we run:
add_library(Phonon SHARED IMPORTED)
message("Phonon IMPORTED_LOCATION: ${LOVR_STEAM_AUDIO_PATH}/lib/OSX/libphonon.dylib")
set_target_properties(Phonon PROPERTIES IMPORTED_LOCATION "${LOVR_STEAM_AUDIO_PATH}/lib/OSX/libphonon.dylib")
set(LOVR_PHONON Phonon)
Which is supposed to create a target and store an IMPORTED_LOCATION for it. Then later in the file, we run:
function(move_lib)
if(TARGET ${ARGV0})
add_custom_command(TARGET lovr POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "TARGET ${ARGV0} COPY $<TARGET_SONAME_FILE:${ARGV0}>"
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_SONAME_FILE:${ARGV0}>
${EXE_DIR}/$<TARGET_SONAME_FILE_NAME:${ARGV0}>
)
endif()
endfunction()
move_lib(${LOVR_PHONON})
This is supposed to extract the previously set IMPORTED_LOCATION, and use that path to copy the file into a designated spot.
When we execute the CMake file, this prints out:
Phonon IMPORTED_LOCATION: /Users/mcc/Downloads/spatializers/steamaudio_api/lib/OSX/libphonon.dylib
...
TARGET Phonon COPY /Users/mcc/Downloads/spatializers/steamaudio_api/lib/OSX/
Somehow, the libphonon.dylib has been dropped. It tries to copy the directory, which is wrong, and COPY refuses to do it anyway, so this is a noop. The program then fails to find libphonon.dylib at runtime.
Essentially identical CMakeLists code works properly on Windows, and the one Linux user we have hasn't reported any problems. As far as I know it's failing on Mac. We have several other targets where we are doing something similar and the copy-TARGET_SONAME_FILE technique is working. One difference between SteamAudio/Phonon and the targets that are working correctly is the those are add_library-- Phonon's target is just used as a container to store the SHARED IMPORTED path, because we don't directly dynamically lnk Phonon, with that library we load at runtime using dlopen or similar.
One thing that I have already tried, which did not help, is adding add_dependencies(lovr ${LOVR_PHONON}). I am running CMake 3.20.0.
What am I doing wrong?