1# Requires Run as Administrator is NOT strictly necessary for User-scope env vars,
  2# but recommended for creating directories in C:\ root if permissions are restricted.
  3
  4$ErrorActionPreference = "Stop"
  5
  6# --- Configuration ---
  7$BaseDir = "C:\Qualcomm"
  8
  9# SDK 1: Hexagon
 10$HexagonUrl     = "https://github.com/snapdragon-toolchain/hexagon-sdk/releases/download/v6.4.0.2/hexagon-sdk-v6.4.0.2-arm64-wos.tar.xz"
 11$HexagonParent  = Join-Path $BaseDir "Hexagon_SDK"
 12$HexagonSdkVersion   = "6.4.0.2"
 13$HexagonToolsVersion = "19.0.04"
 14$HexagonSdkTarget    = Join-Path $HexagonParent $HexagonSdkVersion
 15$HexagonToolsTarget  = Join-Path $HexagonSdkTarget "\tools\HEXAGON_Tools\$HexagonToolsVersion"
 16
 17# SDK 2: OpenCL
 18$OpenCLUrl      = "https://github.com/snapdragon-toolchain/opencl-sdk/releases/download/v2.3.2/adreno-opencl-sdk-v2.3.2-arm64-wos.tar.xz"
 19$OpenCLParent   = Join-Path $BaseDir "OpenCL_SDK"
 20$OpenCLVersion  = "2.3.2"
 21$OpenCLTarget   = Join-Path $OpenCLParent $OpenCLVersion
 22
 23# --- Helper Function ---
 24function Install-QualcommSDK {
 25    param (
 26        [string]$Url,
 27        [string]$ParentDir,
 28        [string]$TargetDir,
 29        [string]$Name
 30    )
 31
 32    # 1. Create Parent Directory
 33    if (-not (Test-Path -Path $ParentDir)) {
 34        Write-Host "Creating directory: $ParentDir" -ForegroundColor Cyan
 35        New-Item -Path $ParentDir -ItemType Directory -Force | Out-Null
 36    }
 37
 38    # 2. Check for Specific Version Directory
 39    if (Test-Path -Path $TargetDir) {
 40        Write-Host "$Name ($TargetDir) already exists. Skipping download." -ForegroundColor Green
 41    }
 42    else {
 43        Write-Host "$Name not found. preparing to download..." -ForegroundColor Yellow
 44
 45        # Create the target directory to extract into
 46        New-Item -Path $TargetDir -ItemType Directory -Force | Out-Null
 47
 48        # Define temporary archive path
 49        $TempFile = Join-Path $ParentDir "temp_sdk.tar.xz"
 50
 51        try {
 52            # Download
 53            Write-Host "Downloading from: $Url"
 54            Invoke-WebRequest -Uri $Url -OutFile $TempFile
 55
 56            # Untar
 57            # Note: We assume Windows includes tar.exe (Win 10 build 17063+)
 58            Write-Host "Extracting archive to $TargetDir..."
 59
 60            # We use -C to extract contents INTO the target directory created above
 61            tar -xJvf $TempFile -C $TargetDir\..
 62
 63            Write-Host "Extraction complete." -ForegroundColor Green
 64        }
 65        catch {
 66            Write-Error "Failed to download or extract $Name. Error: $_"
 67            # Cleanup target dir if failed so script tries again next time
 68            Remove-Item -Path $TargetDir -Recurse -Force -ErrorAction SilentlyContinue
 69        }
 70        finally {
 71            # Cleanup Archive
 72            if (Test-Path $TempFile) { Remove-Item $TempFile -Force }
 73        }
 74    }
 75}
 76
 77# --- Execution ---
 78
 79# 1. Ensure Base C:\Qualcomm exists
 80if (-not (Test-Path $BaseDir)) {
 81    New-Item -Path $BaseDir -ItemType Directory -Force | Out-Null
 82}
 83
 84# 2. Run Install Logic
 85Install-QualcommSDK -Url $HexagonUrl -ParentDir $HexagonParent -TargetDir $HexagonSdkTarget -Name "Hexagon SDK"
 86Install-QualcommSDK -Url $OpenCLUrl -ParentDir $OpenCLParent -TargetDir $OpenCLTarget -Name "OpenCL SDK"
 87
 88# --- Environment Variables ---
 89
 90Write-Host "`nSetting Environment Variables..." -ForegroundColor Cyan
 91
 92# Set OPENCL_SDK_ROOT
 93[System.Environment]::SetEnvironmentVariable('OPENCL_SDK_ROOT', $OpenCLTarget, [System.EnvironmentVariableTarget]::User)
 94$env:OPENCL_SDK_ROOT = $OpenCLTarget # Set for current session as well
 95Write-Host "OPENCL_SDK_ROOT set to:  $OpenCLTarget"
 96
 97# Set HEXAGON_SDK_ROOT
 98[System.Environment]::SetEnvironmentVariable('HEXAGON_SDK_ROOT', $HexagonSdkTarget, [System.EnvironmentVariableTarget]::User)
 99$env:HEXAGON_SDK_ROOT = $HexagonSdkTarget # Set for current session as well
100Write-Host "HEXAGON_SDK_ROOT set to: $HexagonSdkTarget"
101
102# Set HEXAGON_SDK_ROOT
103[System.Environment]::SetEnvironmentVariable('HEXAGON_TOOLS_ROOT', $HexagonToolsTarget, [System.EnvironmentVariableTarget]::User)
104$env:HEXAGON_TOOLS_ROOT = $HexagonToolsTarget # Set for current session as well
105Write-Host "HEXAGON_TOOLS_ROOT set to: $HexagonToolsTarget"