Skip to content

Commit

Permalink
[CMake] Added SWIFT_VC_REVISION_INCLUDE_DIR CMake option
Browse files Browse the repository at this point in the history
While local development, especially for stdlib or test cases,
Re-generating SwiftRevision.inc and re-linking the compiler is
unnecessary and annoying.
The new SWIFT_VC_REVISION_INCLUDE_DIR CMake option provides a way to control
that behavior, by limiting the directories from where VCS revision hash be retrieved.

From only lib/, include/, tools/

utils/build-script -RT --reconfigure \
  --swift-vc-revision-include-dir="lib;include;tools"

From whole repository excluding stdlib/

utils/build-script -RT --reconfigure \
  --swift-vc-revision-include-dir=".;:!stdlib"
  • Loading branch information
rintaro committed May 9, 2016
1 parent b4a5865 commit bfac557
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -79,6 +79,8 @@ set(SWIFT_COMPILER_VERSION "" CACHE STRING
"The internal version of the Swift compiler")
set(CLANG_COMPILER_VERSION "" CACHE STRING
"The internal version of the Clang compiler")
set(SWIFT_VC_REVISION_INCLUDE_DIR "" CACHE STRING
"Semi-colon separated directory names from where revision hash be retrieved")

# Indicate whether Swift should attempt to use the gold linker.
# This is not used on Darwin.
Expand Down
52 changes: 52 additions & 0 deletions cmake/modules/SwiftGitRevision.cmake
@@ -0,0 +1,52 @@
# CMake script that writes Git revision information to a header.
# NOTE: Not for include.
#
# Input variables:
# REPOSITORY_ROOT - root directory of the repository work tree
# NAME - The macro prefix for the repository's info
# HEADER_FILE - The header file to write
# SOURCE_PATH - Semi-colon separated list of paths from where query revision
#
# The output header will contain macros ${NAME}_REPOSITORY and ${NAME}_REVISION,

if(NOT DEFINED SOURCE_PATH)
set(SOURCE_PATH "${REPOSITORY_ROOT}")
endif()

find_program(git_executable NAMES git git.exe git.cmd)
if(git_executable)
execute_process(
COMMAND
${git_executable} log -1 --pretty=format:%H -- ${SOURCE_PATH}
WORKING_DIRECTORY ${REPOSITORY_ROOT}
TIMEOUT 5
RESULT_VARIABLE git_result
OUTPUT_VARIABLE git_output)
if (git_result EQUAL 0)
string(STRIP "${git_output}" revision)
endif()
execute_process(
COMMAND
${git_executable} remote -v
WORKING_DIRECTORY ${REPOSITORY_ROOT}
TIMEOUT 5
RESULT_VARIABLE git_result
OUTPUT_VARIABLE git_output)
if (git_result EQUAL 0)
string(REGEX REPLACE "^(.*\n)?[^ \t]+[ \t]+([^ \t\n]+)[ \t]+\\(fetch\\).*"
"\\2" git_url "${git_output}")
string(STRIP "${git_url}" repository)
endif()
endif()

if(DEFINED revision AND DEFINED repository)
file(APPEND "${HEADER_FILE}.tmp"
"#define ${NAME}_REVISION \"${revision}\"\n")
file(APPEND "${HEADER_FILE}.tmp"
"#define ${NAME}_REPOSITORY \"${repository}\"\n")
execute_process(COMMAND
${CMAKE_COMMAND} -E copy_if_different "${HEADER_FILE}.tmp" "${HEADER_FILE}")
endif()

# Remove tmp file unconditionally
file(REMOVE "${HEADER_FILE}.tmp")
24 changes: 17 additions & 7 deletions lib/Basic/CMakeLists.txt
Expand Up @@ -22,26 +22,35 @@ endfunction()
macro(find_first_existing_vc_file out_var path)
find_first_existing_file(${out_var}
"${path}/.git/logs/HEAD" # Git
"${path}/.svn/wc.db" # SVN 1.7
"${path}/.svn/entries" # SVN 1.6
# Support only Git
# "${path}/.svn/wc.db" # SVN 1.7
# "${path}/.svn/entries" # SVN 1.6
)
endmacro()

