| 1 |
find_package(Git QUIET REQUIRED) |
| 2 |
|
| 3 |
include(ExternalProject) |
| 4 |
|
| 5 |
set(srcRepo ${CMAKE_CURRENT_BINARY_DIR}/srcRepo) |
| 6 |
set(srcDir ${CMAKE_CURRENT_BINARY_DIR}/src) |
| 7 |
set(binDir ${CMAKE_CURRENT_BINARY_DIR}/build) |
| 8 |
file(MAKE_DIRECTORY ${srcRepo}) |
| 9 |
file(MAKE_DIRECTORY ${srcDir}) |
| 10 |
|
| 11 |
file(GLOB entries ${srcRepo}/*) |
| 12 |
file(REMOVE_RECURSE ${entries} ${binDir}) |
| 13 |
file(TOUCH ${srcRepo}/firstFile.txt) |
| 14 |
configure_file(${CMAKE_CURRENT_LIST_DIR}/FetchGitRefs/CMakeLists.txt |
| 15 |
${srcDir}/CMakeLists.txt COPYONLY) |
| 16 |
|
| 17 |
function(execGitCommand) |
| 18 |
execute_process( |
| 19 |
WORKING_DIRECTORY ${srcRepo} |
| 20 |
COMMAND ${GIT_EXECUTABLE} ${ARGN} |
| 21 |
COMMAND_ECHO STDOUT |
| 22 |
COMMAND_ERROR_IS_FATAL ANY |
| 23 |
) |
| 24 |
endfunction() |
| 25 |
|
| 26 |
function(configureAndBuild tag) |
| 27 |
execute_process(COMMAND ${CMAKE_COMMAND} |
| 28 |
-G ${CMAKE_GENERATOR} -T "${CMAKE_GENERATOR_TOOLSET}" |
| 29 |
-A "${CMAKE_GENERATOR_PLATFORM}" |
| 30 |
-D repoDir:PATH=${srcRepo} |
| 31 |
-D gitTag:STRING=${tag} |
| 32 |
-B ${binDir} |
| 33 |
-S ${srcDir} |
| 34 |
COMMAND_ECHO STDOUT |
| 35 |
COMMAND_ERROR_IS_FATAL ANY |
| 36 |
) |
| 37 |
|
| 38 |
execute_process(COMMAND ${CMAKE_COMMAND} --build ${binDir} --target fetcher |
| 39 |
WORKING_DIRECTORY ${binDir} |
| 40 |
COMMAND_ECHO STDOUT |
| 41 |
COMMAND_ERROR_IS_FATAL ANY |
| 42 |
) |
| 43 |
endfunction() |
| 44 |
|
| 45 |
# Setup a fresh source repo with a predictable default branch across all |
| 46 |
# git versions |
| 47 |
execGitCommand(-c init.defaultBranch=master init) |
| 48 |
execGitCommand(config --add user.email "[email protected]") |
| 49 |
execGitCommand(config --add user.name testauthor) |
| 50 |
execGitCommand(config commit.gpgsign "false") |
| 51 |
|
| 52 |
# Create the initial repo structure |
| 53 |
execGitCommand(add firstFile.txt) |
| 54 |
execGitCommand(commit -m "First file") |
| 55 |
|
| 56 |
message(STATUS "First configure-and-build") |
| 57 |
configureAndBuild(master) |
| 58 |
|
| 59 |
# Create a tagged commit that is not on any branch. With git 2.20 or later, |
| 60 |
# this commit won't be fetched without the --tags option. |
| 61 |
file(TOUCH ${srcRepo}/secondFile.txt) |
| 62 |
execGitCommand(add secondFile.txt) |
| 63 |
execGitCommand(commit -m "Second file") |
| 64 |
execGitCommand(tag -a -m "Adding tag" tag_of_interest) |
| 65 |
execGitCommand(reset --hard HEAD~1) |
| 66 |
|
| 67 |
message(STATUS "Configure-and-build, update to tag") |
| 68 |
configureAndBuild(tag_of_interest) |
| 69 |
|
| 70 |
# Do the same, but this time for a commit hash |
| 71 |
file(TOUCH ${srcRepo}/thirdFile.txt) |
| 72 |
execGitCommand(add thirdFile.txt) |
| 73 |
execGitCommand(commit -m "Third file") |
| 74 |
execGitCommand(tag -a -m "Adding another tag" check_for_hash) |
| 75 |
execGitCommand(reset --hard HEAD~1) |
| 76 |
execute_process( |
| 77 |
WORKING_DIRECTORY ${srcRepo} |
| 78 |
COMMAND ${GIT_EXECUTABLE} rev-parse check_for_hash |
| 79 |
COMMAND_ERROR_IS_FATAL ANY |
| 80 |
OUTPUT_VARIABLE commit_hash |
| 81 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 82 |
) |
| 83 |
|
| 84 |
message(STATUS "Configure-and-build, update to commit hash ${commit_hash}") |
| 85 |
configureAndBuild(${commit_hash}) |