MantisBT - CMake
View Issue Details
0014291CMakeCMakepublic2013-07-15 10:432013-12-02 08:51
Paul Smith 
Brad King 
normalmajoralways
closedfixed 
MacOSX10.8.4
CMake 2.8.11.2 
CMake 2.8.12CMake 2.8.12 
0014291: Xcode generator incorrectly adds a custom command to a like-named target
I have a set of rules I'm using to copy ("stage") some files after they're built.  I do not want to use install rules for this for various reasons.  The statements I'm using work great with the Makefile generator and the Visual Studio generator, but the build fails with the Xcode generator.

Xcode tries to run the "stage" rule before it builds the target to be staged, even though the "stage" rule has a DEPENDS statement on the target. The build fails because the target does not exist:

  $ xcodebuild build -configuration RelWithDebInfo
    ...
  === BUILD NATIVE TARGET myapp OF PROJECT TestApp WITH CONFIGURATION RelWithDebInfo ===
  Check dependencies

  PhaseScriptExecution "CMake Rules" TestApp.build/RelWithDebInfo/myapp.build/Script-2C3540A800314C1082C9261D.sh
      cd /Users/build/pds/xcode-test
      /bin/sh -c /Users/paul/xcode-test/TestApp.build/RelWithDebInfo/myapp.build/Script-2C3540A800314C1082C9261D.sh
  make: *** No rule to make target `/Users/paul/xcode-test/RelWithDebInfo/myapp', needed by `/Users/paul/xcode-test/dist/bin/myapp'.  Stop.
  Command /bin/sh failed with exit code 2


  ** BUILD FAILED **


  The following build commands failed:
          PhaseScriptExecution "CMake Rules" TestApp.build/RelWithDebInfo/myapp.build/Script-2C3540A800314C1082C9261D.sh
  (1 failure)
Create a simple C file:

cd ~
mkdir xcode-test
cd xcode-test

cat > myapp.c <<EOF
  #include <stdio.h>

  int main()
  {
      printf("Hello world!\n");
      return 0;
  }
EOF

Then create a simple CMake file:

cat > CMakeLists.txt <<EOF
  cmake_minimum_required(VERSION 2.8)
  project(TestApp C)
  add_executable(myapp myapp.c)

  set(MYPREFIX "${TestApp_SOURCE_DIR}/dist" CACHE PATH "install prefix" FORCE)

  add_custom_command(OUTPUT "${MYPREFIX}/bin/myapp"
      COMMAND "${CMAKE_COMMAND}" -E make_directory "${MYPREFIX}/bin"
      COMMAND "${CMAKE_COMMAND}" -E copy "$<TARGET_FILE:myapp>" "${MYPREFIX}/bin"
      DEPENDS myapp
      COMMENT "Staging myapp to ${MYPREFIX}/bin"
      VERBATIM)

  add_custom_target("stage_myapp" ALL
      DEPENDS "${MYPREFIX}/bin/myapp")
EOF

Now build an Xcode generator, then run xcodebuild:

  $ cmake -G Xcode .
  $ xcodebuild build -configuration RelWithDebInfo
If you use the 'Unix Makefile' generator instead, it will work fine (even with high parallelism). Also I've tried replacing the "DEPENDS myapp" with a full pathname to "myapp" and that fails as well, with the same error.

I've seen this same behavior on various different versions of Xcode: 4.1, 4.6, and 4.6.3 at least.
No tags attached.
Issue History
2013-07-15 10:43Paul SmithNew Issue
2013-07-15 11:06Brad KingNote Added: 0033539
2013-07-15 13:46Brad KingNote Added: 0033542
2013-07-15 13:46Brad KingAssigned To => Brad King
2013-07-15 13:46Brad KingStatusnew => resolved
2013-07-15 13:46Brad KingResolutionopen => fixed
2013-07-15 13:46Brad KingFixed in Version => CMake 2.8.12
2013-07-15 13:46Brad KingTarget Version => CMake 2.8.12
2013-07-15 13:46Brad KingSummaryadd_custom_command() DEPENDS does not work with the Xcode generator => Xcode generator incorrectly adds a custom command to a like-named target
2013-12-02 08:51Robert MaynardNote Added: 0034645
2013-12-02 08:51Robert MaynardStatusresolved => closed

Notes
(0033539)
Brad King   
2013-07-15 11:06   
I can reproduce this even after removing the second target:

-add_custom_target("stage_myapp" ALL
- DEPENDS "${MYPREFIX}/bin/myapp")

Somehow the custom command to copy myapp is ending up as one of the rules used to build myapp. If I change the name of the copied file to be "${MYPREFIX}/bin/myapp2" the problem goes away.
(0033542)
Brad King   
2013-07-15 13:46   
Fixed:

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=4bb6e248 [^]
(0034645)
Robert Maynard   
2013-12-02 08:51   
Closing resolved issues that have not been updated in more than 4 months.