Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Post build event declare var in bat file and use it later

I'm trying to auto sign project with certificate using signtool.exe in Visual Studio 2010. Here is my simplified post-build script:

if $(ConfigurationName) == Debug ( call "$(VS100COMNTOOLS)VCVarsQueryRegistry.bat" call "$(WindowsSdkDir)bin/signtool.exe" sign /f "$(ProjectDir)my.pfx" /p mypass /t timstamp.dll "$(TargetPath)" )

Debug is for testing purposes. I'm trying to mimic Visual Studio command prompt - execute $(VS100COMNTOOLS)VCVarsQueryRegistry.bat which adds some extra variables and then use it later. $(WindowsSdkDir)is what I need.

While that script works perfectly in simple bat file it refuses to work in Visual Studio. Output is:

call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\VCVarsQueryRegistry.bat" call "bin/signtool.exe" sign ...

Notice that it doesn't recognize $(WindowsSdkDir), which is created in VCVarsQueryRegistry.bat.

Is it possible to declare var in bat and use it later in post-build or I should use another approach?

like image 543
nikita Avatar asked Oct 17 '25 02:10

nikita


1 Answers

It's a bit tricky:

  • first, for cmd variables you need to use %var% syntax instead of msbuild-like $(var)
  • next, SET statements from call batches do apply.. but if body precalculates its variables, so you need several if lines instead of just one if body. Here is good explanation of such behavior and other ways to avoid it Weird scope issue in .bat file

So, this one should work:

if $(ConfigurationName) == Debug call "%VS100COMNTOOLS%VCVarsQueryRegistry.bat"
if $(ConfigurationName) == Debug call "%WindowsSdkDir%bin\signtool.exe" sign /f "$(ProjectDir)my.pfx" /p mypass /t timstamp.dll "$(TargetPath)"
like image 168
Lanorkin Avatar answered Oct 18 '25 19:10

Lanorkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!