Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang compile issues (bazel) #289

Open
mrheinen opened this issue Jan 15, 2025 · 6 comments
Open

Golang compile issues (bazel) #289

mrheinen opened this issue Jan 15, 2025 · 6 comments

Comments

@mrheinen
Copy link

This problem might very well be due to my misunderstanding on how to use yara-x with bazel so feel free to treat this with a low priority.

I installed using:
go get -u github.com/VirusTotal/yara-x/go

Now if I try to build a binary I get this error:

`bazel build //cmd/yara:yara
INFO: Analyzed target //cmd/yara:yara (0 packages loaded, 0 targets configured).
ERROR: /disk/store/coding/lophiid/cmd/yara/BUILD.bazel:11:10: GoLink cmd/yara/yara_/yara failed: (Exit 1): builder failed: error executing GoLink command (from target //cmd/yara:yara) bazel-out/k8-opt-exec-ST-d57f47055a04/bin/external/go_sdk/builder_reset/builder link -sdk external/go_sdk -installsuffix linux_amd64 -arc ... (remaining 47 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
external/go_sdk/pkg/tool/linux_amd64/link: running /usr/bin/gcc failed: exit status 1
/tmp/go-link-1884458403/000001.o:compiler.cgo2.c:function _cgo_1093191e5e44_Cfunc_yrx_buffer_destroy:(.text+0x8b): error: undefined reference to 'yrx_buffer_destroy'
/tmp/go-link-1884458403/000001.o:compiler.cgo2.c:function _cgo_1093191e5e44_Cfunc_yrx_compiler_add_source_with_origin:(.text+0xcd): error: undefined reference to 'yrx_compiler_add_source_with_origin'
/tmp/go-link-1884458403/000001.o:compiler.cgo2.c:function _cgo_1093191e5e44_Cfunc_yrx_compiler_ban_module:(.text+0x12e): error: undefined reference to 'yrx_compiler_ban_module'
...
...
...
`

Is there something I'm missing that needs to be done to make this work with bazel ? Any help is really appreciated!

@plusvic
Copy link
Member

plusvic commented Jan 15, 2025

Without seeing your whole setup is going to be hard to help you, but there are a few things you should take into account:

  1. The Golang library uses the YARA-X C library under the hood, when you build the Golang library using go build, the Golang toolchain uses pkg-config to determine the path where the header file (yara_x.h) and compiled library are located. That's why installing the C library with cargo cinstall -p yara-x-capi --release is pre-requisite before building the Golang library. This will put the header file and compiled library at the appropriate place, usually /usr/local/include and /usr/local/lib respectively.

  2. Using Bazel is a different story as the pkg-config mechanism doesn't work in this case. Bazel won't be able to find the header file and compiled library unless you explictily tell where to find those files, which seems to be the problem here.

@mrheinen
Copy link
Author

Thanks for the quick reply! I have spend a few hours to try and figure out how to do it with bazel but in the end I found that compiling this logic with "go build" was just much easier than spending more time. No ideal but anyway.

Maybe this could be solved by allowing a tag to be specified for building the library statically linked ? I'm using a few other cgo dependencies and none of them required any special config.

I think I saw other bazel users in other issues so I'm hoping they can chime in. Can we keep this open for a few days and then close it ?

Cheers!

@plusvic
Copy link
Member

plusvic commented Jan 16, 2025

The tag for building the library statically exists (static_link), but the process is not straightforward. See #257 (comment).

I also tried to build a Golang project that uses YARA-X using Bazel, and my conclusion was that the drawbacks are greater than the benefits. Bazel's support for Rust is still immature (or at least it was when I looked at it). I managed to build a simple Rust project using Bazel, but when you have third-party dependencies that are not pure-Rust (they link agains C libraries), you start having issues.

For this reason, building both the Golang and Rust code with Bazel was out question, and the alternative was building the YARA-X library using cargo, and use Bazel for building the Golang code, using the previously compiled YARA-X library and header files.

At the end I decided to discard Bazel and use cargo and go build.

@mrheinen
Copy link
Author

mrheinen commented Jan 16, 2025 via email

@plusvic
Copy link
Member

plusvic commented Jan 17, 2025

//third_party :), but no, YARA-X is not there yet.

I'll close the issue if you don't mind.

@mrheinen
Copy link
Author

Yes please close as I'm actually already finished with the yara-x integration in lophiid. The current implement made this work super smooth so thanks for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants