繁体   English   中英

为所有项目设置 OutDir 和 IntDir

[英]Setting OutDir and IntDir for all projects

我正在尝试为解决方案中的所有项目设置OutDirIntDir的默认位置。 如: <OutDir>$(SolutionDir)bld\$(Platform)\$(ProjectName)\$(Configuration)\</OutDir>

当我在Directory.Build.props文件中设置它时,它在 Visual Studio 属性对话框中看起来没问题; 但是当我构建时, $(ProjectName)部分是空的。 这告诉我,在 Directory.Build.props 中读取 OutDir 时,此宏不可用。

我尝试将其添加到Directory.Build.targets文件中,但它似乎完全被忽略了。

另一个目标是在初始更改后不修改任何 vcxproj 文件。 新项目会自动继承这些设置。

这可能吗? 也许我将设置放置在文件中的错误位置/顺序...?

这是 Directory.Build.props 的片段:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets">
  </ImportGroup>

  <!-- Define macros for easy and consistent access to various parts of the tree -->
  <PropertyGroup Label="UserMacros">
    <GslInclude>$(SolutionDir)packages\Microsoft.Gsl.0.1.2.1\build\native\include</GslInclude>
    <mySrcDir>$(SolutionDir)src\</mySrcDir>
    <myBldDir>$(SolutionDir)bld\</myBldDir>
    <myBldIntDir>$(SolutionDir)bld_int\</myBldIntDir>
  </PropertyGroup>
  <ItemDefinitionGroup />
  <ItemGroup>
    <BuildMacro Include="GslInclude"> <Value>$(GslInclude)</Value> </BuildMacro>
    <BuildMacro Include="mySrcDir"> <Value>$(mySrcDir)</Value> </BuildMacro>
    <BuildMacro Include="myBldDir"> <Value>$(myBldDir)</Value> </BuildMacro>
    <BuildMacro Include="myBldIntDir"> <Value>$(myBldIntDir)</Value> </BuildMacro>
  </ItemGroup>

  <!-- Platform Toolset, Windows SDK -->
  <PropertyGroup Label="Globals">
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
  </PropertyGroup>

  <!-- Default Compiler settings -->
  <ItemDefinitionGroup>
    <ClCompile>
      <!-- .... -->
    </ClCompile>
  </ItemDefinitionGroup>

  <!-- Default Folders for target output -->
  <PropertyGroup>
    <OutDir>$(myBldDir)$(Platform)\$(ProjectName)\$(Configuration)\</OutDir>
    <IntDir>$(myBldIntDir)$(Platform)\$(ProjectName)\$(Configuration)\</IntDir>
  </PropertyGroup>

</Project>

当然,我只需删除 proj 文件中的所有<OutDir><IntDir>设置。

我也希望能够根据 Visual Studio 的版本对设置设置条件。 我们的一些开发人员正在使用带有旧版 ToolSet 的 VS2017; 有些人正在使用最新的VS2019....

这是可能的,而且它实际上是在项目之间共享设置的推荐方式。 唯一需要注意的是,该方案对部分标签和包含的顺序/放置相当敏感。 以下应该适用于OutDirIntDir

Directory.Build.props

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <_PropertySheetDisplayName>Directory.Build</_PropertySheetDisplayName>
  </PropertyGroup>
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros">
    <!-- other user macros -->
    <OutDir> ... </OutDir>
    <IntDir> ... </IntDir>
  </PropertyGroup>
  <!-- rest of .props file --->

Project.vcxproj

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- ... -->
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <!-- include custom .props here, can also use full or relative path -->
  <ImportGroup Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    <Import Project="Directory.Build.props" />
  </ImportGroup>
  <!-- ... -->

感谢那些为此提供帮助的人。 我让它工作了——至少我的主要目标是为构建“输出”建立一个预定义的位置。

从这里参考信息: https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019并在 VS 道具文件中搜索,我找到了一个宏“MSBuildProjectName”这是在过程的早期设置的。

我在构建树的根目录中创建了一个 Directory.Build.props 文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets">
  </ImportGroup>

  <!-- Define macros for easy and consistent access to various parts of the tree -->
  <PropertyGroup Label="UserMacros">
    <GslInclude>$(SolutionDir)packages\Microsoft.Gsl.0.1.2.1\build\native\include</GslInclude>
    <myBldDir>$(SolutionDir)bld\</myBldDir>
    <myBldIntDir>$(SolutionDir)bld_int\</myBldIntDir>
  </PropertyGroup>
  <ItemDefinitionGroup />
  <ItemGroup>
    <BuildMacro Include="GslInclude"> <Value>$(GslInclude)</Value> </BuildMacro>
    <BuildMacro Include="myBldDir"> <Value>$(myBldDir)</Value> </BuildMacro>
    <BuildMacro Include="myBldIntDir"> <Value>$(myBldIntDir)</Value> </BuildMacro>
</BuildMacro>
    <!-- etc .... -->
  </ItemGroup>

  <!-- Platform Toolset, Windows SDK -->
  <PropertyGroup Label="Globals">
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
  </PropertyGroup>
  <PropertyGroup Label="Configuration">
    <PlatformToolset>v142</PlatformToolset>
  </PropertyGroup>

  <PropertyGroup>
    <IntDir>$(myBldIntDir)$(Platform)\$(MSBuildProjectName)\$(Configuration)\</IntDir>
    <OutDir>$(myBldDir)$(Platform)\$(MSBuildProjectName)\$(Configuration)\</OutDir>
  </PropertyGroup>

  <!-- lots more ....  -->
</Project>

此文件中的设置会影响文件夹结构中位于其“下方”的所有项目。 如果子文件夹需要不同的值,请将 Directory.Build.props 添加到导入“父”道具文件的文件夹中(可能是上面的几层):

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

  <ImportGroup Label="PropertySheets">
  </ImportGroup>

  <PropertyGroup>
    <IntDir>$(myBldIntDir)$(Platform)\Tests\$(MSBuildProjectName)\$(Configuration)\</IntDir>
    <OutDir>$(myBldDir)$(Platform)\Tests\$(MSBuildProjectName)\$(Configuration)\</OutDir>
  </PropertyGroup>

</Project>

'child' props 文件中的设置会覆盖其父项中设置的任何内容。

一旦我删除了 XML 项目文件中的所有<OutDir><IntDir>元素,“输出目录”和“中间目录”设置就会以常规字体显示(不是粗体,表示它没有继承),当然,当我构建,一切都进入合并的文件夹树; 远离源头。 还有一件很酷的事情:新项目将自动填充这些新的默认设置。

注意:@dxiv 指出某些设置可能更挑剔,需要更多的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM