1#.rst:
2# .. command:: generate_compatibility_version_file
3#
4#  Create a version file for a project::
5#
6#    generate_compatibility_version_file(<filename>
7#      [VERSION <major.minor.patch>]
8#      COMPATIBILITY <AnyNewerVersion|SameMajorVersion|ExactVersion>
9#      [C_ABI]
10#      [CXX_LAYOUT]
11#      [CXX_ABI])
12
13#=============================================================================
14# Copyright 2015 Sensics, Inc. <ryan@sensics.com>
15# Copyright 2012 Alexander Neundorf <neundorf@kde.org>
16#
17# Distributed under the OSI-approved BSD License (the "License");
18# see accompanying file Copyright.txt for details.
19#
20# This software is distributed WITHOUT ANY WARRANTY; without even the
21# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22# See the License for more information.
23#=============================================================================
24# (To distribute this file outside of CMake, substitute the full
25#  License text for the above reference.)
26
27
28include(CMakeParseArguments)
29include(CMakePackageConfigHelpers)
30
31set(GCVF_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "" FORCE)
32
33function(generate_compatibility_version_file _filename)
34
35    set(options C_ABI CXX_LAYOUT CXX_ABI)
36    set(oneValueArgs VERSION COMPATIBILITY )
37    set(multiValueArgs )
38    cmake_parse_arguments(GCVF "${options}" "${oneValueArgs}" "${multiValueArgs}"  ${ARGN})
39
40    if(GCVF_UNPARSED_ARGUMENTS)
41        message(FATAL_ERROR "Unknown keywords given to generate_compatibility_version_file(): ${GCVF_UNPARSED_ARGUMENTS}")
42    endif()
43    set(versionTemplateFile "${CMAKE_ROOT}/Modules/BasicConfigVersion-${GCVF_COMPATIBILITY}.cmake.in")
44    if(NOT EXISTS "${versionTemplateFile}")
45        message(FATAL_ERROR "Bad COMPATIBILITY value used for generate_compatibility_version_file(): \"${GCVF_COMPATIBILITY}\"")
46    endif()
47
48    if(GCVF_CXX_ABI)
49        set(GCVF_CXX_LAYOUT TRUE)
50    endif()
51    if(GCVF_CXX_LAYOUT)
52        set(GCVF_C_ABI TRUE)
53    endif()
54
55    if("${GCVF_VERSION}" STREQUAL "")
56        if("${PROJECT_VERSION}" STREQUAL "")
57            message(FATAL_ERROR "No VERSION specified for generate_compatibility_version_file()")
58        else()
59            set(GCVF_VERSION ${PROJECT_VERSION})
60        endif()
61    endif()
62
63    set(GCVF_WIN_CXXLAYOUT)
64    if(MSVC)
65        set(GCVF_WIN_CXXLAYOUT "MSVC")
66    elseif(MINGW)
67        set(GCVF_WIN_CXXLAYOUT "MinGW")
68    elseif(WIN32)
69        set(GCVF_WIN_CXXLAYOUT "other")
70    endif()
71
72    set(PREV_FILE "${_filename}.cmakeversion")
73    write_basic_package_version_file("${PREV_FILE}" VERSION ${GCVF_VERSION} COMPATIBILITY ${GCVF_COMPATIBILITY})
74    set(GCVF_BASIC TRUE)
75    foreach(level BASIC C_ABI CXX_LAYOUT CXX_ABI)
76        if(GCVF_${level})
77            file(READ "${PREV_FILE}" GCVF_PREVIOUS_FILE)
78            set(PREV_FILE "${_filename}.${level}")
79            configure_file("${GCVF_DIR}/CompatibilityVersionFile-${level}.cmake.in" "${PREV_FILE}" @ONLY)
80        endif()
81    endforeach()
82    configure_file("${PREV_FILE}" "${_filename}" COPYONLY)
83endfunction()