Skip to content

Commit

Permalink
[vcpkg, jsonnet, openssl-uwp] Enable use of the system powershell-cor…
Browse files Browse the repository at this point in the history
…e if it is present. (microsoft#13805)
  • Loading branch information
BillyONeal authored Oct 28, 2020
1 parent c34c418 commit a6a1722
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 33 deletions.
1 change: 1 addition & 0 deletions ports/jsonnet/CONTROL
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Source: jsonnet
Version: 0.16.0
Port-Version: 1
Homepage: https://github.com/google/jsonnet
Description: Jsonnet - The data templating language
Build-Depends: nlohmann-json
5 changes: 3 additions & 2 deletions ports/jsonnet/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ vcpkg_from_github(
)

if(VCPKG_TARGET_IS_WINDOWS)
vcpkg_execute_required_process(
COMMAND Powershell -Command "((Get-Content -AsByteStream \"${SOURCE_PATH}/stdlib/std.jsonnet\") -join ',') + ',0' | Out-File -Encoding Ascii \"${SOURCE_PATH}/core/std.jsonnet.h\""
find_program(PWSH_PATH pwsh)
vcpkg_execute_required_process(
COMMAND "${PWSH_PATH}" -Command "((Get-Content -AsByteStream \"${SOURCE_PATH}/stdlib/std.jsonnet\") -join ',') + ',0' | Out-File -Encoding Ascii \"${SOURCE_PATH}/core/std.jsonnet.h\""
WORKING_DIRECTORY "${SOURCE_PATH}"
LOGNAME "std.jsonnet"
)
Expand Down
1 change: 1 addition & 0 deletions ports/openssl-uwp/CONTROL
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Source: openssl-uwp
Version: 1.1.1h
Port-Version: 1
Description: OpenSSL is an open source project that provides a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It is also a general-purpose cryptography library.
Supports: uwp
2 changes: 1 addition & 1 deletion ports/openssl-uwp/EnableUWPSupport.patch
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ index d478f42b0f..e0fb70daca 100644
+my $UWP_info = {};
+sub UWP_info {
+ unless (%$UWP_info) {
+ my $SDKver = `powershell -Command \"& {\$(Get-Item \\\"hklm:\\SOFTWARE\\WOW6432Node\\Microsoft\\Microsoft SDKs\\Windows\\\").GetValue(\\\"CurrentVersion\\\")}\"`;
+ my $SDKver = `pwsh.exe -Command \"& {\$(Get-Item \\\"hklm:\\SOFTWARE\\WOW6432Node\\Microsoft\\Microsoft SDKs\\Windows\\\").GetValue(\\\"CurrentVersion\\\")}\"`;
+ $SDKver =~ s|\R$||;
+ my @SDKver_split = split(/\./, $SDKver);
+ # SDK version older than 10.0.17763 don't support our ASM builds
Expand Down
6 changes: 3 additions & 3 deletions scripts/addPoshVcpkgToPowershellProfile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function findExistingImportModuleDirectives([Parameter(Mandatory=$true)][string]
$scriptsDir = split-path -parent $script:MyInvocation.MyCommand.Definition

$profileEntry = "Import-Module '$scriptsDir\posh-vcpkg'"
$profilePath = $PROFILE # Implicit powershell variable
$profilePath = $PROFILE # Implicit PowerShell variable
$profileDir = Split-Path $profilePath -Parent
if (!(Test-Path $profileDir))
{
Expand All @@ -33,7 +33,7 @@ if ($existingImports.Count -gt 0)
$existingImportsOut = $existingImports -join "`n "
Write-Host "`nposh-vcpkg is already imported to your PowerShell profile. The following entries were found:"
Write-Host " $existingImportsOut"
Write-Host "`nPlease make sure you have started a new Powershell window for the changes to take effect."
Write-Host "`nPlease make sure you have started a new PowerShell window for the changes to take effect."
return
}

Expand All @@ -53,4 +53,4 @@ if (Test-Path $profilePath)
}

Add-Content $profilePath -Value "`n$profileEntry" -Encoding UTF8
Write-Host "`nSuccessfully added posh-vcpkg to your PowerShell profile. Please start a new Powershell window for the changes to take effect."
Write-Host "`nSuccessfully added posh-vcpkg to your PowerShell profile. Please start a new PowerShell window for the changes to take effect."
41 changes: 34 additions & 7 deletions scripts/buildsystems/msbuild/vcpkg.targets
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,45 @@
</Target>

<Target Name="AppLocalFromInstalled" AfterTargets="CopyFilesToOutputDirectory" BeforeTargets="CopyLocalFilesOutputGroup;RegisterOutput" Condition="'$(VcpkgEnabled)' == 'true' and '$(VcpkgApplocalDeps)' == 'true'">
<Message Text="[vcpkg] Starting VcpkgApplocalDeps" Importance="low" />
<WriteLinesToFile
File="$(TLogLocation)$(ProjectName).write.1u.tlog"
Lines="^$(TargetPath);$([System.IO.Path]::Combine($(ProjectDir),$(IntDir)))vcpkg.applocal.log" Encoding="Unicode"/>
File="$(TLogLocation)$(ProjectName).write.1u.tlog"
Lines="^$(TargetPath);$([System.IO.Path]::Combine($(ProjectDir),$(IntDir)))vcpkg.applocal.log" Encoding="Unicode"/>
<PropertyGroup>
<_VcpkgAppLocalPowerShellCommonArguments>-ExecutionPolicy Bypass -noprofile -File "$(MSBuildThisFileDirectory)applocal.ps1" "$(TargetPath)" "$(VcpkgCurrentInstalledDir)$(VcpkgConfigSubdir)bin" "$(TLogLocation)$(ProjectName).write.1u.tlog" "$(IntDir)vcpkg.applocal.log"</_VcpkgAppLocalPowerShellCommonArguments>
</PropertyGroup>
<!-- Search %PATH% for pwsh.exe if it is available. -->
<Exec
Command="$(SystemRoot)\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -noprofile -File %22$(MSBuildThisFileDirectory)applocal.ps1%22 %22$(TargetPath)%22 %22$(VcpkgCurrentInstalledDir)$(VcpkgConfigSubdir)bin%22 %22$(TLogLocation)$(ProjectName).write.1u.tlog%22 %22$(IntDir)vcpkg.applocal.log%22"
StandardOutputImportance="Normal">
Command="pwsh.exe $(_VcpkgAppLocalPowerShellCommonArguments)"
StandardOutputImportance="Normal"
StandardErrorImportance="Normal"
IgnoreExitCode="true"
UseCommandProcessor="false">
<Output TaskParameter="ExitCode"
PropertyName="_VcpkgAppLocalExitCode" />
</Exec>
<ReadLinesFromFile File="$(IntDir)vcpkg.applocal.log">
<!-- Fall back to well known system PowerShell location otherwise. -->
<Message Text="[vcpkg] Failed to run applocal.ps1 using pwsh, falling back to system PowerShell." Importance="low"
Condition="$(_VcpkgAppLocalExitCode) == 9009" />
<Exec
Command="%22$(SystemRoot)\System32\WindowsPowerShell\v1.0\powershell.exe%22 $(_VcpkgAppLocalPowerShellCommonArguments)"
StandardOutputImportance="Normal"
StandardErrorImportance="Normal"
IgnoreExitCode="true"
UseCommandProcessor="false"
Condition="$(_VcpkgAppLocalExitCode) == 9009">
<Output TaskParameter="ExitCode"
PropertyName="_VcpkgAppLocalExitCode" />
</Exec>
<!-- We're ignoring the above exit codes, so translate into a warning if both failed. -->
<Warning Text="[vcpkg] Failed to gather app local DLL dependencies, program may not run. Set VcpkgApplocalDeps to false in your project file to suppress this warning. PowerShell arguments: $(_VcpkgAppLocalPowerShellCommonArguments)"
Condition="$(_VcpkgAppLocalExitCode) != 0"/>
<ReadLinesFromFile File="$(IntDir)vcpkg.applocal.log"
Condition="$(_VcpkgAppLocalExitCode) == 0">
<Output TaskParameter="Lines" ItemName="VcpkgAppLocalDLLs" />
</ReadLinesFromFile>
<Message Text="@(VcpkgAppLocalDLLs,'%0A')" Importance="Normal" />
<ItemGroup>
<Message Text="@(VcpkgAppLocalDLLs,'%0A')" Importance="Normal" Condition="$(_VcpkgAppLocalExitCode) == 0" />
<ItemGroup Condition="$(_VcpkgAppLocalExitCode) == 0">
<ReferenceCopyLocalPaths Include="@(VcpkgAppLocalDLLs)" />
</ItemGroup>
</Target>
Expand Down
20 changes: 18 additions & 2 deletions scripts/buildsystems/vcpkg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,20 @@ endif()

option(VCPKG_APPLOCAL_DEPS "Automatically copy dependencies into the output directory for executables." ON)
option(X_VCPKG_APPLOCAL_DEPS_SERIALIZED "(experimental) Add USES_TERMINAL to VCPKG_APPLOCAL_DEPS to force serialization." OFF)
function(_vcpkg_set_powershell_path)
# Attempt to use pwsh if it is present; otherwise use powershell
if (NOT DEFINED _VCPKG_POWERSHELL_PATH)
find_program(_VCPKG_PWSH_PATH pwsh)
if (_VCPKG_PWSH_PATH-NOTFOUND)
message(DEBUG "vcpkg: Could not find PowerShell Core; falling back to PowerShell")
find_program(_VCPKG_BUILTIN_POWERSHELL_PATH powershell REQUIRED)
set(_VCPKG_POWERSHELL_PATH "${_VCPKG_BUILTIN_POWERSHELL_PATH}" CACHE INTERNAL "The path to the PowerShell implementation to use.")
else()
set(_VCPKG_POWERSHELL_PATH "${_VCPKG_PWSH_PATH}" CACHE INTERNAL "The path to the PowerShell implementation to use.")
endif()
endif() # _VCPKG_POWERSHELL_PATH
endfunction()

function(add_executable name)
_add_executable(${ARGV})
list(FIND ARGV "IMPORTED" IMPORTED_IDX)
Expand All @@ -403,12 +417,13 @@ function(add_executable name)
if(IMPORTED_IDX EQUAL -1 AND ALIAS_IDX EQUAL -1)
if(VCPKG_APPLOCAL_DEPS)
if(_VCPKG_TARGET_TRIPLET_PLAT MATCHES "windows|uwp")
_vcpkg_set_powershell_path()
set(EXTRA_OPTIONS "")
if(X_VCPKG_APPLOCAL_DEPS_SERIALIZED)
set(EXTRA_OPTIONS USES_TERMINAL)
endif()
add_custom_command(TARGET ${name} POST_BUILD
COMMAND powershell -noprofile -executionpolicy Bypass -file ${_VCPKG_TOOLCHAIN_DIR}/msbuild/applocal.ps1
COMMAND "${_VCPKG_POWERSHELL_PATH}" -noprofile -executionpolicy Bypass -file "${_VCPKG_TOOLCHAIN_DIR}/msbuild/applocal.ps1"
-targetBinary $<TARGET_FILE:${name}>
-installedDir "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}$<$<CONFIG:Debug>:/debug>/bin"
-OutVariable out
Expand Down Expand Up @@ -437,8 +452,9 @@ function(add_library name)
if(IMPORTED_IDX EQUAL -1 AND INTERFACE_IDX EQUAL -1 AND ALIAS_IDX EQUAL -1)
get_target_property(IS_LIBRARY_SHARED ${name} TYPE)
if(VCPKG_APPLOCAL_DEPS AND _VCPKG_TARGET_TRIPLET_PLAT MATCHES "windows|uwp" AND (IS_LIBRARY_SHARED STREQUAL "SHARED_LIBRARY" OR IS_LIBRARY_SHARED STREQUAL "MODULE_LIBRARY"))
_vcpkg_set_powershell_path()
add_custom_command(TARGET ${name} POST_BUILD
COMMAND powershell -noprofile -executionpolicy Bypass -file ${_VCPKG_TOOLCHAIN_DIR}/msbuild/applocal.ps1
COMMAND "${_VCPKG_POWERSHELL_PATH}" -noprofile -executionpolicy Bypass -file "${_VCPKG_TOOLCHAIN_DIR}/msbuild/applocal.ps1"
-targetBinary $<TARGET_FILE:${name}>
-installedDir "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}$<$<CONFIG:Debug>:/debug>/bin"
-OutVariable out
Expand Down
10 changes: 5 additions & 5 deletions scripts/cmake/vcpkg_copy_tool_dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
## * [glib](https://github.com/Microsoft/vcpkg/blob/master/ports/glib/portfile.cmake)
## * [fltk](https://github.com/Microsoft/vcpkg/blob/master/ports/fltk/portfile.cmake)
function(vcpkg_copy_tool_dependencies TOOL_DIR)
find_program(PS_EXE powershell PATHS ${DOWNLOADS}/tool)
if (PS_EXE-NOTFOUND)
message(FATAL_ERROR "Could not find powershell in vcpkg tools, please open an issue to report this.")
find_program(PWSH_EXE pwsh)
if (PWSH_EXE-NOTFOUND)
message(FATAL_ERROR "Could not find PowerShell Core; please open an issue to report this.")
endif()
macro(search_for_dependencies PATH_TO_SEARCH)
file(GLOB TOOLS ${TOOL_DIR}/*.exe ${TOOL_DIR}/*.dll)
foreach(TOOL ${TOOLS})
vcpkg_execute_required_process(
COMMAND ${PS_EXE} -noprofile -executionpolicy Bypass -nologo
-file ${SCRIPTS}/buildsystems/msbuild/applocal.ps1
COMMAND "${PWSH_EXE}" -noprofile -executionpolicy Bypass -nologo
-file "${SCRIPTS}/buildsystems/msbuild/applocal.ps1"
-targetBinary ${TOOL}
-installedDir ${PATH_TO_SEARCH}
WORKING_DIRECTORY ${VCPKG_ROOT_DIR}
Expand Down
13 changes: 11 additions & 2 deletions toolsrc/include/vcpkg/base/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,19 @@ namespace vcpkg::Files

void print_paths(const std::vector<fs::path>& paths);

/// Performs "lhs / rhs" according to the C++17 Filesystem Library Specification.
/// This function exists as a workaround for TS implementations.
// Performs "lhs / rhs" according to the C++17 Filesystem Library Specification.
// This function exists as a workaround for TS implementations.
fs::path combine(const fs::path& lhs, const fs::path& rhs);

#if defined(_WIN32)
constexpr char preferred_separator = '\\';
#else
constexpr char preferred_separator = '/';
#endif // _WIN32

// Adds file as a new path element to the end of base, with an additional slash if necessary
std::string add_filename(StringView base, StringView file);

#if defined(_WIN32)
fs::path win32_fix_path_case(const fs::path& source);
#endif // _WIN32
Expand Down
1 change: 1 addition & 0 deletions toolsrc/include/vcpkg/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace vcpkg
static const std::string GIT = "git";
static const std::string MONO = "mono";
static const std::string NINJA = "ninja";
static const std::string POWERSHELL_CORE = "powershell-core";
static const std::string NUGET = "nuget";
static const std::string IFW_INSTALLER_BASE = "ifw_installerbase";
static const std::string IFW_BINARYCREATOR = "ifw_binarycreator";
Expand Down
15 changes: 15 additions & 0 deletions toolsrc/src/vcpkg-test/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,21 @@ TEST_CASE ("win32_fix_path_case", "[files]")
}
#endif // _WIN32

TEST_CASE ("add_filename", "[files]")
{
using vcpkg::Files::add_filename;
using vcpkg::Files::preferred_separator;

CHECK(add_filename("a/b", "c") == std::string("a/b") + preferred_separator + "c");
CHECK(add_filename("a/b/", "c") == "a/b/c");
CHECK(add_filename("a/b\\", "c") == "a/b\\c");
CHECK(add_filename("", "c") == "c");

// note that we don't special case slashes in the second argument; the caller shouldn't do that
CHECK(add_filename("a/b/", "\\c") == "a/b/\\c");
CHECK(add_filename("a/b\\", "/c") == "a/b\\/c");
}

#if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
TEST_CASE ("remove all -- benchmarks", "[files][!benchmark]")
{
Expand Down
33 changes: 29 additions & 4 deletions toolsrc/src/vcpkg/base/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <algorithm>
#include <string>

#if defined(_WIN32)
namespace
{
struct IsSlash
Expand All @@ -32,7 +31,11 @@ namespace
};

constexpr IsSlash is_slash;
} // unnamed namespace

#if defined(_WIN32)
namespace
{
template<size_t N>
bool wide_starts_with(const std::wstring& haystack, const wchar_t (&needle)[N]) noexcept
{
Expand Down Expand Up @@ -1182,14 +1185,14 @@ namespace vcpkg::Files
auto paths = Strings::split_paths(System::get_environment_variable("PATH").value_or_exit(VCPKG_LINE_INFO));

std::vector<fs::path> ret;
std::error_code ec;
for (auto&& path : paths)
{
auto base = path + "/" + name;
auto base = add_filename(path, name);

for (auto&& ext : EXTS)
{
auto p = fs::u8path(base + ext.c_str());
if (Util::find(ret, p) == ret.end() && this->exists(p, ec))
if (Util::find(ret, p) == ret.end() && this->exists(p, ignore_errors))
{
ret.push_back(p);
Debug::print("Found path: ", fs::u8string(p), '\n');
Expand Down Expand Up @@ -1372,4 +1375,26 @@ namespace vcpkg::Files
return fs::path(std::move(in_progress));
}
#endif // _WIN32

std::string add_filename(StringView base, StringView file)
{
std::string result;
const auto base_size = base.size();
const auto file_size = file.size();
if (base_size != 0 && !is_slash(base.data()[base_size - 1]))
{
result.reserve(base_size + file_size + 1);
result.append(base.data(), base_size);
result.push_back(preferred_separator);
result.append(file.data(), file_size);
}
else
{
result.reserve(base_size + file_size);
result.append(base.data(), base_size);
result.append(file.data(), file_size);
}

return result;
}
}
7 changes: 0 additions & 7 deletions toolsrc/src/vcpkg/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,6 @@ namespace vcpkg::Build

return base_env.cmd_cache.get_lazy(build_env_cmd, [&]() {
const fs::path& powershell_exe_path = paths.get_tool_exe("powershell-core");
auto& fs = paths.get_filesystem();
if (!fs.exists(powershell_exe_path.parent_path() / "powershell.exe"))
{
fs.copy(
powershell_exe_path, powershell_exe_path.parent_path() / "powershell.exe", fs::copy_options::none);
}

auto clean_env = System::get_modified_clean_environment(
base_env.env_map, fs::u8string(powershell_exe_path.parent_path()) + ";");
if (build_env_cmd.empty())
Expand Down
38 changes: 38 additions & 0 deletions toolsrc/src/vcpkg/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,36 @@ Mono JIT compiler version 6.8.0.105 (Debian 6.8.0.105+dfsg-2 Wed Feb 26 23:23:50
}
};

struct PowerShellCoreProvider : ToolProvider
{
std::string m_exe = "pwsh";

virtual const std::string& tool_data_name() const override { return m_exe; }
virtual const std::string& exe_stem() const override { return m_exe; }
virtual std::array<int, 3> default_min_version() const override { return {7, 0, 3}; }

virtual Optional<std::string> get_version(const VcpkgPaths&, const fs::path& path_to_exe) const override
{
const auto pwsh_invocation = System::cmd_execute_and_capture_output(
System::CmdLineBuilder().path_arg(path_to_exe).string_arg("--version").extract());
if (pwsh_invocation.exit_code != 0)
{
return nullopt;
}

// Sample output: PowerShell 7.0.3\r\n
auto output = std::move(pwsh_invocation.output);
if (!Strings::starts_with(output, "PowerShell "))
{
Checks::exit_with_message(
VCPKG_LINE_INFO, "Unexpected format of powershell-core version string: %s", output);
}

output.erase(0, 11);
return Strings::trim(std::move(output));
}
};

struct ToolCacheImpl final : ToolCache
{
vcpkg::Cache<std::string, fs::path> path_only_cache;
Expand Down Expand Up @@ -511,6 +541,14 @@ Mono JIT compiler version 6.8.0.105 (Debian 6.8.0.105+dfsg-2 Wed Feb 26 23:23:50
}
return get_path(paths, NinjaProvider());
}
if (tool == Tools::POWERSHELL_CORE)
{
if (System::get_environment_variable("VCPKG_FORCE_SYSTEM_BINARIES").has_value())
{
return {"pwsh", "0"};
}
return get_path(paths, PowerShellCoreProvider());
}
if (tool == Tools::NUGET) return get_path(paths, NuGetProvider());
if (tool == Tools::IFW_INSTALLER_BASE) return get_path(paths, IfwInstallerBaseProvider());
if (tool == Tools::MONO) return get_path(paths, MonoProvider());
Expand Down

0 comments on commit a6a1722

Please sign in to comment.