MantisBT - CMake
View Issue Details
0014370CMakeCMakepublic2013-08-21 13:042014-06-02 08:37
Robert Knight 
Peter Kuemmel 
normalminoralways
closedfixed 
Windows7
CMake 2.8.11.2 
CMake 3.0 
0014370: add_custom_command() commands run from incorrect directory with Ninja generator
In the following project, if using the Ninja generator on Windows the first post-build command runs in the directory that Ninja is run from, not the directory specified by WORKING_DIRECTORY. The second post-build command runs in the correct directory.

Both commands run in the correct directory when using the NMake Makefiles generator.

project(ninja-generator-test)

set(TEST_DIR ${CMAKE_CURRENT_BINARY_DIR}/foo)
file(MAKE_DIRECTORY ${TEST_DIR})

add_executable(test test.cpp)

# this file is created in the directory
# that ninja is run from
add_custom_command(TARGET test
  POST_BUILD
  COMMAND "${CMAKE_COMMAND}" -E touch foo.txt
  WORKING_DIRECTORY ${TEST_DIR}
)

# this file is created in the correct directory
add_custom_command(TARGET test
  POST_BUILD
  COMMAND "${CMAKE_COMMAND}" -E touch foo2.txt
  WORKING_DIRECTORY ${TEST_DIR}
)


The generated Ninja output in build.ninja for the test target is:

#############################################
# Link the executable test.exe

build test.exe: CXX_EXECUTABLE_LINKER CMakeFiles\test.dir\test.obj
  FLAGS = /DWIN32 /D_WINDOWS /W3 /GR /EHsc /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
  LINK_FLAGS = /machine:X86  /debug /INCREMENTAL /subsystem:console
  LINK_LIBRARIES = kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib 
uuid.lib comdlg32.lib advapi32.lib
  POST_BUILD = cmd.exe /c cd C:\Users\Win7-VM\mendeley\misc\ninja-test\build\foo && "C:\Program 
Files (x86)\CMake 2.8\bin\cmake.exe" -E touch foo.txt && cd C:\Users\Win7-VM\mendeley\misc\ninja-test\build\foo 
&& "C:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -E touch foo2.txt
  PRE_LINK = cd .
  TARGET_IMPLIB = test.lib
  TARGET_PDB = test.pdb


Here the first post-build command uses 'cmd.exe /c cd' to switch to the target dir which has no effect and the second uses plain 'cd' which does.
No tags attached.
zip workingdir.zip (679) 2013-11-25 05:19
https://public.kitware.com/Bug/file/4964/workingdir.zip
Issue History
2013-08-21 13:04Robert KnightNew Issue
2013-11-25 05:19Peter KuemmelAssigned To => Peter Kuemmel
2013-11-25 05:19Peter KuemmelStatusnew => assigned
2013-11-25 05:19Peter KuemmelFile Added: workingdir.zip
2013-11-25 06:12Peter KuemmelNote Added: 0034541
2013-11-25 06:12Peter KuemmelStatusassigned => resolved
2013-11-25 06:12Peter KuemmelFixed in Version => CMake 3.0
2013-11-25 06:12Peter KuemmelResolutionopen => fixed
2014-06-02 08:37Robert MaynardNote Added: 0036024
2014-06-02 08:37Robert MaynardStatusresolved => closed

Notes
(0034541)
Peter Kuemmel   
2013-11-25 06:12   
http://public.kitware.com/pipermail/cmake-commits/2013-November/017223.html [^]
(0036024)
Robert Maynard   
2014-06-02 08:37   
Closing resolved issues that have not been updated in more than 4 months.