如何从C#项目的构建事件访问Visual Studio解决方案级平台?

时间:2023-01-16 15:53:15

We have a large VS 2010 solution that is mostly C# code but there are a few native DLLs that various C# projects depend upon (including our unit testing DLL). We're in the processing of trying to support both 32-bit and 64-bit versions of our libraries. So we're now build the native DLLs as 32-bit and 64-bit. The problem is that a lot of our C# project's have post-build events that copy the required native DLLs into the project's TargetDir. Now that we have two different versions of the native DLLs (32 and 64 bit), I need to be able to specify the correct dir to copy the native DLL from. I originally thought I could simply use $(Platform) in the path like so:

我们有一个大型的VS 2010解决方案,主要是C#代码,但有一些本地DLL,各种C#项目依赖(包括我们的单元测试DLL)。我们正在尝试支持32位和64位版本的库。所以我们现在将本机DLL构建为32位和64位。问题是我们的很多C#项目都有后期构建事件,它们将所需的本机DLL复制到项目的TargetDir中。现在我们有两个不同版本的本机DLL(32位和64位),我需要能够指定正确的dir来复制本机DLL。我原本以为我可以简单地在路径中使用$(Platform),如下所示:

copy $(SolutionDir)\NativeDll\$(Platform)\$(Configuration) $(TargetDir)

But that doesn't work because $(Platform) is the project's platform and not the solution level platform. In this case $(Platform) is "Any CPU". From what I can see looking at the post-build event macros in C# project, there doesn't appear to be a way to access the solution level platform that is being built. Is there a better way to accomplish my goal?

但这不起作用,因为$(Platform)是项目的平台,而不是解决方案级平台。在这种情况下,$(平台)是“任何CPU”。从我看到的C#项目中的后期构建事件宏看,似乎没有办法访问正在构建的解决方案级平台。有没有更好的方法来实现我的目标?

5 个解决方案

#1


2  

I believe the solution's platform, unlike that of the projects is simply text.

我相信解决方案的平台与项目不同的只是文本。

What I have down in the past is:

我过去的经历是:

  1. Delete Win32 and "mixed platform" from solution (and keep doing so after adding projects).

    从解决方案中删除Win32和“混合平台”(并在添加项目后继续这样做)。

  2. Set all C# DLLs to build as AnyCPU in solution platforms AnyCPU, x86, x64. (Do not delete AnyCPU if you want to be able to open in Blend or if you have any pure managed applications in the solution.)

    将所有C#DLL设置为在解决方案平台AnyCPU,x86,x64中构建为AnyCPU。 (如果您希望能够在Blend中打开或者解决方案中有任何纯托管应用程序,请不要删除AnyCPU。)

  3. Set C# EXEs and unit tests to build in x86 in x86 solution platform and x64 in x64 solution platform and not to build at all in AnyCPU solution platform.

    将C#EXE和单元测试设置为在x86解决方案平台中的x86和x64解决方案平台中的x64中构建,而不是在AnyCPU解决方案平台中构建。

  4. Set all natives to build in Win32 when solution is x86 and output to $(ProjectDir)\bin\x86\$(Configuration) and intermediate to same with obj in path instead of bin. x64 the same with x64 instead of x86 and Win32.

    当解决方案是x86并将输出设置为$(ProjectDir)\ bin \ x86 \ $(配置)并将其设置为与路径中的obj相同而不是bin时,将所有本机设置为Win32。 x64与x64相同,而不是x86和Win32。

  5. Set pre build events of C# EXEs and unit tests to copy native DLLs they are depended on from relative path with project's configuration name: $(Config)

    设置C#EXE的预构建事件和单元测试,以从项目配置名称的相对路径复制它们所依赖的本机DLL:$(Config)

  6. Set unit tests' class initialize to copy entire contents of tests bin dir (correct platform and configuration, of course) to tests' out dir. Use if #DEBUG and unsafe sizeof(IntPtr) to tell where to look for tests' bin dir.

    设置单元测试的类初始化以复制测试bin目录的整个内容(当然是正确的平台和配置)以测试'out dir。使用#DEBUG和unsafe sizeof(IntPtr)告诉在哪里查找测试的bin目录。

  7. Manually (using Notepad) add relative reference path to .csproj files outside solution that use x86/x64 assemblies from solution's deployment location, so path will include $(Platform) and $(Configuration) and will not be per user.

    手动(使用记事本)在解决方案外部添加相对于.csproj文件的相对引用路径,该解决方案使用来自解决方案部署位置的x86 / x64程序集,因此路径将包括$(Platform)和$(Configuration),而不是每个用户。

Microsoft: Better 32/64 bit support in Visual Studio would be really in place.

微软:Visual Studio中更好的32/64位支持将真正到位。

#2


2  

