Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows Cross-Compilation Support #5

Closed
wingman-jr-addon opened this issue Feb 24, 2022 · 9 comments
Closed

Windows Cross-Compilation Support #5

wingman-jr-addon opened this issue Feb 24, 2022 · 9 comments

Comments

@wingman-jr-addon
Copy link

Thanks for the recent work that made mingw-w64 successfully cross-compile the code. Only one minor issue seems to remain: the .exe extension is appended to the sz executable, which fails on make install.
Perhaps this line should somehow account for the difference in the binary name?

install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${EXE} TYPE BIN)

Manually editing the configured cmake output to append .exe then caused the install to complete without issue.

@ayzk
Copy link
Collaborator

ayzk commented Feb 25, 2022

I made an update to suppress CMake adding the suffix of '.exe' in Windows. Could you try it and let me know? Thanks.

@wingman-jr-addon
Copy link
Author

Thanks @ayzk - I gave it a try, but for some reason the file is still named test/sz.exe in the build folder.
In general, if you remove the .exe extension though, I think Windows might not run and treat it like a normal executable. Windows tends to rely on the extension more heavily for these things as even e.g. .exe vs. .com will have subtle differences in operation.

Additionally, I saw the commit message indicated this was a fix for Win32 - this is actually a Win64 build as I believe Win32 had some other issues during compilation so I assumed it was likely not supported (and not a big deal).

I'm not sure there's much for logs, but here are a few snippets.

Scanning dependencies of target sz
[ 97%] Building CXX object test/CMakeFiles/sz.dir/sz.cpp.obj
[100%] Linking CXX executable sz
[100%] Built target sz
[ 94%] Built target zstd
[ 97%] Linking CXX executable sz
[100%] Built target sz
Install the project...

and

-- Installing: XXX/sz3/build/win/x64/../../../install/win/x64/include/zstd_errors.h
CMake Error at test/cmake_install.cmake:36 (file):
  file INSTALL cannot find
  "XXX/sz3/build/win/x64/test/sz": No such file or
  directory.
Call Stack (most recent call first):
  cmake_install.cmake:68 (include)


make: *** [Makefile:129: install] Error 1

Again, thanks for your work!

@wingman-jr-addon
Copy link
Author

So unfortunately I learned the hard way that SZ 2 does not work on Windows, see szcompressor/SZ#58
I'm not sure how similar the code bases are, but if SZ 3 shares the same issues, it's maybe better to just close this issue out as one that won't get fixed.

@ayzk
Copy link
Collaborator

ayzk commented Apr 9, 2022

@wingman-jr-addon I made some changes for SZ3 and it can produce correct results on Windows. Could you confirm if you can reproduce my results?
Steps:

  1. Install MSYS2 and Cmake on Windows. Make sure to install the Mingw-w64 gcc and g++ following the MSYS2 instructions.
  2. Build the project by cmake .. -G "MinGW Makefiles" and mingw32-make
  3. run the sz executable.

@wingman-jr-addon
Copy link
Author

@ayzk Great work! Looks like things worked out fairly well.

My approach had been using WSL with MinGW. I tried the MSYS2 approach, and found that the steps above seemed to work all right. I see the main page is pointing here so I'll chronicle my experiences.

For others trying this out:

pacman -S mingw-w64-x86_64-cmake
(restart mingw msys terminal)
mkdir -p build/sz3/win/x64
cd build/sz3/win/x64
cmake your/path/to/SZ3 "-DCMAKE_INSTALL_PREFIX:PATH=../../../../install/sz3/win/x64" -G "MinGW Makefiles"
mingw64-make.exe
mingw64-make.exe install

I wasn't sure the best way to verify at this point. I thought perhaps:

mingw64-make.exe test

But it did not find any tests. Maybe a note here would help others?

My end goal, however, was to include this into a Visual Studio solution. Since I had most of the infrastructure there, I went back to the WSL plus MinGW approach from before and got it working with a couple minor fixups.

CMake toolchain file:

# The name of the target operating system
set(CMAKE_SYSTEM_NAME Windows)

# which compilers to use for C and C++
set(CMAKE_C_COMPILER   x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)

# where is the target environment located
set(CMAKE_FIND_ROOT_PATH  /usr/x86_64-w64-mingw32/)

# adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

When targeting C++17, Visual Studio produced a few warnings/errors I needed to suppress.

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4146)
#pragma warning(disable:4700)
#pragma warning(disable:4703)

I also needed to provide a shim for the small usages of clock_gettime - @ayzk do you think those would be easy to replace with something from std::chrono?

After this, I was to able to test that at least Linux compression -> Linux decompression and Windows compression -> Windows decompression worked. I have not tried yet e.g. Linux compression -> Windows decompression.

Thanks for the hard work!

@ayzk
Copy link
Collaborator

ayzk commented Apr 14, 2022

@wingman-jr-addon Thank you very much for the instruction for WSL.
I've replaced clock_gettime with std::chrono.

@wingman-jr-addon wingman-jr-addon changed the title Cross-compilation - .exe suffix not handled in make install Windows Cross-Compilation Support Apr 15, 2022
@vasole
Copy link
Contributor

vasole commented Dec 9, 2022

@wingman-jr-addon

My end goal, however, was to include this into a Visual Studio solution

Current master seems to properly generate a Visual Studio solution

mkdir build
cd build
cmake ..\ -G"Visual Studio 16 2019" (or the default detected should work)

@wingman-jr-addon
Copy link
Author

Sorry for the lengthy delay in responding; I was able to return at last to this project.
I can confirm that your changes to std::chrono allowed me to get rid of clock_gettime @ayzk , and cross-compilation went off without a hitch! Thanks!
@vasole Thanks for the comment.

Think we should be good to close now.

@legendPerceptor
Copy link

2. cmake .. -G "MinGW Makefiles"

I found it not working in Windows MSYS2 MinGW64 env, but it worked in the MSYS2 UCRT64 environment.

After installing the MSYS2, open the UCRT64 terminal. Install the compiler and cmake by the following commands.

pacman -S mingw-w64-ucrt-x86_64-gcc
pacman -S mingw-w64-ucrt-x86_64-cmake

And then it just worked without -G "MinGW".

Go into the sz3 folder and run

mkdir windows-build && cd windows-build
cmake ..
cmake --build . -j 18

The sz3.exe will be successfully built.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants