【问题标题】:How to configure a VS2012 solution for x86 and x64 simultaneously如何同时为 x86 和 x64 配置 VS2012 解决方案
【发布时间】:2013-04-10 08:42:48
【问题描述】:

我们有一个 VS2012 .NET 4 产品,它有 2 个不同的 SKU 的 A 和 B,我们目前只为 x86 构建。 我们也有常用的配置Debug 和Release,这意味着我们目前有 4 种配置。

  • 调试A
  • 调试B
  • ReleaseA
  • 版本B

查看其中一个 .csproj 文件,它看起来像这样

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugA|x86' ">
    <OutputPath>..\bin\DebugA\</OutputPath>
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseA|x86' ">
    <OutputPath>..\bin\ReleaseA\</OutputPath>
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugB|x86' ">
    <OutputPath>..\bin\DebugB\</OutputPath>
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseB|x86' ">
    <OutputPath>..\bin\ReleaseB\</OutputPath>
</PropertyGroup>

显然,添加 x64 会使这从 4 到 8 种不同的组合翻倍,而且 VS 似乎也可以随时添加 AnyCPU 平台配置。确保 30 多个项目中所有 8 个都正确配置需要在 VS 中大量点击,而且很容易出错。

我已经阅读了其他一些解决多目标问题的 SO 问题,以及在参考路径中使用 ${Platform} 为涉及的不同平台包含不同参考的建议之一。我想我可以为我的项目配置做类似的事情,所以我在尝试做多平台时尝试了这个:

<PropertyGroup Condition=" '$(Configuration)' == 'DebugA' Or '$(Configuration)' == 'DebugB' ">
     <OutputPath>..\bin\${Platform}\${Configuration}\</OutputPath>
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseA' Or '$(Configuration)' == 'ReleaseB' ">
     <OutputPath>..\bin\${Platform}\${Configuration}\</OutputPath>
</PropertyGroup> 

理论上,这应该可以满足我对所有 8 种不同组合的需求,只需两个块。但是现在查看 VS,我看不到 neitehr x86 或 x64 作为该项目的可用构建平台。看起来 VS 实际存储构建平台的唯一方法是将它们编码为属性组上的异常条件?这么说吧……

有没有办法制作一个“漂亮”的多平台 .csproj 以与 VS 一起工作?

我是否可以创建 .csprojs 然后决定不在 VS 中编辑它们,相信 msbuild 将使用正确的平台,即使 VS 无法在单个项目的属性窗口中显示任何平台?

编辑:

似乎这个问题有点混乱:明确地说,我想知道如何设置,维护和概述项目的配置,以及我的解决方案的构建配置,当有很多项目和八种组合时配置|平台。我知道如何手动执行此操作,但在 200 多个属性页面中的一个页面上我会失去理智或犯错误。

【问题讨论】:

  • 我认为您需要从“设置、维护和概述”中阐明您想要什么以及您今天发现的挑战。
  • 在一个慢速加载对话框页面上设置(config,platform,project)的一种组合的属性。由于这通常是一个包含 100 多种组合的矩阵,因此这很乏味且容易出错。我需要查看一个矩阵中的所有配置才能有效地维护它,或者我需要一种方法来使用示例中的变量来限制配置的组合数量。

标签: c# visual-studio msbuild


【解决方案1】:

如果您不介意必须手动更改项目文件一次,那么您可以将所有共享配置放在一个配置文件中并从每个项目中引用它。为此,您需要首先创建配置文件。该文件只是一个普通的 MsBuild 文件,其中包含您想要在所有项目之间共享的所有信息(如构建配置)。该文件大致如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5"
         DefaultTargets="Build"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <!-- VS information -->
        <ProductVersion>9.0.30729</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>

        <!-- Default configuration -->
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>

        <!-- Project directories -->
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <OutputPath>$(SolutionDir)\..\build\bin\$(Platform)\$(Configuration)\</OutputPath>
        <IntermediateOutputPath>$(SolutionDir)\..\build\temp\bin\obj\$(AssemblyName)\$(Platform)\$(Configuration)\</IntermediateOutputPath>

        <!-- Build configuration -->
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <DefineConstants>TRACE;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <DefineConstants>TRACE;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <DefineConstants>TRACE;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
  • 第一个PropertyGroup 定义了整体常量,即所有项目用于所有构建配置的常量。正如您所看到的OutputPath,您可以使用$(Platform) 和$(Configuration) 等构建变量来确定二进制文件等的位置。
  • 第一个PropertyGroup 也有一堆通常由Visual Studio 定义的设置,例如ProductVersion。从技术上讲,您不必移动它们,但如果您关心的话,移动它们确实可以减少项目文件中的混乱。
  • 以下部分定义了不同构建配置的不同设置。

一旦你定义了配置文件,假设它被称为BaseConfigurations.targets,那么你必须编辑你的项目文件。不幸的是,您必须浏览所有项目文件,但您只需要这样做一次。链接配置文件后,您可以通过更改配置文件来更改所有共享配置。

