0

Does exist a variable which contains the compiler flags used in some call to CMake's ADD_LIBRARY function, for example the ones used when we add a module:

ADD_LIBRARY(mylib MODULE mysrc.cpp)

Or, is there a way of getting such flags?

2501
  • 25,460
  • 4
  • 47
  • 87
Aleph
  • 1,343
  • 1
  • 12
  • 27
  • The problem is that the CMake generator will finally put together the compiler flags (for the complete formula see my answer [here](https://stackoverflow.com/questions/33828855/is-cmake-set-variable-recursive/33834879#33834879])). So you don't have all the flags during configuration step. What exactly do you want to do with the flags? An option would e.g. be [`CMAKE_EXPORT_COMPILE_COMMANDS`](https://stackoverflow.com/questions/20059670/how-to-use-cmake-export-compile-commands). – Florian Mar 09 '17 at 14:04
  • Thank you Florian! I would like CMake to write a wrapper to ease the compilation of user modules, i.e. write a simple script which could contain something like this on Linux with gcc for example: 'g++ -shared -fPIC -O3'. For portability reasons, I would like to get flags such as '-shared -fPIC' from CMake instead of handling each particular case (system, compiler) – Aleph Mar 09 '17 at 14:32
  • Could you explain what "to write a wrapper to ease the compilation of user modules" actually would do? Something like [this](http://stackoverflow.com/questions/37787796/why-does-cmake-ignore-exported-cxx-and-cc-environment-variables)? – Florian Mar 09 '17 at 14:49

1 Answers1

0

Turning my comments into an answer

There is not a single CMake variable to get the all compiler flags. The problem is that the CMake generator will finally put together the compiler flags (from various CMake variables and properties incl. from depending targets). So you don't have all the flags during configuration step.

I see the following possible problem/solution pairs:

  • CMake is a cross-platform wrapper around your compiler (that's actually what the C stands for), so no need to extract the compiler flags into an external script
  • If you just want to add sort of a filter to what is called by CMake you can user set "launcher" variables/properties accordingly e.g. CMAKE_CXX_COMPILER_LAUNCHER or RULE_LAUNCH_LINK
  • If you want the compiler calls in a machine readable JSON format you could export those by setting CMAKE_EXPORT_COMPILE_COMMANDS
  • If you just want to see the compiler calls incl. all the flags you could set CMAKE_VERBOSE_MAKEFILE
  • If you really just need the compiler flags on the output and you don't want CMake to actually compile anything, you could - at least for CMake's Makefile generators - modify CMAKE_CXX_COMPILE_OBJECT and CMAKE_CXX_CREATE_SHARED_MODULE like this:

    set(CMAKE_DEPFILE_FLAGS_CXX "")
    set(
        CMAKE_CXX_COMPILE_OBJECT 
        "<CMAKE_COMMAND> -E echo <FLAGS>"
    )
    set(
        CMAKE_CXX_CREATE_SHARED_MODULE 
        "<CMAKE_COMMAND> -E echo <CMAKE_SHARED_MODULE_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS>"
    )
    
    file(WRITE mysrc.cpp "")
    add_library(mylib MODULE mysrc.cpp)
    

References

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • Hi Florian! Actually, I finally discovered that CMAKE_CXX_CREATE_SHARED_MODULE contains exactly what I was looking for. Furthermore, I does not seem to be true "there is no CMake variable to get compiler flags". Actually, there exists a lot of variables to get compiler and linker flags. Finally, it is not generally a good idea to modify variables as you do in your example. One good approach is to use SET_TARGET_PROPERTY to modify flags locally for a particular target, say "mylib": SET_TARGET_PROPERTIES(mylib LINK_FLAGS "-undefined dynamic_lookup") – Aleph Mar 13 '17 at 09:04
  • @Aleph You are right, my reasoning was not sound. I've changed my answer accordingly. But those variables I used are not available as properties, so if I want to change those global settings there is no alternative. And yes, the script example is to be taken with caution, because it modifies the compiler/linker calls to do nothing, just echos the flags to `stdout`. – Florian Mar 13 '17 at 09:12
  • Thank you for updating your answer. However, I am not sure that saying there is no alternative to the "SET solution" is true. For compiler flags, a better alternative would be calling ADD_DEFINITIONS or ADD_COMPILE_DEFINITIONS for example. You may also replace any call to SET_TARGET_PROPERTIES by a call to SET_PROPERTY with the GLOBAL option enabled. Furthermore, my initial question deals with a way of getting flags, not how to modify them. My goal is to make CMake write a script, say 'mycpp', which wraps the compilation command because I want the user to be able to compile its own modules – Aleph Mar 13 '17 at 12:53
  • For example, 'mycpp' may be a bash script containing a command line with the name of the compiler (CMAKE_CXX_COMPILER) and the flags suitables for modules (CMAKE_CXX_CREATE_SHARED_MODULE), say 'g++ -shared -bundle $1'. So, the answer to my question is just simple as "the variable you are looking for is CMAKE_CXX_CREATE_SHARED_MODULE". Nonetheless, you answer is interesting and informative as well. – Aleph Mar 13 '17 at 12:56
  • @Aleph I admit I still don't understand what are trying to do too. Why would I like to extract some of the flags from CMake to rebuild what CMake is made for - "cross platform compiling" - in a script? Please add your solution as an answer. I think it would clarify your approach. – Florian Mar 13 '17 at 19:48