Skip to content

Commit c06942a

Browse files
Cleanup and relocate BUILD_VERSION calculation (#1382)
* Cleanup and relocate BUILD_VERSION calculation - The computation of BUILD_VERSION is now lifted above project(), which allows us to use that value in the project() call itself, giving us correct PROJECT_VERSION values throughout. - Define BUILD_VERSION using mongo_setting(), and use the calc_release_version.py to compute its default value. * Don't set a BUILD_VERSION cache var
1 parent 070b35a commit c06942a

File tree

2 files changed

+70
-50
lines changed

2 files changed

+70
-50
lines changed

CMakeLists.txt

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
cmake_minimum_required (VERSION 3.15)
22

3-
project (mongo-c-driver C)
3+
list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/build/cmake")
4+
# Defines BUILD_VERSION, which we use throughout:
5+
include (BuildVersion)
6+
7+
project (
8+
mongo-c-driver
9+
LANGUAGES C
10+
# BUILD_VERSION_SIMPLE is a CMake-compatible version number that omits suffixes
11+
VERSION "${BUILD_VERSION_SIMPLE}"
12+
)
413

5-
list (APPEND CMAKE_MODULE_PATH
6-
"${PROJECT_SOURCE_DIR}/build/cmake"
7-
)
14+
# Set MONGOC_MAJOR_VERSION, MONGOC_MINOR_VERSION, etc.
15+
include (ParseVersion)
16+
ParseVersion ("${BUILD_VERSION}" MONGOC)
17+
# Defines additional similar variables:
18+
include (LoadVersion)
19+
file (WRITE VERSION_CURRENT "${BUILD_VERSION}")
20+
LoadVersion (VERSION_CURRENT MONGOC)
821

922
include (MongoSettings)
1023
include (MongoPlatform)
@@ -264,52 +277,6 @@ if (NOT MSVC)
264277
include (LLDLinker)
265278
endif ()
266279

267-
set (BUILD_VERSION "0.0.0" CACHE STRING "Library version (for both libbson and libmongoc)")
268-
269-
include (ParseVersion)
270-
271-
# Set MONGOC_MAJOR_VERSION, MONGOC_MINOR_VERSION, etc.
272-
if (BUILD_VERSION STREQUAL "0.0.0")
273-
if (EXISTS ${PROJECT_SOURCE_DIR}/VERSION_CURRENT)
274-
file (STRINGS ${PROJECT_SOURCE_DIR}/VERSION_CURRENT BUILD_VERSION)
275-
message (STATUS "file VERSION_CURRENT contained BUILD_VERSION ${BUILD_VERSION}")
276-
else ()
277-
find_package (PythonInterp)
278-
if (PYTHONINTERP_FOUND)
279-
execute_process (
280-
COMMAND ${PYTHON_EXECUTABLE} build/calc_release_version.py
281-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
282-
OUTPUT_VARIABLE CALC_RELEASE_VERSION
283-
RESULT_VARIABLE CALC_RELEASE_VERSION_RESULT
284-
OUTPUT_STRIP_TRAILING_WHITESPACE
285-
)
286-
if (NOT CALC_RELEASE_VERSION_RESULT STREQUAL 0)
287-
# If python failed above, stderr would tell the user about it
288-
message (FATAL_ERROR
289-
"BUILD_VERSION not specified and could not be calculated\
290-
(script invocation failed); specify in CMake command, -DBUILD_VERSION=<version>"
291-
)
292-
else ()
293-
set (BUILD_VERSION ${CALC_RELEASE_VERSION})
294-
message (STATUS "calculated BUILD_VERSION ${BUILD_VERSION}")
295-
endif ()
296-
else ()
297-
message (FATAL_ERROR
298-
"BUILD_VERSION not specified and could not be calculated\
299-
(Python was not found on the system); specify in CMake command, -DBUILD_VERSION=<version>"
300-
)
301-
endif ()
302-
message (STATUS "storing BUILD_VERSION ${BUILD_VERSION} in file VERSION_CURRENT for later use")
303-
file (WRITE ${PROJECT_SOURCE_DIR}/VERSION_CURRENT ${BUILD_VERSION})
304-
endif ()
305-
else ()
306-
message (STATUS "storing BUILD_VERSION ${BUILD_VERSION} in file VERSION_CURRENT for later use")
307-
file (WRITE ${PROJECT_SOURCE_DIR}/VERSION_CURRENT ${BUILD_VERSION})
308-
endif ()
309-
310-
include (LoadVersion)
311-
LoadVersion (${PROJECT_SOURCE_DIR}/VERSION_CURRENT MONGOC)
312-
313280
if ( (ENABLE_BUILD_DEPENDECIES STREQUAL OFF) AND (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) )
314281
set (ENABLE_BUILD_DEPENDECIES ON)
315282
endif ()

build/cmake/BuildVersion.cmake

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
include_guard(GLOBAL)
2+
3+
include(MongoSettings)
4+
5+
# We use Python to calculate the BUILD_VERSION value
6+
find_package(Python COMPONENTS Interpreter)
7+
8+
set(_CALC_VERSION_PY "${CMAKE_CURRENT_LIST_DIR}/../calc_release_version.py")
9+
10+
#[[
11+
Attempts to find the current build version string. If VERSION_CURRENT exists
12+
in the current source directory, uses that. Otherwise, runs calc_release_version.py
13+
to compute the version from the Git history.
14+
15+
The computed build version is set in the parent scope according to `outvar`.
16+
]]
17+
function(compute_build_version outvar)
18+
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
19+
# If it is present, defer to the VERSION_CURRENT file:
20+
set(ver_cur_file "${CMAKE_CURRENT_SOURCE_DIR}/VERSION_CURRENT")
21+
if(EXISTS "${ver_cur_file}")
22+
message(DEBUG "Using existing VERSION_CURRENT file as BUILD_VERSION [${ver_cur_file}]")
23+
file(READ "${ver_cur_file}" version)
24+
set("${outvar}" "${version}" PARENT_SCOPE)
25+
return()
26+
endif()
27+
# Otherwise, we require Python:
28+
if(NOT TARGET Python::Interpreter)
29+
message(WARNING "No default build version could be calculated (Python was not found)")
30+
set("${outvar}" "0.0.0-unknown+no-python-found")
31+
return()
32+
endif()
33+
get_target_property(py Python::Interpreter IMPORTED_LOCATION)
34+
message(STATUS "Computing the current release version...")
35+
execute_process(
36+
COMMAND "${py}" "${_CALC_VERSION_PY}"
37+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
38+
OUTPUT_VARIABLE output
39+
RESULT_VARIABLE retc
40+
OUTPUT_STRIP_TRAILING_WHITESPACE
41+
)
42+
if(retc)
43+
message(FATAL_ERROR "Computing the build version failed! [${retc}]:\n${out}")
44+
endif()
45+
message(DEBUG "calc_release_version.py returned output: “${output}”")
46+
set("${outvar}" "${output}" PARENT_SCOPE)
47+
endfunction()
48+
49+
# Define the BUILD_VERSION:
50+
compute_build_version(BUILD_VERSION)
51+
52+
# Set a BUILD_VERSION_SIMPLE, which is just a three-number-triple that CMake understands
53+
string (REGEX REPLACE "([0-9]+\\.[0-9]+\\.[0-9]+).*$" "\\1" BUILD_VERSION_SIMPLE "${BUILD_VERSION}")

0 commit comments

Comments
 (0)