Introduce <LANG>_STANDARD_LIBRARY
Motivation
With the diversification of standard libraries implementations for C and C++ it has become desirable to be able to detect and change the used standard library from CMake in an easy and portable manner.
Implementation
CMake will add additional compiler flags pertaining to the standard library if <LANG>_STANDARD_LIBRARY
differs from the detected default.
- If the default standard library is false/
"NOTFOUND"
setting<LANG>_STANDARD_LIBRARY
to non-false values is an error due to possible problems stemming from duplicate or conflicting options with the compiler's default standard library. - If the
<LANG>_STANDARD_LIBRARY_<LIBRARY>
is false or not set, setting<LANG>_STANDARD_LIBRARY
to<LIBRARY>
is an error. False values would be reported as "unsupported", unset as "unknown".
The handling of default standard libraries allows introducing this functionality without requiring a policy.
The false/"NOTFOUND"
value will allow adding support for new platforms and standard libraries without backwards compatibility concerns.
Compiler modules would set the following variables:
CMAKE_<LANG>_STANDARD_LIBRARY_<LIBRARY>
CMAKE_<LANG>_STANDARD_LIBRARY_<LIBRARY>_COMPILE_OPTIONS
CMAKE_<LANG>_STANDARD_LIBRARY_<LIBRARY>_LINK_OPTIONS
CMAKE_<LANG>_STANDARD_LIBRARY
Contains the value of the detected default standard library for the current language's compiler. May be modified by user.
Special values:
- false/
"NOTFOUND"
if the standard library is unknown. -
none
for no standard library.
CMAKE_<LANG>_STANDARD_LIBRARY_DEFAULT
Set to the detected default standard library. Stored in CMake<LANG>Compiler.cmake
. Read-only.
Used internally for detecting if the user changed CMAKE_<LANG>_STANDARD_LIBRARY
from the default.
CMAKE_<LANG>_STANDARD_LIBRARY_<LIBRARY>
Set to true if supported by the current compiler. False if not supported (e.g. compiler version too old). Unset if compiler module is unaware of such a standard library.
<LANG>_STANDARD_LIBRARY
Initialized by CMAKE_<LANG>_STANDARD_LIBRARY
.
CXX_STANDARD_LIBRARY
-
libstdc++
– GNU C++ standard library -
libc++
– LLVM C++ standard library -
msvc
– Microsoft C++ standard library
Custom standard libraries
Custom or exotic default standard libraries not known by CMake would be detected as false/"NOTFOUND"
and thus CMake won't mess with standard library options. The new mechanism won't introduce backwards incompatibility in such cases.
It would be possible for users to add support for custom standard libraries not known by CMake by setting <LANG>_STANDARD_LIBRARY_<LIBRARY>
, <LANG>_STANDARD_LIBRARY_<LIBRARY>_COMPILE_OPTIONS
, <LANG>_STANDARD_LIBRARY_<LIBRARY>_LINK_OPTIONS
, etc.
However, it seems advisable to avoid documenting and explicitly supporting custom standard libraries for a while to allow revising these variables in future versions to better support various combinations of compilers and standard libraries.
Steps
Initial:
-
Default C++ standard library detection of none
,libstdc++
,libc++
on Clang. -
Ability to choose between none
,libstdc++
,libc++
on Clang. -
GCC support.
Future:
-
Extend support to MSVC with none
,msvc
,libc++
. -
Extend support to C with at least none
,glibc
on GCC, MSVC and Clang. -
Custom standard library support once there's enough real-world experience with adding support for standard libraries on various compilers.