1bf215546Sopenharmony_ci# Ensure that dxil.dll in on the %PATH%
2bf215546Sopenharmony_ci$dxil_dll = cmd.exe /C "C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=amd64 -no_logo && where dxil.dll" 2>&1
3bf215546Sopenharmony_ciif ($dxil_dll -notmatch "dxil.dll$") {
4bf215546Sopenharmony_ci    Write-Output "Couldn't get path to dxil.dll"
5bf215546Sopenharmony_ci    exit 1
6bf215546Sopenharmony_ci}
7bf215546Sopenharmony_ci$env:Path = "$(Split-Path $dxil_dll);$env:Path"
8bf215546Sopenharmony_ci
9bf215546Sopenharmony_ci$exec_mode_to_stage = @{ Fragment = "fragment"; Vertex = "vertex"; GLCompute = "compute" }
10bf215546Sopenharmony_ci
11bf215546Sopenharmony_ci$spvasm_files = (Get-ChildItem C:\spirv-samples\spvasm\*.spvasm) | Sort-Object Name
12bf215546Sopenharmony_ciforeach ($spvasm in $spvasm_files) {
13bf215546Sopenharmony_ci    $test_name = "Test:$($spvasm.Name):"
14bf215546Sopenharmony_ci    $spvfile = ($spvasm -replace '\.spvasm$', '.spv')
15bf215546Sopenharmony_ci    $content = Get-Content $spvasm
16bf215546Sopenharmony_ci    $spv_version = "1.0"
17bf215546Sopenharmony_ci    if ($content | Where-Object { $_ -match 'Version:\s(\d+\.\d+)' }) {
18bf215546Sopenharmony_ci        $spv_version = $Matches[1]
19bf215546Sopenharmony_ci    }
20bf215546Sopenharmony_ci    
21bf215546Sopenharmony_ci    $as_output = . "$env:VULKAN_SDK\Bin\spirv-as.exe" --target-env spv$spv_version --preserve-numeric-ids -o $spvfile $spvasm 2>&1 | % { if ($_ -is [System.Management.Automation.ErrorRecord]) { $_.Exception.Message } else { $_ } }  | Out-String
22bf215546Sopenharmony_ci    if ($LASTEXITCODE -ne 0) {
23bf215546Sopenharmony_ci        Write-Output "$test_name Skip: Unable to assemble shader"
24bf215546Sopenharmony_ci        Write-Output "$as_output`n"
25bf215546Sopenharmony_ci        continue
26bf215546Sopenharmony_ci    }
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci    $entry_points = $content | Select-String -Pattern '^OpEntryPoint\s(\w+)[^"]+"(\w+)"' | Select-Object -ExpandProperty Matches -First 1
29bf215546Sopenharmony_ci    if ($entry_points.Count -eq 0) {
30bf215546Sopenharmony_ci        Write-Output "$test_name Skip"
31bf215546Sopenharmony_ci        Write-Output "No OpEntryPoint not found`n"
32bf215546Sopenharmony_ci        continue
33bf215546Sopenharmony_ci    }
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci    foreach ($match in $entry_points) {
36bf215546Sopenharmony_ci        $exec_mode, $entry_point = $match.Groups[1].Value, $match.Groups[2].Value
37bf215546Sopenharmony_ci        $subtest = "$test_name$entry_point|${exec_mode}:"
38bf215546Sopenharmony_ci        $stage = $exec_mode_to_stage[$exec_mode]
39bf215546Sopenharmony_ci        if ($stage -eq '') {
40bf215546Sopenharmony_ci            Write-Output "$subtest Fail: Unknown shader type ($exec_mode)"
41bf215546Sopenharmony_ci            continue
42bf215546Sopenharmony_ci        }
43bf215546Sopenharmony_ci        
44bf215546Sopenharmony_ci        $s2d_output = .\_install\bin\spirv2dxil.exe -v -e "$entry_point" -s "$stage" -o NUL $spvfile 2>&1 | ForEach-Object { if ($_ -is [System.Management.Automation.ErrorRecord]) { $_.Exception.Message } else { $_ } }  | Out-String
45bf215546Sopenharmony_ci        if ($LASTEXITCODE -eq 0) {
46bf215546Sopenharmony_ci            Write-Output "$subtest Pass"
47bf215546Sopenharmony_ci        }
48bf215546Sopenharmony_ci        else {
49bf215546Sopenharmony_ci            Write-Output "$subtest Fail"
50bf215546Sopenharmony_ci            $sanitized_output = $s2d_output -replace ', file .+, line \d+' -replace '    In file .+:\d+'
51bf215546Sopenharmony_ci            Write-Output "$sanitized_output`n"
52bf215546Sopenharmony_ci        }
53bf215546Sopenharmony_ci    }
54bf215546Sopenharmony_ci}
55