VirusTotal / yara

The pattern matching swiss knife
https://virustotal.github.io/yara/
BSD 3-Clause "New" or "Revised" License
7.95k stars 1.42k forks source link

Building yara inside bazel project #2013

Closed santalvarez closed 7 months ago

santalvarez commented 7 months ago

Hi, I have a project where I want to use the yara library but im having trouble building it.

This is how I have my project set up:

WORKSPACE file

...
git_repository(
    name = "com_github_virustotal_yara",
    remote = "https://github.com/VirusTotal/yara",
    commit = "d1ff3ecc465d7ed882943fd1278dd8981b704f02", # v4.3.2
)

load("@com_github_virustotal_yara//:bazel/yara_deps.bzl", "yara_deps")
yara_deps()
...

BUILD file

...
load("@com_github_virustotal_yara//:bazel/yara.bzl", "yara_library")
yara_library(
    name = "libyara",
    crypto_libs = ["@openssl//:crypto"],
    includes = ["@com_github_virustotal_yara//:libyara/include"],
    modules = [
        "hash",
        "macho",
        "tests",
        "string",
    ],
    modules_srcs = [
        "@com_github_virustotal_yara//:libyara/modules/hash/hash.c",
        "@com_github_virustotal_yara//:libyara/modules/macho/macho.c",
        "@com_github_virustotal_yara//:libyara/modules/tests/tests.c",
        "@com_github_virustotal_yara//:libyara/modules/string/string.c",
    ],
)
...

The problem comes when I try to build the libyara dependency and I get the errors shown below which indicate some problems with the visibility of the files. Is it possible to do this? Or is the current bazel config in yara only meant to be used to build the standalone yara lib. (Im a bazel noob so I apologize if Im not understanding something).

ERROR: /Users/santalvarez/Documents/mhunt/Sources/Agent/BUILD:30:13: in cc_library rule //Sources/Agent:libyara: target '@com_github_virustotal_yara//:libyara/modules/hash/hash.c' is not visible from target '//Sources/Agent:libyara'. Check the visibility declaration of the former target if you think the dependency is legitimate. To set the visibility of that source file target, use the exports_files() function
ERROR: /Users/santalvarez/Documents/mhunt/Sources/Agent/BUILD:30:13: in cc_library rule //Sources/Agent:libyara: target '@com_github_virustotal_yara//:libyara/modules/macho/macho.c' is not visible from target '//Sources/Agent:libyara'. Check the visibility declaration of the former target if you think the dependency is legitimate. To set the visibility of that source file target, use the exports_files() function
ERROR: /Users/santalvarez/Documents/mhunt/Sources/Agent/BUILD:30:13: in cc_library rule //Sources/Agent:libyara: target '@com_github_virustotal_yara//:libyara/modules/tests/tests.c' is not visible from target '//Sources/Agent:libyara'. Check the visibility declaration of the former target if you think the dependency is legitimate. To set the visibility of that source file target, use the exports_files() function
ERROR: /Users/santalvarez/Documents/mhunt/Sources/Agent/BUILD:30:13: in cc_library rule //Sources/Agent:libyara: target '@com_github_virustotal_yara//:libyara/modules/string/string.c' is not visible from target '//Sources/Agent:libyara'. Check the visibility declaration of the former target if you think the dependency is legitimate. To set the visibility of that source file target, use the exports_files() function
plusvic commented 7 months ago

This is an example BUILD file that we use ourselves in VirusTotal, the only difference with yours is the absense of the @com_github_virustotal_yara//: prefix in module source paths. So, you may try that.

load("@com_github_virustotal_yara//:bazel/yara.bzl", "yara_library")

yara_library(
    name = "libyara",
    modules = [
        # Standard open-source modules.
        "cuckoo",
        "elf",
        "hash",
        "magic",
        "math",
        "pe",
        "tests",
        "time",
        "dotnet",
    ],
    modules_srcs = [
        "libyara/modules/cuckoo/cuckoo.c",
        "libyara/modules/elf/elf.c",
        "libyara/modules/hash/hash.c",
        "libyara/modules/magic/magic.c",
        "libyara/modules/math/math.c",
        "libyara/modules/pe/pe.c",
        "libyara/modules/pe/pe_utils.c",
        "libyara/modules/tests/tests.c",
        "libyara/modules/time/time.c",
        "libyara/modules/dotnet/dotnet.c",
    ],
    deps = [
        "@jansson//:jansson",
        "@magic//:magic",
    ]
)
santalvarez commented 7 months ago

That works! Thanks