Home | History | Annotate | Download | only in llvm
      1 # CMake project that writes Subversion revision information to a header.
      2 #
      3 # Input variables:
      4 #   SOURCE_DIRS - A list of source directories.
      5 #   NAMES       - A list of macro prefixes for each of the source directories.
      6 #   HEADER_FILE - The header file to write
      7 #
      8 # The output header will contain macros <NAME>_REPOSITORY and <NAME>_REVISION,
      9 # where "<NAME>" and is substituted with the names specified in the input
     10 # variables, for each of the SOURCE_DIRS given.
     11 
     12 # Chop off cmake/modules/GetSVN.cmake
     13 get_filename_component(LLVM_DIR "${CMAKE_SCRIPT_MODE_FILE}" PATH)
     14 get_filename_component(LLVM_DIR "${LLVM_DIR}" PATH)
     15 get_filename_component(LLVM_DIR "${LLVM_DIR}" PATH)
     16 
     17 # Handle strange terminals
     18 set(ENV{TERM} "dumb")
     19 
     20 macro(get_source_info_svn path revision repository)
     21   # If svn is a bat file, find_program(Subversion) doesn't find it.
     22   # Explicitly search for that here; Subversion_SVN_EXECUTABLE will override
     23   # the find_program call in FindSubversion.cmake.
     24   find_program(Subversion_SVN_EXECUTABLE NAMES svn svn.bat)
     25 
     26   # FindSubversion does not work with symlinks. See PR 8437
     27   if (NOT IS_SYMLINK "${path}")
     28     find_package(Subversion)
     29   endif()
     30   if (Subversion_FOUND)
     31     subversion_wc_info( ${path} Project )
     32     if (Project_WC_REVISION)
     33       set(${revision} ${Project_WC_REVISION} PARENT_SCOPE)
     34     endif()
     35     if (Project_WC_URL)
     36       set(${repository} ${Project_WC_URL} PARENT_SCOPE)
     37     endif()
     38   endif()
     39 endmacro()
     40 
     41 macro(get_source_info_git_svn path revision repository)
     42   find_program(git_executable NAMES git git.exe git.cmd)
     43   if (git_executable)
     44     execute_process(COMMAND ${git_executable} svn info
     45       WORKING_DIRECTORY ${path}
     46       TIMEOUT 5
     47       RESULT_VARIABLE git_result
     48       OUTPUT_VARIABLE git_output)
     49     if (git_result EQUAL 0)
     50       string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
     51         "\\2" git_svn_rev "${git_output}")
     52       set(${revision} ${git_svn_rev} PARENT_SCOPE)
     53       string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
     54         "\\2" git_url "${git_output}")
     55       set(${repository} ${git_url} PARENT_SCOPE)
     56     endif()
     57   endif()
     58 endmacro()
     59 
     60 macro(get_source_info_git path revision repository)
     61   find_program(git_executable NAMES git git.exe git.cmd)
     62   if (git_executable)
     63     execute_process(COMMAND ${git_executable} log -1 --pretty=format:%H
     64       WORKING_DIRECTORY ${path}
     65       TIMEOUT 5
     66       RESULT_VARIABLE git_result
     67       OUTPUT_VARIABLE git_output)
     68     if (git_result EQUAL 0)
     69       set(${revision} ${git_output} PARENT_SCOPE)
     70     endif()
     71     execute_process(COMMAND ${git_executable} remote -v
     72       WORKING_DIRECTORY ${path}
     73       TIMEOUT 5
     74       RESULT_VARIABLE git_result
     75       OUTPUT_VARIABLE git_output)
     76     if (git_result EQUAL 0)
     77       string(REGEX REPLACE "^(.*\n)?[^ \t]+[ \t]+([^ \t\n]+)[ \t]+\\(fetch\\).*"
     78         "\\2" git_url "${git_output}")
     79       set(${repository} "${git_url}" PARENT_SCOPE)
     80     endif()
     81   endif()
     82 endmacro()
     83 
     84 function(get_source_info path revision repository)
     85   if (EXISTS "${path}/.svn")
     86     get_source_info_svn("${path}" revision repository)
     87   elseif (EXISTS "${path}/.git/svn/refs")
     88     get_source_info_git_svn("${path}" revision repository)
     89   elseif (EXISTS "${path}/.git")
     90     get_source_info_git("${path}" revision repository)
     91   endif()
     92 endfunction()
     93 
     94 function(append_info name path)
     95   get_source_info("${path}" revision repository)
     96   string(STRIP "${revision}" revision)
     97   string(STRIP "${repository}" repository)
     98   file(APPEND "${HEADER_FILE}.txt"
     99     "#define ${name}_REVISION \"${revision}\"\n")
    100   file(APPEND "${HEADER_FILE}.txt"
    101     "#define ${name}_REPOSITORY \"${repository}\"\n")
    102 endfunction()
    103 
    104 function(validate_inputs source_dirs names)
    105   list(LENGTH source_dirs source_dirs_length)
    106   list(LENGTH names names_length)
    107   if (NOT source_dirs_length EQUAL names_length)
    108     message(FATAL_ERROR
    109             "GetSVN.cmake takes two arguments: a list of source directories, "
    110             "and a list of names. Expected two lists must be of equal length, "
    111             "but got ${source_dirs_length} source directories and "
    112             "${names_length} names.")
    113   endif()
    114 endfunction()
    115 
    116 if (DEFINED SOURCE_DIRS AND DEFINED NAMES)
    117   validate_inputs("${SOURCE_DIRS}" "${NAMES}")
    118 
    119   list(LENGTH SOURCE_DIRS source_dirs_length)
    120   math(EXPR source_dirs_max_index ${source_dirs_length}-1)
    121   foreach(index RANGE ${source_dirs_max_index})
    122     list(GET SOURCE_DIRS ${index} source_dir)
    123     list(GET NAMES ${index} name)
    124     append_info(${name} ${source_dir})
    125   endforeach()
    126 endif()
    127 
    128 # Allow -DFIRST_SOURCE_DIR arguments until Clang migrates to the new
    129 # -DSOURCE_DIRS argument.
    130 if(DEFINED FIRST_SOURCE_DIR)
    131   append_info(${FIRST_NAME} "${FIRST_SOURCE_DIR}")
    132   if(DEFINED SECOND_SOURCE_DIR)
    133     append_info(${SECOND_NAME} "${SECOND_SOURCE_DIR}")
    134   endif()
    135 endif()
    136 
    137 # Copy the file only if it has changed.
    138 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
    139   "${HEADER_FILE}.txt" "${HEADER_FILE}")
    140 file(REMOVE "${HEADER_FILE}.txt")
    141 
    142