CMake can filter the compiler output through another program

May I report that CMake passes all stderr output from the compiler and / or linker through an external program and shows the output of that in the terminal?

+2


source to share


1 answer


For Makefile generators, you can filter the compiler output by installing a custom launcher for compilation rules. The following CMake list file will outline 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 your project root, add the following 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 launch rule is first copied into the project's binary directory with a call configure_file

in the CMake list file. The executable bit of the file must be set gcc_filter.sh.in

. The shell script passes all the arguments to the compiler and then dumps the compiler output to another shell script myfilter.sh

in the root of the project:

#!/bin/sh
exec wc

      

The example is myfilter.sh

simple to call wc

, but more complex filtering of the output can be easily accomplished using the above recipe.

+3


source







All Articles