1#
2# Copyright 2010-2014,2016 Ettus Research LLC
3# Copyright 2018 Ettus Research, a National Instruments Company
4#
5# SPDX-License-Identifier: GPL-3.0-or-later
6#
7
8########################################################################
9include(UHDPython) #requires python for parsing
10find_package(Git QUIET)
11
12########################################################################
13# Setup Version Numbers
14#  - Increment major on large-scale library changes
15#  - Increment API on API changes
16#  - Increment ABI on ABI changes
17#  - Increment patch for bugfixes and docs
18#  - set UHD_VERSION_DEVEL to true for master and development branches
19########################################################################
20set(UHD_VERSION_MAJOR 4)
21set(UHD_VERSION_API   0)
22set(UHD_VERSION_ABI   0)
23set(UHD_VERSION_PATCH 0)
24set(UHD_VERSION_DEVEL FALSE)
25
26########################################################################
27# If we're on a development branch, we skip the patch version
28########################################################################
29if(DEFINED UHD_VERSION_PATCH_OVERRIDE)
30    set(UHD_VERSION_DEVEL FALSE)
31    set(UHD_VERSION_PATCH ${UHD_VERSION_PATCH_OVERRIDE})
32endif(DEFINED UHD_VERSION_PATCH_OVERRIDE)
33if(NOT DEFINED UHD_VERSION_DEVEL)
34    set(UHD_VERSION_DEVEL FALSE)
35endif(NOT DEFINED UHD_VERSION_DEVEL)
36set(UHD_GIT_BRANCH "")
37if(GIT_FOUND)
38    execute_process(
39        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
40        COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
41        OUTPUT_VARIABLE _git_branch OUTPUT_STRIP_TRAILING_WHITESPACE
42        RESULT_VARIABLE _git_branch_result
43    )
44    if(_git_branch_result EQUAL 0)
45        set(UHD_GIT_BRANCH ${_git_branch})
46        if(UHD_GIT_BRANCH MATCHES "^UHD-")
47            message(STATUS "Operating on release branch (${UHD_GIT_BRANCH}).")
48	    set(UHD_VERSION_DEVEL FALSE)
49        elseif(UHD_GIT_BRANCH STREQUAL "master")
50            message(STATUS "Operating on master branch.")
51            set(UHD_VERSION_DEVEL TRUE)
52        else()
53            message(STATUS "Working off of feature or development branch. Updating version number.")
54            execute_process(
55                COMMAND ${PYTHON_EXECUTABLE} -c "print('${_git_branch}'.replace('/', '-'))"
56                OUTPUT_VARIABLE _git_safe_branch OUTPUT_STRIP_TRAILING_WHITESPACE
57            )
58            set(UHD_VERSION_PATCH ${_git_safe_branch})
59            set(UHD_VERSION_DEVEL TRUE)
60        endif()
61    else()
62        message(STATUS "Could not determine git branch. Probably building from tarball.")
63    endif()
64else(GIT_FOUND)
65    message(WARNING "Could not detect git executable! Could not determine exact version of UHD!")
66endif(GIT_FOUND)
67if(DEFINED UHD_GIT_BRANCH_OVERRIDE)
68    message(STATUS "Overriding auto-detected git branch and setting to: ${UHD_GIT_BRANCH_OVERRIDE}")
69    set(UHD_GIT_BRANCH ${UHD_GIT_BRANCH_OVERRIDE})
70endif(DEFINED UHD_GIT_BRANCH_OVERRIDE)
71
72########################################################################
73# Version information discovery through git log
74########################################################################
75
76#grab the git ref id for the current head
77execute_process(
78    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
79    COMMAND ${GIT_EXECUTABLE} describe --always --abbrev=8 --long
80    OUTPUT_VARIABLE _git_describe OUTPUT_STRIP_TRAILING_WHITESPACE
81    RESULT_VARIABLE _git_describe_result
82)
83
84#only set the build info on success
85if(_git_describe_result EQUAL 0)
86    if(NOT UHD_GIT_COUNT)
87        execute_process(
88            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
89            COMMAND ${PYTHON_EXECUTABLE} -c "
90try:
91    print('${_git_describe}'.split('-')[-2])
92except IndexError:
93    print('0')
94"
95            OUTPUT_VARIABLE UHD_GIT_COUNT OUTPUT_STRIP_TRAILING_WHITESPACE
96        )
97    endif()
98    if(NOT UHD_GIT_HASH)
99        execute_process(
100            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
101            COMMAND ${PYTHON_EXECUTABLE} -c "
102try:
103    print('${_git_describe}'.split('-')[-1])
104except IndexError:
105    print('unknown')
106"
107             OUTPUT_VARIABLE UHD_GIT_HASH OUTPUT_STRIP_TRAILING_WHITESPACE
108        )
109    endif()
110endif()
111
112## Set default values if all fails. Make sure they're identical to the ones above.
113if(NOT UHD_GIT_COUNT)
114    set(UHD_GIT_COUNT "0")
115endif()
116
117if(NOT UHD_GIT_HASH)
118    set(UHD_GIT_HASH "unknown")
119endif()
120
121if(UHD_RELEASE_MODE)
122    set(UHD_GIT_HASH ${UHD_RELEASE_MODE})
123
124    #Ignore UHD_GIT_COUNT in UHD_VERSION if the string 'release' is in UHD_RELEASE_MODE
125    execute_process(
126        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
127        COMMAND ${PYTHON_EXECUTABLE} -c "print ('release' in '${UHD_RELEASE_MODE}') or ('rc' in '${UHD_RELEASE_MODE}')"
128        OUTPUT_VARIABLE TRIM_UHD_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE
129    )
130endif()
131
132
133########################################################################
134# Define the derived version variables:
135if(DEFINED UHD_VERSION)
136    set(UHD_VERSION "${UHD_VERSION}" CACHE STRING "Set UHD_VERSION to a custom value")
137elseif(TRIM_UHD_VERSION STREQUAL "True")
138    set(UHD_VERSION "${UHD_VERSION_MAJOR}.${UHD_VERSION_API}.${UHD_VERSION_ABI}.${UHD_VERSION_PATCH}-${UHD_GIT_HASH}")
139else()
140    set(UHD_VERSION "${UHD_VERSION_MAJOR}.${UHD_VERSION_API}.${UHD_VERSION_ABI}.${UHD_VERSION_PATCH}-${UHD_GIT_COUNT}-${UHD_GIT_HASH}")
141endif()
142if(DEFINED UHD_ABI_VERSION)
143    set(UHD_ABI_VERSION "${UHD_ABI_VERSION}"
144        CACHE STRING "Set UHD_ABI_VERSION to a custom value")
145else()
146    set(UHD_ABI_VERSION "${UHD_VERSION_MAJOR}.${UHD_VERSION_API}.${UHD_VERSION_ABI}")
147endif()
148
149set(UHD_COMPONENT "UHD")
150