llvm-project/llvm/cmake/modules/GenerateVersionFromVCS.cmake
Han Zhu ee68f612ba Second try: [clang][cmake] Add options to pass in vcs repo and revision info
V2: Fix cmake error when -DLLVM_APPEND_VC_REV=OFF

Clang may be built in an environment where Git is not available. In our case,
Clang is part of a larger monorepo which is not Git-based, and
GenerateVersionFromVCS was not able to get source info.

Provide options to pass in repo and revision info from cmake.
```
cmake \
  -DCLANG_VC_REPOSITORY=abc://repo.url.com \
  -DCLANG_VC_REVISION=abcd1234 \
  ...
```
This would allow us to prepare the source info beforehand and pass it to the
clang binary.

Differential Revision: https://reviews.llvm.org/D148262
2023-04-17 10:54:28 -07:00

56 lines
1.7 KiB
CMake

# CMake script that writes version control information to a header.
#
# Input variables:
# NAMES - A list of names for each of the source directories.
# <NAME>_SOURCE_DIR - A path to source directory for each name in NAMES.
# HEADER_FILE - The header file to write
#
# The output header will contain macros <NAME>_REPOSITORY and <NAME>_REVISION,
# where "<NAME>" is substituted with the names specified in the input variables,
# for each of the <NAME>_SOURCE_DIR given.
get_filename_component(LLVM_CMAKE_DIR "${CMAKE_SCRIPT_MODE_FILE}" PATH)
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(VersionFromVCS)
# Handle strange terminals
set(ENV{TERM} "dumb")
function(append_info name revision repository)
if(revision)
file(APPEND "${HEADER_FILE}.tmp"
"#define ${name}_REVISION \"${revision}\"\n")
else()
file(APPEND "${HEADER_FILE}.tmp"
"#undef ${name}_REVISION\n")
endif()
if(repository)
file(APPEND "${HEADER_FILE}.tmp"
"#define ${name}_REPOSITORY \"${repository}\"\n")
else()
file(APPEND "${HEADER_FILE}.tmp"
"#undef ${name}_REPOSITORY\n")
endif()
endfunction()
foreach(name IN LISTS NAMES)
if(${name}_VC_REPOSITORY AND ${name}_VC_REVISION)
set(revision ${${name}_VC_REVISION})
set(repository ${${name}_VC_REPOSITORY})
elseif(DEFINED ${name}_SOURCE_DIR)
if (${name}_SOURCE_DIR)
get_source_info("${${name}_SOURCE_DIR}" revision repository)
endif()
else()
message(FATAL_ERROR "${name}_SOURCE_DIR is not defined")
endif()
append_info(${name} "${revision}" "${repository}")
endforeach()
# Copy the file only if it has changed.
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${HEADER_FILE}.tmp" "${HEADER_FILE}")
file(REMOVE "${HEADER_FILE}.tmp")