【问题标题】:Conditionally include source file based on compiler version?根据编译器版本有条件地包含源文件?
【发布时间】:2017-12-31 18:28:38
【问题描述】:

我们正在测试一个项目设置,该设置要求我们根据编译器的版本包含或排除源文件。测试项目位于 GitHub 上的CRC-Test。对于 PoC,我们正在使用 CRC-32C 开发流程,因为英特尔和 ARM 都有它。稍后将应用于 AES、CLMUL、SHA、AVX{2}、BMI{2}、ARMv7a NEON、ARMv8 等(MS 编译器通过内部函数支持 NEON 和 ARMv8)。

我试图有条件地编译的源文件是crc-simd.cpp。它具有使用 SSE4.2 内部函数和 ARMv8 内部函数的实现(crc.cpp 提供通用的 C++ 实现)。

我在crc-test.vcxproj 添加了一个 VCX 项目文件。 ClCompile 有一个ItemGroup

<ItemGroup>
  <ClCompile Include="main.cpp" >
    <PrecompiledHeader />
  </ClCompile>
  <ClCompile Include="crc.cpp" >
    <PrecompiledHeader />
  </ClCompile>
  <ClCompile Include="crc-simd.cpp" >
    <PrecompiledHeader />
  </ClCompile>
</ItemGroup>

我需要 &lt;ClCompile Include="crc-simd.cpp" &gt; 的条件,即 “如果 cl.exe 为 15.00 或更高,则包含源文件”。在预处理器中,我会检查_MSC_VER &gt;= 1500。在 MSBuild 中,我想我正在寻找类似的东西:

<ItemGroup Condition="'$(CL_VERSION)'>='15.00'" Label="SSE4.2 acceleration">
  <ClCompile Include="crc-simd.cpp" >
    <PrecompiledHeader />
  </ClCompile>
</ItemDefinitionGroup>

我找到了List of MSBuild built-in variables,但我不知道$(CL_VERSION) 在现实生活中是什么。搜索“cl”并没有产生有趣的命中。我在MSBuild vs compiler 找到了一个问题,但它提出了另一个问题。

我的问题是,如何在 MSBuild 条件中引用 cl.exe 版本?

一个相关的问题是,有时我需要完整版。例如,在 VS2005 SP1、cl.exe 版本 15.00.30729 (_MSC_FULL_VER &gt;= 150030729) 中添加了 AES 支持。我的相关问题是,如何在 MSBuild 条件中引用 cl.exe full 版本?


尝试从开发人员提示中使用以下内容:

<ItemDefinitionGroup Condition="'$(CL_VERSION)'>='15.00'" Label="SSE4.2 acceleration">
  <ClCompile Include="crc-simd.cpp" >
    <PrecompiledHeader />
  </ClCompile>
</ItemDefinitionGroup>

结果:

> cls && msbuild

Microsoft (R) Build Engine version 4.6.1087.0
[Microsoft .NET Framework, version 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 7/25/2017 1:23:31 PM.
Project "C:\Users\Test\CRC-Test\crc-test.vcxproj" on node 1 (default ta
rgets).
C:\Users\Test\CRC-Test\crc-test.vcxproj(127,14): error MSB4066: The att
ribute "Include" in element <ClCompile> is unrecognized.
Done Building Project "C:\Users\Test\CRC-Test\crc-test.vcxproj" (defaul
t targets) -- FAILED.

Build FAILED.

"C:\Users\Test\CRC-Test\crc-test.vcxproj" (default target) (1) ->
  C:\Users\Test\CRC-Test\crc-test.vcxproj(127,14): error MSB4066: The a
ttribute "Include" in element <ClCompile> is unrecognized.

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.11

该项目目前是为 Visual Studio 2010 设置的。如果您使用不同的版本,请根据您的喜好进行更改。不需要VCUpgrade

<PlatformToolset>v100</PlatformToolset>
  • VS2010 → v100
  • VS2012 → v110
  • VS2013 → v120
  • VS2015 → v130
  • VS2017 → v140

这里是GNUmakefile 中Linux 项目的精髓。当编译器和平台支持硬件加速时,我们会引入crc-simd.cpp文件以加快实现速度。这是我们想要用 MSBuild 做的事情。

HAS_ARMV8 ?= $(shell uname -m | $(EGREP) -i -c 'aarch32|aarch64')
HAS_SSE42 ?= $(shell uname -m | $(EGREP) -i -c 'amd64|x86_64|i*86')
GCC43_OR_LATER := $(shell $(CXX) -v 2>&1 | $(EGREP) -i -c "gcc version (4\.[3-9]|[5-9]\.)")
GCC48_OR_LATER := $(shell $(CXX) -v 2>&1 | $(EGREP) -i -c "gcc version (4\.[8-9]|[5-9]\.)")
...

OBJECTS = main.o crc.o

ifeq ($(HAS_ARMV8)$(GCC48_OR_LATER),11)
OBJECTS += crc-simd.o
endif

ifeq ($(HAS_SSE42)$(GCC43_OR_LATER),11)
OBJECTS += crc-simd.o
endif
...

ifeq ($(HAS_ARMV8)$(GCC48_OR_LATER),11)
crc-simd.o : crc-simd.cpp
    $(CXX) $(CXXFLAGS) -march=armv8-a+crc -c $<
endif

ifeq ($(HAS_SSE42)$(GCC43_OR_LATER),11)
crc-simd.o : crc-simd.cpp
    $(CXX) $(CXXFLAGS) -msse4.2 -c $<
endif
...

【问题讨论】:

  • 似乎 SO 的语法高亮显示在"'$(CL_VERSION)'&gt;='15.00'"上的行程

标签: c++ visual-studio msbuild


【解决方案1】:

可以使用与生成文件类似的方法:运行 cl.exe,使用正则表达式获取版本,如果版本大于某个数字,则包含源文件。在代码中:

<Target Name="GetClVersion">
  <!-- Run cl, store output in variable -->
  <Exec Command="cl.exe" ConsoleToMSBuild="True" IgnoreExitCode="True">
    <Output TaskParameter="ConsoleOutput" PropertyName="ClOut" />
  </Exec>
  <PropertyGroup>
    <!-- cl.exe version number is something like 18.00.31101 -->
    <ClVersion>$([System.Text.RegularExpressions.Regex]::Match('$(ClOut)','(\d+\.\d+\.\d+)'))</ClVersion>
    <!-- Turn it into an integer like 180031101 by getting rid of dots -->
    <ClVersion>$([System.String]::Copy('$(ClVersion)').Replace('.', ''))</ClVersion>
  </PropertyGroup>
</Target>

<!-- This will run early in the build, before compilation,
     so we can modify the ClCompile ItemGroup. Needs to be done
     in a target, else we cannot use results from other targets
     (GetClVersion in this case) -->
<Target Name="ShowClVersion" DependsOnTargets="GetClVersion"
        BeforeTargets="BuildGenerateSources">
  <!-- &gt; = escaped > sign -->
  <ItemGroup Condition="$(ClVersion) &gt; 180031100">
    <ClCompile Include="somefile.cpp" />
  </ItemGroup>
</Target>

注意:您找到的内置变量列表是用于构建系统的 msbuild,它与编译器有点分开;这就是为什么没有 CL_VERSION 的原因,就像 makefile 默认没有 GCC_VERSION 一样。也值得尝试使用

<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>

而不是坚持一个或保留多个项目,每个 VS 版本一个。它还阻止 VS 要求升级(因为它看到它正在使用默认平台工具集)。我没有 VS 版本

【讨论】:

  • 谢谢@stijn。对于 VS2010(及更高版本),我们需要 DefaultPlatformToolset。微软似乎没有记录它:DefaultPlatformToolset msdn,所以我应该避免它,直到它记录下来。否则我们会收到大量邮件列表消息和错误报告。
  • 顺便说一句,我欠你一票。今天早上我在通过关闭队列时达到了每日限额。
  • DefaultPlatformToolset 确实没有在互联网上明确记录,但我不确定这是否是一个足够好的论据而不使用它;尤其是与优势相比。你可以防止它没有被定义,例如this project file 在 dotnet/corefx 源中:在没有 DefaultPlatformToolset 定义的情况下,它与硬编码完全相同。
  • 再次感谢@stijn。我尝试在本地机器和 AppVeyor 上使用 DefaultPlatformToolset 。它没有很好地测试。它导致构建在两种环境中都失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多