<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span>Hello,</span></div><div><span>I am facing a weird scenario when using the ADD_CUSTOM_TARGET / ADD_CUSTOM_COMMAND in building an executable for my project. I have scoured the internet for a solution but to no avail. Let me illustrate my case:</span></div><div><br><span></span></div><div><span>I need to do a pre-build operation by checking the presence of an already built executable. If it does, then the process would be something like this</span></div><div><br><span></span></div><div><span>SET(MY_EXE ${PROJECT_SOURCE_DIR}/PreBuild/aExecutable.exe)</span></div><div><br><span></span></div><div><span>IF(EXISTS ${MY_EXE})</span></div><div><span>...EXECUTE_PROCESS(COMMAND MY_EXE &lt;ARGS&gt;)&nbsp; #this one works as expected</span></div><div><span>ELSE()</span></div><div><span>...#need to build the executable and then run
 it as a post-build operation</span></div><div><span>ENDIF()</span></div><div><br><span></span></div><div><span>Now, I have tried both modes to build the executable and I have failed... Here is what I have done:</span></div><div><br><span></span></div><div><span>###using ADD_CUSTOM_COMMAND<br></span></div><div><span>&nbsp; ADD_EXECUTABLE(</span><span>aExecutable ${MY_SRCS})</span></div><div><span>&nbsp; TARGET_LINK_LIBRARIES(aExecutable ${MY_LIBS})</span></div><div><span>&nbsp; GET_TARGET_PROPERTY(EXE_NAME </span><span>aExecutable</span><span> LOCATION)</span></div><div>&nbsp; #copy the just built executable to the correct location - Works!!!</div><div>&nbsp; #run the executable as&nbsp; post-build operation -- Fails!!!<br></div><div><span class="tab">&nbsp; </span><span>ADD_CUSTOM_COMMAND(</span></div><div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="tab">&nbsp;&nbsp;&nbsp;
 </span><span>POST_BUILD<br></span></div><div><span>&nbsp;</span><span class="tab">&nbsp;&nbsp;&nbsp; </span><span class="tab">&nbsp;&nbsp;&nbsp;</span><span class="tab">&nbsp;&nbsp;&nbsp;</span><span class="tab"> </span><span>OUTPUT </span><span> ${PROJECT_SOURCE_DIR}/PreBuild</span><br><span>&nbsp;</span><span class="tab">&nbsp;&nbsp;&nbsp; </span><span class="tab">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span>COMMAND ${EXE_NAME} ${PROJECT_SOURCE_DIR}/InputFile.txt </span><span></span><span> ${PROJECT_SOURCE_DIR}/PreBuild</span></div><div><span></span><span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DEPENDS aExecutable<br>&nbsp;)<br></span></div><div>&nbsp;<br><span>###using ADD_CUSTOM_COMMAND<br>#This also Fails!!!!<br></span><span>ADD_CUSTOM_TARGET(</span>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="tab">&nbsp;&nbsp;&nbsp; </span><span>dummy ALL<br>
  </span></div>
<div><span>&nbsp;</span><span class="tab">&nbsp;&nbsp;&nbsp; </span><span class="tab">&nbsp;&nbsp;&nbsp;</span><span class="tab">&nbsp;&nbsp;&nbsp;</span><span class="tab">&nbsp;</span><span></span><span class="tab"></span><span>COMMAND ${EXE_NAME} ${PROJECT_SOURCE_DIR}/InputFile.txt </span><span></span><span> ${PROJECT_SOURCE_DIR}/PreBuild</span></div>
<div><span></span><span> )</span></div><br><span>###using ADD_CUSTOM_COMMAND, but with dependencies<br>
#This partially works!!!!<br>
</span><span>ADD_CUSTOM_TARGET(</span>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="tab">&nbsp;&nbsp;&nbsp; </span><span>dummy ALL<br>
  </span></div>

<div><span>&nbsp;</span><span class="tab">&nbsp;&nbsp;&nbsp; </span><span class="tab">&nbsp;&nbsp;&nbsp;</span><span class="tab">&nbsp;&nbsp;&nbsp;</span><span class="tab">&nbsp;</span><span></span><span class="tab"></span><span>COMMAND ${EXE_NAME} ${PROJECT_SOURCE_DIR}/InputFile.txt </span><span></span><span> ${PROJECT_SOURCE_DIR}/PreBuild</span></div>

<div><span></span><span> )</span></div>
<br>ADD_DEPENDENCIES(dummy aExecutable)<br><br><br>So amongst all the three combinations, the last combination seems to work in that I can see the required behavior of the executable. However, in VS2010 I get an error message as "....exists with cmd.exe"; Apparently it is trying to build the dummy project "dummy". <br><br>What am I doing work here? Conceptually, I think ADD_CUSTOM_COMMAND would be the correct approach for doing this.<br><br>Thanks in advance for any help.<br></div><div>Sumit</div></div></body></html>