一个普通的项目文件大概是这样的:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{33017F71-5A1C-4113-9041-4DD3F58921D0}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>MyProject</RootNamespace>
    <AssemblyName>MyProject</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

为了链接配置文件,您需要:

  • 从第一个 PropertyGroup 中删除已在您的配置文件中定义的那些
  • 添加行&lt;SolutionDir Condition="'$(SolutionDir)' == '' or '$(SolutionDir)' == '*undefined*'"&gt;$(MSBuildProjectDirectory)\..&lt;/SolutionDir&gt;。如果您仅从 Visual Studio 构建(因为 Visual Studio 会自动定义 SolutionDir 变量),则这不是必需的,但如果您还想通过 MsBuild 构建项目,则这是必需的。这一行还假设每个项目都在它自己的子目录中,并且解决方案文件是每个项目文件的一个目录,即您的结构类似于:

    source
        MyProject
            MyProject.csproj
        MySolution.sln 
    
  • 在第一个 PropertyGroup 下方添加以下行 &lt;Import Project="$(SolutionDir)\BaseConfiguration.targets" /&gt;。这向 MsBuild(以及 Visual Studio)表明您要导入配置文件。

  • 删除构建配置
  • 在文件末尾删除行&lt;Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /&gt;。这是在您的配置文件中定义的,因此不再需要。

在所有这些之后,您的项目文件应该如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <SolutionDir Condition="'$(SolutionDir)' == '' or '$(SolutionDir)' == '*undefined*'">$(MSBuildProjectDirectory)\..</SolutionDir>
    <ProjectGuid>{33017F71-5A1C-4113-9041-4DD3F58921D0}</ProjectGuid>
    <OutputType>Library</OutputType>
    <RootNamespace>MyProject</RootNamespace>
    <AssemblyName>MyProject</AssemblyName>
  </PropertyGroup>
  <Import Project="$(SolutionDir)\BaseConfiguration.targets" />
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

注意事项:

  • 如果您采用这种方法,Visual Studio 应该能够识别所有不同的构建配置并允许您选择正确的配置。请注意,您可能需要进入解决方案的“配置管理器”才能在特定解决方案配置中包含或排除项目。
  • 如果您采用此方法,则无法再通过项目的属性页更改任何全局定义的属性。您必须在配置文件中进行更改,这将反映在每个项目的属性中。
  • 如果您使用的是 Visual Studio 2010 或更早版本,那么如果您对配置文件进行了更改,则需要重新加载解决方案(如果已打开),因为 Visual Studio 2010 不会检测到包含文件的更改。 Visual Studio 2012 应该能够检测到包含文件的更改。

【讨论】:

    【解决方案2】:

    您想要批量构建,您可以在其中选择要运行的不同构建。

    您可以使用工具 -> 选项 -> 键盘将其映射到键盘快捷键,然后搜索 Build.BatchBuild

    【讨论】:

    • 该功能不只是用于运行多个(不同)构建吗?我的问题是如何建立和维护一个大型的构建矩阵,而不必做重复和容易出错的工作。批量构建是否可以帮助我设置和概述 sln 和 csproj 文件,以获得具有 8 种配置/平台组合的大型解决方案?
    【解决方案3】:

    在我看来,在配置名称中结合平台和 SKU 是不明智的。在您的情况下,我建议仅坚持使用 Debug 和 Release 项目配置。您的解决方案应该有一个用于 SKU A 的项目和一个用于 SKU B 的单独项目(共享任何公共文件)。除了两个构建配置之外,每个项目还可以针对 x86 和 x64 平台。然后,您可以根据需要添加任意数量的解决方案配置来管理,而不会使单个项目配置变得更加复杂。

    【讨论】:

    • SKU:s 在代码方面是 98% 相同的,只有几个编译符号将它们分开,并且有 30 个项目有几千个类。因此,我认为将其复制到单独的 SKU 是不切实际的。与它如何与许多编译的大型库进行比较,例如对于 Silverlight/Desktop/Winphone 作为配置,通常使用 #if SILVERLIGHT。
    • 明确地说,平台就是平台。 “x86”和“x64”,通常配置必须控制其他任何东西,例如framework:silverlight/desktop/phone、sku:pro/lite、buildtype:debug/release等等。
    • 我明白你的意思。多个项目是组织解决方案的最有效方式,但听起来您有理由采用不同的方法。
    • 当您说“多个项目”时,您的意思是“多个解决方案”吗?我们已经有多个项目,其中 30 个!你的意思是我复制所有这些并得到60个,包括解决方案A中的30个和解决方案B中的30个?这不是我现在遇到的问题 double 吗,例如,当我需要更改输出路径时?那我需要开60个项目?
    • 解决方案的数量与管理项目设置的复杂性并不真正相关。您可以将同一个项目添加到多个解决方案中,这样做并不少见。对您而言重要的是项目结构。您不应将单个项目用于多个 SKU。无论有多少其他项目或这些项目包含多少解决方案,该单个项目都应该是两个独立的项目。
    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 2011-02-05
    • 1970-01-01
    • 2013-12-27
    • 2012-08-22
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    相关资源
    最近更新 更多