]> vilimpoc.org git repositories - dotfiles/blobdiff - setup-windows-no-uac.ps1
dotfiles: detect that elevation is impossible instead of misreporting it
[dotfiles] / setup-windows-no-uac.ps1
index 3b67f0f0024132f5bf5c3ec70f7e82a767e11fdd..1fd2c1f3e664e9ee84ef16ff7bb1a7309072c3ce 100644 (file)
@@ -40,7 +40,7 @@ param(
     # for several, dot-call the script or use -Command:
     #     .\setup-windows-no-uac.ps1 -Skip BinSkim,GitConfig
     #     powershell -File .\setup-windows-no-uac.ps1 -Skip BinSkim
-    [ValidateSet('WinMerge', 'VsWhere', 'BinSkim', 'GitConfig', 'NinjaPath', 'ArchAudit')]
+    [ValidateSet('WinMerge', 'VsWhere', 'BinSkim', 'GitConfig', 'NinjaPath', 'LlvmPath', 'ArchAudit')]
     [string[]] $Skip = @()
 )
 
@@ -421,6 +421,51 @@ function Set-NativeNinjaFirst {
     Add-ToUserPath (Split-Path $native -Parent) -Prepend
 }
 
+function Add-LlvmToUserPath {
+    # Put the upstream LLVM's bin directory on the user PATH.
+    #
+    # Two reasons this needs a step rather than trusting the installer:
+    #
+    # 1. WHERE IT LANDS. LLVM's NSIS installer targets %ProgramFiles%\LLVM, and
+    #    when it cannot write there - a standard user, no elevation - it does not
+    #    fail. It silently falls back to a per-user directory, observed as
+    #    %USERPROFILE%\Documents\LLVM, and winget still reports "Successfully
+    #    installed". So the package is registered, the compiler is genuinely
+    #    there and native, and nothing can find it.
+    # 2. PATH. The installer's "add to PATH" option is not taken in a silent
+    #    install, so clang-cl is not a command afterwards either way.
+    #
+    # Appended, not prepended: this is a second compiler kept deliberately
+    # alongside MSVC, and it should not quietly win a `clang-cl` that some script
+    # meant for Visual Studio's copy. Note VS does NOT put its own
+    # VC\Tools\Llvm on the PATH (its clang-cl is reached through CMake's
+    # -T ClangCL), so there is no collision to lose here.
+    Write-Step 'Upstream LLVM on the user PATH'
+    $candidates = @(
+        (Join-Path $env:ProgramFiles 'LLVM\bin')
+        (Join-Path ${env:ProgramFiles(x86)} 'LLVM\bin')
+        (Join-Path $env:LOCALAPPDATA 'Programs\LLVM\bin')
+        (Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'LLVM\bin')
+        (Join-Path $env:USERPROFILE 'Documents\LLVM\bin')
+    )
+    $dir = $candidates | Where-Object { Test-Path (Join-Path $_ 'clang-cl.exe') } | Select-Object -First 1
+    if (-not $dir) {
+        Write-Host '    Not installed (winget install LLVM.LLVM); Visual Studio''s own clang-cl is unaffected.' -ForegroundColor DarkGray
+        return
+    }
+    $exe  = Join-Path $dir 'clang-cl.exe'
+    $mach = Get-PEMachine $exe
+    Write-Host "    $exe ($mach)"
+    if ($IsArm64 -and $mach -ne 'ARM64') {
+        Write-Warning "This LLVM is $mach, not ARM64. winget install LLVM.LLVM should resolve to the -woa64 build on this host."
+    }
+    if ($dir -notmatch [regex]::Escape($env:ProgramFiles)) {
+        Write-Host '    Note: not under Program Files - the installer fell back to a per-user' -ForegroundColor Yellow
+        Write-Host '    location because it could not write there. Re-run elevated for a machine-wide install.' -ForegroundColor Yellow
+    }
+    Add-ToUserPath $dir
+}
+
 function Get-PEMachine {
     # Architecture of a PE, read straight from the COFF header: the 2 bytes at
     # the e_lfanew offset + 4. Cheap, and it answers the only question that
@@ -513,8 +558,9 @@ function Invoke-ArchAudit {
     # puts the native copy first; this is the check that it worked.
     $Remedies = @{
         'ninja.exe'    = 'Visual Studio bundles an x64 ninja. Install the native one (winget install Ninja-build.Ninja) and re-run - the NinjaPath step puts its directory at the front of the user PATH.'
-        'cmake.exe'    = 'Install the native build with: winget install Kitware.CMake'
-        'clang-cl.exe' = 'Install the upstream native LLVM with: winget install LLVM.LLVM'
+        'cmake.exe'     = 'Install the native build with: winget install Kitware.CMake (its MSI is machine-scope, so it needs an administrator).'
+        'clang-cl.exe'  = 'Install the upstream native LLVM with: winget install LLVM.LLVM'
+        'WinMergeU.exe' = 'Upstream ships ARM64 as a MACHINE-scope installer and x64 as per-user, so an unelevated winget picks x64. Re-run as an administrator to get: winget install WinMerge.WinMerge --architecture arm64'
     }
 
     $vs = $null
@@ -531,7 +577,13 @@ function Invoke-ArchAudit {
         'pwsh.exe'            = @()
         'cmake.exe'           = @((Join-Path $env:ProgramFiles 'CMake\bin\cmake.exe'))
         'ninja.exe'           = @()
-        'clang-cl.exe'        = @((Join-Path $env:ProgramFiles 'LLVM\bin\clang-cl.exe'))
+        # Upstream LLVM. The Documents path is not a typo - see Add-LlvmToUserPath
+        # for why an unelevated install lands there.
+        'clang-cl.exe'        = @(
+            (Join-Path $env:ProgramFiles 'LLVM\bin\clang-cl.exe')
+            (Join-Path $env:LOCALAPPDATA 'Programs\LLVM\bin\clang-cl.exe')
+            (Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'LLVM\bin\clang-cl.exe')
+        )
         'dotnet.exe'          = @((Join-Path $env:ProgramFiles 'dotnet\dotnet.exe'))
         'WinMergeU.exe'       = @((Join-Path $env:ProgramFiles 'WinMerge\WinMergeU.exe'), (Join-Path ${env:ProgramFiles(x86)} 'WinMerge\WinMergeU.exe'))
         'BinSkim.exe'         = @((Join-Path $BinSkimDir 'BinSkim.exe'))
@@ -559,11 +611,21 @@ function Invoke-ArchAudit {
     $native = if ($IsArm64) { 'ARM64' } else { 'x64' }
     $unexpected = @()
     foreach ($name in $targets.Keys) {
-        # PATH first - that is the binary a build would actually invoke - then the
-        # explicit candidates. The bare exe name is stripped of any " (label)".
-        $exe = ($name -split ' ')[0]
-        $path = Resolve-OnPath $exe
-        if (-not $path) { $path = $targets[$name] | Where-Object { $_ -and (Test-Path $_) } | Select-Object -First 1 }
+        # A LABELLED entry - "clang-cl.exe (VS)" - names one specific copy, so it
+        # is resolved ONLY from its explicit candidates. Falling back to the PATH
+        # there would silently answer with a different installation of the same
+        # tool: with upstream LLVM on the PATH, the "(VS)" row reported the
+        # upstream compiler and so claimed Visual Studio's was present when it was
+        # not. Unlabelled entries still resolve PATH-first, because for those the
+        # question is which binary a build would actually invoke.
+        $exe      = ($name -split ' ')[0]
+        $isPinned = $name -ne $exe
+        if ($isPinned) {
+            $path = $targets[$name] | Where-Object { $_ -and (Test-Path $_) } | Select-Object -First 1
+        } else {
+            $path = Resolve-OnPath $exe
+            if (-not $path) { $path = $targets[$name] | Where-Object { $_ -and (Test-Path $_) } | Select-Object -First 1 }
+        }
         if (-not $path) { Write-Host ("    {0,-22} {1}" -f $name, '- not installed') -ForegroundColor DarkGray; continue }
 
         $mach = Get-PEMachine $path
@@ -605,6 +667,7 @@ $steps = [ordered]@{
     BinSkim   = { Install-BinSkim }
     GitConfig = { Set-GlobalGitConfig }
     NinjaPath = { Set-NativeNinjaFirst }
+    LlvmPath  = { Add-LlvmToUserPath }
     # Last on purpose: it reports on what the steps above (and the winget installs
     # in setup-windows.bat) actually put on the box.
     ArchAudit = { Invoke-ArchAudit }