-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
commands_integrate.cpp
341 lines (293 loc) · 14.1 KB
/
commands_integrate.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "pch.h"
#include "vcpkg_Checks.h"
#include "vcpkg_Commands.h"
#include "vcpkg_Files.h"
#include "vcpkg_System.h"
#include "vcpkg_Util.h"
namespace vcpkg::Commands::Integrate
{
static const std::array<fs::path, 2> OLD_SYSTEM_TARGET_FILES = {
System::get_program_files_32_bit() / "MSBuild/14.0/Microsoft.Common.Targets/ImportBefore/vcpkg.nuget.targets",
System::get_program_files_32_bit() / "MSBuild/14.0/Microsoft.Common.Targets/ImportBefore/vcpkg.system.targets"};
static const fs::path SYSTEM_WIDE_TARGETS_FILE =
System::get_program_files_32_bit() / "MSBuild/Microsoft.Cpp/v4.0/V140/ImportBefore/Default/vcpkg.system.props";
static std::string create_appdata_targets_shortcut(const std::string& target_path) noexcept
{
return Strings::format(R"###(
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Condition="Exists('%s') and '$(VCPkgLocalAppDataDisabled)' == ''" Project="%s" />
</Project>
)###",
target_path,
target_path);
}
static std::string create_system_targets_shortcut() noexcept
{
return R"###(
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- version 1 -->
<PropertyGroup>
<VCLibPackagePath Condition="'$(VCLibPackagePath)' == ''">$(LOCALAPPDATA)\vcpkg\vcpkg.user</VCLibPackagePath>
</PropertyGroup>
<Import Condition="'$(VCLibPackagePath)' != '' and Exists('$(VCLibPackagePath).targets')" Project="$(VCLibPackagePath).targets" />
</Project>
)###";
}
static std::string create_nuget_targets_file_contents(const fs::path& msbuild_vcpkg_targets_file) noexcept
{
const std::string as_string = msbuild_vcpkg_targets_file.string();
return Strings::format(R"###(
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="%s" Condition="Exists('%s')" />
<Target Name="CheckValidPlatform" BeforeTargets="Build">
<Error Text="Unsupported architecture combination. Remove the 'vcpkg' nuget package." Condition="'$(VCPkgEnabled)' != 'true' and '$(VCPkgDisableError)' == ''"/>
</Target>
</Project>
)###",
as_string,
as_string);
}
static std::string create_nuget_props_file_contents() noexcept
{
return R"###(
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VCPkgLocalAppDataDisabled>true</VCPkgLocalAppDataDisabled>
</PropertyGroup>
</Project>
)###";
}
static std::string get_nuget_id(const fs::path& vcpkg_root_dir)
{
std::string dir_id = vcpkg_root_dir.generic_string();
std::replace(dir_id.begin(), dir_id.end(), '/', '.');
dir_id.erase(1, 1); // Erasing the ":"
// NuGet id cannot have invalid characters. We will only use alphanumeric and dot.
Util::erase_remove_if(dir_id, [](char c) { return !isalnum(c) && (c != '.'); });
const std::string nuget_id = "vcpkg." + dir_id;
return nuget_id;
}
static std::string create_nuspec_file_contents(const fs::path& vcpkg_root_dir,
const std::string& nuget_id,
const std::string& nupkg_version)
{
static constexpr auto CONTENT_TEMPLATE = R"(
<package>
<metadata>
<id>@NUGET_ID@</id>
<version>@VERSION@</version>
<authors>vcpkg</authors>
<description>
This package imports all libraries currently installed in @VCPKG_DIR@. This package does not contain any libraries and instead refers to the folder directly (like a symlink).
</description>
</metadata>
<files>
<file src="vcpkg.nuget.props" target="build\native\@NUGET_ID@.props" />
<file src="vcpkg.nuget.targets" target="build\native\@NUGET_ID@.targets" />
</files>
</package>
)";
std::string content = std::regex_replace(CONTENT_TEMPLATE, std::regex("@NUGET_ID@"), nuget_id);
content = std::regex_replace(content, std::regex("@VCPKG_DIR@"), vcpkg_root_dir.string());
content = std::regex_replace(content, std::regex("@VERSION@"), nupkg_version);
return content;
}
enum class ElevationPromptChoice
{
YES,
NO
};
static ElevationPromptChoice elevated_cmd_execute(const std::string& param)
{
SHELLEXECUTEINFO sh_ex_info = {0};
sh_ex_info.cbSize = sizeof(sh_ex_info);
sh_ex_info.fMask = SEE_MASK_NOCLOSEPROCESS;
sh_ex_info.hwnd = nullptr;
sh_ex_info.lpVerb = "runas";
sh_ex_info.lpFile = "cmd"; // Application to start
sh_ex_info.lpParameters = param.c_str(); // Additional parameters
sh_ex_info.lpDirectory = nullptr;
sh_ex_info.nShow = SW_HIDE;
sh_ex_info.hInstApp = nullptr;
if (!ShellExecuteExA(&sh_ex_info))
{
return ElevationPromptChoice::NO;
}
if (sh_ex_info.hProcess == nullptr)
{
return ElevationPromptChoice::NO;
}
WaitForSingleObject(sh_ex_info.hProcess, INFINITE);
CloseHandle(sh_ex_info.hProcess);
return ElevationPromptChoice::YES;
}
static fs::path get_appdata_targets_path()
{
static const fs::path LOCAL_APP_DATA =
fs::path(System::get_environment_variable(L"LOCALAPPDATA").value_or_exit(VCPKG_LINE_INFO));
return LOCAL_APP_DATA / "vcpkg" / "vcpkg.user.targets";
}
static void integrate_install(const VcpkgPaths& paths)
{
auto& fs = paths.get_filesystem();
// TODO: This block of code should eventually be removed
for (auto&& old_system_wide_targets_file : OLD_SYSTEM_TARGET_FILES)
{
if (fs.exists(old_system_wide_targets_file))
{
const std::string param =
Strings::format(R"(/c DEL "%s" /Q > nul)", old_system_wide_targets_file.string());
const ElevationPromptChoice user_choice = elevated_cmd_execute(param);
switch (user_choice)
{
case ElevationPromptChoice::YES: break;
case ElevationPromptChoice::NO:
System::println(System::Color::warning, "Warning: Previous integration file was not removed");
Checks::exit_fail(VCPKG_LINE_INFO);
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
}
std::error_code ec;
const fs::path tmp_dir = paths.buildsystems / "tmp";
fs.create_directory(paths.buildsystems, ec);
fs.create_directory(tmp_dir, ec);
bool should_install_system = true;
const Expected<std::string> system_wide_file_contents = fs.read_contents(SYSTEM_WIDE_TARGETS_FILE);
static const std::regex RE(R"###(<!-- version (\d+) -->)###");
if (const auto contents_data = system_wide_file_contents.get())
{
std::match_results<std::string::const_iterator> match;
const auto found = std::regex_search(*contents_data, match, RE);
if (found)
{
const int ver = atoi(match[1].str().c_str());
if (ver >= 1) should_install_system = false;
}
}
if (should_install_system)
{
const fs::path sys_src_path = tmp_dir / "vcpkg.system.targets";
fs.write_contents(sys_src_path, create_system_targets_shortcut());
const std::string param = Strings::format(R"(/c mkdir "%s" & copy "%s" "%s" /Y > nul)",
SYSTEM_WIDE_TARGETS_FILE.parent_path().string(),
sys_src_path.string(),
SYSTEM_WIDE_TARGETS_FILE.string());
const ElevationPromptChoice user_choice = elevated_cmd_execute(param);
switch (user_choice)
{
case ElevationPromptChoice::YES: break;
case ElevationPromptChoice::NO:
System::println(System::Color::warning, "Warning: integration was not applied");
Checks::exit_fail(VCPKG_LINE_INFO);
default: Checks::unreachable(VCPKG_LINE_INFO);
}
Checks::check_exit(VCPKG_LINE_INFO,
fs.exists(SYSTEM_WIDE_TARGETS_FILE),
"Error: failed to copy targets file to %s",
SYSTEM_WIDE_TARGETS_FILE.string());
}
const fs::path appdata_src_path = tmp_dir / "vcpkg.user.targets";
fs.write_contents(appdata_src_path,
create_appdata_targets_shortcut(paths.buildsystems_msbuild_targets.string()));
auto appdata_dst_path = get_appdata_targets_path();
const auto rc = fs.copy_file(appdata_src_path, appdata_dst_path, fs::copy_options::overwrite_existing, ec);
if (!rc || ec)
{
System::println(System::Color::error,
"Error: Failed to copy file: %s -> %s",
appdata_src_path.string(),
appdata_dst_path.string());
Checks::exit_fail(VCPKG_LINE_INFO);
}
System::println(System::Color::success, "Applied user-wide integration for this vcpkg root.");
const fs::path cmake_toolchain = paths.buildsystems / "vcpkg.cmake";
System::println("\n"
"All MSBuild C++ projects can now #include any installed libraries.\n"
"Linking will be handled automatically.\n"
"Installing new libraries will make them instantly available.\n"
"\n"
"CMake projects should use -DCMAKE_TOOLCHAIN_FILE=%s",
cmake_toolchain.generic_string());
Checks::exit_success(VCPKG_LINE_INFO);
}
static void integrate_remove(Files::Filesystem& fs)
{
const fs::path path = get_appdata_targets_path();
std::error_code ec;
const bool was_deleted = fs.remove(path, ec);
Checks::check_exit(VCPKG_LINE_INFO, !ec, "Error: Unable to remove user-wide integration: %d", ec.message());
if (was_deleted)
{
System::println(System::Color::success, "User-wide integration was removed");
}
else
{
System::println(System::Color::success, "User-wide integration is not installed");
}
Checks::exit_success(VCPKG_LINE_INFO);
}
static void integrate_project(const VcpkgPaths& paths)
{
auto& fs = paths.get_filesystem();
const fs::path& nuget_exe = paths.get_nuget_exe();
const fs::path& buildsystems_dir = paths.buildsystems;
const fs::path tmp_dir = buildsystems_dir / "tmp";
std::error_code ec;
fs.create_directory(buildsystems_dir, ec);
fs.create_directory(tmp_dir, ec);
const fs::path targets_file_path = tmp_dir / "vcpkg.nuget.targets";
const fs::path props_file_path = tmp_dir / "vcpkg.nuget.props";
const fs::path nuspec_file_path = tmp_dir / "vcpkg.nuget.nuspec";
const std::string nuget_id = get_nuget_id(paths.root);
const std::string nupkg_version = "1.0.0";
fs.write_contents(targets_file_path, create_nuget_targets_file_contents(paths.buildsystems_msbuild_targets));
fs.write_contents(props_file_path, create_nuget_props_file_contents());
fs.write_contents(nuspec_file_path, create_nuspec_file_contents(paths.root, nuget_id, nupkg_version));
// Using all forward slashes for the command line
const std::wstring cmd_line = Strings::wformat(LR"("%s" pack -OutputDirectory "%s" "%s" > nul)",
nuget_exe.native(),
buildsystems_dir.native(),
nuspec_file_path.native());
const int exit_code = System::cmd_execute_clean(cmd_line);
const fs::path nuget_package = buildsystems_dir / Strings::format("%s.%s.nupkg", nuget_id, nupkg_version);
Checks::check_exit(
VCPKG_LINE_INFO, exit_code == 0 && fs.exists(nuget_package), "Error: NuGet package creation failed");
System::println(System::Color::success, "Created nupkg: %s", nuget_package.string());
auto source_path = buildsystems_dir.u8string();
source_path = std::regex_replace(source_path, std::regex("`"), "``");
System::println(R"(
With a project open, go to Tools->NuGet Package Manager->Package Manager Console and paste:
Install-Package %s -Source "%s"
)",
nuget_id,
source_path);
Checks::exit_success(VCPKG_LINE_INFO);
}
const char* const INTEGRATE_COMMAND_HELPSTRING =
" vcpkg integrate install Make installed packages available user-wide. Requires admin privileges on "
"first use\n"
" vcpkg integrate remove Remove user-wide integration\n"
" vcpkg integrate project Generate a referencing nuget package for individual VS project use\n";
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
static const std::string EXAMPLE = Strings::format("Commands:\n"
"%s",
INTEGRATE_COMMAND_HELPSTRING);
args.check_exact_arg_count(1, EXAMPLE);
args.check_and_get_optional_command_arguments({});
if (args.command_arguments[0] == "install")
{
return integrate_install(paths);
}
if (args.command_arguments[0] == "remove")
{
return integrate_remove(paths.get_filesystem());
}
if (args.command_arguments[0] == "project")
{
return integrate_project(paths);
}
Checks::exit_with_message(VCPKG_LINE_INFO, "Unknown parameter %s for integrate", args.command_arguments[0]);
}
}