mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
feat: make codegen take OOT Apple platforms into account (#42047)
Summary: ### The problem 1. We have a library that's supported on iOS but doesn't have support for visionOS. 2. We run pod install 3. Codegen runs and generates Code for this library and tries to reference library class in `RCTThirdPartyFabricComponentsProvider` 4. Example: ```objc Class<RCTComponentViewProtocol> RNCSafeAreaProviderCls(void) __attribute__((used)); // 0 ``` This is an issue because the library files are not linked for visionOS platform (because code is linked only for iOS due to pod supporting only iOS). ### Solution Make codegen take Apple OOT platforms into account by adding compiler macros if the given platform doesn't explicitly support this platform in the native package's podspec file. Example generated output for library supporting only `ios` and `visionos` in podspec:  I used compiler conditionals because not every platform works the same, and if in the future let's say react-native-visionos were merged upstream compiler conditionals would still work. Also tvOS uses Xcode targets to differentiate which platform it builds so conditionally adding things to the generated file wouldn't work. ## Changelog: [IOS] [ADDED] - make codegen take OOT Apple platforms into account Pull Request resolved: https://github.com/facebook/react-native/pull/42047 Test Plan: 1. Generate a sample app with a template 5. Add third-party library (In my case it was https://github.com/callstack/react-native-slider) 6. Check if generated codegen code includes compiler macros Reviewed By: cipolleschi Differential Revision: D52656076 Pulled By: dmytrorykun fbshipit-source-id: c827f358997c70a3c49f80c55915c28bdab9b97f
This commit is contained in:
1 parent
a13d51ff1c
commit
ebb2b9c3fc
9 files changed
+235
-19
No files matched your search
+50
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict
|
||||
* @format
|
||||
*/
|
||||
|
||||
const APPLE_PLATFORMS_MACRO_MAP = {
|
||||
ios: 'TARGET_OS_IOS',
|
||||
macos: 'TARGET_OS_OSX',
|
||||
tvos: 'TARGET_OS_TV',
|
||||
visionos: 'TARGET_OS_VISION',
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds compiler macros to the file template to exclude unsupported platforms.
|
||||
*/
|
||||
function generateSupportedApplePlatformsMacro(
|
||||
fileTemplate: string,
|
||||
supportedPlatformsMap: ?{[string]: boolean},
|
||||
): string {
|
||||
if (!supportedPlatformsMap) {
|
||||
return fileTemplate;
|
||||
}
|
||||
|
||||
const compilerMacroString = Object.keys(supportedPlatformsMap)
|
||||
.reduce((acc: string[], platform) => {
|
||||
if (!supportedPlatformsMap[platform]) {
|
||||
return [...acc, `!${APPLE_PLATFORMS_MACRO_MAP[platform]}`];
|
||||
}
|
||||
return acc;
|
||||
}, [])
|
||||
.join(' && ');
|
||||
|
||||
if (!compilerMacroString) {
|
||||
return fileTemplate;
|
||||
}
|
||||
|
||||
return `#if ${compilerMacroString}
|
||||
${fileTemplate}
|
||||
#endif
|
||||
`;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateSupportedApplePlatformsMacro,
|
||||
};
|
||||
+16
-2
@@ -12,6 +12,10 @@
|
||||
|
||||
import type {SchemaType} from '../../CodegenSchema';
|
||||
|
||||
const {
|
||||
generateSupportedApplePlatformsMacro,
|
||||
} = require('./ComponentsProviderUtils');
|
||||
|
||||
// File path -> contents
|
||||
type FilesOutput = Map<string, string>;
|
||||
|
||||
@@ -63,13 +67,18 @@ Class<RCTComponentViewProtocol> ${className}Cls(void) __attribute__((used)); //
|
||||
`.trim();
|
||||
|
||||
module.exports = {
|
||||
generate(schemas: {[string]: SchemaType}): FilesOutput {
|
||||
generate(
|
||||
schemas: {[string]: SchemaType},
|
||||
supportedApplePlatforms?: {[string]: {[string]: boolean}},
|
||||
): FilesOutput {
|
||||
const fileName = 'RCTThirdPartyFabricComponentsProvider.h';
|
||||
|
||||
const lookupFuncs = Object.keys(schemas)
|
||||
.map(libraryName => {
|
||||
const schema = schemas[libraryName];
|
||||
return Object.keys(schema.modules)
|
||||
const librarySupportedApplePlatforms =
|
||||
supportedApplePlatforms?.[libraryName];
|
||||
const generatedLookup = Object.keys(schema.modules)
|
||||
.map(moduleName => {
|
||||
const module = schema.modules[moduleName];
|
||||
if (module.type !== 'Component') {
|
||||
@@ -100,6 +109,11 @@ module.exports = {
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join('\n');
|
||||
|
||||
return generateSupportedApplePlatformsMacro(
|
||||
generatedLookup,
|
||||
librarySupportedApplePlatforms,
|
||||
);
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
|
||||
+19
-3
@@ -12,6 +12,10 @@
|
||||
|
||||
import type {SchemaType} from '../../CodegenSchema';
|
||||
|
||||
const {
|
||||
generateSupportedApplePlatformsMacro,
|
||||
} = require('./ComponentsProviderUtils');
|
||||
|
||||
// File path -> contents
|
||||
type FilesOutput = Map<string, string>;
|
||||
|
||||
@@ -60,13 +64,19 @@ const LookupMapTemplate = ({
|
||||
{"${className}", ${className}Cls}, // ${libraryName}`;
|
||||
|
||||
module.exports = {
|
||||
generate(schemas: {[string]: SchemaType}): FilesOutput {
|
||||
generate(
|
||||
schemas: {[string]: SchemaType},
|
||||
supportedApplePlatforms?: {[string]: {[string]: boolean}},
|
||||
): FilesOutput {
|
||||
const fileName = 'RCTThirdPartyFabricComponentsProvider.mm';
|
||||
|
||||
const lookupMap = Object.keys(schemas)
|
||||
.map(libraryName => {
|
||||
const schema = schemas[libraryName];
|
||||
return Object.keys(schema.modules)
|
||||
const librarySupportedApplePlatforms =
|
||||
supportedApplePlatforms?.[libraryName];
|
||||
|
||||
const generatedLookup = Object.keys(schema.modules)
|
||||
.map(moduleName => {
|
||||
const module = schema.modules[moduleName];
|
||||
if (module.type !== 'Component') {
|
||||
@@ -98,7 +108,13 @@ module.exports = {
|
||||
|
||||
return componentTemplates.length > 0 ? componentTemplates : null;
|
||||
})
|
||||
.filter(Boolean);
|
||||
.filter(Boolean)
|
||||
.join('\n');
|
||||
|
||||
return generateSupportedApplePlatformsMacro(
|
||||
generatedLookup,
|
||||
librarySupportedApplePlatforms,
|
||||
);
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
|
||||
+2
-1
@@ -71,7 +71,8 @@ Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char
|
||||
{\\"MultiComponent1NativeComponent\\", MultiComponent1NativeComponentCls}, // TWO_COMPONENTS_SAME_FILE,
|
||||
{\\"MultiComponent2NativeComponent\\", MultiComponent2NativeComponentCls}, // TWO_COMPONENTS_SAME_FILE
|
||||
|
||||
{\\"MultiFile1NativeComponent\\", MultiFile1NativeComponentCls}, // TWO_COMPONENTS_DIFFERENT_FILES,
|
||||
{\\"MultiFile1NativeComponent\\", MultiFile1NativeComponentCls}, // TWO_COMPONENTS_DIFFERENT_FILES
|
||||
|
||||
{\\"MultiFile2NativeComponent\\", MultiFile2NativeComponentCls}, // TWO_COMPONENTS_DIFFERENT_FILES
|
||||
|
||||
{\\"CommandNativeComponent\\", CommandNativeComponentCls}, // COMMANDS
|
||||
|
||||
Reference in new issue
Block a user