alexreinking created this revision.
alexreinking added reviewers: chandlerc, gottesmm, pmccormick.
Herald added subscribers: cota, teijeong, rdzhabarov, tatianashp, msifontes, 
jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, lucyrfox, mgester, 
arpith-jacob, antiagainst, shauheen, rriddle, mehdi_amini, mgorny.
Herald added a reviewer: bollu.
Herald added a reviewer: sscalpone.
alexreinking requested review of this revision.
Herald added subscribers: cfe-commits, stephenneuendorffer, nicolasvasilache.
Herald added projects: clang, MLIR.

When supplying a package with `<Project>Config.cmake` files, it is required to 
also supply a `<Project>ConfigVersion.cmake` file which defines compatibility 
with version numbers. If these files aren't distributed then supplying a 
version number will fail. But on the other hand, //not// supplying a version 
number can result in the wrong package being selected. For example, because the 
base LLVM package //does// supply `LLVMConfigVersion.cmake`, the following can 
occur:

  find_package(LLVM 11.0 REQUIRED)  # Succeeds
  find_package(LLD 11.0)  # Fails because there's no version config
  find_package(LLD)  # Succeeds, finds version 10.

This patch would not affect any existing uses of the CMake build/packages 
because the `ConfigVersion.cmake` file is ignored when no version is given and 
the status quo is that supplying a version fails.

This patch also fixes a bug I opened a year ago here: 
https://bugs.llvm.org/show_bug.cgi?id=45027


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97513

Files:
  clang/cmake/modules/CMakeLists.txt
  clang/cmake/modules/ClangConfigVersion.cmake.in
  flang/cmake/modules/CMakeLists.txt
  flang/cmake/modules/FlangConfigVersion.cmake.in
  lld/CMakeLists.txt
  lld/cmake/modules/CMakeLists.txt
  lld/cmake/modules/LLDConfigVersion.cmake.in
  mlir/CMakeLists.txt
  mlir/cmake/modules/CMakeLists.txt
  mlir/cmake/modules/MLIRConfigVersion.cmake.in
  polly/CMakeLists.txt
  polly/cmake/CMakeLists.txt
  polly/cmake/PollyConfigVersion.cmake.in

Index: polly/cmake/PollyConfigVersion.cmake.in
===================================================================
--- /dev/null
+++ polly/cmake/PollyConfigVersion.cmake.in
@@ -0,0 +1,13 @@
+set(PACKAGE_VERSION "@PACKAGE_VERSION@")
+
+# Polly is API-compatible only with matching major.minor versions
+# and patch versions not less than that requested.
+if("@POLLY_VERSION_MAJOR@.@POLLY_VERSION_MINOR@" VERSION_EQUAL
+    "${PACKAGE_FIND_VERSION_MAJOR}.${PACKAGE_FIND_VERSION_MINOR}"
+   AND NOT "@POLLY_VERSION_PATCH@" VERSION_LESS "${PACKAGE_FIND_VERSION_PATCH}")
+  set(PACKAGE_VERSION_COMPATIBLE 1)
+  if("@POLLY_VERSION_PATCH@" VERSION_EQUAL
+      "${PACKAGE_FIND_VERSION_PATCH}")
+    set(PACKAGE_VERSION_EXACT 1)
+  endif()
+endif()
Index: polly/cmake/CMakeLists.txt
===================================================================
--- polly/cmake/CMakeLists.txt
+++ polly/cmake/CMakeLists.txt
@@ -71,6 +71,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/PollyConfig.cmake.in
   ${POLLY_CONFIG_CMAKE_DIR}/PollyConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/PollyConfigVersion.cmake.in
+  ${POLLY_CONFIG_CMAKE_DIR}/PollyConfigVersion.cmake
+  @ONLY)
 
 file(GENERATE
   OUTPUT ${POLLY_CONFIG_CMAKE_DIR}/${POLLY_EXPORTS_FILE_NAME}
@@ -120,6 +124,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/PollyConfig.cmake.in
   ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/PollyConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/PollyConfigVersion.cmake.in
+  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/PollyConfigVersion.cmake
+  @ONLY)
 file(GENERATE OUTPUT
   ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${POLLY_EXPORTS_FILE_NAME}
   CONTENT "${POLLY_EXPORTS}")
@@ -128,6 +136,7 @@
   install(
     FILES
     "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/PollyConfig.cmake"
+    "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/PollyConfigVersion.cmake"
     "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${POLLY_EXPORTS_FILE_NAME}"
     DESTINATION "${POLLY_INSTALL_PACKAGE_DIR}")
 endif ()
