How to list all CMake build options and their default values?
Asked Answered
T

3

103

How can I list cmake default build option in command-line?
I need to build OpenCV libraries from source. Before that, I want to know what are the default build settings.

Trailblazer answered 31/5, 2013 at 6:33 Comment(1)
Try the GUI (cmake-gui)?Hodeida
R
123

cmake -LAH

To list all option(...) and set(... CACHE ...) variables, including those marked as advanced, do:

cmake -LAH

where:

  • -L: list variables

  • -A: include advanced variables.

    CMake marks most but not all of its default pre-defined variables as, and you can mark you own variables as advanced with mark_as_advanced). Some default CMake advanced variables include basic default compilation parameters such as:

    CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++
    CMAKE_CXX_FLAGS:STRING=
    CMAKE_CXX_FLAGS_DEBUG:STRING=-g
    

    If you are only interested in the main configuration options of some project, you will likely want to remove the -A as it would be mostly noise.

  • -H: include the help strings as // My help above each setting. -H was previously mentioned at: https://mcmap.net/q/209812/-how-to-list-all-cmake-build-options-and-their-default-values consider upvoting that answer

Example

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(ProjectName)
set(GREETING "hello" CACHE STRING "How to greet")
set(GREETING2 "hello2" CACHE STRING "Supersecret greeting")
mark_as_advanced(FORCE VAR GREETING2)
set(MYNOCACHE "mynocacheval")
option(WORLD "Print world or not?" ON)

If we run:

mkdir -p build
cd build
cmake -LAH ..

the output contains:

// How to greet
GREETING:STRING=hello

// Supersecret greeting
GREETING2:STRING=hello2

// How to greet
MSG:STRING=hello

// Supersecret greeting
MSG2:STRING=hello2

// Print world or not?
WORLD:BOOL=ON

plus a bunch of default defined CMake variables. If we remove -A we get only:

// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=debug

// Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local

// How to greet
GREETING:STRING=hello

// How to greet
MSG:STRING=hello

// Print world or not?
WORLD:BOOL=ON

so we see that our advanced variable GREETING2 is not present. We also understand that CMake feels that two of their default variables are often useful to merit the non-advanced distinction:

  • CMAKE_BUILD_TYPE to control debug vs release: Debug vs Release in CMake
  • CMAKE_INSTALL_PREFIX to decide where to install the project output

Without -H we get just:

CMAKE_BUILD_TYPE:STRING=debug
CMAKE_INSTALL_PREFIX:PATH=/usr/local
GREETING:STRING=hello
MSG:STRING=hello
WORLD:BOOL=ON

Note that the values are the cached values, not the defaults. E.g. if we first set a cached value:

cmake -DGREETING=bye ..

then running again cmake -L .. gives:

CMAKE_BUILD_TYPE:STRING=debug
CMAKE_INSTALL_PREFIX:PATH=/usr/local
GREETING:STRING=bye
MSG:STRING=hello
WORLD:BOOL=ON

Better way to determine the exact build options: run a verbose build!

I think some of people coming to this question are trying to understand how CMake is building their things exactly.

While you could deduce much of that from cmake -LA, a better more direct and sure-fire way would be as per: Using CMake with GNU Make: How can I see the exact commands? to just build with with:

make VERBOSE=1

The output would then contain a line of type:

/usr/bin/cc -DGREETING=\"hello\" -DWORLD -MD -MT CMakeFiles/main.dir/main.c.o -MF CMakeFiles/main.dir/main.c.o.d -o CMakeFiles/main.dir/main.c.o -c /home/ciro/main.c

which makes it clear exactly what CMake is doing.

ccmake ncurses

ccmake is an nCurses Cmake UI. The advantage over cmake -L is that you can interactively set the values as you find them:

sudo apt-get install cmake-curses-gui
ccmake .

shows:

enter image description here

To see the advanced options from cmake-curses-gui you can just enter the t key as mentioned on the UI which toggles them on or off.

cmake-gui

This UI is a bit better than ccmake in terms of capabilities:

sudo apt install cmake-qt-gui
cmake-gui .

enter image description here

we have:

  • "Advanced" checkbox to show the advanced options
  • "Grouped" checkbox to group options. By default it produces two categories:
    • "CMAKE" for the "CMAKE" default options
    • "Ungrouped Entries" for your variables Option groups are apparently generated automatically based on common option name prefixes: Creating groups of CMake options

Tested in Ubuntu 22.10, cmake 3.5.2.

Ridgley answered 22/3, 2017 at 7:33 Comment(9)
OP asked for ALL options - for that one needs cmake -LA.Blouson
@PrzemekD thanks, mentioned it in the answer. Clients don't always know what is best for them :-)Ridgley
Well... those are the options relevant to the project. How can you list all POSSIBLE options?Winner
Sometimes it will not show all options listed in CMakeLists.txt, e.g.: OpenBLAS. You could run cmake -D BUILD_SHARED_LIBS=ON but nothing of proposed above will show it avaliability or status :(Wireworm
@Wireworm maybe it is because that is a general CMake option and not added specifically by OpenBLAS: cmake.org/cmake/help/v3.0/variable/BUILD_SHARED_LIBS.htmlRidgley
@CiroSantilli新疆改造中心法轮功六四事件 makes sense, but then how I could get values of standard options? Also, Will it work if I have cmake files in subdirs (~nested cmake)?Wireworm
@Wireworm I'm not sure, try it out and ask a new question and link to it if you don't find out :-)Ridgley
Try cmake -LAH with additional information due to the H flag.Restrictive
with cmake version 3.25.1, cmake -LAH outputs the following warning: "No source or binary directory provided. Both will be assumed to be the same as the current working directory, but note that this warning will become a fatal error in future CMake releases.". Therefore, specify the directory. E.g. while in the source root directory, use cmake -LAH . ... or while in a conventional build/ directory, use cmake -LAH ..Nitza
D
33

You can do cmake -LAH too. The H flag will provide you help for each option.

Dasha answered 31/10, 2018 at 2:11 Comment(1)
This only lists options as a by-product of doing many other things, for example if called like this it would clutter the root folder with extra files. Better use in form cd build && cmake -LAH ...Sinasinai
I
3

I do not know of an direct way to do it.

A way around this is to edit the main CMakeLists.txt and print at the end of the file the settings you are interested. The Variables where the most important cmake setting are stored are listed here:

I always print these variables at the end of my CMakeLists.txt to see the settings.

MESSAGE(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
MESSAGE(STATUS "Library Type: " ${LIB_TYPE})
MESSAGE(STATUS "Compiler flags:" ${CMAKE_CXX_COMPILE_FLAGS})
MESSAGE(STATUS "Compiler cxx debug flags:" ${CMAKE_CXX_FLAGS_DEBUG})
MESSAGE(STATUS "Compiler cxx release flags:" ${CMAKE_CXX_FLAGS_RELEASE})
MESSAGE(STATUS "Compiler cxx min size flags:" ${CMAKE_CXX_FLAGS_MINSIZEREL})
MESSAGE(STATUS "Compiler cxx flags:" ${CMAKE_CXX_FLAGS})
Immortal answered 31/5, 2013 at 8:55 Comment(1)
a more detail answer can be found here : #24767950Anglian

© 2022 - 2024 — McMap. All rights reserved.