Bazel: Implicit include of generated files

Created on 27 Feb 2018  Β·  9Comments  Β·  Source: bazelbuild/bazel

Description of the problem / feature request:

If we make cc_inc_library() out of generated files, bazel can not properly understand that these files are _described_.

generated/BUILD:

package(default_visibility = ["//visibility:public"])

genrule(
    name = "generate-header",
    tools = ["generate-header.py"],
    outs = ["header.h"],
    cmd = "python $(location generate-header.py) $@"
)

cc_inc_library(
    name = "include",
    hdrs = ["header.h"]
)

BUILD:

package(default_visibility = ["//visibility:public"])

cc_binary(
    name = "hello",
    srcs = ["main.cpp"],
    deps = ["//generated:include"]
)

main.cpp:

#include <iostream>

#include <generated/header.h>


int main()
{
    std::cout << "Hello, world!\n";
    return 0;
}

That produces the following error:

C:\Users\Chebotarev_V\Documents\src\bazel-issues\implicit-include-of-generated-headers>bazel.exe build ...
INFO: Analysed 3 targets (3 packages loaded).
INFO: Found 3 targets...
ERROR: C:/users/chebotarev_v/documents/src/bazel-issues/implicit-include-of-generated-headers/BUILD:3:1: undeclared inclusion(s) in rule '//:hello':
this rule is missing dependency declarations for the following files included by 'main.cpp':
  'bazel-out/x64_windows-fastbuild/genfiles/generated/header.h'
INFO: Elapsed time: 1.868s, Critical Path: 0.53s
FAILED: Build did NOT complete successfully

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

Please find an example of this issue in my repository:
https://github.com/excitoon/bazel-issues/tree/master/implicit-include-of-generated-headers

What operating system are you running Bazel on?

Windows 10 x64

What's the output of bazel info release?

Build label: 0.11.0
Build target: bazel-out/x64_windows-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Thu Dec 9 18:32:10 +50117 (1519402213930)
Build timestamp: 1519402213930
Build timestamp as int: 1519402213930

Have you found anything relevant by searching the web?

Nothing.

bug

Most helpful comment

Friendly ping, can this issue be closed? Can you use cc_library for your use case and the bug is not present with cc_library? Note that since https://github.com/bazelbuild/bazel/commit/5494ce4d63660f0c958a56c9cf6a5a8836967a97 cc_inc_library is not provided by bazel.

All 9 comments

cc_inc_library shouldn't be used, it covers a very specific use case internally and is strictly less usable than cc_library. It was open sourced accidentally. I revived the "lets delete cc_inc_library" thread.

Do you still observe the same problem with cc_library?

Thank you, it works fine with cc_library. Am I right that functionality of cc_inc_library()/@prefix matches strip_include_prefix attribute of cc_library()?

Unfortunately, cc_library() does not offer the same functionality.

It does not recreate its own prefix in include directory.

Real paths:

β”‚   β”œβ”€β”€β”€inc_library
β”‚   β”‚   β”‚   BUILD
β”‚   β”‚   β”‚   
β”‚   β”‚   └───v1
β”‚   β”‚           inc_library.h

cc_inc_library() + @prefix="v1":

└───hellolib
    └───inc_library
            inc_library.h

cc_library() + @strip_include_prefix="v1":

└───hellolib
        inc_library.h

It is also useful, but in alternative cases.

Basically, strip_include_prefix attribute does not work well with proposed way to include header files:

https://docs.bazel.build/versions/master/bazel-and-cpp.html#include-paths

Make all include paths relative to the workspace directory.

Use quoted includes (#include "foo/bar/baz.h") for non-system headers, not angle-brackets (#include <foo/bar/baz.h>)

But if you combine include_prefix with strip_include_prefix it works exactly the same right?

strip_include_prefix = "v1"
include_prefix = "inc_library"

and you should see:

└───hellolib
    └───inc_library
            inc_library.h

Thank you. That's right, I've just seen it. But with cc_library() and all attributes mentioned I can no longer access inc_library.h by full path:

#include <inc_library/inc_library.h> // From _virtual_includes
#include <inc_library/v1/inc_library.h> // From real path
ERROR: C:/users/chebotarev_v/documents/src/sandbox/includes/BUILD:3:1: undeclared inclusion(s) in rule '//includes:hello':
this rule is missing dependency declarations for the following files included by 'includes/main.cpp':
  'C:/users/chebotarev_v/documents/src/sandbox/inc_library/v1/inc_library.h'
Target //includes:hello failed to build

You can make it work by adding includes = [ "." ], right?

Friendly ping, can this issue be closed? Can you use cc_library for your use case and the bug is not present with cc_library? Note that since https://github.com/bazelbuild/bazel/commit/5494ce4d63660f0c958a56c9cf6a5a8836967a97 cc_inc_library is not provided by bazel.

Yeah, I can. Thank you.

Was this page helpful?
0 / 5 - 0 ratings