Skip to content

Commit 08b90d8

Browse files
cortinicofacebook-github-bot
authored andcommitted
Introduce the target_compile_reactnative_options function (#49747)
Summary: Pull Request resolved: #49747 I'm adding the `target_compile_reactnative_options` to centralize the management of compilation flags for React Native CMake build. Changelog: [Internal] [Changed] - Reviewed By: javache Differential Revision: D70386746
1 parent b8a0499 commit 08b90d8

File tree

21 files changed

+104
-113
lines changed

21 files changed

+104
-113
lines changed

packages/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cmake_minimum_required(VERSION 3.13)
1818
set(CMAKE_VERBOSE_MAKEFILE on)
1919

2020
include(${CMAKE_CURRENT_LIST_DIR}/folly-flags.cmake)
21+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
2122

2223
# We configured the REACT_COMMON_DIR variable as it's commonly used to reference
2324
# shared C++ code in other targets.
@@ -60,16 +61,7 @@ target_include_directories(${CMAKE_PROJECT_NAME}
6061
${CMAKE_CURRENT_SOURCE_DIR}
6162
${PROJECT_BUILD_DIR}/generated/autolinking/src/main/jni)
6263

63-
target_compile_options(${CMAKE_PROJECT_NAME}
64-
PRIVATE
65-
-Wall
66-
-Werror
67-
-fexceptions
68-
-frtti
69-
-std=c++20
70-
-DLOG_TAG=\"ReactNative\"
71-
-DFOLLY_NO_CONFIG=1
72-
)
64+
target_compile_reactnative_options(${CMAKE_PROJECT_NAME} PRIVATE "ReactNative")
7365

7466
# Prefab packages from React Native
7567
find_package(ReactAndroid REQUIRED CONFIG)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
# This CMake file exposes the React Native Flags that all the libraries should use when
10+
# compiling a module that will end up inside libreactnative.so
11+
12+
SET(reactnative_FLAGS
13+
-Wall
14+
-Werror
15+
-fexceptions
16+
-frtti
17+
-std=c++20
18+
-DFOLLY_NO_CONFIG=1
19+
)
20+
21+
function(target_compile_reactnative_options target_name scope)
22+
target_compile_options(${target_name}
23+
${scope}
24+
-Wall
25+
-fexceptions
26+
-frtti
27+
-std=c++20
28+
-DFOLLY_NO_CONFIG=1
29+
)
30+
set (extra_args ${ARGN})
31+
list(LENGTH extra_args extra_count)
32+
set (tag "ReactNative")
33+
if (${extra_count} GREATER 0)
34+
list(GET extra_args 0 user_provided_tag)
35+
target_compile_options(${target_name} ${scope} -DLOG_TAG=\"${user_provided_tag}\")
36+
endif ()
37+
endfunction()
38+

packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ set(CMAKE_VERBOSE_MAKEFILE on)
88

99
project(ReactAndroid)
1010

11+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
12+
1113
# Convert input paths to CMake format (with forward slashes)
1214
file(TO_CMAKE_PATH "${REACT_ANDROID_DIR}" REACT_ANDROID_DIR)
1315
file(TO_CMAKE_PATH "${REACT_BUILD_DIR}" REACT_BUILD_DIR)
@@ -22,7 +24,6 @@ endif(CCACHE_FOUND)
2224

2325
# Make sure every shared lib includes a .note.gnu.build-id header
2426
add_link_options(-Wl,--build-id)
25-
add_compile_options(-Wall -Werror)
2627

2728
function(add_react_android_subdir relative_path)
2829
add_subdirectory(${REACT_ANDROID_DIR}/${relative_path} ReactAndroid/${relative_path})
@@ -242,6 +243,8 @@ target_link_libraries(reactnative
242243
yogacore
243244
)
244245

246+
target_compile_reactnative_options(reactnative PRIVATE)
247+
245248
target_include_directories(reactnative
246249
PUBLIC
247250
$<TARGET_PROPERTY:bridgeless,INTERFACE_INCLUDE_DIRECTORIES>
@@ -361,14 +364,9 @@ add_executable(reactnative_unittest
361364
# ${REACT_COMMON_DIR}/react/renderer/core/tests/ConcreteShadowNodeTest.cpp
362365
# ${REACT_COMMON_DIR}/react/renderer/core/tests/ComponentDescriptorTest.cpp
363366
)
364-
target_compile_options(reactnative_unittest
365-
PRIVATE
366-
-Wall
367-
-Werror
368-
-fexceptions
369-
-frtti
370-
-std=c++20
371-
-DHERMES_ENABLE_DEBUGGER)
367+
368+
target_compile_reactnative_options(reactnative_unittest PRIVATE)
369+
target_compile_options(reactnative_unittest PRIVATE -DHERMES_ENABLE_DEBUGGER)
372370

373371
target_link_libraries(reactnative_unittest
374372
fabricjni

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/CMakeLists.txt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ cmake_minimum_required(VERSION 3.13)
77
set(CMAKE_VERBOSE_MAKEFILE on)
88

99
include(${REACT_ANDROID_DIR}/src/main/jni/first-party/jni-lib-merge/SoMerging-utils.cmake)
10-
11-
add_compile_options(
12-
-fvisibility=hidden
13-
-fexceptions
14-
-frtti
15-
-std=c++20
16-
-O3)
10+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
1711

1812
file(GLOB yoga_SRC CONFIGURE_DEPENDS jni/*.cpp)
1913
add_library(yoga OBJECT ${yoga_SRC})
@@ -27,3 +21,6 @@ target_link_libraries(yoga
2721
log
2822
android
2923
)
24+
25+
target_compile_reactnative_options(yoga PRIVATE)
26+
target_compile_options(yoga PRIVATE -fvisibility=hidden -O3)

packages/react-native/ReactAndroid/src/main/jni/react/devsupport/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
cmake_minimum_required(VERSION 3.13)
77
set(CMAKE_VERBOSE_MAKEFILE on)
88

9-
add_compile_options(-fexceptions -frtti -std=c++20 -Wall -DLOG_TAG=\"ReactNative\")
10-
119
include(${REACT_ANDROID_DIR}/src/main/jni/first-party/jni-lib-merge/SoMerging-utils.cmake)
10+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
1211

1312
file(GLOB react_devsupportjni_SRC CONFIGURE_DEPENDS *.cpp)
1413

@@ -21,3 +20,5 @@ target_include_directories(react_devsupportjni PUBLIC .)
2120
target_link_libraries(react_devsupportjni
2221
fbjni
2322
jsinspector)
23+
24+
target_compile_reactnative_options(react_devsupportjni PRIVATE "ReactNative")

packages/react-native/ReactAndroid/src/main/jni/react/fabric/CMakeLists.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
cmake_minimum_required(VERSION 3.13)
77

88
include(${REACT_ANDROID_DIR}/src/main/jni/first-party/jni-lib-merge/SoMerging-utils.cmake)
9+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
910

1011
file(GLOB fabricjni_SRCS CONFIGURE_DEPENDS *.cpp)
1112

@@ -63,12 +64,4 @@ target_link_libraries(
6364
yoga
6465
)
6566

66-
target_compile_options(
67-
fabricjni
68-
PRIVATE
69-
-DLOG_TAG=\"Fabric\"
70-
-fexceptions
71-
-frtti
72-
-std=c++20
73-
-Wall
74-
)
67+
target_compile_reactnative_options(fabricjni PRIVATE "Fabric")

packages/react-native/ReactAndroid/src/main/jni/react/featureflags/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
cmake_minimum_required(VERSION 3.13)
77

88
include(${REACT_ANDROID_DIR}/src/main/jni/first-party/jni-lib-merge/SoMerging-utils.cmake)
9+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
910

1011
file(GLOB react_featureflagsjni_SRCS CONFIGURE_DEPENDS *.cpp)
1112

@@ -25,13 +26,4 @@ target_link_libraries(
2526
)
2627

2728
target_merge_so(react_featureflagsjni)
28-
29-
target_compile_options(
30-
react_featureflagsjni
31-
PRIVATE
32-
-DLOG_TAG=\"ReactNative\"
33-
-fexceptions
34-
-frtti
35-
-std=c++20
36-
-Wall
37-
)
29+
target_compile_reactnative_options(react_featureflagsjni PRIVATE "ReactNative")

packages/react-native/ReactAndroid/src/main/jni/react/hermes/instrumentation/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ set(CMAKE_VERBOSE_MAKEFILE on)
99
file(GLOB_RECURSE jsijniprofiler_SRC CONFIGURE_DEPENDS *.cpp)
1010

1111
include(${REACT_ANDROID_DIR}/src/main/jni/first-party/jni-lib-merge/SoMerging-utils.cmake)
12+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
1213

1314
add_library(
1415
jsijniprofiler
1516
OBJECT
1617
${jsijniprofiler_SRC}
1718
)
18-
target_compile_options(
19-
jsijniprofiler
20-
PRIVATE
21-
-fexceptions
22-
)
19+
target_compile_reactnative_options(jsijniprofiler PRIVATE)
2320
target_merge_so(jsijniprofiler)
2421

2522
target_include_directories(jsijniprofiler PRIVATE .)

packages/react-native/ReactAndroid/src/main/jni/react/hermes/reactexecutor/CMakeLists.txt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@ set(CMAKE_VERBOSE_MAKEFILE on)
99
file(GLOB_RECURSE hermes_executor_SRC CONFIGURE_DEPENDS *.cpp)
1010

1111
include(${REACT_ANDROID_DIR}/src/main/jni/first-party/jni-lib-merge/SoMerging-utils.cmake)
12+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
1213

1314
add_library(hermes_executor
1415
OBJECT
1516
${hermes_executor_SRC}
1617
)
17-
target_compile_options(
18-
hermes_executor
19-
PRIVATE
20-
$<$<CONFIG:Debug>:-DHERMES_ENABLE_DEBUGGER=1>
21-
-std=c++20
22-
-fexceptions
23-
)
2418
target_merge_so(hermes_executor)
2519
target_include_directories(hermes_executor PRIVATE .)
2620
target_link_libraries(
@@ -30,3 +24,5 @@ target_link_libraries(
3024
jsi
3125
reactnative
3226
)
27+
target_compile_reactnative_options(hermes_executor PRIVATE)
28+
target_compile_options(hermes_executor PRIVATE $<$<CONFIG:Debug>:-DHERMES_ENABLE_DEBUGGER=1>)

packages/react-native/ReactAndroid/src/main/jni/react/jni/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
cmake_minimum_required(VERSION 3.13)
77
set(CMAKE_VERBOSE_MAKEFILE on)
88

9-
file(GLOB reactnativejni_SRC CONFIGURE_DEPENDS *.cpp)
9+
include(${REACT_ANDROID_DIR}/cmake-utils/react-native-flags.cmake)
1010

11-
add_compile_options(
12-
-fexceptions
13-
-Wno-unused-lambda-capture
14-
-std=c++20)
11+
file(GLOB reactnativejni_SRC CONFIGURE_DEPENDS *.cpp)
1512

1613
######################
1714
### reactnativejni ###
@@ -41,3 +38,5 @@ target_link_libraries(reactnativejni
4138
runtimeexecutor
4239
yoga
4340
)
41+
target_compile_reactnative_options(reactnativejni PRIVATE)
42+
target_compile_options(reactnativejni PRIVATE -Wno-unused-lambda-capture)

0 commit comments

Comments
 (0)