CMAKE add_custom_command: How to use my raw comman

2019-06-23 08:30发布

问题:

I am trying to add some custom build commands to my vc2010 project by using ADD_CUSTOM_COMMAND. But:

[1] I just find that CMAKE will automatically insert much more code than I hvae expected.

For example, I want the command exactly to be:

"c:\Some Folder\MyTool.exe" -arg0 -arg1

The corresponding code in CMakeLists.txt is like:

add_custom_command( OUTPUT ${outfile}
                  COMMAND "c:\Some Folder\MyTool.exe" ARGS "-arg0 -arg1"
                  # COMMAND "\"c:\Some Folder\MyTool.exe\"" will FAIL to be parsed!
                  MAIN_DEPENDENCY ${infile}
                  VERBATIM)

but actually I got:

setlocal
c:\Some Folder\MyTool.exe "-arg0 -arg1"
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd

This is not what I want. I have also tried the string: COMMAND "\"${MYTOOL_PATH}\" -arg0 -arg1", but CMake just failed to parse it because there is a quote in front of the string. So it's bad I can't quote the command string because the path c:\Some Folder\MyTool.exe contains spaces.

[2] I also notice CMake will automatically expand my dependency pathes.

If I add to the command DEPENS "$(PATH_MACRO)", I will eventually get the dependency path automatically expanded by CMake: C:\MySolutionDir\$(PATH_MACRO); But I only want $(PATH_MACRO), because I will make sure visual studio understands what $(PATH_MACRO) refers to.

So here is my question:

How can CMake only accept my raw inputs, without auto expanding the path or inserting code I don't expect? I will make sure my inputs will be valid for visual studio. Thanks!

PS. My CMake version: 2.8.10.2. VS version: visual studio 2010 Ultimate

回答1:

In case this helps someone. I was trying to get binary signing to work as a post-build step using signtool.exe but ONLY for Release builds. Only the uncommented syntax works, none of the others do. I'm kinda at a loss as to why some of the others do not work, but at least I found one that does. There seems to be some strange rules about when literal quotes are generated and when they are not, depending on what is inside the quoted expression. Perhaps someone can explain.

function(my_sign_binary TARGET)
#SET(COMMAND_LINE signtool sign $<TARGET_FILE:${TARGET}>)
add_custom_command(
    TARGET ${TARGET} 
    POST_BUILD 
    COMMENT "Signing: ${TARGET}"
    COMMAND "$<$<CONFIG:Release>:signtool>" "$<$<CONFIG:Release>:sign>"  "$<$<CONFIG:Release>:$<TARGET_FILE:${TARGET}>>"

    #COMMAND $<$<CONFIG:Release>:signtool.exe sign $<TARGET_FILE:${TARGET}>>
    #COMMAND echo 1. signtool sign $<TARGET_FILE:${TARGET}>
    #COMMAND echo 2. "$<$<CONFIG:Release>:signtool sign $<TARGET_FILE:${TARGET}>>"
    #COMMAND "$<$<CONFIG:Release>:signtool sign $<TARGET_FILE:${TARGET}>>"
    #COMMAND "$<$<CONFIG:Release>:signtool>" "$<$<CONFIG:Release>:\ sign\ $<TARGET_FILE:${TARGET}>>"
    #COMMAND echo 3. $<$<CONFIG:Release>:"signtool sign $<TARGET_FILE:${TARGET}>">
    #COMMAND "echo 4. $<$<CONFIG:Release>:signtool sign $<TARGET_FILE:${TARGET}>>"
    #COMMAND echo 5. [$<$<CONFIG:Release>:signtool sign $<TARGET_FILE:${TARGET}>>]
    #COMMAND $<$<CONFIG:Release>:signtool sign $<TARGET_FILE:${TARGET}>>
    #COMMAND $<$<CONFIG:Release>:${COMMAND_LINE}>
    #COMMAND $<$<CONFIG:Release>:COMMAND_LINE>
    #COMMAND $<$<CONFIG:Release>:"${COMMAND_LINE}">
    #COMMAND $<CONFIG:Release>:signtool sign $<TARGET_FILE:${TARGET}>
    #COMMAND echo 8. "${$<$<CONFIG:Release>:COMMAND_LINE>}"
    #COMMAND ${"$<$<CONFIG:Release>:COMMAND_LINE>"}
)
endfunction()


回答2:

For issue 1, try "c:\\Some Folder\\MyTool.exe" -arg0 -arg1. The ARGS parameter isn't required, but the point is to omit the quotes around the -arg0 -arg1.

For issue 2, since $(PATH_MACRO) is not an absolute path, CMake sees it as a path relative to your source directory. The best way to resolve this is probably to use "generator expressions", but without further info on your exact case, I can't provide a better answer.

For info on generator expressions, see the docs for add_custom_command.



标签: cmake