Skip to content

Commit 4a58203

Browse files
committed
Add BUILD_COMPILER and BUILD_VERSION defines
1 parent 05f478e commit 4a58203

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ if(NOT BUILD_COMPILER)
2121
MESSAGE( STATUS "Compiler: " ${BUILD_COMPILER} )
2222
endif()
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
2548
get_directory_property(hasParent PARENT_DIRECTORY)
2649

@@ -130,3 +153,11 @@ endif()
130153
add_executable( picode_example picode_example.cpp )
131154
target_link_libraries( picode_example PRIVATE ${PROJECT_NAME} )
132155
set_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

Comments
 (0)