add_custom_command: Xcode always runs POST_BUILD commands
Adding a simple POST_BUILD command to a target has different behavior on different generators. On Make and Ninja POST_BUILD is only invoked if the target was re-built. On Xcode, POST_BUILD is invoked even if the target has not changed, it suffices if it was considered to be built.
See the attached simple project. It attaches an "ls -al" command to a simple library target. Below is the output of generating and building twice with xcodebuild and make, and you can see that in the former ls is triggered twice whilst in the latter - once.
This inconsistency makes it difficult to write portable CMake code.CMakeLists.txt
post_build_test/xcode.generator$ cmake -G Xcode ..
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ejdys/Downloads/post_build_test/xcode.generator
post_build_test/xcode.generator$ xcodebuild
=== BUILD AGGREGATE TARGET ZERO_CHECK OF PROJECT Project WITH THE DEFAULT CONFIGURATION (Debug) ===
Check dependencies
Write auxiliary files
write-file /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/ZERO_CHECK.build/Script-FA723C093BF247E7A24A2485.sh
chmod 0755 /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/ZERO_CHECK.build/Script-FA723C093BF247E7A24A2485.sh
PhaseScriptExecution CMake\ Rules xcode.generator/Project.build/Debug/ZERO_CHECK.build/Script-FA723C093BF247E7A24A2485.sh
cd /Users/ejdys/Downloads/post_build_test
/bin/sh -c /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/ZERO_CHECK.build/Script-FA723C093BF247E7A24A2485.sh
echo ""
make -f /Users/ejdys/Downloads/post_build_test/xcode.generator/CMakeScripts/ReRunCMake.make
make[1]: `/Users/ejdys/Downloads/post_build_test/xcode.generator/CMakeFiles/cmake.check_cache' is up to date.
=== BUILD TARGET TestLib OF PROJECT Project WITH THE DEFAULT CONFIGURATION (Debug) ===
Check dependencies
Write auxiliary files
write-file /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Script-940BE169A07341F7B385AA89.sh
chmod 0755 /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Script-940BE169A07341F7B385AA89.sh
/bin/mkdir -p /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Objects-normal/x86_64
write-file /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Objects-normal/x86_64/TestLib.LinkFileList
CompileC xcode.generator/Project.build/Debug/TestLib.build/Objects-normal/x86_64/test.o test.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/ejdys/Downloads/post_build_test
export LANG=en_US.US-ASCII
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=267 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -fcolor-diagnostics -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-float-conversion -Wno-non-literal-null-conversion -Wno-objc-literal-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR=\"Debug\" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.13 -g -Wno-sign-conversion -Wno-infinite-recursion -Wno-move -Wno-comma -Wno-block-capture-autoreleasing -Wno-strict-prototypes -Wno-range-loop-analysis -I/Users/ejdys/Downloads/post_build_test/xcode.generator/Debug/include -I/Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/DerivedSources/x86_64 -I/Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/DerivedSources -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -F/Users/ejdys/Downloads/post_build_test/xcode.generator/Debug -MMD -MT dependencies -MF /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Objects-normal/x86_64/test.d --serialize-diagnostics /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Objects-normal/x86_64/test.dia -c /Users/ejdys/Downloads/post_build_test/test.cpp -o /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Objects-normal/x86_64/test.o
Libtool xcode.generator/Debug/libTestLib.a normal x86_64
cd /Users/ejdys/Downloads/post_build_test
export MACOSX_DEPLOYMENT_TARGET=10.13
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static -arch_only x86_64 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -L/Users/ejdys/Downloads/post_build_test/xcode.generator/Debug -filelist /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Objects-normal/x86_64/TestLib.LinkFileList -o /Users/ejdys/Downloads/post_build_test/xcode.generator/Debug/libTestLib.a
PhaseScriptExecution CMake\ PostBuild\ Rules xcode.generator/Project.build/Debug/TestLib.build/Script-940BE169A07341F7B385AA89.sh
cd /Users/ejdys/Downloads/post_build_test
/bin/sh -c /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Script-940BE169A07341F7B385AA89.sh
ls -al
total 40
drwxr-xr-x@ 9 ejdys staff 288 Nov 2 15:30 .
drwxr-xr-x 6 ejdys staff 192 Nov 2 15:25 ..
-rw-r--r-- 1 ejdys staff 13016 Nov 2 15:30 CMakeCache.txt
drwxr-xr-x 10 ejdys staff 320 Nov 2 15:30 CMakeFiles
drwxr-xr-x 16 ejdys staff 512 Nov 2 15:30 CMakeScripts
drwxr-xr-x 3 ejdys staff 96 Nov 2 15:30 Debug
drwxr-xr-x@ 3 ejdys staff 96 Nov 2 15:30 Project.build
drwxr-xr-x 4 ejdys staff 128 Nov 2 15:30 Project.xcodeproj
-rw-r--r-- 1 ejdys staff 1406 Nov 2 15:30 cmake_install.cmake
=== BUILD AGGREGATE TARGET ALL_BUILD OF PROJECT Project WITH THE DEFAULT CONFIGURATION (Debug) ===
Check dependencies
Write auxiliary files
write-file /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/ALL_BUILD.build/Script-DC9F4D5503994BE69A6151FA.sh
chmod 0755 /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/ALL_BUILD.build/Script-DC9F4D5503994BE69A6151FA.sh
PhaseScriptExecution CMake\ Rules xcode.generator/Project.build/Debug/ALL_BUILD.build/Script-DC9F4D5503994BE69A6151FA.sh
cd /Users/ejdys/Downloads/post_build_test
/bin/sh -c /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/ALL_BUILD.build/Script-DC9F4D5503994BE69A6151FA.sh
echo ""
echo Build\ all\ projects
Build all projects
** BUILD SUCCEEDED **
post_build_test/xcode.generator$ xcodebuild
=== BUILD AGGREGATE TARGET ZERO_CHECK OF PROJECT Project WITH THE DEFAULT CONFIGURATION (Debug) ===
Check dependencies
PhaseScriptExecution CMake\ Rules xcode.generator/Project.build/Debug/ZERO_CHECK.build/Script-FA723C093BF247E7A24A2485.sh
cd /Users/ejdys/Downloads/post_build_test
/bin/sh -c /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/ZERO_CHECK.build/Script-FA723C093BF247E7A24A2485.sh
echo ""
make -f /Users/ejdys/Downloads/post_build_test/xcode.generator/CMakeScripts/ReRunCMake.make
make[1]: `/Users/ejdys/Downloads/post_build_test/xcode.generator/CMakeFiles/cmake.check_cache' is up to date.
=== BUILD TARGET TestLib OF PROJECT Project WITH THE DEFAULT CONFIGURATION (Debug) ===
Check dependencies
PhaseScriptExecution CMake\ PostBuild\ Rules xcode.generator/Project.build/Debug/TestLib.build/Script-940BE169A07341F7B385AA89.sh
cd /Users/ejdys/Downloads/post_build_test
/bin/sh -c /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/TestLib.build/Script-940BE169A07341F7B385AA89.sh
ls -al
total 40
drwxr-xr-x@ 9 ejdys staff 288 Nov 2 15:30 .
drwxr-xr-x 6 ejdys staff 192 Nov 2 15:25 ..
-rw-r--r-- 1 ejdys staff 13016 Nov 2 15:30 CMakeCache.txt
drwxr-xr-x 10 ejdys staff 320 Nov 2 15:30 CMakeFiles
drwxr-xr-x 16 ejdys staff 512 Nov 2 15:30 CMakeScripts
drwxr-xr-x 3 ejdys staff 96 Nov 2 15:30 Debug
drwxr-xr-x@ 3 ejdys staff 96 Nov 2 15:30 Project.build
drwxr-xr-x 4 ejdys staff 128 Nov 2 15:30 Project.xcodeproj
-rw-r--r-- 1 ejdys staff 1406 Nov 2 15:30 cmake_install.cmake
=== BUILD AGGREGATE TARGET ALL_BUILD OF PROJECT Project WITH THE DEFAULT CONFIGURATION (Debug) ===
Check dependencies
PhaseScriptExecution CMake\ Rules xcode.generator/Project.build/Debug/ALL_BUILD.build/Script-DC9F4D5503994BE69A6151FA.sh
cd /Users/ejdys/Downloads/post_build_test
/bin/sh -c /Users/ejdys/Downloads/post_build_test/xcode.generator/Project.build/Debug/ALL_BUILD.build/Script-DC9F4D5503994BE69A6151FA.sh
echo ""
echo Build\ all\ projects
Build all projects
** BUILD SUCCEEDED **
post_build_test/xcode.generator$ cd ../make.generator/
post_build_test/make.generator$ cmake ..
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ejdys/Downloads/post_build_test/make.generator
post_build_test/make.generator$ make
Scanning dependencies of target TestLib
[ 50%] Building CXX object CMakeFiles/TestLib.dir/test.cpp.o
[100%] Linking CXX static library libTestLib.a
total 64
drwxr-xr-x 7 ejdys staff 224 Nov 2 15:31 .
drwxr-xr-x 6 ejdys staff 192 Nov 2 15:25 ..
-rw-r--r-- 1 ejdys staff 13576 Nov 2 15:31 CMakeCache.txt
drwxr-xr-x 16 ejdys staff 512 Nov 2 15:31 CMakeFiles
-rw-r--r-- 1 ejdys staff 4940 Nov 2 15:31 Makefile
-rw-r--r-- 1 ejdys staff 1398 Nov 2 15:31 cmake_install.cmake
-rw-r--r-- 1 ejdys staff 2352 Nov 2 15:31 libTestLib.a
[100%] Built target TestLib
post_build_test/make.generator$ make
[100%] Built target TestLib