Question

I have the following scenario:

  • two Visual Studio C++ solutions - one is used internally and one is supposed to be shipped to the client as a sample code. The two solutions differ in the number of configurations and the number of projects available

  • The two solutions have one common project I need to customize a bit depending on the solution that includes it

  • What I need to customize depending on the parent solution is the following:

    • the number of project configurations available - I have 4 configurations for the internal solution and only 2 for the client shipped one
    • some post build commands
    • some inclusion paths
    • a define which is specific only for the project which is shipped with the client solution
  • What I want to avoid is to create and maintain a separate project file for the client shipped solution

I tried to play with property sheets but I think that addresses a different problem than mine, mainly to share configurations between different projects. I'm also looking on how to use the Condition attribute in the project property files to see if that could help me (still investigating).

EDIT : I'm not allowed to use any project generation tools like CMake, premake etc.

Is there any way in Visual Studio to achieve what I need? Any suggestions in how I could to improve the setup I have so far - maybe this two solutions scenario could be avoided?

Was it helpful?

Solution

Since Visual Studio projects are processed by MSBuild they can be customized to your needs.

Importing a custom targets project file into your existing .vcxproj could look something like this:

        <!--
        ...
        way to the end of your .vcxproj file

        -->

        <!-- start of custom import part -->

        <Import Project=".\YourInternal.targets" Condition="Exists('.\YourInternal.targets')" />

        <!-- end of custom import part -->

        <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
        <ImportGroup Label="ExtensionTargets">
    </ImportGroup>
</Project>

Your .vcxproj will only contain the subset of items / configurations / commands you want to deliver to your customer - everything related to your internal build process will be contained in one or more .targets project file which will simply be exclued from deleivery to your customer.

For example if you have a internal Custom Build Step \ Execute Before \ Clean you would remove it from your .vcxproj file and add it to your YourInternal.targetsproject file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
        <CustomBuildBeforeTargets>Clean</CustomBuildBeforeTargets>
    </PropertyGroup>
</Project>

If you have a collection of items you need to extend for internal processing, you can concatenate the base set of your .vcxproj with additional items in your .targets file(s):

.vcxproj

...
<ItemGroup>
    <ClInclude Include="Header.h" />
</ItemGroup>
...

.targets

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <!-- includes to items definded in your .vcxproj -->
        <ClInclude Include="@(ClInclude)" />

        <!-- add more items here -->
        <ClInclude Include="Foo.h" />
        <ClInclude Include="Bar.h" />
    </ItemGroup>
</Project>

This examples merely scratch the surface of what you can achieve by extending your .vcxproj by MSBuild commands.

OTHER TIPS

You could use some XSLT processing to cleanup the internal projects and a seperate solution file that you are shipping to the client.

You can use a project generation tool like premake. It uses LUA to generate solutions and projects, so you can definitely customize the project settings depending on the parent solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top