Here is a sample project:
cmake_minimum_required(VERSION 3.30)
project(myLib)
add_library(${PROJECT_NAME} SHARED myLib.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION "1.2.3.4" SOVERSION "1")
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND dsymutil "$<TARGET_FILE:${PROJECT_NAME}>" -o "$<TARGET_FILE:${PROJECT_NAME}>.dSYM"
COMMENT "Extracting dSYM for ${PROJECT_NAME}"
VERBATIM
)
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND strip -x "$<TARGET_FILE:${PROJECT_NAME}>"
COMMENT "Stripping symbols from ${PROJECT_NAME}"
VERBATIM
)
When I use “Unix Makefiles” or “XCode” generators, I don’t have any issue. But when I use “Ninja” generator the project falls apart: the extracted dSYM files are broken if I run a make command twice.
What happens here is that ninja thinks it needs to rebuild the internal “Create symbolic links” target every time. Running with explain
debug option of ninja gives the reason:
$ ninja -d explain
ninja explain: recorded mtime of libmyLib.1.dylib older than most recent input libmyLib.1.2.3.4.dylib (1735912151630693962 vs 1735912151702593130)
ninja explain: libmyLib.dylib is dirty
[1/1] Creating library symlink libmyLib.1.dylib libmyLib.dylib
warning: no debug symbols in executable (-arch arm64)
It thinks (for a good reason) the symbolic links are older than the output (real) dylib file. Because the strip
command alters the output, it’s date is modified but not recorded by Ninja.
I tried to “touch” the symlinks but cmake -E touch
follows symlinks and touches the real file instead of the links. I then tried to use bash touch -h
command but it doesn’t record the new timestamp for the links in Ninja’s database.
This issue doesn’t occur for the other generators, only for Ninja (probably due to how the symlink creation is done with that generator).
Is there a way to solve that issue? I wanted to use BYPRODUCTS
but it doesn’t support target-specific generator expression. I guess we could create a cmake script file and run it post_build but it’s a bit of a pain (if no other easier solution, then I’ll do that).
Thanks