ParaViewDetermineVersion.cmake
Go to the documentation of this file.
1 # SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 # SPDX-License-Identifier: BSD-3-Clause
3 
4 # Used to determine the version for ParaView source using "git describe", if git
5 # is found. On success sets following variables in caller's scope:
6 # ${var_prefix}_VERSION
7 # ${var_prefix}_VERSION_MAJOR
8 # ${var_prefix}_VERSION_MINOR
9 # ${var_prefix}_VERSION_PATCH
10 # ${var_prefix}_VERSION_PATCH_EXTRA
11 # ${var_prefix}_VERSION_FULL
12 # ${var_prefix}_VERSION_IS_RELEASE is true, if patch-extra is empty.
13 #
14 # If git is not found, or git describe cannot be run successfully, then these
15 # variables are left unchanged and status message is printed.
16 #
17 # Arguments are:
18 # source_dir : Source directory
19 # git_command : git executable
20 # var_prefix : prefix for variables e.g. "PARAVIEW".
21 function(determine_version source_dir git_command var_prefix)
22  if ("$Format:$" STREQUAL "")
23  # We are in an exported tarball and should use the shipped version
24  # information. Just return here to avoid the warning message at the end of
25  # this function.
26  return ()
27  elseif (NOT PARAVIEW_GIT_DESCRIBE AND
28  EXISTS ${git_command} AND
29  EXISTS ${source_dir}/.git)
30  execute_process(
31  COMMAND ${git_command} describe
32  WORKING_DIRECTORY ${source_dir}
33  RESULT_VARIABLE result
34  OUTPUT_VARIABLE output
35  ERROR_QUIET
36  OUTPUT_STRIP_TRAILING_WHITESPACE
37  ERROR_STRIP_TRAILING_WHITESPACE)
38  if (NOT result EQUAL 0)
39  # git describe failed (bad return code).
40  set(output "")
41  endif()
42  else ()
43  # note, output may be set to empty if PARAVIEW_GIT_DESCRIBE is not defined.
44  set(output "${PARAVIEW_GIT_DESCRIBE}")
45  endif()
46 
47  unset(tmp_VERSION)
48  extract_version_components(${source_dir} "${output}" tmp)
49  if(DEFINED tmp_VERSION)
50  if (${var_prefix}_VERSION_PATCH GREATER "20200101")
51  if (NOT tmp_VERSION_MAJOR STREQUAL ${var_prefix}_VERSION_MAJOR OR
52  NOT tmp_VERSION_MINOR STREQUAL ${var_prefix}_VERSION_MINOR)
53  message(WARNING
54  "Version from git (${tmp_VERSION}) disagrees with hard coded version (${${var_prefix}_VERSION}). Either update the git tags or version.txt.")
55  if (NOT "$ENV{CI}" STREQUAL "")
56  message(WARNING
57  "Please update your fork tags, using this command: `git fetch origin --tags && git push gitlab --tags`.")
58  endif ()
59  endif ()
60  # git describe rely on tags that do not have access to the patch version, correct the ${var_prefix}_VERSION accordingly
61  if (${var_prefix}_VERSION_PATCH GREATER tmp_VERSION_PATCH)
62  set(tmp_VERSION_PATCH "${${var_prefix}_VERSION_PATCH}")
63  set(tmp_VERSION "${tmp_VERSION_MAJOR}.${tmp_VERSION_MINOR}.${tmp_VERSION_PATCH}")
64  endif ()
65  elseif (NOT "${tmp_VERSION}" STREQUAL "${${var_prefix}_VERSION}")
66  message(WARNING
67  "Version from git (${tmp_VERSION}) disagrees with hard coded version (${${var_prefix}_VERSION}). Either update the git tags or version.txt.")
68  if (NOT "$ENV{CI}" STREQUAL "")
69  message(WARNING
70  "Please update your fork tags, using this command: `git fetch origin --tags && git push gitlab --tags`.")
71  endif ()
72  endif()
73  foreach(suffix BRANCH VERSION VERSION_MAJOR VERSION_MINOR VERSION_PATCH
74  VERSION_PATCH_EXTRA VERSION_FULL VERSION_IS_RELEASE)
75  set(${var_prefix}_${suffix} ${tmp_${suffix}} PARENT_SCOPE)
76  endforeach()
77  else()
78  message(STATUS
79  "Could not use git to determine source version, using version ${${var_prefix}_VERSION_FULL}")
80  endif()
81 endfunction()
82 
83 # Extracts components from a version string. See determine_version() for usage.
84 function(extract_version_components source_dir version_string var_prefix)
85  string(REGEX MATCH "^v?(([0-9]+)\\.([0-9]+)\\.([0-9]+)-?(.*))$"
86  version_matches "${version_string}")
87  if(CMAKE_MATCH_0)
88  # note, we don't use CMAKE_MATCH_0 for `full` since it may or may not have
89  # the `v` prefix.
90  set(full ${CMAKE_MATCH_1})
91  set(major ${CMAKE_MATCH_2})
92  set(minor ${CMAKE_MATCH_3})
93  set(patch ${CMAKE_MATCH_4})
94  set(patch_extra ${CMAKE_MATCH_5})
95  set(branch "")
96  if (patch_extra)
97  if (NOT source_dir)
98  set(source_dir "${ParaView_SOURCE_DIR}")
99  endif ()
100  execute_process(
101  COMMAND ${git_command}
102  name-rev
103  --name-only
104  --no-undefined # error if these names don't work
105  --refs=refs/tags/* # tags
106  --refs=refs/heads/* # branches
107  --refs=refs/pipelines/* # CI
108  HEAD
109  WORKING_DIRECTORY ${source_dir}
110  RESULT_VARIABLE result
111  OUTPUT_VARIABLE output
112  ERROR_QUIET
113  OUTPUT_STRIP_TRAILING_WHITESPACE
114  ERROR_STRIP_TRAILING_WHITESPACE)
115  if (result EQUAL 0)
116  # The branch name.
117  set(branch "${output}")
118  endif()
119  endif ()
120 
121  set(${var_prefix}_BRANCH "${branch}" PARENT_SCOPE)
122  set(${var_prefix}_VERSION "${major}.${minor}" PARENT_SCOPE)
123  set(${var_prefix}_VERSION_MAJOR ${major} PARENT_SCOPE)
124  set(${var_prefix}_VERSION_MINOR ${minor} PARENT_SCOPE)
125  set(${var_prefix}_VERSION_PATCH ${patch} PARENT_SCOPE)
126  set(${var_prefix}_VERSION_PATCH_EXTRA ${patch_extra} PARENT_SCOPE)
127  set(${var_prefix}_VERSION_FULL ${full} PARENT_SCOPE)
128  if("${major}.${minor}.${patch}" VERSION_EQUAL "${full}" AND
129  # Date-based patch versions are never releases.
130  patch LESS "20000101")
131  set(${var_prefix}_VERSION_IS_RELEASE TRUE PARENT_SCOPE)
132  else()
133  set(${var_prefix}_VERSION_IS_RELEASE FALSE PARENT_SCOPE)
134  endif()
135  endif()
136 endfunction()
function extract_version_components(source_dir, version_string, var_prefix)
name
#define VERSION
Definition: jconfigint.h:17
function determine_version(source_dir, git_command, var_prefix)