When I had to do this, I simply made all of my assemblies buildable as x86 or x64 rather than AnyCPU, and had two separate output packages. There's really no point in AnyCPU if you KNOW your process must be 32-bit or 64-bit a prori.

当我必须这样做时,我只是简单地将我的所有程序集构建为x86或x64而不是AnyCPU,并且有两个独立的输出包。如果你知道你的进程必须是32位或64位prori,那么AnyCPU真的没有意义。

#3


1  

I've not used it myself, but Build -> Batch Build is probably what you want. With it you can build multiple platforms.

我自己没有使用它,但Build - > Batch Build可能就是你想要的。有了它,您可以构建多个平台。

http://msdn.microsoft.com/en-us/library/169az28z.aspx

http://msdn.microsoft.com/en-us/library/169az28z.aspx

Of course, this doesn't actually enable you to access the 'platform' for the solution, but you don't need to since you'll build each platform separately.

当然,这实际上并不能让您访问解决方案的“平台”,但您不需要,因为您将单独构建每个平台。

Update: If you want to automate the build, create a batch file with the following contents

更新:如果要自动构建,请创建包含以下内容的批处理文件

"c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv" ../solution.sln /rebuild "platform"

where 'platform' is "Release|Any CPU", "Release|x86" etc. and repeat the line for each configuration you need to build. Use the Configuration Manager to set up each project for a a build for x86 and x64 and you should have what you want.

其中'platform'是“Release | Any CPU”,“Release | x86”等,并为您需要构建的每个配置重复该行。使用配置管理器为x86和x64的构建设置每个项目,您应该拥有所需的项目。

#4


0  

I don't think the 'Active Solution Configuration' has an equivalent macro property.

我不认为'Active Solution Configuration'具有等效的宏属性。

What I suggest is to manually add a custom property in all .csproj files, like this (see the new MyVar custom property added for each configuration/platform combination):

我建议在所有.csproj文件中手动添加自定义属性,如下所示(请参阅为每个配置/平台组合添加的新MyVar自定义属性):

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  ...
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
    <MyVar>MyDebugAnyCpu</MyVar>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <MyVar>MyReleaseAnyCpu</MyVar>
  </PropertyGroup>

You can use the 'Unload project' and 'Edit MyProject.csproj' menus to edit the .csprojet whil in Visual Studio. What's important to know is Visual Studio will not destroy these 'unknown' values even if you save it using the normal GUI editor.

您可以使用“卸载项目”和“编辑MyProject.csproj”菜单来编辑Visual Studio中的.csprojet。重要的是要知道即使使用普通的GUI编辑器保存它们,Visual Studio也不会破坏这些“未知”值。

Then in the post build event, you can use these values, for example:

然后在post build事件中,您可以使用这些值,例如:

copy $(SolutionDir)\$(MyVar)\$(Platform)\$(Configuration) $(TargetDir)

#5


0  

Francesco Pretto has an extension that helps with this. It seems to have some quirks and deficiencies, but it's a start.

Francesco Pretto有一个扩展,有助于此。它似乎有一些怪癖和不足,但它是一个开始。

http://visualstudiogallery.msdn.microsoft.com/619d92a2-4ead-410d-a105-135f7b4b4df9

http://visualstudiogallery.msdn.microsoft.com/619d92a2-4ead-410d-a105-135f7b4b4df9

With source on github:

在github上使用source:

https://github.com/ceztko/SolutionConfigurationName

https://github.com/ceztko/SolutionConfigurationName

#1


2  

I believe the solution's platform, unlike that of the projects is simply text.

我相信解决方案的平台与项目不同的只是文本。

What I have down in the past is:

我过去的经历是:

  1. Delete Win32 and "mixed platform" from solution (and keep doing so after adding projects).

    从解决方案中删除Win32和“混合平台”(并在添加项目后继续这样做)。

  2. Set all C# DLLs to build as AnyCPU in solution platforms AnyCPU, x86, x64. (Do not delete AnyCPU if you want to be able to open in Blend or if you have any pure managed applications in the solution.)

    将所有C#DLL设置为在解决方案平台AnyCPU,x86,x64中构建为AnyCPU。 (如果您希望能够在Blend中打开或者解决方案中有任何纯托管应用程序,请不要删除AnyCPU。)

  3. Set C# EXEs and unit tests to build in x86 in x86 solution platform and x64 in x64 solution platform and not to build at all in AnyCPU solution platform.

    将C#EXE和单元测试设置为在x86解决方案平台中的x86和x64解决方案平台中的x64中构建,而不是在AnyCPU解决方案平台中构建。

  4. Set all natives to build in Win32 when solution is x86 and output to $(ProjectDir)\bin\x86\$(Configuration) and intermediate to same with obj in path instead of bin. x64 the same with x64 instead of x86 and Win32.

    当解决方案是x86并将输出设置为$(ProjectDir)\ bin \ x86 \ $(配置)并将其设置为与路径中的obj相同而不是bin时,将所有本机设置为Win32。 x64与x64相同,而不是x86和Win32。

  5. Set pre build events of C# EXEs and unit tests to copy native DLLs they are depended on from relative path with project's configuration name: $(Config)

    设置C#EXE的预构建事件和单元测试,以从项目配置名称的相对路径复制它们所依赖的本机DLL:$(Config)

  6. Set unit tests' class initialize to copy entire contents of tests bin dir (correct platform and configuration, of course) to tests' out dir. Use if #DEBUG and unsafe sizeof(IntPtr) to tell where to look for tests' bin dir.

    设置单元测试的类初始化以复制测试bin目录的整个内容(当然是正确的平台和配置)以测试'out dir。使用#DEBUG和unsafe sizeof(IntPtr)告诉在哪里查找测试的bin目录。

  7. Manually (using Notepad) add relative reference path to .csproj files outside solution that use x86/x64 assemblies from solution's deployment location, so path will include $(Platform) and $(Configuration) and will not be per user.

    手动(使用记事本)在解决方案外部添加相对于.csproj文件的相对引用路径,该解决方案使用来自解决方案部署位置的x86 / x64程序集,因此路径将包括$(Platform)和$(Configuration),而不是每个用户。

