SetUpVersion.cmake 2.32 KB
Newer Older
Brian Panneton's avatar
Brian Panneton committed
1
2
3
4
5
# Version Suite
# Author: Brian Panneton
# Descrition: 	This small suite allows you to add support
#		for versioning in your projects

6
# This allows you to turn on and off the auto
Brian Panneton's avatar
Brian Panneton committed
7
# update of the (project name)Version.hpp file
8
SET(VERSION_CONTROL_AUTOUPDATE OFF CACHE BOOL "Automaticaly Update The Version")
Brian Panneton's avatar
Brian Panneton committed
9
10
11
12
13
14
15
16
17
MARK_AS_ADVANCED(VERSION_CONTROL_AUTOUPDATE)

# We need to make sure we have the header file
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/CMake/VersionSuite)

# Default incase CalculateVerison is not called
SET(vMajor "0")
SET(vMinor "0")

18
# This Macro allows you to set up the Version in a one liner
19
MACRO(VersionCreate versionName versionMajor export_name)
20
21
    VersionMajorSet(${versionMajor})
    VersionCalculate()
22
23
    VersionWrite(${versionName} ${export_name} "${ARGN}")
ENDMACRO(VersionCreate versionName versionMajor export_name)
24

Brian Panneton's avatar
Brian Panneton committed
25
26
27
28
29
30
31
# This Macro allows you to set the rewrite number
MACRO(VersionMajorSet versionMajor)
	SET(vMajor ${versionMajor})
ENDMACRO(VersionMajorSet)

# This Macro calculates the number of tags from your git repo
MACRO(VersionCalculate)
32
33
34
35
36
37
38
39
    FIND_PACKAGE(Git)
    IF(GIT_FOUND)
        EXEC_PROGRAM(${GIT_EXECUTABLE} ${CMAKE_SOURCE_DIR} ARGS tag OUTPUT_VARIABLE return)
        STRING(REGEX REPLACE "\n" ";" return "${return}")
        SET(count 0)
        FOREACH(r ${return})
            MATH(EXPR count "${count} + 1")
        ENDFOREACH(r ${return})
40
        SET(vMinor ${count})
41
42
43
    ELSE(GIT_FOUND)
        SET(vMinor "X")
    ENDIF(GIT_FOUND)
Brian Panneton's avatar
Brian Panneton committed
44
45
ENDMACRO(VersionCalculate)

46
# This Macro writes your hpp/cpp files
47
48
49
50
51
MACRO(VersionWrite vProjectName export_name)
    SET(include_list "${ARGN}")
    FOREACH(il ${include_list})
        SET(includes "${includes}\n\#include \"${il}\"")
    ENDFOREACH(il ${include_list})
52
53
    FILE(WRITE ${CMAKE_BINARY_DIR}/${vProjectName}Version.hpp
"/* Current Version of ${vProjectName}
54
55
56
 * Major is: ${vMajor}
 * Minor is: ${vMinor}
 */
57
${includes}
58
\#include \"ProjectVersion.hpp\"
59
extern ${export_name} ProjectVersion ${vProjectName}Version;\n"
60
61
    )

62
	FILE(WRITE ${CMAKE_BINARY_DIR}/${vProjectName}Version.cpp
63
"/* Current Version of ${vProjectName}
64
 * Make sure to include this file in your built sources
65
66
 */
\#include \"${vProjectName}Version.hpp\"
67
ProjectVersion ${vProjectName}Version = ProjectVersion(\"${vProjectName}\", \"${vMajor}\", \"${vMinor}\");\n"
Brian Panneton's avatar
Brian Panneton committed
68
	)
69
ENDMACRO(VersionWrite vProjectName export_name)