Are there any compiler independent flags that can be set? I'd like to be able to set single variable to e.g. `OPTIMIZE_MOST` and get `-O3` on gcc and `/O2` in MS C++ compiler. Is there something I can use or should flags be set for each compiler separately?
['https://Stackoverflow.com/questions/55740242', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3826362/']
Simply spoken: No, there is no flag to directly set the optimization level independently for every compiler. 

However, CMake provides so called [build types](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html). Those are independent of the compiler in use and each comes with a predefined selection of flags, one of which is the optimization flag.

Available build types are

* `Debug`
* `Release`
* `RelWithDebInfo`
* `MinSizeRel`

For a comprehensive explanation, I refer to [this answer](https://stackoverflow.com/a/24767451/579698). It also provides some code that helps to identify the flags in question when included into the `CMakeLists.txt` file:

```
message("CMAKE_C_FLAGS_DEBUG is ${CMAKE_C_FLAGS_DEBUG}")
message("CMAKE_C_FLAGS_RELEASE is ${CMAKE_C_FLAGS_RELEASE}")
message("CMAKE_C_FLAGS_RELWITHDEBINFO is ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
message("CMAKE_C_FLAGS_MINSIZEREL is ${CMAKE_C_FLAGS_MINSIZEREL}")

message("CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}")
message("CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}")
message("CMAKE_CXX_FLAGS_RELWITHDEBINFO is ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
message("CMAKE_CXX_FLAGS_MINSIZEREL is ${CMAKE_CXX_FLAGS_MINSIZEREL}")

```
To a degree. For some concepts, CMake has support for specifying them in a compiler-agnostic manner, usually by setting properties on the target in question. Unfortunately, there is no one location where all such possibilities would be listed. I went through the current [list of target properties](https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets) and identified the following properties as "absrtacting away build-tool option syntax":

* `COMPILE_PDB_NAME`
* `INCLUDE_DIRECTORIES`
* `INSTALL_RPATH`
* `INTERPROCEDURAL_OPTIMIZATION`
* `LINK_DIRECTORIES`
* `LINK_LIBRARIES`
* `PDB_NAME`
* `PDB_OUTPUT_DIRECTORY`
* (plus the properties for setting output name, path, etc.)

There is apparently nothing for handling optimisation flags other than IPO.

To the best of my knowledge, there is also no generic process for adding these — they are added to CMake as someone finds the need for them (and the time to implement them).