Files
react-native/packages/react-native-codegen/src/generators/modules/GenerateModuleJniH.js
T
Nick Gerleman 17154a661f C++ 20 all the things (#39454)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39454

1. Build with C++ 20 everywhere.
2. Update folly in OSS build to avoid a warning, and let us opt out of heavy bits required for supporting coroutines (we are still more than a year behind).
3. Update the folly podspec for the new version of folly
4. Update the many, many, copy/pasted header search paths for folly dependencies to be able to resolve fmt, now exposed to folly headers
4. Replaces a deprecated atomic usage with equivalent.
5. Fixes a deprecated implicit capturing `this` by value in lambda (same behavior, added a TODO because it seems sus)

Changelog: [Internal]

Reviewed By: sammy-SC

Differential Revision: D49271685

fbshipit-source-id: 16610f7bcd1032d5796cc11f1cfa92a33d8c2593
2023-09-15 01:17:55 -07:00

149 lines
3.6 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';
type FilesOutput = Map<string, string>;
const {getModules} = require('./Utils');
const ModuleClassDeclarationTemplate = ({
hasteModuleName,
}: $ReadOnly<{hasteModuleName: string}>) => {
return `/**
* JNI C++ class for module '${hasteModuleName}'
*/
class JSI_EXPORT ${hasteModuleName}SpecJSI : public JavaTurboModule {
public:
${hasteModuleName}SpecJSI(const JavaTurboModule::InitParams &params);
};
`;
};
const HeaderFileTemplate = ({
modules,
libraryName,
}: $ReadOnly<{modules: string, libraryName: string}>) => {
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
* once the code is regenerated.
*
* ${'@'}generated by codegen project: GenerateModuleJniH.js
*/
#pragma once
#include <ReactCommon/JavaTurboModule.h>
#include <ReactCommon/TurboModule.h>
#include <jsi/jsi.h>
namespace facebook {
namespace react {
${modules}
JSI_EXPORT
std::shared_ptr<TurboModule> ${libraryName}_ModuleProvider(const std::string &moduleName, const JavaTurboModule::InitParams &params);
} // namespace react
} // namespace facebook
`;
};
// Note: this CMakeLists.txt template includes dependencies for both NativeModule and components.
const CMakeListsTemplate = ({
libraryName,
}: $ReadOnly<{libraryName: string}>) => {
return `# 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.
cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)
file(GLOB react_codegen_SRCS CONFIGURE_DEPENDS *.cpp react/renderer/components/${libraryName}/*.cpp)
add_library(
react_codegen_${libraryName}
SHARED
\${react_codegen_SRCS}
)
target_include_directories(react_codegen_${libraryName} PUBLIC . react/renderer/components/${libraryName})
target_link_libraries(
react_codegen_${libraryName}
fbjni
folly_runtime
glog
jsi
${libraryName !== 'rncore' ? 'react_codegen_rncore' : ''}
react_debug
react_nativemodule_core
react_render_core
react_render_debug
react_render_graphics
react_render_imagemanager
rrc_image
rrc_view
turbomodulejsijni
yoga
)
target_compile_options(
react_codegen_${libraryName}
PRIVATE
-DLOG_TAG=\\"ReactNative\\"
-fexceptions
-frtti
-std=c++20
-Wall
)
`;
};
module.exports = {
generate(
libraryName: string,
schema: SchemaType,
packageName?: string,
assumeNonnull: boolean = false,
): FilesOutput {
const nativeModules = getModules(schema);
const modules = Object.keys(nativeModules)
.filter(hasteModuleName => {
const module = nativeModules[hasteModuleName];
return !(
module.excludedPlatforms != null &&
module.excludedPlatforms.includes('android')
);
})
.sort()
.map(hasteModuleName => ModuleClassDeclarationTemplate({hasteModuleName}))
.join('\n');
const fileName = `${libraryName}.h`;
const replacedTemplate = HeaderFileTemplate({
modules: modules,
libraryName: libraryName.replace(/-/g, '_'),
});
return new Map([
[`jni/${fileName}`, replacedTemplate],
['jni/CMakeLists.txt', CMakeListsTemplate({libraryName: libraryName})],
]);
},
};