Do not store .cpp/.h files inside src/main/java - hermes modules (#34420)

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

Currently we expose native code (.h, .cpp) inside the src/main/java folder.

This is making impossible for users on New Architecture to open the project
inside Android Studio.

The problem is that the src/main/java is reserved to Java/Kotlin sources only.
AGP 7.2 also removed support for mixed source roots:
https://developer.android.com/studio/releases/gradle-plugin#duplicate-content-roots

This is essentially forcing users to write Java code without any autocompletion
as all the React Native Java classes are considered C++ files.

I'm addressing this issue folder by folder by moving them
from ReactAndroid/src/main/java/com/facebook/... to ReactAndroid/src/main/jni/react/...

This is the diff for hermes/reactexecutor and hermes/instrumentation

Changelog:
[Internal] [Changed] - Do not store .cpp/.h files inside src/main/java - hermes modules

Reviewed By: yungsters

Differential Revision: D38700760

fbshipit-source-id: 50cf38a0dae4f617e6d78317e5fe2a858290d0c0
This commit is contained in:
Nicola Corti
2022-08-15 09:12:08 -07:00
committed by Facebook GitHub Bot
parent 333ba5967e
commit 5d34c55523
13 changed files with 54 additions and 54 deletions
@@ -1,5 +0,0 @@
---
Checks: '>
clang-diagnostic-*,
'
...
@@ -1,4 +1,4 @@
load("//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "FBJNI_TARGET", "react_native_dep", "react_native_target", "react_native_xplat_dep", "rn_android_library", "rn_xplat_cxx_library")
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")
rn_android_library(
name = "instrumentation",
@@ -24,33 +24,6 @@ rn_android_library(
react_native_dep("java/com/facebook/proguard/annotations:annotations"),
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
react_native_dep("libraries/fbjni:java"),
":jni_hermes_samplingprofiler",
],
)
rn_xplat_cxx_library(
name = "jni_hermes_samplingprofiler",
srcs = [
"HermesSamplingProfiler.cpp",
"OnLoad.cpp",
],
headers = ["HermesSamplingProfiler.h"],
header_namespace = "",
compiler_flags_enable_exceptions = True, # TODO: is this necessary?
compiler_flags_enable_rtti = True, # TODO: is this necessary?
fbandroid_allow_jni_merging = True,
labels = [
"pfh:ReactNative_CommonInfrastructurePlaceholder",
"supermodule:xplat/default/public.react_native.infra",
],
platforms = ANDROID,
soname = "libjsijniprofiler.$(ext)",
visibility = [
react_native_dep("java/com/facebook/jsi:jsi"),
],
deps = [
react_native_target("jni/react/jni:jni"),
FBJNI_TARGET,
react_native_xplat_dep("hermes/API:HermesAPI"),
react_native_target("jni/react/hermes/instrumentation:jni_hermes_samplingprofiler"),
],
)
@@ -1,31 +0,0 @@
# 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_RECURSE jsijniprofiler_SRC CONFIGURE_DEPENDS *.cpp)
add_library(
jsijniprofiler
SHARED
${jsijniprofiler_SRC}
)
target_compile_options(
jsijniprofiler
PRIVATE
-fexceptions
)
target_include_directories(jsijniprofiler PRIVATE .)
target_link_libraries(
jsijniprofiler
fb
fbjni
jsireact
folly_runtime
hermes-engine::libhermes
jsi
reactnativejni
)
@@ -1,49 +0,0 @@
/*
* 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.
*/
#include <fbjni/fbjni.h>
#include <string>
namespace facebook {
namespace jsi {
namespace jni {
namespace jni = ::facebook::jni;
class HermesMemoryDumper : public jni::JavaClass<HermesMemoryDumper> {
public:
constexpr static auto kJavaDescriptor =
"Lcom/facebook/hermes/instrumentation/HermesMemoryDumper;";
bool shouldSaveSnapshot() {
static auto shouldSaveSnapshotMethod =
javaClassStatic()->getMethod<jboolean()>("shouldSaveSnapshot");
return shouldSaveSnapshotMethod(self());
}
std::string getInternalStorage() {
static auto getInternalStorageMethod =
javaClassStatic()->getMethod<jstring()>("getInternalStorage");
return getInternalStorageMethod(self())->toStdString();
}
std::string getId() {
static auto getInternalStorageMethod =
javaClassStatic()->getMethod<jstring()>("getId");
return getInternalStorageMethod(self())->toStdString();
}
void setMetaData(std::string crashId) {
static auto getIdMethod =
javaClassStatic()->getMethod<void(std::string)>("setMetaData");
getIdMethod(self(), crashId);
}
};
} // namespace jni
} // namespace jsi
} // namespace facebook
@@ -1,42 +0,0 @@
/*
* 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.
*/
#include "HermesSamplingProfiler.h"
#include <hermes/hermes.h>
namespace facebook {
namespace jsi {
namespace jni {
void HermesSamplingProfiler::enable(jni::alias_ref<jclass>) {
hermes::HermesRuntime::enableSamplingProfiler();
}
void HermesSamplingProfiler::disable(jni::alias_ref<jclass>) {
hermes::HermesRuntime::disableSamplingProfiler();
}
void HermesSamplingProfiler::dumpSampledTraceToFile(
jni::alias_ref<jclass>,
std::string filename) {
hermes::HermesRuntime::dumpSampledTraceToFile(filename);
}
void HermesSamplingProfiler::registerNatives() {
javaClassLocal()->registerNatives({
makeNativeMethod("enable", HermesSamplingProfiler::enable),
makeNativeMethod("disable", HermesSamplingProfiler::enable),
makeNativeMethod(
"dumpSampledTraceToFile",
HermesSamplingProfiler::dumpSampledTraceToFile),
});
}
} // namespace jni
} // namespace jsi
} // namespace facebook
@@ -1,40 +0,0 @@
/*
* 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.
*/
#ifndef HERMESSAMPLINGPROFILER_H_
#define HERMESSAMPLINGPROFILER_H_
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
namespace facebook {
namespace jsi {
namespace jni {
namespace jni = ::facebook::jni;
class HermesSamplingProfiler : public jni::JavaClass<HermesSamplingProfiler> {
public:
constexpr static auto kJavaDescriptor =
"Lcom/facebook/hermes/instrumentation/HermesSamplingProfiler;";
static void enable(jni::alias_ref<jclass>);
static void disable(jni::alias_ref<jclass>);
static void dumpSampledTraceToFile(
jni::alias_ref<jclass>,
std::string filename);
static void registerNatives();
private:
HermesSamplingProfiler();
};
} // namespace jni
} // namespace jsi
} // namespace facebook
#endif /* HERMESSAMPLINGPROFILER_H_ */
@@ -1,14 +0,0 @@
/*
* 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.
*/
#include "HermesSamplingProfiler.h"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return facebook::jni::initialize(vm, [] {
facebook::jsi::jni::HermesSamplingProfiler::registerNatives();
});
}
@@ -1,4 +1,4 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library", "rn_xplat_cxx_library")
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")
rn_android_library(
name = "reactexecutor",
@@ -17,7 +17,7 @@ rn_android_library(
react_native_target("java/com/facebook/hermes/instrumentation:instrumentation"),
react_native_target("java/com/facebook/hermes/instrumentation:hermes_samplingprofiler"),
react_native_target("java/com/facebook/react/bridge:bridge"),
":jni",
react_native_target("jni/react/hermes/reactexecutor:jni"),
":runtimeconfig",
],
)
@@ -37,23 +37,3 @@ rn_android_library(
react_native_target("java/com/facebook/hermes/instrumentation:instrumentation"),
],
)
rn_xplat_cxx_library(
name = "jni-interface",
srcs = [],
header_namespace = "hermes/reactexecutor",
exported_headers = glob(["*.h"]),
visibility = ["PUBLIC"],
)
rn_xplat_cxx_library(
name = "jni",
srcs = ["OnLoad.cpp"],
headers = [],
header_namespace = "",
compiler_flags = ["-fexceptions"],
soname = "libhermes-executor.$(ext)",
visibility = [
react_native_target("java/com/facebook/hermes/reactexecutor:reactexecutor"),
],
)
@@ -1,41 +0,0 @@
# 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_RECURSE hermes_executor_SRC CONFIGURE_DEPENDS *.cpp)
if(${CMAKE_BUILD_TYPE} MATCHES Debug)
set(HERMES_TARGET_SUFFIX debug)
else()
set(HERMES_TARGET_SUFFIX release)
endif()
set(HERMES_TARGET_NAME hermes-executor-${HERMES_TARGET_SUFFIX})
add_library(
${HERMES_TARGET_NAME}
SHARED
${hermes_executor_SRC}
)
target_compile_options(
${HERMES_TARGET_NAME}
PRIVATE
$<$<CONFIG:Debug>:-DHERMES_ENABLE_DEBUGGER=1>
-fexceptions
)
target_include_directories(${HERMES_TARGET_NAME} PRIVATE .)
target_link_libraries(
${HERMES_TARGET_NAME}
hermes-executor-common
jsireact
fb
fbjni
folly_runtime
hermes-engine::libhermes
jsi
reactnativejni
)
@@ -1,118 +0,0 @@
/*
* 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.
*/
#include <../instrumentation/HermesMemoryDumper.h>
#include <HermesExecutorFactory.h>
#include <android/log.h>
#include <fbjni/fbjni.h>
#include <glog/logging.h>
#include <hermes/Public/GCConfig.h>
#include <hermes/Public/RuntimeConfig.h>
#include <jni.h>
#include <react/jni/JReactMarker.h>
#include <react/jni/JSLogging.h>
#include <react/jni/JavaScriptExecutorHolder.h>
#include <react/jni/NativeTime.h>
#include <memory>
namespace facebook {
namespace react {
static void hermesFatalHandler(const std::string &reason) {
LOG(ERROR) << "Hermes Fatal: " << reason << "\n";
__android_log_assert(nullptr, "Hermes", "%s", reason.c_str());
}
static std::once_flag flag;
static ::hermes::vm::RuntimeConfig makeRuntimeConfig(jlong heapSizeMB) {
namespace vm = ::hermes::vm;
auto gcConfigBuilder =
vm::GCConfig::Builder()
.withName("RN")
// For the next two arguments: avoid GC before TTI by initializing the
// runtime to allocate directly in the old generation, but revert to
// normal operation when we reach the (first) TTI point.
.withAllocInYoung(false)
.withRevertToYGAtTTI(true);
if (heapSizeMB > 0) {
gcConfigBuilder.withMaxHeapSize(heapSizeMB << 20);
}
return vm::RuntimeConfig::Builder()
.withGCConfig(gcConfigBuilder.build())
.withEnableSampleProfiling(true)
.build();
}
static void installBindings(jsi::Runtime &runtime) {
react::Logger androidLogger =
static_cast<void (*)(const std::string &, unsigned int)>(
&reactAndroidLoggingHook);
react::bindNativeLogger(runtime, androidLogger);
react::PerformanceNow androidNativePerformanceNow =
static_cast<double (*)()>(&reactAndroidNativePerformanceNowHook);
react::bindNativePerformanceNow(runtime, androidNativePerformanceNow);
}
class HermesExecutorHolder
: public jni::HybridClass<HermesExecutorHolder, JavaScriptExecutorHolder> {
public:
static constexpr auto kJavaDescriptor =
"Lcom/facebook/hermes/reactexecutor/HermesExecutor;";
static jni::local_ref<jhybriddata> initHybridDefaultConfig(
jni::alias_ref<jclass>) {
JReactMarker::setLogPerfMarkerIfNeeded();
std::call_once(flag, []() {
facebook::hermes::HermesRuntime::setFatalHandler(hermesFatalHandler);
});
return makeCxxInstance(
std::make_unique<HermesExecutorFactory>(installBindings));
}
static jni::local_ref<jhybriddata> initHybrid(
jni::alias_ref<jclass>,
jlong heapSizeMB) {
JReactMarker::setLogPerfMarkerIfNeeded();
auto runtimeConfig = makeRuntimeConfig(heapSizeMB);
std::call_once(flag, []() {
facebook::hermes::HermesRuntime::setFatalHandler(hermesFatalHandler);
});
return makeCxxInstance(std::make_unique<HermesExecutorFactory>(
installBindings, JSIExecutor::defaultTimeoutInvoker, runtimeConfig));
}
static bool canLoadFile(jni::alias_ref<jclass>, const std::string &path) {
return true;
}
static void registerNatives() {
registerHybrid(
{makeNativeMethod("initHybrid", HermesExecutorHolder::initHybrid),
makeNativeMethod(
"initHybridDefaultConfig",
HermesExecutorHolder::initHybridDefaultConfig),
makeNativeMethod("canLoadFile", HermesExecutorHolder::canLoadFile)});
}
private:
friend HybridBase;
using HybridBase::HybridBase;
};
} // namespace react
} // namespace facebook
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return facebook::jni::initialize(
vm, [] { facebook::react::HermesExecutorHolder::registerNatives(); });
}