I have a C# project with two platforms: x86 and x64. This C# project depends on a native dll that must also be built as x86 or x64.
So far, I have successfully added cmake as a pre-build event to the .csproj of the C# project:
<Target Name="BeforeBuild">
<Message Text="Building native library" />
<Exec Command="cmake -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
<Exec Command="cmake --build . --target install" />
</Target>
This builds and copies the native dll to the output directory of the project, matching the selected configuration (e.g. bin/x86/Debug or bin/x64/Release).
If I use the x86 configuration in Visual Studio, then everything is fine. However, if I use the x64 configuration I get a failure, because the native library is still built as x86. In other words, I need to find a way to inform cmake to build a x64 binary when $(Platform) is 'x64'.
Is there a commandline switch to instruct cmake to built a x64 binary? I tried the -G and -T options, but they don't appear to support this (or I wasn't able to find the correct way to use them).
Ideas welcome!
Edit: I've managed to get a little bit closer by passing the $(VisualStudioVersion) property to cmake.
<PropertyGroup>
<CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'x86'">-G"Visual Studio $(VisualStudioVersion)"</CMakeGenerator>
<CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'AnyCPU'">-G"Visual Studio $(VisualStudioVersion) Win64"</CMakeGenerator>
<CMakeGenerator Condition="'$(OS)' != 'Windows_NT'"></CMakeGenerator>
</PropertyGroup>
<Target Name="BeforeBuild">
<Message Text="Building native library" />
<Exec Command="cmake $(CMakeGenerator) -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
<Exec Command="cmake --build . --target install" />
</Target>
Unfortunately, $(VisualStudioVersion) returns a decimal number (e.g. 12.0 for VS2013), whereas cmake expects an integer (e.g. 12 for VS2013). If I can convert $(VisualStudioVersion) to an integer, then this should work! Can this be done?
Edit 2: solved!
MSBuild 4.0/4.5 adds property functions that can be used to modify properties. In this case, the following works perfectly:
<PropertyGroup>
<CMakeVSVersion>$(VisualStudioVersion.Substring(0, $(VisualStudioVersion).IndexOf(".")))</CMakeVSVersion>
<CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'x86'">-G"Visual Studio $(CMakeVSVersion)"</CMakeGenerator>
<CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'AnyCPU'">-G"Visual Studio $(CMakeVSVersion) Win64"</CMakeGenerator>
<CMakeGenerator Condition="'$(OS)' != 'Windows_NT'"></CMakeGenerator>
</PropertyGroup>
<Target Name="BeforeBuild">
<Message Text="Building native library" />
<Exec Command="cmake $(CMakeGenerator) -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
<Exec Command="cmake --build . --target install" />
</Target>
Hopefully someone might find this useful in the future!