mirror of
https://github.com/react/react-native.git
synced 2026-09-28 13:23:09 +08:00
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
51 lines
1.1 KiB
JavaScript
51 lines
1.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
|
|
*/
|
|
|
|
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,
|
|
};
|