How can I use the $(location :label) variable in the defines attribute of a Bazel cc_binary rule?

  bazel, c++, windows

This is in Bazel for Windows.

I have a genrule that generates a data file (see Looking for examples of Bazel genrules that generate data files for more details) and then I have a cc_binary() rule that depends on it.

Then I wanted to define a macro with the location of the data file for the executable to pick it up. "data" is the name of my genrule, which builds successfully. I tried to use $(location :data) in the defines, but it errors with "variable location is not defined". I tried to work it around with $(BINDIR) and I end up getting an acceptable path, but for some reason, my program is not able to open the file.

Here is my current rule. I had to resort to an even uglier workaround to make it to work. Please see the comments in the defines attribute:

# loads data.dat via bazel run
cc_binary(
    name = "Packed",
    srcs = [
        "mainPacked.cpp",
    ],
    deps = [
        ":adventure",
    ],
    data = [":data"],
    local_defines = [
# works, but it's ugly
        "DATA_FILE=../../../../../TheBlackDarkSanctum/data.dat",
# doesn't work, but shows a 'correct' filename: file bazel-out/x64_windows-fastbuild/bin/TheBlackDarkSanctum/data.dat
#       "DATA_FILE=$(BINDIR)/TheBlackDarkSanctum/data.dat",
# variable location is not defined
#       "DATA_FILE=$(location :data)",
    ],
)

Source: Windows Questions

LEAVE A COMMENT