2023-05-03 00:43:43 +00:00
set ( TEMPLATE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.h.in" )
2023-05-01 16:23:47 +00:00
set ( HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h" )
set ( BUILD_NUMBER 0 )
set ( BUILD_COMMIT "unknown" )
2023-09-15 20:59:49 +00:00
set ( BUILD_COMPILER "unknown" )
set ( BUILD_TARGET "unknown" )
2023-05-01 16:23:47 +00:00
# Look for git
find_package ( Git )
if ( NOT Git_FOUND )
execute_process (
C O M M A N D w h i c h g i t
O U T P U T _ V A R I A B L E G I T _ E X E C U T A B L E
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
)
if ( NOT GIT_EXECUTABLE STREQUAL "" )
set ( Git_FOUND TRUE )
message ( STATUS "Found Git using 'which': ${GIT_EXECUTABLE}" )
else ( )
message ( WARNING "Git not found using 'find_package' or 'which'. Build info will not be accurate. Consider installing Git or ensuring it is in the PATH." )
endif ( )
endif ( )
# Get the commit count and hash
if ( Git_FOUND )
execute_process (
C O M M A N D $ { G I T _ E X E C U T A B L E } r e v - p a r s e - - s h o r t H E A D
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R }
O U T P U T _ V A R I A B L E H E A D
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
R E S U L T _ V A R I A B L E G I T _ H E A D _ R E S U L T
)
execute_process (
C O M M A N D $ { G I T _ E X E C U T A B L E } r e v - l i s t - - c o u n t H E A D
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R }
O U T P U T _ V A R I A B L E C O U N T
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
R E S U L T _ V A R I A B L E G I T _ C O U N T _ R E S U L T
)
if ( GIT_HEAD_RESULT EQUAL 0 AND GIT_COUNT_RESULT EQUAL 0 )
set ( BUILD_COMMIT ${ HEAD } )
set ( BUILD_NUMBER ${ COUNT } )
endif ( )
endif ( )
2023-09-15 20:59:49 +00:00
if ( GIT_HEAD_RESULT EQUAL 0 AND GIT_COUNT_RESULT EQUAL 0 )
set ( BUILD_COMMIT ${ HEAD } )
set ( BUILD_NUMBER ${ COUNT } )
endif ( )
execute_process (
C O M M A N D s h - c " $ @ - - v e r s i o n | h e a d - 1 " _ $ { C M A K E _ C _ C O M P I L E R }
O U T P U T _ V A R I A B L E O U T
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
R E S U L T _ V A R I A B L E R E S
)
if ( RES EQUAL 0 )
set ( BUILD_COMPILER ${ OUT } )
endif ( )
execute_process (
C O M M A N D $ { C M A K E _ C _ C O M P I L E R } - d u m p m a c h i n e
O U T P U T _ V A R I A B L E O U T
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
R E S U L T _ V A R I A B L E R E S
)
if ( RES EQUAL 0 )
set ( BUILD_TARGET ${ OUT } )
endif ( )
2023-05-01 16:23:47 +00:00
# Only write the header if it's changed to prevent unnecessary recompilation
if ( EXISTS ${ HEADER_FILE } )
2023-09-15 20:59:49 +00:00
file ( READ ${ HEADER_FILE } CONTENTS )
string ( REGEX MATCH "BUILD_COMMIT \" ( [^\ "]*)\" " _ ${ CONTENTS } )
set ( OLD_COMMIT ${ CMAKE_MATCH_1 } )
string ( REGEX MATCH "BUILD_COMPILER \" ( [^\ "]*)\" " _ ${ CONTENTS } )
set ( OLD_COMPILER ${ CMAKE_MATCH_1 } )
string ( REGEX MATCH "BUILD_TARGET \" ( [^\ "]*)\" " _ ${ CONTENTS } )
set ( OLD_TARGET ${ CMAKE_MATCH_1 } )
if (
N O T O L D _ C O M M I T S T R E Q U A L B U I L D _ C O M M I T O R
N O T O L D _ C O M P I L E R S T R E Q U A L B U I L D _ C O M P I L E R O R
N O T O L D _ T A R G E T S T R E Q U A L B U I L D _ T A R G E T
)
2023-05-01 16:23:47 +00:00
configure_file ( ${ TEMPLATE_FILE } ${ HEADER_FILE } )
endif ( )
else ( )
configure_file ( ${ TEMPLATE_FILE } ${ HEADER_FILE } )
endif ( )