Last active
May 4, 2019 09:17
-
-
Save sebgod/650818206dfa29b019b466dc40e144df to your computer and use it in GitHub Desktop.
Invoke Mercury compiler using PowerShell. Discovers MSVC compiler as well.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-MercuryCompiler { | |
param ( | |
[String] $mercuryHome | |
) | |
$env_Orig = Get-Environment | |
try { | |
if (-not (Test-PathAllowNull $mercuryHome)) { | |
$mercuryHome = $env:MERCURY_HOME | |
} | |
if (-not (Test-PathAllowNull $mercuryHome)) { | |
$mercuryOnPath = Get-Command "mercury_compile.exe" -ErrorAction SilentlyContinue | |
if (Test-PathAllowNull $mercuryOnPath) { | |
$mercuryHome = Split-Path (Split-Path $mercuryOnPath) | |
} | |
} | |
if (-not (Test-PathAllowNull $mercuryHome)) { | |
throw [System.ArgumentException]::New("Mercury home '${mercuryHome}' ` | |
does not point to a valid Mercury installation", 'mercuryHome') | |
} | |
Invoke-CmdScript $(join-path (Get-LatestVSInstallationPath) "Common7\Tools\VsDevCmd.bat") | |
$env:Path = "$mercuryHome/bin;$env:Path" | |
$env:MERCURY_STDLIB_DIR = "$mercuryHome/lib/mercury" | |
& "$mercuryHome/bin/mercury_compile.exe" --install-command copy $args -ErrorAction SilentlyContinue | |
Write-Error $LASTEXITCODE | |
} | |
finally { | |
Restore-Environment $env_Orig | |
} | |
} | |
function Test-PathAllowNull { | |
param ( | |
[string] $path | |
) | |
($null -ne $path) -and ($path -ne '') -and (Test-Path $path) | |
} | |
function Get-ProgramFilesX86 { | |
switch ($null -ne ${env:ProgramFiles(x86)}) { | |
$true { ${env:ProgramFiles(x86)} } | |
$false { ${env:ProgramFiles} } | |
} | |
} | |
function Get-LatestVSInstallationPath { | |
&"$(Get-ProgramFilesX86)\Microsoft Visual Studio\Installer\vswhere.exe" ` | |
-format value -utf8 -products * ` | |
-requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ` | |
-property installationPath -latest | |
} | |
# courtesy of https://www.itprotoday.com/powershell/take-charge-environment-variables-powershell | |
# Invokes a Cmd.exe shell script and updates the environment. | |
function Invoke-CmdScript { | |
param( | |
[String] $scriptName | |
) | |
$cmdLine = """$scriptName"" $args & set" | |
& "$env:ComSpec" /s /c $cmdLine | | |
select-string '^([^=]*)=(.*)$' | | |
foreach-object { | |
$varName = $_.Matches[0].Groups[1].Value | |
$varValue = $_.Matches[0].Groups[2].Value | |
Set-Item Env:$varName $varValue | |
} | |
} | |
# Returns the current environment. | |
function Get-Environment { | |
get-childitem Env: | |
} | |
# Restores the environment to a previous state. | |
function Restore-Environment { | |
param( | |
[parameter(Mandatory=$TRUE)] | |
[System.Collections.DictionaryEntry[]] $oldEnv | |
) | |
# Removes any added variables. | |
compare-object $oldEnv $(Get-Environment) -property Key -passthru | | |
where-object { $_.SideIndicator -eq "=>" } | | |
ForEach-Object { remove-item Env:$($_.Name) } | |
# Reverts any changed variables to original values. | |
compare-object $oldEnv $(Get-Environment) -property Value -passthru | | |
where-object { $_.SideIndicator -eq "<=" } | | |
ForEach-Object { set-item Env:$($_.Name) $_.Value } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment