From bba4790ff762235bf54bd9eb1716db0123d76363 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 25 May 2023 23:37:53 +0200 Subject: [PATCH] feat: Implement cross-compiling to MSVC --- .fdignore | 4 ++++ .gitignore | 13 +++++------ Cargo.toml | 13 +++++++++++ Justfile | 54 +++++++++++++++++++++++++++++++++++++++++++++ rust-toolchain.toml | 4 ++++ rustfmt.toml | 20 +++++++++++++++++ 6 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 .fdignore create mode 100644 Justfile create mode 100644 rust-toolchain.toml create mode 100644 rustfmt.toml diff --git a/.fdignore b/.fdignore new file mode 100644 index 0000000..306cc3a --- /dev/null +++ b/.fdignore @@ -0,0 +1,4 @@ +/msvc +/target +/.xwin-cache +/.wineprefix diff --git a/.gitignore b/.gitignore index 9ce42ca..d391e22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,6 @@ +.envrc /target - - -# Added by cargo -# -# already existing elements were commented out - -#/target -/Cargo.lock +/msvc +/tools/xwin +/.xwin-cache +/.wineprefix diff --git a/Cargo.toml b/Cargo.toml index d157fa5..d083bef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,18 @@ members = [ "lib/nat_traversal" ] +[unstable] +build-std = true + # [profile.dev.package.backtrace] # 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 diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..57b5fa9 --- /dev/null +++ b/Justfile @@ -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 diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..6b745bd --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "nightly" +components = ["rust-src"] +targets = ["x86_64-pc-windows-msvc"] diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..a031646 --- /dev/null +++ b/rustfmt.toml @@ -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 . +# +unstable_features = true +hard_tabs = false +max_width = 100 +edition = "2021" +use_field_init_shorthand = true