Skip to content

Commit 4fcd2f9

Browse files
j-piaseckimeta-codesync[bot]
authored andcommitted
Run codegen for each API view separately (#55997)
Summary: Pull Request resolved: #55997 Changelog: [Internal] Updates the C++ API snapshot generator to invoke codegen automatically for each created API view, generating for the specific platform that's being considered. Reviewed By: cipolleschi Differential Revision: D95788766 fbshipit-source-id: 8b376542f7822b73e44399c2615c327332378830
1 parent 0162958 commit 4fcd2f9

5 files changed

Lines changed: 115 additions & 67 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,6 @@ fix_*.patch
179179

180180
# Doxygen XML output used for C++ API tracking
181181
/packages/react-native/**/api/xml
182+
/packages/react-native/**/api/codegen
182183
/packages/react-native/**/.doxygen.config.generated
183184
/scripts/cxx-api/codegen

scripts/cxx-api/config.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ReactCommon:
2-
include_codegen: false
2+
codegen:
33
inputs:
44
- packages/react-native/ReactCommon
55
exclude_patterns:
@@ -22,13 +22,13 @@ ReactCommon:
2222
REACT_NATIVE_PRODUCTION: 1
2323

2424
ReactAndroid:
25-
include_codegen: true
25+
codegen:
26+
platform: android
2627
inputs:
2728
- packages/react-native/ReactCommon
2829
- packages/react-native/ReactAndroid
2930
exclude_patterns:
3031
- "*/test_utils/*"
31-
- "*/FBReactNativeSpec*/*"
3232
- "*/platform/cxx/*"
3333
- "*/platform/windows/*"
3434
- "*/platform/macos/*"
@@ -48,7 +48,8 @@ ReactAndroid:
4848

4949
# ReactIOS?
5050
ReactApple:
51-
include_codegen: true
51+
codegen:
52+
platform: ios
5253
inputs:
5354
- packages/react-native/ReactCommon
5455
- packages/react-native/React

scripts/cxx-api/parser/__main__.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
Entry point for running the parser package as a script.
88
99
Usage:
10-
# With codegen modules path:
11-
python ... --codegen-path /path/to/codegen
12-
1310
# With output directory:
1411
python ... --output-dir /path/to/output
1512
"""
@@ -73,18 +70,62 @@ def build_doxygen_config(
7370
f.write(config)
7471

7572

73+
def build_codegen(platform: str, verbose: bool = False) -> str:
74+
react_native_dir = os.path.join(get_react_native_dir(), "packages", "react-native")
75+
76+
result = subprocess.run(
77+
[
78+
"node",
79+
"./scripts/generate-codegen-artifacts.js",
80+
"--path",
81+
"./",
82+
"--outputPath",
83+
"./api/codegen",
84+
"--targetPlatform",
85+
platform,
86+
"--forceOutputPath",
87+
],
88+
cwd=react_native_dir,
89+
)
90+
91+
if result.returncode != 0:
92+
if verbose:
93+
print(f"Codegen finished with error: {result.stderr}")
94+
sys.exit(1)
95+
else:
96+
if verbose:
97+
print("Codegen finished successfully")
98+
99+
return os.path.join(react_native_dir, "api", "codegen")
100+
101+
76102
def build_snapshot_for_view(
77103
api_view: str,
78104
react_native_dir: str,
79105
include_directories: list[str],
80106
exclude_patterns: list[str],
81107
definitions: dict[str, str | int],
82108
output_dir: str,
109+
codegen_platform: str | None = None,
83110
verbose: bool = True,
84111
input_filter: str = None,
85112
) -> None:
113+
# If there is already an output directory, delete it
114+
if os.path.exists(os.path.join(react_native_dir, "api")):
115+
if verbose:
116+
print("Deleting existing output directory")
117+
subprocess.run(["rm", "-rf", os.path.join(react_native_dir, "api")])
118+
86119
if verbose:
87120
print(f"Generating API view: {api_view}")
121+
122+
if codegen_platform is not None:
123+
codegen_dir = build_codegen(codegen_platform, verbose=verbose)
124+
include_directories.append(codegen_dir)
125+
elif verbose:
126+
print("Skipping codegen")
127+
128+
if verbose:
88129
print("Generating Doxygen config file")
89130

90131
build_doxygen_config(
@@ -95,12 +136,6 @@ def build_snapshot_for_view(
95136
input_filter=input_filter,
96137
)
97138

98-
# If there is already a doxygen output directory, delete it
99-
if os.path.exists(os.path.join(react_native_dir, "api")):
100-
if verbose:
101-
print("Deleting existing output directory")
102-
subprocess.run(["rm", "-rf", os.path.join(react_native_dir, "api")])
103-
104139
if verbose:
105140
print("Running Doxygen")
106141
if input_filter:
@@ -120,6 +155,7 @@ def build_snapshot_for_view(
120155
if result.returncode != 0:
121156
if verbose:
122157
print(f"Doxygen finished with error: {result.stderr}")
158+
sys.exit(1)
123159
else:
124160
if verbose:
125161
print("Doxygen finished successfully")
@@ -152,11 +188,6 @@ def main():
152188
type=str,
153189
help="Output directory for the snapshot",
154190
)
155-
parser.add_argument(
156-
"--codegen-path",
157-
type=str,
158-
help="Path to codegen generated code",
159-
)
160191
parser.add_argument(
161192
"--check",
162193
action="store_true",
@@ -194,9 +225,6 @@ def main():
194225
if verbose:
195226
print(f"Running in directory: {react_native_package_dir}")
196227

197-
if verbose and args.codegen_path:
198-
print(f"Codegen output path: {os.path.abspath(args.codegen_path)}")
199-
200228
input_filter_path = os.path.join(
201229
get_react_native_dir(),
202230
"scripts",
@@ -216,7 +244,6 @@ def main():
216244
snapshot_configs = parse_config_file(
217245
config_path,
218246
get_react_native_dir(),
219-
codegen_path=args.codegen_path,
220247
)
221248

222249
def build_snapshots(output_dir: str, verbose: bool) -> None:
@@ -229,6 +256,7 @@ def build_snapshots(output_dir: str, verbose: bool) -> None:
229256
exclude_patterns=config.exclude_patterns,
230257
definitions=config.definitions,
231258
output_dir=output_dir,
259+
codegen_platform=config.codegen_platform,
232260
verbose=verbose,
233261
input_filter=input_filter,
234262
)
@@ -240,6 +268,7 @@ def build_snapshots(output_dir: str, verbose: bool) -> None:
240268
exclude_patterns=[],
241269
definitions={},
242270
output_dir=output_dir,
271+
codegen_platform=None,
243272
verbose=verbose,
244273
input_filter=input_filter,
245274
)

scripts/cxx-api/parser/config.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ class ApiViewSnapshotConfig:
3636
inputs: list[str]
3737
exclude_patterns: list[str]
3838
definitions: dict[str, str | int]
39+
codegen_platform: str | None = None
3940

4041

4142
def parse_config(
4243
raw_config: dict,
4344
base_dir: str,
44-
codegen_path: str | None = None,
4545
) -> list[ApiViewSnapshotConfig]:
4646
"""
4747
Parse a raw config dictionary and return a flattened list of snapshot configs.
@@ -62,10 +62,8 @@ def parse_config(
6262
for path in (view_config.get("inputs") or [])
6363
]
6464

65-
include_codegen = view_config.get("include_codegen", False)
66-
if include_codegen and codegen_path:
67-
inputs.append(os.path.abspath(codegen_path))
68-
65+
codegen_config = view_config.get("codegen") or {}
66+
codegen_platform = codegen_config.get("platform")
6967
exclude_patterns = view_config.get("exclude_patterns") or []
7068
base_definitions = view_config.get("definitions") or {}
7169

@@ -85,6 +83,7 @@ def parse_config(
8583
inputs=inputs,
8684
exclude_patterns=exclude_patterns,
8785
definitions=base_definitions,
86+
codegen_platform=codegen_platform,
8887
)
8988
)
9089
else:
@@ -97,6 +96,7 @@ def parse_config(
9796
inputs=inputs,
9897
exclude_patterns=exclude_patterns,
9998
definitions=merged_definitions,
99+
codegen_platform=codegen_platform,
100100
)
101101
)
102102

@@ -106,7 +106,6 @@ def parse_config(
106106
def parse_config_file(
107107
config_path: str,
108108
base_dir: str,
109-
codegen_path: str | None = None,
110109
) -> list[ApiViewSnapshotConfig]:
111110
"""
112111
Parse the config.yml file and return a flattened list of snapshot configs.
@@ -122,4 +121,4 @@ def parse_config_file(
122121
with open(config_path, "r") as stream:
123122
raw_config = yaml.safe_load(stream)
124123

125-
return parse_config(raw_config, base_dir, codegen_path)
124+
return parse_config(raw_config, base_dir)

0 commit comments

Comments
 (0)