|
| 1 | +# Copyright 2018 The Imaging Source Europe GmbH |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# |
| 16 | +# Convenience functions for accessing git information in cmake |
| 17 | +# |
| 18 | + |
| 19 | + |
| 20 | +# git_is_repository |
| 21 | +# verifies that cmake is in a git repository |
| 22 | +# useful when people use the 'download as zip' functionality from github, et al |
| 23 | +# sets given argument to TRUE if git is used, otherwise it will be set to FALSE |
| 24 | +function (git_is_repository is_repo) |
| 25 | + |
| 26 | + execute_process( |
| 27 | + COMMAND git rev-parse --git-dir |
| 28 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 29 | + OUTPUT_VARIABLE FUNC_ROOT_DIR |
| 30 | + ERROR_QUIET |
| 31 | + ) |
| 32 | + |
| 33 | + if (FUNC_ROOT_DIR) |
| 34 | + set(${is_repo} TRUE PARENT_SCOPE) |
| 35 | + else () |
| 36 | + set(${is_repo} FALSE PARENT_SCOPE) |
| 37 | + endif (FUNC_ROOT_DIR) |
| 38 | + |
| 39 | +endfunction (git_is_repository) |
| 40 | + |
| 41 | +# git_commit_hash |
| 42 | +# fills the given argument with the hash of the current commit |
| 43 | +# |
| 44 | +function (git_commit_hash return_value) |
| 45 | + execute_process( |
| 46 | + COMMAND git log -1 --format=%h |
| 47 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 48 | + OUTPUT_VARIABLE GIT_COMMIT_HASH |
| 49 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 50 | + ERROR_QUIET |
| 51 | + ) |
| 52 | + |
| 53 | + if (GIT_COMMIT_HASH) |
| 54 | + set(${return_value} "${GIT_COMMIT_HASH}" PARENT_SCOPE) |
| 55 | + endif (GIT_COMMIT_HASH) |
| 56 | + |
| 57 | +endfunction (git_commit_hash) |
| 58 | + |
| 59 | +# git_commit_count |
| 60 | +# fills the given argument with the number of commits on this branch |
| 61 | +function (git_commit_count return_value) |
| 62 | + |
| 63 | + execute_process( |
| 64 | + COMMAND git rev-list --count HEAD |
| 65 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 66 | + OUTPUT_VARIABLE GIT_COMMIT_COUNT |
| 67 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 68 | + ERROR_QUIET |
| 69 | + ) |
| 70 | + |
| 71 | + if (GIT_COMMIT_COUNT) |
| 72 | + set(${return_value} "${GIT_COMMIT_COUNT}" PARENT_SCOPE) |
| 73 | + endif (GIT_COMMIT_COUNT) |
| 74 | + |
| 75 | +endfunction (git_commit_count) |
| 76 | + |
| 77 | +# git_commit_tag |
| 78 | +# fills the given argument with the tag the current commit has |
| 79 | +function (git_commit_tag return_value) |
| 80 | + execute_process( |
| 81 | + COMMAND git describe --exact-match HEAD |
| 82 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 83 | + OUTPUT_VARIABLE GIT_TAG |
| 84 | + ERROR_VARIABLE GIT_TAG_ERROR |
| 85 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 86 | + ERROR_QUIET |
| 87 | + ) |
| 88 | + |
| 89 | + if (GIT_TAG) |
| 90 | + set(${return_value} "${GIT_TAG}" PARENT_SCOPE) |
| 91 | + endif (GIT_TAG) |
| 92 | + |
| 93 | +endfunction (git_commit_tag) |
| 94 | + |
| 95 | +# git_branch |
| 96 | +# fills the given argument with the name of the current branch |
| 97 | +# |
| 98 | +function (git_branch return_value) |
| 99 | + execute_process( |
| 100 | + COMMAND git rev-parse --abbrev-ref HEAD |
| 101 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 102 | + OUTPUT_VARIABLE GIT_BRANCH |
| 103 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 104 | + ERROR_QUIET |
| 105 | + ) |
| 106 | + |
| 107 | + if (GIT_BRANCH) |
| 108 | + set(${return_value} "${GIT_BRANCH}" PARENT_SCOPE) |
| 109 | + endif (GIT_BRANCH) |
| 110 | + |
| 111 | +endfunction (git_branch) |
| 112 | + |
| 113 | +# defines the following variables in the parent scope |
| 114 | +# GIT_REPO_PRESENT |
| 115 | +# GIT_COMMIT_HASH |
| 116 | +# GIT_COMMIT_COUNT |
| 117 | +# GIT_TAG |
| 118 | +# GIT_BRANCH |
| 119 | +# |
| 120 | +# the variables will not defined should error occur or should |
| 121 | +# the project not exist within a git repo |
| 122 | +# |
| 123 | +function (find_git_settings) |
| 124 | + git_is_repository( repo_present ) |
| 125 | + if( repo_present ) |
| 126 | + set(GIT_REPO_PRESENT ${repo_present} PARENT_SCOPE) |
| 127 | + |
| 128 | + git_commit_hash(hash) |
| 129 | + if (hash) |
| 130 | + set(GIT_COMMIT_HASH "${hash}" PARENT_SCOPE) |
| 131 | + endif (hash) |
| 132 | + |
| 133 | + git_commit_count(count) |
| 134 | + if (count) |
| 135 | + set(GIT_COMMIT_COUNT "${count}" PARENT_SCOPE) |
| 136 | + endif (count) |
| 137 | + |
| 138 | + git_commit_tag(tag) |
| 139 | + if (tag) |
| 140 | + set(GIT_TAG "${tag}" PARENT_SCOPE) |
| 141 | + endif (tag) |
| 142 | + |
| 143 | + git_branch(branch) |
| 144 | + if (branch) |
| 145 | + set(GIT_BRANCH "${branch}" PARENT_SCOPE) |
| 146 | + endif (branch) |
| 147 | + |
| 148 | + endif( repo_present ) |
| 149 | +endfunction () |
0 commit comments