I need to re-sign my assembly after the build has finished (and I've done some other things to it), so I started by adding an <Exec>
Task that called C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe
. This has to work for other developers/environments, so I was hoping I could just copy sn.exe
and sn.exe.config
from that folder and store it in our code repository so I could always call a common version of it from a known location.
sn.exe
crashes in isolation outside of the sdk directory, so I'm wondering how I can reference it without knowing what path it will be under. Different people have different environments (x86 vs x64, different install directories, different versions), so I would like to be able to easily reference the latest version of the tool (or perhaps any version). Seems like a simple enough tool, perhaps there is another way to sign an assembly with another tool/command/msbuild task? Any help would be appreciated.
The method I'm using at the moment involves using a property function to perform a search for SN.exe underneath the framework SDK path - ala:
So far I've only tested this personally on Visual Studio 2013, but the documentation implies it ought to work back to Visual Studio 2010.
You can create an environment variable on each development machine that references the executable, which MSBuild lets you reference as a property.
So, create the environment variable via the advanced tab of system properties. I usually just create a system environment variable as opposed to one scoped to the current user. You will have to restart Visual Studio for it to pick it up.
Then, reference it in MSBuild:
Where
SnExe
is the environment variable you defined.For what its worth the $(SDK40ToolsPath) variable worked for me in a similar situation. This negates the need to know which specific version of the tools are installed eg:
Turns out there's a task called "GetFrameworkSdkPath" that will get the Windows SDK location. From there, I had to test to see if sn.exe existed directly in the
bin
folder, or if it's inbin\NETFX 4.0 Tools\
. Seems reliable so far.To properly reference a tool like
sn
orsqlmetal
(what I am after) in an msbuild script in the way that will work for the most people, you must take into consideration different aspects of the operating environment and framework implementation. There are two main cases: Microsoft Windows and Microsoft’s implementation of the framework followed by everything else (by which I mean Mono/unix). An example of the correct approach which supports the situations I can think of is listed at the end.Microsoft
The proper way to find where
sn
or other similar tools live in Windows is to start with the GetFrameworkSdkPath task, as already mentioned.However, as the question suggests, the exact location within the FrameworkSdkPath that
sn
or another tool lives cannot be determined directly. The referenced answer suggests that the only possible folders under FrameworkSdkPath for tools to reside in arebin
andbin/NETFX 4.0 Tools
. However, other values are possible (Visual Studio 2013 Preview usesbin/NETFX 4.5.1 Tools
). Thus, the only proper way to search forsn
is to use a glob expression or recursively search for it. I have trouble figuring out how to do glob expansion with MSBuild and the built-in MSBuild tasks do not seem to support searching under FrameworkSdkPath for particular utilities. However, cmd’sWHERE
has this functionality and can be used to do the search. The result is something like the following msbuild code:(See Property Functions to see why I can use
String.TrimEnd
here.WHERE
doesn’t like trailing slashes. EDIT: I added use of Property Functions to accessRegex.Replace()
to delete all but the first found path in theSNPath
property. One of my friend’s machines’sWHERE
invocations would output multiple results for certain commands and broke any attempt to<Exec/>
the fond tool. This change ensures that only one result is found and that<Exec/>
s actually succeed.)Now you can invoke
sn
with<Exec Command=""$(SNPath)"" />
.Portable
Unsurprisingly, resolving the path to
sn
is much simpler on any operating system other than Windows. On Mac OSX and any distribution of Linux, I findsn
in the PATH. UsingGetFrameworkSdkPath
does not help in such a situation; in fact, this seems to return a path under whichsn
cannot be found, at least for the old versions of mono-2.10 I tested while using xbuild:FrameworkSdkPath
is/Library/Frameworks/Mono.framework/Versions/2.10.5/lib/mono/2.0
and/usr/bin/sn
is a symlink to/Library/Frameworks/Mono.framework/Commands/sn
.FrameworkSdkPath
is/usr/lib64/mono/2.0
andsn
is/usr/bin/sn
(which is a shell script invoking/usr/lib64/mono/4.0/sn.exe
withmono
).Thus, all we need to do is try to execute
sn
. Any unix users placing theirsn
implementations in non-standard places already know to update PATH appropriately, so the build script has no need to ever search for it. Also,WHERE
does not exist in unix. Thus, in the unix case, we want to replace the first<Exec/>
call with something that will output justsn
on unix and still do the full search when run on Windows. To differentiate unix-like and Windows environments, we use a trick which takes advantage of unix shells’ shortcut for thetrue
commmand and cmd’s label syntax. As a short example, the following script will outputI’m unix!
in a unix shellout andI’m Windows :-/
on a Windows shellout.Taking advantage of this, our resulting
GetSNPath
Task looks like:The result is a portable method for finding the string required to invoke
sn
. This last solution lets you support both Microsoft and its msbuild and every other platform using xbuild. It also overcomes hardcodingbin\NETFX 4.0 Tools
into .csproj files to support future and current versions of Microsoft tools simultaneously.