Skip to content

Commit

Permalink
Create rn_codegen BUCK macro (#31340)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #31340

Changelog: [Internal]

Reviewed By: yungsters

Differential Revision: D27669950

fbshipit-source-id: 3129d87da8f74d896c0b9e82799b8896b3ed7635
  • Loading branch information
RSNara authored and facebook-github-bot committed Apr 15, 2021
1 parent 1747536 commit e1eb938
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 39 deletions.
46 changes: 7 additions & 39 deletions Libraries/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,22 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

load("//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load(
"//tools/build_defs/oss:rn_codegen_defs.bzl",
"rn_codegen_components",
"rn_codegen_modules",
)
load(
"//tools/build_defs/oss:rn_defs.bzl",
"react_native_root_target",
)

fb_native.genrule(
# The schema name must have the following format: "{name}-codegen-modules-schema"
# Why: Internally, we have build scripts that find all NativeModule schemas in the
# dependencies of an app, and build TurboModuleManager delegates. Those scripts assume
# that all schema targets have the aforementioned naming scheme.
name = "FBReactNativeSpec-codegen-modules-schema",
srcs = glob(
[
"**/*.js",
],
exclude = [
"**/__tests__/**/*",
],
),
cmd = "$(exe {}) $OUT $SRCS".format(react_native_root_target("packages/react-native-codegen:write_to_json")),
out = "schema.json",
labels = [
"codegen_rule",
"react_native_schema_target",
],
"rn_codegen",
)

rn_codegen_modules(
rn_codegen(
name = "FBReactNativeSpec",
android_package_name = "com.facebook.fbreact.specs",
codegen_modules = True,
library_labels = ["supermodule:xplat/default/public.react_native.infra"],
schema_target = ":FBReactNativeSpec-codegen-modules-schema",
native_module_spec_name = "FBReactNativeSpec",
)

rn_codegen_components(
# TODO: Merge this into FBReactNativeSpec
rn_codegen(
name = "FBReactNativeComponentSpec",
codegen_components = True,
library_labels = ["supermodule:xplat/default/public.react_native.infra"],
# Why does FBReactNativeComponentSpec depend on -codegen-modules-schema?
# The module codegen schema also contains components. We cannot change the name
# of the schema target, because internally, we have infra that depends on how
# it's named.
#
# TODO(T83341482): Clean up how OSS NativeModule codegen is declared
schema_target = ":FBReactNativeSpec-codegen-modules-schema",
)
68 changes: 68 additions & 0 deletions tools/build_defs/oss/rn_codegen_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,74 @@ load(
_rn_codegen_components = "rn_codegen_components",
_rn_codegen_modules = "rn_codegen_modules",
)
load("//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load(
"//tools/build_defs/oss:rn_defs.bzl",
"react_native_root_target",
)

rn_codegen_components = _rn_codegen_components
rn_codegen_modules = _rn_codegen_modules

def rn_codegen(
name,
native_module_spec_name = None,
android_package_name = None,
codegen_components = False,
codegen_modules = False,
library_labels = []):
if (codegen_modules):
error_header = "rn_codegen(name=\"{}\")".format(name)
if not native_module_spec_name:
fail("{}: When codegen_modules = True, native_module_spec_name must be specified.".format(error_header))

if not android_package_name:
fail("{}: When codegen_modules = True, android_package_name must be specified.".format(error_header))

spec_srcs = native.glob(
[
"**/Native*.js",
],
exclude = [
"**/__*__/**",
],
)

module_schema_target = "{}-codegen-modules-schema".format(native_module_spec_name)

fb_native.genrule(
name = module_schema_target,
srcs = spec_srcs,
cmd = "$(exe {}) $OUT $SRCS".format(react_native_root_target("packages/react-native-codegen:write_to_json")),
out = "schema-{}.json".format(native_module_spec_name),
labels = ["codegen_rule", "react_native_schema_target"],
)

rn_codegen_modules(
name = native_module_spec_name,
android_package_name = android_package_name,
schema_target = ":{}".format(module_schema_target),
library_labels = library_labels,
)

if (codegen_components):
fb_native.genrule(
name = "codegen_rn_components_schema_{}".format(name),
srcs = native.glob(
[
"**/*NativeComponent.js",
],
exclude = [
"**/__*__/**",
],
),
cmd = "$(exe {}) $OUT $SRCS".format(react_native_root_target("packages/react-native-codegen:write_to_json")),
out = "schema-{}.json".format(name),
labels = ["codegen_rule"],
)

rn_codegen_components(
name = name,
schema_target = ":codegen_rn_components_schema_{}".format(name),
library_labels = library_labels,
)

0 comments on commit e1eb938

Please sign in to comment.