ca110us / go-clamav

go wrapper for libclamav
GNU General Public License v2.0
33 stars 12 forks source link

Windows how to? #6

Open WTIGER001 opened 2 years ago

WTIGER001 commented 2 years ago

This a great wrapper for clamav. I especially liked the scripts / instructions provided and the example app. That helped me a lot. I was able to get my CLI and Server versions of the app working great for Linux and I used the static linking. Now I want to figure out how to provide the CLI on Windows 10 too... I know ClamAV supports windows 10 but I am sure the process (mainly the prepare.sh) would be way different. Do you have any idea how I would do this?

TerminalFi commented 2 years ago

It is going to be very different.

https://docs.clamav.net/manual/Installing/Installing-from-source-Windows.html

ca110us commented 2 years ago

Hi @WTIGER001 ,it is glad that this project was useful to you. But i'm sorry, I haven't tried static building for windows yet. Maybe you can refer to this blog: Cross-Compiling Golang (CGO) Projects

WTIGER001 commented 2 years ago

Ok.. thanks... it was a long shot..

On Fri, May 20, 2022, 4:42 AM ca110us @.***> wrote:

Hi @WTIGER001 https://github.com/WTIGER001 ,it is glad that this project was useful to you. But i'm sorry, I haven't tried static building for windows yet. Maybe you can refer to this blog: Cross-Compiling Golang (CGO) Projects https://dh1tw.de/2019/12/cross-compiling-golang-cgo-projects/

— Reply to this email directly, view it on GitHub https://github.com/ca110us/go-clamav/issues/6#issuecomment-1132640838, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABBXKIU7OVLIAZW4OVAISE3VK5F6HANCNFSM5WMAIVMA . You are receiving this because you were mentioned.Message ID: @.***>

marius-enlock commented 7 months ago

@WTIGER001 I have managed to do it, there are some tricky parts, but I linked the information for them in the comment; When installing the dependencies with vxpkg you will need to set xz from a mirror repository because it is disabled from the main repository due to the big exploit from 2024

I have used clamav 1.2.0 source; should be similar with 1.3.0

I have followed the guide from the clamav and managed to statically build the library on windows 10. You need Visual Studio 2022 (with C++ CMake extension from the installer); vcpkg; and other from the comments in my pseudo batch script

:: Guide: https://docs.clamav.net/manual/Installing/Installing-from-source-Windows.html#building-with-vcpkg

$env:VCPKG_ROOT="C:\Users\ThrowAway\Desktop\work\vcpkg"

$env:PATH="$env:VCPKG_ROOT;$env:PATH"

$env:VCPKG_DEFAULT_TRIPLET="x64-windows"

goto comment

:: got fixed after downloading from within Visual Studio Installer
:: 1st. Visual Studio Installer -> Desktop Development with C++ -> C++ CMake tools for Windows
:: 2nd. Visual Studio Installer -> Universal Windows: Platform build tools

:comment

C:\Users\ThrowAway\Desktop\work\vcpkg\vcpkg.exe install 'curl[openssl]' 'json-c' 'libxml2' 'pcre2' 'pthreads' 'zlib' 'pdcurses' 'bzip2' 'check'

:: Issue with xz will affect the above stuff https://github.com/microsoft/vcpkg/issues/37839

$env:PATH="C:\Program Files\CMake\bin\;$env:PATH"

:: Install rust and cargo https://doc.rust-lang.org/cargo/getting-started/installation.html

:: Install python3

$env:PATH="C:\Users\ThrowAway\AppData\Local\Microsoft\WindowsApps\python3;$env:PATH"

$env:PKG_CONFIG_PATH="C:\Users\ThrowAway\Desktop\work\vcpkg\installed\x64-windows\lib\pkgconfig"

:: Install pytest

python3 -m pip install --user pytest

:: Enable libclamav only because I can't seem to make libcurl work (needed for libfreshclam)

cmake .. -A x64 `
  -D CMAKE_TOOLCHAIN_FILE="C:\Users\ThrowAway\Desktop\work\vcpkg\scripts\buildsystems\vcpkg.cmake" `
  -D ENABLE_DEBUG=OFF `
  -D ENABLE_LIBCLAMAV_ONLY=ON `
  -D ENABLE_TESTS=OFF `
  -D ENABLE_EXAMPLES=OFF `
  -D ENABLE_STATIC_LIB=ON `
  -D ENABLE_SHARED_LIB=OFF `
  -D ENABLE_JSON_SHARED=OFF `
  -D ENABLE_UNRAR=ON `
  -D LIBCHECK_LIBRARY="C:\Users\ThrowAway\Desktop\work\vcpkg\installed\x64-windows\lib\checkDynamic.lib" `
  -D JSONC_LIBRARY="C:\Users\ThrowAway\Desktop\work\vcpkg\installed\x64-windows\lib\json-c.lib" `
  -D CURL_LIBRARY="C:\Users\ThrowAway\Desktop\work\vcpkg\installed\x64-windows\lib\libcurl.lib" `
  -D CURL_INCLUDE_DIR="C:\Users\ThrowAway\Desktop\work\vcpkg\installed\x64-windows\include\curl" `
  -D CMAKE_INSTALL_PREFIX="install"

cmake --build . --config RelWithDebInfo

cmake --build . --config RelWithDebInfo --target install
marius-enlock commented 7 months ago

Note that you will have to use CPATH instead of LD_LIBRARY_PATH to link the headers

CPATH=C:\Users\ThrowAway\Downloads\clamav-1.2.0.tar\clamav-1.2.0\build\install\include;C:\Users\ThrowAway\Desktop\work\vcpkg\installed\x64-windows\include\openssl"  CGO_LDFLAGS="-LC:\Users\ThrowAway\Downloads\clamav-1.2.0.tar\clamav-1.2.0\build\install\ -lclamav_static -LC:\Users\ThrowAway\Desktop\work\vcpkg\installed\x64-windows\lib -lcrypto -ljson-c -lbz2 -lz -lxml2" go build .
marius-enlock commented 7 months ago

Another pain point to be aware of:

Make sure the gcc and ld used by cgo is the same used to build the library statically otherwise your program will fail to build throwing undefined references For example:

/tmp/go-build/cgo-gcc-prolog:55: undefined reference to `cl_strerror'

In my case when building libclamav I had to compile de dependencies and specify the target in the cmake flag with the following compiler and linker in order to get cgo to work

CMAKE_LINKER: C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/bin/ld.exe
CMAKE_CXX_COMPILER: C:/ProgramData/mingw64/mingw64/bin/gcc.exe

If you have multiple .a/.lib libraries linked you also need to compile and link them with the same gcc and ld as the one cgo is using