Skip to content

Cleanup and relocate BUILD_VERSION calculation #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 17 additions & 50 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
cmake_minimum_required (VERSION 3.15)

project (mongo-c-driver C)
list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/build/cmake")
# Defines BUILD_VERSION, which we use throughout:
include (BuildVersion)

project (
mongo-c-driver
LANGUAGES C
# BUILD_VERSION_SIMPLE is a CMake-compatible version number that omits suffixes
VERSION "${BUILD_VERSION_SIMPLE}"
)

list (APPEND CMAKE_MODULE_PATH
"${PROJECT_SOURCE_DIR}/build/cmake"
)
# Set MONGOC_MAJOR_VERSION, MONGOC_MINOR_VERSION, etc.
include (ParseVersion)
ParseVersion ("${BUILD_VERSION}" MONGOC)
# Defines additional similar variables:
include (LoadVersion)
file (WRITE VERSION_CURRENT "${BUILD_VERSION}")
LoadVersion (VERSION_CURRENT MONGOC)

include (MongoSettings)
include (MongoPlatform)
Expand Down Expand Up @@ -264,52 +277,6 @@ if (NOT MSVC)
include (LLDLinker)
endif ()

set (BUILD_VERSION "0.0.0" CACHE STRING "Library version (for both libbson and libmongoc)")

include (ParseVersion)

# Set MONGOC_MAJOR_VERSION, MONGOC_MINOR_VERSION, etc.
if (BUILD_VERSION STREQUAL "0.0.0")
if (EXISTS ${PROJECT_SOURCE_DIR}/VERSION_CURRENT)
file (STRINGS ${PROJECT_SOURCE_DIR}/VERSION_CURRENT BUILD_VERSION)
message (STATUS "file VERSION_CURRENT contained BUILD_VERSION ${BUILD_VERSION}")
else ()
find_package (PythonInterp)
if (PYTHONINTERP_FOUND)
execute_process (
COMMAND ${PYTHON_EXECUTABLE} build/calc_release_version.py
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE CALC_RELEASE_VERSION
RESULT_VARIABLE CALC_RELEASE_VERSION_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (NOT CALC_RELEASE_VERSION_RESULT STREQUAL 0)
# If python failed above, stderr would tell the user about it
message (FATAL_ERROR
"BUILD_VERSION not specified and could not be calculated\
(script invocation failed); specify in CMake command, -DBUILD_VERSION=<version>"
)
else ()
set (BUILD_VERSION ${CALC_RELEASE_VERSION})
message (STATUS "calculated BUILD_VERSION ${BUILD_VERSION}")
endif ()
else ()
message (FATAL_ERROR
"BUILD_VERSION not specified and could not be calculated\
(Python was not found on the system); specify in CMake command, -DBUILD_VERSION=<version>"
)
endif ()
message (STATUS "storing BUILD_VERSION ${BUILD_VERSION} in file VERSION_CURRENT for later use")
file (WRITE ${PROJECT_SOURCE_DIR}/VERSION_CURRENT ${BUILD_VERSION})
endif ()
else ()
message (STATUS "storing BUILD_VERSION ${BUILD_VERSION} in file VERSION_CURRENT for later use")
file (WRITE ${PROJECT_SOURCE_DIR}/VERSION_CURRENT ${BUILD_VERSION})
endif ()

include (LoadVersion)
LoadVersion (${PROJECT_SOURCE_DIR}/VERSION_CURRENT MONGOC)

if ( (ENABLE_BUILD_DEPENDECIES STREQUAL OFF) AND (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) )
set (ENABLE_BUILD_DEPENDECIES ON)
endif ()
Expand Down
53 changes: 53 additions & 0 deletions build/cmake/BuildVersion.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
include_guard(GLOBAL)

include(MongoSettings)

# We use Python to calculate the BUILD_VERSION value
find_package(Python COMPONENTS Interpreter)

set(_CALC_VERSION_PY "${CMAKE_CURRENT_LIST_DIR}/../calc_release_version.py")

#[[
Attempts to find the current build version string. If VERSION_CURRENT exists
in the current source directory, uses that. Otherwise, runs calc_release_version.py
to compute the version from the Git history.

The computed build version is set in the parent scope according to `outvar`.
]]
function(compute_build_version outvar)
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
# If it is present, defer to the VERSION_CURRENT file:
set(ver_cur_file "${CMAKE_CURRENT_SOURCE_DIR}/VERSION_CURRENT")
if(EXISTS "${ver_cur_file}")
message(DEBUG "Using existing VERSION_CURRENT file as BUILD_VERSION [${ver_cur_file}]")
file(READ "${ver_cur_file}" version)
set("${outvar}" "${version}" PARENT_SCOPE)
return()
endif()
# Otherwise, we require Python:
if(NOT TARGET Python::Interpreter)
message(WARNING "No default build version could be calculated (Python was not found)")
set("${outvar}" "0.0.0-unknown+no-python-found")
return()
endif()
get_target_property(py Python::Interpreter IMPORTED_LOCATION)
message(STATUS "Computing the current release version...")
execute_process(
COMMAND "${py}" "${_CALC_VERSION_PY}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE output
RESULT_VARIABLE retc
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(retc)
message(FATAL_ERROR "Computing the build version failed! [${retc}]:\n${out}")
endif()
message(DEBUG "calc_release_version.py returned output: “${output}”")
set("${outvar}" "${output}" PARENT_SCOPE)
endfunction()

# Define the BUILD_VERSION:
compute_build_version(BUILD_VERSION)

# Set a BUILD_VERSION_SIMPLE, which is just a three-number-triple that CMake understands
string (REGEX REPLACE "([0-9]+\\.[0-9]+\\.[0-9]+).*$" "\\1" BUILD_VERSION_SIMPLE "${BUILD_VERSION}")