在没有硬编码路径的情况下,如何在Visual Studio 2013的生成后事件中使用signtool?

问题描述:

我创建了一个构建后事件,以使用以下构建后脚本成功构建应用程序后进行代码签名.

I've created a post build event to do code signing of the application after a successful build with the following post build script.

copy $(TargetPath) $(TargetDir)SignedApp.exe
signtool sign /t http://timestamp.verisign.com/scripts/timestamp.dll /a $(TargetDir)SignedApp.exe

我收到错误消息'signtool'无法识别为内部或外部命令. 因此,似乎用于构建事件的路径未指向signtool实用程序.当我运行 VS2013 x86本机工具命令提示符时,我可以运行signtool,因为它包含指向以下内容的路径:

I get the error 'signtool' is not recognized as an internal or external command. So it seems the path used for the build event doesn't point to the signtool utility. When I run the VS2013 x86 Native Tools Command Prompt I can run signtool as it includes a path which points to:

C:\Program Files (x86)\Windows Kits\8.1\bin\x86

我可以将此路径硬编码到我的构建事件中

I could hard-code this path into my build event

"C:\Program Files (x86)\Windows Kits\8.1\bin\x86\signtool" sign /t http://timestamp.verisign.com/scripts/timestamp.dll /a $(TargetDir)SignedApp.exe

但是,这似乎不可移植.如何获得为本地命令提示符定义的相同路径,以供我的后期构建事件使用,而无需对其进行硬编码?我查看了宏列表,但没有找到任何有用的宏.

However that seems non-portable. How do I get the same path defined for the Native Command Prompt to be used by my post build event without hard coding it? I've looked at the list of macros but haven't found any that would be useful.

我决定采用的解决方案是:

The solution I decided on was:

REM If SIGNTOOL environment variable is not set then try setting it to a known location
if "%SIGNTOOL%"=="" set SIGNTOOL=%ProgramFiles(x86)%\Windows Kits\8.1\bin\x86\signtool.exe
REM Check to see if the signtool utility is missing
if exist "%SIGNTOOL%" goto OK1
    REM Give error that SIGNTOOL environment variable needs to be set
    echo "Must set environment variable SIGNTOOL to full path for signtool.exe code signing utility"
    echo Location is of the form "C:\Program Files (x86)\Windows Kits\8.1\x86\bin\signtool.exe"
    exit -1
:OK1
echo Copying $(TargetFileName) to $(TargetDir)SignedApp.exe
copy $(TargetPath) $(TargetDir)SignedApp.exe
"%SIGNTOOL%" sign /t http://timestamp.verisign.com/scripts/timestamp.dll /a $(TargetDir)SignedApp.exe

这是@Dennis Kuypers建议4的变体.开发人员必须将环境变量SIGNTOOL设置到正确的位置.如果他们没有这样做,则尝试一个已知的可能位置.如果失败,则报告错误,指示他们正确设置SIGNTOOL env var.

This was a variation on @Dennis Kuypers suggestion #4. The developer must set environment variable SIGNTOOL to the correct location. If they fail to do so then one known possible location is attempted. If that fails then error is reported instructing them to set SIGNTOOL env var appropriately.

我确实发现有一个环境变量 WindowsSdkDir

I did discover there is an environment variable WindowsSdkDir

WindowsSdkDir=C:\Program Files (x86)\Windows Kits\8.1\

但同样,此设置仅在运行本地命令提示符时设置,因此在运行构建后事件脚本时未定义.

But again, this was set only when running the Native Command Prompt and thus was not defined when running the post build event script.