Skip to content

Commit e3830dd

Browse files
Andrei Shikovfacebook-github-bot
Andrei Shikov
authored andcommitted
CMake setup for ReactAndroid (#33472)
Summary: Pull Request resolved: #33472 Changes native build of ReactAndroid to CMake instead of ndk-build. Removes a few workarounds around AGP issues with ndk-build which seems to be working with CMake by default. Changelog: [Changed][Android] - Use CMake to build ReactAndroid module Reviewed By: cortinico Differential Revision: D35018803 fbshipit-source-id: af477937ed70a5ddfafef4e6260a397ee9911580
1 parent 1907bd3 commit e3830dd

File tree

5 files changed

+174
-42
lines changed

5 files changed

+174
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ project.xcworkspace
3636
/ReactAndroid/gradlew
3737
/ReactAndroid/gradlew.bat
3838
/ReactAndroid/hermes-engine/build/
39+
/ReactAndroid/hermes-engine/.cxx/
3940
/template/android/app/build/
4041
/template/android/build/
4142

ReactAndroid/build.gradle

+34-40
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,6 @@ def reactNativeArchitectures() {
215215
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
216216
}
217217

218-
def ndkBuildJobs() {
219-
return project.findProperty("jobs") ?: Runtime.runtime.availableProcessors()
220-
}
221-
222218
tasks.register("packageReactNdkLibsForBuck") {
223219
dependsOn("packageReactNdkDebugLibsForBuck")
224220
}
@@ -286,29 +282,48 @@ android {
286282
testInstrumentationRunner("androidx.test.runner.AndroidJUnitRunner")
287283

288284
externalNativeBuild {
289-
ndkBuild {
290-
arguments "NDK_APPLICATION_MK=$projectDir/src/main/jni/Application.mk",
291-
"THIRD_PARTY_NDK_DIR=$thirdPartyNdkDir",
292-
"REACT_COMMON_DIR=$projectDir/../ReactCommon",
293-
"REACT_GENERATED_SRC_DIR=$buildDir/generated/source",
294-
"REACT_SRC_DIR=$projectDir/src/main/java/com/facebook/react",
295-
"APP_STL=c++_shared",
296-
"-j${ndkBuildJobs()}"
297-
298-
if (Os.isFamily(Os.FAMILY_MAC)) {
299-
// This flag will suppress "fcntl(): Bad file descriptor" warnings on local builds.
300-
arguments "--output-sync=none"
301-
}
285+
cmake {
286+
arguments "-DREACT_COMMON_DIR=${rootProject.projectDir}/ReactCommon",
287+
"-DREACT_ANDROID_DIR=$projectDir",
288+
"-DANDROID_STL=c++_shared",
289+
"-DANDROID_TOOLCHAIN=clang",
290+
"-DANDROID_PLATFORM=android-21"
291+
292+
targets "reactnativejni",
293+
"jscexecutor",
294+
"jsijniprofiler",
295+
"reactnativeblob",
296+
"reactperfloggerjni",
297+
"turbomodulejsijni",
298+
"fabricjni"
302299
}
303300
}
304301
ndk {
305302
abiFilters (*reactNativeArchitectures())
306303
}
307304
}
308305

306+
buildTypes {
307+
debug {
308+
externalNativeBuild {
309+
cmake {
310+
targets "hermes-executor-debug"
311+
}
312+
}
313+
}
314+
315+
release {
316+
externalNativeBuild {
317+
cmake {
318+
targets "hermes-executor-release"
319+
}
320+
}
321+
}
322+
}
323+
309324
externalNativeBuild {
310-
ndkBuild {
311-
path "src/main/jni/react/jni/Android.mk"
325+
cmake {
326+
path "src/main/jni/CMakeLists.txt"
312327
}
313328
}
314329

@@ -407,27 +422,6 @@ afterEvaluate {
407422

408423
// Needed as some of the native sources needs to be downloaded
409424
// before configureNdkBuildDebug could be executed.
410-
reactNativeArchitectures().each { architecture ->
411-
tasks.findByName("configureNdkBuildDebug[${architecture}]")?.configure { dependsOn(preBuild) }
412-
tasks.findByName("configureNdkBuildRelease[${architecture}]")?.configure { dependsOn(preBuild) }
413-
}
414-
configureNdkBuildDebug.dependsOn(preBuild)
415-
configureNdkBuildRelease.dependsOn(preBuild)
416-
417-
// As we're consuming Hermes via Prefab, we're using the AGP C++ reference feature
418-
// which is supposed to work for CMake only, we need to introduce an explicit dependency
419-
// on publishing libraries prefab tasks.
420-
// See https://developer.android.com/studio/releases/gradle-plugin?buildsystem=ndk-build#cpp-references.
421-
// If we don't do this, the `configureNdkBuild*` is flaky and fails if the `prefabDebugPackage`
422-
// is not yet executed.
423-
// This can be safely removed once we move to CMake or once AGP supports C++ references on ndk-build.
424-
configureNdkBuildDebug.dependsOn(":ReactAndroid:hermes-engine:prefabDebugPackage")
425-
configureNdkBuildRelease.dependsOn(":ReactAndroid:hermes-engine:prefabReleasePackage")
426-
reactNativeArchitectures().each { architecture ->
427-
tasks.findByName("configureNdkBuildDebug[${architecture}]")?.configure { dependsOn(":ReactAndroid:hermes-engine:prefabDebugPackage") }
428-
tasks.findByName("configureNdkBuildRelease[${architecture}]")?.configure { dependsOn(":ReactAndroid:hermes-engine:prefabReleasePackage") }
429-
}
430-
431425
publishing {
432426
publications {
433427
release(MavenPublication) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
cmake_minimum_required(VERSION 3.13)
7+
set(CMAKE_VERBOSE_MAKEFILE on)
8+
9+
file(GLOB_RECURSE jsijniprofiler_SRC CONFIGURE_DEPENDS *.cpp)
10+
11+
add_library(
12+
jsijniprofiler
13+
SHARED
14+
${jsijniprofiler_SRC}
15+
)
16+
target_compile_options(
17+
jsijniprofiler
18+
PRIVATE
19+
-fexceptions
20+
)
21+
target_include_directories(jsijniprofiler PRIVATE .)
22+
target_link_libraries(
23+
jsijniprofiler
24+
fb
25+
fbjni
26+
jsireact
27+
folly_runtime
28+
hermes-engine::libhermes
29+
jsi
30+
reactnativejni
31+
)
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
cmake_minimum_required(VERSION 3.13)
7+
set(CMAKE_VERBOSE_MAKEFILE on)
8+
9+
project(ReactAndroid)
10+
11+
# Make sure every shared lib includes a .note.gnu.build-id header
12+
add_link_options(-Wl,--build-id)
13+
add_compile_options(-Wall -Werror -std=c++1y)
14+
15+
function(add_react_android_subdir relative_path)
16+
add_subdirectory(${REACT_ANDROID_DIR}/${relative_path} ReactAndroid/${relative_path})
17+
endfunction()
18+
19+
function(add_react_third_party_ndk_subdir relative_path)
20+
add_react_android_subdir(build/third-party-ndk/${relative_path})
21+
endfunction()
22+
23+
function(add_react_common_subdir relative_path)
24+
add_subdirectory(${REACT_COMMON_DIR}/${relative_path} ReactCommon/${relative_path})
25+
endfunction()
26+
27+
# Third-party prefabs
28+
find_package(hermes-engine REQUIRED CONFIG)
29+
30+
# Third-party downloaded targets
31+
add_react_third_party_ndk_subdir(glog)
32+
add_react_third_party_ndk_subdir(boost)
33+
add_react_third_party_ndk_subdir(double-conversion)
34+
add_react_third_party_ndk_subdir(fmt)
35+
add_react_third_party_ndk_subdir(libevent)
36+
add_react_third_party_ndk_subdir(folly)
37+
add_react_third_party_ndk_subdir(jsc)
38+
39+
# Common targets
40+
add_react_common_subdir(yoga)
41+
add_react_common_subdir(runtimeexecutor)
42+
add_react_common_subdir(reactperflogger)
43+
add_react_common_subdir(logger)
44+
add_react_common_subdir(jsiexecutor)
45+
add_react_common_subdir(cxxreact)
46+
add_react_common_subdir(jsi)
47+
add_react_common_subdir(butter)
48+
add_react_common_subdir(callinvoker)
49+
add_react_common_subdir(jsinspector)
50+
add_react_common_subdir(hermes/executor)
51+
add_react_common_subdir(hermes/inspector)
52+
add_react_common_subdir(react/renderer/runtimescheduler)
53+
add_react_common_subdir(react/debug)
54+
add_react_common_subdir(react/config)
55+
add_react_common_subdir(react/renderer/animations)
56+
add_react_common_subdir(react/renderer/attributedstring)
57+
add_react_common_subdir(react/renderer/componentregistry)
58+
add_react_common_subdir(react/renderer/mounting)
59+
add_react_common_subdir(react/renderer/scheduler)
60+
add_react_common_subdir(react/renderer/telemetry)
61+
add_react_common_subdir(react/renderer/templateprocessor)
62+
add_react_common_subdir(react/renderer/uimanager)
63+
add_react_common_subdir(react/renderer/core)
64+
add_react_common_subdir(react/renderer/graphics)
65+
add_react_common_subdir(react/renderer/debug)
66+
add_react_common_subdir(react/renderer/imagemanager)
67+
add_react_common_subdir(react/renderer/components/view)
68+
add_react_common_subdir(react/renderer/components/switch)
69+
add_react_common_subdir(react/renderer/components/textinput)
70+
add_react_common_subdir(react/renderer/components/progressbar)
71+
add_react_common_subdir(react/renderer/components/slider)
72+
add_react_common_subdir(react/renderer/components/root)
73+
add_react_common_subdir(react/renderer/components/image)
74+
add_react_common_subdir(react/renderer/componentregistry/native)
75+
add_react_common_subdir(react/renderer/components/text)
76+
add_react_common_subdir(react/renderer/components/unimplementedview)
77+
add_react_common_subdir(react/renderer/components/modal)
78+
add_react_common_subdir(react/renderer/components/scrollview)
79+
add_react_common_subdir(react/renderer/leakchecker)
80+
add_react_common_subdir(react/renderer/textlayoutmanager)
81+
add_react_common_subdir(react/utils)
82+
add_react_common_subdir(react/bridging)
83+
add_react_common_subdir(react/renderer/mapbuffer)
84+
add_react_common_subdir(react/nativemodule/core)
85+
86+
# ReactAndroid JNI targets
87+
add_react_android_subdir(build/generated/source/codegen/jni)
88+
add_react_android_subdir(src/main/java/com/facebook/hermes/reactexecutor)
89+
add_react_android_subdir(src/main/jni/first-party/fbjni)
90+
add_react_android_subdir(src/main/jni/first-party/fb)
91+
add_react_android_subdir(src/main/jni/first-party/fbgloginit)
92+
add_react_android_subdir(src/main/jni/first-party/yogajni)
93+
add_react_android_subdir(src/main/jni/react/jni)
94+
add_react_android_subdir(src/main/java/com/facebook/react/reactperflogger/jni)
95+
add_react_android_subdir(src/main/java/com/facebook/react/jscexecutor)
96+
add_react_android_subdir(src/main/java/com/facebook/react/turbomodule/core/jni)
97+
add_react_android_subdir(src/main/java/com/facebook/react/uimanager/jni)
98+
add_react_android_subdir(src/main/java/com/facebook/react/common/mapbuffer/jni)
99+
add_react_android_subdir(src/main/java/com/facebook/react/fabric/jni)
100+
add_react_android_subdir(src/main/java/com/facebook/react/modules/blob/jni)
101+
add_react_android_subdir(src/main/java/com/facebook/hermes/instrumentation/)
102+

ReactCommon/runtimeexecutor/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ set(CMAKE_VERBOSE_MAKEFILE on)
88

99
add_compile_options(-fexceptions -frtti -std=c++17 -Wall)
1010

11-
add_library(runtimeexecutor INTERFACE)
11+
file(GLOB_RECURSE runtimeexecutor_SRC CONFIGURE_DEPENDS *.cpp *.h)
1212

13-
target_include_directories(runtimeexecutor INTERFACE .)
13+
add_library(runtimeexecutor SHARED ${runtimeexecutor_SRC})
14+
15+
target_include_directories(runtimeexecutor PUBLIC .)
16+
17+
target_link_libraries(runtimeexecutor jsi)

0 commit comments

Comments
 (0)