Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MSBuild to update the FILEVERSION in a C++ resource file causes compile failures

I have been tasked with automating our build process with msbuild. We have about twenty Visual C++ projects and twenty C# projects making it a pain to hand edit all the version strings. Our version numbers are meat-generated at build time, so we need to pass the full version string as a property on the command line.

My initial implementation used the MSBuild Extension Pack's File task to do replacements on the .rc and AssemblyInfo.cs files.

<FileSystem.File TaskAction="Replace" Files="@(AssemblyInfoFiles)" RegexPattern='\[assembly: AssemblyVersion\(".*"\)\]' Replacement='[assembly: AssemblyVersion("$(Version)")]'/>
<FileSystem.File TaskAction="Replace" Files="@(AssemblyInfoFiles)" RegexPattern='\[assembly: AssemblyFileVersion\(".*"\)\]' Replacement='[assembly: AssemblyFileVersion("$(Version)")]'/>
<FileSystem.File TaskAction="Replace" Files="@(RCFiles)" RegexPattern="FILEVERSION\s+\S+" Replacement="FILEVERSION $(Version)"/>
<FileSystem.File TaskAction="Replace" Files="@(RCFiles)" RegexPattern="PRODUCTVERSION\s+\S+" Replacement="PRODUCTVERSION $(Version)"/>
<FileSystem.File TaskAction="Replace" Files="@(RCFiles)" RegexPattern='VALUE\s+"FileVersion",\s*".*"' Replacement='VALUE "FileVersion", "$(Version)"'/>
<FileSystem.File TaskAction="Replace" Files="@(RCFiles)" RegexPattern='VALUE\s+"ProductVersion",\s*".*"' Replacement='VALUE "ProductVersion", "$(Version)"'/>

This is working great on the AssemblyInfo.cs files, but when I compile any of the C++ projects I get errors like this:

.\MyProject.rc(23): error RC2135: file not found: 0x09
.\MyProject.rc(71): error RC2135: file not found: 1
.\MyProject.rc(72): error RC2135: file not found: 6
.\MyProject.rc(73): error RC2135: file not found: 6
.\MyProject.rc(74): error RC2135: file not found: 0x3fL
.\MyProject.rc(80): error RC2135: file not found: FILEOS
.\MyProject.rc(81): error RC2135: file not found: 0x2L
.\MyProject.rc(84): error RC2164: unexpected value in RCDATA
.\MyProject.rc(86): error RC2135: file not found: BLOCK
.\MyProject.rc(88): error RC2135: file not found: VALUE

The first error is encountered on line 23, but when I diff MyProject.rc against source control everything looks OK. Only the version numbers are changed, those changes begin on line 72, and the string 0x09 does not appear anywhere in the file. What in the world is going on here? Lines 21 - 25 look like this:

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

I'm also open to any suggestions on other methods to accomplish the same goal.

like image 779
Jared Avatar asked Feb 10 '10 17:02

Jared


3 Answers

The Replace TaskAction was inserting Unicode characters while the rc files are using ASCII encoding. Adding TextEncoding="ASCII" to all the rc replacement tasks results in compilable files.

A limitation of using the ASCII character set is that you can't use the © symbol. But if you choose TextEncoding="Windows-1252" that solves the problem.

like image 130
Jared Avatar answered Nov 04 '22 12:11

Jared


I would separate out the version information from the AssemblyInfo.cs and *.rc files. Create an AssemblyVersion.cs and Version.rc file, which contain the (common) versioning information for all your assemblies. You'd generate these at the start of your build. Because they only contain the versioning information you don't have to use regular expressions, you can overwrite the entire file each time.

like image 4
Gordon Broom Avatar answered Nov 04 '22 12:11

Gordon Broom


You still need to replace the periods (.) in the FILEVERSION with commas (,). If you build without the correct format, you won't see the file version in the file's properties | details page. I would go ahead and do the same for the PRODUCTVERSION, although it doesn't seem to be necessary.

I used MSBuild Property Functions. I'm sure you can regex/replace them too.

<FileSystem.File TaskAction="Replace" 
                 TextEncoding="ASCII" 
                 Files="@(AppResource)" 
                 RegexPattern="FILEVERSION\s+\S+" 
                 Replacement="FILEVERSION $(Version.Replace('.',','))"/>
like image 3
Anthony Mastrean Avatar answered Nov 04 '22 12:11

Anthony Mastrean