# Copyright 2026, Raffi Enficiaud.
#
# Checks that the version of the python distribution reaches the cmake configuration
# through the variables cmake-pip injects.
#
# Like check_python_interpreter, this configuration fails on purpose: pip removes the
# temporary build directory, so the only reliable way to observe what cmake was given is
# to make it report the values in an error message the test can read back.

cmake_minimum_required(VERSION 3.21)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# The negative check. If cmake-pip ever stops injecting the versions, the build fails
# here, with a message that says so, instead of failing further down with an empty value
# that could be mistaken for a mismatch.
if(NOT DEFINED CMAKE_PIP_PACKAGE_VERSION)
  message(FATAL_ERROR "[TEST] CMAKE_PIP_PACKAGE_VERSION was not passed by cmake-pip")
endif()

if(NOT DEFINED CMAKE_PIP_PACKAGE_VERSION_NUMERIC)
  message(FATAL_ERROR "[TEST] CMAKE_PIP_PACKAGE_VERSION_NUMERIC was not passed by cmake-pip")
endif()

# Handing the numeric version to project() is part of the test: CMake accepts only
# numeric dotted fields here, so a value still carrying a pre-release, development or
# local segment fails the configuration outright.
project(TestCMakePIPVersion VERSION ${CMAKE_PIP_PACKAGE_VERSION_NUMERIC})

# One value per line, so that the test can scan the output line by line.
message(FATAL_ERROR
        "[TEST] reporting the versions given to cmake\n"
        "[TEST] cmake_pip_version=${CMAKE_PIP_PACKAGE_VERSION}\n"
        "[TEST] cmake_pip_version_numeric=${CMAKE_PIP_PACKAGE_VERSION_NUMERIC}\n"
        "[TEST] cmake_project_version=${PROJECT_VERSION}\n")
