microsoft/openvmm

Public

mirrored fromhttps://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6531c77717c0bab81bc1fec26aa386d69cbe507a

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

build_support/setup_windows_cross.sh

117lines · modecode

1#!/bin/bash
2
3# See the guide page on more information on required dependencies.
4
5# Validate that a tool is present.
6function 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.
15function 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
53function setup_windows_cross {
54 local llvm_version=${HVLITE_LLVM_VERSION:-14}
55 # NOTE: clang-cl-<ver> is the msvc style arguments, which is what we want. This
56 # is sometimes in a different package than the default clang-<ver> which
57 # is gcc style.
58 local clang=clang-cl-"$llvm_version"
59 local lld=lld-link-"$llvm_version"
60 local lib=llvm-lib-"$llvm_version"
61 local dlltool=llvm-dlltool-"$llvm_version"
62 local rc=llvm-rc-"$llvm_version"
63
64 check_cross_tool "$clang" || return 1
65 check_cross_tool "$lld" || return 1
66 check_cross_tool "$lib" || return 1
67 check_cross_tool "$dlltool" || return 1
68 check_cross_tool "$rc" || return 1
69
70 export WINDOWS_CROSS_CL="$clang"
71 export WINDOWS_CROSS_LINK="$lld"
72 export DLLTOOL="$dlltool"
73 local mydir="$(dirname -- "${BASH_SOURCE[0]}")"
74 local myfulldir="$(realpath "$mydir")"
75
76 if [[ -x /bin/wslpath ]] && [[ $(wslpath -aw "$myfulldir") != '\\wsl.localhost\'* ]];
77 then
78 >&2 echo -e "\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"
79 fi
80
81 local tooldir="$(realpath "$myfulldir/../build_support/windows_cross")"
82
83 if env=$(extract_include_lib x86_64 Microsoft.VisualStudio.Component.VC.Tools.x86.x64 x64); then
84 # Extract the variables, one line each.
85 IFS=$'\n' read -rd '' INCLUDE LIB <<< "$env" || true
86 export WINDOWS_CROSS_X86_64_LIB="$LIB"
87 export WINDOWS_CROSS_X86_64_INCLUDE="$INCLUDE"
88 export CC_x86_64_pc_windows_msvc="$tooldir/x86_64-clang-cl"
89 export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER="$tooldir/x86_64-lld-link"
90 export AR_x86_64_pc_windows_msvc="$lib"
91 export RC_x86_64_pc_windows_msvc="$rc"
92 [ -h $CC_x86_64_pc_windows_msvc ] || ci/error.sh "$CC_x86_64_pc_windows_msvc is not a symbolic link, check git config core.symlinks:\n" || exit 1
93 [ -h $CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER ] || ci/error.sh "$CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER is not a symbolic link, check git config core.symlinks:\n" || exit 1
94 echo x86_64
95 fi
96
97 # FUTURE: use just "arm64" for the arch when the host arch is arm64.
98 if env=$(extract_include_lib aarch64 Microsoft.VisualStudio.Component.VC.Tools.ARM64 x64_arm64); then
99 # Extract the variables, one line each.
100 IFS=$'\n' read -rd '' INCLUDE LIB <<< "$env" || true
101 export WINDOWS_CROSS_AARCH64_LIB="$LIB"
102 export WINDOWS_CROSS_AARCH64_INCLUDE="$INCLUDE"
103 export CC_aarch64_pc_windows_msvc="$tooldir/aarch64-clang-cl"
104 export CARGO_TARGET_AARCH64_PC_WINDOWS_MSVC_LINKER="$tooldir/aarch64-lld-link"
105 export AR_aarch64_pc_windows_msvc="$lib"
106 export RC_aarch64_pc_windows_msvc="$rc"
107 [ -h $CC_aarch64_pc_windows_msvc ] || ci/error.sh "$CC_aarch64_pc_windows_msvc is not a symbolic link, check git config core.symlinks:\n" || exit 1
108 [ -h $CARGO_TARGET_AARCH64_PC_WINDOWS_MSVC_LINKER ] || ci/error.sh "$CARGO_TARGET_AARCH64_PC_WINDOWS_MSVC_LINKER is not a symbolic link, check git config core.symlinks:\n" || exit 1
109 echo aarch64
110 fi
111}
112
113# Check if this file was run directly instead of sourced, and fail with a
114# warning if so.
115(return 0 2>/dev/null) || ci/error.sh "You must run $0 by sourcing it. Try instead:\n . $0" || exit 1
116
117setup_windows_cross
118