Index: polly/CMakeLists.txt
===================================================================
--- polly/CMakeLists.txt
+++ polly/CMakeLists.txt
@@ -5,6 +5,7 @@
 
   # Where is LLVM installed?
   find_package(LLVM CONFIG REQUIRED)
+  set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
   set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
   include(HandleLLVMOptions)
   include(AddLLVM)
@@ -48,6 +49,14 @@
 set(POLLY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 set(POLLY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
 
+# Compute the Polly version from the LLVM version.
+string(REGEX MATCH "([0-9]+)\\.([0-9]+)(\\.([0-9]+))?" POLLY_VERSION ${PACKAGE_VERSION})
+message(STATUS "Polly version: ${POLLY_VERSION}")
+
+set(POLLY_VERSION_MAJOR "${CMAKE_MATCH_1}")
+set(POLLY_VERSION_MINOR "${CMAKE_MATCH_2}")
+set(POLLY_VERSION_PATCH "${CMAKE_MATCH_4}")  # Using 4 instead of 3 skips the dot before the patch number.
+
 # Add path for custom modules
 set(CMAKE_MODULE_PATH
   ${CMAKE_MODULE_PATH}
Index: mlir/cmake/modules/MLIRConfigVersion.cmake.in
===================================================================
--- /dev/null
+++ mlir/cmake/modules/MLIRConfigVersion.cmake.in
@@ -0,0 +1,13 @@
+set(PACKAGE_VERSION "@PACKAGE_VERSION@")
+
+# MLIR is API-compatible only with matching major.minor versions
+# and patch versions not less than that requested.
+if("@MLIR_VERSION_MAJOR@.@MLIR_VERSION_MINOR@" VERSION_EQUAL
+    "${PACKAGE_FIND_VERSION_MAJOR}.${PACKAGE_FIND_VERSION_MINOR}"
+   AND NOT "@MLIR_VERSION_PATCH@" VERSION_LESS "${PACKAGE_FIND_VERSION_PATCH}")
+  set(PACKAGE_VERSION_COMPATIBLE 1)
+  if("@MLIR_VERSION_PATCH@" VERSION_EQUAL
+      "${PACKAGE_FIND_VERSION_PATCH}")
+    set(PACKAGE_VERSION_EXACT 1)
+  endif()
+endif()
Index: mlir/cmake/modules/CMakeLists.txt
===================================================================
--- mlir/cmake/modules/CMakeLists.txt
+++ mlir/cmake/modules/CMakeLists.txt
@@ -28,6 +28,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfig.cmake.in
   ${mlir_cmake_builddir}/MLIRConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfigVersion.cmake.in
+  ${mlir_cmake_builddir}/MLIRConfigVersion.cmake
+  @ONLY)
 set(MLIR_CONFIG_CMAKE_DIR)
 set(MLIR_CONFIG_LLVM_CMAKE_DIR)
 set(MLIR_CONFIG_EXPORTS_FILE)
@@ -63,6 +67,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfig.cmake.in
   ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfigVersion.cmake.in
+  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfigVersion.cmake
+  @ONLY)
 set(MLIR_CONFIG_CODE)
 set(MLIR_CONFIG_CMAKE_DIR)
 set(MLIR_CONFIG_LLVM_CMAKE_DIR)
@@ -80,6 +88,7 @@
 
   install(FILES
     ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfig.cmake
+    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfigVersion.cmake
     ${CMAKE_CURRENT_SOURCE_DIR}/AddMLIR.cmake
     DESTINATION ${MLIR_INSTALL_PACKAGE_DIR}
     COMPONENT mlir-cmake-exports)
Index: mlir/CMakeLists.txt
===================================================================
--- mlir/CMakeLists.txt
+++ mlir/CMakeLists.txt
@@ -23,6 +23,8 @@
   set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
     "${CMAKE_CURRENT_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}")
   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
+
+  set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
 endif()
 
 set(MLIR_MAIN_SRC_DIR     ${CMAKE_CURRENT_SOURCE_DIR}  )
