feat: Implement cross-compiling to MSVC

This commit is contained in:
Lucas Schwiderski 2023-05-25 23:37:53 +02:00
parent 51e00b6394
commit bba4790ff7
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
6 changed files with 100 additions and 8 deletions

4
.fdignore Normal file
View file

@ -0,0 +1,4 @@
/msvc
/target
/.xwin-cache
/.wineprefix

13
.gitignore vendored
View file

@ -1,9 +1,6 @@
.envrc
/target /target
/msvc
/tools/xwin
# Added by cargo /.xwin-cache
# /.wineprefix
# already existing elements were commented out
#/target
/Cargo.lock

View file

@ -5,5 +5,18 @@ members = [
"lib/nat_traversal" "lib/nat_traversal"
] ]
[unstable]
build-std = true
# [profile.dev.package.backtrace] # [profile.dev.package.backtrace]
# opt-level = 3 # opt-level = 3
[profile.release]
strip = "debuginfo"
# The MSVC toolchain cannot handle LTO properly. Some symbol related to
# panic unwind would always be missing.
# So we use a separate profile for when we can compile with LTO.
[profile.release-lto]
inherits = "release"
lto = true

54
Justfile Normal file
View file

@ -0,0 +1,54 @@
xwin_version := '0.2.12'
xwin_prefix := 'xwin-' + xwin_version + '-x86_64-unknown-linux-musl'
export WINEPREFIX := justfile_directory() + "/.wineprefix"
export WINEDEBUG := '-all'
# Note that we only disable unused-command-line-argument here since clang-cl
# doesn't implement all of the options supported by cl, but the ones it doesn't
# are _generally_ not interesting.
CL_FLAGS := "-Wno-unused-command-line-argument -fuse-ld=lld-link ./msvc/crt/include ./msvc/sdk/include/ucrt ./msvc/sdk/include/um ./msvc/sdk/include/shared"
export CC_x86_64_pc_windows_msvc := "clang-cl"
export CXX_x86_64_pc_windows_msvc := "clang-cl"
export AR_x86_64_pc_windows_msvc := "llvm-lib"
# Use wine to run test executables
export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER := "wine"
# Let cargo know what linker to invoke if you haven't already specified it
# in a .cargo/config.toml file
export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER := "lld-link"
export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS := "-Lnative=./msvc/crt/lib/x86_64 -Lnative=./msvc/sdk/lib/um/x86_64 -Lnative=./msvc/sdk/lib/ucrt/x86_64"
export CFLAGS_x86_64_pc_windows_msvc := CL_FLAGS
export CXXFLAGS_x86_64_pc_windows_msvc := CL_FLAGS
default: build
build: start-wineserver
cargo build -Zbuild-std --target x86_64-pc-windows-msvc
[ -d "$game_dir" ] && cp ./target/x86_64-pc-windows-msvc/debug/dt_p2p.dll "$game_dir/binaries/plugins/dt_p2p_debug_pluginw64_release.dll"
[no-exit-message]
wine *ARGS:
@wine {{ ARGS }}
start-wineserver:
#!/bin/sh
set -eu
bold=$(tput bold)
normal=$(tput sgr0)
mkdir -p "$WINEPREFIX"
if ! pgrep wineserver >/dev/null; then
echo "${bold}wineserver -p${normal}"
wineserver -p
echo "${bold}wine wineboot --init --update${normal}"
wine wineboot --init --update
fi
setup-msvc:
rustup target add x86_64-pc-windows-msvc
rustup component add rust-src
mkdir -p ./tools
curl --fail -L https://github.com/Jake-Shadle/xwin/releases/download/{{ xwin_version }}/{{ xwin_prefix }}.tar.gz | tar -xzv -C ./tools --strip-components=1 {{ xwin_prefix }}/xwin
./tools/xwin --accept-license splat --include-debug-libs --output ./msvc

4
rust-toolchain.toml Normal file
View file

@ -0,0 +1,4 @@
[toolchain]
channel = "nightly"
components = ["rust-src"]
targets = ["x86_64-pc-windows-msvc"]

20
rustfmt.toml Normal file
View file

@ -0,0 +1,20 @@
# Copyright (C) 2022 Lucas Schwiderski
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
unstable_features = true
hard_tabs = false
max_width = 100
edition = "2021"
use_field_init_shorthand = true