output of cmake command to a file

You might be able to do this by writing a custom compiler wrapper and setting it via CMAKE_<LANG>_COMPILER_LAUNCHER. This would give you full control over each compiler invocation.