forked from aseba-community/aseba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseVersion.cmake
91 lines (80 loc) · 3.33 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")
else (GIT_FOUND)
message("-- Git executable NOT found")
endif (GIT_FOUND)
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git/")
set(HAS_GIT_REP 1)
endif (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git/")
# react accordingly
if (GIT_FOUND AND HAS_GIT_REP)
message("-- Git repository found")
set(HAS_DYN_VERSION)
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(NOT ${git_rev_result} EQUAL 0)
message(SEND_ERROR "Command \"${GIT_EXECUTABLE} rev-parse --short --verify HEAD\" failed with output:\n${git_rev_error}")
endif(NOT ${git_rev_result} EQUAL 0)
# write a file with the GIT_REV define
file(WRITE ${CMAKE_BINARY_DIR}/version.h.txt "#define ASEBA_BUILD_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")