【问题标题】:Import targets during build process with MSBuild在构建过程中使用 MSBuild 导入目标
【发布时间】:2021-02-13 13:46:39
【问题描述】:

我使用 vcpkg 作为 Visual Studio C++ 项目的包管理器。我已经创建了初始目标来基本设置 vcpkg(克隆 vcpkg 存储库并下载 vcpkg.exe):

<Target Name="EnsureVcpkgBuild" BeforeTargets="PrepareForBuild">
    <Exec Command="git clone $(VcpkgRepo).git ..\packages\vcpkg" Condition="!Exists('..\packages\vcpkg\.git')" ContinueOnError="true" />
    <Exec Command="git --git-dir=..\packages\vcpkg\.git --work-tree=..\packages\vcpkg pull --ff-only" Condition="Exists('..\packages\vcpkg\.git')" ContinueOnError="true" />
    <Exec Command="..\packages\vcpkg\bootstrap-vcpkg.bat" Condition="!Exists('..\packages\vcpkg\vcpkg.exe')" ContinueOnError="true" />
    <PropertyGroup>
      <ErrorText>This project uses vcpkg that is missing on this computer. Manually download from $(VcpkgRepo) and place the contents in {0}. Or install git and add it to your path, for more information see https://git-scm.com. The missing file is {1}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\packages\vcpkg\vcpkg.exe')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\vcpkg\', '..\packages\vcpkg\vcpkg.exe'))" />
    <Error Condition="!Exists('..\packages\vcpkg\scripts\buildsystems\msbuild\vcpkg.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\vcpkg\', '..\packages\vcpkg\scripts\buildsystems\msbuild\vcpkg.targets'))" />
</Target>

在我的项目中,我正在导入 vcpkg 目标文件:

<ImportGroup>
    <Import Project="..\packages\vcpkg\scripts\buildsystems\msbuild\vcpkg.targets" Condition="Exists('..\packages\vcpkg\scripts\buildsystems\msbuild\vcpkg.targets')" />
</ImportGroup>

此设置可确保在 Visual Studio 中顺利构建,无需任何额外工作。但是,在使用 CI/CD 管道时,我必须先调用EnsureVcpkgBuild,然后再尝试构建项目。我能否以不必调用 EnsureVcpkgBuild 并启动构建自动设置 vcpkg 并导入 vcpkg.targets 的方式设置此项目?

【问题讨论】:

    标签: c++ windows visual-studio msbuild vcpkg


    【解决方案1】:

    可以直接使用nuget包自动实现。

    我认为你写了一个目标,将reop项目vcpkg下载到本地,然后从下载的vcpkg项目中手动导入核心目标。

    在本地可以轻松搞定,但是在cl管道上就不合适了。

    所以使用 nuget 可能会更容易。

    只需将 nuget 包 Vcpkg.Nuget 安装到您的 c++ 项目中即可。

    所有的导入目标都在 nuget 包中,可以通过安装 nuget 自动导入到您的项目中。

    当您在 CI/CD 管道上时,只需添加 nuget restore task 即可恢复管道上的 nuget 包。

    【讨论】:

    • 这是一个有趣的解决方案,因为我已经在使用 nuget restore 我可以使用这种方法。
    • Github页面显示该项目不再维护。虽然,我已经接受了你的回答(因为只有你回答了),但我建议不要使用这个。 Vcpkg 团队似乎正在努力将其与 Visual Studio 集成,所以将来我可能不必担心这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多