Skip to content

WIP: add support for C++ exceptions/RTTI

Bastien Schatt requested to merge bschatt/cmake:CXX_EXCEPTIONS_RTTI into master

This is an attempt to handle this long overdue feature. The goal is to be able to disable C++ exceptions and/or RTTI. This is especially important for MSVC because the flags are currently hardcoded in the default CMAKE_CXX_FLAGS (see #19084). It means you have code like that :

string(REPLACE "/GR" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # Disable RTTI
string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # Disable Exceptions

I added two target properties : <lang>_EXCEPTIONS and <lang>_RTTI (both ON by default). They are initialized with the values of CMAKE_<lang>_EXCEPTIONS and CMAKE_<lang>_RTTI respectively.

The compile flags are configured in the platform files :

  • CMAKE_<lang>_COMPILE_OPTIONS_EXCEPTIONS and CMAKE_<lang>_COMPILE_OPTIONS_NO_EXCEPTIONS,
  • CMAKE_<lang>_COMPILE_OPTIONS_RTTI and CMAKE_<lang>_COMPILE_OPTIONS_NO_RTTI.

Currently, the Android platform adds exceptions/RTTI flags depending on the chosen STL implementation. I'm not sure how to handle this exactly. Maybe add CMAKE_<lang>_EXCEPTIONS_DEFAULT / CMAKE_<lang>_RTTI_DEFAULT variables? Also I believe the Intel compiler was relying on the flag set in the Windows-MSVC file. I will have to handle this properly (can't test right now).

Tested :

  • GCC (Unix Makefile generator)
  • Clang (Unix Makefile generator)
  • MSVC (Visual Studio 2019 generator)
  • ClangCL (Visual Studio 2019 generator with builtin clang)
  • Android platform (STL)
  • Intel compiler (flags)
  • Other platforms/compilers

Issue: #17991

Edited by Brad King

Merge request reports