From e1eb938c9de40288695f794672c11e7e2be720d0 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 15 Apr 2021 13:25:16 -0700 Subject: [PATCH] Create rn_codegen BUCK macro (#31340) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/31340 Changelog: [Internal] Reviewed By: yungsters Differential Revision: D27669950 fbshipit-source-id: 3129d87da8f74d896c0b9e82799b8896b3ed7635 --- Libraries/BUCK | 46 +++------------- tools/build_defs/oss/rn_codegen_defs.bzl | 68 ++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 39 deletions(-) diff --git a/Libraries/BUCK b/Libraries/BUCK index 586510279a0321..389a6e37799081 100644 --- a/Libraries/BUCK +++ b/Libraries/BUCK @@ -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", ) diff --git a/tools/build_defs/oss/rn_codegen_defs.bzl b/tools/build_defs/oss/rn_codegen_defs.bzl index 07d3b05ed6d45a..4a2a3eff671f0e 100644 --- a/tools/build_defs/oss/rn_codegen_defs.bzl +++ b/tools/build_defs/oss/rn_codegen_defs.bzl @@ -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, + )