### Install and Download MSVC/SDK using msvc-kit CLI Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/basic.md Installs the msvc-kit tool and downloads the latest MSVC and SDK components. This is the initial setup step for using msvc-kit from the command line. ```bash # Install msvc-kit ``` ```bash cargo install msvc-kit # Download latest MSVC and SDK ``` ```bash msvc-kit download # Setup environment ``` ```bash msvc-kit setup --script --shell powershell | Invoke-Expression # Verify ``` ```bash cl /? ``` -------------------------------- ### Quick Start: Download MSVC, SDK, and Setup Environment Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/library.md A quick example demonstrating how to download MSVC and SDK components using default options, set up the environment, and print relevant paths like cl.exe, INCLUDE, and LIB. ```rust use msvc_kit::{download_msvc, download_sdk, setup_environment, DownloadOptions}; #[tokio::main] async fn main() -> msvc_kit::Result<()> { // Download with default options let options = DownloadOptions::default(); let msvc_info = download_msvc(&options).await?; let sdk_info = download_sdk(&options).await?; // Setup environment let env = setup_environment(&msvc_info, Some(&sdk_info))?; // Access paths println!("cl.exe: {:?}", env.cl_exe_path()); println!("INCLUDE: {}", env.include_path_string()); println!("LIB: {}", env.lib_path_string()); Ok(()) } ``` -------------------------------- ### GitHub Actions CI/CD Setup for MSVC Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/what-is-msvc-kit.md This example demonstrates how to integrate msvc-kit into a GitHub Actions workflow to ensure MSVC build tools are available for Windows builds. It follows the same installation and setup steps as a local environment. ```yaml # GitHub Actions example - name: Install MSVC run: | cargo install msvc-kit msvc-kit download msvc-kit setup --script --shell powershell | Invoke-Expression ``` -------------------------------- ### Usage Examples for Rust Quick Compile Tool Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/quick-compile.md Instructions on how to build and use the Rust-based quick compile tool ('qc'). It covers installing the tool via Cargo and then using it to compile C++ source files with different options. ```bash # Build the tool cargo install --path . # Use it qc main.cpp qc main.cpp --release qc main.cpp --release --output myapp ``` -------------------------------- ### Configure msvc-kit in GitHub Actions CI/CD Source: https://context7.com/loonghao/msvc-kit/llms.txt Provides a GitHub Actions workflow example for setting up and using msvc-kit on Windows runners. This automates the installation, configuration, and environment setup for MSVC toolchains within a CI/CD pipeline. ```yaml # .github/workflows/build.yml name: Build on: [push, pull_request] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: Install msvc-kit run: cargo install msvc-kit - name: Configure msvc-kit run: | msvc-kit config --set-dir ${{ github.workspace }}/msvc-kit msvc-kit download - name: Setup MSVC environment run: msvc-kit setup --script --shell powershell | Invoke-Expression - name: Build project run: cargo build --release ``` -------------------------------- ### CI/CD Example (GitHub Actions) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/dcc/unreal-engine.md Provides an example of a GitHub Actions workflow for building an Unreal Engine 5 plugin. It includes steps for installing msvc-kit, downloading MSVC, setting up the environment, and building the plugin. ```yaml name: Build UE5 Plugin on: [push] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: Install msvc-kit run: cargo install msvc-kit - name: Download MSVC for UE5.4 run: msvc-kit download --msvc-version 14.38 --sdk-version 10.0.22621.0 - name: Setup Environment run: msvc-kit setup --script --shell powershell | Invoke-Expression - name: Build Plugin run: | & "$env:UE_ROOT\Engine\Build\BatchFiles\RunUAT.bat" ` BuildPlugin ` -Plugin="${{ github.workspace }}\MyPlugin.uplugin" ` -Package="${{ github.workspace }}\Output" ``` -------------------------------- ### Example: Retrieving All Tool Paths Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/msvc-environment.md Shows how to use the `tool_paths()` method to get a struct containing the paths to all available MSVC tools. The example then prints the paths for the compiler, linker, lib, assembler, make, and resource compiler. ```rust let tools = env.tool_paths(); println!("Compiler: {:?}", tools.cl); println!("Linker: {:?}", tools.link); println!("Lib: {:?}", tools.lib); println!("Assembler: {:?}", tools.ml64); println!("Make: {:?}", tools.nmake); println!("Resource Compiler: {:?}", tools.rc); ``` -------------------------------- ### Setup Environment using msvc-kit Rust Library Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/basic.md Shows how to set up the MSVC build environment using the msvc-kit Rust library after downloading MSVC and the SDK. It checks for the 'cl.exe' compiler and retrieves environment paths. ```rust use msvc_kit::{download_msvc, download_sdk, setup_environment, DownloadOptions}; #[tokio::main] async fn main() -> msvc_kit::Result<()> { let options = DownloadOptions::default(); let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; let env = setup_environment(&msvc, Some(&sdk))?; // Check tools if env.has_cl_exe() { println!("cl.exe found at: {:?}", env.cl_exe_path()); } // Get environment strings println!("INCLUDE: {}", env.include_path_string()); println!("LIB: {}", env.lib_path_string()); Ok(()) } ``` -------------------------------- ### Check Installed MSVC and SDK Versions using msvc-kit Rust Library Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/basic.md Demonstrates how to list the installed MSVC and SDK versions on the system using the msvc-kit Rust library. It iterates through the found versions and prints their details. ```rust use msvc_kit::{MsvcVersion, SdkVersion}; fn main() { // List installed MSVC versions let msvc_versions = MsvcVersion::list_installed(); println!("Installed MSVC versions:"); for v in msvc_versions { println!(" {} at {:?}", v.version, v.path); } // List installed SDK versions let sdk_versions = SdkVersion::list_installed(); println!("Installed SDK versions:"); for v in sdk_versions { println!(" {} at {:?}", v.version, v.path); } } ``` -------------------------------- ### Rust Example: Download MSVC and SDK Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Demonstrates how to use the msvc-kit Rust library to download MSVC and Windows SDK components. It utilizes a builder pattern for configuration and asynchronous operations. ```rust use msvc_kit::{download_msvc, download_sdk, setup_environment, DownloadOptions}; use msvc_kit::{list_available_versions, Architecture}; #[tokio::main] async fn main() -> msvc_kit::Result<()> { // List available versions from Microsoft let versions = list_available_versions().await?; println!("Latest MSVC: {:?}", versions.latest_msvc); println!("Latest SDK: {:?}", versions.latest_sdk); // Download with builder pattern let options = DownloadOptions::builder() .target_dir("C:/msvc-kit") .arch(Architecture::X64) .build(); let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; let env = setup_environment(&msvc, Some(&sdk))?; println!("cl.exe: {:?}", env.cl_exe_path()); Ok(()) } ``` -------------------------------- ### Access MSVC and SDK Directory Paths (Rust) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/custom-paths.md Demonstrates how to obtain specific directory paths for MSVC and SDK installations, including the main installation directory, bin, include, and lib directories, using the msvc-kit Rust library. ```rust use msvc_kit::{download_msvc, download_sdk, DownloadOptions}; #[tokio::main] async fn main() -> msvc_kit::Result<()> { let options = DownloadOptions::default(); let msvc = download_msvc(&options).await?; println!("MSVC paths:"); println!(" Install: {:?}", msvc.install_path); println!(" Bin: {:?}", msvc.bin_dir()); println!(" Include: {:?}", msvc.include_dir()); println!(" Lib: {:?}", msvc.lib_dir()); let sdk = download_sdk(&options).await?; println!("SDK paths:"); println!(" Install: {:?}", sdk.install_path); println!(" Bin: {:?}", sdk.bin_dir()); println!(" Include: {:?}", sdk.include_dir()); println!(" Lib: {:?}", sdk.lib_dir()); Ok(()) } ``` -------------------------------- ### Cross-Compilation Setup (Rust API) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/architecture.md Rust API example for setting up cross-compilation environments. This snippet demonstrates how to configure the download options to build ARM64 binaries on an x64 host, including downloading the necessary cross-compilation tools and libraries. ```rust use msvc_kit::{DownloadOptions, Architecture}; // Build ARM64 binaries on x64 host let options = DownloadOptions::builder() .arch(Architecture::Arm64) .host_arch(Architecture::X64) .target_dir("C:/msvc-kit") .build(); // Downloads: HostX64.TargetARM64 tools + ARM64 CRT/MFC/ATL let info = msvc_kit::download_msvc(&options).await?; ``` -------------------------------- ### Rust build.rs: Programmatic MSVC Setup Check Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/build-script.md Illustrates how to programmatically check for MSVC availability within a Rust build.rs script and provides a warning if it's not found, suggesting the 'msvc-kit setup' command. It then proceeds to compile a C file using cc-rs. ```rust // build.rs use std::process::Command; fn main() { // Check if MSVC is available let cl_check = Command::new("cl").arg("/?").output(); if cl_check.is_err() { // Setup MSVC using msvc-kit println!("cargo:warning=MSVC not found, please run: msvc-kit setup"); } cc::Build::new() .file("src/native.c") .compile("native"); } ``` -------------------------------- ### Compile Static Library with msvc-kit Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/quick-compile.md Shows how to compile C++ source files into object files and then archive them into a static library using the 'lib' tool. msvc-kit is used for environment setup. ```powershell msvc-kit setup --script --shell powershell | iex # Compile cl /c /O2 /EHsc mylib.cpp # Create static library lib /OUT:mylib.lib mylib.obj ``` -------------------------------- ### Setup Environment Scripts Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Activates the MSVC development environment using provided setup scripts. These scripts make compiler tools like cl, link, and nmake available in the current shell session. ```bash # CMD cd msvc-bundle setup.bat ``` ```powershell # PowerShell cd msvc-bundle .\setup.ps1 ``` ```bash # Bash/WSL cd msvc-bundle source setup.sh ``` -------------------------------- ### Full Download Example with Multiple Options (Bash) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/cli-download.md A comprehensive example demonstrating the use of multiple options to configure the download of MSVC and SDK, including versions, target directory, architectures, and download settings. ```bash msvc-kit download \ --msvc-version 14.44 \ --sdk-version 10.0.26100.0 \ --target C:\msvc-kit \ --arch x64 \ --host-arch x64 \ --parallel-downloads 8 ``` -------------------------------- ### GitHub Actions Basic CI/CD Setup Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/ci-cd.md Sets up a basic CI/CD workflow using GitHub Actions to build a Rust project with msvc-kit. It checks out the code, installs Rust and msvc-kit, downloads MSVC, sets up the environment, and builds the project. ```yaml name: Build on: [push, pull_request] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: Install Rust uses: dtolnay/rust-action@stable - name: Install msvc-kit run: cargo install msvc-kit - name: Download MSVC run: msvc-kit download - name: Setup Environment run: msvc-kit setup --script --shell powershell | Invoke-Expression - name: Build run: cargo build --release ``` -------------------------------- ### Setup MSVC Environment Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/library.md Demonstrates setting up the MSVC environment using provided installation information for MSVC and optionally the Windows SDK. It returns an `MsvcEnvironment` struct containing paths and environment variables. ```rust use msvc_kit::setup_environment; // Assuming msvc_info and sdk_info are of type InstallInfo obtained from download functions // let env = setup_environment(&msvc_info, Some(&sdk_info))?; ``` -------------------------------- ### Install and Use msvc-kit via Command Line (PowerShell) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/index.md This snippet demonstrates how to install and use the msvc-kit toolchain using PowerShell commands. It covers installation via Winget, direct script download, and Cargo, followed by downloading the MSVC and Windows SDK, setting up environment variables, and verifying the installation by running 'cl /help'. ```powershell # Install via Winget (recommended) winget install loonghao.msvc-kit # Or via PowerShell script irm https://github.com/loonghao/msvc-kit/releases/latest/download/install.ps1 | iex # Or via Cargo cargo install msvc-kit # Download latest MSVC + Windows SDK msvc-kit download # Setup environment (PowerShell) msvc-kit setup --script --shell powershell | Invoke-Expression # Now you can compile! cl /help ``` -------------------------------- ### Setup Environment for CMD (Batch) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Generates a batch script for the Windows Command Prompt (CMD) to set up the MSVC environment. It can also create a portable setup by specifying a runtime directory. ```batch # Setup for current CMD session msvc-kit setup --script --shell cmd > setup.bat && setup.bat # Portable setup msvc-kit setup --script --shell cmd --portable-root "%~dp0runtime" > setup.bat ``` -------------------------------- ### Install and Setup msvc-kit for Rust Development Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/what-is-msvc-kit.md This snippet shows the basic commands to install msvc-kit using cargo, download the necessary MSVC components, and set up the environment for use with Rust projects. It automates the process of making the MSVC toolchain available to `cargo build`. ```bash cargo install msvc-kit msvc-kit download msvc-kit setup --script --shell powershell | Invoke-Expression ``` -------------------------------- ### Export Environment and Install Info to JSON using msvc-kit Rust Library Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/basic.md Shows how to export the MSVC environment configuration and installation details to JSON format using the msvc-kit Rust library. This is useful for configuration management and inter-process communication. ```rust use msvc_kit::{download_msvc, download_sdk, setup_environment, DownloadOptions}; #[tokio::main] async fn main() -> msvc_kit::Result<()> { let options = DownloadOptions::default(); let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; let env = setup_environment(&msvc, Some(&sdk))?; // Export environment to JSON let json = env.to_json(); println!("{}", serde_json::to_string_pretty(&json)?); // Export install info let msvc_json = msvc.to_json(); println!("{}", serde_json::to_string_pretty(&msvc_json)?); Ok(()) } ``` -------------------------------- ### Download MSVC to Custom Directory in Rust Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/download-options.md Shows how to specify a custom installation directory for MSVC downloads using `DownloadOptions`. This example overrides the default `target_dir` while keeping other options at their default values. ```rust use msvc_kit::{download_msvc, DownloadOptions}; use std::path::PathBuf; let options = DownloadOptions { target_dir: PathBuf::from("C:/my-msvc"), ..Default::default() }; ``` -------------------------------- ### Persist Environment Setup (PowerShell) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Sets up the MSVC environment persistently in the Windows registry, requiring administrator privileges. This ensures the environment is available across all new shell sessions without manual setup. ```powershell msvc-kit setup --persistent ``` -------------------------------- ### Example: Accessing cl.exe Path and Running Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/msvc-environment.md Illustrates how to get the path to `cl.exe` using `cl_exe_path()` and then execute it with the `/help` argument using `std::process::Command`. This demonstrates practical usage of the tool path methods. ```rust let env = setup_environment(&msvc, Some(&sdk))?; if let Some(cl) = env.cl_exe_path() { println!("cl.exe: {:?}", cl); // Run cl.exe std::process::Command::new(&cl) .arg("/help") .status()?; } ``` -------------------------------- ### Complete MSVC Build Example (Rust) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/tool-paths.md Provides a comprehensive example of setting up the MSVC environment, compiling a C++ file with `cl.exe`, and linking it into an executable with `link.exe` using the `msvc-kit` crate. It also sets necessary environment variables. ```rust use msvc_kit::{download_msvc, download_sdk, setup_environment, DownloadOptions}; use std::process::Command; async fn build_project() -> msvc_kit::Result<()> { // Setup environment let options = DownloadOptions::default(); let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; let env = setup_environment(&msvc, Some(&sdk))?; let tools = env.tool_paths(); // Set environment variables std::env::set_var("INCLUDE", env.include_path_string()); std::env::set_var("LIB", env.lib_path_string()); let cl = tools.cl.expect("cl.exe not found"); let link = tools.link.expect("link.exe not found"); // Compile Command::new(&cl) .args(["/c", "/O2", "main.cpp"]) .status()?; // Link Command::new(&link) .args(["main.obj", "/OUT:main.exe"]) .status()?; println!("Build complete!"); Ok(()) } ``` -------------------------------- ### Download MSVC to Custom Directory (Rust Library) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/custom-paths.md Illustrates using the msvc-kit Rust library to download MSVC and SDK components to a custom target directory. It configures download options and prints the installation paths. ```rust use msvc_kit::{download_msvc, download_sdk, DownloadOptions}; use std::path::PathBuf; #[tokio::main] async fn main() -> msvc_kit::Result<()> { let options = DownloadOptions { target_dir: PathBuf::from("D:\\BuildTools"), ..Default::default() }; let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; println!("MSVC: {:?}", msvc.install_path); println!("SDK: {:?}", sdk.install_path); Ok(()) } ``` -------------------------------- ### One-Liner C Compilation with msvc-kit Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/quick-compile.md Compiles a simple C program using msvc-kit's setup command and the cl compiler in a single PowerShell line. It assumes 'hello.c' is present in the current directory. ```powershell msvc-kit setup --script --shell powershell | iex; cl hello.c ``` -------------------------------- ### Setup Environment for Bash/WSL (Bash) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Configures the MSVC environment for Bash shells, commonly used in environments like Windows Subsystem for Linux (WSL) or Git Bash. ```bash eval "$(msvc-kit setup --script --shell bash)" ``` -------------------------------- ### PowerShell Usage Examples for compile.ps1 Script Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/quick-compile.md Demonstrates how to use the 'compile.ps1' script for both debug and release builds of C++ applications. It shows how to specify the source file and compiler options. ```powershell # Debug build .\compile.ps1 main.cpp -Cpp # Release build .\compile.ps1 main.cpp -Cpp -Release -Output app.exe ``` -------------------------------- ### PowerShell: CMake Build with MSVC Kit Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/build-script.md A PowerShell script to set up MSVC using 'msvc-kit setup' and then configure and build a project using CMake. It demonstrates building with both NMake Makefiles and Ninja generators. ```powershell # setup-and-build.ps1 # Setup MSVC msvc-kit setup --script --shell powershell | Invoke-Expression # Configure cmake -B build -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release # Build cmake --build build # Or use Ninja (faster) cmake -B build -G "Ninja" -DCMAKE_BUILD_TYPE=Release cmake --build build ``` -------------------------------- ### GitLab CI Setup with msvc-kit Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/ci-cd.md Configures a GitLab CI pipeline for building a Rust project with msvc-kit on a Windows runner. It installs Rust and msvc-kit, downloads MSVC, sets up the environment variables, and builds the project. ```yaml build: image: mcr.microsoft.com/windows/servercore:ltsc2022 tags: - windows script: - choco install rust -y - cargo install msvc-kit - msvc-kit download - $env = msvc-kit env --format json | ConvertFrom-Json - $env.PSObject.Properties | ForEach-Object { Set-Item "env:$($_.Name)" $_.Value } - cargo build --release ``` -------------------------------- ### Install msvc-kit via PowerShell Script Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Installs msvc-kit by downloading and executing an installation script directly from GitHub using PowerShell. ```powershell irm https://github.com/loonghao/msvc-kit/releases/latest/download/install.ps1 | iex ``` -------------------------------- ### Batch: Standalone MSVC Build Script Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/build-script.md A standalone Batch script for building a project with MSVC. It sets up the MSVC environment using 'msvc-kit setup', creates a build directory, compiles C++ source files, and links them into an executable. ```batch @echo off REM build.bat REM Setup MSVC for /f "delims=" %%i in ('msvc-kit setup --script --shell cmd') do %%i REM Create build directory if not exist build mkdir build REM Compile echo Compiling... cl /c /O2 /EHsc /MD /Fobuild\ src\*.cpp REM Link echo Linking... link /OUT:build\app.exe build\*.obj echo Build complete: build\app.exe ``` -------------------------------- ### Install msvc-kit Pre-built Binaries (PowerShell) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Downloads a pre-built Windows binary for msvc-kit and extracts it to the user's Cargo bin directory, making it available in the system's PATH. ```powershell Invoke-WebRequest -Uri "https://github.com/loonghao/msvc-kit/releases/latest/download/msvc-kit-x86_64-pc-windows-msvc.zip" -OutFile msvc-kit.zip Expand-Archive msvc-kit.zip -DestinationPath $env:USERPROFILE\.cargo\bin -Force ``` -------------------------------- ### Install and Use msvc-kit CLI (Bash) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Installs the msvc-kit command-line interface using Cargo, downloads the latest MSVC and Windows SDK, and sets up the environment for the current shell session. ```bash cargo install msvc-kit msvc-kit download msvc-kit setup --script --shell bash | eval ``` -------------------------------- ### Install msvc-kit from Source (Bash) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Clones the msvc-kit repository from GitHub and installs it locally using Cargo, allowing for development or use of the latest unreleased code. ```bash git clone https://github.com/loonghao/msvc-kit.git cd msvc-kit cargo install --path . ``` -------------------------------- ### Rust build.rs: C Library with Bindgen Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/build-script.md A comprehensive Rust build.rs example that compiles C source files into a library using cc-rs. It also includes optional integration with the 'bindgen' crate to generate Rust bindings for C headers. ```rust // build.rs use std::env; use std::path::PathBuf; fn main() { // Get output directory let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); // Build C library cc::Build::new() .file("native/math_ops.c") .file("native/string_ops.c") .include("native/include") .opt_level(2) .compile("native_ops"); // Generate bindings (optional, with bindgen) #[cfg(feature = "bindgen")] { let bindings = bindgen::Builder::default() .header("native/include/ops.h") .generate() .expect("Unable to generate bindings"); bindings .write_to_file(out_dir.join("bindings.rs")) .expect("Couldn't write bindings!"); } // Link libraries println!("cargo:rustc-link-lib=native_ops"); println!("cargo:rustc-link-search={}", out_dir.display()); } ``` -------------------------------- ### Basic InstallInfo Usage Example in Rust Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/install-info.md Demonstrates the basic usage of downloading an MSVC component and accessing its information. It shows how to obtain an InstallInfo object and print its properties like type, version, path, validity, and size. ```rust use msvc_kit::{download_msvc, DownloadOptions}; let options = DownloadOptions::default(); let info = download_msvc(&options).await?; println!("Component: {}", info.component_type); // "msvc" println!("Version: {}", info.version); // "14.44.34823" println!("Path: {:?}", info.install_path); println!("Valid: {}", info.is_valid()); println!("Size: {} bytes", info.total_size()); ``` -------------------------------- ### Verify MSVC Installation (PowerShell) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/dcc/unreal-engine.md Verifies the successful installation and setup of MSVC by checking the compiler version. It uses the `cl /?` command to display the compiler information. ```powershell cl /? # Should show: Microsoft (R) C/C++ Optimizing Compiler Version 19.38.xxxxx ``` -------------------------------- ### Multi-File Compilation with msvc-kit Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/quick-compile.md Compiles multiple C++ source files into a single executable. This example uses wildcard compilation and specifies the output executable name. msvc-kit is used for environment setup. ```powershell msvc-kit setup --script --shell powershell | iex # Compile all .cpp files cl /O2 /EHsc /Fe:app.exe src\*.cpp ``` -------------------------------- ### Download MSVC Tools and Setup Environment (Rust) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/tool-paths.md Demonstrates how to download MSVC and SDK components, set up the build environment, and retrieve the tool paths using the `msvc-kit` crate. This is a common pattern for preparing a build environment. ```rust use msvc_kit::{download_msvc, download_sdk, setup_environment, DownloadOptions}; let options = DownloadOptions::default(); let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; let env = setup_environment(&msvc, Some(&sdk))?; // Get all tool paths let tools = env.tool_paths(); ``` -------------------------------- ### MSVC Environment Variables JSON Output Example Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/cli-setup.md An example of the JSON output format for environment variables provided by `msvc-kit env --format json`. This format is useful for scripting and automated parsing. ```json { "VCINSTALLDIR": "C:\\msvc-kit\\VC", "VCToolsInstallDir": "C:\\msvc-kit\\VC\\Tools\\MSVC\\14.44.34823", "VCToolsVersion": "14.44.34823", "WindowsSdkDir": "C:\\msvc-kit\\Windows Kits\\10", "WindowsSDKVersion": "10.0.26100.0\\", "INCLUDE": "C:\\msvc-kit\\VC\\Tools\\MSVC\\14.44.34823\\include;...", "LIB": "C:\\msvc-kit\\VC\\Tools\\MSVC\\14.44.34823\\lib\\x64;...", "PATH": "C:\\msvc-kit\\VC\\Tools\\MSVC\\14.44.34823\\bin\\Hostx64\\x64;..." } ``` -------------------------------- ### Usage Examples Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/download-options.md Illustrates various ways to use the `DownloadOptions` struct to configure download operations. ```APIDOC ## Usage Examples ### Default Options ```rust use msvc_kit::{download_msvc, DownloadOptions}; let options = DownloadOptions::default(); let info = download_msvc(&options).await?; ``` ### Custom Directory ```rust use msvc_kit::{download_msvc, DownloadOptions}; use std::path::PathBuf; let options = DownloadOptions { target_dir: PathBuf::from("C:/my-msvc"), ..Default::default() }; ``` ### Specific Versions ```rust use msvc_kit::{download_msvc, download_sdk, DownloadOptions}; let options = DownloadOptions { msvc_version: Some("14.44".to_string()), sdk_version: Some("10.0.26100.0".to_string()), ..Default::default() }; let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; ``` ### Cross-Compilation ```rust use msvc_kit::{download_msvc, DownloadOptions, Architecture}; // Build ARM64 binaries on x64 host let options = DownloadOptions { arch: Architecture::Arm64, host_arch: Some(Architecture::X64), ..Default::default() }; ``` ### Performance Tuning ```rust use msvc_kit::{download_msvc, DownloadOptions}; let options = DownloadOptions { parallel_downloads: 8, // More parallel downloads verify_hashes: false, // Skip verification (not recommended) ..Default::default() }; ``` ``` -------------------------------- ### Auto-setup MSVC Environment in Bash Profile Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/cli-setup.md Adds a command to the `~/.bashrc` file to automatically set up the MSVC environment using `msvc-kit setup` when a new Bash session starts. It checks for the `msvc-kit` command before execution. ```bash # Auto-setup MSVC environment if command -v msvc-kit &> /dev/null; eval "$(msvc-kit setup --script --shell bash)" fi ``` -------------------------------- ### Download MSVC to Custom Directory (CLI) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/custom-paths.md Demonstrates how to download and set up MSVC toolchains to a specified custom directory using the msvc-kit command-line interface. ```bash # Install to custom directory msvc-kit download --target D:\BuildTools # Setup from custom directory msvc-kit setup --script --shell powershell | Invoke-Expression ``` -------------------------------- ### Auto-setup MSVC Environment in PowerShell Profile Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/cli-setup.md Configures the PowerShell profile (`$PROFILE`) to automatically set up the MSVC environment by running `msvc-kit setup` if the command is available. This ensures the toolchain is ready upon starting a new PowerShell session. ```powershell # Auto-setup MSVC environment if (Get-Command msvc-kit -ErrorAction SilentlyContinue) { msvc-kit setup --script --shell powershell | Invoke-Expression } ``` -------------------------------- ### Configure Cross-Compilation Options in Rust Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/download-options.md Demonstrates setting up `DownloadOptions` for cross-compilation, specifically building ARM64 binaries on an x64 host. This involves setting the `arch` to `Architecture::Arm64` and `host_arch` to `Some(Architecture::X64)`. ```rust use msvc_kit::{download_msvc, DownloadOptions, Architecture}; // Build ARM64 binaries on x64 host let options = DownloadOptions { arch: Architecture::Arm64, host_arch: Some(Architecture::X64), ..Default::default() }; ``` -------------------------------- ### Rust: Setup MSVC Environment Variables Source: https://context7.com/loonghao/msvc-kit/llms.txt Configure environment variables and access tool paths after downloading MSVC and SDK components using the msvc-kit Rust library. Demonstrates setting environment variables for the current process and retrieving tool paths. ```rust use msvc_kit::{download_msvc, download_sdk, setup_environment, get_env_vars, DownloadOptions}; use std::env; #[tokio::main] async fn main() -> msvc_kit::Result<()> { let options = DownloadOptions::default(); let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; // Create environment from install info let msvc_env = setup_environment(&msvc, Some(&sdk))?; // Access tool paths if msvc_env.has_cl_exe() { println!("cl.exe: {:?}", msvc_env.cl_exe_path()); } println!("link.exe: {:?}", msvc_env.link_exe_path()); println!("lib.exe: {:?}", msvc_env.lib_exe_path()); println!("rc.exe: {:?}", msvc_env.rc_exe_path()); // Get all tools at once let tools = msvc_env.tool_paths(); println!("Compiler: {:?}", tools.cl); println!("Linker: {:?}", tools.link); // Get environment strings for shell println!("INCLUDE: {}", msvc_env.include_path_string()); println!("LIB: {}", msvc_env.lib_path_string()); println!("PATH additions: {}", msvc_env.bin_path_string()); // Apply to current process env::set_var("INCLUDE", msvc_env.include_path_string()); env::set_var("LIB", msvc_env.lib_path_string()); let current_path = env::var("PATH").unwrap_or_default(); env::set_var("PATH", format!("{}; மூல{}", msvc_env.bin_path_string(), current_path)); // Get as HashMap for custom processing let env_map = get_env_vars(&msvc_env); for (key, value) in &env_map { println!("{}={}", key, value); } Ok(()) } ``` -------------------------------- ### Create Absolute Script Context Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/library.md Example of creating a `ScriptContext` for absolute path scripts, which references specific installation directories. This is suitable for environments where MSVC is pre-installed. ```rust use msvc_kit::{ScriptContext, Architecture}; use std::path::PathBuf; let ctx = ScriptContext::absolute( PathBuf::from("C:/msvc-kit"), "14.44.34823", "10.0.26100.0", Architecture::X64, Architecture::X64, ); ``` -------------------------------- ### Clone, Setup, and Generate Project Files (Bash/PowerShell) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/dcc/unreal-engine.md Demonstrates the process of cloning the Unreal Engine source code, setting up the MSVC environment, and generating project files using the Unreal Engine's setup scripts. This prepares the project for building. ```bash git clone https://github.com/EpicGames/UnrealEngine.git cd UnrealEngine ``` ```powershell # Setup MSVC environment first msvc-kit setup --script --shell powershell | Invoke-Expression # Run setup .\Setup.bat .\GenerateProjectFiles.bat ``` -------------------------------- ### Print Environment Variables as JSON Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Outputs environment variables in JSON format. This is useful for integrating msvc-kit environment setup into other applications or scripts that consume JSON. ```bash msvc-kit env --format json ``` -------------------------------- ### Compile Windows GUI Application with msvc-kit Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/quick-compile.md Compiles a Windows GUI application, including necessary flags for GUI subsystems and linking user32 and gdi32 libraries. msvc-kit is used for environment setup. ```powershell msvc-kit setup --script --shell powershell | iex cl /O2 /EHsc /DWIN32 /D_WINDOWS main.cpp user32.lib gdi32.lib /link /SUBSYSTEM:WINDOWS ``` -------------------------------- ### Download MSVC and SDK using msvc-kit Rust Library Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/basic.md Demonstrates how to programmatically download the latest MSVC and SDK using the msvc-kit Rust library. It utilizes `tokio` for asynchronous operations and `DownloadOptions` for configuration. ```rust use msvc_kit::{download_msvc, download_sdk, DownloadOptions}; #[tokio::main] async fn main() -> msvc_kit::Result<()> { let options = DownloadOptions::default(); println!("Downloading MSVC..."); let msvc = download_msvc(&options).await?; println!("MSVC installed to: {:?}", msvc.install_path); println!("Downloading SDK..."); let sdk = download_sdk(&options).await?; println!("SDK installed to: {:?}", sdk.install_path); Ok(()) } ``` -------------------------------- ### Download MSVC and Setup Environment (Bash/PowerShell) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/dcc/unreal-engine.md Downloads the specified MSVC version and Windows SDK using msvc-kit, and sets up the environment variables required for building Unreal Engine projects. This is a prerequisite for all other build steps. ```bash # For UE 5.4+ msvc-kit download --msvc-version 14.38 --sdk-version 10.0.22621.0 ``` ```powershell msvc-kit setup --script --shell powershell | Invoke-Expression ``` -------------------------------- ### Tune Download Performance in Rust Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/download-options.md Shows how to optimize download performance by adjusting `parallel_downloads` and `verify_hashes` in `DownloadOptions`. This example increases parallel downloads to 8 and disables hash verification (with a note of caution). ```rust use msvc_kit::{download_msvc, DownloadOptions}; let options = DownloadOptions { parallel_downloads: 8, // More parallel downloads verify_hashes: false, // Skip verification (not recommended) ..Default::default() }; ``` -------------------------------- ### Azure Pipelines CI/CD Setup with msvc-kit Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/ci-cd.md Sets up a CI/CD pipeline in Azure Pipelines to build a Rust project using msvc-kit. It installs Rust, msvc-kit, downloads MSVC, configures the environment, and compiles the project. ```yaml trigger: - main pool: vmImage: 'windows-latest' steps: - task: RustInstaller@1 inputs: rustVersion: 'stable' - script: cargo install msvc-kit displayName: 'Install msvc-kit' - script: msvc-kit download displayName: 'Download MSVC' - powershell: msvc-kit setup --script --shell powershell | Invoke-Expression displayName: 'Setup Environment' - script: cargo build --release displayName: 'Build' ``` -------------------------------- ### Download MSVC with Default Options in Rust Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/download-options.md Demonstrates how to download MSVC using the default `DownloadOptions`. This is a basic usage example that leverages the `download_msvc` function from the `msvc_kit` crate. ```rust use msvc_kit::{download_msvc, DownloadOptions}; let options = DownloadOptions::default(); let info = download_msvc(&options).await?; ``` -------------------------------- ### Create MsvcEnvironment Instance Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/msvc-environment.md Demonstrates how to create an MsvcEnvironment instance by downloading necessary MSVC and SDK components using `download_msvc` and `download_sdk`, then setting up the environment with `setup_environment`. Requires `msvc-kit` crate and asynchronous execution. ```rust use msvc_kit::{download_msvc, download_sdk, setup_environment, DownloadOptions}; let options = DownloadOptions::default(); let msvc = download_msvc(&options).await?; let sdk = download_sdk(&options).await?; // Create environment from install info let env = setup_environment(&msvc, Some(&sdk))?; ``` -------------------------------- ### Compile DLL Library with msvc-kit Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/quick-compile.md Demonstrates the two-step process for creating a DLL: first compiling the source files into an object file with DLL export definitions, then linking the object file into a DLL. msvc-kit sets up the environment. ```powershell msvc-kit setup --script --shell powershell | iex # Compile cl /c /O2 /EHsc /MD /DDLL_EXPORTS mylib.cpp # Link as DLL link /DLL /OUT:mylib.dll mylib.obj ``` -------------------------------- ### Print Environment Variables as Shell Script Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Outputs environment variables required for MSVC development in a format suitable for sourcing in a shell script. This allows programmatic setup of the development environment. ```bash msvc-kit env ``` -------------------------------- ### Rust build.rs: Basic cc-rs Usage Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/build-script.md Demonstrates the basic usage of the 'cc' crate in Rust's build.rs file to compile a C source file using MSVC. Assumes 'msvc-kit setup' has been run. ```rust // build.rs fn main() { cc::Build::new() .file("src/native.c") .compile("native"); } ``` -------------------------------- ### Integrate MSVC with MSBuild Build Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/cli-setup.md Illustrates how to set up the MSVC environment with `msvc-kit` and then use MSBuild to compile a project, specifically targeting the 'Release' configuration. ```bash msvc-kit setup --script --shell powershell | Invoke-Expression msbuild MyProject.vcxproj /p:Configuration=Release ``` -------------------------------- ### Download MSVC and SDK with Options (Bash) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Downloads specific versions of MSVC and Windows SDK, targets a custom directory, and specifies host and target architectures. Includes options to skip SDK/MSVC, control parallel downloads, and disable verification. ```bash # Download latest versions msvc-kit download # Specify versions and directories msvc-kit download \ --msvc-version 14.44 \ --sdk-version 10.0.26100.0 \ --target C:\msvc-kit \ --arch x64 \ --host-arch x64 # Download only MSVC msvc-kit download --no-sdk # Download only SDK msvc-kit download --no-msvc # Control parallel downloads msvc-kit download --parallel-downloads 8 # Skip hash verification msvc-kit download --no-verify ``` -------------------------------- ### Load and Save Configuration with msvc-kit Source: https://context7.com/loonghao/msvc-kit/llms.txt Demonstrates how to load, modify, and save persistent configuration settings using the msvc-kit library. This is useful for managing default toolchain versions and installation directories. ```rust use msvc_kit::{load_config, save_config, MsvcKitConfig}; fn main() -> msvc_kit::Result<()> { // Load existing configuration let mut config = load_config()?; println!("Current install dir: {:?}", config.install_dir); println!("Default MSVC: {:?}", config.default_msvc_version); println!("Default SDK: {:?}", config.default_sdk_version); // Modify configuration config.install_dir = Some("C:/msvc-kit".into()); config.default_msvc_version = Some("14.44".to_string()); config.default_sdk_version = Some("10.0.26100.0".to_string()); // Save configuration save_config(&config)?; Ok(()) } ``` -------------------------------- ### PowerShell: Standalone MSVC Build Script Source: https://github.com/loonghao/msvc-kit/blob/main/docs/examples/build-script.md A standalone PowerShell script for building a project using MSVC. It first sets up the MSVC environment using 'msvc-kit setup', creates a build directory, compiles C++ source files, and then links them into an executable. ```powershell # build.ps1 param( [string]$Configuration = "Release", [string]$Arch = "x64" ) # Setup MSVC Write-Host "Setting up MSVC environment..." msvc-kit setup --script --shell powershell | Invoke-Expression # Create build directory $BuildDir = "build\$Configuration" New-Item -ItemType Directory -Force -Path $BuildDir | Out-Null # Compile Write-Host "Compiling..." $Sources = Get-ChildItem -Path "src" -Filter "*.cpp" -Recurse foreach ($src in $Sources) { $obj = Join-Path $BuildDir ($src.BaseName + ".obj") cl /c /O2 /EHsc /MD /Fo"$obj" $src.FullName } # Link Write-Host "Linking..." $Objects = Get-ChildItem -Path $BuildDir -Filter "*.obj" $ObjList = $Objects.FullName -join " " link /OUT:"$BuildDir\app.exe" $ObjList Write-Host "Build complete: $BuildDir\app.exe" ``` -------------------------------- ### Install msvc-kit via Winget (PowerShell) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Installs the msvc-kit tool using the Windows Package Manager (winget), providing a convenient and recommended installation method. ```powershell winget install loonghao.msvc-kit ``` -------------------------------- ### Verify msvc-kit Installation Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/installation.md Commands to verify that msvc-kit has been installed correctly and to check its version and available help options. ```bash msvc-kit --version # msvc-kit 0.1.x msvc-kit --help ``` -------------------------------- ### Example: Setting MSVC Environment Variables Source: https://github.com/loonghao/msvc-kit/blob/main/docs/api/msvc-environment.md Demonstrates how to set the `INCLUDE`, `LIB`, and `PATH` environment variables programmatically using the strings generated by `include_path_string()`, `lib_path_string()`, and `bin_path_string()`. This is crucial for configuring the build environment. ```rust use std::env; let msvc_env = setup_environment(&msvc, Some(&sdk))?; // Set INCLUDE env::set_var("INCLUDE", msvc_env.include_path_string()); // Set LIB env::set_var("LIB", msvc_env.lib_path_string()); // Prepend to PATH let current_path = env::var("PATH").unwrap_or_default(); env::set_var("PATH", format!("{};{}", msvc_env.bin_path_string(), current_path)); ``` -------------------------------- ### Install msvc-kit from crates.io (Bash) Source: https://github.com/loonghao/msvc-kit/blob/main/README.md Installs the msvc-kit command-line tool directly from the crates.io registry using the Cargo package manager. ```bash cargo install msvc-kit ``` -------------------------------- ### Troubleshooting: Verify INCLUDE and LIB Paths (PowerShell) Source: https://github.com/loonghao/msvc-kit/blob/main/docs/dcc/unreal-engine.md Provides instructions on how to troubleshoot linker errors by verifying that the INCLUDE and LIB environment variables are correctly set. ```powershell echo $env:INCLUDE echo $env:LIB ``` -------------------------------- ### Set msvc-kit Installation Directory Source: https://github.com/loonghao/msvc-kit/blob/main/docs/guide/cli-config.md Sets the installation directory for msvc-kit. This option allows users to specify a custom location for msvc-kit installations, overriding the default path. The new path is persisted in the configuration file. ```bash msvc-kit config --set-dir C:\msvc-kit ```