将PreBuildEvent转换为具有条件的目标?

时间:2016-10-06 15:47:45

标签: visual-studio xsd msbuild msbuild-task

我有一个名为cryptdll.vcxproj的项目。 cryptdll 取决于解决方案中其他两个项目的工件。其他项目是 cryptlib cryptest 。对于那些对 cryptdll 中的元素布局感兴趣的人,它位于cryptdll

依赖项有点不寻常,在Visual Studio编辑器中不易表达。它们不常见,因为政策要求Win32\Output\Debug\cryptest.exe始终用于执行PostBuildEvent cryptdll

我发现我可以将以下内容添加到 cryptdll 以使事情按预期运行:

<!-- Win32/Debug cryptest.exe for DLL MAC'ing -->
<ItemDefinitionGroup Condition="!Exists('Win32\Output\Debug\cryptest.exe')" Label="MAC tool">
  <PreBuildEvent>
    <Message>Creating Win32/Debug cryptest.exe for MAC computation</Message>
    <Command>
      msbuild /t:Build /p:Configuration=Debug;Platform=Win32 cryptlib.vcxproj
      msbuild /t:Build /p:Configuration=Debug;Platform=Win32 cryptest.vcxproj
    </Command>
  </PreBuildEvent>
</ItemDefinitionGroup>

当我尝试将其转换为目标时,它停止了工作。以下是目标规则。

<!-- Win32/Debug cryptest.exe for DLL MAC'ing -->
<Target Condition="!Exists('Win32\Output\Debug\cryptest.exe')" Name="MAC tool" Label="MAC tool">
  <MSbuild
    Projects="cryptlib.vcxproj"
    Properties="Configuration=Debug;Platform=Win32;"/>
  <MSbuild
    Projects="cryptest.vcxproj"
    Properties="Configuration=Debug;Platform=Win32;"/>
</Target>

我的问题是,转换为Target的代码有什么问题,我该如何解决?

为了完整起见,我想要Makefile人员称之为 Prerequisite 的内容。我可以从搜索中得知,"MSBuild prerequisite"的每个结果都无关紧要。

以下是Target&#d; d输出的样子。请注意,该任务被跳过,就好像它不存在一样。

>del /q /s Win32 x64
...

>msbuild /t:build /p:Configuration=Release;Platform=x64 cryptdll.vcxproj
Microsoft (R) Build Engine version 4.6.1055.0

Build started 10/6/2016 11:40:26 AM.
Project "c:\cryptopp\cryptdll.vcxproj" on node 1 (build target(s)).
PrepareForBuild:
  Creating directory "x64\cryptdll\Release\".
  Creating directory "x64\DLL_Output\Release\".
InitializeBuildStatus:
  Creating "x64\cryptdll\Release\cryptdll.unsuccessfulbuild" because "AlwaysCre
  ate" was specified.
CustomBuild:
  Performing Custom Build Tools
   Assembling: c:\cryptopp\x64dll.asm
ClCompile:
  c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\AMD64\CL.exe /c /Z
  i /nologo /W4 /WX- /O2 /Ob2 /Oi /Oy /D NDEBUG /D CRYPTOPP_EXPORTS /D CRYPTOPP
  _ENABLE_COMPLIANCE_WITH_FIPS_140_2=1 /D USE_PRECOMPILED_HEADERS /D _WINDLL /G
  F /Gm- /EHsc /MT /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Yc"pch.h" /Fp"
  x64\cryptdll\Release\cryptopp.pch" /Fo"x64\cryptdll\Release\\" /Fd"x64\cryptd
  ll\Release\vc100.pdb" /Gd /TP /errorReport:none pch.cpp
  pch.cpp
  ...

  <rest of the DLL is built>

PostBuildEvent:
  Description: Adding MAC to DLL

          Win32\output\debug\cryptest.exe mac_dll "cryptopp\x64\DLL_Output\Rele
  ase\cryptopp.dll"
          IF %ERRORLEVEL% EQU 0 (echo mac done > "x64\DLL_Output\Release\"\cryp
  topp.mac.done)

  :VCEnd
  The system cannot find the path specified.
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(1
13,5): error MSB3073: The command "\r [c:\cryptopp\cryptdll.vcxproj]
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(1
13,5): error MSB3073:         Win32\output\debug\cryptest.exe mac_dll "c:\crypt
opp\x64\DLL_Output\Release\cryptopp.dll"\r [c:\cryptopp\cryptdll.vcxproj]
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(1
13,5): error MSB3073:         IF %ERRORLEVEL% EQU 0 (echo mac done > "x64\DLL_O
utput\Release\"\cryptopp.mac.done)\r [c:\cryptopp\cryptdll.vcxproj]
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(1
13,5): error MSB3073:       \r [c:\cryptopp\cryptdll.vcxproj]
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(1
13,5): error MSB3073: :VCEnd" exited with code 3. [c:\cryptopp\cryptdll.vcxproj
Done Building Project "c:\cryptopp\cryptdll.vcxproj" (build target(s)) -- FAILE
D.

Build FAILED.

1 个答案:

答案 0 :(得分:1)

当我尝试将其转换为任务时您的意思是Target,这与Task不同:第一个包含后者。

注意,任务被跳过就好像它不存在一样。你不会告诉msbuild在什么地方对目标做什么,所以它不可能知道何时调用它。你想在构建之前调用它,所以你必须以某种方式表达它。这里,BeforeTargets属性(上面相同链接中的文档)是规范的方式:

<Target Condition="!Exists('Win32\Output\Debug\cryptest.exe')"
        Name="MAC tool"
        BeforeTargets="Build">

旁注:为什么要将一个Label添加到只复制名称的目标?