Setting processor affinity on CSC.exe launched by CoreCompile MSBuild Task
-
19-06-2021 - |
题
I am wondering if there is simple way to ensure that: when a C# project is compiled, the Csc.exe
launched inherits the parent processor affinity settings; or perhaps a way that I can supply this.
I have been trying to accomplish this by launching a bat
file from VS.NET cmd prompt:
start /affinity 01 custombuild.cmd
Inside my custombuild.cmd
I have:
@echo off
msbuild Libraries.sln /t:rebuild /p:Configuration=Release;platform=x64 /m:1
:END
The command line call to Csc.exe
that is generated looks like the following:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe ...
(the rest omitted for brevity.)
I'd like Csc.exe
to inherit the processor affinity, or a simple way to be able to override how Csc.exe
call is generated so that it becomes:
start /affinity 01 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe ...
(the rest omitted for brevity.)
I also noticed that CoreCompile target is defined in Microsoft.CSharp.targets, should I be considering overriding MSBuildToolsPath variable so I can sneak in my own version. This feels rather hacky. Any help would be much appreciated.
Status Update: I have worked out that the csc.exe process launched by csc task is actually prepared in ExecuteTool method of the Task class's member and i have created a custom task CpuAffineCscTask which derives from the Csc and overrides ExecuteTool method. My goal is to launch the csc.exe using a pinvoke call to CreateProcess in suspended state, then set affinity and then resume the thread. I also used reflection to make the state of various variables is correctly set etc. Now i am trying to override the CoreCompile target the one which is defined in MicrosoftCSharp.targets file but when my target runs i get the following runtime error. Are there any known examples of this around ? Even though the whole cmd correctly prints out in the msbuild diagnostic output. The diagnostic output follows for your consideration.
Task "MSBuild.Tasks.CpuAffineCscTask" (TaskId:14) C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /unsafe+ /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /doc:..\Bin\Release\Ugp.Igt.Core.XML /define:TRACE /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\WindowsBase.dll" /debug:pdbonly /filealign:512 /keyfile:..\StrongNameKeyFile.snk /optimize+ /out:obj\Release\Ugp.Igt.Core.dll /target:library ..\DevelopmentKitVersionInfo.cs AnonymousDisposable.cs ByteArrayExtensionMethods.cs TypeMustImplementMethodAttribute.cs WeakEvent.cs "C:\Users\bhatiah\AppData\Local\Temp.NETFramework,Version=v4.0.AssemblyAttributes.cs" (TaskId:14) E:\source\Core\Microsoft.CSharp.targets(217,3): error MSB6001: Invalid command line switch for "Csc.exe". Value cannot be null. E:\source\Core\Microsoft.CSharp.targets(217,3): error MSB6001: Parameter name: SafeHandle cannot be null. Done executing task "Igt.MSBuild.Tasks.CpuAffineCscTask" -- FAILED. (TaskId:14) Done building target "CoreCompile" in project "Igt.Core.csproj" -- FAILED.: (TargetId:37) Target "_CheckForCompileOutputs: (TargetId:38)" in file "c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets" from project "E:\source\Core\Igt.Core\Igt.Core.csproj" (target "_CleanGetCurrentAndPriorFileWrites" depends on it):
Any help would be very much appreciated.
PS: I am redirecting all three IO streams since i am using CreateProcess and pipes but post creation i use Process.getProcessById .net method to get a reference to the process object and attach the handlers.
hardy
解决方案
Get the command string by overriding the executeTool method, preappend it with start /affinity 01 write it out to a batch file in the same folder as the csproj file, use shellexecute method to run it. (might need to ensure all the output directories exist).