r281666 - Simplify Clang's version number configuration in CMake.

David L. Jones via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 15 15:12:27 PDT 2016


Author: dlj
Date: Thu Sep 15 17:12:26 2016
New Revision: 281666

URL: http://llvm.org/viewvc/llvm-project?rev=281666&view=rev
Log:
Simplify Clang's version number configuration in CMake.

Currently, the Clang version is computed as follows:

 1. LLVM defines major, minor, and patch versions, all statically set. Today,
    these are 4, 0, and 0, respectively.
 2. The static version numbers are combined into PACKAGE_VERSION along with a
    suffix, so the result today looks like "4.0.0svn".
 3. Clang extracts CLANG_VERSION from PACKAGE_VERSION using a regexp. The regexp
    allows the patch level to omitted, and drops any non-digit trailing values.
    Today, this result looks like "4.0.0".
 4. CLANG_VERSION is then split further into CLANG_VERSION_MAJOR and
    CLANG_VERSION_MINOR. Today, these resolve to 4 and 0, respectively.
 5. If CLANG_VERSION matches a regexp with three version components, then
    CLANG_VERSION_PATCHLEVEL is extracted and the CLANG_HAS_VERSION_PATCHLEVEL
    variable is set to 1. Today, these values are 0 and 1, respectively.
 6. The CLANG_VERSION_* variables (and CLANG_HAS_VERSION_PATCHLEVEL) are
    configured into [llvm/tools/clang/]include/clang/Basic/Version.inc
    verbatim by CMake.
 7. In [llvm/tools/clang/]include/clang/Basic/Version.h, macros are defined
    conditionally, based on CLANG_HAS_VERSION_PATCHLEVEL, to compute
    CLANG_VERSION_STRING as either a two- or three-level version number. Today,
    this value is "4.0.0", because despite the patchlevel being 0, it was
    matched by regexp and is thus "HAS"ed by the preprocessor. This string is
    then used wherever Clang's "version" is needed [*].

[*] Including, notably, by compiler-rt, for computing its installation path.

This change collapses steps 2-5 by defaulting Clang to use LLVM's (non-string)
version components for the Clang version (see [*] for why not PACKAGE_VERSION),
and collapses steps 6 and 7 by simply writing CLANG_VERSION_STRING into
Version.inc. The Clang version today always uses the patchlevel form, so the
collapsed Version.inc does not have logic for a version without a patch level.

