Skip to content

Instantly share code, notes, and snippets.

@han-minhee
Created July 17, 2025 15:09
Show Gist options
  • Save han-minhee/fb8724cdd7216eef46f3c9d839ad1dc7 to your computer and use it in GitHub Desktop.
Save han-minhee/fb8724cdd7216eef46f3c9d839ad1dc7 to your computer and use it in GitHub Desktop.
This script will setup oneAPI related environment variables on Windows PowerShell.
#
# Intel oneAPI Environment Setup for Windows (PowerShell)
#
# By default, Intel oneAPI requires running .bat scripts, which isn't that ideal as
# 1. most Windows environments now use Powershell as their default shells and
# 2. having to run the script everytime is really annoying.
# This script sets up the Intel oneAPI environment variables for the current PowerShell session
# It's designed to replace the functionality of setvars.bat from Intel oneAPI
# You can run this script without any argument.
#
# Usage:
# .\setup_oneapi_windows.ps1 [-arch <arch>] [-component <component>] [-config <config>] [-CreateEnvFile] [-Verbose]
#
# Parameters:
# -arch : Architecture (ia32, intel64)
# -component : Component to set up (compiler, mkl, tbb, etc.)
# -config : Configuration (debug, release)
# -CreateEnvFile : Create a PowerShell environment file for future use
# -Verbose : Show detailed output
#
param (
[string]$arch = "intel64",
[string]$component = "all",
[string]$config = "release",
[switch]$CreateEnvFile = $false,
[switch]$Verbose = $false
)
# Function to find oneAPI installation
function Find-OneAPIPath {
$commonPaths = @(
"C:\Program Files (x86)\Intel\oneAPI",
"${env:ProgramFiles(x86)}\Intel\oneAPI",
"${env:ProgramFiles}\Intel\oneAPI"
)
# Check common paths
foreach ($path in $commonPaths) {
if (Test-Path $path) {
# if found, print the variable
Write-Host "Using the OneAPI path: $path"
return $path
}
}
# Check environment variable
if ($env:ONEAPI_ROOT) {
if (Test-Path $env:ONEAPI_ROOT) {
return $env:ONEAPI_ROOT
}
}
# Not found
return $null
}
# Function to check if setvars.bat exists
function Test-SetVarsExists {
param (
[string]$OneAPIPath
)
$setvarsPath = Join-Path $OneAPIPath "setvars.bat"
return Test-Path $setvarsPath
}
# Function to run setvars.bat and capture environment changes
function Invoke-SetVars {
param (
[string]$OneAPIPath,
[string]$Arch,
[string]$Component,
[string]$Config
)
$setvarsPath = Join-Path $OneAPIPath "setvars.bat"
if ($Verbose) {
Write-Host "Running $setvarsPath to set up environment..."
Write-Host "Architecture: $Arch"
Write-Host "Component: $Component"
Write-Host "Configuration: $Config"
}
# Build arguments for setvars.bat
$setVarsArgs = ""
if ($Arch -ne "all") { $setVarsArgs += " --arch=$Arch" }
if ($Component -ne "all") { $setVarsArgs += " --component=$Component" }
if ($Config -ne "release") { $setVarsArgs += " --config=$Config" }
# Create a temporary batch file to capture environment
$tempBatchFile = [System.IO.Path]::GetTempFileName() + ".bat"
$tempEnvFile = [System.IO.Path]::GetTempFileName()
# Create batch file content
@"
@echo off
call "$setvarsPath"$setVarsArgs > nul 2>&1
set > "$tempEnvFile"
"@ | Out-File -FilePath $tempBatchFile -Encoding ASCII
# Run the batch file
cmd.exe /c $tempBatchFile
# Read the environment variables
$envVars = Get-Content $tempEnvFile
# Clean up temp files
Remove-Item $tempBatchFile -Force
Remove-Item $tempEnvFile -Force
# Parse and set environment variables
foreach ($line in $envVars) {
if ($line -match "^([^=]+)=(.*)$") {
$name = $matches[1]
$value = $matches[2]
# Skip some variables that shouldn't be copied
if ($name -in @("PROMPT", "COMSPEC", "CMDEXTVERSION", "CMDCMDLINE")) {
continue
}
# Set environment variable for current process
[System.Environment]::SetEnvironmentVariable($name, $value, [System.EnvironmentVariableTarget]::Process)
if ($Verbose) {
Write-Host "Set $name=$value"
}
}
}
}
# Function to create environment file for future use
function New-EnvFile {
param (
[string]$OneAPIPath,
[string]$Arch,
[string]$Component,
[string]$Config
)
$envFile = Join-Path $HOME ".oneapi_env.ps1"
if ($Verbose) {
Write-Host "Creating environment file at $envFile..."
}
# Build arguments for setvars.bat
$setVarsArgs = ""
if ($Arch -ne "all") { $setVarsArgs += " --arch=$Arch" }
if ($Component -ne "all") { $setVarsArgs += " --component=$Component" }
if ($Config -ne "release") { $setVarsArgs += " --config=$Config" }
# Create a temporary batch file to capture environment
$tempBatchFile = [System.IO.Path]::GetTempFileName() + ".bat"
$tempEnvFile = [System.IO.Path]::GetTempFileName()
$setvarsPath = Join-Path $OneAPIPath "setvars.bat"
# Create batch file content
@"
@echo off
call "$setvarsPath"$setVarsArgs > nul 2>&1
set > "$tempEnvFile"
"@ | Out-File -FilePath $tempBatchFile -Encoding ASCII
# Run the batch file
cmd.exe /c $tempBatchFile
# Read the environment variables
$envVars = Get-Content $tempEnvFile
# Create PowerShell environment file
$psContent = @"
# Intel oneAPI Environment Variables
# Generated by setup_oneapi_windows.ps1
# Source this file to set up Intel oneAPI environment
# Usage: . ~/.oneapi_env.ps1
#
# Architecture: $Arch
# Component: $Component
# Configuration: $Config
"@
# Add environment variables
foreach ($line in $envVars) {
if ($line -match "^([^=]+)=(.*)$") {
$name = $matches[1]
$value = $matches[2]
# Skip some variables that shouldn't be copied
if ($name -in @("PROMPT", "COMSPEC", "CMDEXTVERSION", "CMDCMDLINE")) {
continue
}
# Add to PowerShell file
$psContent += "`$env:$name = `"$value`"`n"
}
}
# Write the file
$psContent | Out-File -FilePath $envFile -Encoding UTF8
# Clean up temp files
Remove-Item $tempBatchFile -Force
Remove-Item $tempEnvFile -Force
Write-Host "Environment file created at $envFile"
Write-Host "To use it in the future, run: . ~/.oneapi_env.ps1"
}
# Function to list available components
function Get-OneAPIComponents {
param (
[string]$OneAPIPath
)
$components = @()
# Check common component directories
$componentDirs = @(
"compiler",
"mkl",
"tbb",
"ipp",
"dnnl",
"dal",
"ccl",
"vpl",
"ippcp"
)
foreach ($dir in $componentDirs) {
$path = Join-Path $OneAPIPath $dir
if (Test-Path $path) {
$components += $dir
}
}
return $components
}
# Main script
# Find oneAPI installation
$oneAPIPath = Find-OneAPIPath
if (-not $oneAPIPath) {
Write-Host "Intel oneAPI installation not found." -ForegroundColor Yellow
Write-Host "If you're using a custom path for oneAPI installation, modify the top part of the script"
Write-Host "Please install Intel oneAPI Base Toolkit from:"
Write-Host "https://www.intel.com/content/www/us/en/developer/tools/oneapi/base-toolkit-download.html"
exit 1
}
if ($Verbose) {
Write-Host "Found Intel oneAPI at: $oneAPIPath"
}
# Check if setvars.bat exists
if (-not (Test-SetVarsExists $oneAPIPath)) {
Write-Host "setvars.bat not found in $oneAPIPath" -ForegroundColor Yellow
Write-Host "Intel oneAPI installation may be incomplete."
exit 1
}
# List available components if verbose
if ($Verbose) {
$components = Get-OneAPIComponents -OneAPIPath $oneAPIPath
Write-Host "Available components: $($components -join ', ')"
}
# Run setvars.bat and set environment
Invoke-SetVars -OneAPIPath $oneAPIPath -Arch $arch -Component $component -Config $config
# Create environment file if requested
if ($CreateEnvFile) {
New-EnvFile -OneAPIPath $oneAPIPath -Arch $arch -Component $component -Config $config
}
# Verify setup
$dpcppPath = Get-Command dpcpp.exe -ErrorAction SilentlyContinue
if ($dpcppPath) {
if ($Verbose) {
Write-Host "Intel oneAPI DPC++ compiler found at: $($dpcppPath.Source)"
}
Write-Host "Intel oneAPI environment successfully configured." -ForegroundColor Green
}
else {
Write-Host "Intel DPC++ compiler not found in PATH." -ForegroundColor Yellow
if ($component -eq "all" -or $component -eq "compiler") {
Write-Host "Environment may not be fully configured."
}
else {
Write-Host "This is expected since you didn't select the compiler component."
}
}
# List available SYCL devices
if ($component -eq "all" -or $component -eq "compiler") {
Write-Host "`nAvailable SYCL devices:"
try {
$syclLs = Join-Path $oneAPIPath "compiler\latest\bin\sycl-ls.exe"
if (Test-Path $syclLs) {
& $syclLs
}
else {
Write-Host " sycl-ls tool not found at $syclLs"
}
}
catch {
Write-Host " Error listing SYCL devices: $_"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment