Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 1 | # rs_bindings_from_cc |
| 2 | |
Marcel Hlopko | 7ebafb2 | 2021-07-30 12:14:00 +0000 | [diff] [blame] | 3 | Disclaimer: This project is experimental, under heavy development, and should |
Marcel Hlopko | c355642 | 2022-03-14 16:08:14 +0000 | [diff] [blame] | 4 | not be used yet. |
Marcel Hlopko | 7ebafb2 | 2021-07-30 12:14:00 +0000 | [diff] [blame] | 5 | |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 6 | `: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 | |
| 11 | For convenience, `:test_wrapper` is a shell script that passes all Clang command |
Marcel Hlopko | 9a94fc4 | 2022-04-06 23:35:36 -0700 | [diff] [blame] | 12 | line flags from the current Bazel C++ toolchain: |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 13 | |
| 14 | ``` |
Devin Jeanpierre | d7f4b3b | 2021-10-06 15:28:18 +0000 | [diff] [blame] | 15 | bazel run //rs_bindings_from_cc:test_wrapper -- --public_headers=hello_world.h |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 16 | ``` |
| 17 | |
| 18 | or: |
| 19 | |
| 20 | ``` |
| 21 | bazel build //rs_bindings_from_cc:test_wrapper |
Devin Jeanpierre | d7f4b3b | 2021-10-06 15:28:18 +0000 | [diff] [blame] | 22 | bazel-bin/rs_bindings_from_cc/test_wrapper --public_headers=hello_world.h |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 23 | ``` |
| 24 | |
| 25 | ## Testing |
| 26 | |
Marcel Hlopko | 5b8c112 | 2021-12-10 06:59:23 +0000 | [diff] [blame] | 27 | If possible follow these recommendations: |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 28 | |
Marcel Hlopko | 5b8c112 | 2021-12-10 06:59:23 +0000 | [diff] [blame] | 29 | * Unit tests for |
Kinuko Yasuda | abf4f3e | 2022-08-16 12:09:49 -0700 | [diff] [blame] | 30 | [`src_code_gen`](rs_bindings_from_cc/src_code_gen.rs) |
| 31 | should: |
| 32 | * be written in Rust |
Marcel Hlopko | 5b8c112 | 2021-12-10 06:59:23 +0000 | [diff] [blame] | 33 | * have snippets of C++ as input |
| 34 | * use |
Kinuko Yasuda | abf4f3e | 2022-08-16 12:09:49 -0700 | [diff] [blame] | 35 | [`assert_cc_matches!/assert_rs_matches!/assert_cc_not_matches!/assert_rs_not_matches!`](rs_bindings_from_cc/token_stream_matchers.rs) |
Marcel Hlopko | 5b8c112 | 2021-12-10 06:59:23 +0000 | [diff] [blame] | 36 | macros |
| 37 | * Unit tests for the |
Kinuko Yasuda | abf4f3e | 2022-08-16 12:09:49 -0700 | [diff] [blame] | 38 | [`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 Hlopko | 5b8c112 | 2021-12-10 06:59:23 +0000 | [diff] [blame] | 42 | 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 Yasuda | abf4f3e | 2022-08-16 12:09:49 -0700 | [diff] [blame] | 44 | ([`importer_test.cc`](rs_bindings_from_cc/importer_test.cc)) |
Marcel Hlopko | 5b8c112 | 2021-12-10 06:59:23 +0000 | [diff] [blame] | 45 | * 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 Yasuda | abf4f3e | 2022-08-16 12:09:49 -0700 | [diff] [blame] | 48 | [`rs_bindings_from_cc_test.sh`](rs_bindings_from_cc/test/rs_bindings_from_cc_test.sh). |
Marcel Hlopko | 5b8c112 | 2021-12-10 06:59:23 +0000 | [diff] [blame] | 49 | * Write golden file tests (comparing both the C++ and Rust generated source |
| 50 | code against the checked-in files) in |
Kinuko Yasuda | abf4f3e | 2022-08-16 12:09:49 -0700 | [diff] [blame] | 51 | [`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. |
Googler | b2ef22d | 2022-01-04 11:17:22 +0000 | [diff] [blame] | 55 | * Write full executable end-to-end tests (verifying that interop tools and |
Marcel Hlopko | 9a94fc4 | 2022-04-06 23:35:36 -0700 | [diff] [blame] | 56 | Bazel rules generate outputs that can be built and executed) as small |
Marcel Hlopko | 5b8c112 | 2021-12-10 06:59:23 +0000 | [diff] [blame] | 57 | projects with a `rust_test` or `cc_test` on top in subpackages of `test`. |
Devin Jeanpierre | d9c382a | 2021-12-15 10:23:25 +0000 | [diff] [blame] | 58 | |
Kinuko Yasuda | ed9efd8 | 2022-08-18 13:55:01 -0700 | [diff] [blame] | 59 | To 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 | |
Googler | b2ef22d | 2022-01-04 11:17:22 +0000 | [diff] [blame] | 61 | To get Rust backtraces for `rs_bindings_from_cc` when running end-to-end tests, |
| 62 | use `bazel test --action_env=RUST_BACKTRACE=1` to run the tests. |
| 63 | |
Marcel Hlopko | 253ceb4 | 2022-09-21 04:10:35 -0700 | [diff] [blame] | 64 | ## Debugging |
| 65 | |
| 66 | If you want to build the tool specially, for example using sanitizers, use the |
| 67 | script at `rs_bindings_from_cc/generate_bindings_for_target_with_tool_flags.sh`, for example: |
| 68 | |
| 69 | ``` |
| 70 | rs_bindings_from_cc/generate_bindings_for_target_with_tool_flags.sh \ |
| 71 | //base \ |
| 72 | --config=asan |
| 73 | ``` |
| 74 | |
| 75 | If you want to build the tool specially and use it for generating bindings from |
| 76 | a golden file, use the `<header basename>_rs_test` target. For `types.h` the |
| 77 | command would be: |
| 78 | |
| 79 | ``` |
| 80 | rs_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 | |
| 85 | If you want to see the Clang AST dump of some file (generated files work too), run: |
| 86 | |
| 87 | ``` |
| 88 | bazel build --per_file_copt=<PATH_TO_FILE>@-Xclang,-ast-dump,-fno-color-diagnostics <TARGET> > /tmp/output_file |
| 89 | ``` |
| 90 | |
Devin Jeanpierre | d9c382a | 2021-12-15 10:23:25 +0000 | [diff] [blame] | 91 | ## Contributing |
| 92 | |
Kinuko Yasuda | abf4f3e | 2022-08-16 12:09:49 -0700 | [diff] [blame] | 93 | Chat room (internal): https://chat.google.com/room/AAAAImO--WA |
Devin Jeanpierre | d9c382a | 2021-12-15 10:23:25 +0000 | [diff] [blame] | 94 | |
Kinuko Yasuda | abf4f3e | 2022-08-16 12:09:49 -0700 | [diff] [blame] | 95 | 20% starter projects list (internal): b/hotlists/3645339 |