在 Visual Studio 中使用自动构建进行条件编译

发布于 2024-07-13 23:51:29 字数 407 浏览 6 评论 0原文

这就是我想要做的:

  • 单个构建脚本
  • 该脚本从同一个 Visual Studio 项目构建两个可执行文件。
  • 第一个编译的 .exe 有少量代码禁用
  • 另一个已编译的 .exe 已启用所有内容。

我一直在阅读条件编译,这可以解决我需要启用/禁用代码块。

我只是不知道如何使用 msbuild 从构建脚本控制条件编译。

有没有办法从构建脚本或其他方式操纵条件编译变量来完成我想要做的事情?

Here's what I'm trying to do:

  • A single build script
  • That script builds two executables from the same Visual Studio project.
  • The first compiled .exe has a small amount of code disabled.
  • The other compiled .exe has everything enabled.

I've been reading up on conditional compilation and that takes care of my needs as far as enabling/disabling blocks of code.

I just can't figure out how to control conditional compilation from a build script using msbuild.

Is there a way to manipulate conditional compilation variables from a build script or some other way to accomplish what I'm trying to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海未深 2024-07-20 23:51:29

在项目文件中使用构建配置。 在根据配置选择性包含的 PropertyGroup 中设置参数。 然后,该配置还可以定义程序集的两个不同版本的输出路径。

对于需要删除某些代码的版本,请使用包含 PropertyGroup 的配置。

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'CompiledOutDebug|AnyCPU' ">
   <DefineConstants>$(DefineConstants);MY_CONDITIONAL_COMPILATION_CONSTANT</DefineConstants>
</PropertyGroup>

然后使用 MSBuild 脚本调用项目 MSBuild 脚本两次,并使用 MSBuild 任务的 Properties 属性来指定要生成的配置:

<Target Name="Build">
    <MSBuild Projects="MyProject.csproj;" 
             Targets="Build" 
             Properties="Configuration=Release" />
    <MSBuild Projects="MyProject.csproj"
             Targets="Build"
             Properties="Configuration=CompiledOutDebug" />
  </Target>

Use build configurations in your project file. Set the parameters in a PropertyGroup that is optionally included based on the configuration. The configuration can then also define the output path for the two different versions of the assembly.

For the version that needs to remove some code use a configuration that includes the PropertyGroup.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'CompiledOutDebug|AnyCPU' ">
   <DefineConstants>$(DefineConstants);MY_CONDITIONAL_COMPILATION_CONSTANT</DefineConstants>
</PropertyGroup>

Then use an MSBuild script that calls the project MSBuild script twice and uses the Properties attribute of the MSBuild task to specify the configuration to build:

<Target Name="Build">
    <MSBuild Projects="MyProject.csproj;" 
             Targets="Build" 
             Properties="Configuration=Release" />
    <MSBuild Projects="MyProject.csproj"
             Targets="Build"
             Properties="Configuration=CompiledOutDebug" />
  </Target>
那些过往 2024-07-20 23:51:29

哈米什比我先一步。

这是使用相同概念的替代解决方案:

在命令行:

msbuild -t:Clean
msbuild
CopyOutputDirForWithoutDefine.cmd
msbuild -t:Clean
msbuild -property:DefineConstants=MY_CONDITIONAL_COMPILE_CONSTANT
CopyOutputDirForWithDefine.cmd

第一个和第三个“msbuild -t:Clean”确保您没有以前构建中留下的粪便。 第二个“msbuild”在没有条件定义的情况下构建,而第四个“msbuild”则使用条件定义构建。

如果以上只是拍摄项目中的几个,那么批处理文件可能就足够了。 我建议学习一些 MSBuild,并像 Hamish 所做的那样,在 MSBuild 文件中实际编写所有内容的脚本。

Hamish beat me to it.

Here's an alternate solution using the same concepts:

At the command line:

msbuild -t:Clean
msbuild
CopyOutputDirForWithoutDefine.cmd
msbuild -t:Clean
msbuild -property:DefineConstants=MY_CONDITIONAL_COMPILE_CONSTANT
CopyOutputDirForWithDefine.cmd

The 1st and 3rd 'msbuild -t:Clean' ensures that you don't have left over turds from previous builds. The 2nd 'msbuild' builds without the conditional define, while the 4rth builds with the conditional define.

If the above are just a couple on shot items, then a batch file maybe enough. I recommend learning a bit of MSBuild and actually scripting everything in a MSBuild file as Hamish has done.

小伙你站住 2024-07-20 23:51:29

如果您不想为两次编译创建单独的目标,则可以通过在第二次调用构建时在 DefineConstants 属性中指定条件定义来实现:

<Target Name="Build">
    <MSBuild Projects="MyProject.csproj;"
             Targets="Build"
             Properties="Configuration=Debug" />
    <MSBuild Projects="MyProject.csproj"
             Targets="Build"
             Properties="Configuration=Debug;
                         AssemblyName=$(AssemblyName)_Conditional;
                         DefineConstants=$(DefineConstants);CONDITIONAL_DEFINE" />
</Target>

请注意,如果您这样做,则需要还要覆盖 AssemblyName,否则您的第二个构建可能会从第一个构建中选择中间文件。

您还应该查看 MSDN 上的 MSBuild 任务 文档,其中有那里有一些有趣的花絮。

If you don't want to create a separate target for the two compilations, you can do it by specifying the conditional define in the DefineConstants property when you call the build the second time:

<Target Name="Build">
    <MSBuild Projects="MyProject.csproj;"
             Targets="Build"
             Properties="Configuration=Debug" />
    <MSBuild Projects="MyProject.csproj"
             Targets="Build"
             Properties="Configuration=Debug;
                         AssemblyName=$(AssemblyName)_Conditional;
                         DefineConstants=$(DefineConstants);CONDITIONAL_DEFINE" />
</Target>

Note that if you do it this way, you need to also overwrite the AssemblyName, otherwise your second build might pick intermediate files from the first build.

You should also look at the MSBuild task docs on MSDN, there are some interesting tidbits there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文