Skip to content

Commit

Permalink
Overhaul how VERSION.TXT is generated
Browse files Browse the repository at this point in the history
Generate VERSION.TXT at build time rather than configure time. Since
configuring only needs to happen infrequently, it was possible that the
contents would be significantly out of date if a user configures, then
after some time, fetches the latest changes and builds again. By moving
generation to build time, we ensure that the time stamp (which includes
a time and not just a day) is always maximally correct, and likewise
that the git SHA is correct.

Additionally, two other changes are made to the VERSION.TXT contents.
First, rather than just a time stamp, the default value is now an actual
version identifier following the usual pattern of experimental builds
(i.e. 0.0.YYYYMMDD.HHMMSS+git<sha8>). Second, if the environment
variable DRAKE_VERSION is set, its value overrides the value that would
otherwise appears in VERSION.TXT. This mechanism is intended to allow
official builds a way to inject release version numbers, as opposed to
the current mechanism of replacing VERSION.TXT.
  • Loading branch information
mwoehlke-kitware committed Mar 15, 2024
1 parent 9a8f606 commit 6827f07
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
29 changes: 10 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -563,33 +563,24 @@ configure_file(cmake/bazel.rc.in drake_build_cwd/.bazelrc @ONLY)
configure_file(cmake/WORKSPACE.in drake_build_cwd/WORKSPACE.bazel @ONLY)
file(CREATE_LINK "${PROJECT_SOURCE_DIR}/.bazeliskrc" drake_build_cwd/.bazeliskrc SYMBOLIC)

set(GIT_DIR "${PROJECT_SOURCE_DIR}/.git")
set(GIT_REVISION HEAD)

find_package(Git)

if(GIT_FOUND AND EXISTS "${GIT_DIR}")
execute_process(COMMAND
"${GIT_EXECUTABLE}" "--git-dir=${GIT_DIR}" rev-parse HEAD
RESULT_VARIABLE GIT_REV_PARSE_RESULT_VARIABLE
OUTPUT_VARIABLE GIT_REV_PARSE_OUTPUT_VARIABLE
OUTPUT_STRIP_TRAILING_WHITESPACE
)

if(GIT_REV_PARSE_RESULT_VARIABLE EQUAL 0)
set(GIT_REVISION "${GIT_REV_PARSE_OUTPUT_VARIABLE}")
endif()
endif()

string(TIMESTAMP BUILD_TIMESTAMP "%Y%m%d%H%M%S")

configure_file(tools/install/libdrake/VERSION.TXT.in VERSION.TXT @ONLY)
set(GIT_DIR "${PROJECT_SOURCE_DIR}/.git")

execute_process(
COMMAND "${Bazel_EXECUTABLE}" info --announce_rc
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/drake_build_cwd"
)

add_custom_target(drake_version ALL
COMMAND "${CMAKE_COMMAND}"
"-DGIT_DIR=${GIT_DIR}"
"-DGIT_EXECUTABLE=${GIT_EXECUTABLE}"
"-DINPUT_FILE=${PROJECT_SOURCE_DIR}/tools/install/libdrake/VERSION.TXT.in"
"-DOUTPUT_FILE=${PROJECT_BINARY_DIR}/VERSION.TXT"
-P "${PROJECT_SOURCE_DIR}/tools/install/libdrake/generate_version.cmake"
)

add_custom_target(drake_cxx_python ALL
COMMAND "${Bazel_EXECUTABLE}" build ${BAZEL_INSTALL_TARGET}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/drake_build_cwd"
Expand Down
2 changes: 1 addition & 1 deletion tools/install/libdrake/VERSION.TXT.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@BUILD_TIMESTAMP@ @GIT_REVISION@
@DRAKE_VERSION@ @GIT_REVISION@
26 changes: 26 additions & 0 deletions tools/install/libdrake/generate_version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
set(GIT_REVISION HEAD)
set(BUILD_IDENTIFIER unknown)

if(GIT_EXECUTABLE AND EXISTS "${GIT_DIR}")
execute_process(COMMAND
"${GIT_EXECUTABLE}" "--git-dir=${GIT_DIR}" rev-parse HEAD
RESULT_VARIABLE GIT_REV_PARSE_RESULT_VARIABLE
OUTPUT_VARIABLE GIT_REV_PARSE_OUTPUT_VARIABLE
OUTPUT_STRIP_TRAILING_WHITESPACE
)

if(GIT_REV_PARSE_RESULT_VARIABLE EQUAL 0)
set(GIT_REVISION "${GIT_REV_PARSE_OUTPUT_VARIABLE}")
string(SUBSTRING ${GIT_REVISION} 0 8 GIT_REVISION_SHORT)
set(BUILD_IDENTIFIER git${GIT_REVISION_SHORT})
endif()
endif()

if(DEFINED ENV{DRAKE_VERSION})
set(DRAKE_VERSION "$ENV{DRAKE_VERSION}")
else()
string(TIMESTAMP BUILD_TIMESTAMP "%Y%m%d.%H%M%S")
set(DRAKE_VERSION "0.0.${BUILD_TIMESTAMP}+${BUILD_IDENTIFIER}")
endif()

configure_file(${INPUT_FILE} ${OUTPUT_FILE} @ONLY)

0 comments on commit 6827f07

Please sign in to comment.