-
Notifications
You must be signed in to change notification settings - Fork 34
/
ConanProfilesManager.cs
177 lines (152 loc) · 7.02 KB
/
ConanProfilesManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.VCProjectEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows;
namespace conan_vs_extension
{
public class ConanProfilesManager
{
public ConanProfilesManager()
{
}
public static string getProfileName(VCConfiguration vcConfig)
{
return vcConfig.Name.Replace("|", "_");
}
private string getConanArch(string platform)
{
var archMap = new Dictionary<string, string>();
archMap["x64"] = "x86_64";
archMap["Win32"] = "x86";
archMap["ARM64"] = "armv8";
return archMap[platform];
}
private string getConanCompilerVersion(string platformToolset, string vsVersion)
{
if (Version.TryParse(vsVersion, out Version parsedVersion))
{
// https://github.com/conan-io/conan/issues/16239
if (parsedVersion.Major == 17 && parsedVersion.Minor >= 10)
{
return "194";
}
}
var msvcVersionMap = new Dictionary<string, string>();
msvcVersionMap["v143"] = "193";
msvcVersionMap["v142"] = "192";
msvcVersionMap["v141"] = "191";
return msvcVersionMap[platformToolset];
}
private string GetRuntimeLibraryType(runtimeLibraryOption runtimeLibraryValue)
{
switch (runtimeLibraryValue)
{
case runtimeLibraryOption.rtMultiThreaded:
case runtimeLibraryOption.rtMultiThreadedDebug:
return "static";
case runtimeLibraryOption.rtMultiThreadedDLL:
case runtimeLibraryOption.rtMultiThreadedDebugDLL:
return "dynamic";
default:
return "dynamic";
}
}
private string getConanCppstd(string languageStandard)
{
// https://learn.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=msvc-170
if (languageStandard.ToLower().Contains("default"))
{
return "14";
}
List<string> cppStdValues = new List<string>() { "14", "17", "20", "23" };
foreach (string cppStdValue in cppStdValues)
{
if (languageStandard.Contains(cppStdValue))
{
return cppStdValue;
}
}
return "null";
}
public void GenerateProfilesForProject(Project project)
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
if (project != null && project.Object is VCProject vcProject)
{
string projectDirectory = System.IO.Path.GetDirectoryName(project.FullName);
string path = Path.Combine(projectDirectory, "conandata.yml");
// only generate the profiles if we had a conandata that means
// that at some point the user wanted to use conan
bool fileExists = File.Exists(path);
if (fileExists)
{
string conanProjectDirectory = System.IO.Path.Combine(projectDirectory, ".conan");
if (!Directory.Exists(conanProjectDirectory))
{
Directory.CreateDirectory(conanProjectDirectory);
}
foreach (VCConfiguration vcConfig in (IEnumerable)vcProject.Configurations)
{
string profileName = getProfileName(vcConfig);
string profilePath = System.IO.Path.Combine(conanProjectDirectory, profileName);
string toolset = vcConfig.Evaluate("$(PlatformToolset)").ToString();
string vsVersion = vcConfig.Evaluate("$(MSBuildVersion)").ToString();
string compilerVersion = getConanCompilerVersion(toolset, vsVersion);
string arch = getConanArch(vcConfig.Evaluate("$(PlatformName)").ToString());
IVCRulePropertyStorage generalRule = vcConfig.Rules.Item("ConfigurationGeneral") as IVCRulePropertyStorage;
string languageStandard = generalRule == null ? null : generalRule.GetEvaluatedPropertyValue("LanguageStandard");
string cppStd = getConanCppstd(languageStandard);
var tools = (IVCCollection) vcConfig.Tools;
var vcCTool = (VCCLCompilerTool) tools.Item("VCCLCompilerTool");
string runtime = GetRuntimeLibraryType(vcCTool.RuntimeLibrary);
string buildType = vcConfig.ConfigurationName;
string profileContent =
$@"
[settings]
arch={arch}
build_type={buildType}
compiler=msvc
compiler.cppstd={cppStd}
compiler.runtime={runtime}
" +
$@"
compiler.runtime_type={buildType}
compiler.version={compilerVersion}
os=Windows
";
bool shouldWriteFile = true;
if (File.Exists(profilePath))
{
string existingProfileContent = File.ReadAllText(profilePath);
if (existingProfileContent == profileContent)
{
shouldWriteFile = false;
}
}
if (shouldWriteFile)
{
File.WriteAllText(profilePath, profileContent);
// We create this .runconan file to indicate that there were changes in the profile and that
// Conan should run, this file is removed by the script that launches conan to reset the state
string runConanFilePath = System.IO.Path.Combine(conanProjectDirectory, ".runconan");
File.WriteAllText(runConanFilePath, "");
}
}
}
}
//MessageBox.Show($"Generated profiles for actual project.", "Conan profiles generated", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show($"There was a problem generating the file: {ex.Message}", "Error - Conan C/C++ Package Manager", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}