Historically speaking, this technique began with the VER file in r82085 (which
survives in the form of the regexp in #3). The major, minor, and patchlevel
versions were introduced by r106863 (which remains in #4-6). The VER file itself
was deleted in favor of the LLVM version number in r106914. On the LLVM side,
the individual LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR, and PACKAGE_VERSION
weren't introduced for nearly two more years, until r150405.

Modified:
    cfe/trunk/CMakeLists.txt
    cfe/trunk/include/clang/Basic/Version.h
    cfe/trunk/include/clang/Basic/Version.inc.in
    cfe/trunk/lib/Frontend/InitPreprocessor.cpp

Modified: cfe/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CMakeLists.txt?rev=281666&r1=281665&r2=281666&view=diff
==============================================================================
--- cfe/trunk/CMakeLists.txt (original)
+++ cfe/trunk/CMakeLists.txt Thu Sep 15 17:12:26 2016
@@ -262,22 +262,13 @@ if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_
 endif()
 
 # Compute the Clang version from the LLVM version.
-string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
-  ${PACKAGE_VERSION})
+set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
+set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
+set(CLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
+# Unlike PACKAGE_VERSION, CLANG_VERSION does not include LLVM_VERSION_SUFFIX.
+set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCHLEVEL}")
 message(STATUS "Clang version: ${CLANG_VERSION}")
 
-string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
-  ${CLANG_VERSION})
-string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
-  ${CLANG_VERSION})
-if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
-  set(CLANG_HAS_VERSION_PATCHLEVEL 1)
-  string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
-    ${CLANG_VERSION})
-else()
-  set(CLANG_HAS_VERSION_PATCHLEVEL 0)
-endif()
-
 # Configure the Version.inc file.
 configure_file(
   ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in

Modified: cfe/trunk/include/clang/Basic/Version.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Version.h?rev=281666&r1=281665&r2=281666&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Version.h (original)
+++ cfe/trunk/include/clang/Basic/Version.h Thu Sep 15 17:12:26 2016
@@ -19,26 +19,6 @@
 #include "clang/Basic/Version.inc"
 #include "llvm/ADT/StringRef.h"
 
-/// \brief Helper macro for CLANG_VERSION_STRING.
-#define CLANG_MAKE_VERSION_STRING2(X) #X
-
-#ifdef CLANG_VERSION_PATCHLEVEL
-/// \brief Helper macro for CLANG_VERSION_STRING.
-#define CLANG_MAKE_VERSION_STRING(X,Y,Z) CLANG_MAKE_VERSION_STRING2(X.Y.Z)
-
-/// \brief A string that describes the Clang version number, e.g., "1.0".
-#define CLANG_VERSION_STRING \
-  CLANG_MAKE_VERSION_STRING(CLANG_VERSION_MAJOR,CLANG_VERSION_MINOR, \
-                            CLANG_VERSION_PATCHLEVEL)
-#else
-/// \brief Helper macro for CLANG_VERSION_STRING.
-#define CLANG_MAKE_VERSION_STRING(X,Y) CLANG_MAKE_VERSION_STRING2(X.Y)
-
-/// \brief A string that describes the Clang version number, e.g., "1.0".
-#define CLANG_VERSION_STRING \
-  CLANG_MAKE_VERSION_STRING(CLANG_VERSION_MAJOR,CLANG_VERSION_MINOR)
-#endif
-
 namespace clang {
   /// \brief Retrieves the repository path (e.g., Subversion path) that
   /// identifies the particular Clang branch, tag, or trunk from which this

Modified: cfe/trunk/include/clang/Basic/Version.inc.in
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Version.inc.in?rev=281666&r1=281665&r2=281666&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Version.inc.in (original)
+++ cfe/trunk/include/clang/Basic/Version.inc.in Thu Sep 15 17:12:26 2016
@@ -1,6 +1,5 @@
 #define CLANG_VERSION @CLANG_VERSION@
+#define CLANG_VERSION_STRING "@CLANG_VERSION@"
 #define CLANG_VERSION_MAJOR @CLANG_VERSION_MAJOR@
 #define CLANG_VERSION_MINOR @CLANG_VERSION_MINOR@
-#if @CLANG_HAS_VERSION_PATCHLEVEL@
 #define CLANG_VERSION_PATCHLEVEL @CLANG_VERSION_PATCHLEVEL@
-#endif

Modified: cfe/trunk/lib/Frontend/InitPreprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/InitPreprocessor.cpp?rev=281666&r1=281665&r2=281666&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/InitPreprocessor.cpp (original)
+++ cfe/trunk/lib/Frontend/InitPreprocessor.cpp Thu Sep 15 17:12:26 2016
@@ -511,16 +511,12 @@ static void InitializePredefinedMacros(c
 #define TOSTR(X) TOSTR2(X)
   Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
   Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
-#ifdef CLANG_VERSION_PATCHLEVEL
   Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
-#else
-  Builder.defineMacro("__clang_patchlevel__", "0");
-#endif
+#undef TOSTR
+#undef TOSTR2
   Builder.defineMacro("__clang_version__", 
                       "\"" CLANG_VERSION_STRING " "
                       + getClangFullRepositoryVersion() + "\"");
-#undef TOSTR
-#undef TOSTR2
   if (!LangOpts.MSVCCompat) {
     // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
     // not compiling for MSVC compatibility




More information about the cfe-commits mailing list