Possible Bug or Documentation Error in FindOpenMp: OpenMP_VERSION Not Set.

Hello,

I’ve noticed that the FindOpenMP module doesn’t seem to set the OpenMP_Version
variable. Minimal CMake project file to reproduce:

cmake_minimum_required(VERSION 3.1)
project(TEST LANGUAGES C CXX)
find_package(OpenMP 2.0)
message("OpenMP: FOUND=${OpenMP_FOUND} VERSION=${OpenMP_VERSION} C_VERSION=${OpenMP_C_VERSION} CXX_VERSION=${OpenMP_CXX_VERSION}")

Example output on an M1 Macbook Air:

$ cmake --version && cmake ..
cmake version 3.25.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).
-- The C compiler identification is AppleClang 14.0.0.14000029
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_C: -Xclang -fopenmp (found suitable version "5.0", minimum required is "2.0")
-- Found OpenMP_CXX: -Xclang -fopenmp (found suitable version "5.0", minimum required is "2.0")
-- Found OpenMP: TRUE (found suitable version "5.0", minimum required is "2.0")
OpenMP: FOUND=TRUE VERSION= C_VERSION=5.0 CXX_VERSION=5.0
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/build

Given that the documentation states:

OpenMP_VERSION
Minimal version of the OpenMP standard detected among the requested languages,
or all enabled languages if no components were specified.

I would have expected OpenMP_VERSION to be defined and set to 5.0 here, but that
doesn’t happen on any platform I’ve tried on so far. It is pretty easy to ‘fix’
it, as seen in my example below, but I guess this variable really should be set
by the module itself.

# Make sure that there is a proper version of OpenMP specified.
if (OpenMP_FOUND AND NOT DEFINED OpenMP_VERSION)
  set(OpenMP_VERSION 2.0)
  if (DEFINED OpenMP_C_VERSION AND OpenMP_VERSION VERSION_LESS ${OpenMP_C_VERSION})
    set(OpenMP_VERSION ${OpenMP_C_VERSION})
  endif()
  if (DEFINED OpenMP_CXX_VERSION AND ${OpenMP_VERSION} VERSION_LESS ${OpenMP_CXX_VERSION})
    set(OpenMP_VERSION ${OpenMP_CXX_VERSION})
  endif()
endif()

So I guess the question is: Is this a bug in the FindOpenMP.cmake module or a
documentation error?

Yeah, it seems like the if logic there is backwards from the docs. I’m not familiar enough with OpenMP to know what is the better solution.