d512bd292cf43f3f875e5063b5725e1fc6597f89
[oota-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.
13
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)
18
19 # Handle strange terminals
20 set(ENV{TERM} "dumb")
21
22 macro(get_source_info_svn path revision repository)
23   # If svn is a bat file, find_program(Subversion) doesn't find it.
24   # Explicitly search for that here; Subversion_SVN_EXECUTABLE will override
25   # the find_program call in FindSubversion.cmake.
26   find_program(Subversion_SVN_EXECUTABLE NAMES svn svn.bat)
27
28   # FindSubversion does not work with symlinks. See PR 8437
29   if (NOT IS_SYMLINK "${path}")
30     find_package(Subversion)
31   endif()
32   if (Subversion_FOUND)
33     subversion_wc_info( ${path} Project )
34     if (Project_WC_REVISION)
35       set(${revision} ${Project_WC_REVISION} PARENT_SCOPE)
36     endif()
37     if (Project_WC_URL)
38       set(${repository} ${Project_WC_URL} PARENT_SCOPE)
39     endif()
40   endif()
41 endmacro()
42
43 macro(get_source_info_git_svn path revision repository)
44   find_program(git_executable NAMES git git.exe git.cmd)
45   if (git_executable)
46     execute_process(COMMAND ${git_executable} svn info
47       WORKING_DIRECTORY ${path}
48       TIMEOUT 5
49       RESULT_VARIABLE git_result
50       OUTPUT_VARIABLE git_output)
51     if (git_result EQUAL 0)
52       string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
53         "\\2" git_svn_rev "${git_output}")
54       set(${revision} ${git_svn_rev} PARENT_SCOPE)
55       string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
56         "\\2" git_url "${git_output}")
57       set(${repository} ${git_url} PARENT_SCOPE)
58     endif()
59   endif()
60 endmacro()
61
62 macro(get_source_info_git path revision repository)
63   find_program(git_executable NAMES git git.exe git.cmd)
64   if (git_executable)
65     execute_process(COMMAND ${git_executable} log -1 --pretty=format:%H
66       WORKING_DIRECTORY ${path}
67       TIMEOUT 5
68       RESULT_VARIABLE git_result
69       OUTPUT_VARIABLE git_output)
70     if (git_result EQUAL 0)
71       set(${revision} ${git_output} PARENT_SCOPE)
72     endif()
73     execute_process(COMMAND ${git_executable} remote -v
74       WORKING_DIRECTORY ${path}
75       TIMEOUT 5
76       RESULT_VARIABLE git_result
77       OUTPUT_VARIABLE git_output)
78     if (git_result EQUAL 0)
79       string(REGEX REPLACE "^(.*\n)?[^ \t]+[ \t]+([^ \t\n]+)[ \t]+\\(fetch\\).*"
80         "\\2" git_url "${git_output}")
81       set(${repository} "${git_url}" PARENT_SCOPE)
82     endif()
83   endif()
84 endmacro()
85
86 function(get_source_info path revision repository)
87   if (EXISTS "${path}/.svn")
88     get_source_info_svn("${path}" revision repository)
89   elseif (EXISTS "${path}/.git/svn")
90     get_source_info_git_svn("${path}" revision repository)
91   elseif (EXISTS "${path}/.git")
92     get_source_info_git("${path}" revision repository)
93   endif()
94 endfunction()
95
96 function(append_info name path)
97   get_source_info("${path}" revision repository)
98   string(STRIP "${revision}" revision)
99   string(STRIP "${repository}" repository)
100   file(APPEND "${HEADER_FILE}.txt"
101     "#define ${name}_REVISION \"${revision}\"\n")
102   file(APPEND "${HEADER_FILE}.txt"
103     "#define ${name}_REPOSITORY \"${repository}\"\n")
104 endfunction()
105
106 append_info(${FIRST_NAME} "${FIRST_SOURCE_DIR}")
107 if(DEFINED SECOND_SOURCE_DIR)
108   append_info(${SECOND_NAME} "${SECOND_SOURCE_DIR}")
109 endif()
110
111 # Copy the file only if it has changed.
112 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
113   "${HEADER_FILE}.txt" "${HEADER_FILE}")
114 file(REMOVE "${HEADER_FILE}.txt")
115