tree: 475aae8d1b8f96c3f23065079907db2d3ff1aaf1 [path history] [tgz]
  1. proto/
  2. testdata/
  3. textproto/
  4. BUILD
  5. CommandLineUtils.cc
  6. CommandLineUtils.h
  7. CommandLineUtilsTest.cc
  8. cxx_details.cc
  9. cxx_details.h
  10. cxx_extractor.cc
  11. cxx_extractor.h
  12. cxx_extractor_bazel_main.cc
  13. cxx_extractor_main.cc
  14. cxx_extractor_test.cc
  15. index_pack.cc
  16. index_pack.h
  17. index_pack_test.cc
  18. language.cc
  19. language.h
  20. objc_bazel_support.cc
  21. objc_bazel_support.h
  22. objc_bazel_support_test.cc
  23. objc_extractor_bazel_main.cc
  24. path_utils.cc
  25. path_utils.h
  26. README.md
kythe/cxx/extractor/README.md

Bazel C++ extractor

An extractor that builds index files from a Bazel-based project. Extractor builds a kindex file for each cc_library and cc_binary in the project. This extractor based on Bazel action_listener rule.

Usage

These instructions assume the kythe is installed in /opt/kythe. If not, follow installation instructions.

Currently Bazel doesn't have a convenient way to use action listener outside of a project. So first you need to add action_listener to the root BUILD file of the project.

# Extra action invokes /opt/kythe/extractors/bazel_cxx_extractor
extra_action(
    name = "extractor",
    cmd = ("/opt/kythe/extractors/bazel_cxx_extractor " +
           "$(EXTRA_ACTION_FILE) $(output $(ACTION_ID).cxx.kindex) $(location :vnames.json)"),
    data = [":vnames.json"],
    out_templates = ["$(ACTION_ID).cxx.kindex"],
)

action_listener(
    name = "extract_cxx",
    extra_actions = [":extractor"],
    mnemonics = ["CppCompile"],
    visibility = ["//visibility:public"],
)

Extractor requires a vnames.json file that tells extractor how to translate certain filepaths. For example in Bazel project java files often stored in java and javatests directories. But filepath like java/com/some/domain/Foo.java should be extracted as com/some/domain/Foo.java with the java prefix omitted. vnames.json file tells extractor how to rename certain filepaths during extraction. As an example check vnames.json from Kythe repo: link.

cd $YOUR_BAZEL_PROJECT
# As example copy vnames.json from Kythe repo. But you should change it for your project
# later.
curl https://raw.githubusercontent.com/kythe/kythe/master/kythe/data/vnames.json > vnames.json

Run extractor:

# run on all targets
bazel test --experimental_action_listener=:extract_cxx  //...

# run on specific target (e.g. some cc_binary)
bazel test --experimental_action_listener=:extract_cxx  //java/some/folder:foo

Extracted kindex files will be in bazel-out/local-fastbuild/extra_actions/extractor folder. One kindex file per target.

find -L bazel-out -name '*cxx.kindex'

Development

Building extractor from sources:

export KYTHE_PROJECT=path/to/kythe/repo/directory
cd $KYTHE_PROJECT
bazel build //kythe/cxx/extractor:cxx_extractor_bazel

Freshly built extractor will be in folder bazel-bin/kythe/cxx/extractor/cxx_extractor_bazel /opt/kythe/extractors/bazel_cxx_extractor. Follow instructions of above but extra_action replace /opt/kythe/extractors/bazel_cxx_extractor with the path to the freshly built exractor.