Skip to content

Commit 535b926

Browse files
authored
CMake: Allow version to be explicitly set without requiring git (#384)
The main CMakeLists.txt was updated to allow explicitly setting the version for the project by setting the `ADS_VERSION` variable (e.g. cmake .. -DADS_VERSION=1.0.0). The default behavior is to determine the version by reading the information from git. Adding the option to override this variable at configure time allows the library to be built outside of its git repository, such as when the code is vendored directly into another project and added using `add_subdirectory`.
1 parent b15bc26 commit 535b926

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

CMakeLists.txt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
cmake_minimum_required(VERSION 3.5)
2-
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
3-
include(GetGitRevisionDescription)
4-
git_describe(GitTagVersion --tags)
5-
string(REGEX REPLACE "^([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GitTagVersion}")
6-
string(REGEX REPLACE "^[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GitTagVersion}")
7-
string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GitTagVersion}")
8-
set(VERSION_SHORT "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
2+
3+
# By default, the version information is extracted from the git index. However,
4+
# we can override this behavior by explicitly setting ADS_VERSION and
5+
# skipping the git checks. This is useful for cases where this project is being
6+
# used independently of its original git repo (e.g. vendored in another project)
7+
if(NOT ADS_VERSION)
8+
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
9+
include(GetGitRevisionDescription)
10+
git_describe(GitTagVersion --tags)
11+
string(REGEX REPLACE "^([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GitTagVersion}")
12+
string(REGEX REPLACE "^[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GitTagVersion}")
13+
string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GitTagVersion}")
14+
set(VERSION_SHORT "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
15+
else()
16+
string(REGEX MATCHALL "[\.]" VERSION_DOT_MATCHES ${ADS_VERSION})
17+
list(LENGTH VERSION_DOT_MATCHES VERSION_DOT_COUNT)
18+
if(VERSION_DOT_COUNT EQUAL 2)
19+
set(VERSION_SHORT ${ADS_VERSION})
20+
else()
21+
message(FATAL_ERROR "ADS_VERSION must be in major.minor.patch format, e.g. 3.8.1. Got ${ADS_VERSION}")
22+
endif()
23+
endif()
24+
25+
926
project(QtADS LANGUAGES CXX VERSION ${VERSION_SHORT})
27+
1028
option(BUILD_STATIC "Build the static library" OFF)
1129
option(BUILD_EXAMPLES "Build the examples" ON)
30+
1231
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
1332
set(ads_PlatformDir "x86")
1433
else()
1534
set(ads_PlatformDir "x64")
1635
endif()
36+
1737
add_subdirectory(src)
38+
1839
if(BUILD_EXAMPLES)
1940
add_subdirectory(examples)
2041
add_subdirectory(demo)

0 commit comments

Comments
 (0)