JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
############################################################################
# BcToolboxCMakeUtils.cmake
# Copyright (C) 2017  Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
############################################################################
 
set(BCTOOLBOX_CMAKE_UTILS_DIR "${CMAKE_CURRENT_LIST_DIR}")
 
macro(bc_apply_compile_flags SOURCE_FILES)
    if(${SOURCE_FILES})
        set(options "")
        foreach(a ${ARGN})
            if(${a})
                string(REPLACE ";" " " options_${a} "${${a}}")
                set(options "${options} ${options_${a}}")
            endif()
        endforeach()
        if(options)
            set_source_files_properties(${${SOURCE_FILES}} PROPERTIES COMPILE_FLAGS "${options}")
        endif()
    endif()
endmacro()
 
macro(bc_git_version PROJECT_NAME PROJECT_VERSION)
    find_package(Git)
    add_custom_target(${PROJECT_NAME}-git-version
        COMMAND ${CMAKE_COMMAND} -DGIT_EXECUTABLE=${GIT_EXECUTABLE} -DPROJECT_NAME=${PROJECT_NAME} -DPROJECT_VERSION=${PROJECT_VERSION} -DWORK_DIR=${CMAKE_CURRENT_SOURCE_DIR} -DTEMPLATE_DIR=${BCTOOLBOX_CMAKE_UTILS_DIR} -DOUTPUT_DIR=${CMAKE_CURRENT_BINARY_DIR} -P ${BCTOOLBOX_CMAKE_UTILS_DIR}/BcGitVersion.cmake
        BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/gitversion.h"
    )
endmacro()
 
 
macro(bc_project_build_version PROJECT_VERSION PROJECT_BUILD_VERSION) 
    find_program (WC wc)
 
    if (WC)
                set(GIT_MINIMUN_VERSION 1.7.1) #might be even lower
    else()
                set(GIT_MINIMUN_VERSION 1.7.10) # --count option of git rev-list is available only since (more or less) git 1.7.10)
    endif()
 
    find_package(Git ${GIT_MINIMUN_VERSION})
    string(COMPARE GREATER "${GIT_VERSION_STRING}" "1.7.10" GIT_REV_LIST_HAS_COUNT)
 
    if (GIT_REV_LIST_HAS_COUNT)
                set(GIT_REV_LIST_COMMAND "${GIT_EXECUTABLE}" "rev-list" "--count" "${PROJECT_VERSION}..HEAD")
                set(WC_COMMAND  "more") #nop
    else()
                set(GIT_REV_LIST_COMMAND "${GIT_EXECUTABLE}" "rev-list" "${PROJECT_VERSION}..HEAD")
                set(WC_COMMAND "${WC}"  "-l")
    endif()
 
    if(GIT_EXECUTABLE)
                execute_process(
                        COMMAND ${GIT_REV_LIST_COMMAND}
                        COMMAND ${WC_COMMAND}
                        OUTPUT_VARIABLE PROJECT_VERSION_BUILD
                        OUTPUT_STRIP_TRAILING_WHITESPACE
                        WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
                )
    endif()
    if (NOT PROJECT_VERSION_BUILD) 
        set(PROJECT_VERSION_BUILD 0)
    endif()
endmacro()