Skip to content

Commit cbfd29c

Browse files
committed
Add PREFER_VERSION_FILE build option.
Improve CMake error on invalid version string.
1 parent 5058dc5 commit cbfd29c

File tree

5 files changed

+51
-15
lines changed

5 files changed

+51
-15
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(gpick)
33
option(ENABLE_NLS "compile with gettext support" true)
44
option(USE_GTK3 "use GTK3 instead of GTK2" true)
55
option(DEV_BUILD "use development flags" false)
6+
option(PREFER_VERSION_FILE "read version information from file instead of using GIT" false)
67
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
78
file(GLOB SOURCES
89
source/*.cpp source/*.h

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ gettext ([http://www.gnu.org/s/gettext](http://www.gnu.org/s/gettext)). Required
5454

5555
### Building
5656

57-
Using CMake:
57+
#### Using CMake:
5858

5959
`mkdir build && cd build` to create out-of-source build directory.
6060

61-
`cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local` to prepare build files for installation to '/usr/local'.
61+
`cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local` to prepare build files for installation to `/usr/local`.
6262

6363
`make` to compile all files.
6464

6565
`make install` to install executable and resources to `DESTDIR`. Default `DESTDIR` value is set by `CMAKE_INSTALL_PREFIX` variable.
6666

67-
Using SCons:
67+
#### Using SCons:
6868

6969
`scons` to compile all files and place executable file in `build/`.
7070

@@ -76,3 +76,4 @@ ENABLE\_NLS - compile with gettext support. Enabled by default.
7676

7777
USE\_GTK3 - use GTK3 instead of GTK2. Enabled by default.
7878

79+
PREFER\_VERSION\_FILE - read version information from file instead of using GIT. Disabled by default. This option enables unconditional `.version` file usage. `.version` file is included in release source archives and is a simple text file containing the following information in four lines: major/minor version, revision, commit hash and commit date.

SConscript

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ vars.Add('MSVS_VERSION', 'Visual Studio version', '11.0')
1717
vars.Add(BoolVariable('PREBUILD_GRAMMAR', 'Use prebuild grammar files', False))
1818
vars.Add(BoolVariable('USE_GTK3', 'Use GTK3 instead of GTK2', True))
1919
vars.Add(BoolVariable('DEV_BUILD', 'Use development flags', False))
20+
vars.Add(BoolVariable('PREFER_VERSION_FILE', 'Read version information from file instead of using GIT', False))
2021
vars.Update(env)
2122

2223
if env['LOCALEDIR'] == '':
@@ -39,7 +40,7 @@ if env['BUILD_TARGET'] == 'win32':
3940
env.Tool('mingw')
4041

4142
env.AddCustomBuilders()
42-
env.GetVersionInfo()
43+
env.GetVersionInfo(env['PREFER_VERSION_FILE'])
4344

4445
try:
4546
umask = os.umask(0o022)

cmake/Version.cmake

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
cmake_minimum_required(VERSION 3.12)
2-
find_program(GIT_EXECUTABLE git DOC "Git version control")
3-
mark_as_advanced(GIT_EXECUTABLE)
4-
find_file(GITDIR NAMES .git PATHS ${CMAKE_CURRENT_SOURCE_DIR} NO_DEFAULT_PATH NO_PACKAGE_ROOT_PATH)
5-
if (GIT_EXECUTABLE AND GITDIR)
2+
if (NOT PREFER_VERSION_FILE)
3+
find_program(GIT_EXECUTABLE git DOC "Git version control")
4+
mark_as_advanced(GIT_EXECUTABLE)
5+
find_file(GITDIR NAMES .git PATHS ${CMAKE_CURRENT_SOURCE_DIR} NO_DEFAULT_PATH NO_PACKAGE_ROOT_PATH)
6+
endif()
7+
if (NOT PREFER_VERSION_FILE AND GIT_EXECUTABLE AND GITDIR)
68
set(old_tz $ENV{TZ})
79
set(ENV{TZ} UTC)
810
execute_process(COMMAND "${GIT_EXECUTABLE}" describe "--match=gpick-*" "--match=v*" --always --long
@@ -14,6 +16,10 @@ if (GIT_EXECUTABLE AND GITDIR)
1416
if (version_error)
1517
message(FATAL_ERROR "Failed to get version: ${version_error}")
1618
endif()
19+
string(REGEX MATCH "^(gpick-|v)([(0-9)]+\\.[(0-9)(a-z)]+(\\.[(0-9)(a-z)]+)?(-[(0-9)]+)?).*" version_good ${version})
20+
if (NOT version_good)
21+
message(FATAL_ERROR "Invalid version string: ${version}")
22+
endif()
1723
string(REGEX REPLACE "^(gpick-|v)([(0-9)]+\\.[(0-9)(a-z)]+(\\.[(0-9)(a-z)]+)?(-[(0-9)]+)?).*" "\\2" version_parts ${version})
1824
string(REPLACE "-" ";" version_parts ${version_parts})
1925
list(GET version_parts 0 GPICK_BUILD_VERSION)
@@ -41,10 +47,32 @@ else()
4147
message(FATAL_ERROR "Version file \".version\" is required when GIT can not be used to get version information.")
4248
endif()
4349
file(STRINGS "${VERSION_FILE}" version_parts LIMIT_COUNT 4)
50+
list(LENGTH version_parts version_part_count)
51+
if (version_part_count LESS 1)
52+
message(FATAL_ERROR "Version file \".version\" must contain version number in first line.")
53+
endif()
4454
list(GET version_parts 0 GPICK_BUILD_VERSION)
45-
list(GET version_parts 1 GPICK_BUILD_REVISION)
46-
list(GET version_parts 2 GPICK_BUILD_HASH)
47-
list(GET version_parts 3 GPICK_BUILD_DATE)
55+
if (NOT GPICK_BUILD_VERSION)
56+
message(FATAL_ERROR "Version file \".version\" must contain version number in first line.")
57+
endif()
58+
if (version_part_count GREATER 1)
59+
list(GET version_parts 1 GPICK_BUILD_REVISION)
60+
endif()
61+
if (version_part_count GREATER 2)
62+
list(GET version_parts 2 GPICK_BUILD_HASH)
63+
endif()
64+
if (version_part_count GREATER 3)
65+
list(GET version_parts 3 GPICK_BUILD_DATE)
66+
endif()
67+
if (NOT GPICK_BUILD_REVISION)
68+
set(GPICK_BUILD_REVISION 0)
69+
endif()
70+
if (NOT GPICK_BUILD_HASH)
71+
set(GPICK_BUILD_HASH "0")
72+
endif()
73+
if (NOT GPICK_BUILD_DATE)
74+
set(GPICK_BUILD_DATE "-")
75+
endif()
4876
endif()
4977
if (GPICK_BUILD_REVISION GREATER 0)
5078
set(GPICK_BUILD_VERSION_FULL "${GPICK_BUILD_VERSION}-${GPICK_BUILD_REVISION}")

tools/gpick.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,15 @@ def GetSourceFiles(self, dir_exclude_pattern, file_exclude_pattern):
151151
MatchFiles(files, self.GetLaunchDir(), os.sep, dir_exclude_prog, file_exclude_prog)
152152
return files
153153

154-
def GetVersionInfo(self):
155-
try:
156-
(version, revision, hash, date) = getVersionInfo()
157-
except:
154+
def GetVersionInfo(self, preferVersionFile):
155+
haveVersionInfo = False
156+
if not preferVersionFile:
157+
try:
158+
(version, revision, hash, date) = getVersionInfo()
159+
haveVersionInfo = True
160+
except:
161+
pass
162+
if not haveVersionInfo:
158163
try:
159164
with open("../.version", "r", encoding = 'utf-8') as version_file:
160165
(version, revision, hash, date) = version_file.read().splitlines()

0 commit comments

Comments
 (0)