cjdelisle / packetcrypt_rs

PacketCrypt in Rust
53 stars 43 forks source link

Windows build fails #37

Open Frank-Buss opened 2 years ago

Frank-Buss commented 2 years ago

When I try to compile it on Windows, I get the following error:

   Compiling serde_urlencoded v0.6.1
   Compiling toml v0.5.8
The following warnings were emitted during compilation:

warning: march=native is enabled, this build is non-portable
warning: cl : Befehlszeile error D8021 : Ung�ltiges numerisches Argument /Wno-implicit-function-declaration.

error: failed to run custom build command for `packetcrypt-sys v0.4.0 (W:\projects\pkt.cash\packetcrypt_rs\packetcrypt-sys)`

Caused by:
  process didn't exit successfully: `W:\projects\pkt.cash\packetcrypt_rs\target\release\build\packetcrypt-sys-a62cb9e478073187\build-script-build` (exit code: 1)
  --- stderr

  error occurred: Command "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.30.30705\\bin\\HostX64\\x64\\cl.exe" "-nologo" "-MD" "-O2" "-Brepro" "-I" "W:\\projects\\pkt.cash\\packetcrypt_rs\\target\\release\\build\\libsodium-sys-df89fd4f4518355c\\out\\source\\libsodium\\src\\libsodium\\include" "-I" "packetcrypt/include" "-I" "packetcrypt/src" "-W4" "-Wno-implicit-function-declaration" "-O2" "-FoW:\\projects\\pkt.cash\\packetcrypt_rs\\target\\release\\build\\packetcrypt-sys-e959eb6f756871c5\\out\\lib\\packetcrypt/src/Validate.o" "-c" "packetcrypt/src/Validate.c" with args "cl.exe" did not execute successfully (status code exit code: 2).

warning: build failed, waiting for other jobs to finish...
error: build failed

Tested with Visual Studio Community 2019 and 2022.

Frank-Buss commented 2 years ago

One problem with Visual Studio is this line: https://github.com/cjdelisle/packetcrypt_rs/blob/9cce37edd78f94f8ddd8b451022b5b34b3a766ca/packetcrypt-sys/build.rs#L113 Another problem might be this line: https://github.com/cjdelisle/packetcrypt_rs/blob/9cce37edd78f94f8ddd8b451022b5b34b3a766ca/packetcrypt-sys/build.rs#L113 but it still uses "-O2" from somewhere when deleted.

But I managed to cross-compile it from Linux now. Prerequisite (on Debian) : apt install mingw-w64. Then install the Rust cross compilation tool, as described here: https://rust-lang.github.io/rustup/cross-compilation.html and finally you can build it for Windows on Linux like this:

cargo build --target x86_64-pc-windows-gnu --release
alexandervenezia commented 2 years ago

I was able to successfully compile the repo on my Windows system by switching to the GNU toolchain for rustup, which would second that the problem is with an interaction with MSVC. Anyone having issues might want to try these commands:

rustup toolchain install stable-x86_64-pc-windows-gnu rustup set default-host x86_64-pc-windows-gnu

Before attempting to compile as normal. Obviously, it would be ideal if this were not necessary and the default MSVC toolchain could be used, but it may be a functional workaround.

cjdelisle commented 2 years ago

The problem here is that there is C code in packetcrypt_rs and when we're on windows it's trying to use cl.exe to compile it. The language which cl.exe compiles is "not really C", it's Microsoft's flavor of C++, projects which support cl.exe are lined with ifdef _MSC_VER to adjust to this compiler's dialect, something which I would ideally like to keep out of this codebase.

However, new versions of microsoft visual studio do ship with the option of using the clang compiler, but last time I checked, the rust package cc, which manages the compiling, was not capable of finding the clang executable in order to use it.

Frank-Buss commented 2 years ago

The bug has nothing to do with how MSVC implements the C++ standard, but with wrong compiler options. And MSVC is pretty standard conform last time I checked, I don't expect many MSC_VER tests. Except for the Microsoft declspec mess for using DLLs, but this is just one place and a few defines for exported/imported functions, in case a DLL is used at all and it is not linked statically.

cjdelisle commented 2 years ago

Oh, interesting. I don't have a windows computer handy but if you can get it building with the right flags then we should be able to put it on github actions so that we're sure it doesn't break in the future. The build flags are in build.rs inside of packetcrypt-sys

Frank-Buss commented 2 years ago

I don't know if github actions support Visual Studio. But I managed to compile it now on a clean Windows 10 installation. Needs only msys2, probably this could be used with github actions. Here are the steps: https://github.com/cjdelisle/packetcrypt_rs/issues/39#issuecomment-999982652 I tested it in a Oracle Virtual Box VM, so should be possible for you to reproduce this (a Windows 10 Pro licence key on eBay costs about $20, and the ISO is officially available from the Microsoft webpage), and then add the instructions to the readme. Or maybe better a separate build-readme for Windows, because most people might just download the binary release.