forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use macros to determine compiler version. (googleapis/google-cl…
…oud-cpp-spanner#298) * feat: Use macros to determine compiler version. Fixes googleapis/google-cloud-cpp-spanner#294 and closes googleapis/google-cloud-cpp-spanner#281 The logic for determining the compiler ID and version are lifted from the CMake v3.5 sources (the minimum we support). Only GCC, Clang and MSVC are supported but it is trivial to support more. This breaks the logic out into a separate file, although it is possible I misunderstood the requirements and I'm happy to correct it. cc: @devjgm * Address review comments. * Address review comments. * Fix typo. * Fix another typo. * Address review comment.
- Loading branch information
Showing
10 changed files
with
237 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/spanner/internal/build_info.h" | ||
#include "google/cloud/internal/port_platform.h" | ||
#include <algorithm> | ||
#include <cctype> | ||
#include <iterator> | ||
#include <sstream> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace spanner { | ||
inline namespace SPANNER_CLIENT_NS { | ||
namespace internal { | ||
|
||
/** | ||
* The macros for determining the compiler ID are taken from: | ||
* https://gitlab.kitware.com/cmake/cmake/tree/v3.5.0/Modules/Compiler/\*-DetermineCompiler.cmake | ||
* We do not care to detect every single compiler possible and only target the | ||
* most popular ones. | ||
* | ||
* Order is significant as some compilers can define the same macros. | ||
*/ | ||
std::string CompilerId() { | ||
#if defined(__apple_build_version__) && defined(__clang__) | ||
return "AppleClang"; | ||
#elif defined(__clang__) | ||
return "Clang"; | ||
#elif defined(__GNUC__) | ||
return "GNU"; | ||
#elif defined(_MSC_VER) | ||
return "MSVC"; | ||
#endif | ||
|
||
return "Unknown"; | ||
} | ||
|
||
std::string CompilerVersion() { | ||
std::ostringstream os; | ||
|
||
#if defined(__apple_build_version__) && defined(__clang__) | ||
os << __clang_major__ << "." << __clang_minor__ << "." << __clang_patchlevel__ | ||
<< "." << __apple_build_version__; | ||
return std::move(os).str(); | ||
#elif defined(__clang__) | ||
os << __clang_major__ << "." << __clang_minor__ << "." | ||
<< __clang_patchlevel__; | ||
return std::move(os).str(); | ||
#elif defined(__GNUC__) | ||
os << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__; | ||
return std::move(os).str(); | ||
#elif defined(_MSC_VER) | ||
os << _MSC_VER / 100 << "."; | ||
os << _MSC_VER % 100; | ||
#if defined(_MSC_FULL_VER) | ||
#if _MSC_VER >= 1400 | ||
os << "." << _MSC_FULL_VER % 100000; | ||
#else | ||
os << "." << _MSC_FULL_VER % 10000; | ||
#endif | ||
#endif | ||
return std::move(os).str(); | ||
#endif | ||
|
||
return "Unknown"; | ||
} | ||
|
||
std::string CompilerFeatures() { | ||
#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS | ||
return "ex"; | ||
#else | ||
return "noex"; | ||
#endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS | ||
} | ||
|
||
std::string LanguageVersion() { | ||
switch (__cplusplus) { | ||
case 199711L: | ||
return "98"; | ||
case 201103L: | ||
return "2011"; | ||
case 201402L: | ||
return "2014"; | ||
case 201703L: | ||
return "2017"; | ||
default: | ||
return "unknown"; | ||
} | ||
} | ||
|
||
} // namespace internal | ||
} // namespace SPANNER_CLIENT_NS | ||
} // namespace spanner | ||
} // namespace cloud | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef GOOGLE_CLOUD_CPP_SPANNER_GOOGLE_CLOUD_SPANNER_INTERNAL_COMPILER_INFO_H_ | ||
#define GOOGLE_CLOUD_CPP_SPANNER_GOOGLE_CLOUD_SPANNER_INTERNAL_COMPILER_INFO_H_ | ||
|
||
#include "google/cloud/spanner/version.h" | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace spanner { | ||
inline namespace SPANNER_CLIENT_NS { | ||
namespace internal { | ||
|
||
/** | ||
* Returns the compiler ID. | ||
* | ||
* The Compiler ID is a string like "GNU" or "Clang", as described by | ||
* https://cmake.org/cmake/help/v3.5/variable/CMAKE_LANG_COMPILER_ID.html | ||
*/ | ||
std::string CompilerId(); | ||
|
||
/** | ||
* Returns the compiler version. | ||
* | ||
* This string will be something like "9.1.1". | ||
*/ | ||
std::string CompilerVersion(); | ||
|
||
/** | ||
* Returns certain interesting compiler features. | ||
* | ||
* Currently this returns one of "ex" or "noex" to indicate whether or not | ||
* C++ exceptions are enabled. | ||
*/ | ||
std::string CompilerFeatures(); | ||
|
||
/** | ||
* Returns the 4-digit year of the C++ language standard. | ||
*/ | ||
std::string LanguageVersion(); | ||
|
||
} // namespace internal | ||
} // namespace SPANNER_CLIENT_NS | ||
} // namespace spanner | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_SPANNER_GOOGLE_CLOUD_SPANNER_INTERNAL_COMPILER_INFO_H_ |
Oops, something went wrong.