Silence custom command depending on CMAKE_VERBOSE_MAKEFILE

前端 未结 2 941
猫巷女王i
猫巷女王i 2021-01-21 15:21

I\'ve a custom command in my CMake script which generates a lot of output. I\'d like to take advantage of CMAKE_VERBOSE_MAKEFILE so I can decide if I want to see this output or

相关标签:
2条回答
  • 2021-01-21 15:45

    Technically speaking, CMAKE_VERBOSE_MAKEFILE exists for the purpose of hiding and showing command lines, not command output.

    If I had to do this, I would use a custom variable.

    But on the main topic, here is how you should do:

    if (COMMAND_VERBOSE)
      execute_process(COMMAND "mycustom_command")
    else (COMMAND_VERBOSE)
      execute_process(COMMAND "mycustom_command" OUTPUT_QUIET)
    endif (COMMAND_VERBOSE)
    

    This is the most portable way to do so.

    There is also an ERROR_QUIET flag, however it is a bad idea to disable error messages, else the user would be unable to see why the command failed if it failed.

    If you are using add_custom_command or add_custom_target instead, such a flag does not exist. You'll have to provide a manual redirection to /dev/null (Unix), or NUL (Windows).

    0 讨论(0)
  • 2021-01-21 16:01

    As SirDarius pointed out, execute_process() has an option to silence tool output, while add_custom_command() / add_custom_target() do not.

    But there is a way to work around this: By putting the actual call to your tool in a separate CMake script wrapper, using execute_process() with OUTPUT_QUIET enabled or disabled depending on a switch:

    # mycustom_command.cmake
    if ( OUTPUT )
        execute_process( COMMAND mycustom_command )
    else()
        execute_process( COMMAND mycustom_command OUTPUT_QUIET )
    endif()
    

    Then, use add_custom_command() and the CMake script processing mode (-P) to call that script from your main CMakeLists.txt, with the script switch enabled / disabled by whatever variable you use for that purpose in your CMakeLists.txt file.

    add_custom_command( OUTPUT outfile
                        COMMAND ${CMAKE_COMMAND} -P mycustom_command.cmake -DOUTPUT=${OUTPUT_DESIRED}
                      )
    

    This is fully portable. If your mycustom_command is build from within your project as a target, just add it as DEPENDENCY to your add_custom_command() to have it build in time for the script call.

    0 讨论(0)
提交回复
热议问题