[CMake] can't pipe cl.exe in a custom command

Jesper Eskilson jesper at eskilson.se
Tue Nov 6 03:44:53 EST 2007


Brandon Van Every wrote:
> I'm trying to grab the cl.exe banner so I can determine the MSVC
> version number.  If cl.exe is in the path, then the following works at
> a Windows Command Prompt.  This gives a short banner with the VC
> version number and copyright.
> 
>   cl /? 2> banner.txt
> 
> But when I try to do it in a custom command, I can't get it to pipe to
> a file.  I see the output go by in the Visual Studio output window,
> with 1> prefixed in front of all the lines, and it is saved in
> BuildLog.htm, but cbanner.txt has 0 length no matter whether I use >
> 1> 2> as the pipe.  Does MSVC simply not have a notion of piping or
> something?  Do I have to do something MSVC-specific to capture the
> output or the error?
> 
> IF(MSVC)
> ADD_CUSTOM_COMMAND(
>   OUTPUT ${CMAKE_BINARY_DIR}/cbanner.txt
>   WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
>   COMMAND ${CMAKE_C_COMPILER} /? 2> cbanner.txt
> )
> ADD_CUSTOM_TARGET(cbanner ALL
>   DEPENDS ${CMAKE_BINARY_DIR}/cbanner.txt
> )
> ENDIF(MSVC)
> 

This is a "feature" in Visual Studio 8, which drove me nuts before I 
realized what was going on. The short story is that cl sends its output 
via a backchannel (a pipe, presumable) to the output window when run 
from inside Visual Studio, so redirection does not work (there is 
nothing to redirect). To get around this, you need to clear the 
environment variable VS_UNICODE_OUTPUT before invoking cl.exe.

The longer story (with some useful links):

http://thisisnotaprogrammersblog.blogspot.com/2007/05/redirecting-output-from-compilerlinker.html

-- 
/Jesper



More information about the CMake mailing list