Commit c688b401 authored by Marc Chevrier's avatar Marc Chevrier Committed by Brad King

cmstd: Modernize CMake system headers

Provide a standardized way to handle the C++ "standard" headers
customized to be used with current CMake C++ standard constraints.
Offer under directory `cm` headers which can be used as direct
replacements of the standard ones.  For example:

    #include <cm/string_view>

can be used safely for CMake development in place of the `<string_view>`
standard header.

Fixes: #19491
parent 9c31d83a
......@@ -368,11 +368,24 @@ macro (CMAKE_BUILD_UTILITIES)
# Setup third-party libraries.
# Everything in the tree should be able to include files from the
# Utilities directory.
if (CMAKE_SYSTEM_NAME STREQUAL "AIX" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# using -isystem option generate error "template with C linkage"
include_directories("${CMake_SOURCE_DIR}/Utilities/std")
else()
include_directories(SYSTEM "${CMake_SOURCE_DIR}/Utilities/std")
endif()
include_directories(
${CMake_BINARY_DIR}/Utilities
${CMake_SOURCE_DIR}/Utilities
)
#---------------------------------------------------------------------
# Build CMake std library for CMake and CTest.
set(CMAKE_STD_LIBRARY cmstd)
add_subdirectory(Utilities/std)
CMAKE_SET_TARGET_FOLDER(cmstd "Utilities/std")
# check for the use of system libraries versus builtin ones
# (a macro defined in this file)
CMAKE_HANDLE_SYSTEM_LIBRARIES()
......
......@@ -439,9 +439,6 @@ set(SRCS
cmXMLWriter.h
cmake.cxx
cmake.h
cm_string_view.cxx
cm_string_view.hxx
cm_static_string_view.hxx
cmCommand.cxx
cmCommand.h
......@@ -683,13 +680,13 @@ set(SRCS
cmWriteFileCommand.cxx
cmWriteFileCommand.h
cm_static_string_view.hxx
cm_get_date.h
cm_get_date.c
cm_utf8.h
cm_utf8.c
cm_codecvt.hxx
cm_codecvt.cxx
cm_thread.hxx
cmDuration.h
cmDuration.cxx
......@@ -855,6 +852,7 @@ endforeach()
# create a library used by the command line and the GUI
add_library(CMakeLib ${SRCS})
target_link_libraries(CMakeLib cmsys
${CMAKE_STD_LIBRARY}
${CMAKE_EXPAT_LIBRARIES} ${CMAKE_ZLIB_LIBRARIES}
${CMAKE_TAR_LIBRARIES}
${CMAKE_CURL_LIBRARIES}
......
......@@ -11,8 +11,9 @@
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmUuid.h"
#include "cm_string_view.hxx"
#include <algorithm>
#include <cm/string_view>
#include "cmWIXDirectoriesSourceWriter.h"
#include "cmWIXFeaturesSourceWriter.h"
......
......@@ -9,10 +9,10 @@
#include "cmSystemTools.h"
#include "cmUuid.h"
#include "cm_sys_stat.h"
#include "cmCMakeToWixPath.h"
#include "cm_sys_stat.h"
cmWIXFilesSourceWriter::cmWIXFilesSourceWriter(cmCPackLog* logger,
std::string const& filename,
GuidType componentGuidType)
......
......@@ -10,9 +10,11 @@
#include "cmGeneratedFileStream.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cm_sys_stat.h"
#include "cmsys/Glob.hxx"
#include "cm_sys_stat.h"
#include <cstring>
#include <map>
#include <ostream>
......
......@@ -17,7 +17,7 @@
#include <utility>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
int cmCPackExternalGenerator::InitializeInternal()
{
......
......@@ -4,6 +4,7 @@
#define cmCPackExternalGenerator_h
#include "cmCPackGenerator.h"
#include "cm_sys_stat.h"
#include <memory>
......
......@@ -10,9 +10,10 @@
#include <string>
#include <vector>
#include "cm_sys_stat.h"
#include "cmCPackComponentGroup.h"
#include "cmSystemTools.h"
#include "cm_sys_stat.h"
class cmCPackLog;
class cmGlobalGenerator;
......
......@@ -4,13 +4,14 @@
#include <sstream>
#include "cm_sys_stat.h"
#include "cmCPackGenerator.h"
#include "cmCPackLog.h"
#include "cmDuration.h"
#include "cmGeneratedFileStream.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cm_sys_stat.h"
cmCPackOSXX11Generator::cmCPackOSXX11Generator() = default;
......
......@@ -8,11 +8,12 @@
#include <string>
#include <vector>
#include "cm_sys_stat.h"
#include "cmArchiveWrite.h"
#include "cmCPackGenerator.h"
#include "cmCPackLog.h"
#include "cmSystemTools.h"
#include "cm_sys_stat.h"
cmCPackSTGZGenerator::cmCPackSTGZGenerator()
: cmCPackArchiveGenerator(cmArchiveWrite::CompressGZip, "paxr", ".sh")
......
......@@ -12,7 +12,7 @@
#include <utility>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
class cmCTestBuildHandler;
class cmCTestGenericHandler;
......
......@@ -7,10 +7,12 @@
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmXMLWriter.h"
#include "cm_string_view.hxx"
#include "cmsys/FStream.hxx"
#include "cmsys/RegularExpression.hxx"
#include <cm/string_view>
#include <utility>
cmCTestCVS::cmCTestCVS(cmCTest* ct, std::ostream& log)
......
......@@ -11,7 +11,7 @@
#include <string>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
class cmCTestGenericHandler;
......
......@@ -12,7 +12,7 @@
#include <string>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
class cmCTestGenericHandler;
......
......@@ -12,7 +12,7 @@
#include <utility>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
class cmExecutionStatus;
......
......@@ -7,7 +7,7 @@
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
#include "cmCTestTestCommand.h"
#include "cmCommand.h"
......
......@@ -12,7 +12,7 @@
#include <utility>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
class cmExecutionStatus;
......
......@@ -12,7 +12,7 @@
#include <utility>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
class cmExecutionStatus;
......
......@@ -20,7 +20,7 @@
#include <sstream>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
cmCTestRunTest::cmCTestRunTest(cmCTestMultiProcessHandler& multiHandler)
: MultiTestHandler(multiHandler)
......
......@@ -5,7 +5,7 @@
#include "cmsys/Directory.hxx"
#include "cmsys/Process.h"
#include "cm_memory.hxx"
#include <cm/memory>
#include "cmCTest.h"
#include "cmCTestBuildCommand.h"
......
......@@ -12,7 +12,7 @@
#include <utility>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
class cmExecutionStatus;
......
......@@ -13,7 +13,7 @@
#include <utility>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
class cmExecutionStatus;
......
......@@ -13,7 +13,7 @@
#include <sstream>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
class cmExecutionStatus;
......
......@@ -11,7 +11,7 @@
#include <string>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
class cmCTestGenericHandler;
......
......@@ -2,7 +2,7 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCTestTestHandler.h"
#include "cm_memory.hxx"
#include <cm/memory>
#include "cmAlgorithms.h"
#include "cmCTest.h"
......
......@@ -11,7 +11,7 @@
#include <string>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
class cmCTestGenericHandler;
......
......@@ -20,7 +20,7 @@
#include <chrono>
#include <sstream>
#include "cm_memory.hxx"
#include <cm/memory>
static const char* cmCTestUpdateHandlerUpdateStrings[] = {
"Unknown", "CVS", "SVN", "BZR", "GIT", "HG", "P4"
......
......@@ -12,7 +12,7 @@
#include <string>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
class cmCTestGenericHandler;
......
......@@ -238,81 +238,4 @@ typename Range::const_iterator cmFindNot(Range const& r, T const& t)
return std::find_if(r.begin(), r.end(), [&t](T const& i) { return i != t; });
}
template <class Iter>
std::reverse_iterator<Iter> cmMakeReverseIterator(Iter it)
{
return std::reverse_iterator<Iter>(it);
}
namespace cm {
#if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
using std::size;
#else
// std::size backport from C++17.
template <class C>
# if !defined(_MSC_VER) || _MSC_VER >= 1900
constexpr
# endif
auto
size(C const& c) -> decltype(c.size())
{
return c.size();
}
template <typename T, size_t N>
# if !defined(_MSC_VER) || _MSC_VER >= 1900
constexpr
# endif
std::size_t
size(const T (&)[N]) throw()
{
return N;
}
#endif
template <typename T>
int isize(const T& t)
{
return static_cast<int>(cm::size(t));
}
#if __cplusplus >= 201402L || defined(_MSVC_LANG) && _MSVC_LANG >= 201402L
using std::cbegin;
using std::cend;
#else
// std::c{begin,end} backport from C++14
template <class C>
# if defined(_MSC_VER) && _MSC_VER < 1900
auto cbegin(C const& c)
# else
constexpr auto cbegin(C const& c) noexcept(noexcept(std::begin(c)))
# endif
-> decltype(std::begin(c))
{
return std::begin(c);
}
template <class C>
# if defined(_MSC_VER) && _MSC_VER < 1900
auto cend(C const& c)
# else
constexpr auto cend(C const& c) noexcept(noexcept(std::end(c)))
# endif
-> decltype(std::end(c))
{
return std::end(c);
}
#endif
} // namespace cm
#endif
......@@ -6,7 +6,7 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include "cm_static_string_view.hxx"
#include "cm_string_view.hxx"
#include <cm/string_view>
#include <cassert>
#include <functional>
......
......@@ -15,7 +15,7 @@
#include <sstream>
#include "cm_memory.hxx"
#include <cm/memory>
static std::string ReplaceOrigin(const std::string& rpath,
const std::string& origin)
......
......@@ -12,7 +12,7 @@
#include <string>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
cmBinUtilsMacOSMachOLinker::cmBinUtilsMacOSMachOLinker(
cmRuntimeDependencyArchive* archive)
......
......@@ -12,7 +12,7 @@
#include <sstream>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
#ifdef _WIN32
# include <windows.h>
......
......@@ -29,7 +29,7 @@
# include <unistd.h> // IWYU pragma: keep
#endif
#include "cm_memory.hxx"
#include <cm/memory>
#include "cmAlgorithms.h"
#include "cmCTestBuildAndTestHandler.h"
......
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cm_memory.hxx"
#include <cm/memory>
#include "cmCommands.h"
#include "cmPolicies.h"
......
......@@ -23,7 +23,7 @@
#include <sstream>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
/*
......
......@@ -6,7 +6,7 @@
#include "cm_rhash.h"
#include "cmsys/FStream.hxx"
#include "cm_memory.hxx"
#include <cm/memory>
static unsigned int const cmCryptoHashAlgoToId[] = {
/* clang-format needs this comment to break after the opening brace */
......
......@@ -5,7 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include "cm_string_view.hxx"
#include <cm/string_view>
#include <cstddef>
#include <memory>
......
......@@ -9,7 +9,7 @@
#include <string>
#include <vector>
#include "cm_string_view.hxx" // IWYU pragma: keep
#include <cm/string_view> // IWYU pragma: keep
/** Data structure to represent a single command line. */
class cmCustomCommandLine : public std::vector<std::string>
......
......@@ -2,7 +2,7 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmDefinitions.h"
#include "cm_string_view.hxx"
#include <cm/string_view>
#include <cassert>
#include <functional>
......
......@@ -5,11 +5,12 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include "cm_string_view.hxx"
#include "cmLinkedTree.h"
#include "cmString.hxx"
#include <cm/string_view>
#include <functional>
#include <string>
#include <unordered_map>
......
......@@ -5,8 +5,8 @@
#include "cmDependsJavaLexer.h"
#include "cmSystemTools.h"
#include "cm_string_view.hxx"
#include "cmsys/FStream.hxx"
#include <cm/string_view>
#include <cstdio>
#include <cstdlib>
#include <cstring>
......
......@@ -4,8 +4,8 @@
#include "cmAlgorithms.h"
#include "cm_kwiml.h"
#include "cm_memory.hxx"
#include "cmsys/FStream.hxx"
#include <cm/memory>
#include <cstddef>
#include <map>
#include <memory>
......
......@@ -8,7 +8,7 @@
#include <string>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
#include "cmCommand.h"
......
......@@ -24,7 +24,7 @@
#include <sstream>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
static std::string cmExportFileGeneratorEscape(std::string const& str)
{
......
......@@ -6,7 +6,7 @@
#include <map>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
#include "cmExecutionStatus.h"
#include "cmGeneratedFileStream.h"
......
......@@ -5,10 +5,10 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include "cm_sys_stat.h"
#include <string>
#include "cm_sys_stat.h"
namespace cmFSPermissions {
// Table of permissions flags.
......
......@@ -20,7 +20,9 @@
#include <utility>
#include <vector>
#include "cm_memory.hxx"
#include <cm/memory>
#include "cm_sys_stat.h"
#include "cmAlgorithms.h"
#include "cmArgumentParser.h"
......@@ -44,7 +46,6 @@
#include "cmSubcommandTable.h"
#include "cmSystemTools.h"
#include "cmTimestamp.h"
#include "cm_sys_stat.h"
#include "cmake.h"
#if !defined(CMAKE_BOOTSTRAP)
......
......@@ -6,9 +6,10 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include "cmFileTimeCache.h"
#include "cm_sys_stat.h"
#include "cmsys/RegularExpression.hxx"
#include "cm_sys_stat.h"
#include <string>
#include <vector>
......
......@@ -2,11 +2,11 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmFileTimes.h"
#include "cm_sys_stat.h"
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
#include "cm_sys_stat.h"
#if defined(_WIN32)
# include "cmSystemTools.h"
......
......@@ -18,7 +18,7 @@
#include <sstream>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
#include "cmAlgorithms.h"
#include "cmMakefile.h"
......
......@@ -6,9 +6,10 @@
#include <cstdlib>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
#include <cm/string_view>
#include "cm_static_string_view.hxx"
#include "cm_string_view.hxx"
#include "cmExecutionStatus.h"
#include "cmFunctionBlocker.h"
......
......@@ -7,7 +7,7 @@
#include <vector>
#include "cm_string_view.hxx"
#include <cm/string_view>
#include "cmListFileCache.h"
......
......@@ -5,9 +5,10 @@
#include <sstream>
#include <utility>
#include "cm_memory.hxx"
#include <cm/memory>
#include <cm/string_view>
#include "cm_static_string_view.hxx"
#include "cm_string_view.hxx"
#include "cmAlgorithms.h"
#include "cmExecutionStatus.h"
......