Files
react-native/packages/react-native-codegen/src/generators/components/GenerateThirdPartyFabricComponentsProviderH.js
T
Oskar Kwaśniewski ebb2b9c3fc 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:

![CleanShot 2023-12-22 at 15 48 22@2x](https://github.com/facebook/react-native/assets/52801365/0cdfe7f5-441d-4466-8713-5f65feef26e7)

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
2024-01-23 06:53:57 -08:00

127 lines
3.1 KiB
JavaScript

/**
* 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
*/
'use strict';
import type {SchemaType} from '../../CodegenSchema';
const {
generateSupportedApplePlatformsMacro,
} = require('./ComponentsProviderUtils');
// File path -> contents
type FilesOutput = Map<string, string>;
const FileTemplate = ({lookupFuncs}: {lookupFuncs: string}) => `
/*
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* ${'@'}generated by GenerateRCTThirdPartyFabricComponentsProviderH
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type-c-linkage"
#import <React/RCTComponentViewProtocol.h>
#ifdef __cplusplus
extern "C" {
#endif
Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char *name);
#if RCT_NEW_ARCH_ENABLED
#ifndef RCT_DYNAMIC_FRAMEWORKS
${lookupFuncs}
#endif
#endif
#ifdef __cplusplus
}
#endif
#pragma GCC diagnostic pop
`;
const LookupFuncTemplate = ({
className,
libraryName,
}: {
className: string,
libraryName: string,
}) =>
`
Class<RCTComponentViewProtocol> ${className}Cls(void) __attribute__((used)); // ${libraryName}
`.trim();
module.exports = {
generate(
schemas: {[string]: SchemaType},
supportedApplePlatforms?: {[string]: {[string]: boolean}},
): FilesOutput {
const fileName = 'RCTThirdPartyFabricComponentsProvider.h';
const lookupFuncs = Object.keys(schemas)
.map(libraryName => {
const schema = schemas[libraryName];
const librarySupportedApplePlatforms =
supportedApplePlatforms?.[libraryName];
const generatedLookup = Object.keys(schema.modules)
.map(moduleName => {
const module = schema.modules[moduleName];
if (module.type !== 'Component') {
return;
}
const {components} = module;
// No components in this module
if (components == null) {
return null;
}
return Object.keys(components)
.filter(componentName => {
const component = components[componentName];
return !(
component.excludedPlatforms &&
component.excludedPlatforms.includes('iOS')
);
})
.map(componentName => {
return LookupFuncTemplate({
className: componentName,
libraryName,
});
})
.join('\n');
})
.filter(Boolean)
.join('\n');
return generateSupportedApplePlatformsMacro(
generatedLookup,
librarySupportedApplePlatforms,
);
})
.join('\n');
const replacedTemplate = FileTemplate({
lookupFuncs,
});
return new Map([[fileName, replacedTemplate]]);
},
};