0
votes

I've been trying to figure out a proper way of leveraging the information already present in a solution file of a rather big c++ code base.

The problem I'm trying to solve is calling an external script on some of the project files within the solution, but observing the already present dependencies specified in the solution and single project files.

I've successfully called said (python) script by adding a custom target to one of my project files and using msbuild with the /t:{TargetName} command on the vcxproj file.

I could now add this target to every project file that needs to call this script and afterwards call msbuild for each of them in the correct order, however this defeats the purpose of leveraging the dependencies known by the solution.

Calling the solution with the custom target does however not work (as seen by other stackoverflow questions like this: Invoke Custom MSBuild Target on Solution File).

On the other hand, since I want to be able to ONLY call the script target and not being dependent on also calling some Build command, I cannot use the proposed workarounds in some of those answers of adding a Post or PreBuild target.

Is there any other way of using the dependencies without having to go the route of msbuild and custom targets, or is there an other workaround that could serve my purpose?

1

1 Answers

0
votes

Is there any other way of using the dependencies without having to go the route of msbuild and custom targets, or is there an other workaround that could serve my purpose?

You can build the sln programmatically, Here is a console APP with c# for your reference.

using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using System;

namespace LoadAllProject
{
    class Program
    {
        static void Main(string[] args)
        {
            ILogger logger = new ConsoleLogger();
            string solutionPath = @"D:\Project\Msbuild\CppApp5\CppApp5.sln";
            var solutionFile = SolutionFile.Parse(solutionPath);
            foreach (var item in solutionFile.ProjectsInOrder)
            {
                Project project = ProjectCollection.GlobalProjectCollection.LoadProject(item.AbsolutePath);
                project.SetGlobalProperty("Configuration", "Debug");
                if (project.GetPropertyValue("RootNamespace") == "CppApp5")
                {
                    project.Build(new[] { "Build", "Yourcustomtarget" }, new[] { logger });
                }
                else
                { 
                    project.Build(new[] { "Build" }, new[] { logger });
                }
            }
            Console.ReadKey();
        }
    }
}