Fix the Redefinition of 'NativeXXXSpecJSI' error with Frameworks (#44005)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44005

When using frameworks on iOS, there is a possibility that modules import the Spec.h file twice and this might end up in a Redefinition of some symbols and duplication of symbols which ends up in build errors, as reported here: https://github.com/facebook/react-native/issues/42670.

This change adds some [`#include guards`](https://en.wikipedia.org/wiki/Include_guard) in codegen to avoid the redefinition of those symbols if the header is imported/included multiple times.

Note: I also experimented with `#pragma once`, but it looks like Apple is not happy with that directive. [It seems](https://forums.developer.apple.com/forums/thread/739964) that it started working flakely from Xcode 15.

## Changelog:
[General][Fixed] - Make sure that we can't include Codegen symbols multiple times

Reviewed By: cortinico

Differential Revision: D55925605

fbshipit-source-id: 15ca076aace2ffbd03ab8fa8a68a3d8ce0d1ea65
This commit is contained in:
Riccardo Cipolleschi
2024-04-11 11:39:43 -07:00
committed by Facebook GitHub Bot
parent 9433506465
commit 46b6453eb6
3 changed files with 82 additions and 7 deletions
@@ -46,15 +46,20 @@ namespace facebook::react {
} // namespace facebook::react`;
const HeaderFileTemplate = ({
headerFileName,
moduleDeclarations,
structInlineMethods,
assumeNonnull,
}: $ReadOnly<{
headerFileName: string,
moduleDeclarations: string,
structInlineMethods: string,
assumeNonnull: boolean,
}>) =>
`/**
}>) => {
const headerFileNameWithNoExt = headerFileName.replace(/\.h$/, '');
return (
`/**
* 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
@@ -70,6 +75,11 @@ const HeaderFileTemplate = ({
#ifndef __cplusplus
#error This file must be compiled as Obj-C++. If you are importing it, you must change your file extension to .mm.
#endif
// Avoid multiple includes of ${headerFileNameWithNoExt} symbols
#ifndef ${headerFileNameWithNoExt}_H
#define ${headerFileNameWithNoExt}_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -82,11 +92,15 @@ const HeaderFileTemplate = ({
#import <vector>
` +
(assumeNonnull ? '\nNS_ASSUME_NONNULL_BEGIN\n' : '') +
moduleDeclarations +
'\n' +
structInlineMethods +
(assumeNonnull ? '\nNS_ASSUME_NONNULL_END\n' : '\n');
(assumeNonnull ? '\nNS_ASSUME_NONNULL_BEGIN\n' : '') +
moduleDeclarations +
'\n' +
structInlineMethods +
(assumeNonnull ? '\nNS_ASSUME_NONNULL_END\n' : '\n') +
`#endif // ${headerFileNameWithNoExt}_H` +
'\n'
);
};
const SourceFileTemplate = ({
headerFileName,
@@ -197,6 +211,7 @@ module.exports = {
const headerFileName = `${libraryName}.h`;
const headerFile = HeaderFileTemplate({
headerFileName,
moduleDeclarations: moduleDeclarations.join('\n'),
structInlineMethods: structInlineMethods.join('\n'),
assumeNonnull,