Making Cmake Print Commands Before Executing

Making CMake print commands before executing

I am fairly sure this will work:

make VERBOSE=1

You should also be able to add this to your CMakeLists.txt to permanently set that:

set(CMAKE_VERBOSE_MAKEFILE on)

This is covered in the CMake FAQ.

Making make print commands before executing when NOT using CMake

By default, make does print every command before executing it. This printing can be suppressed by one of the following mechanisms:

  • on a case-by-case basis, by adding @ at the beginning of the command
  • globally, by adding the .SILENT built-in target.
  • somewhere along the make process, by invoking sub-make(s) with one of the flags -s, --silent or --quiet, as in $(MAKE) --silent -C someDir, for example. From that moment on, command echoing is suppressed in the sub-make.

If your makefile does not print the commands, then it is probably using one of these three mechanisms, and you have to actually inspect the makefile(s) to figure out which.

As a workaround to avoid these echo-suppressing mechanisms, you could re-define the shell to be used to use a debug mode, for example like make SHELL="/bin/bash -x" target. Other shells have similar options. With that approach, it is not make printing the commands, but the shell itself.

If you use the flag -n or --just-print, the echo-suppressing mechanisms will be ignored and you will always see all commands that make thinks should be executed -- but they are not actually executed, just printed. That might be a good way to figure out what you can actually expect to see.

The VERBOSE variable has no standard meaning for make, but only if your makefile interprets it.

How do you make CMake print *all* commands not just build commands?

To get the commands in execute_process printed, you can set where you want them printed individually, in each execute_process command using COMMAND_ECHO:

execute_process(
COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" --gtest_list_tests
WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
TIMEOUT ${_TEST_DISCOVERY_TIMEOUT}
OUTPUT_VARIABLE output
RESULT_VARIABLE result
COMMAND_ECHO STDOUT
)

or universally for all execute_process commands throughout your CMake project by setting this variable in your top-level CMake file:

set(CMAKE_EXECUTE_PROCESS_COMMAND_ECHO STDOUT)

These features are available in CMake versions 3.15 and above.


For what it's worth, you can also get the full printout of every command CMake runs (with expanded CMake variables) using the cmake command line option:

cmake --trace-expand ..

but this may be much more verbosity than you're looking for.

Using CMake with GNU Make: How can I see the exact commands?

When you run make, add VERBOSE=1 to see the full command output. For example:

cmake .
make VERBOSE=1

Or you can add -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON to the cmake command for permanent verbose command output from the generated Makefiles.

cmake -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON .
make

To reduce some possibly less-interesting output you might like to use the following options. The option CMAKE_RULE_MESSAGES=OFF removes lines like [ 33%] Building C object..., while --no-print-directory tells make to not print out the current directory filtering out lines like make[1]: Entering directory and make[1]: Leaving directory.

cmake -DCMAKE_RULE_MESSAGES:BOOL=OFF -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON .
make --no-print-directory

cmake, print compile/link commands

The verbose argument should do what you want.

Content copied (with format adjusted slightly) here for future reference:

CMake/Verbose output

CMake has a nice colored output which hides the commandline. This is pretty to look at in the long run but sometimes when you write your configurations you want to know if you got all the compiler flags right. There is two ways to disable the pretty output, well, it's essentialy the same but still two different ways.

The first way is to simply run make with the additional argument "VERBOSE=1". This will show each command being run for this session, which is the most useful way to see if the flags is correct:

make VERBOSE=1

The second way is to permanently disable the pretty output in your CMakeLists.txt by setting CMAKE_VERBOSE_MAKEFILE:

set( CMAKE_VERBOSE_MAKEFILE on )

Content is available under Attribution-ShareAlike 2.5 unless otherwise noted.

How to execute a command in cmake before generation?

Thanks to @Tsyvarev and the others. Here is the way I choose:

CMakeList.txt

cmake_minimum_required(VERSION 3.5)

project(datalinkmsg)

add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/DatalinkMsg.cpp
COMMAND make -C ${CMAKE_CURRENT_SOURCE_DIR}/msg -f Makefile)

add_custom_target( generate
ALL make -C ${CMAKE_CURRENT_SOURCE_DIR}/msg -f Makefile
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/DatalinkMsg.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/msg/msgGen.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/msg/*.msg )
...

this runs with the corresponding msg/Makefile

MSGDEFS = *.msg

TARGET = msgGen
SOURCE = msgGen.cpp

GENTARGETCPP = ../src/DatalinkMsg.cpp
GENTARGETH = ../include/DatalinkMsg.h

all: $(GENTARGETCPP) $(GENTARGETH) $(MSGDEFS) $(SOURCE)

$(GENTARGETCPP): $(MSGDEFS) $(SOURCE)
make gen

$(GENTARGETH): $(MSGDEFS) $(SOURCE)
make gen

gen: $(TARGET) $(MSGDEFS) $(SOURCE)
mkdir -p ../src/
mkdir -p ../include/
./$<

$(TARGET): msgGen.cpp
g++ $< -o $@ --std=c++11

clean:
rm -f $(TARGET) $(GENTARGETCPP) $(GENTARGETH)

See commands of make and all called make files thereafter

You cannot use an alias, because aliases only exist in interactive shells and are not present in non-interactive shells like the one make uses to invoke comments in recipes. Same with shell functions.

You also cannot set the SHELL variable to a command that uses arguments: the value of SHELL must be only a command name. You can, though, set the .SHELLFLAGS variable to extra options to pass to the shell. Don't forget you need the -c option in addition to whatever flags you add!

Something like .SHELLFLAGS=-xc

If your makefiles are all properly invoking their submakes then using make -n as the initial invocation should Just Work.

By "properly invoking" I mean, they should always use the $(MAKE) variable when invoking sub-makes and never use a raw make command.

If they do not invoke sub-make properly and you cannot fix that, then the best you can do is write a shell script called make that does what your aliases above do, then add that to your PATH so it's found first. Obviously inside the script you'll have to use a fully-qualified path to run the "real" make else you'll get infinite recursion :)

CMake post-build output command with print

Just use TARGET flow of add_custom_command:

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND arm-none-eabi-size --format=berkeley "rtos_clion.elf")

The command will be executed after executable has been (re)built.

how to make cmake not silent GNU make

Try cmake -DCMAKE_VERBOSE_MAKEFILE=ON, or make VERBOSE=1, or both.



Related Topics



Leave a reply



Submit