Add options and documentation for env command (#3007)

* [vcpkg] Add options and documentation for env command

* [vcpkg-env] Cleanup. Add tools/*.
This commit is contained in:
Jacob Zhong 2018-03-18 20:24:19 +08:00 committed by Robert Schumacher
parent 55e2cccc19
commit d253123055
4 changed files with 66 additions and 14 deletions

View File

@ -1,5 +1,7 @@
#pragma once
#include <unordered_map>
#include <vcpkg/base/files.h>
#include <vcpkg/base/optional.h>
#include <vcpkg/base/strings.h>
@ -38,7 +40,8 @@ namespace vcpkg::System
std::string output;
};
int cmd_execute_clean(const CStringView cmd_line);
int cmd_execute_clean(const CStringView cmd_line,
const std::unordered_map<std::string, std::string>& extra_env = {});
int cmd_execute(const CStringView cmd_line);

View File

@ -153,12 +153,12 @@ namespace vcpkg::System
R"(powershell -NoProfile -ExecutionPolicy Bypass -Command "& {& '%s' %s}")", script_path.u8string(), args);
}
int cmd_execute_clean(const CStringView cmd_line)
int cmd_execute_clean(const CStringView cmd_line, const std::unordered_map<std::string, std::string>& extra_env)
{
#if defined(_WIN32)
static const std::string SYSTEM_ROOT = get_environment_variable("SystemRoot").value_or_exit(VCPKG_LINE_INFO);
static const std::string SYSTEM_32 = SYSTEM_ROOT + R"(\system32)";
static const std::string NEW_PATH = Strings::format(
std::string NEW_PATH = Strings::format(
R"(Path=%s;%s;%s\Wbem;%s\WindowsPowerShell\v1.0\)", SYSTEM_32, SYSTEM_ROOT, SYSTEM_32, SYSTEM_32);
std::vector<std::wstring> env_wstrings = {
@ -224,11 +224,22 @@ namespace vcpkg::System
env_cstr.push_back(L'\0');
}
if (extra_env.find("PATH") != extra_env.end())
NEW_PATH += Strings::format(";%s", extra_env.find("PATH")->second);
env_cstr.append(Strings::to_utf16(NEW_PATH));
env_cstr.push_back(L'\0');
env_cstr.append(L"VSLANG=1033");
env_cstr.push_back(L'\0');
for (auto item : extra_env)
{
if (item.first == "PATH") continue;
env_cstr.append(Strings::to_utf16(item.first));
env_cstr.push_back(L'=');
env_cstr.append(Strings::to_utf16(item.second));
env_cstr.push_back(L'\0');
}
STARTUPINFOW startup_info;
memset(&startup_info, 0, sizeof(STARTUPINFOW));
startup_info.cb = sizeof(STARTUPINFOW);

View File

@ -1,5 +1,6 @@
#include "pch.h"
#include <vcpkg/base/strings.h>
#include <vcpkg/base/system.h>
#include <vcpkg/build.h>
#include <vcpkg/commands.h>
@ -7,25 +8,66 @@
namespace vcpkg::Commands::Env
{
static constexpr StringLiteral OPTION_BIN = "--bin";
static constexpr StringLiteral OPTION_INCLUDE = "--include";
static constexpr StringLiteral OPTION_DEBUG_BIN = "--debug-bin";
static constexpr StringLiteral OPTION_TOOLS = "--tools";
static constexpr StringLiteral OPTION_PYTHON = "--python";
static constexpr std::array<CommandSwitch, 5> SWITCHES = {{
{OPTION_BIN, "Add installed bin/ to PATH"},
{OPTION_INCLUDE, "Add installed include/ to INCLUDE"},
{OPTION_DEBUG_BIN, "Add installed debug/bin/ to PATH"},
{OPTION_TOOLS, "Add installed tools/*/ to PATH"},
{OPTION_PYTHON, "Add installed python/ to PYTHONPATH"},
}};
const CommandStructure COMMAND_STRUCTURE = {
Help::create_example_string("env --triplet x64-windows"),
0,
0,
{},
{SWITCHES, {}},
nullptr,
};
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths, const Triplet& default_triplet)
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths, const Triplet& triplet)
{
args.parse_arguments(COMMAND_STRUCTURE);
const auto& fs = paths.get_filesystem();
const auto pre_build_info = Build::PreBuildInfo::from_triplet_file(paths, default_triplet);
const ParsedArguments options = args.parse_arguments(COMMAND_STRUCTURE);
const auto pre_build_info = Build::PreBuildInfo::from_triplet_file(paths, triplet);
const Toolset& toolset = paths.get_toolset(pre_build_info);
auto env_cmd = Build::make_build_env_cmd(pre_build_info, toolset);
std::unordered_map<std::string, std::string> extra_env = {};
const bool add_bin = Util::Sets::contains(options.switches, OPTION_BIN);
const bool add_include = Util::Sets::contains(options.switches, OPTION_INCLUDE);
const bool add_debug_bin = Util::Sets::contains(options.switches, OPTION_DEBUG_BIN);
const bool add_tools = Util::Sets::contains(options.switches, OPTION_TOOLS);
const bool add_python = Util::Sets::contains(options.switches, OPTION_PYTHON);
std::vector<std::string> path_vars;
if (add_bin) path_vars.push_back((paths.installed / triplet.to_string() / "bin").u8string());
if (add_debug_bin) path_vars.push_back((paths.installed / triplet.to_string() / "debug" / "bin").u8string());
if (add_include) extra_env.emplace("INCLUDE", (paths.installed / triplet.to_string() / "include").u8string());
if (add_tools)
{
auto tools_dir = paths.installed / triplet.to_string() / "tools";
auto tool_files = fs.get_files_non_recursive(tools_dir);
path_vars.push_back(tools_dir.u8string());
for (auto&& tool_dir : tool_files)
{
if (fs.is_directory(tool_dir)) path_vars.push_back(tool_dir.u8string());
}
}
if (add_python) extra_env.emplace("PYTHONPATH", (paths.installed / triplet.to_string() / "python").u8string());
if (path_vars.size() > 0) extra_env.emplace("PATH", Strings::join(";", path_vars));
if (env_cmd.empty())
System::cmd_execute_clean("cmd");
System::cmd_execute_clean("cmd", extra_env);
else
System::cmd_execute_clean(env_cmd + " && cmd");
System::cmd_execute_clean(env_cmd + " && cmd", extra_env);
Checks::exit_success(VCPKG_LINE_INFO);
}

View File

@ -98,14 +98,10 @@ namespace vcpkg::Help
" vcpkg create <pkg> <url>\n"
" [archivename] Create a new package\n"
" vcpkg owns <pat> Search for files in installed packages\n"
" vcpkg cache List cached compiled packages\n"
" vcpkg env Creates a clean shell environment for development or compiling.\n"
" vcpkg version Display version information\n"
" vcpkg contact Display contact information to send feedback\n"
"\n"
//"internal commands:\n"
//" --check-build-deps <controlfile>\n"
//" --create-binary-control <controlfile>\n"
//"\n"
"Options:\n"
" --triplet <t> Specify the target architecture triplet.\n"
" (default: %%VCPKG_DEFAULT_TRIPLET%%, see 'vcpkg help triplet')\n"