-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #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
- Loading branch information
1 parent
a13d51f
commit ebb2b9c
Showing
9 changed files
with
235 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/react-native-codegen/src/generators/components/ComponentsProviderUtils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ages/react-native/scripts/codegen/__test_fixtures__/test-library-2/test-library-2.podspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 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. | ||
|
||
Pod::Spec.new do |s| | ||
s.name = "test-library-2" | ||
s.version = "0.0.0" | ||
s.ios.deployment_target = "9.0" | ||
s.osx.deployment_target = "13.0" | ||
s.tvos.deployment_target = "1.0" | ||
end |
10 changes: 10 additions & 0 deletions
10
packages/react-native/scripts/codegen/__test_fixtures__/test-library/test-library.podspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# 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. | ||
|
||
Pod::Spec.new do |s| | ||
s.name = "test-library" | ||
s.version = "0.0.0" | ||
s.platforms = { :ios => "9.0", :osx => "13.0", visionos: "1.0" } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters