|
| 1 | +# Tracing to Source Files (Bazel / Source Code Linker) |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +`lobster-bazel` scans source files for inline tracing tags and produces a |
| 6 | +`.lobster` file in the `lobster-imp-trace` format. It is designed for use in |
| 7 | +Bazel-based projects where the build system can provide lists of source files to |
| 8 | +scan, but the tool itself is not Bazel-specific and can be used with any build |
| 9 | +system that can produce file-list inputs. |
| 10 | + |
| 11 | +Supported file types (automatically detected by extension): |
| 12 | + |
| 13 | +| Extension | Language | Comment sign | |
| 14 | +|----------------------|-----------|--------------| |
| 15 | +| `.c`, `.cc`, `.cpp`, `.cxx`, `.h`, `.hh`, `.hpp`, `.hxx` | C/C++ | `//` | |
| 16 | +| `.rs` | Rust | `//` | |
| 17 | +| `.py` | Python | `#` | |
| 18 | +| `.bzl` | Starlark | `#` | |
| 19 | +| `.trlc`, `.rsl` | TRLC | `#` | |
| 20 | + |
| 21 | +Files with unsupported extensions are silently skipped; the run continues and |
| 22 | +reports the number of items extracted from the remaining files. |
| 23 | + |
| 24 | +## Adding tracing tags to source files |
| 25 | + |
| 26 | +Add a single-line comment **at the start of the line** with the tracing tag |
| 27 | +attribute and the requirement ID: |
| 28 | + |
| 29 | +```python |
| 30 | +# Python example |
| 31 | +def process(): |
| 32 | + # req-traceability: COMP_REQ_001 |
| 33 | + pass |
| 34 | +``` |
| 35 | + |
| 36 | +```cpp |
| 37 | +// C++ example |
| 38 | +void process() { |
| 39 | + // req-traceability: COMP_REQ_001 |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +```rust |
| 44 | +// Rust example |
| 45 | +fn process() { |
| 46 | + // req-traceability: COMP_REQ_001 |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +> **Note:** The tag pattern must appear at the start of the (stripped) line. |
| 51 | +> Inline comments at the end of a code statement are intentionally **not** |
| 52 | +> matched to avoid false positives. |
| 53 | +
|
| 54 | +The tag attribute (e.g. `req-traceability`) is configurable via `--tag`. |
| 55 | +The default tag used by the `lobster_linker` Bazel rule is `lobster-trace`. |
| 56 | + |
| 57 | +## Creating lobster files |
| 58 | + |
| 59 | +The tool reads **file-list files** as positional arguments. Each file-list file |
| 60 | +contains one source file path per line. This design integrates naturally with |
| 61 | +Bazel's `$(locations ...)` expansion or any tool that can dump a list of files. |
| 62 | + |
| 63 | +```sh |
| 64 | +$ lobster-bazel --output impl.lobster \ |
| 65 | + --tag req-traceability \ |
| 66 | + source_files.txt |
| 67 | +``` |
| 68 | + |
| 69 | +Where `source_files.txt` contains: |
| 70 | + |
| 71 | +``` |
| 72 | +src/module_a.py |
| 73 | +src/module_b.rs |
| 74 | +include/module_c.hpp |
| 75 | +``` |
| 76 | + |
| 77 | +Multiple file-list files and multiple `--tag` values are supported: |
| 78 | + |
| 79 | +```sh |
| 80 | +$ lobster-bazel --output impl.lobster \ |
| 81 | + --tag req-traceability \ |
| 82 | + --tag req-Id \ |
| 83 | + sources_a.txt sources_b.txt |
| 84 | +``` |
| 85 | + |
| 86 | +An optional `--namespace` argument controls the namespace prefix for the |
| 87 | +generated tags (default: `source`): |
| 88 | + |
| 89 | +```sh |
| 90 | +$ lobster-bazel --output impl.lobster \ |
| 91 | + --tag req-traceability \ |
| 92 | + --namespace impl \ |
| 93 | + source_files.txt |
| 94 | +``` |
| 95 | + |
| 96 | +### Error behaviour |
| 97 | + |
| 98 | +- **Unsupported file extension**: the file is skipped with a warning; the run |
| 99 | + continues. |
| 100 | +- **Unreadable or missing source file**: an error is logged and the file is |
| 101 | + skipped; the run continues and exits with code 0. |
| 102 | +- **Unreadable file-list file**: a fatal error is logged and the tool exits |
| 103 | + with code 1. |
| 104 | + |
| 105 | +## Bazel integration |
| 106 | + |
| 107 | +When using Bazel, use the `lobster_linker` rule from `lobster.bzl`: |
| 108 | + |
| 109 | +```starlark |
| 110 | +load("@lobster//:lobster.bzl", "lobster_linker", "lobster_test") |
| 111 | + |
| 112 | +lobster_linker( |
| 113 | + name = "impl_trace", |
| 114 | + srcs = [ |
| 115 | + "//src:my_library", |
| 116 | + "//src:my_binary", |
| 117 | + ], |
| 118 | + tracing_tags = ["req-traceability"], |
| 119 | +) |
| 120 | +``` |
| 121 | + |
| 122 | +The `lobster_linker` rule automatically collects all source files from the |
| 123 | +listed targets and passes them to `lobster-bazel`. The output is exposed as a |
| 124 | +`LobsterProvider` so it can be consumed by `lobster_test`. |
| 125 | + |
| 126 | +The `subrule_lobster_linker` subrule is also exported for composing the linker |
| 127 | +into other custom Bazel rules: |
| 128 | + |
| 129 | +```starlark |
| 130 | +load("@lobster//:lobster.bzl", "subrule_lobster_linker") |
| 131 | +``` |
0 commit comments