forked from manticoresoftware/manticoresearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphinxrev.cmake
143 lines (121 loc) · 5.35 KB
/
sphinxrev.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# this cmake file fully replaces all old things like svnxrev, hgxrev, etc scripts.
cmake_minimum_required ( VERSION 3.17 )
# guess version strings from current git repo
function ( guess_from_git )
if (NOT EXISTS "${MANTICORE_SOURCE_DIR}/.git")
return ()
endif ()
find_package ( Git QUIET )
if (NOT GIT_FOUND)
return ()
endif ()
# without this in some environments you can get error "detected dubious ownership in repository"
# `git config --global --add safe.directory '*'` in the docker image it runs in may not help. TODO: check why
execute_process ( COMMAND "${GIT_EXECUTABLE}" config --global --add safe.directory "${MANTICORE_SOURCE_DIR}")
# extract short has as SPH_GIT_COMMIT_ID
execute_process ( COMMAND "${GIT_EXECUTABLE}" log -1 --format=%h
WORKING_DIRECTORY "${MANTICORE_SOURCE_DIR}"
RESULT_VARIABLE res
OUTPUT_VARIABLE SPH_GIT_COMMIT_ID
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE )
set ( SPH_GIT_COMMIT_ID "${SPH_GIT_COMMIT_ID}" PARENT_SCOPE )
# extract timestamp and make number YYMMDDHH from it
execute_process ( COMMAND "${GIT_EXECUTABLE}" log -1 --date=format:"%y%m%d%H" --format=%cd
WORKING_DIRECTORY "${MANTICORE_SOURCE_DIR}"
RESULT_VARIABLE res
OUTPUT_VARIABLE GIT_TIMESTAMP_ID
OUTPUT_STRIP_TRAILING_WHITESPACE )
string ( SUBSTRING "${GIT_TIMESTAMP_ID}" 1 8 GIT_TIMESTAMP_ID )
set ( GIT_TIMESTAMP_ID ${GIT_TIMESTAMP_ID} PARENT_SCOPE )
# timestamp for reproducible packages
execute_process ( COMMAND "${GIT_EXECUTABLE}" log -1 --pretty=%ct
WORKING_DIRECTORY "${MANTICORE_SOURCE_DIR}"
RESULT_VARIABLE res
OUTPUT_VARIABLE GIT_EPOCH_ID
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE )
set ( SOURCE_DATE_EPOCH ${GIT_EPOCH_ID} PARENT_SCOPE )
# extract branch name (top of 'git status -s -b'), throw out leading '## '
execute_process ( COMMAND "${GIT_EXECUTABLE}" status -s -b
WORKING_DIRECTORY "${MANTICORE_SOURCE_DIR}"
RESULT_VARIABLE res
OUTPUT_VARIABLE GIT_BRANCH_ID
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE )
string ( REGEX REPLACE "\n.*$" "" GIT_BRANCH_ID "${GIT_BRANCH_ID}" )
string ( REPLACE "## " "" GIT_BRANCH_ID "${GIT_BRANCH_ID}" )
set ( GIT_BRANCH_ID "git branch ${GIT_BRANCH_ID}" PARENT_SCOPE )
endfunction ()
# guess version strings from template header file (git archive mark it there)
function ( extract_from_git_slug HEADER )
if (EXISTS "${HEADER}")
FILE ( STRINGS "${HEADER}" _CONTENT )
foreach (LINE ${_CONTENT})
# match definitions like - // GIT_*_ID VALUE
if ("${LINE}" MATCHES "^//[ \t]+(GIT_.*_ID)[ \t]\"(.*)\"")
set ( ${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" )
endif ()
endforeach ()
if (GIT_COMMIT_ID STREQUAL "$Format:%h$")
return () # no slug
endif ()
# commit id
set ( SPH_GIT_COMMIT_ID "${GIT_COMMIT_ID}" PARENT_SCOPE )
# timestamp
string ( REPLACE "-" "" GIT_TIMESTAMP_ID "${GIT_TIMESTAMP_ID}" )
string ( SUBSTRING "${GIT_TIMESTAMP_ID}" 2 6 GIT_TIMESTAMP_ID )
set ( GIT_TIMESTAMP_ID "${GIT_TIMESTAMP_ID}" PARENT_SCOPE )
# epoch for packaging
set ( SOURCE_DATE_EPOCH ${GIT_EPOCH_ID} PARENT_SCOPE )
# branch id
set ( GIT_BRANCH_ID "from tarball" PARENT_SCOPE )
endif ()
endfunction ()
# function definitions finished, execution starts from here
##################################
# first try to use binary git
guess_from_git ()
# 2-nd try - if we build from git archive. Correct hash and date provided then, but no branch
if (NOT SPH_GIT_COMMIT_ID)
extract_from_git_slug ( "${MANTICORE_SOURCE_DIR}/src/sphinxversion.h.in" )
endif ()
# extract version number string from sphinxversion.h.in
if (NOT VERNUMBERS)
FILE ( STRINGS "${MANTICORE_SOURCE_DIR}/src/sphinxversion.h.in" _STRINGS LIMIT_COUNT 500
REGEX "^#define[ \t]+VERNUMBERS.*" )
STRING ( REGEX REPLACE ".*\"(.*)\"(.*)$" "\\1" VERNUMBERS "${_STRINGS}" )
endif ()
if (NOT SPH_GIT_COMMIT_ID)
# nothing found, fall back to fake 'developer' version. That is ok if sources mirrored to another host without .git
message ( STATUS "Dev mode, no guess, using predefined version" )
if (NOT VERNUMBERS)
set ( VERNUMBERS "0.0.1" )
endif ()
set ( GIT_TIMESTAMP_ID "220512" )
set ( SPH_GIT_COMMIT_ID "DEADBEEF" )
set ( BUILD_TAG "devmode" )
set ( GIT_BRANCH_ID "developer version" )
set ( SOURCE_DATE_EPOCH "1607089638" )
set ( DEV_BUILD ON )
endif ()
set ( GDB_SOURCE_DIR "${MANTICORE_SOURCE_DIR}" )
# determine build tag as even/odd value of patch version
if (NOT BUILD_TAG)
string ( REGEX MATCH "[0-9]+.[0-9]+.([0-9]+)" _ ${VERNUMBERS} )
math ( EXPR oddvalue "${CMAKE_MATCH_1} % 2" OUTPUT_FORMAT DECIMAL )
if (oddvalue)
set ( BUILD_TAG "dev" )
set ( DEV_BUILD ON )
endif ()
endif ()
# All info collected (we need SPH_GIT_COMMIT_ID, GIT_TIMESTAMP_ID, GIT_BRANCH_ID and BUILD_TAG, if any)
set ( VERFILE "${MANTICORE_BINARY_DIR}/config/gen_sphinxversion.h" )
configure_file ( "${MANTICORE_SOURCE_DIR}/src/sphinxversion.h.in" "${VERFILE}" )
configure_file ( "${MANTICORE_SOURCE_DIR}/dist/CPackOptions.cmake.in" "${MANTICORE_BINARY_DIR}/config/CPackOptions.cmake" @ONLY )
# configure packaging
SET ( ENV{SOURCE_DATE_EPOCH} "${SOURCE_DATE_EPOCH}" ) # that makes builds reproducable
SET ( CPACK_PACKAGE_VERSION "${VERNUMBERS}-${GIT_TIMESTAMP_ID}-${SPH_GIT_COMMIT_ID}" )
string ( TOLOWER "${CPACK_PACKAGE_NAME}" CPACK_PACKAGE_NAME_LOWERCASE )
SET ( CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME_LOWERCASE}-${CPACK_PACKAGE_VERSION}" )
SET ( CPACK_RPM_PACKAGE_VERSION "${VERNUMBERS}_${GIT_TIMESTAMP_ID}.${SPH_GIT_COMMIT_ID}" )