blob: d512bd292cf43f3f875e5063b5725e1fc6597f89 [file] [log] [blame]
Douglas Gregor96fa0fc2013-03-25 23:14:19 +00001# CMake project that writes Subversion revision information to a header.
2#
3# Input variables:
4# FIRST_SOURCE_DIR - First source directory
Jordan Rosec1800d22014-11-19 22:03:21 +00005# 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)
Douglas Gregor96fa0fc2013-03-25 23:14:19 +00008# HEADER_FILE - The header file to write
Jordan Rosec1800d22014-11-19 22:03:21 +00009#
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.
Douglas Gregor96fa0fc2013-03-25 23:14:19 +000013
Jordan Rosec1800d22014-11-19 22:03:21 +000014# Chop off cmake/modules/GetSVN.cmake
15get_filename_component(LLVM_DIR "${CMAKE_SCRIPT_MODE_FILE}" PATH)
16get_filename_component(LLVM_DIR "${LLVM_DIR}" PATH)
17get_filename_component(LLVM_DIR "${LLVM_DIR}" PATH)
Douglas Gregor96fa0fc2013-03-25 23:14:19 +000018
Jordan Rosec1800d22014-11-19 22:03:21 +000019# Handle strange terminals
20set(ENV{TERM} "dumb")
21
Paul Robinson56a31a32014-12-05 00:50:15 +000022macro(get_source_info_svn path revision repository)
Nico Webera33c5942014-12-10 00:10:21 +000023 # 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
Paul Robinson56a31a32014-12-05 00:50:15 +000028 # 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()
41endmacro()
42
43macro(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()
60endmacro()
61
62macro(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()
84endmacro()
85
86function(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()
94endfunction()
95
Jordan Rosec1800d22014-11-19 22:03:21 +000096function(append_info name path)
Paul Robinson56a31a32014-12-05 00:50:15 +000097 get_source_info("${path}" revision repository)
Jordan Rosec1800d22014-11-19 22:03:21 +000098 string(STRIP "${revision}" revision)
Jordan Rosec1800d22014-11-19 22:03:21 +000099 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")
104endfunction()
105
106append_info(${FIRST_NAME} "${FIRST_SOURCE_DIR}")
107if(DEFINED SECOND_SOURCE_DIR)
108 append_info(${SECOND_NAME} "${SECOND_SOURCE_DIR}")
Douglas Gregor96fa0fc2013-03-25 23:14:19 +0000109endif()
Jordan Rosec1800d22014-11-19 22:03:21 +0000110
111# Copy the file only if it has changed.
112execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
113 "${HEADER_FILE}.txt" "${HEADER_FILE}")
114file(REMOVE "${HEADER_FILE}.txt")
115