@@ -33,6 +35,14 @@
 set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
 set(MLIR_TOOLS_DIR   ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
 
+# Compute the MLIR version from the LLVM version.
+string(REGEX MATCH "([0-9]+)\\.([0-9]+)(\\.([0-9]+))?" MLIR_VERSION ${PACKAGE_VERSION})
+message(STATUS "MLIR version: ${MLIR_VERSION}")
+
+set(MLIR_VERSION_MAJOR "${CMAKE_MATCH_1}")
+set(MLIR_VERSION_MINOR "${CMAKE_MATCH_2}")
+set(MLIR_VERSION_PATCH "${CMAKE_MATCH_4}")  # Using 4 instead of 3 skips the dot before the patch number.
+
 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
 
 include(AddMLIR)
Index: lld/cmake/modules/LLDConfigVersion.cmake.in
===================================================================
--- /dev/null
+++ lld/cmake/modules/LLDConfigVersion.cmake.in
@@ -0,0 +1,13 @@
+set(PACKAGE_VERSION "@PACKAGE_VERSION@")
+
+# LLD is API-compatible only with matching major.minor versions
+# and patch versions not less than that requested.
+if("@LLD_VERSION_MAJOR@.@LLD_VERSION_MINOR@" VERSION_EQUAL
+    "${PACKAGE_FIND_VERSION_MAJOR}.${PACKAGE_FIND_VERSION_MINOR}"
+   AND NOT "@LLD_VERSION_PATCH@" VERSION_LESS "${PACKAGE_FIND_VERSION_PATCH}")
+  set(PACKAGE_VERSION_COMPATIBLE 1)
+  if("@LLD_VERSION_PATCH@" VERSION_EQUAL
+      "${PACKAGE_FIND_VERSION_PATCH}")
+    set(PACKAGE_VERSION_EXACT 1)
+  endif()
+endif()
Index: lld/cmake/modules/CMakeLists.txt
===================================================================
--- lld/cmake/modules/CMakeLists.txt
+++ lld/cmake/modules/CMakeLists.txt
@@ -23,6 +23,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/LLDConfig.cmake.in
   ${lld_cmake_builddir}/LLDConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/LLDConfigVersion.cmake.in
+  ${lld_cmake_builddir}/LLDConfigVersion.cmake
+  @ONLY)
 set(LLD_CONFIG_CMAKE_DIR)
 set(LLD_CONFIG_LLVM_CMAKE_DIR)
 set(LLD_CONFIG_EXPORTS_FILE)
@@ -46,6 +50,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/LLDConfig.cmake.in
   ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/LLDConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/LLDConfigVersion.cmake.in
+  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/LLDConfigVersion.cmake
+  @ONLY)
 set(LLD_CONFIG_CODE)
 set(LLD_CONFIG_CMAKE_DIR)
 set(LLD_CONFIG_EXPORTS_FILE)
@@ -59,6 +67,7 @@
 
   install(FILES
     ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/LLDConfig.cmake
+    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/LLDConfigVersion.cmake
     DESTINATION ${LLD_INSTALL_PACKAGE_DIR}
     COMPONENT lld-cmake-exports)
 
Index: lld/CMakeLists.txt
===================================================================
--- lld/CMakeLists.txt
+++ lld/CMakeLists.txt
@@ -119,14 +119,13 @@
 endif()
 
 # Compute the LLD version from the LLVM version.
-string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLD_VERSION
+string(REGEX MATCH "([0-9]+)\\.([0-9]+)(\\.([0-9]+))?" LLD_VERSION
   ${PACKAGE_VERSION})
 message(STATUS "LLD version: ${LLD_VERSION}")
 
-string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" LLD_VERSION_MAJOR
-  ${LLD_VERSION})
-string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" LLD_VERSION_MINOR
-  ${LLD_VERSION})
+set(LLD_VERSION_MAJOR "${CMAKE_MATCH_1}")
+set(LLD_VERSION_MINOR "${CMAKE_MATCH_2}")
+set(LLD_VERSION_PATCH "${CMAKE_MATCH_4}")  # Using 4 instead of 3 skips the dot before the patch number.
 
 # Configure the Version.inc file.
 configure_file(
Index: flang/cmake/modules/FlangConfigVersion.cmake.in
===================================================================
--- /dev/null
+++ flang/cmake/modules/FlangConfigVersion.cmake.in
@@ -0,0 +1,13 @@
+set(PACKAGE_VERSION "@PACKAGE_VERSION@")
+
+# Flang is API-compatible only with matching major.minor versions
+# and patch versions not less than that requested.
+if("@FLANG_VERSION_MAJOR@.@FLANG_VERSION_MINOR@" VERSION_EQUAL
+    "${PACKAGE_FIND_VERSION_MAJOR}.${PACKAGE_FIND_VERSION_MINOR}"
+   AND NOT "@FLANG_VERSION_PATCHLEVEL@" VERSION_LESS "${PACKAGE_FIND_VERSION_PATCH}")
+  set(PACKAGE_VERSION_COMPATIBLE 1)
+  if("@FLANG_VERSION_PATCHLEVEL@" VERSION_EQUAL
+      "${PACKAGE_FIND_VERSION_PATCH}")
+    set(PACKAGE_VERSION_EXACT 1)
+  endif()
+endif()
Index: flang/cmake/modules/CMakeLists.txt
===================================================================
--- flang/cmake/modules/CMakeLists.txt
+++ flang/cmake/modules/CMakeLists.txt
@@ -23,6 +23,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/FlangConfig.cmake.in
   ${flang_cmake_builddir}/FlangConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/FlangConfigVersion.cmake.in
+  ${flang_cmake_builddir}/FlangConfigVersion.cmake
+  @ONLY)
 set(FLANG_CONFIG_CMAKE_DIR)
 set(FLANG_CONFIG_LLVM_CMAKE_DIR)
 set(FLANG_CONFIG_EXPORTS_FILE)
