Bazel: Cannot define string literals in cc_library

Created on 25 Feb 2020  Â·  3Comments  Â·  Source: bazelbuild/bazel

ATTENTION! Please read and follow:

  • if this is a _question_ about how to build / test / query / deploy using Bazel, or a _discussion starter_, send it to [email protected]
  • if this is a _bug_ or _feature request_, fill the form below as best as you can.

Description of the problem / feature request:

Cannot #define a variable as a string literal.

Feature requests: what underlying problem are you trying to solve with this feature?

Replace this line with your answer.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

BUILD:

cc_binary(
    name = "hello-world",
    srcs = ["main.cc"],
    defines = ["FOO=\"BAR\""],
    # defines = ['FOO="BAR"'],
    # defines = ['FOO=\"BAR\"'],
    # defines = [r'FOO="BAR"'],
)

main.cc:

#include <iostream>

// #define FOO "BAR"

int main() {
    #if defined(FOO)
        std::cout << "Foo = " << FOO << std::endl;
    #else
        std::cout << "Hello World!" << std::endl;
    #endif
}

This example should compile but any definition above causes the double quotes to disappear, which makes the above code look like BAR is an undeclared variable.

What operating system are you running Bazel on?

MacOS 10.15

What's the output of bazel info release?

release 2.0.0-homebrew

If bazel info release returns "development version" or "(@non-git)", tell us how you built Bazel.

NA (homebrew)

What's the output of git remote get-url origin ; git rev-parse master ; git rev-parse HEAD ?

Replace this line with your answer.

Have you found anything relevant by searching the web?

https://www.reddit.com/r/bazel/comments/dslohm/cc_library_local_defines_possible_to_escape_for/

Any other information, logs, or outputs that you want to share?

Replace these lines with your answer.

If the files are large, upload as attachment or provide link.

P2 team-Rules-CPP bug

All 3 comments

The following should work in your build rule:

defines = ["FOO=\\\"BAR\\\""]
defines = ['FOO=\\"BAR\\"']

You're subject to two rounds of tokenization; parsing of the BUILD file and then Bourne shell's command line parser. You need the literal FOO=\"BAR\" to reach the command line for the compiler, hence the double escaping.

Doh! Such a simple fix. Thank you!

I would argue that backslash escaping is a hack around rather than a full
solution. We should not have to assume Bourne shell parsing. That is *nix
centric.

A complete solution would be to let the user write
defines=["var=value that may have spaces"]

The CC toolchain would do whatever quoting is needed to get the correct arg
passed to whatever compiler we happen to be using

On Thu, Mar 5, 2020, 6:59 PM Harsh Mohan notifications@github.com wrote:

Doh! Such a simple fix. Thank you!

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/bazelbuild/bazel/issues/10859?email_source=notifications&email_token=AAXHHHCHJI5PUQ7DUJXLCADRGA4EVA5CNFSM4K3CAXZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEN7KNPY#issuecomment-595502783,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAXHHHFU4NXAKI6GUHR3ORLRGA4EVANCNFSM4K3CAXZA
.

Was this page helpful?
0 / 5 - 0 ratings