Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize cmake output

For example, when compiling .c files, I want cmake to just print

CC    xxx.c

to stdout, like linux kbuild.

like image 302
Kang Jianbin Avatar asked Jan 18 '23 04:01

Kang Jianbin


2 Answers

CMake output can be customized by suppressing the standard messages produced by CMake and outputting a custom message in a launcher script instead. Add the following code to your outermost CMakeLists.txt:

set_property(GLOBAL PROPERTY RULE_MESSAGES OFF)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_SOURCE_DIR}/custom_output.sh")

Setting the global RULE_MESSAGES property to OFF will suppress the standard messages. The global RULE_LAUNCH_COMPILE property is set to a custom launch script named custom_output.sh which needs to be added to the root of the CMake source tree:

#!/bin/sh

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

# extract parameters
SOURCE_FILE="${@: -1:1}"
OBJECT_FILE="${@: -3:1}"

echo "CC $(basename \"$SOURCE_FILE\")"

# invoke compiler
exec "$@"

The script's executable bit needs to be set.

The linker output can be customized in the same fashion by also setting a RULE_LAUNCH_LINK script.

like image 161
sakra Avatar answered Jan 23 '23 03:01

sakra


This is not possible in the current version of CMake, without editing the source yourself. Of course sed and awk will help you.

like image 38
Daniel Blezek Avatar answered Jan 23 '23 03:01

Daniel Blezek