add_custom_command PRE_LINK is executed after linking when using the Xcode generator
With the following sample project
cmake_minimum_required(VERSION 3.16)
project(app LANGUAGES CXX)
set(main "${CMAKE_BINARY_DIR}/main.cpp")
file(GENERATE OUTPUT "${main}" CONTENT "int main() { return 0;}")
add_executable(app ${main})
add_custom_command(TARGET app
PRE_LINK
COMMAND ${CMAKE_COMMAND} -E echo "Hello world"
VERBATIM)
configure either with the legacy build system or the new build system
cmake .. -GXcode -T buildsystem=1
xcodebuild
# or
cmake .. -GXcode -T buildsystem=12
xcodebuild
the custom command is always executed after linking is done, rather than before.
Ld /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/Debug/app normal (in target 'app' from project 'app')
cd /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -target x86_64-apple-macos10.15 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk -L/Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/Debug -F/Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/Debug -filelist /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/app.build/Debug/app.build/Objects-normal/x86_64/app.LinkFileList -Xlinker -object_path_lto -Xlinker /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/app.build/Debug/app.build/Objects-normal/x86_64/app_lto.o -Xlinker -no_deduplicate -Wl,-search_paths_first -Wl,-headerpad_max_install_names -Xlinker -no_adhoc_codesign -Xlinker -dependency_info -Xlinker /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/app.build/Debug/app.build/Objects-normal/x86_64/app_dependency_info.dat -o /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/Debug/app
WriteAuxiliaryFile /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/app.build/Debug/app.build/Script-4AA4D3A3C160EC5D4F079634.sh (in target 'app' from project 'app')
cd /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link
write-file /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/app.build/Debug/app.build/Script-4AA4D3A3C160EC5D4F079634.sh
PhaseScriptExecution CMake\ PreLink\ Rules /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/app.build/Debug/app.build/Script-4AA4D3A3C160EC5D4F079634.sh (in target 'app' from project 'app')
cd /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link
/bin/sh -c /Volumes/T3/Dev/projects/cmake/general/xcode_pre_link/build/app.build/Debug/app.build/Script-4AA4D3A3C160EC5D4F079634.sh
Hello world
Checking the source code, there doesn't seem to be anything special in how the commands are handled except for the generated order of the build phases https://gitlab.kitware.com/cmake/cmake/-/blob/v3.24.0-rc1/Source/cmGlobalXCodeGenerator.cxx#L1792
I don't know whether build phase order really controlled the order of commands at some point, but it doesn't seem to work anymore.
Environment
CMake 3.23
Xcode 12.4
Build version 12D4e
Edited by Brad King