Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASIO] Pre build script #801

Closed
alisomay opened this issue Oct 12, 2023 · 1 comment
Closed

[ASIO] Pre build script #801

alisomay opened this issue Oct 12, 2023 · 1 comment

Comments

@alisomay
Copy link
Contributor

Hey 👋

I followed the readme for the asio setup which involves multiple steps.
It worked fine for me.
Then I've also written a PowerShell script to automatize it a bit.
I'd like to share it here maybe it will be useful to someone.
This is not a suggestion for adoption but it makes things easier in development.

Also it is a bit annoying that we can do pre-build scripts in rust build system, it would have been useful for such cases.

All the best.

Write-Output "============================================================================="
Write-Output "Pre build script for cpal asio feature."
Write-Output "Make sure that you have sourced this script instead of directly executing it."
Write-Output "============================================================================="

function Invoke-VcVars {
    <#
    .SYNOPSIS
    This function sets the Visual Studio build environment for the current session.

    .DESCRIPTION
    The function first determines the system architecture. It then searches for the vcvarsall.bat file 
    specific to the detected architecture and executes it to set the Visual Studio build environment 
    for the current session.
    #>
    
    # Determine the system architecture
    Write-Output "Determining system architecture..."
    $arch = if ([Environment]::Is64BitOperatingSystem) {
        switch -Wildcard ((Get-CimInstance -ClassName Win32_Processor).Description) {
            "*ARM64*"{ "arm64" }
            "*ARM*"{ "arm" }
            default { "amd64" }
        }
    } else { 
        "x86" 
    }

    Write-Output "Architecture detected as $arch."

    # Define search paths based on architecture
    $paths = if ($arch -eq 'amd64') {
        @('C:\Program Files (x86)\Microsoft Visual Studio\', 'C:\Program Files\Microsoft Visual Studio\')
    } else {
        @('C:\Program Files\Microsoft Visual Studio\')
    }

    # Search for vcvarsall.bat and execute the first instance found with the appropriate architecture argument
    Write-Output "Searching for vcvarsall.bat..."
    foreach ($path in $paths) {
        $vcvarsPath = Get-ChildItem $path -Recurse -Filter vcvarsall.bat -ErrorAction SilentlyContinue | Select-Object -First 1
        if ($vcvarsPath) {
            Write-Output "Found vcvarsall.bat at $($vcvarsPath.FullName). Initializing environment..."
            $cmdOutput = cmd /c """$($vcvarsPath.FullName)"" $arch && set"
            foreach ($line in $cmdOutput) {
                if ($line -match "^(.*?)=(.*)$") {
                    $varName = $matches[1]
                    $varValue = $matches[2]
                    [System.Environment]::SetEnvironmentVariable($varName, $varValue, "Process")
                }
            }
            return
        }
    }

    Write-Error "Error: Could not find vcvarsall.bat. Please install the latest version of Visual Studio."
    exit 1
}

# Main script begins here

# Ensure execution policy allows for script execution
if (-not (Get-ExecutionPolicy -Scope CurrentUser) -eq "Unrestricted") {
    Set-ExecutionPolicy -Scope CurrentUser Unrestricted
}

# Check if running on Windows
if ($env:OS -match "Windows") {
    Write-Output "Detected Windows OS."

    # Directory to store the ASIO SDK
    $out_dir = [System.IO.Path]::GetTempPath()
    $asio_dir = Join-Path $out_dir "asio_sdk"

    if (-not (Test-Path $asio_dir)) {
        Write-Output "ASIO SDK not found. Downloading..."
        
        # Download the ASIO SDK
        $asio_zip_path = Join-Path $out_dir "asio_sdk.zip"
        Invoke-WebRequest -Uri "https://www.steinberg.net/asiosdk" -OutFile $asio_zip_path

        # Unzip the ASIO SDK
        Write-Output "Unzipping ASIO SDK..."
        Expand-Archive -Path $asio_zip_path -DestinationPath $out_dir -Force

        # Move the contents of the inner directory (like asiosdk_2.3.3_2019-06-14) to $asio_dir
        $innerDir = Get-ChildItem -Path $out_dir -Directory | Where-Object { $_.Name -match 'asio.*' } | Select-Object -First 1
        Move-Item -Path "$($innerDir.FullName)\*" -Destination $asio_dir -Force
    } else {
        Write-Output "ASIO SDK already exists. Skipping download."
    }

    # Set the CPAL_ASIO_DIR environment variable
    Write-Output "Setting CPAL_ASIO_DIR environment variable..."
    $env:CPAL_ASIO_DIR = $asio_dir

    # Check if LIBCLANG_PATH is set
    if (-not $env:LIBCLANG_PATH) {
        Write-Error "Error: LIBCLANG_PATH is not set!"
        Write-Output "Please ensure LLVM is installed and set the LIBCLANG_PATH environment variable."
        exit 1
    } else {
        Write-Output "LIBCLANG_PATH is set to $env:LIBCLANG_PATH."
    }

    # Run the vcvars function
    Invoke-VcVars
} else {
    Write-Output "This setup script is intended for Windows only."
}
@alisomay
Copy link
Contributor Author

#804

Implements this script in asio-sys build.rs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant