hylo-lang / llvm-build

LLVM library binaries that are compatible with the Swift runtime
2 stars 0 forks source link

Make Windows builds agnostic to the client toolchain #15

Open dabrahams opened 3 months ago

dabrahams commented 3 months ago

@tothambrus11 discovered that the path to VS Enterprise is being encoded into the LLVMExports.cmake of our prebuilt LLVM distribution. He's only got Community, so nothing works for him.

As part of the build, we should postprocess the file to use VSINSTALLDIR from the environment: $ENV{VSINSTALLDIR}

tothambrus11 commented 3 months ago

I made a Powershell script that can replace the path in LLVMExports.cmake if placed inside the LLVM folder inside e.g. fix-vs.ps1, then invoking it in Powershell by ./fix-vs.ps1. Where could we add this to the built archives?

# The path to the LLVMExports.cmake file
$llvmExportsPath = "./lib/cmake/llvm/LLVMExports.cmake"
# The old path to be replaced
$oldPath = "C:/Program Files/Microsoft Visual Studio/2022/Enterprise/"

# Check if the LLVMExports.cmake file exists
if (-Not (Test-Path $llvmExportsPath)) {
    Write-Error "The file $llvmExportsPath does not exist."
    exit 1
}

# Get the Visual Studio installation path from the VSINSTALLDIR environment variable
$vsInstallDir = $env:VSINSTALLDIR

$helpfulMessage = "Please set it to your VS installation directory, e.g. C:/Program Files/Microsoft Visual Studio/2022/Enterprise/"

# Check if the VSINSTALLDIR environment variable is set
if (-Not $vsInstallDir) {
    Write-Error "The VSINSTALLDIR environment variable is not set. $helpfulMessage"
    exit 1
}
# Replace \-es with /-es to make the path consistent (/-es are also valid path separators on Windows)
$vsInstallDir = $vsInstallDir -replace "\\", "/"
# Ensure the path ends with a forward slash
if (-Not $vsInstallDir.EndsWith("/")) {
    $vsInstallDir += "/"
}

# Check if the vsInstallDir directory exists
if (-Not (Test-Path $vsInstallDir)) {
    Write-Error "The directory $vsInstallDir does not exist. $helpfulMessage"
    exit 1
}

# Print the VSINSTALLDIR:
Write-Output "Found Visual Studio Installation at: $vsInstallDir"

# Read the contents of the LLVMExports.cmake file
$fileContent = Get-Content -Path $llvmExportsPath -Raw

# Replace the old path with the new path from the VSINSTALLDIR environment variable
$newContent = $fileContent -replace [regex]::Escape($oldPath), $vsInstallDir

if ($fileContent -eq $newContent) {
    Write-Output "No replacements were made in $llvmExportsPath."
} else {
    # Write the updated content back to the LLVMExports.cmake file
    Set-Content -Path $llvmExportsPath -Value $newContent
    Write-Output "The path has been successfully updated in $llvmExportsPath."
}
dabrahams commented 3 months ago

This shouldn't shipped as part of the tarballs we create, but instead it should be invoked in this project's CI as a step after creating the LLVM installation.

tothambrus11 commented 3 months ago

Oh, now I see. This script just hardcodes the path as it replaces things but it could just as well replace it with the reference to the environment variable.