我能查看使用MSBuild执行的构建命令吗?

11

我使用 MSBuild 构建项目文件。 默认情况下,MSBuild 仅会输出编译结果,但是我需要检查执行了哪些命令。

我能查看实际执行的构建命令吗?我想要检查在 csc 命令中使用的编译器选项和引用。


1
对我来说,msbuild默认会打印所有的csc命令。 - user202729
4个回答

7

您可以设置Visual Studio构建的输出为详细模式。 进入菜单栏Tools -> Options -> Projects and Solutions -> Build and Run。 更改MSBuild项目构建输出详细程度选项,通常从默认的Minimal改为Normal、Detailed或Diagnostic。


5
你可以使用 /v[erbosity] 标志。我不知道你是否能够得到传递给 CSC 的标志列表,但如果你使用 /v:Detailed 或者 /v:diagnostic ,它会输出有关正在执行的目标、所有 MSBuild 变量及其值以及所有已解析的引用程序集的大量信息。
[编辑:] 如果你寻找它,它似乎也会显示对 csc.exe 的命令行调用,即使在 /v:detailed 模式下也是如此。

2
  1. 从Visual Studio 2019命令提示符中,
  2. cd到包含我的sln文件的目录,
  3. 如果有任何对象文件和可执行文件,请删除它们,因为如果不这样做,MSBUILD可能不会重新编译,
  4. 运行msbuild /v:normal > msbuild.log,
  5. 观察msbuild.log文件中是否包含CLCompile和Link:
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\CL.exe /c /IC:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\..\..\src /IC:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\..\..\..\..\pdelib\pdelib_v7.8.3\v7.8.3\msdos_mcn\vc140_64_md\core\include /IC:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\..\..\..\..\pdelib\pdelib_v7.8.3\v7.8.3\msdos_mcn\vc140_64_md\step\include /IC:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\..\..\..\..\pdelib\pdelib_v7.8.3\v7.8.3\msdos_mcn\vc140_64_md\iges\include /ZI /JMC /nologo /W3 /WX- /diagnostics:column /sdl /Od /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE /Gm- /EHsc /RTC1 /MD /GS /fp:precise /permissive- /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"x64\Debug\\" /Fd"x64\Debug\vc142.pdb" /external:W3 /Gd /TP /FC /errorReport:queue /wd4996 /wd4703 ..\..\src\test_pdelib.cpp ..\..\src\test_step.cpp
  test_pdelib.cpp
  test_step.cpp
  Generating Code...
Link:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\link.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\x64\Debug\test_pdelib.exe" /INCREMENTAL /ILK:"x64\Debug\test_pdelib.ilk" /NOLOGO /LIBPATH:C:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\..\..\..\pdelib_v7.8.3\v7.8.3\msdos_mcn\vc140_64_md\core\lib /LIBPATH:C:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\..\..\..\pdelib_v7.8.3\v7.8.3\msdos_mcn\vc140_64_md\step\lib /LIBPATH:C:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\..\..\..\pdelib_v7.8.3\v7.8.3\msdos_mcn\vc140_64_md\iges\lib libgdx.lib libpdx.lib libstep.lib libiges.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\x64\Debug\test_pdelib.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\x64\Debug\test_pdelib.lib" /MACHINE:X64 x64\Debug\test_pdelib.obj
  x64\Debug\test_step.obj
  test_pdelib.vcxproj -> C:\Users\Administrator\Desktop\pdelib\test_pdelib\v7.8.3\VSE2019.16.11.4\x64\Debug\test_pdelib.exe

-fl 会同时写入日志。默认名称为 msbuild.log :-) 名称/位置可以使用 -flp 进行更改。您还可以为日志文件设置不同于控制台的详细程度,例如 -flp:LogFile=msbuild-detailed.log;Verbosity=diagnostic - Fizz

1
在控制台上最细粒度的控制方式是使用:
-consoleLoggerParameters:ShowCommandLine

aka -clp:ShowCommandLine。根据VS2019的说明文档

默认控制台记录器的详细程度为正常,并包含摘要。

Summary也是一种输出类型,可以打印或不打印,与ShowCommandLine处于同一类别)

结果证明(在VS 2019下进行的实验见下文),最小的-v:m详细程度会抑制工具的stdout输出,除了msbuild标语之外,并抑制cmd回显。

在正常及以上的详细程度下,使用VS 2019时,cmd回显和工具标准输出都会被打印出来。但是你可以通过添加-clp:ShowCommandLine将回显添加回-v:m中。这实际上打印出的噪音比正常的msbuild详细级别要少得多,特别是来自msbuild本身的噪音。不幸的是,你不能将-clp:ShowCommandLine-v:q组合使用,以抑制工具的横幅而获得cmd回显;-v:q仍将打印横幅,但取消-clp:ShowCommandLine的效果。
我不太常自己构建 C# 项目,但问题下的评论(由 user202729 在 2018 年 2 月 25 日 15:35 发布)是正确的,即 msbuild,至少是来自 VS2019 的那个,会在正常详细程度上输出 cmd 行。

我使用了微软提供的最小的 msbuild C# 示例 链接,从 x64 dev cmd 提示符中运行测试:

M:\tests\cs-build>msbuild helloworld.csproj -t:Build
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 1/16/2022 9:42:20 AM.
Project "M:\tests\cs-build\helloworld.csproj" on node 1 (Build target(s)).
Build:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current
  \Bin\Roslyn\csc.exe /out:Helloworld.exe Helloworld.cs
  CompilerServer: tool - using command line tool by design 'C:\Program Files (x
  86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\csc.exe
  ' - 753a030a-3ca4-43b9-9e98-18d923493186
Done Building Project "M:\tests\cs-build\helloworld.csproj" (Build target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.80

“有些由VS生成的构建文件可能会覆盖隐式默认值。为了记录,以防微软更改示例,csproj文件如下:”
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Compile Include="helloworld.cs" />
  </ItemGroup>
  <Target Name="Build">
    <Csc Sources="@(Compile)"/>  
  </Target>
</Project>

我无法完全找到记录下来的每个详细程度所包含的输出内容集合。这里有一个关于此的Q,但那里的现有答案只是重复了微软文档上的内容,对此并没有提供足够详细的说明。
人们已经发现的一件事是,在最小详细程度下,由msbuild调用的工具的stdout消息会被抑制,但在普通及以上详细程度下会被打印出来。因此,我猜测最小详细程度也会抑制cmd回显...而它的确做到了:
M:\tests\cs-build>msbuild helloworld.csproj -t:Build -v:m
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.


M:\tests\cs-build>

因此,您实际上可以通过使用-v:m -clp:ShowCommandLine来获得cmd echo,但除了错误和msbuild横幅之外,没有其他的msbuild chatter。
M:\tests\cs-build>msbuild helloworld.csproj -t:Build -v:m -clp:ShowCommandLine
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current
  \Bin\Roslyn\csc.exe /out:Helloworld.exe Helloworld.cs

哎呀,你不能将-clp:ShowCommandLine-v:q结合使用,以抑制横幅但获取命令行回显; -v:q仍会打印横幅,但取消-clp:ShowCommandLine的效果。
M:\tests\cs-build>msbuild helloworld.csproj -t:Build -v:q -clp:ShowCommandLine
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.


M:\tests\cs-build>

您可以使用-nologo单独抑制msbuild横幅,因此-v:m -clp:ShowCommandLine -nologo是有效的组合。
M:\tests\cs-build>msbuild helloworld.csproj -t:Build -v:m -nologo -clp:ShowCommandLine
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current
  \Bin\Roslyn\csc.exe /out:Helloworld.exe Helloworld.cs

M:\tests\cs-build>

测试 最小化的本机CPP示例(这对我更感兴趣),我得到了类似的输出,意味着命令行默认打印。
M:\tests\cpp-build>msbuild myproject.vcxproj /p:configuration=debug
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 1/16/2022 10:05:01 AM.
Project "M:\tests\cpp-build\myproject.vcxproj" on node 1 (default targets).
InitializeBuildStatus:
  Touching "debug\myproject.tlog\unsuccessfulbuild".
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\1
  4.29.30133\bin\HostX86\x86\CL.exe /c /Zi /nologo /W1 /WX- /diagnostics:column
   /O2 /Oy- /Gm- /EHsc /MD /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline
  /Fo"debug\\" /Fd"debug\vc142.pdb" /external:W1 /Gd /TP /analyze- /FC /errorRe
  port:queue main.cpp
  main.cpp
Link:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\1
  4.29.30133\bin\HostX86\x86\link.exe /ERRORREPORT:QUEUE /OUT:"M:\tests\cpp-bui
  ld\debug\myproject.exe" /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.li
  b comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc3
  2.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'
  " /manifest:embed /DEBUG:FULL /PDB:"M:\tests\cpp-build\debug\myproject.pdb" /
  TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"M:\tests\cpp-build\debug\myproject.li
  b" /MACHINE:X86 /SAFESEH debug\main.obj
  myproject.vcxproj -> M:\tests\cpp-build\debug\myproject.exe
FinalizeBuildStatus:
  Deleting file "debug\myproject.tlog\unsuccessfulbuild".
  Touching "debug\myproject.tlog\myproject.lastbuildstate".
Done Building Project "M:\tests\cpp-build\myproject.vcxproj" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.32

我不会在这里粘贴那个.vcxproj文件,因为它相当长。

这个相当冗长的输出(包括cmd命令行)肯定不是我从典型的cmake项目生成的这样的文件所经历的体验,尽管经过一些测试后,后者的cmake默认使用...msbuild默认的详细程度,因此包括cmd echo。我不会在这里粘贴通过cmake生成的vcxproj构建的(更长的)输出,但您可以在https://pastebin.com/WQtbYeDE上看到它。那是为前面的MS C++示例制作的最小CMakeLists.txt。

cmake_minimum_required(VERSION 3.10)

# set the project name
project(Tutorial)

# add the executable
add_executable(Tutorial main.cpp)


此外,无论是将由CMake生成的.sln还是直接将由CMake生成的.vcxproj进行msbuild构建,在冗长度上没有区别。
简而言之,现在在使用msbuild时,您需要关闭cmd的回显而不是打开回显;大多数工作流程的默认设置是打开回显。不过,我还没有测试VS IDE生成的文件在这方面的情况。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接