blob: 39ae2ef7cc9a7b0231cefaa400b6920aa192f8c9 [file] [log] [blame] [view]
Marcel Hlopkoe8f1c4e2021-07-28 18:12:49 +00001# rs_bindings_from_cc
2
Marcel Hlopko7ebafb22021-07-30 12:14:00 +00003Disclaimer: This project is experimental, under heavy development, and should
Marcel Hlopkoc3556422022-03-14 16:08:14 +00004not be used yet.
Marcel Hlopko7ebafb22021-07-30 12:14:00 +00005
Marcel Hlopkoe8f1c4e2021-07-28 18:12:49 +00006`:rs_bindings_from_cc` parses C++ headers and generates:
7
8* a Rust source file with bindings for the C++ API
9* a C++ source file with the implementation of the bindings
10
11For convenience, `:test_wrapper` is a shell script that passes all Clang command
Marcel Hlopko9a94fc42022-04-06 23:35:36 -070012line flags from the current Bazel C++ toolchain:
Marcel Hlopkoe8f1c4e2021-07-28 18:12:49 +000013
14```
Devin Jeanpierred7f4b3b2021-10-06 15:28:18 +000015bazel run //rs_bindings_from_cc:test_wrapper -- --public_headers=hello_world.h
Marcel Hlopkoe8f1c4e2021-07-28 18:12:49 +000016```
17
18or:
19
20```
21bazel build //rs_bindings_from_cc:test_wrapper
Devin Jeanpierred7f4b3b2021-10-06 15:28:18 +000022bazel-bin/rs_bindings_from_cc/test_wrapper --public_headers=hello_world.h
Marcel Hlopkoe8f1c4e2021-07-28 18:12:49 +000023```
24
25## Testing
26
Marcel Hlopko5b8c1122021-12-10 06:59:23 +000027If possible follow these recommendations:
Marcel Hlopkoe8f1c4e2021-07-28 18:12:49 +000028
Marcel Hlopko5b8c1122021-12-10 06:59:23 +000029* Unit tests for
Kinuko Yasudaabf4f3e2022-08-16 12:09:49 -070030 [`src_code_gen`](rs_bindings_from_cc/src_code_gen.rs)
31 should:
32 * be written in Rust
Marcel Hlopko5b8c1122021-12-10 06:59:23 +000033 * have snippets of C++ as input
34 * use
Kinuko Yasudaabf4f3e2022-08-16 12:09:49 -070035 [`assert_cc_matches!/assert_rs_matches!/assert_cc_not_matches!/assert_rs_not_matches!`](rs_bindings_from_cc/token_stream_matchers.rs)
Marcel Hlopko5b8c1122021-12-10 06:59:23 +000036 macros
37* Unit tests for the
Kinuko Yasudaabf4f3e2022-08-16 12:09:49 -070038 [`importer`](rs_bindings_from_cc/importer.h)
39 should:
40 * be written in Rust
41 ([`ir_from_cc_test.rs`](rs_bindings_from_cc/ir_from_cc_test.rs))
Marcel Hlopko5b8c1122021-12-10 06:59:23 +000042 so they cover both AST logic and IR serialization/deserialization, but
43 C++ tests (thanks to its nice matchers) are also OK at the moment
Kinuko Yasudaabf4f3e2022-08-16 12:09:49 -070044 ([`importer_test.cc`](rs_bindings_from_cc/importer_test.cc))
Marcel Hlopko5b8c1122021-12-10 06:59:23 +000045 * have snippets of C++ as input
46 * make assertions on the content of the IR
47* Write tests for the command line interface of interop tools in
Kinuko Yasudaabf4f3e2022-08-16 12:09:49 -070048 [`rs_bindings_from_cc_test.sh`](rs_bindings_from_cc/test/rs_bindings_from_cc_test.sh).
Marcel Hlopko5b8c1122021-12-10 06:59:23 +000049* Write golden file tests (comparing both the C++ and Rust generated source
50 code against the checked-in files) in
Kinuko Yasudaabf4f3e2022-08-16 12:09:49 -070051 [`test/golden`](rs_bindings_from_cc/test/golden/).
52 * Run
53 [`rs_bindings_from_cc/test/golden/update.sh`](rs_bindings_from_cc/test/golden/update.sh)
54 to regenerate checked-in files.
Googlerb2ef22d2022-01-04 11:17:22 +000055* Write full executable end-to-end tests (verifying that interop tools and
Marcel Hlopko9a94fc42022-04-06 23:35:36 -070056 Bazel rules generate outputs that can be built and executed) as small
Marcel Hlopko5b8c1122021-12-10 06:59:23 +000057 projects with a `rust_test` or `cc_test` on top in subpackages of `test`.
Devin Jeanpierred9c382a2021-12-15 10:23:25 +000058
Kinuko Yasudaed9efd82022-08-18 13:55:01 -070059To run individual rust tests with `bazel test` (like `bazel test --test_filter=<test>` for gtest cases), give the test function name as `--test_arg=<test>`.
60
Googlerb2ef22d2022-01-04 11:17:22 +000061To get Rust backtraces for `rs_bindings_from_cc` when running end-to-end tests,
62use `bazel test --action_env=RUST_BACKTRACE=1` to run the tests.
63
Marcel Hlopko253ceb42022-09-21 04:10:35 -070064## Debugging
65
66If you want to build the tool specially, for example using sanitizers, use the
67script at `rs_bindings_from_cc/generate_bindings_for_target_with_tool_flags.sh`, for example:
68
69```
70rs_bindings_from_cc/generate_bindings_for_target_with_tool_flags.sh \
71 //base \
72 --config=asan
73```
74
75If you want to build the tool specially and use it for generating bindings from
76a golden file, use the `<header basename>_rs_test` target. For `types.h` the
77command would be:
78
79```
80rs_bindings_from_cc/generate_bindings_for_target_with_tool_flags.sh \
81 //rs_bindings_from_cc/test/golden:types_rs_test \
82 --config=asan
83```
84
85If you want to see the Clang AST dump of some file (generated files work too), run:
86
87```
88bazel build --per_file_copt=<PATH_TO_FILE>@-Xclang,-ast-dump,-fno-color-diagnostics <TARGET> > /tmp/output_file
89```
90
Devin Jeanpierred9c382a2021-12-15 10:23:25 +000091## Contributing
92
Kinuko Yasudaabf4f3e2022-08-16 12:09:49 -070093Chat room (internal): https://chat.google.com/room/AAAAImO--WA
Devin Jeanpierred9c382a2021-12-15 10:23:25 +000094
Kinuko Yasudaabf4f3e2022-08-16 12:09:49 -07009520% starter projects list (internal): b/hotlists/3645339