hillu / go-yara

Go bindings for YARA
BSD 2-Clause "Simplified" License
349 stars 113 forks source link

building simple example on linux fails #152

Open geraldstanje opened 4 days ago

geraldstanje commented 4 days ago

Hi,

i have get some error to run the simple example on linux using docker. how can i build it using golang for a static build?

also what is the Building without pkg-config used for?

here my dockerfile:

FROM golang:latest AS builder

RUN apt-get update && apt-get install -y ca-certificates
RUN apt-get install libyara-dev musl-tools -y

WORKDIR /home/contrib
COPY . .

#ENV CGO_CFLAGS="-I${YARA_SRC}/libyara/include"
#ENV CGO_LDFLAGS="-L${YARA_SRC}/libyara/.libs -lyara"

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=musl-gcc PKG_CONFIG_PATH=${PREFIX_LINUX_MUSL}/lib/pkgconfig go build -ldflags '-extldflags "-static"' -tags yara_static -o /home/contrib/contrib ./_examples/simple-yara

error:

[+] Building 2.1s (12/14)                                                                                                                               docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                                    0.0s
 => => transferring dockerfile: 861B                                                                                                                                    0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                                                                        0.4s
 => [internal] load metadata for docker.io/library/golang:latest                                                                                                        0.4s
 => [internal] load .dockerignore                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                         0.0s
 => [builder 1/6] FROM docker.io/library/golang:latest@sha256:a66eda637829ce891e9cf61ff1ee0edf544e1f6c5b0e666c7310dce231a66f28                                          0.0s
 => [internal] load build context                                                                                                                                       0.0s
 => => transferring context: 654B                                                                                                                                       0.0s
 => CACHED [stage-1 1/3] FROM docker.io/library/ubuntu:latest@sha256:2e863c44b718727c860746568e1d54afd13b2fa71b160f5cd9058fc436217b30                                   0.0s
 => CACHED [builder 2/6] RUN apt-get update && apt-get install -y ca-certificates                                                                                       0.0s
 => CACHED [builder 3/6] RUN apt-get install libyara-dev musl-tools -y                                                                                                  0.0s
 => CACHED [builder 4/6] WORKDIR /home/contrib                                                                                                                          0.0s
 => CACHED [builder 5/6] COPY . .                                                                                                                                       0.0s
 => ERROR [builder 6/6] RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=musl-gcc PKG_CONFIG_PATH=${PREFIX_LINUX_MUSL}/lib/pkgconfig go build -ldflags '-extldflags "-stat  1.7s
------                                                                                                                                                                       
 > [builder 6/6] RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=musl-gcc PKG_CONFIG_PATH=${PREFIX_LINUX_MUSL}/lib/pkgconfig go build -ldflags '-extldflags "-static"' -tags yara_static -o /home/contrib/contrib ./_examples/simple-yara:                                                                                                                   
0.086 go: downloading github.com/hillu/go-yara/v4 v4.3.2
1.281 # runtime/cgo
1.281 cc1: error: unrecognized command-line option '-m64'
------
Dockerfile:12
--------------------
  10 |     #ENV CGO_LDFLAGS="-L${YARA_SRC}/libyara/.libs -lyara"
  11 |     
  12 | >>> RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=musl-gcc PKG_CONFIG_PATH=${PREFIX_LINUX_MUSL}/lib/pkgconfig go build -ldflags '-extldflags "-static"' -tags yara_static -o /home/contrib/contrib ./_examples/simple-yara
  13 |     
  14 |     FROM ubuntu:latest
--------------------
ERROR: failed to solve: process "/bin/sh -c GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=musl-gcc PKG_CONFIG_PATH=${PREFIX_LINUX_MUSL}/lib/pkgconfig go build -ldflags '-extldflags \"-static\"' -tags yara_static -o /home/contrib/contrib ./_examples/simple-yara" did not complete successfully: exit code: 1
hillu commented 3 days ago

Can you run go build with parameter -x so that all program invocations are logged? I am not sure why musl-gcc is invoked with -m64. And I'm not sure why it doesn't understand that parameter. I am also not sure whether using musl-gcc to link libraries that have otherwise been built against glibc is supposed to work. In other words, I'm not sure whether I can help you on your path. What does work is building YARA from sources using musl-gcc as descibed in README.cross-building.md. Another option, since you are already using Docker, is to build using Alpine Linux or any other distribution that uses Musl as its standard libc.