@@ -21,6 +21,29 @@ if(NOT BUILD_COMPILER)
2121 MESSAGE ( STATUS "Compiler: " ${BUILD_COMPILER} )
2222endif ()
2323
24+ # Check for git repository information
25+ IF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /.git)
26+ FIND_PACKAGE (Git)
27+ IF (GIT_FOUND)
28+ EXECUTE_PROCESS (
29+ COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
30+ WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR} "
31+ OUTPUT_VARIABLE "BUILD_VERSION"
32+ ERROR_QUIET
33+ OUTPUT_STRIP_TRAILING_WHITESPACE)
34+ SET (BUILD_VERSION "${BUILD_VERSION} " )
35+ MESSAGE ( STATUS "Git commit: ${BUILD_VERSION} " )
36+ ENDIF (GIT_FOUND)
37+ ENDIF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /.git)
38+
39+ # Force re-run CMake to set BUILD_VERSION on any git commit
40+ set_property (
41+ DIRECTORY
42+ APPEND
43+ PROPERTY CMAKE_CONFIGURE_DEPENDS
44+ .git/index
45+ )
46+
2447# Check if has parent directory
2548get_directory_property (hasParent PARENT_DIRECTORY )
2649
@@ -130,3 +153,11 @@ endif()
130153add_executable ( picode_example picode_example.cpp )
131154target_link_libraries ( picode_example PRIVATE ${PROJECT_NAME} )
132155set_target_properties ( picode_example PROPERTIES EXCLUDE_FROM_ALL TRUE )
156+
157+ # If git info available adds to picode_example executable as environment var
158+ if (DEFINED BUILD_VERSION)
159+ target_compile_definitions ( picode_example PRIVATE BUILD_VERSION=${BUILD_VERSION} )
160+ endif (DEFINED BUILD_VERSION)
161+
162+ # Add complier identification to picode_example executable as environment var
163+ target_compile_definitions ( picode_example PRIVATE BUILD_COMPILER=${BUILD_COMPILER} )
0 commit comments