set(get_svn_script "${LLVM_MAIN_SRC_DIR}/cmake/modules/GetSVN.cmake")
set(get_revision_script
"${SWIFT_SOURCE_DIR}/cmake/modules/SwiftGitRevision.cmake")

function(generate_revision_inc revision_inc_var name dir)
find_first_existing_vc_file(dep_file "${dir}")
if(DEFINED dep_file)
if(ARGN)
set(src_path "${ARGN}")
else()
set(src_path "${dir}")
endif()

# Create custom target to generate the VC revision include.
set(revision_inc "${CMAKE_CURRENT_BINARY_DIR}/${name}Revision.inc")
string(TOUPPER ${name} upper_name)
add_custom_command(OUTPUT "${revision_inc}"
DEPENDS "${dep_file}" "${get_svn_script}"
COMMAND
${CMAKE_COMMAND} "-DFIRST_SOURCE_DIR=${dir}"
"-DFIRST_NAME=${upper_name}"
${CMAKE_COMMAND} "-DREPOSITORY_ROOT=${dir}"
"-DNAME=${upper_name}"
"-DHEADER_FILE=${revision_inc}"
-P "${get_svn_script}")
"-DSOURCE_PATH='${src_path}'"
-P "${get_revision_script}")

# Mark the generated header as being generated.
set_source_files_properties("${revision_inc}"
Expand All @@ -53,7 +62,8 @@ endfunction()

generate_revision_inc(llvm_revision_inc LLVM "${LLVM_MAIN_SRC_DIR}")
generate_revision_inc(clang_revision_inc Clang "${CLANG_MAIN_SRC_DIR}")
generate_revision_inc(swift_revision_inc Swift "${SWIFT_SOURCE_DIR}")
generate_revision_inc(swift_revision_inc Swift "${SWIFT_SOURCE_DIR}"
"${SWIFT_VC_REVISION_INCLUDE_DIR}")

set(version_inc_files
${llvm_revision_inc} ${clang_revision_inc} ${swift_revision_inc})
Expand Down
2 changes: 2 additions & 0 deletions utils/build-script-impl
Expand Up @@ -175,6 +175,7 @@ KNOWN_SETTINGS=(
swift-user-visible-version "3.0" "user-visible version of the Swift language"
swift-compiler-version "" "string that indicates a compiler version for Swift"
clang-compiler-version "" "string that indicates a compiler version for Clang"
swift-vc-revision-include-dir "" "a semicolon-separated list of directories from where swift VCS revision be retrieved"
embed-bitcode-section "0" "embed an LLVM bitcode section in stdlib/overlay binaries for supported platforms"
darwin-crash-reporter-client "" "whether to enable CrashReporter integration"
darwin-stdlib-install-name-dir "" "the directory of the install_name for standard library dylibs"
Expand Down Expand Up @@ -1666,6 +1667,7 @@ for deployment_target in "${HOST_TARGET}" "${CROSS_COMPILE_TOOLS_DEPLOYMENT_TARG
-DSWIFT_PATH_TO_LLVM_BUILD:PATH="${llvm_build_dir}"
-DSWIFT_PATH_TO_CMARK_SOURCE:PATH="${CMARK_SOURCE_DIR}"
-DSWIFT_PATH_TO_CMARK_BUILD:PATH="$(build_directory ${deployment_target} cmark)"
-DSWIFT_VC_REVISION_INCLUDE_DIR:STRING="${SWIFT_VC_REVISION_INCLUDE_DIR}"
)

if [[ "${CMAKE_GENERATOR}" == "Xcode" ]] ; then
Expand Down

0 comments on commit bfac557

Please sign in to comment.