add Static build

This commit is contained in:
ca110us 2022-03-30 14:12:40 +08:00
parent 7c10d28065
commit 23c4b7a54c
5 changed files with 134 additions and 8 deletions

View File

@ -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) For other Linux distributions, see [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html)
## Quick Start ## Quick Start
### Dynamic linking
```bash ```bash
$ cd example && cat main.go $ cd example && cat main.go
``` ```
@ -89,12 +90,32 @@ func main() {
``` ```
```bash ```bash
$ go run main.go $ CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go
db load succeed: 9263 db load succeed: 9263
209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected 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 ## Reference
[mirtchovski/clamav](https://github.com/mirtchovski/clamav) [mirtchovski/clamav](https://github.com/mirtchovski/clamav)

View File

@ -34,6 +34,7 @@ sudo cmake --build . --target install
其他 Linux 发行版参照 [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html) 其他 Linux 发行版参照 [clamav documentation](https://docs.clamav.net/manual/Installing/Installing-from-source-Unix.html)
## 快速开始 ## 快速开始
### 动态链接
```bash ```bash
$ cd example && cat main.go $ cd example && cat main.go
``` ```
@ -89,12 +90,31 @@ func main() {
``` ```
```bash ```bash
$ go run main.go $ CGO_LDFLAGS="-L/usr/local/lib -lclamav" go run main.go
db load succeed: 9263 db load succeed: 9263
209 YARA.Unix_Packer_UpxDetail.UNOFFICIAL Virus(es) detected 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) [mirtchovski/clamav](https://github.com/mirtchovski/clamav)

View File

@ -5,9 +5,6 @@
package goclamav package goclamav
/* /*
#cgo CFLAGS: -g -Wall
#cgo LDFLAGS: -L/usr/local/lib/ -lclamav
#include <clamav.h> #include <clamav.h>
#include <stdlib.h> #include <stdlib.h>
*/ */

View File

@ -1,9 +1,6 @@
package goclamav package goclamav
/* /*
#cgo CFLAGS: -g -Wall
#cgo LDFLAGS: -L/usr/local/lib/ -lclamav
#include <clamav.h> #include <clamav.h>
#include <stdlib.h> #include <stdlib.h>
*/ */

91
prepare.sh Normal file
View File

@ -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/