Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Adjust Setup PowerShell Scripts to Run on Barebones Windows #1277

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions Building on Windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ prompt. The terminal prompt can be at the console of the target
Windows machine, or via a remote SSH session. Just a regular
PowerShell prompt is needed - you do NOT need to use a preconfigured
"Developer PowerShell" prompt, and in fact the Pistache configuration
scripts will configure a Developer prompt for you if required.
scripts will configure a Developer prompt for you if
required. Likewise, you can use a prompt without administrator rights.

Newer versions of Windows may disable script execution by default. To
enable script execution, at a PowerShell prompt with admin rights do:
Set-ExecutionPolicy -ExecutionPolicy Bypass

Older versions of Windows (e.g. Windows Server 2019) that default to
Internet Explorer may block access to Internet downloads needed by the
Pistache setup scripts. To enable that Internet access, see that the
note "Disabling Enhanced Security Configuration for Internet Explorer"
later in this document.

To begin - clone, fork or download and expand the zip of Pistache to
fetch Pistache to your Windows machine:
Expand Down Expand Up @@ -65,10 +76,10 @@ Pistache is installed to the pistache_distribution subdirectory of the
C:\Program Files\pistache_distribution
You can link your own executables to Pistache via
\Program Files\pistache_distribution. Note that you will link your
executable to an import library (.lib file) and, to run your program,
you will ensure that your $env:path includes the location of the
Pistache DLL; Windows then loads the Pistache DLL when your executable
runs, based on the information provided in the .lib file.
executable to the Pistache import library (.lib file) and, to run your
program, you will ensure that your $env:path includes the location of
the Pistache DLL; Windows then loads the Pistache DLL when your
executable runs, based on the information provided in the .lib file.

Note also that you do not need to link to or load pistachelog.dll in
any way. pistachelog.dll's only purpose is to provide a place to keep
Expand Down Expand Up @@ -238,6 +249,38 @@ debugger for Visual Studio debug builds, and gdb for GCC debug builds
installed).


Disabling Enhanced Security Configuration for Internet Explorer
===============================================================
Older versions of Windows (e.g. Windows Server 2019) that default to
Internet Explorer may block access to Internet downloads needed by the
Pistache setup scripts. To enable that Internet access:
Via the Windows GUI on Windows Server 2019, go to:
Server Manager > Local Server > IE Enhanced Security Configuration
Then, select Off for both Administrators and Users
And restart Internet Explorer
OR at a PowerShell prompt with admin rights, do:
$AdminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}"
$UserKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}"
Set-ItemProperty -Path $AdminKey -Name "IsInstalled" -Value 0
Set-ItemProperty -Path $UserKey -Name "IsInstalled" -Value 0
Stop-Process -Name Explorer


Troubleshooting
===============
If mingit is installed, and you get an error "fatal exceeded maximum
include depth" on invoking git, you likely have a circular include in
the file "$env:ProgramFiles\Git\etc\gitconfig". Open that gitconfig
file in an editor and comment out the line that looks like:
path = C:/Program Files/Git/etc/gitconfig
You can comment it out by placing a semicolon at the line's start:
; path = C:/Program Files/Git/etc/gitconfig

More generally, if the setup script goes wrong in a hard-to-understand
way, one thing to try is quitting the shell, starting a new shell, and
running the script again.


How It Works
============
Pistache on Windows works very much as it does on macOS, i.e. by using
Expand Down
43 changes: 39 additions & 4 deletions src/winlog/installmanatinstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
# and set the Windows Registry key property value
# HKCU:\Software\pistacheio\pistache\psLogToStdoutAsWell.

$have_admin_rights = ([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

$pistinstbase="$env:DESTDIR\$env:MESON_INSTALL_PREFIX"
if (($env:MESON_INSTALL_PREFIX) -and`
[System.IO.Path]::IsPathRooted($env:MESON_INSTALL_PREFIX))
Expand Down Expand Up @@ -53,15 +57,12 @@ if (Test-Path -Path "$pistacheloggccdll")
Write-Host "Copied $pistacheloggccdll to $pistachelogdll"
}
}

if (-Not (Test-Path "$pistachelogdll"))
{
throw "pistachelog.dll not found at $pistachelogdll"
}

wevtutil um "$pistwinlogman" # Uninstall - does nothing if not installed
wevtutil im "$pistwinlogman" # Install

# Next we create the Windows Registry log-to-stdout-as-well value for
# Pistache, if it doesn't exist already. If that registry property
# already exists, we don't change it.
Expand All @@ -87,3 +88,37 @@ if (-Not ($key2.Property -contains "psLogToStdoutAsWell"))
}
New-ItemProperty @newItemPropertySplat
}

Write-Host 'Installing Pistache logging manifest into Windows'
if ($have_admin_rights) {
wevtutil um "$pistwinlogman" # Uninstall - does nothing if not installed
wevtutil im "$pistwinlogman" # Install
}
else {
$pst_command = `
'Write-Host ''In admin shell, installing logging manifest''; ' + `
'wevtutil um ''' + $pistwinlogman + '''; wevtutil im ''' + `
$pistwinlogman + '''; if($?) { ' + `
'Write-Host ''Success; exiting from shell that has Admin rights''; ' + `
'} else { ' + `
'Write-Host "Press any key to continue" -ForegroundColor Yellow; ' + `
'$x = $host.ui.RawUI.ReadKey(''NoEcho,IncludeKeyDown''); ' + `
'Write-Host ''Error; exiting from shell that has Admin rights'' }; ' + `
'[Environment]::Exit(0)'

Write-Host "To install manifest, launching cmd with admin rights"

# We use "Start-Process" so we can do "-verb RunAs", which provides
# admin-level privileges, like doing "sudo" on Linux
# ("wevtutil im ..." requires admin rights)

$modeproc = Start-Process -FilePath powershell.exe `
-ArgumentList "-NoProfile", "-noexit", `
"-WindowStyle", "Normal", `
"-Command", "$pst_command" `
-PassThru -verb RunAs

Write-Host "Wait for cmd to complete, no timeout"
$modeproc | Wait-Process -ErrorAction SilentlyContinue
Write-Host "cmd completed"
}
11 changes: 9 additions & 2 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ if host_machine.system() == 'windows'
'-NonInteractive', '-WindowStyle', 'Hidden',
'-NoLogo',
'-Command',
'if (!(Test-Path -Path "@OUTPUT0@")) { New-Item -ItemType SymbolicLink -Path "@OUTPUT0@" -Target "'+libpistache_filename+'" }'],
'if (!(Test-Path -Path "@OUTPUT0@")) { Copy-Item "'+libpistache_filename+'" "@OUTPUT0@" }'],
depends: [libpistache])
# Above, it would be great to use "New-Item -ItemType SymbolicLink",
# however, Windows requires administrative privileges to create a
# symbolic link, so we use Copy-Item instead.
# 'if (!(Test-Path -Path "@OUTPUT0@")) {
# New-Item -ItemType SymbolicLink -Path "@OUTPUT0@" `
# -Target "'+libpistache_filename+'" }'

unversioned_dll_cts += unversioned_dll_ct1

Expand All @@ -115,8 +121,9 @@ if host_machine.system() == 'windows'
'-NonInteractive', '-WindowStyle', 'Hidden',
'-NoLogo',
'-Command',
'if (!(Test-Path -Path "@OUTPUT0@")) { New-Item -ItemType SymbolicLink -Path "@OUTPUT0@" -Target "'+libpistache_filename+'" }'],
'if (!(Test-Path -Path "@OUTPUT0@")) { Copy-Item "'+libpistache_filename+'" "@OUTPUT0@" }'],
depends: [libpistache])
# As per prior comment, used Copy-Item rather than creating a SymbolicLink
unversioned_dll_cts += unversioned_dll_ct2
endif
endif
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.27.20241227
0.4.28.20250104
61 changes: 46 additions & 15 deletions winscripts/gccsetup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@

$savedpwd=$pwd

$pst_outer_ps_cmd = $PSCommandPath

. $PSScriptRoot/helpers/commonsetup.ps1
if ($pst_stop_running) {
cd "$savedpwd"
pstPressKeyIfRaisedAndErrThenExit
Exit(0) # Exit the script, but not the shell (no "[Environment]::")
}

# Set $env:force_msys_gcc if you want to force the use of msys64's gcc
# even if a different gcc is already installed. (Note:
Expand All @@ -25,20 +32,27 @@ if (($env:force_msys_gcc) -or `
}
else {
cd ~
Write-Host "Downloading msys2-x86_64-latest.sfx.exe"
Invoke-WebRequest -Uri `
"https://repo.msys2.org/distrib/msys2-x86_64-latest.sfx.exe" `
-Outfile "msys2-x86_64-latest.sfx.exe"
Write-Host "Self-extracting msys2-x86_64-latest.sfx.exe"
.\msys2-x86_64-latest.sfx.exe -y -o$env:SYSTEMDRIVE\
# We get admin privilege here so ...sfx.exe can extract to
# SYSTEMDRIVE\msys64 (see "-oXXX" parms on ...sfx.exe)
if (! (pstRunScriptWithAdminRightsIfNotAlready)) {
$msys2_installed_by_this_shell = $TRUE
Write-Host "Downloading msys2-x86_64-latest.sfx.exe"
Invoke-WebRequest -Uri `
"https://repo.msys2.org/distrib/msys2-x86_64-latest.sfx.exe" `
-Outfile "msys2-x86_64-latest.sfx.exe"
Write-Host "Self-extracting msys2-x86_64-latest.sfx.exe"
.\msys2-x86_64-latest.sfx.exe -y "-o$env:SYSTEMDRIVE\"
}
if (Test-Path -Path "$env:SYSTEMDRIVE\msys64") {
$msys64_dir = "$env:SYSTEMDRIVE\msys64"
}
else {
throw "msys64 didn't install as expected?"
}
Write-Host "Checking msys2 shell"
& $msys64_dir\msys2_shell.cmd -defterm -here -no-start -ucrt64 -c “echo msysFirstTerminal”
if ($msys2_installed_by_this_shell) {
Write-Host "Checking msys2 shell"
& $msys64_dir\msys2_shell.cmd -defterm -here -no-start -ucrt64 -c “echo msysFirstTerminal”
}
}

if (! (Test-Path -Path "$msys64_dir\ucrt64\bin\gcc.exe")) {
Expand All @@ -60,8 +74,6 @@ if (($env:force_msys_gcc) -or `
$env:CXX="g++"
$env:CC="gcc"

$savedpwd = $pwd

if (! (Get-Command mc.exe -errorAction SilentlyContinue)) {
if (Test-Path -Path "$env:ProgramFiles\Windows Kits") {
$win_sdk_found=1
Expand Down Expand Up @@ -123,13 +135,30 @@ if (! (Get-Command ninja -errorAction SilentlyContinue)) {
# Can't find ninja in "$env:VCPKG_DIR\installed"; install it now

if (Get-Command winget -errorAction SilentlyContinue) {
winget install "Ninja-build.Ninja";
# Don't set ninja_dir - leaving it empty will mean it
# doesn't get added to $env:path below, which is good
# since winget takes care of the path for us
winget install "Ninja-build.Ninja"

if (! (Get-Command ninja -errorAction SilentlyContinue)) {
# Although winget will have adjusted the path for us
# already, that adjustment takes effect only after we
# have started a new shell. So for the benefit of this
# shell, since ninja is newly installed by winget, we
# add it to the path
if (Test-Path -Path `
"$env:LOCALAPPDATA\Microsoft\WinGet\Links\ninja.exe") {
$ninja_dir = "$env:LOCALAPPDATA\Microsoft\WinGet\Links"
}
elseif (Test-Path -Path `
"$env:APPDATA\Microsoft\WinGet\Links\ninja.exe") {
$ninja_dir = "$env:APPDATA\Microsoft\WinGet\Links"
}
else {
Write-Warning "WARNING: ninja.exe not found where expected"
}
}
}
else {
if (! (vcpkg list "vcpkg-tool-ninja")) {
($ninja_there = (vcpkg list "vcpkg-tool-ninja")) *> $null
if (! $ninja_there) {
vcpkg install vcpkg-tool-ninja

if (($env:VCPKG_DIR) -And `
Expand Down Expand Up @@ -178,4 +207,6 @@ if ((! ($env:plain_prompt)) -or ($env:plain_prompt -ne "Y"))

cd "$savedpwd"

pstPressKeyIfRaisedAndErrThenExit

Write-Host "SUCCESS: gcc.exe, mc.exe and ninja.exe set up"
Loading
Loading