vcpkg国内镜像源替换
一、从Gitee上下载vcpkg(镜像仓库的代码一定要是最新的,这里提供几个)
git clone https://gitee.com/mirrors/vcpkg.git
git clone https://gitee.com/SpikeXue_admin/vcpkg.git
git clone https://gitee.com/Link-Not-Found/vcpkg.git
二、修改vcpkg-tool的下载地址
1、修改vcpkg.exe文件的下载地址
1)、在scripts文件夹
下找到bootstrap.ps1
、bootstrap.sh
和update-vcpkg-tool-metadata.ps1
文件,全局替换三个文件中的https://github.com/
,将其替换为https://mirror.ghproxy.com/https://github.com/
。
2)、在windows环境下,实际执行的脚本是boostrap.ps1,具体代码为:
这里我尝试将github.com
改为mirror.ghproxy.com/https://github.com
,发现后面安装vcpkg.exe时提示我url无效,因此我使用了另一种解决方案,话不多说,直接上代码:
# 将原来的代码注释掉
# if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64' -or $env:PROCESSOR_IDENTIFIER -match "ARMv[8,9] \(64-bit\)") {
# & "$scriptsDir/tls12-download-arm64.exe" "mirror.ghproxy.com/https://github.com" "/microsoft/vcpkg-tool/releases/download/$versionDate/vcpkg-arm64.exe" "$vcpkgRootDir\vcpkg.exe"
# } else {
# & "$scriptsDir/tls12-download.exe" "mirror.ghproxy.com/https://github.com" "/microsoft/vcpkg-tool/releases/download/$versionDate/vcpkg.exe" "$vcpkgRootDir\vcpkg.exe"
# }
# Write-Host ""
# if ($LASTEXITCODE -ne 0)
# {
# Write-Error "Downloading vcpkg.exe failed. Please check your internet connection, or consider downloading a recent vcpkg.exe from https://github.com/microsoft/vcpkg-tool with a browser."
# throw
# }
# Write-Host ""
# 检测处理器架构
if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64' -or $env:PROCESSOR_IDENTIFIER -match "ARMv[8,9] \(64-bit\)") {
$url = "https://mirror.ghproxy.com/https://github.com/microsoft/vcpkg-tool/releases/download/$versionDate/vcpkg-arm64.exe"
$outputFile = "$vcpkgRootDir\vcpkg.exe"
} else {
$url = "https://mirror.ghproxy.com/https://github.com/microsoft/vcpkg-tool/releases/download/$versionDate/vcpkg.exe"
$outputFile = "$vcpkgRootDir\vcpkg.exe"
}
try{
# 使用BITS启动文件传输
Start-BitsTransfer -Source $url -Destination $outputFile -ErrorAction Stop
Write-Host ""
}
catch {
Write-Error "Downloading vcpkg.exe failed. Please check your internet connection, or consider downloading a recent vcpkg.exe from https://github.com/microsoft/vcpkg-tool with a browser."
throw
}
修改代码后,下载vcpkg.exe时会出现进度条,偶尔还会报错,可能是连接超时(那也比直接从github下载快),请多试几次(指的是执行bootstrap-vcpkg.bat脚本,我测试下来,最多执行两次就行了)。
3)、bootstrap.sh对应的linux下的安装脚本。修改命令如下:
sed -i 's#https://github.com/#https://mirror.ghproxy.com/https://github.com/#g' /app/vcpkg/scripts/bootstrap.sh
注意:/app/vcpkg/xxx是我的vcpkg安装路径,需要替换为自己的!
2、修改其他vcpkg-tool的下载地址
在完成上面的修改后,其实就可以执行bootstrap-vcpkg.bat脚本去安装vcpkg.exe了,但是等你执行vcpkg install xxx时,有时又依赖于别的vcpkg-tool,比如powershell(虽然windows电脑都有powershell,但是你的版本不一定满足vcpkg的要求,它会重新下载),而这些vcpkg-tool的下载地址则是配置在vcpkgTools.xml
文件中的,老样子,将https://github.com/
替换为https://mirror.ghproxy.com/https://github.com/
,注意,vcpkgTools.xml文件中的工具的下载地址不全都是github.com开头的,剩余的如果有需要自己去寻找对应的国内镜像地址然后进行替换。我特意拉了vcpkg-tool的源代码,vcpkg.exe在执行时会读取vcpkgTools.xml中的配置,根据配置去指定下载地址下载对应的工具。
三、修改第三方库的下载地址
之前是告诉大家全局替换某个文件夹下的github.com开头的字符,但是经过反复测试,这种方法很笨,而且不是所有第三方库的源码都放在github上的,因此这次直接从根源上解决问题:修改下载脚本-----vcpkg_download_distfile.cmake
(在scripts/cmake文件夹下),找到vcpkg_download_distfile
函数,在vcpkg_list(SET urls_param)
后修改:
vcpkg_list(SET urls_param)
# 新增一个变量,存储修改后的url集合,用于在控制台中打印
vcpkg_list(SET arg_URLS_Real)
foreach(url IN LISTS arg_URLS)
# 将第三方库的地址更换为国内镜像源地址,这五个只是我目前找到的,如果有更多的需要替换的地址,形如:
# string(REPLACE <oldUrl> <newUrl> url "${url}"),按照这个格式继续添加即可
string(REPLACE "http://download.savannah.nongnu.org/releases/gta/" "https://marlam.de/gta/releases/" url "${url}")
string(REPLACE "https://github.com/" "https://mirror.ghproxy.com/https://github.com/" url "${url}")
string(REPLACE "https://ftp.gnu.org/" "https://mirrors.aliyun.com/" url "${url}")
string(REPLACE "https://raw.githubusercontent.com/" "https://mirror.ghproxy.com/https://raw.githubusercontent.com/" url "${url}")
string(REPLACE "http://ftp.gnu.org/pub/gnu/" "https://mirrors.aliyun.com/gnu/" url "${url}")
string(REPLACE "https://ftp.postgresql.org/pub/" "https://mirrors.tuna.tsinghua.edu.cn/postgresql/" url "${url}")
string(REPLACE "https://support.hdfgroup.org/ftp/lib-external/szip/2.1.1/src/" "https://distfiles.macports.org/szip/" url "${url}")
vcpkg_list(APPEND urls_param "--url=${url}")
# 存储新的第三方库下载地址
vcpkg_list(APPEND arg_URLS_Real "${url}")
endforeach()
if(NOT vcpkg_download_distfile_QUIET)
# message(STATUS "Downloading ${arg_URLS} -> ${arg_FILENAME}...")
# 控制台打印信息时,使用实际的下载地址,因为arg_URLS变量无法修改(我不会改,好像是改不了)
message(STATUS "Downloading ${arg_URLS_Real} -> ${arg_FILENAME}...")
endif()
四、执行bootstrap-vcpkg.bat脚本(其实第二步操作完就可以执行这个脚本了),执行完毕后,使用vcpkg install xxx安装你想要的第三方库(必须执行完第三步),再也不必因为网络问题而苦苦等待啦!
五、注意
如果在vcpkg install xxx的时候出现了第四步没有替换过的新的国内访问很慢的第三方库地址,那么请寻找对应的国内镜像,然后继续按照第四步说的,按照指定格式(string(REPLACE <oldUrl> <newUrl> url "${url}")
)继续修改vcpkg_download_distfile.cmake
文件,修改完毕后,重新执行vcpkg install命令。
六、Linux环境下修改上述文件的完整命令
sed -i 's#https://github.com/#https://mirror.ghproxy.com/https://github.com/#g' /app/vcpkg/scripts/bootstrap.sh \
&& sed -i 's#https://github.com/#https://mirror.ghproxy.com/https://github.com/#g' /app/vcpkg/scripts/vcpkgTools.xml \
&& sed -z -i 's| vcpkg_list(SET urls_param)\n foreach(url IN LISTS arg_URLS)\n vcpkg_list(APPEND urls_param "--url=${url}")\n endforeach()\n if(NOT vcpkg_download_distfile_QUIET)\n message(STATUS "Downloading ${arg_URLS} -> ${arg_FILENAME}...")\n endif()| vcpkg_list(SET urls_param)\n vcpkg_list(SET arg_URLS_Real)\n foreach(url IN LISTS arg_URLS)\n string(REPLACE "http://download.savannah.nongnu.org/releases/gta/" "https://marlam.de/gta/releases/" url "${url}")\n string(REPLACE "https://github.com/" "https://mirror.ghproxy.com/https://github.com/" url "${url}")\n string(REPLACE "https://ftp.gnu.org/" "https://mirrors.aliyun.com/" url "${url}")\n string(REPLACE "https://raw.githubusercontent.com/" "https://mirror.ghproxy.com/https://raw.githubusercontent.com/" url "${url}")\n string(REPLACE "http://ftp.gnu.org/pub/gnu/" "https://mirrors.aliyun.com/gnu/" url "${url}")\n string(REPLACE "https://ftp.postgresql.org/pub/" "https://mirrors.tuna.tsinghua.edu.cn/postgresql/" url "${url}")\n string(REPLACE "https://support.hdfgroup.org/ftp/lib-external/szip/2.1.1/src/" "https://distfiles.macports.org/szip/" url "${url}")\n vcpkg_list(APPEND urls_param "--url=${url}")\n vcpkg_list(APPEND arg_URLS_Real "${url}")\n endforeach()\n if(NOT vcpkg_download_distfile_QUIET)\n message(STATUS "Downloading ${arg_URLS_Real} -> ${arg_FILENAME}...")\n endif()|g' /app/vcpkg/scripts/cmake/vcpkg_download_distfile.cmake
注意:上面命令中的vcpkg工具的路径,我的是/app/vcpkg/xxx,需要替换成你自己的
七、补充说明(一定要看):
2025.03.06
1、新版本vcpkg取消了vcpkgTools.xml配置文件改为使用vcpkg-tools.json,因此上面的脚本需要将vcpkgTools.xml更换为vcpkg-tools.json,即形如sed -i 's#https://github.com/#https://mirror.ghproxy.com/https://github.com/#g' /app/vcpkg/scripts/vcpkgTools.xml
改为sed -i 's#https://github.com/#https://mirror.ghproxy.com/https://github.com/#g' /app/vcpkg/scripts/vcpkg-tools.json
2、https://mirror.ghproxy.com
加速地址已经失效,可以改为https://ghfast.top
3、新版本的vcpkg_download_distfile.cmake
脚本中的内容也变了,因此命令也要更改
windows环境:
108至110行:
foreach(url IN LISTS arg_URLS)
# 将第三方库的地址更换为国内镜像源地址,这五个只是我目前找到的,如果有更多的需要替换的地址,形如:
# string(REPLACE <oldUrl> <newUrl> url "${url}"),按照这个格式继续添加即可
string(REPLACE "http://download.savannah.nongnu.org/releases/gta/" "https://marlam.de/gta/releases/" url "${url}")
string(REPLACE "https://github.com/" "https://ghfast.top/https://github.com/" url "${url}")
string(REPLACE "https://ftp.gnu.org/" "https://mirrors.aliyun.com/" url "${url}")
string(REPLACE "https://raw.githubusercontent.com/" "https://ghfast.top/https://raw.githubusercontent.com/" url "${url}")
string(REPLACE "http://ftp.gnu.org/pub/gnu/" "https://mirrors.aliyun.com/gnu/" url "${url}")
string(REPLACE "https://ftp.postgresql.org/pub/" "https://mirrors.tuna.tsinghua.edu.cn/postgresql/" url "${url}")
string(REPLACE "https://support.hdfgroup.org/ftp/lib-external/szip/2.1.1/src/" "https://distfiles.macports.org/szip/" url "${url}")
vcpkg_list(APPEND params "--url=${url}")
endforeach()
linux环境:
sed -i 's#https://github.com/#https://ghfast.top/https://github.com/#g' /app/vcpkg/scripts/bootstrap.sh \
&& sed -i 's#https://github.com/#https://ghfast.top/https://github.com/#g' /app/vcpkg/scripts/vcpkg-tools.json \
&& sed -z -i 's| foreach(url IN LISTS arg_URLS)\n vcpkg_list(APPEND params "--url=${url}")\n endforeach()\n| foreach(url IN LISTS arg_URLS)\n string(REPLACE "http://download.savannah.nongnu.org/releases/gta/" "https://marlam.de/gta/releases/" url "${url}")\n string(REPLACE "https://github.com/" "https://ghfast.top/https://github.com/" url "${url}")\n string(REPLACE "https://ftp.gnu.org/" "https://mirrors.aliyun.com/" url "${url}")\n string(REPLACE "https://raw.githubusercontent.com/" "https://ghfast.top/https://raw.githubusercontent.com/" url "${url}")\n string(REPLACE "http://ftp.gnu.org/pub/gnu/" "https://mirrors.aliyun.com/gnu/" url "${url}")\n string(REPLACE "https://ftp.postgresql.org/pub/" "https://mirrors.tuna.tsinghua.edu.cn/postgresql/" url "${url}")\n string(REPLACE "https://support.hdfgroup.org/ftp/lib-external/szip/2.1.1/src/" "https://distfiles.macports.org/szip/" url "${url}")\n vcpkg_list(APPEND params "--url=${url}")\n endforeach()\n|g' /app/vcpkg/scripts/cmake/vcpkg_download_distfile.cmake