blob: 18edbeabe3e4b5a2c6a9ffee4a147d2903789816 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001# Adds version control information to the variable VERS. For
2# determining the Version Control System used (if any) it inspects the
3# existence of certain subdirectories under SOURCE_DIR (if provided as an
4# extra argument, otherwise uses CMAKE_CURRENT_SOURCE_DIR).
5
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02006function(get_source_info path revision repository)
7 find_package(Git QUIET)
Andrew Walbran16937d02019-10-22 13:54:20 +01008 if(GIT_FOUND)
9 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
10 WORKING_DIRECTORY ${path}
11 RESULT_VARIABLE git_result
12 OUTPUT_VARIABLE git_output
13 ERROR_QUIET)
14 if(git_result EQUAL 0)
15 string(STRIP "${git_output}" git_output)
16 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020017 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
18 WORKING_DIRECTORY ${path}
19 RESULT_VARIABLE git_result
20 OUTPUT_VARIABLE git_output)
21 if(git_result EQUAL 0)
22 string(STRIP "${git_output}" git_output)
23 set(${revision} ${git_output} PARENT_SCOPE)
24 endif()
25 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref --symbolic-full-name @{upstream}
26 WORKING_DIRECTORY ${path}
27 RESULT_VARIABLE git_result
28 OUTPUT_VARIABLE git_output
29 ERROR_QUIET)
30 if(git_result EQUAL 0)
31 string(REPLACE "/" ";" branch ${git_output})
32 list(GET branch 0 remote)
Andrew Walbran16937d02019-10-22 13:54:20 +010033 else()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020034 set(remote "origin")
35 endif()
36 execute_process(COMMAND ${GIT_EXECUTABLE} remote get-url ${remote}
37 WORKING_DIRECTORY ${path}
38 RESULT_VARIABLE git_result
39 OUTPUT_VARIABLE git_output
40 ERROR_QUIET)
41 if(git_result EQUAL 0)
42 string(STRIP "${git_output}" git_output)
43 set(${repository} ${git_output} PARENT_SCOPE)
44 else()
45 set(${repository} ${path} PARENT_SCOPE)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046 endif()
47 endif()
Andrew Walbran16937d02019-10-22 13:54:20 +010048 else()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020049 message(WARNING "Git not found. Version cannot be determined.")
Andrew Walbran16937d02019-10-22 13:54:20 +010050 endif()
Andrew Walbran16937d02019-10-22 13:54:20 +010051endfunction()