Microsoft: Better 32/64 bit support in Visual Studio would be really in place.

微软:Visual Studio中更好的32/64位支持将真正到位。

#2


2  

When I had to do this, I simply made all of my assemblies buildable as x86 or x64 rather than AnyCPU, and had two separate output packages. There's really no point in AnyCPU if you KNOW your process must be 32-bit or 64-bit a prori.

当我必须这样做时,我只是简单地将我的所有程序集构建为x86或x64而不是AnyCPU,并且有两个独立的输出包。如果你知道你的进程必须是32位或64位prori,那么AnyCPU真的没有意义。

#3


1  

I've not used it myself, but Build -> Batch Build is probably what you want. With it you can build multiple platforms.

我自己没有使用它,但Build - > Batch Build可能就是你想要的。有了它,您可以构建多个平台。

http://msdn.microsoft.com/en-us/library/169az28z.aspx

http://msdn.microsoft.com/en-us/library/169az28z.aspx

Of course, this doesn't actually enable you to access the 'platform' for the solution, but you don't need to since you'll build each platform separately.

当然,这实际上并不能让您访问解决方案的“平台”,但您不需要,因为您将单独构建每个平台。

Update: If you want to automate the build, create a batch file with the following contents

更新:如果要自动构建,请创建包含以下内容的批处理文件

"c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv" ../solution.sln /rebuild "platform"

where 'platform' is "Release|Any CPU", "Release|x86" etc. and repeat the line for each configuration you need to build. Use the Configuration Manager to set up each project for a a build for x86 and x64 and you should have what you want.

其中'platform'是“Release | Any CPU”,“Release | x86”等,并为您需要构建的每个配置重复该行。使用配置管理器为x86和x64的构建设置每个项目,您应该拥有所需的项目。

#4


0  

I don't think the 'Active Solution Configuration' has an equivalent macro property.

我不认为'Active Solution Configuration'具有等效的宏属性。

What I suggest is to manually add a custom property in all .csproj files, like this (see the new MyVar custom property added for each configuration/platform combination):

我建议在所有.csproj文件中手动添加自定义属性,如下所示(请参阅为每个配置/平台组合添加的新MyVar自定义属性):

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  ...
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
    <MyVar>MyDebugAnyCpu</MyVar>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <MyVar>MyReleaseAnyCpu</MyVar>
  </PropertyGroup>

You can use the 'Unload project' and 'Edit MyProject.csproj' menus to edit the .csprojet whil in Visual Studio. What's important to know is Visual Studio will not destroy these 'unknown' values even if you save it using the normal GUI editor.

您可以使用“卸载项目”和“编辑MyProject.csproj”菜单来编辑Visual Studio中的.csprojet。重要的是要知道即使使用普通的GUI编辑器保存它们,Visual Studio也不会破坏这些“未知”值。

Then in the post build event, you can use these values, for example:

然后在post build事件中,您可以使用这些值,例如:

copy $(SolutionDir)\$(MyVar)\$(Platform)\$(Configuration) $(TargetDir)

#5


0  

Francesco Pretto has an extension that helps with this. It seems to have some quirks and deficiencies, but it's a start.

Francesco Pretto有一个扩展,有助于此。它似乎有一些怪癖和不足,但它是一个开始。

http://visualstudiogallery.msdn.microsoft.com/619d92a2-4ead-410d-a105-135f7b4b4df9

http://visualstudiogallery.msdn.microsoft.com/619d92a2-4ead-410d-a105-135f7b4b4df9

With source on github:

在github上使用source:

https://github.com/ceztko/SolutionConfigurationName

https://github.com/ceztko/SolutionConfigurationName