Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake and portable nul / /dev/null device

In my Cmake script I need to redirect the standard output to the NUL / /dev/null device. I searched the CMake documentation for a portable solution but didn't find something.

I could do something like

if (WIN32)
  set(NULDEV NUL)
else()
  set(NULDEV /dev/null)
endif()

and use in the code ${NULDEV}, but I'd prefer a portable solution coming with CMake.

Edit usage form:

add_custom_target(docs
    COMMENT "Generating documentation."
    COMMAND ${CMAKE_COMMAND} -E chdir ${PROJECT_BINARY_DIR} "${THE_PROGRAM}" arguments > nul
)

Is this possible?

like image 367
albert Avatar asked Oct 21 '25 06:10

albert


1 Answers

If you are running a shell command using execute_process() and want to quite the output. you can use the OUTPUT_QUIET and/or ERROR_QUIET options.

From the execute_process documentation:

OUTPUT_QUIET, ERROR_QUIET
    The standard output or standard error results will be quietly ignored.

Example 1:

execute_process(COMMAND "${THE_PROGRAM}" argument OUTPUT_QUIET)

If you are using add_custom_target(), then it is unfortunately not that straight forward. What you could do is:

Example 2:

  1. Create a wrapper cmake script for executing your program:

    # generate_docs.cmake
    execute_process(COMMAND "${THE_PROGRAM}" argument OUTPUT_QUIET)
    
  2. Let CMake execute the wrapper script instead of running the program directly:

    add_custom_target(docs
        COMMENT "Generating documentation."
        COMMAND ${CMAKE_COMMAND} -P generate_docs.cmake
    )
    
like image 173
sergej Avatar answered Oct 25 '25 00:10

sergej