Run codegen for each API view separately (#55997)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Jakub Piasecki authored and meta-codesync[bot] committed 2026-03-10 09:09:06 -07:00
1 parent 01629586df
commit 4fcd2f9f6d
5 files changed
+115 -67

No files matched your search

+1
View File
@@ -179,5 +179,6 @@ fix_*.patch
# Doxygen XML output used for C++ API tracking
/packages/react-native/**/api/xml
/packages/react-native/**/api/codegen
/packages/react-native/**/.doxygen.config.generated
/scripts/cxx-api/codegen
+5 -4
View File
@@ -1,5 +1,5 @@
ReactCommon:
include_codegen: false
codegen:
inputs:
- packages/react-native/ReactCommon
exclude_patterns:
@@ -22,13 +22,13 @@ ReactCommon:
REACT_NATIVE_PRODUCTION: 1
ReactAndroid:
include_codegen: true
codegen:
platform: android
inputs:
- packages/react-native/ReactCommon
- packages/react-native/ReactAndroid
exclude_patterns:
- "*/test_utils/*"
- "*/FBReactNativeSpec*/*"
- "*/platform/cxx/*"
- "*/platform/windows/*"
- "*/platform/macos/*"
@@ -48,7 +48,8 @@ ReactAndroid:
# ReactIOS?
ReactApple:
include_codegen: true
codegen:
platform: ios
inputs:
- packages/react-native/ReactCommon
- packages/react-native/React
+47 -18
View File
@@ -7,9 +7,6 @@
Entry point for running the parser package as a script.
Usage:
# With codegen modules path:
python ... --codegen-path /path/to/codegen
# With output directory:
python ... --output-dir /path/to/output
"""
@@ -73,6 +70,35 @@ def build_doxygen_config(
f.write(config)
def build_codegen(platform: str, verbose: bool = False) -> str:
react_native_dir = os.path.join(get_react_native_dir(), "packages", "react-native")
result = subprocess.run(
[
"node",
"./scripts/generate-codegen-artifacts.js",
"--path",
"./",
"--outputPath",
"./api/codegen",
"--targetPlatform",
platform,
"--forceOutputPath",
],
cwd=react_native_dir,
)
if result.returncode != 0:
if verbose:
print(f"Codegen finished with error: {result.stderr}")
sys.exit(1)
else:
if verbose:
print("Codegen finished successfully")
return os.path.join(react_native_dir, "api", "codegen")
def build_snapshot_for_view(
api_view: str,
react_native_dir: str,
@@ -80,11 +106,26 @@ def build_snapshot_for_view(
exclude_patterns: list[str],
definitions: dict[str, str | int],
output_dir: str,
codegen_platform: str | None = None,
verbose: bool = True,
input_filter: str = None,
) -> None:
# If there is already an output directory, delete it
if os.path.exists(os.path.join(react_native_dir, "api")):
if verbose:
print("Deleting existing output directory")
subprocess.run(["rm", "-rf", os.path.join(react_native_dir, "api")])
if verbose:
print(f"Generating API view: {api_view}")
if codegen_platform is not None:
codegen_dir = build_codegen(codegen_platform, verbose=verbose)
include_directories.append(codegen_dir)
elif verbose:
print("Skipping codegen")
if verbose:
print("Generating Doxygen config file")
build_doxygen_config(
@@ -95,12 +136,6 @@ def build_snapshot_for_view(
input_filter=input_filter,
)
# If there is already a doxygen output directory, delete it
if os.path.exists(os.path.join(react_native_dir, "api")):
if verbose:
print("Deleting existing output directory")
subprocess.run(["rm", "-rf", os.path.join(react_native_dir, "api")])
if verbose:
print("Running Doxygen")
if input_filter:
@@ -120,6 +155,7 @@ def build_snapshot_for_view(
if result.returncode != 0:
if verbose:
print(f"Doxygen finished with error: {result.stderr}")
sys.exit(1)
else:
if verbose:
print("Doxygen finished successfully")
@@ -152,11 +188,6 @@ def main():
type=str,
help="Output directory for the snapshot",
)
parser.add_argument(
"--codegen-path",
type=str,
help="Path to codegen generated code",
)
parser.add_argument(
"--check",
action="store_true",
@@ -194,9 +225,6 @@ def main():
if verbose:
print(f"Running in directory: {react_native_package_dir}")
if verbose and args.codegen_path:
print(f"Codegen output path: {os.path.abspath(args.codegen_path)}")
input_filter_path = os.path.join(
get_react_native_dir(),
"scripts",
@@ -216,7 +244,6 @@ def main():
snapshot_configs = parse_config_file(
config_path,
get_react_native_dir(),
codegen_path=args.codegen_path,
)
def build_snapshots(output_dir: str, verbose: bool) -> None:
@@ -229,6 +256,7 @@ def main():
exclude_patterns=config.exclude_patterns,
definitions=config.definitions,
output_dir=output_dir,
codegen_platform=config.codegen_platform,
verbose=verbose,
input_filter=input_filter,
)
@@ -240,6 +268,7 @@ def main():
exclude_patterns=[],
definitions={},
output_dir=output_dir,
codegen_platform=None,
verbose=verbose,
input_filter=input_filter,
)
+6 -7
View File
@@ -36,12 +36,12 @@ class ApiViewSnapshotConfig:
inputs: list[str]
exclude_patterns: list[str]
definitions: dict[str, str | int]
codegen_platform: str | None = None
def parse_config(
raw_config: dict,
base_dir: str,
codegen_path: str | None = None,
) -> list[ApiViewSnapshotConfig]:
"""
Parse a raw config dictionary and return a flattened list of snapshot configs.
@@ -62,10 +62,8 @@ def parse_config(
for path in (view_config.get("inputs") or [])
]
include_codegen = view_config.get("include_codegen", False)
if include_codegen and codegen_path:
inputs.append(os.path.abspath(codegen_path))
codegen_config = view_config.get("codegen") or {}
codegen_platform = codegen_config.get("platform")
exclude_patterns = view_config.get("exclude_patterns") or []
base_definitions = view_config.get("definitions") or {}
@@ -85,6 +83,7 @@ def parse_config(
inputs=inputs,
exclude_patterns=exclude_patterns,
definitions=base_definitions,
codegen_platform=codegen_platform,
)
)
else:
@@ -97,6 +96,7 @@ def parse_config(
inputs=inputs,
exclude_patterns=exclude_patterns,
definitions=merged_definitions,
codegen_platform=codegen_platform,
)
)
@@ -106,7 +106,6 @@ def parse_config(
def parse_config_file(
config_path: str,
base_dir: str,
codegen_path: str | None = None,
) -> list[ApiViewSnapshotConfig]:
"""
Parse the config.yml file and return a flattened list of snapshot configs.
@@ -122,4 +121,4 @@ def parse_config_file(
with open(config_path, "r") as stream:
raw_config = yaml.safe_load(stream)
return parse_config(raw_config, base_dir, codegen_path)
return parse_config(raw_config, base_dir)
+56 -38
View File
@@ -26,7 +26,7 @@ class TestParseConfig(unittest.TestCase):
"""Single view without variants"""
config = {
"ReactCommon": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": ["packages/react-native/ReactCommon"],
"exclude_patterns": ["*/jni/*"],
"definitions": {"FOO": 1},
@@ -46,7 +46,7 @@ class TestParseConfig(unittest.TestCase):
"""Single view with debug and release variants"""
config = {
"ReactCommon": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": ["packages/react-native/ReactCommon"],
"exclude_patterns": [],
"definitions": {},
@@ -78,7 +78,7 @@ class TestParseConfig(unittest.TestCase):
"""Base definitions are merged with variant definitions"""
config = {
"ReactAndroid": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {"RN_SERIALIZABLE_STATE": 1, "ANDROID": 1},
@@ -108,7 +108,7 @@ class TestParseConfig(unittest.TestCase):
"""Variant definitions override base definitions with same key"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {"MODE": "base", "SHARED": 1},
@@ -129,7 +129,7 @@ class TestParseConfig(unittest.TestCase):
"""Variant with empty definitions still inherits base"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {"BASE": 1},
@@ -147,7 +147,7 @@ class TestParseConfig(unittest.TestCase):
"""Variant with None definitions still inherits base"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {"BASE": 1},
@@ -169,7 +169,7 @@ class TestParseConfig(unittest.TestCase):
"""Relative paths are joined with base_dir"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": ["packages/foo", "packages/bar"],
"exclude_patterns": [],
"definitions": {},
@@ -186,7 +186,7 @@ class TestParseConfig(unittest.TestCase):
"""Absolute paths are not modified"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": ["/absolute/path/foo", "relative/path"],
"exclude_patterns": [],
"definitions": {},
@@ -203,7 +203,7 @@ class TestParseConfig(unittest.TestCase):
"""Empty inputs list"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {},
@@ -217,7 +217,7 @@ class TestParseConfig(unittest.TestCase):
"""None inputs treated as empty list"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": None,
"exclude_patterns": [],
"definitions": {},
@@ -231,49 +231,67 @@ class TestParseConfig(unittest.TestCase):
# Codegen path handling
# =========================================================================
def test_codegen_path_added_when_include_codegen_true(self):
"""Codegen path is appended when include_codegen is true"""
def test_codegen_platform_set_when_generate_true(self):
"""codegen_platform is set when codegen.generate is true"""
config = {
"TestView": {
"include_codegen": True,
"inputs": ["packages/foo"],
"codegen": {"generate": True, "platform": "android"},
"inputs": [],
"exclude_patterns": [],
"definitions": {},
}
}
result = parse_config(config, "/base/dir", codegen_path="/codegen/path")
result = parse_config(config, "/base/dir")
self.assertIn("/codegen/path", result[0].inputs)
self.assertEqual(len(result[0].inputs), 2)
self.assertEqual(result[0].codegen_platform, "android")
def test_codegen_path_not_added_when_include_codegen_false(self):
"""Codegen path is not added when include_codegen is false"""
def test_codegen_platform_ios(self):
"""codegen_platform correctly stores ios platform"""
config = {
"TestView": {
"include_codegen": False,
"inputs": ["packages/foo"],
"codegen": {"generate": True, "platform": "ios"},
"inputs": [],
"exclude_patterns": [],
"definitions": {},
}
}
result = parse_config(config, "/base/dir", codegen_path="/codegen/path")
result = parse_config(config, "/base/dir")
self.assertNotIn("/codegen/path", result[0].inputs)
self.assertEqual(len(result[0].inputs), 1)
self.assertEqual(result[0].codegen_platform, "ios")
def test_codegen_path_not_added_when_none(self):
"""Codegen path not added when codegen_path is None"""
def test_codegen_platform_propagated_to_variants(self):
"""codegen_platform is propagated to all variant configs"""
config = {
"TestView": {
"include_codegen": True,
"inputs": ["packages/foo"],
"codegen": {"generate": True, "platform": "android"},
"inputs": [],
"exclude_patterns": [],
"definitions": {},
"variants": {
"debug": {"definitions": {"DEBUG": 1}},
"release": {"definitions": {"NDEBUG": 1}},
},
}
}
result = parse_config(config, "/base/dir")
self.assertEqual(len(result), 2)
for r in result:
self.assertEqual(r.codegen_platform, "android")
def test_codegen_missing_defaults_to_no_codegen(self):
"""Missing codegen config defaults to no codegen"""
config = {
"TestView": {
"inputs": [],
"exclude_patterns": [],
"definitions": {},
}
}
result = parse_config(config, "/base/dir", codegen_path=None)
result = parse_config(config, "/base/dir")
self.assertEqual(len(result[0].inputs), 1)
self.assertEqual(len(result[0].inputs), 0)
self.assertIsNone(result[0].codegen_platform)
# =========================================================================
# Multiple views
@@ -283,13 +301,13 @@ class TestParseConfig(unittest.TestCase):
"""Multiple views are all parsed"""
config = {
"ViewA": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {"A": 1},
},
"ViewB": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {"B": 1},
@@ -305,7 +323,7 @@ class TestParseConfig(unittest.TestCase):
"""Multiple views each with variants"""
config = {
"ViewA": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {},
@@ -315,7 +333,7 @@ class TestParseConfig(unittest.TestCase):
},
},
"ViewB": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {},
@@ -338,7 +356,7 @@ class TestParseConfig(unittest.TestCase):
"""Exclude patterns are passed through unchanged"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": ["*/jni/*", "*/platform/ios/*"],
"definitions": {},
@@ -355,7 +373,7 @@ class TestParseConfig(unittest.TestCase):
"""None exclude_patterns treated as empty list"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": None,
"definitions": {},
@@ -373,7 +391,7 @@ class TestParseConfig(unittest.TestCase):
"""Variant names are capitalized in snapshot name"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": {},
@@ -408,7 +426,7 @@ class TestParseConfig(unittest.TestCase):
"""None definitions treated as empty dict"""
config = {
"TestView": {
"include_codegen": False,
"codegen": {"generate": False},
"inputs": [],
"exclude_patterns": [],
"definitions": None,