A site devoted to discussing techniques that promote quality and ethical practices in software development.

Wednesday, May 11, 2011

Recipe to add assembly version to Matlab NE Builder produced assembly

Matlab NE Builder, once called .Net Builder, is a tool from Matlab to package the Matlab script (the .m files) into a .Net assembly making the functions specified in Matlab available to .Net application.

This tool unfortunately provides token .Net infrastructure support. It can produce a strongly named assembly but it does not have facility to allow user to specify the assembly version data, a vital piece of information to support strict version binding policy.

Every assembly produced by NE Builder, regardless strongly name or not, has the version 0.0.0.0. As a result it is not very useful. According to Matlab forum, Matlab does not have any way to deal with this issue. This blog post provides a recipe of allowing user to add assembly version or any other pieces of information to the NE Builder produced assembly. It does not use any undocumented or hack to produce the result; it simply uses the same standard .Net build specifications the NE Builder Deployment tool uses.

  1. From the Deployment tool (read NE Builder) build the assembly and making sure that you clear the "Embedded the CTF Archive". This is because when you use embedded CTF archive, the builder deletes the CTF file after building the assembly. We need this file in the subsequent steps and hence not embedding lets us access this file. Furthermore, it is easier if you make the source and output directory the same.
  2. Save the build steps to a build log and open it with a text editor.
  3. Look out for the line containing the CSC.exe as we will use that to produce the response file for the C# compiler.
  4. Copy the text after CSC.exe into a text file. You can insert line break to make it more readable.
  5. Add AssemblyInfo.cs to the response file. This file contains the assembly information you wish to include into the assembly.
  6. Save the text file into a file with customary .rsp extension into the same directory as the source.
  7. Create the AssemblyInfo.cs file if not exist and specify your assembly version and other information.
  8. Run the CSC with this response file to produce your assembly which will contain the required assembly version.
Step 2-6 are only required as a once off exercise and if you add or remove .m file from your package, edit the response file accordingly.

This recipe then allows one to produce a versionable strong name .Net assembly that carries the Matlab script.

4 comments:

Anonymous said...

The output DLL comes without CTF file embedded in it, and it doesn't work

L. Mar said...

You are correct in that the output DLL does not have the CTF as we deliberately not embedding it. The message you are referring to does has one key instruction missing. That is the use of /linkres in the CSC compiler to define a resource (external) linkage to the CTF file. Without this you will have to manually carry this CTF to the location of the DLL.

Please refer to my other message http://do-the-right-things.blogspot.com.au/2011/05/complete-build-process-of-matlab.html for discussion of this. If you have provided an e-mail address, I can discuss this matter with you.

We have used this technique in many Matlab NE generated project.

Anonymous said...

What do you mean with "5. Add AssemblyInfo.cs to the response file"?

L. Mar said...

This step is not Matlab specific but a standard build process of any .Net assembly.

The AssemblyInfo.cs contains among other assembly identification information the most relevant piece in this discussion, the AssemblyVersion.

Including this file into the response file ensure that the build process will include the version number with your final Matlab assembly. In the standard Matlab build, this is not defined and hence resulting in version 0.0.0.0.

Blog Archive