1 # CMake project that writes Subversion revision information to a header.
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
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
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)
28 # FindSubversion does not work with symlinks. See PR 8437
29 if (NOT IS_SYMLINK "${path}")
30 find_package(Subversion)
33 subversion_wc_info( ${path} Project )
34 if (Project_WC_REVISION)
35 set(${revision} ${Project_WC_REVISION} PARENT_SCOPE)
38 set(${repository} ${Project_WC_URL} PARENT_SCOPE)
43 macro(get_source_info_git_svn path revision repository)
44 find_program(git_executable NAMES git git.exe git.cmd)
46 execute_process(COMMAND ${git_executable} svn info
47 WORKING_DIRECTORY ${path}
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)
62 macro(get_source_info_git path revision repository)
63 find_program(git_executable NAMES git git.exe git.cmd)
65 execute_process(COMMAND ${git_executable} log -1 --pretty=format:%H
66 WORKING_DIRECTORY ${path}
68 RESULT_VARIABLE git_result
69 OUTPUT_VARIABLE git_output)
70 if (git_result EQUAL 0)
71 set(${revision} ${git_output} PARENT_SCOPE)
73 execute_process(COMMAND ${git_executable} remote -v
74 WORKING_DIRECTORY ${path}
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)
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)
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")
106 append_info(${FIRST_NAME} "${FIRST_SOURCE_DIR}")
107 if(DEFINED SECOND_SOURCE_DIR)
108 append_info(${SECOND_NAME} "${SECOND_SOURCE_DIR}")
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")