cmlib/CMLibProjectVersion.cmake
2019-02-12 19:31:17 +03:00

26 lines
1.1 KiB
CMake

# read file cmake/etc/version.txt and set global properties
# VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_INT and VERSION_STR
function(set_project_version)
set(_version_file "${CMAKE_SOURCE_DIR}/cmake/etc/version.txt")
if(NOT EXISTS "${_version_file}")
message(FATAL_ERROR "File ${_version_file} doesn't exists")
endif()
file(READ "${_version_file}" _version)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${_version_file})
if(NOT "${_version}" MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)\n")
message(FATAL_ERROR "${_version_file} has wrong format")
endif()
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)\n" _ ${_version})
set_property(GLOBAL PROPERTY VERSION_MAJOR ${CMAKE_MATCH_1})
set_property(GLOBAL PROPERTY VERSION_MINOR ${CMAKE_MATCH_2})
set_property(GLOBAL PROPERTY VERSION_PATCH ${CMAKE_MATCH_3})
math(EXPR _version_int "(${CMAKE_MATCH_1} << 16) + (${CMAKE_MATCH_2} << 8) + ${CMAKE_MATCH_3}")
set_property(GLOBAL PROPERTY VERSION_INT ${_version_int})
set_property(GLOBAL PROPERTY VERSION_STR "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
endfunction()