Make GetSVN.cmake do its VCS queries with native CMake code.
[opencl/llvm.git] / cmake / modules / GetSVN.cmake
1 # CMake project that writes Subversion revision information to a header.
2 #
3 # Input variables:
4 #   FIRST_SOURCE_DIR  - First source directory
5 #   FIRST_NAME        - The macro prefix for the first repository's info
6 #   SECOND_SOURCE_DIR - Second source directory (opt)
7 #   SECOND_NAME       - The macro prefix for the second repository's info (opt)
8 #   HEADER_FILE       - The header file to write
9 #
10 # The output header will contain macros FIRST_REPOSITORY and FIRST_REVISION,
11 # and SECOND_REPOSITORY and SECOND_REVISION if requested, where "FIRST" and
12 # "SECOND" are substituted with the names specified in the input variables.
14 # Chop off cmake/modules/GetSVN.cmake 
15 get_filename_component(LLVM_DIR "${CMAKE_SCRIPT_MODE_FILE}" PATH)
16 get_filename_component(LLVM_DIR "${LLVM_DIR}" PATH)
17 get_filename_component(LLVM_DIR "${LLVM_DIR}" PATH)
19 # Handle strange terminals
20 set(ENV{TERM} "dumb")
22 macro(get_source_info_svn path revision repository)
23   # FindSubversion does not work with symlinks. See PR 8437
24   if (NOT IS_SYMLINK "${path}")
25     find_package(Subversion)
26   endif()
27   if (Subversion_FOUND)
28     subversion_wc_info( ${path} Project )
29     if (Project_WC_REVISION)
30       set(${revision} ${Project_WC_REVISION} PARENT_SCOPE)
31     endif()
32     if (Project_WC_URL)
33       set(${repository} ${Project_WC_URL} PARENT_SCOPE)
34     endif()
35   endif()
36 endmacro()
38 macro(get_source_info_git_svn path revision repository)
39   find_program(git_executable NAMES git git.exe git.cmd)
40   if (git_executable)
41     execute_process(COMMAND ${git_executable} svn info
42       WORKING_DIRECTORY ${path}
43       TIMEOUT 5
44       RESULT_VARIABLE git_result
45       OUTPUT_VARIABLE git_output)
46     if (git_result EQUAL 0)
47       string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
48         "\\2" git_svn_rev "${git_output}")
49       set(${revision} ${git_svn_rev} PARENT_SCOPE)
50       string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
51         "\\2" git_url "${git_output}")
52       set(${repository} ${git_url} PARENT_SCOPE)
53     endif()
54   endif()
55 endmacro()
57 macro(get_source_info_git path revision repository)
58   find_program(git_executable NAMES git git.exe git.cmd)
59   if (git_executable)
60     execute_process(COMMAND ${git_executable} log -1 --pretty=format:%H
61       WORKING_DIRECTORY ${path}
62       TIMEOUT 5
63       RESULT_VARIABLE git_result
64       OUTPUT_VARIABLE git_output)
65     if (git_result EQUAL 0)
66       set(${revision} ${git_output} PARENT_SCOPE)
67     endif()
68     execute_process(COMMAND ${git_executable} remote -v
69       WORKING_DIRECTORY ${path}
70       TIMEOUT 5
71       RESULT_VARIABLE git_result
72       OUTPUT_VARIABLE git_output)
73     if (git_result EQUAL 0)
74       string(REGEX REPLACE "^(.*\n)?[^ \t]+[ \t]+([^ \t\n]+)[ \t]+\\(fetch\\).*"
75         "\\2" git_url "${git_output}")
76       set(${repository} "${git_url}" PARENT_SCOPE)
77     endif()
78   endif()
79 endmacro()
81 function(get_source_info path revision repository)
82   if (EXISTS "${path}/.svn")
83     get_source_info_svn("${path}" revision repository)
84   elseif (EXISTS "${path}/.git/svn")
85     get_source_info_git_svn("${path}" revision repository)
86   elseif (EXISTS "${path}/.git")
87     get_source_info_git("${path}" revision repository)
88   endif()
89 endfunction()
91 function(append_info name path)
92   get_source_info("${path}" revision repository)
93   string(STRIP "${revision}" revision)
94   string(STRIP "${repository}" repository)
95   file(APPEND "${HEADER_FILE}.txt"
96     "#define ${name}_REVISION \"${revision}\"\n")
97   file(APPEND "${HEADER_FILE}.txt"
98     "#define ${name}_REPOSITORY \"${repository}\"\n")
99 endfunction()
101 append_info(${FIRST_NAME} "${FIRST_SOURCE_DIR}")
102 if(DEFINED SECOND_SOURCE_DIR)
103   append_info(${SECOND_NAME} "${SECOND_SOURCE_DIR}")
104 endif()
106 # Copy the file only if it has changed.
107 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
108   "${HEADER_FILE}.txt" "${HEADER_FILE}")
109 file(REMOVE "${HEADER_FILE}.txt")