Skip to content

Instantly share code, notes, and snippets.

@thomasantony
Last active March 18, 2022 20:35
Show Gist options
  • Save thomasantony/61acff95a6abc2cd8d4bfe14c038b5b6 to your computer and use it in GitHub Desktop.
Save thomasantony/61acff95a6abc2cd8d4bfe14c038b5b6 to your computer and use it in GitHub Desktop.
A script for setting up a windows cross-compiler toolchain on WSL or Linux that works for ffi crates built using `cc`. Made specifically for building orbiter addons in Rust on Linux.
#!/bin/sh
# Build and install llvm from source
wget https://github.com/llvm/llvm-project/archive/refs/heads/main.zip
unzip llvm-project-main.zip
rm llvm-project-main.zip
cd llvm-project-main
# cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS="clang;lld"
cmake -S llvm -B build -DLLVM_DEFAULT_TARGET_TRIPLE="i686-pc-windows-msvc" -DLLVM_ENABLE_PROJECTS="clang;lld"
cd build
make -j32
sudo make install
sudo mkdir /opt/xwin
sudo chown $USER:staff /opt/xwin
mkdir -p /opt/xwin/x86 /opt/xwin/x64 /opt/xwin/bin
# Build xwin from source with patch in this Gist
xwin --accept-license 1 --arch=x86 splat --output=/opt/xwin/x86 --disable-symlinks
xwin --accept-license 1 --arch=x86_64 splat --output=/opt/xwin/x64 --disable-symlinks
brew install wine-stable
rustup target add x86_64-pc-windows-msvc
rustup target add x86-pc-windows-msvc
# Create wrappers in /opt/xwin/bin for i686_clang, i686_ld and x64_ld
# point cargo config to that
cat << EOF >> ~/.cargo/config
[target.i686-pc-windows-msvc]
linker = "/opt/xwin/bin/i686_ld"
EOF
# Use environment variables to set CC and CXX
#!/bin/sh
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 14
rm llvm.sh
rm /usr/bin/lld-14
rm /usr/bin/clang-14
rm /usr/bin/llvm-ar-14
rm /usr/bin/lld-link-14
ln -s /usr/bin/lld-14 /usr/bin/ld.lld
ln -s /usr/bin/clang-14 /usr/bin/clang-cl
ln -s /usr/bin/llvm-ar-14 /usr/bin/llvm-lib
ln -s /usr/bin/lld-link-14 /usr/bin/lld-link
cargo install xwin
xwin --accept-license 1 --arch=x86 splat --output=/xwin
# xwin --accept-license 1 --arch=x86_64 splat --output=/xwin64
mkdir -p /xwin/bin
cat << EOF > /xwin/bin/i686_clang
#!/bin/sh
SUPPRESS_WARNINGS="-Wno-ignored-qualifiers -Wno-unused-parameter -Wno-unused-parameter -Wno-unused-command-line-argument -Wno-gnu-anonymous-struct -Wno-nonportable-system-include-path -Wno-reserved-macro-identifier -Wno-non-virtual-dtor -Wno-c++98-compat-pedantic -Wno-comma -Wno-suggest-override -Wno-unknown-argument -Wno-undefined-func-template -Wno-unused-macros -Wno-missing-prototypes -Wno-dollar-in-identifier-extension -Wno-c++98-compat -Wno-cast-qual -Wno-date-time -Wno-delete-non-abstract-non-virtual-dtor -Wno-documentation -Wno-documentation-deprecated-sync -Wno-documentation-unknown-command -Wno-extra-semi -Wno-format-security -Wno-gnu-anonymous-struct -Wno-ignored-qualifiers -Wno-missing-braces -Wno-nested-anon-types -Wno-newline-eof -Wno-nonportable-system-include-path -Wno-old-style-cast -Wno-overloaded-virtual -Wno-reserved-identifier -Wno-shadow-field -Wno-shadow-field-in-constructor -Wno-sign-conversion -Wno-suggest-destructor-override -Wno-unused-parameter -Wno-writable-strings -Wno-zero-as-null-pointer-constant"
clang-cl -EHa ${SUPPRESS_WARNINGS} -std=c++11 -fuse-ld=lld-link -imsvc/xwin/crt/include -imsvc/xwin/sdk/include/ucrt -imsvc/xwin/sdk/include/um -imsvc/xwin/sdk/include/shared "$@"
EOF
cat << EOF > /xwin/bin/i686_ld
#!/bin/sh
/usr/bin/lld-link -libpath:/xwin/sdk/lib/um/x86 -libpath:/xwin/sdk/lib/ucrt/x86 -libpath:/xwin/crt/lib/x86 "$@"
EOF
chmod +x /xwin/bin/*
cat << EOF >> ~/.cargo/config
[target.i686-pc-windows-msvc]
linker = "/xwin/bin/i686_ld"
EOF
diff --git a/src/splat.rs b/src/splat.rs
index 393250b..8a4ba9c 100644
--- a/src/splat.rs
+++ b/src/splat.rs
@@ -477,7 +477,8 @@ pub(crate) fn splat(
#[inline]
fn symlink(original: &str, link: &Path) -> Result<(), Error> {
std::os::unix::fs::symlink(original, link)
- .with_context(|| format!("unable to symlink from {} to {}", link, original))
+ .with_context(|| format!("unable to symlink from {} to {}", link, original)).ok();
+ Ok(())
}
pub(crate) fn finalize_splat(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment