2

My opject using cmake, some compile options is configured by set

set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DMY_MACRO_DEBUG")
...
set(CMAKE_CXX_FLAGS_RELEASE "-g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DMY_MACRO_RELEASE")
...

and some compile options is configured by add_definitions()

add_definitions(-DMY_MACRO_1)
add_definitions(-DMY_MACRO_2)
...

after finish the configure, I want print all compile options using message

I want something output like follow

Compile options in debug mode:   -g -O0 -Wall -DMY_MACRO_DEBUG   -DMY_MACRO_1 -DMY_MACRO_2
Compile options in release mode: -g -O3 -Wall -DMY_MACRO_RELEASE -DMY_MACRO_1 -DMY_MACRO_2

How can I implement that? Thanks for your time.

Appendix 1

I know using make VERBOSE=1 can show the information, but it can only show debug options using debug mode, show release options using release mode. It can not show all mode's options at same time. Furthermore, it also include too many -I /path1 -I /path2. It is not a comfortable way to read that.

I want the information be display after we type cmake ..

Appendix 2

If -DMY_MACRO_1 -DMY_MACRO_2 can be print by some cmake variable something like ${CMAKE_ALREADY_ADD_DEFINITIONS}.

message(${CMAKE_ALREADY_ADD_DEFINITIONS})

the problem can be easy settled. But I don know if there exist a varible like that.

Xu Hui
  • 1,213
  • 1
  • 11
  • 24

1 Answers1

1

Compile Commands

The best way I know of to do this is with the CMAKE_EXPORT_COMPILE_COMMANDS setting. When set to ON it will generate a compile_commands.json in your build directory that lists every file and it's compile command. Simply run it once for Debug and one for Release and then you can diff them to see the difference.

https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html

In your top level CMakeLists.txt:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Printing

Now based on your appendix 2... if you just want to print out the compile definitions for a given target you would do something like:

get_target_property(compile_defs app COMPILE_DEFINITIONS)
message(STATUS "App compile definitions are $compile_defs)

For all libraries/executable you can have them write their compile definitions to a file

function(stash_defs app
file(APPEND 
  ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}_compile_defs.txt 
  "${CMAKE_CURRENT_BINARY_DIR}/${app}: ${compile_defs}")
endfunction()

...
...

# For each binary...
stash_defs(my_binary)

Note with the above each time you configure cmake you will be appending to the file. You would need some additional CMake magic to make the compile_defs.txt file part of the build target so it gets cleaned/re-spun each configure call.

kreynolds
  • 426
  • 4
  • 15
  • Using the fisrt way is work. but it will also contain some `-I /include_path -L /lib_path`. It is not so clear to obtain compile options. but it is much better than `make VERBOSE=1` – Xu Hui Apr 28 '20 at 03:53
  • @XuHui I updated the second part of the answer for something that is not as pretty as `compile_commands.json` but it should help remove all the fluff and give you only what you want. – kreynolds Apr 28 '20 at 12:21