diff --git a/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake b/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake index ba599f4..c697a0c 100644 --- a/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake +++ b/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake @@ -22,14 +22,20 @@ endfunction() function(_vcpkg_checkout vcpkg_root vcpkg_ref) message(STATUS "vcpkg checkout to ${vcpkg_ref}") - execute_process( - COMMAND ${GIT_EXECUTABLE} fetch origin ${vcpkg_ref} - WORKING_DIRECTORY ${vcpkg_root} - RESULT_VARIABLE result) - - if(NOT result EQUAL "0") + if(EXISTS "${vcpkg_root}/.git/shallow") message( - FATAL_ERROR "${GIT_EXECUTABLE} fetch ${vcpkg_ref} failed with ${result}") + WARNING + "vcpkg is shallow now and unshallow to retrieve the Git tree object hash for a specific version port......" + ) + execute_process( + COMMAND ${GIT_EXECUTABLE} fetch --unshallow + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message( + FATAL_ERROR "${GIT_EXECUTABLE} fetch --unshallow failed with ${result}") + endif() endif() execute_process( @@ -47,7 +53,7 @@ endfunction() # clone function(_vcpkg_clone vcpkg_root vcpkg_repo vcpkg_ref) execute_process( - COMMAND ${GIT_EXECUTABLE} clone ${vcpkg_repo} ${vcpkg_root} --depth=1 + COMMAND ${GIT_EXECUTABLE} clone ${vcpkg_repo} ${vcpkg_root} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE result) @@ -123,7 +129,7 @@ function(_vcpkg_upgrade vcpkg_root vcpkg_repo vcpkg_ref) message(STATUS "Upgrade vcpkg") message(STATUS "vcpkg current commit: ${current_git_hash}") - message(STATUS "vcpkg release: ${vcpkg_ref}") + message(STATUS "vcpkg target commit: ${vcpkg_ref}") execute_process( COMMAND ${GIT_EXECUTABLE} remote set-url origin ${vcpkg_repo} @@ -150,9 +156,7 @@ endfunction() # find root function(_vcpkg_find_root cache_dir_name out_vcpkg_root) - if(DEFINED ENV{VCPKG_ROOT} AND NOT "$ENV{VCPKG_ROOT}" STREQUAL "") - set(root "$ENV{VCPKG_ROOT}") - elseif("${__vcpkg_bootstrap_host}" STREQUAL "Windows") + if("${__vcpkg_bootstrap_host}" STREQUAL "Windows") set(root "$ENV{LOCALAPPDATA}/vcpkg/projects/${cache_dir_name}/cache") else() set(root "$ENV{HOME}/.cache/vcpkg/projects/${cache_dir_name}") diff --git a/cmake/vcpkg/bootstrap/vcpkg_configure.cmake b/cmake/vcpkg/bootstrap/vcpkg_configure.cmake index 2f63cc3..527c070 100644 --- a/cmake/vcpkg/bootstrap/vcpkg_configure.cmake +++ b/cmake/vcpkg/bootstrap/vcpkg_configure.cmake @@ -36,8 +36,26 @@ function(_vcpkg_export_mode) message(STATUS "Disable manifest mode for using vcpkg exported artifacts") endfunction() +macro(detect_vcpkg) + # Respect environment variable VCPKG_ROOT and VCPKG_INSTALLATION_ROOT if set + if(DEFINED ENV{VCPKG_ROOT} AND NOT "$ENV{VCPKG_ROOT}" STREQUAL "") + set(_VCPKG_ROOT + "$ENV{VCPKG_ROOT}" + CACHE PATH "Vcpkg root directory" FORCE) + elseif(DEFINED ENV{VCPKG_INSTALLATION_ROOT} + AND NOT "$ENV{VCPKG_INSTALLATION_ROOT}" STREQUAL "") + set(_VCPKG_ROOT + "$ENV{VCPKG_INSTALLATION_ROOT}" + CACHE PATH "Vcpkg root directory" FORCE) + else() + unset(_VCPKG_ROOT CACHE) + endif() +endmacro() + # bootstrap and configure vcpkg macro(vcpkg_configure) + detect_vcpkg() + if(NOT VCPKG_EXPORT_MODE) _vcpkg_bootstrap(${ARGN}) _vcpkg_skip_install_on_reconfigure() diff --git a/cmake/vcpkg/vcpkg.toolchain.cmake b/cmake/vcpkg/vcpkg.toolchain.cmake index aae0787..149df3a 100644 --- a/cmake/vcpkg/vcpkg.toolchain.cmake +++ b/cmake/vcpkg/vcpkg.toolchain.cmake @@ -19,29 +19,13 @@ unset(IN_TRY_COMPILE) # Load the common settings include(${CMAKE_CURRENT_LIST_DIR}/scripts/build_type.cmake) -# Vcpkg build environment -file(READ ${CMAKE_SOURCE_DIR}/vcpkg.json _vcpkg_json) -string(JSON _builtin_baseline GET ${_vcpkg_json} builtin-baseline) - -# Respect environment variable VCPKG_ROOT and VCPKG_INSTALLATION_ROOT if set -if(DEFINED ENV{VCPKG_ROOT} AND NOT "$ENV{VCPKG_ROOT}" STREQUAL "") - set(_VCPKG_ROOT - "$ENV{VCPKG_ROOT}" - CACHE PATH "Vcpkg root directory" FORCE) -elseif(DEFINED ENV{VCPKG_INSTALLATION_ROOT} - AND NOT "$ENV{VCPKG_INSTALLATION_ROOT}" STREQUAL "") - set(_VCPKG_ROOT - "$ENV{VCPKG_INSTALLATION_ROOT}" - CACHE PATH "Vcpkg root directory" FORCE) -else() - unset(_VCPKG_ROOT CACHE) -endif() - include(${CMAKE_CURRENT_LIST_DIR}/bootstrap/vcpkg-config.cmake) set(VCPKG_VERBOSE ON CACHE BOOL "Vcpkg VCPKG_VERBOSE") +file(READ ${CMAKE_SOURCE_DIR}/vcpkg.json _vcpkg_json) +string(JSON _builtin_baseline GET ${_vcpkg_json} builtin-baseline) vcpkg_configure(CACHE_DIR_NAME ss-pybind11 REPO https://github.com/microsoft/vcpkg.git REF ${_builtin_baseline}) diff --git a/template/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake b/template/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake index ba599f4..c697a0c 100644 --- a/template/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake +++ b/template/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake @@ -22,14 +22,20 @@ endfunction() function(_vcpkg_checkout vcpkg_root vcpkg_ref) message(STATUS "vcpkg checkout to ${vcpkg_ref}") - execute_process( - COMMAND ${GIT_EXECUTABLE} fetch origin ${vcpkg_ref} - WORKING_DIRECTORY ${vcpkg_root} - RESULT_VARIABLE result) - - if(NOT result EQUAL "0") + if(EXISTS "${vcpkg_root}/.git/shallow") message( - FATAL_ERROR "${GIT_EXECUTABLE} fetch ${vcpkg_ref} failed with ${result}") + WARNING + "vcpkg is shallow now and unshallow to retrieve the Git tree object hash for a specific version port......" + ) + execute_process( + COMMAND ${GIT_EXECUTABLE} fetch --unshallow + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message( + FATAL_ERROR "${GIT_EXECUTABLE} fetch --unshallow failed with ${result}") + endif() endif() execute_process( @@ -47,7 +53,7 @@ endfunction() # clone function(_vcpkg_clone vcpkg_root vcpkg_repo vcpkg_ref) execute_process( - COMMAND ${GIT_EXECUTABLE} clone ${vcpkg_repo} ${vcpkg_root} --depth=1 + COMMAND ${GIT_EXECUTABLE} clone ${vcpkg_repo} ${vcpkg_root} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE result) @@ -123,7 +129,7 @@ function(_vcpkg_upgrade vcpkg_root vcpkg_repo vcpkg_ref) message(STATUS "Upgrade vcpkg") message(STATUS "vcpkg current commit: ${current_git_hash}") - message(STATUS "vcpkg release: ${vcpkg_ref}") + message(STATUS "vcpkg target commit: ${vcpkg_ref}") execute_process( COMMAND ${GIT_EXECUTABLE} remote set-url origin ${vcpkg_repo} @@ -150,9 +156,7 @@ endfunction() # find root function(_vcpkg_find_root cache_dir_name out_vcpkg_root) - if(DEFINED ENV{VCPKG_ROOT} AND NOT "$ENV{VCPKG_ROOT}" STREQUAL "") - set(root "$ENV{VCPKG_ROOT}") - elseif("${__vcpkg_bootstrap_host}" STREQUAL "Windows") + if("${__vcpkg_bootstrap_host}" STREQUAL "Windows") set(root "$ENV{LOCALAPPDATA}/vcpkg/projects/${cache_dir_name}/cache") else() set(root "$ENV{HOME}/.cache/vcpkg/projects/${cache_dir_name}") diff --git a/template/cmake/vcpkg/bootstrap/vcpkg_configure.cmake b/template/cmake/vcpkg/bootstrap/vcpkg_configure.cmake index 2f63cc3..527c070 100644 --- a/template/cmake/vcpkg/bootstrap/vcpkg_configure.cmake +++ b/template/cmake/vcpkg/bootstrap/vcpkg_configure.cmake @@ -36,8 +36,26 @@ function(_vcpkg_export_mode) message(STATUS "Disable manifest mode for using vcpkg exported artifacts") endfunction() +macro(detect_vcpkg) + # Respect environment variable VCPKG_ROOT and VCPKG_INSTALLATION_ROOT if set + if(DEFINED ENV{VCPKG_ROOT} AND NOT "$ENV{VCPKG_ROOT}" STREQUAL "") + set(_VCPKG_ROOT + "$ENV{VCPKG_ROOT}" + CACHE PATH "Vcpkg root directory" FORCE) + elseif(DEFINED ENV{VCPKG_INSTALLATION_ROOT} + AND NOT "$ENV{VCPKG_INSTALLATION_ROOT}" STREQUAL "") + set(_VCPKG_ROOT + "$ENV{VCPKG_INSTALLATION_ROOT}" + CACHE PATH "Vcpkg root directory" FORCE) + else() + unset(_VCPKG_ROOT CACHE) + endif() +endmacro() + # bootstrap and configure vcpkg macro(vcpkg_configure) + detect_vcpkg() + if(NOT VCPKG_EXPORT_MODE) _vcpkg_bootstrap(${ARGN}) _vcpkg_skip_install_on_reconfigure() diff --git a/template/cmake/vcpkg/vcpkg.toolchain.cmake.jinja b/template/cmake/vcpkg/vcpkg.toolchain.cmake.jinja index 690c74f..f9a795e 100644 --- a/template/cmake/vcpkg/vcpkg.toolchain.cmake.jinja +++ b/template/cmake/vcpkg/vcpkg.toolchain.cmake.jinja @@ -19,29 +19,13 @@ unset(IN_TRY_COMPILE) # Load the common settings include(${CMAKE_CURRENT_LIST_DIR}/scripts/build_type.cmake) -# Vcpkg build environment -file(READ ${CMAKE_SOURCE_DIR}/vcpkg.json _vcpkg_json) -string(JSON _builtin_baseline GET ${_vcpkg_json} builtin-baseline) - -# Respect environment variable VCPKG_ROOT and VCPKG_INSTALLATION_ROOT if set -if(DEFINED ENV{VCPKG_ROOT} AND NOT "$ENV{VCPKG_ROOT}" STREQUAL "") - set(_VCPKG_ROOT - "$ENV{VCPKG_ROOT}" - CACHE PATH "Vcpkg root directory" FORCE) -elseif(DEFINED ENV{VCPKG_INSTALLATION_ROOT} - AND NOT "$ENV{VCPKG_INSTALLATION_ROOT}" STREQUAL "") - set(_VCPKG_ROOT - "$ENV{VCPKG_INSTALLATION_ROOT}" - CACHE PATH "Vcpkg root directory" FORCE) -else() - unset(_VCPKG_ROOT CACHE) -endif() - include(${CMAKE_CURRENT_LIST_DIR}/bootstrap/vcpkg-config.cmake) set(VCPKG_VERBOSE ON CACHE BOOL "Vcpkg VCPKG_VERBOSE") +file(READ ${CMAKE_SOURCE_DIR}/vcpkg.json _vcpkg_json) +string(JSON _builtin_baseline GET ${_vcpkg_json} builtin-baseline) vcpkg_configure(CACHE_DIR_NAME {{ repo_name}} REPO https://github.com/microsoft/vcpkg.git REF ${_builtin_baseline})