2

Running into issues with a CMake POST_BUILD command. Whenever I try to build my CMake project (libFDSequencer) I error out with a pseudo-random error code (anything between 123 and 9009). I have narrowed down the root of the problem to the fact that CMake apparently generates extra batch code surrounding aa add_custom_command() call. I discovered this by building my project using Visual Studio, going to my project properties, and checking out the POST_BUILD commands.

My CMake code to run the custom command looks like this...

# The first line is the original command I want to run (which also work when  
# I just copy paste it into the POST_BUILD commands in project properties).

MESSAGE("${CMAKE_CURRENT_SOURCE_DIR}/mexTesting.cmd ${CMAKE_CURRENT_SOURCE_DIR}/src/fd/util/interfaces ${CMAKE_CURRENT_SOURCE_DIR}/mex ${CMAKE_CURRENT_SOURCE_DIR}/build/RelWithDebInfo/ ${CMAKE_CURRENT_SOURCE_DIR}/include " \"-lFDSequencer\ -lFDCommon\ -lSetupAPI\ -lwbemuuid\" " ")
add_custom_command(
    TARGET FDSequencer
    POST_BUILD
    COMMAND "echo Hello World"
)

MESSAGE("\n-----DONE------\n")

The version with CMake generated code in my POST_BUILD project properties secction looks like this...

setlocal
"echo Hello World"
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd

However, when I just run echo Hello World like this (how I assumed it was run by add_custom_command()) (edited in the POST_BUILD command field in VS project properties)

"echo Hello World"

or 

echo Hello World

It works just fine when I remove the extra code. I'm wondering if there is any way to disable this code generation from CMake or if there is a file that contains this generated code that I can overwrite somehow? Does anyone know why this is generated (besides unnecessary error handling)?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Ian
  • 675
  • 1
  • 9
  • 25
  • Where's the VCEnd label? – jwdonahue Feb 22 '18 at 21:30
  • It appears to be passing the quotes through to the script and that is breaking it. Try it without the quotes around `echo Hello World` command. – jwdonahue Feb 22 '18 at 21:32
  • Take the [tour], read [Ask] and [MCVE]. You apparently haven't posted the real code. Post that code. Actually, copy and paste that code into a cmd file and run it from the console window, what happens? I suspect you are not drilling down into the failure to actually understand what is going on. It is unlikely that the "extra code" is the root of the problem here. – jwdonahue Feb 22 '18 at 22:00

1 Answers1

0

See the CMAKE documentation for the add_custom_command() function. You're using quotes where there should be none. It should be:

add_custom_command(
    TARGET FDSequencer
    POST_BUILD
    COMMAND echo Hello World
)

If the actual command you are trying to run is returning a value that the CMAKE script doesn't like, you can always do something like the following:

add_custom_command(
    TARGET FDSequencer
    POST_BUILD
    COMMAND Whatever_Your_MCVE_Should_Have_Been arg1 arg2 etc & exit /b 0
)
jwdonahue
  • 6,199
  • 2
  • 21
  • 43
  • Is there any command line directive to have the command be run without the added code? I tried it without the quotes and it seemed to work ok, but the full command that I want to use starts up a matlab command window, which is being suppressed by the extra code there. I don't want an end user to have to manually edit the properties POST_BUILD box in VS. – Ian Feb 22 '18 at 21:47
  • I hope not. Your build system should check the return values from all commands. How exactly do the error checks interfere with your matlab call? Hmm... is that an interactive window you are popping up in your build? – jwdonahue Feb 22 '18 at 21:51