Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ function generateNativeCode(
schemaInfos /*: $ReadOnlyArray<$FlowFixMe> */,
includesGeneratedCode /*: boolean */,
platform /*: string */,
forceOutputPath /*: boolean */ = false,
) /*: Array<void> */ {
return schemaInfos.map(schemaInfo => {
generateCode(outputPath, schemaInfo, includesGeneratedCode, platform);
generateCode(
outputPath,
schemaInfo,
includesGeneratedCode,
platform,
forceOutputPath,
);
});
}

Expand All @@ -33,6 +40,7 @@ function generateCode(
schemaInfo /*: $FlowFixMe */,
includesGeneratedCode /*: boolean */,
platform /*: string */,
forceOutputPath /*: boolean */ = false,
) {
if (shouldSkipGenerationForFBReactNativeSpec(schemaInfo, platform)) {
codegenLog(
Expand Down Expand Up @@ -60,8 +68,9 @@ function generateCode(
);

// Finally, copy artifacts to the final output directory.
const outputDir =
reactNativeCoreLibraryOutputPath(libraryName, platform) ?? outputPath;
const outputDir = forceOutputPath
? outputPath
: (reactNativeCoreLibraryOutputPath(libraryName, platform) ?? outputPath);
fs.mkdirSync(outputDir, {recursive: true});
cpSyncRecursiveIfChanged(tmpOutputDir, outputDir);
codegenLog(`Generated artifacts: ${outputDir}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function execute(
optionalBaseOutputPath /*: ?string */,
source /*: string */,
runReactNativeCodegen /*: boolean */ = true,
forceOutputPath /*: boolean */ = false,
) {
try {
codegenLog(`Analyzing ${path.join(projectRoot, 'package.json')}`);
Expand Down Expand Up @@ -146,6 +147,7 @@ function execute(
),
pkgJsonIncludesGeneratedCode(pkgJson),
platform,
forceOutputPath,
);
}

Expand Down
22 changes: 20 additions & 2 deletions packages/react-native/scripts/generate-codegen-artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,26 @@ const argv = yargs
description: 'Whether the script is invoked from an `app` or a `library`',
default: 'app',
})
.option('f', {
alias: 'forceOutputPath',
description:
'Whether to force React Native Core artifacts to output to the specified path',
type: 'boolean',
default: false,
})
.usage('Usage: $0 -p [path to app] -t [target platform] -o [output path]')
.demandOption(['p', 't']).argv;

// $FlowFixMe[prop-missing]
executor.execute(argv.path, argv.targetPlatform, argv.outputPath, argv.source);
executor.execute(
// $FlowFixMe[prop-missing]
argv.path,
// $FlowFixMe[prop-missing]
argv.targetPlatform,
// $FlowFixMe[prop-missing]
argv.outputPath,
// $FlowFixMe[prop-missing]
argv.source,
true, // runReactNativeCodegen
// $FlowFixMe[prop-missing]
argv.forceOutputPath,
);
9 changes: 5 additions & 4 deletions scripts/cxx-api/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ReactCommon:
include_codegen: false
codegen:
inputs:
- packages/react-native/ReactCommon
exclude_patterns:
Expand All @@ -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/*"
Expand All @@ -48,7 +48,8 @@ ReactAndroid:

# ReactIOS?
ReactApple:
include_codegen: true
codegen:
platform: ios
inputs:
- packages/react-native/ReactCommon
- packages/react-native/React
Expand Down
65 changes: 47 additions & 18 deletions scripts/cxx-api/parser/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -73,18 +70,62 @@ 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,
include_directories: list[str],
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(
Expand All @@ -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:
Expand All @@ -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")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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:
Expand All @@ -229,6 +256,7 @@ def build_snapshots(output_dir: str, verbose: bool) -> None:
exclude_patterns=config.exclude_patterns,
definitions=config.definitions,
output_dir=output_dir,
codegen_platform=config.codegen_platform,
verbose=verbose,
input_filter=input_filter,
)
Expand All @@ -240,6 +268,7 @@ def build_snapshots(output_dir: str, verbose: bool) -> None:
exclude_patterns=[],
definitions={},
output_dir=output_dir,
codegen_platform=None,
verbose=verbose,
input_filter=input_filter,
)
Expand Down
13 changes: 6 additions & 7 deletions scripts/cxx-api/parser/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {}

Expand All @@ -85,6 +83,7 @@ def parse_config(
inputs=inputs,
exclude_patterns=exclude_patterns,
definitions=base_definitions,
codegen_platform=codegen_platform,
)
)
else:
Expand All @@ -97,6 +96,7 @@ def parse_config(
inputs=inputs,
exclude_patterns=exclude_patterns,
definitions=merged_definitions,
codegen_platform=codegen_platform,
)
)

Expand All @@ -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.
Expand All @@ -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)
Loading
Loading