CMake can I filter compiler output through another

2019-05-21 00:55发布

问题:

Can I tell CMake to pipe all stderr output from the compiler and/or linker through an external program and showing the output of that on the terminal instead?

回答1:

For Makefile generators you can apply a filter to the compiler output by setting a custom launcher for compile rules. The following CMake list file sketches the necessary steps:

cmake_minimum_required(VERSION 2.8)

project (Main)

configure_file(
    "${PROJECT_SOURCE_DIR}/gcc_filter.sh.in"
    "${PROJECT_BINARY_DIR}/gcc_filter.sh"
    @ONLY)

add_executable (Main Main.cpp)

set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${PROJECT_BINARY_DIR}/gcc_filter.sh")

In the project root directory add the following shell script template file gcc_filter.sh.in:

#!/bin/sh

# shell script invoked with the following arguments
# $(CXX) $(CXX_DEFINES) $(CXX_FLAGS) -o OBJECT_FILE -c SOURCE_FILE

# invoke compiler
exec "$@" 2>&1 | "@PROJECT_SOURCE_DIR@/myfilter.sh"

The actual shell script invoked by the custom launcher rule is first copied to the project binary directory with the configure_file call in the CMake list file. The executable bit of the file gcc_filter.sh.in must be set. The shell script forwards all arguments to the compiler and then pipes the compiler output to another shell script myfilter.sh in the project root directory:

#!/bin/sh
exec wc

The example myfilter.sh just invokes wc but more elaborate output filtering can be easily done using the above recipe.



标签: cmake