Search code examples
c++cmakeinclude

cmake3 - how to process CMakeLists.txt include commands and view result


How can a CMakeLists.txt file be viewed with include commands executed, and included code in place?

Example: Imagine a CMakeLists.txt that has multiple includes, like so:

include(version)
include(options)
include(programs)
include(settings)
include(stuff)
// etc..

Is it possible to generate and view the assembled result after all the includes in CMakeLists.txt are processed? Ty :^)


Solution

  • The simplest way is to use --trace or --trace-expand flags. Both of them make cmake print every line it executes into stderr. For --trace it prints lines as they are read1, for --trace-expand all variables get expanded first.

    I usually end up with call redirecting stderr to cmake.log file:

    cmake . --trace-expand 2> cmake.log
    

    If the output is too long2 for your needs, you may try to reduce it with --trace-source=<file>. Extract from --help: Trace only this CMake file/module. Multiple options allowed.

    1 Quotation marks "" are sometimes lost.

    2 It is really long... First pass over almost empty project yields 14k+ lines of log. Successive calls only 500+. There is a lot of stuff happening in CMake guts.