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
@@ -17,6 +17,11 @@ exports[`GenerateModuleObjCpp can generate a header file NativeModule specs 1`]
#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 RNCodegenModuleFixtures symbols
#ifndef RNCodegenModuleFixtures_H
#define RNCodegenModuleFixtures_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -1341,6 +1346,7 @@ inline JS::NativeSampleTurboModuleOptional::Constants::Builder::Builder(Constant
return i.unsafeRawValue();
}) {}
#endif // RNCodegenModuleFixtures_H
"
`;
@@ -1361,6 +1367,11 @@ exports[`GenerateModuleObjCpp can generate a header file NativeModule specs with
#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 RNCodegenModuleFixtures symbols
#ifndef RNCodegenModuleFixtures_H
#define RNCodegenModuleFixtures_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -2688,6 +2699,7 @@ inline JS::NativeSampleTurboModuleOptional::Constants::Builder::Builder(Constant
}) {}
NS_ASSUME_NONNULL_END
#endif // RNCodegenModuleFixtures_H
"
`;
@@ -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,
@@ -18,6 +18,11 @@ Map {
#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 SampleWithUppercaseName symbols
#ifndef SampleWithUppercaseName_H
#define SampleWithUppercaseName_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -45,6 +50,7 @@ namespace facebook::react {
};
} // namespace facebook::react
#endif // SampleWithUppercaseName_H
",
}
`;
@@ -67,6 +73,11 @@ Map {
#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 complex_objects symbols
#ifndef complex_objects_H
#define complex_objects_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -304,6 +315,7 @@ inline facebook::react::LazyVector<JS::NativeSampleTurboModule::SpecGetArraysOpt
id const p = _v[@\\"arrayOfObjects\\"];
return RCTBridgingToVec(p, ^JS::NativeSampleTurboModule::SpecGetArraysOptionsArrayOfObjectsElement(id itemValue_0) { return JS::NativeSampleTurboModule::SpecGetArraysOptionsArrayOfObjectsElement(itemValue_0); });
}
#endif // complex_objects_H
",
}
`;
@@ -326,6 +338,11 @@ Map {
#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 cxx_only_native_modules symbols
#ifndef cxx_only_native_modules_H
#define cxx_only_native_modules_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -339,6 +356,7 @@ Map {
#endif // cxx_only_native_modules_H
",
}
`;
@@ -361,6 +379,11 @@ Map {
#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 empty_native_modules symbols
#ifndef empty_native_modules_H
#define empty_native_modules_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -388,6 +411,7 @@ namespace facebook::react {
};
} // namespace facebook::react
#endif // empty_native_modules_H
",
}
`;
@@ -410,6 +434,11 @@ Map {
#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 native_modules_with_type_aliases symbols
#ifndef native_modules_with_type_aliases_H
#define native_modules_with_type_aliases_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -557,6 +586,7 @@ inline std::optional<bool> JS::AliasTurboModule::Options::allowExternalStorage()
id const p = _v[@\\"allowExternalStorage\\"];
return RCTBridgingToOptionalBool(p);
}
#endif // native_modules_with_type_aliases_H
",
}
`;
@@ -579,6 +609,11 @@ Map {
#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 real_module_example symbols
#ifndef real_module_example_H
#define real_module_example_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -799,6 +834,7 @@ inline id<NSObject> _Nullable JS::NativeExceptionsManager::ExceptionData::extraD
id const p = _v[@\\"extraData\\"];
return p;
}
#endif // real_module_example_H
",
}
`;
@@ -821,6 +857,11 @@ Map {
#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 simple_native_modules symbols
#ifndef simple_native_modules_H
#define simple_native_modules_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -910,6 +951,7 @@ inline JS::NativeSampleTurboModule::Constants::Builder::Builder(const Input i) :
inline JS::NativeSampleTurboModule::Constants::Builder::Builder(Constants i) : _factory(^{
return i.unsafeRawValue();
}) {}
#endif // simple_native_modules_H
",
}
`;
@@ -932,6 +974,11 @@ Map {
#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 two_modules_different_files symbols
#ifndef two_modules_different_files_H
#define two_modules_different_files_H
#import <Foundation/Foundation.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
@@ -975,6 +1022,7 @@ namespace facebook::react {
} // namespace facebook::react
#endif // two_modules_different_files_H
",
}
`;