hillu / go-yara

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

Module Install fails b/c pkg-config cannot find libcrypto which is needed by yara #111

Closed hop-along-polly closed 1 year ago

hop-along-polly commented 1 year ago

When I run go install github.com/hillu/go-yara or when I run my prototype app using this library (i.e. go run main.go) on my M1 Macbook I get this error.

# pkg-config --cflags  -- yara
Package libcrypto was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcrypto.pc'
to the PKG_CONFIG_PATH environment variable
Package 'libcrypto', required by 'yara', not found
pkg-config: exit status 1

To resolve this issue I have installed yara, openssl and pkg-config with homebrew.

brew install yara
brew install openssl
brew install pkg-config

I have also tried setting the PKG_CONFIG_PATH to look at the openssl version installed. According to this LinuxFAQ post brew install openssl should include libcrypto

PKG_CONFIG_PATH = $(which openssl) pkg-config --cflgas -- yara

I'm not sure if this is a yara issue, a pkg-config issue or something going on with the install of this go module. Any guidance would be greatly appreciated.

hop-along-polly commented 1 year ago

I resolved the issues with the install command by adding /opt/homebrew/opt/yara/lib/pkgconfig:/opt/homebrew/opt/openssl/lib/pkgconfig to the PKG_CONFIG_PATH environment variable.

PKG_CONFIG_PATH=/opt/homebrew/opt/yara/lib/pkgconfig:/opt/homebrew/opt/openssl/lib/pkgconfig go install github.com/hillu/go-yara/v4

You can get away with only adding /opt/homebrew to the PKG_CONFIG_PATH as well which begs the question why doesn't pkg-config look there already?

NOTE: when running my code I'm now getting an error which seems to be a platform related.

/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: -no_pie is deprecated when targeting new OS versions
ld: warning: ignoring file /opt/homebrew/Cellar/yara/4.2.3/lib/libyara.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
hillu commented 1 year ago

I don't own a Mac, so I can't confirm or test anything.

The linker complaining about files that are not for the correct architecture seems normal.

Is your last error message complete? Seems that there ought to be some more information after "x86_64:".

hop-along-polly commented 1 year ago

@hillu Thanks for the response. I actually figured out this second error as well.

Turns out the error regarding undefined symbols was a result of homebrew installing the amd64 toolchain for go instead of the arm64 toolchain which is correct for an M1 Macbook.

Here's the steps I took to fix this.

  1. Check which version of go is installed go env | grep GOARCH. In my case I got back GOARCH=amd64 which is not correct
  2. If the GOARCH is incorrect you need to uninstall go. See official instructions here
    sudo rm -rf /usr/local/go
    sudo rm /etc/paths.d/go
  3. Download and install the correct version of go from the official download page (and don't use homebrew b/c it assumes you want the amd64 version)

CONCLUSION To resolve both errors i've mentioned in this issue.

  1. Install the correct version of go for my systems architecture
  2. Install yara, pkg-config and openssl through homebrew
    brew install yara
    brew install pkg-config
    brew install openssl
  3. Use this command during the go install step
    PKG_CONFIG_PATH=/opt/homebrew/opt/yara/lib/pkgconfig:/opt/homebrew/opt/openssl/lib/pkgconfig go install github.com/hillu/go-yara/v4

NOTE: Go tries to use github.com/hillu/go-yara as the import but I wanted github.com/hillu/go-yara/v4 since I was using yara 4.2. Just be aware of that because it can make it seem like the libcrypto error never got resolved.