9

Is there any method to list platform toolsets available in VS2012? I mean a list that might contain v90, v100, v110, v110_xp and any externally provided platform toolset. Alternatively (should that be easier): is there a way to check if given platform toolset is installed or not?

Tomasz Grobelny
  • 2,666
  • 3
  • 33
  • 43
  • I have a C# program that does it, as it depends on MsBuild which is a .NET program. Would that be an acceptable answer? – Simon Mourier Jul 04 '14 at 17:03
  • Long forgotten about this question... I think it is worth posting any hint as answer, even if it is not in the language that I (or Sneftel) may want to use. – Tomasz Grobelny Jul 09 '14 at 10:37
  • Personally, I'd rather just know where on the disk to look, but doing that without invoking MSBuild might be an approximate process. – Sneftel Jul 11 '14 at 14:12

1 Answers1

4

Here is a console app utility (in C#) that dumps the list of toolset (for each available configuration). You need to add a reference to Microsoft.Build to be able to compile. Note the proper list of toolset is supposed to depend on the project to build:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ListToolsets
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Format is ListToolsets <project file path>");
                return;
            }

            foreach (var toolset in PlatformToolset.GetPlatformToolsets(args[0]))
            {
                Console.WriteLine(toolset.Platform);
                foreach (string ts in toolset.Toolsets)
                {
                    Console.WriteLine(" " + ts);
                }
            }
        }

    }

    public class PlatformToolset
    {
        private PlatformToolset()
        {
            Toolsets = new List<string>();
        }

        public string Platform { get; private set; }
        public IList<string> Toolsets { get; private set; }

        public static IList<PlatformToolset> GetPlatformToolsets(string projectPath)
        {
            var list = new List<PlatformToolset>();
            var project = new Microsoft.Build.Evaluation.Project(projectPath);
            AddPlatformToolsets(project, @"$(VCTargetsPath14)\Platforms", list);
            AddPlatformToolsets(project, @"$(VCTargetsPath12)\Platforms", list);
            AddPlatformToolsets(project, @"$(VCTargetsPath11)\Platforms", list);
            AddPlatformToolsets(project, @"$(VCTargetsPath10)\Platforms", list);
            return list;
        }

        private static void AddPlatformToolsets(Microsoft.Build.Evaluation.Project project, string path, IList<PlatformToolset> list)
        {
            string platforms = Path.GetFullPath(project.ExpandString(path));
            if (!Directory.Exists(platforms))
                return;

            foreach (string platformPath in Directory.GetDirectories(platforms))
            {
                string platform = Path.GetFileName(platformPath);
                PlatformToolset ts = list.FirstOrDefault(t => t.Platform == platform);
                if (ts == null)
                {
                    ts = new PlatformToolset();
                    ts.Platform = platform;
                    list.Add(ts);
                }

                foreach (string toolset in Directory.GetDirectories(Path.Combine(platformPath, "PlatformToolsets")))
                {
                    string name = Path.GetFileName(toolset);
                    string friendlyName = project.GetPropertyValue("_PlatformToolsetFriendlyNameFor_" + name);
                    ts.Toolsets.Add(string.IsNullOrWhiteSpace(friendlyName) ? name : friendlyName);
                }
            }
        }
    }
}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Seems like a good direction. However, these VCTargetsPathXX seem somewhat magic... BTW, adding VCTargetsPath12 is necessary to detect v120(_xp) toolsets. My main question is: will this detect third party toolsets or only those delivered with VS. – Tomasz Grobelny Jul 11 '14 at 08:33
  • There is no magic here. VCTargetsPathsXX are defined in msbuild target files (.targets or other extensions) that you'll find in "...ProgramFiles\MsBuild\...". You can replace it by the local VS installation path. This detects all toolset that are located in those directories. – Simon Mourier Jul 11 '14 at 08:45
  • Yes, but how do I get a list of all applicable VCTargetsPathXX variables? For example in VS2014 I will need to add VCTargetsPath13, or however they call it. – Tomasz Grobelny Jul 11 '14 at 09:25
  • You asked for VS2012 in your question. – Simon Mourier Jul 11 '14 at 10:05
  • Maybe this is a non-issue, as Tomasz didn't complain, but he got an answer 1.5 years after submitting the question (and here I am, responding a year later.) In that time, VS2014 became relevant to the topic, and even more so now. Can you force a PlatformToolset to be recognized, even if it isn't listed in the project? – Bishop Jun 10 '15 at 17:54
  • 1
    @Bishop - I don't know any VS2014 - sorry to be picky :) I have updated the answer to support VS2013 though – Simon Mourier Jun 10 '15 at 22:24