Saving clang output to file when cmake is used with CLion

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Saving clang output to file when cmake is used with CLion

mike lojkovic
I have a script I'm using to figure out which headers should be in my
precompiled header. It requires the clang -H flag passed to work. I'm
able to get the output on the console without issue, but can't figure
out how to just pass clang's output to a file for my script to
analyze. redirecting the console output to a file with ">" won't work
since I'm compiling from CLion. Is there a way to do this from within
a CMakeLists.txt?  I'm thinking something like tee would work, but
can't find a variable to chain to tee.
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Saving clang output to file when cmake is used with CLion

Dan Liew
Hi,

On 13 April 2017 at 01:35, mike lojkovic <[hidden email]> wrote:
> I have a script I'm using to figure out which headers should be in my
> precompiled header. It requires the clang -H flag passed to work. I'm
> able to get the output on the console without issue, but can't figure
> out how to just pass clang's output to a file for my script to
> analyze. redirecting the console output to a file with ">" won't work
> since I'm compiling from CLion. Is there a way to do this from within
> a CMakeLists.txt?  I'm thinking something like tee would work, but
> can't find a variable to chain to tee.

I have two ideas for this

1. Write a wrapper script for clang and tell CMake to use that as the
compiler. Something like this would do it

```
#!/bin/bash
: ${STDOUT_DEST?"STDOUT_DEST is not set"}
: ${STDERR_DEST?"STDERR_DEST is not set"}
CLANG_BIN="$(which clang)"
{ ${CLANG_BIN} "$@" 2>&1 1>&3 3>&- | tee -i ${STDERR_DEST}; } 3>&1
1>&2 | tee -i ${STDOUT_DEST}
```

this wrapper script logs the standard output and standard error to
separate files (overwriting existing files). The file names are set by
environment variables. You'll probably need to modify this script to
suit your needs.

then configure cmake to use this as the compiler

```
CC=/path/to/clang_wrapper cmake /path/to/source_dir
```

If you have C++ code you'll need to write a similar wrapper for `clang++`.

2.  Use the compilation database [1] which will record all the clang
invocations and then write a script to read the JSON file
and re-run the commands with the `-H` flag added and capture the
output and do whatever you need to do with it.

[1] https://clang.llvm.org/docs/JSONCompilationDatabase.html


HTH,
Dan.
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Loading...