From 23c4b7a54cf3774e8a9b5c7d61b83e0c9335951f Mon Sep 17 00:00:00 2001 From: ca110us Date: Wed, 30 Mar 2022 14:12:40 +0800 Subject: [PATCH] add Static build --- README.md | 23 ++++++++++++- README_CN.md | 22 ++++++++++++- clamav.go | 3 -- define.go | 3 -- prepare.sh | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 prepare.sh diff --git a/README.md b/README.md index caf2b99..934d88e 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ sudo cmake --build . --target install For other Linux distributions, see [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html) ## Quick Start +### Dynamic linking ```bash $ cd example && cat main.go ``` @@ -89,12 +90,32 @@ func main() { ``` ```bash -$ go run main.go +$ CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go db load succeed: 9263 209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected ``` +If the `libclamav.so` file is not found, try it: + +```bash +$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go + +db load succeed: 9263 +209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected +``` + +### Static build +```bash +sudo bash ./prepare.sh + +export CGO_CFLAGS="-g -Wall -I./clamav-mussels-cookbook/mussels/install/include" +export CGO_LDFLAGS="-L./clamav-mussels-cookbook/mussels/install/lib -lclamav_static -lbz2_static -lclammspack_static -lclamunrar_iface_static -lclamunrar_static -lcrypto -ljson-c -lpcre2-8 -lpcre2-posix -lssl -lxml2 -lz -lm -ldl -lstdc++" + +CGO_ENABLED=1 go build --ldflags '--extldflags "-static -fpic"' main.go +``` + + ## Reference [mirtchovski/clamav](https://github.com/mirtchovski/clamav) diff --git a/README_CN.md b/README_CN.md index aeec100..0cc559b 100644 --- a/README_CN.md +++ b/README_CN.md @@ -34,6 +34,7 @@ sudo cmake --build . --target install 其他 Linux 发行版参照 [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html) ## 快速开始 +### 动态链接 ```bash $ cd example && cat main.go ``` @@ -89,12 +90,31 @@ func main() { ``` ```bash -$ go run main.go +$ CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go db load succeed: 9263 209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected ``` +如果找不到 `libclamav.so` 文件,尝试如下: + +```bash +$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go + +db load succeed: 9263 +209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected +``` + +### 静态编译 +```bash +sudo bash ./prepare.sh + +export CGO_CFLAGS="-g -Wall -I./clamav-mussels-cookbook/mussels/install/include" +export CGO_LDFLAGS="-L./clamav-mussels-cookbook/mussels/install/lib -lclamav_static -lbz2_static -lclammspack_static -lclamunrar_iface_static -lclamunrar_static -lcrypto -ljson-c -lpcre2-8 -lpcre2-posix -lssl -lxml2 -lz -lm -ldl -lstdc++" + +CGO_ENABLED=1 go build --ldflags '--extldflags "-static -fpic"' main.go +``` + ## 参考 [mirtchovski/clamav](https://github.com/mirtchovski/clamav) diff --git a/clamav.go b/clamav.go index 48125b7..98d440d 100644 --- a/clamav.go +++ b/clamav.go @@ -5,9 +5,6 @@ package goclamav /* -#cgo CFLAGS: -g -Wall -#cgo LDFLAGS: -L/usr/local/lib/ -lclamav - #include #include */ diff --git a/define.go b/define.go index 9ccf071..c99a295 100644 --- a/define.go +++ b/define.go @@ -1,9 +1,6 @@ package goclamav /* -#cgo CFLAGS: -g -Wall -#cgo LDFLAGS: -L/usr/local/lib/ -lclamav - #include #include */ diff --git a/prepare.sh b/prepare.sh new file mode 100644 index 0000000..6f0b7e4 --- /dev/null +++ b/prepare.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +# make static lib for clamav deps +apt-get update +apt-get install -y python3-pip +apt-get install -y build-essential clang llvm +apt-get install -y libstdc++6 libstdc++-6-dev +apt-get install -y flex bison python3-dev pkg-config ninja-build +python3 -m pip install mussels + +wget https://cmake.org/files/v3.21/cmake-3.21.5-linux-x86_64.tar.gz +tar -zxvf cmake-3.21.5-linux-x86_64.tar.gz -C /usr/local/ +ln -s /usr/local/cmake-3.21.5-linux-x86_64/bin/cmake /usr/bin/cmake +rm -rf cmake-3.21.5-linux-x86_64.tar.gz + +git clone --depth 1 https://github.com/ca110us/clamav-mussels-cookbook.git +cd clamav-mussels-cookbook +rm -rf mussels/* &> /dev/null +mkdir mussels &> /dev/null +msl build libclamav_deps -t host-static -w mussels/work -i mussels/install +cd - + +# make get clamav source code +git clone https://github.com/Cisco-Talos/clamav.git +cd clamav +git checkout clamav-0.104.0 +cd - + + +# libclamav +cd clamav + +rm -rf ./build/* &> /dev/null +mkdir build &> /dev/null +cd - +export CLAMAV_DEPENDENCIES="$(pwd)/clamav-mussels-cookbook/mussels/install/" + +cd clamav/build + +cmake .. -G Ninja \ + -DCMAKE_BUILD_TYPE="Release" \ + -DJSONC_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include/json-c" \ + -DJSONC_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libjson-c.a" \ + -DBZIP2_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include" \ + -DBZIP2_LIBRARY_RELEASE="$CLAMAV_DEPENDENCIES/lib/libbz2_static.a" \ + -DOPENSSL_ROOT_DIR="$CLAMAV_DEPENDENCIES" \ + -DOPENSSL_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include" \ + -DOPENSSL_CRYPTO_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libcrypto.a" \ + -DOPENSSL_SSL_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libssl.a" \ + -DLIBXML2_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include/libxml2" \ + -DLIBXML2_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libxml2.a" \ + -DPCRE2_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include" \ + -DPCRE2_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libpcre2-8.a" \ + -DZLIB_INCLUDE_DIR="$CLAMAV_DEPENDENCIES/include" \ + -DZLIB_LIBRARY="$CLAMAV_DEPENDENCIES/lib/libz.a" \ + -DENABLE_JSON_SHARED=OFF \ + -DENABLE_STATIC_LIB=ON \ + -DENABLE_SYSTEMD=OFF \ + -DENABLE_TESTS=OFF \ + -DENABLE_LIBCLAMAV_ONLY=ON \ + -DENABLE_UNRAR=ON \ + -DENABLE_SHARED_LIB=OFF \ + -DDATABASE_DIRECTORY=/var/lib/clamav \ + -DCMAKE_INSTALL_PREFIX=install + +cmake --build . + +cd - + +rm -rf ./lib/* +mkdir lib &> /dev/null +cp clamav/build/libclamav/libclamav_static.a ./lib +cp clamav/build/libclammspack/libclammspack_static.a ./lib +cp clamav/build/libclamunrar/libclamunrar_static.a ./lib +cp clamav/build/libclamunrar_iface/libclamunrar_iface_static.a ./lib + +cp "$CLAMAV_DEPENDENCIES/lib/libbz2_static.a" ./lib +cp "$CLAMAV_DEPENDENCIES/lib/libjson-c.a" ./lib +cp "$CLAMAV_DEPENDENCIES/lib/libcrypto.a" ./lib +cp "$CLAMAV_DEPENDENCIES/lib/libssl.a" ./lib +cp "$CLAMAV_DEPENDENCIES/lib/libxml2.a" ./lib +cp "$CLAMAV_DEPENDENCIES/lib/libpcre2-8.a" ./lib +cp "$CLAMAV_DEPENDENCIES/lib/libz.a" ./lib + +rm -rf ./include/* +mkdir include &> /dev/null +cp clamav/build/*.h ./include +cp clamav/libclamav/clamav.h ./include + +cp ./include/* clamav-mussels-cookbook/mussels/install/include/ +cp ./lib/* clamav-mussels-cookbook/mussels/install/lib/ \ No newline at end of file