microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
build_support/setup_windows_cross.sh
122lines · modecode
| 1 | #!/bin/bash |
| 2 | |
| 3 | # See the guide page on more information on required dependencies. |
| 4 | |
| 5 | # Validate that a tool is present. |
| 6 | function check_cross_tool { |
| 7 | if ! command -v "$1" >/dev/null 2>/dev/null; then |
| 8 | >&2 echo "missing $1 - Try 'sudo apt install clang-tools-14 lld-14' or check the guide." |
| 9 | false |
| 10 | fi |
| 11 | } |
| 12 | |
| 13 | # Extract the contents of the Windows INCLUDE and LIB environment variables |
| 14 | # after running vcvarsall.bat. |
| 15 | function extract_include_lib { |
| 16 | ( |
| 17 | set -e |
| 18 | |
| 19 | arch="$1" |
| 20 | component="$2" |
| 21 | arch_param="$3" |
| 22 | |
| 23 | vswhere="$(wslpath 'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe')" |
| 24 | [ -x "$vswhere" ] || { |
| 25 | >&2 echo "$arch: missing Visual Studio installation" |
| 26 | exit 1 |
| 27 | } |
| 28 | |
| 29 | vcvarsall="$(</dev/null "$vswhere" -requires "$component" -products '*' -latest -find 'VC\Auxiliary\Build\vcvarsall.bat' -format value)" |
| 30 | [ -n "$vcvarsall" ] || { |
| 31 | >&2 echo "warning: $arch: failed to find VC tools" |
| 32 | exit 1 |
| 33 | } |
| 34 | |
| 35 | vcvarsall_wsl="$(wslpath "$vcvarsall")" |
| 36 | vcvarsall_path="$(dirname "$vcvarsall_wsl")" |
| 37 | cd "$vcvarsall_path" |
| 38 | |
| 39 | # Run the script in cmd, then re-invoke WSL with WSLENV to convert the |
| 40 | # Windows path list variables INCLUDE and LIB to Linux path list |
| 41 | # variables. Then convert the Linux path separator : back to ; since |
| 42 | # that's what clang/LLVM expects. |
| 43 | # |
| 44 | # shellcheck disable=SC2016 |
| 45 | </dev/null cmd.exe /v:on /c ".\\vcvarsall.bat $arch_param > nul" "&&" \ |
| 46 | set "WSLENV=INCLUDE/l:LIB/l" "&&" \ |
| 47 | wsl -d "$WSL_DISTRO_NAME" echo '$INCLUDE' '^&^&' echo '$LIB' \ |
| 48 | | tr ':' ';' |
| 49 | [[ ${PIPESTATUS[0]} == 0 ]] |
| 50 | ) |
| 51 | } |
| 52 | |
| 53 | function fatal_error { |
| 54 | >&2 echo -e "\033[0;31m$1\033[0m" |
| 55 | exit 1 |
| 56 | } |
| 57 | |
| 58 | function setup_windows_cross { |
| 59 | local llvm_version=${OPENVMM_LLVM_VERSION:-${HVLITE_LLVM_VERSION:-14}} |
| 60 | # NOTE: clang-cl-<ver> is the msvc style arguments, which is what we want. This |
| 61 | # is sometimes in a different package than the default clang-<ver> which |
| 62 | # is gcc style. |
| 63 | local clang=clang-cl-"$llvm_version" |
| 64 | local lld=lld-link-"$llvm_version" |
| 65 | local lib=llvm-lib-"$llvm_version" |
| 66 | local dlltool=llvm-dlltool-"$llvm_version" |
| 67 | local rc=llvm-rc-"$llvm_version" |
| 68 | |
| 69 | check_cross_tool "$clang" || return 1 |
| 70 | check_cross_tool "$lld" || return 1 |
| 71 | check_cross_tool "$lib" || return 1 |
| 72 | check_cross_tool "$dlltool" || return 1 |
| 73 | check_cross_tool "$rc" || return 1 |
| 74 | |
| 75 | export WINDOWS_CROSS_CL="$clang" |
| 76 | export WINDOWS_CROSS_LINK="$lld" |
| 77 | export DLLTOOL="$dlltool" |
| 78 | local mydir="$(dirname -- "${BASH_SOURCE[0]}")" |
| 79 | local myfulldir="$(realpath "$mydir")" |
| 80 | |
| 81 | if [[ -x /bin/wslpath ]] && [[ $(wslpath -aw "$myfulldir") != '\\wsl.localhost\'* ]]; |
| 82 | then |
| 83 | fatal_error "\033[0;33mWARNING: This script is being run from a Windows partition. This will not work. Please move your repo clone to the WSL filesystem.\033[0m" |
| 84 | fi |
| 85 | |
| 86 | local tooldir="$(realpath "$myfulldir/../build_support/windows_cross")" |
| 87 | |
| 88 | if env=$(extract_include_lib x86_64 Microsoft.VisualStudio.Component.VC.Tools.x86.x64 x64); then |
| 89 | # Extract the variables, one line each. |
| 90 | IFS=$'\n' read -rd '' INCLUDE LIB <<< "$env" || true |
| 91 | export WINDOWS_CROSS_X86_64_LIB="$LIB" |
| 92 | export WINDOWS_CROSS_X86_64_INCLUDE="$INCLUDE" |
| 93 | export CC_x86_64_pc_windows_msvc="$tooldir/x86_64-clang-cl" |
| 94 | export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER="$tooldir/x86_64-lld-link" |
| 95 | export AR_x86_64_pc_windows_msvc="$lib" |
| 96 | export RC_x86_64_pc_windows_msvc="$rc" |
| 97 | [ -h $CC_x86_64_pc_windows_msvc ] || fatal_error "$CC_x86_64_pc_windows_msvc is not a symbolic link, check git config core.symlinks:\n" |
| 98 | [ -h $CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER ] || fatal_error "$CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER is not a symbolic link, check git config core.symlinks:\n" |
| 99 | echo x86_64 |
| 100 | fi |
| 101 | |
| 102 | # FUTURE: use just "arm64" for the arch when the host arch is arm64. |
| 103 | if env=$(extract_include_lib aarch64 Microsoft.VisualStudio.Component.VC.Tools.ARM64 x64_arm64); then |
| 104 | # Extract the variables, one line each. |
| 105 | IFS=$'\n' read -rd '' INCLUDE LIB <<< "$env" || true |
| 106 | export WINDOWS_CROSS_AARCH64_LIB="$LIB" |
| 107 | export WINDOWS_CROSS_AARCH64_INCLUDE="$INCLUDE" |
| 108 | export CC_aarch64_pc_windows_msvc="$tooldir/aarch64-clang-cl" |
| 109 | export CARGO_TARGET_AARCH64_PC_WINDOWS_MSVC_LINKER="$tooldir/aarch64-lld-link" |
| 110 | export AR_aarch64_pc_windows_msvc="$lib" |
| 111 | export RC_aarch64_pc_windows_msvc="$rc" |
| 112 | [ -h $CC_aarch64_pc_windows_msvc ] || fatal_error "$CC_aarch64_pc_windows_msvc is not a symbolic link, check git config core.symlinks:\n" |
| 113 | [ -h $CARGO_TARGET_AARCH64_PC_WINDOWS_MSVC_LINKER ] || fatal_error "$CARGO_TARGET_AARCH64_PC_WINDOWS_MSVC_LINKER is not a symbolic link, check git config core.symlinks:\n" |
| 114 | echo aarch64 |
| 115 | fi |
| 116 | } |
| 117 | |
| 118 | # Check if this file was run directly instead of sourced, and fail with a |
| 119 | # warning if so. |
| 120 | (return 0 2>/dev/null) || fatal_error "You must run $0 by sourcing it. Try instead:\n . $0" |
| 121 | |
| 122 | setup_windows_cross |
| 123 | |