|
| 1 | +# scripts/cxx-api |
| 2 | + |
| 3 | +Python build pipeline for React Native's C++ (and Objective-C) API snapshots. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`scripts/cxx-api` generates human-readable snapshots of React Native's public C++ API surface. It uses [Doxygen](https://www.doxygen.nl/) to parse C/C++/Objective-C headers and a custom Python parser to produce a simplified, sorted representation of every public symbol. |
| 8 | + |
| 9 | +The pipeline produces one `.api` snapshot file per configured **API view × variant** combination: |
| 10 | + |
| 11 | +| Snapshot | Description | |
| 12 | +|---|---| |
| 13 | +| `ReactCommonDebugCxx.api` | Platform-independent C++ API (debug) | |
| 14 | +| `ReactCommonReleaseCxx.api` | Platform-independent C++ API (release) | |
| 15 | +| `ReactAndroidDebugCxx.api` | Android-specific C++ API (debug) | |
| 16 | +| `ReactAndroidReleaseCxx.api` | Android-specific C++ API (release) | |
| 17 | +| `ReactAppleDebugCxx.api` | Apple-specific C++/Obj-C API (debug) | |
| 18 | +| `ReactAppleReleaseCxx.api` | Apple-specific C++/Obj-C API (release) | |
| 19 | + |
| 20 | +For each view, debug and release variants are generated with different preprocessor definitions (e.g. `REACT_NATIVE_DEBUG` vs `NDEBUG`), since `#ifdef` guards in the source headers can produce a different public API surface per variant. |
| 21 | + |
| 22 | +Snapshot files are committed to the repo under `scripts/cxx-api/api-snapshots/`. |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +#### Generate snapshots |
| 27 | + |
| 28 | +Maintainers should run this command whenever making intentional C++ API changes: |
| 29 | + |
| 30 | +```sh |
| 31 | +python -m scripts.cxx-api.parser |
| 32 | +``` |
| 33 | + |
| 34 | +#### Check snapshots against committed baseline |
| 35 | + |
| 36 | +This mode generates snapshots to a temporary directory and compares them against the committed `.api` files. It is designed for CI: |
| 37 | + |
| 38 | +```sh |
| 39 | +python -m scripts.cxx-api.parser --check |
| 40 | +``` |
| 41 | + |
| 42 | +If any snapshot differs, a unified diff is printed and the process exits with a non-zero status. To fix a failing check, regenerate the snapshots with `python -m scripts.cxx-api.parser` and commit the updated `.api` files. |
| 43 | + |
| 44 | +## How it works |
| 45 | + |
| 46 | +The pipeline has two main stages: |
| 47 | + |
| 48 | +### 1. Doxygen XML generation |
| 49 | + |
| 50 | +Doxygen is configured via a generated config file (built from `.doxygen.config.template`) with the input directories, exclude patterns, and preprocessor definitions specified in `config.yml`. It outputs XML describing every symbol found in the headers. |
| 51 | + |
| 52 | +### 2. Snapshot parsing |
| 53 | + |
| 54 | +The Python parser (`parser/`) reads the Doxygen XML output and builds a scope tree of the public API surface. The tree is then serialized to a deterministically sorted, human-readable `.api` text format. |
| 55 | + |
| 56 | +## When to use it |
| 57 | + |
| 58 | +The snapshot should be regenerated whenever making intentional changes to the public C++ API surface. This includes additions, removals, and changes to files located in: |
| 59 | +- `xplat/js/react-native-github/` |
| 60 | +- `xplat/js/react-native-github/ReactCommon/` |
| 61 | +- `xplat/js/react-native-github/ReactAndroid/` |
| 62 | +- `xplat/js/react-native-github/ReactApple/` |
| 63 | +- `xplat/js/react-native-github/Libraries/` |
| 64 | + |
| 65 | +## Configuration |
| 66 | + |
| 67 | +All API views and their variants are defined in `config.yml`. Each view specifies: |
| 68 | + |
| 69 | +| Field | Description | |
| 70 | +|---|---| |
| 71 | +| `inputs` | Directories to scan for headers | |
| 72 | +| `exclude_patterns` | Glob patterns for files to skip | |
| 73 | +| `definitions` | Preprocessor macros to define | |
| 74 | +| `variants` | Named build variants (e.g. debug/release) with extra definitions | |
| 75 | +| `codegen` | Optional codegen platform (`android`, `ios`) to generate TurboModule/Component headers before scanning | |
| 76 | +| `private_directories` | Directories whose headers are scanned (they may be transitively included) but should not contribute public symbols. If any public API entity is defined in a private directory, a warning is printed to help catch accidental API exposure. | |
| 77 | + |
| 78 | +## Snapshot format |
| 79 | + |
| 80 | +The `.api` files use a minimal pseudo-C++ syntax designed for easy diffing: |
| 81 | + |
| 82 | +``` |
| 83 | +namespace facebook::react { |
| 84 | + class ComponentDescriptor { |
| 85 | + public ComponentDescriptor(ComponentDescriptorParameters params); |
| 86 | + public ComponentHandle getComponentHandle(); |
| 87 | + } |
| 88 | +
|
| 89 | + enum class AccessibilityRole { |
| 90 | + None = 0, |
| 91 | + Button = 1, |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +- Scopes and members are sorted alphabetically. |
| 97 | +- Access specifiers (`public`, `protected`) are preserved. |
| 98 | +- Template parameters are included. |
| 99 | +- Doc comments and source file names are stripped. |
0 commit comments