@@ -49,6 +53,11 @@
   ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/FlangConfig.cmake
   @ONLY)
 
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/FlangConfigVersion.cmake.in
+  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/FlangConfigVersion.cmake
+  @ONLY)
+
 set(FLANG_CONFIG_CODE)
 set(FLANG_CONFIG_CMAKE_DIR)
 set(FLANG_CONFIG_EXPORTS_FILE)
@@ -62,6 +71,7 @@
 
   install(FILES
     ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/FlangConfig.cmake
+    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/FlangConfigVersion.cmake
     DESTINATION ${FLANG_INSTALL_PACKAGE_DIR}
     COMPONENT flang-cmake-exports)
 
Index: clang/cmake/modules/ClangConfigVersion.cmake.in
===================================================================
--- /dev/null
+++ clang/cmake/modules/ClangConfigVersion.cmake.in
@@ -0,0 +1,13 @@
+set(PACKAGE_VERSION "@PACKAGE_VERSION@")
+
+# Clang is API-compatible only with matching major.minor versions
+# and patch versions not less than that requested.
+if("@CLANG_VERSION_MAJOR@.@CLANG_VERSION_MINOR@" VERSION_EQUAL
+    "${PACKAGE_FIND_VERSION_MAJOR}.${PACKAGE_FIND_VERSION_MINOR}"
+   AND NOT "@CLANG_VERSION_PATCHLEVEL@" VERSION_LESS "${PACKAGE_FIND_VERSION_PATCH}")
+  set(PACKAGE_VERSION_COMPATIBLE 1)
+  if("@CLANG_VERSION_PATCHLEVEL@" VERSION_EQUAL
+      "${PACKAGE_FIND_VERSION_PATCH}")
+    set(PACKAGE_VERSION_EXACT 1)
+  endif()
+endif()
Index: clang/cmake/modules/CMakeLists.txt
===================================================================
--- clang/cmake/modules/CMakeLists.txt
+++ clang/cmake/modules/CMakeLists.txt
@@ -23,6 +23,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/ClangConfig.cmake.in
   ${clang_cmake_builddir}/ClangConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/ClangConfigVersion.cmake.in
+  ${clang_cmake_builddir}/ClangConfigVersion.cmake
+  @ONLY)
 set(CLANG_CONFIG_CMAKE_DIR)
 set(CLANG_CONFIG_LLVM_CMAKE_DIR)
 set(CLANG_CONFIG_EXPORTS_FILE)
@@ -48,6 +52,10 @@
   ${CMAKE_CURRENT_SOURCE_DIR}/ClangConfig.cmake.in
   ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/ClangConfig.cmake
   @ONLY)
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/ClangConfigVersion.cmake.in
+  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/ClangConfigVersion.cmake
+  @ONLY)
 set(CLANG_CONFIG_CODE)
 set(CLANG_CONFIG_CMAKE_DIR)
 set(CLANG_CONFIG_EXPORTS_FILE)
@@ -61,6 +69,7 @@
 
   install(FILES
     ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/ClangConfig.cmake
+    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/ClangConfigVersion.cmake
     ${CMAKE_CURRENT_SOURCE_DIR}/AddClang.cmake
     DESTINATION ${CLANG_INSTALL_PACKAGE_DIR}
     COMPONENT clang-cmake-exports)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D97513: Add <Proj... Alex Reinking via Phabricator via cfe-commits

Reply via email to