forked from aseba-community/aseba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseVersion.cmake
91 lines (81 loc) · 3.39 KB
/
ParseVersion.cmake
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
82
83
84
85
86
87
88
89
90
# This module checks for SVN / Git binaries, extracts the revision number, and writes it in version.h
# The user can provide a version number through the USER_BUILD_VERSION variable
# version management
if (USER_BUILD_VERSION)
message("-- User version given: " ${USER_BUILD_VERSION})
file(WRITE ${CMAKE_BINARY_DIR}/version.h "#define ASEBA_BUILD_VERSION \"${USER_BUILD_VERSION}\"\n")
return()
endif (USER_BUILD_VERSION)
##########
# detect subversion
##########
find_package(Subversion)
if (Subversion_FOUND)
message("-- Subversion executable found")
else (Subversion_FOUND)
message("-- Subversion executable NOT found")
endif (Subversion_FOUND)
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn/")
set(HAS_SVN_REP 1)
endif (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn/")
# react accordingly
if (Subversion_FOUND AND HAS_SVN_REP)
message("-- SVN repository found")
set(HAS_DYN_VERSION)
# extract working copy information for SOURCE_DIR into MY_XXX variables
Subversion_WC_INFO(${CMAKE_CURRENT_SOURCE_DIR} ASEBA)
# write a file with the SVNVERSION define
file(WRITE ${CMAKE_BINARY_DIR}/version.h.txt "#define ASEBA_BUILD_VERSION \"svn-${ASEBA_WC_REVISION}\"\n")
# copy the file to the final header only if the version changes
# reduces needless rebuilds
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different version.h.txt version.h)
add_custom_target(versionheader ALL DEPENDS ${CMAKE_BINARY_DIR}/version.h)
set_source_files_properties(${CMAKE_BINARY_DIR}/version.h PROPERTIES GENERATED TRUE HEADER_FILE_ONLY TRUE)
return()
endif (Subversion_FOUND AND HAS_SVN_REP)
##########
# detect git
##########
find_package(Git QUIET)
if (NOT GIT_FOUND)
find_program(GIT_EXECUTABLE git NO_CMAKE_FIND_ROOT_PATH)
if (GIT_EXECUTABLE)
set(GIT_FOUND TRUE)
endif (GIT_EXECUTABLE)
endif (NOT GIT_FOUND)
if (GIT_FOUND)
message("-- Git executable found")
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short --verify HEAD
OUTPUT_VARIABLE GIT_REV
ERROR_VARIABLE git_rev_error
RESULT_VARIABLE git_rev_result
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(${git_rev_result} EQUAL 0)
set(HAS_GIT_REP 1)
endif(${git_rev_result} EQUAL 0)
else (GIT_FOUND)
message("-- Git executable NOT found")
endif (GIT_FOUND)
# react accordingly
if (GIT_FOUND AND HAS_GIT_REP)
message("-- Git repository found")
set(HAS_DYN_VERSION)
# write a file with the GIT_REV define
file(WRITE ${CMAKE_BINARY_DIR}/version.h.txt "#define ASEBA_BUILD_VERSION \"git-${GIT_REV}\"\n")
# write NSIS version file (for Windows build)
file(READ "${PROJECT_SOURCE_DIR}/common/consts.h" COMMON_CONSTS_H)
string(REGEX MATCH [0-9]\\.[0-9]\\.[0-9] ASEBA_VERSION ${COMMON_CONSTS_H})
file(WRITE ${CMAKE_BINARY_DIR}/version.nsi "!define VERSION \"${ASEBA_VERSION}-git-${GIT_REV}\"\n")
# copy the file to the final header only if the version changes
# reduces needless rebuilds
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different version.h.txt version.h)
add_custom_target(versionheader ALL DEPENDS ${CMAKE_BINARY_DIR}/version.h)
set_source_files_properties(${CMAKE_BINARY_DIR}/version.h PROPERTIES GENERATED TRUE HEADER_FILE_ONLY TRUE)
return()
endif (GIT_FOUND AND HAS_GIT_REP)
##########
# default
##########
message("-- (svn / git executable not found OR not a svn / git repository) and no user version given, version is unknown.")
file(WRITE ${CMAKE_BINARY_DIR}/version.h "#define ASEBA_BUILD_VERSION \"unknown\"\n")