Skip to content

Commit

Permalink
chore: add cmake formatting and linting (#6811)
Browse files Browse the repository at this point in the history
## Summary

🚀 

## Test plan

Android linting CI
  • Loading branch information
tjzel authored Dec 13, 2024
1 parent 4ce84b2 commit 2e83da2
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 160 deletions.
34 changes: 23 additions & 11 deletions packages/react-native-reanimated/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,50 @@ project(Reanimated)
cmake_minimum_required(VERSION 3.8)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
set(CMAKE_EXPORT_COMPILE_COMMANDS
ON
CACHE INTERNAL "")

set(CMAKE_CXX_STANDARD 20)

# default CMAKE_CXX_FLAGS: "-g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-protector-all"
# default CMAKE_CXX_FLAGS: "-g -DANDROID -fdata-sections -ffunction-sections
# -funwind-tables -fstack-protector-strong -no-canonical-prefixes
# -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-protector-all"
include("${REACT_NATIVE_DIR}/ReactAndroid/cmake-utils/folly-flags.cmake")
add_compile_options(${folly_FLAGS})

string(APPEND CMAKE_CXX_FLAGS " -DREACT_NATIVE_MINOR_VERSION=${REACT_NATIVE_MINOR_VERSION} -DREANIMATED_VERSION=${REANIMATED_VERSION} -DHERMES_ENABLE_DEBUGGER=${HERMES_ENABLE_DEBUGGER}")
string(
APPEND
CMAKE_CXX_FLAGS
" -DREACT_NATIVE_MINOR_VERSION=${REACT_NATIVE_MINOR_VERSION} -DREANIMATED_VERSION=${REANIMATED_VERSION} -DHERMES_ENABLE_DEBUGGER=${HERMES_ENABLE_DEBUGGER}"
)

string(APPEND CMAKE_CXX_FLAGS " -fexceptions -fno-omit-frame-pointer -frtti -fstack-protector-all -std=c++${CMAKE_CXX_STANDARD} -Wall -Werror")
string(
APPEND
CMAKE_CXX_FLAGS
" -fexceptions -fno-omit-frame-pointer -frtti -fstack-protector-all -std=c++${CMAKE_CXX_STANDARD} -Wall -Werror"
)

if(${IS_NEW_ARCHITECTURE_ENABLED})
string(APPEND CMAKE_CXX_FLAGS " -DRCT_NEW_ARCH_ENABLED")
string(APPEND CMAKE_CXX_FLAGS " -DRCT_NEW_ARCH_ENABLED")
endif()

if(${IS_REANIMATED_EXAMPLE_APP})
string(APPEND CMAKE_CXX_FLAGS " -DIS_REANIMATED_EXAMPLE_APP -Wpedantic")
string(APPEND CMAKE_CXX_FLAGS " -DIS_REANIMATED_EXAMPLE_APP -Wpedantic")
endif()

if(NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug")
string(APPEND CMAKE_CXX_FLAGS " -DNDEBUG")
string(APPEND CMAKE_CXX_FLAGS " -DNDEBUG")
endif()

if(${JS_RUNTIME} STREQUAL "hermes")
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_HERMES=1")
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_HERMES=1")
elseif(${JS_RUNTIME} STREQUAL "jsc")
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_JSC=1")
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_JSC=1")
elseif(${JS_RUNTIME} STREQUAL "v8")
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_V8=1")
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_V8=1")
else()
message(FATAL_ERROR "Unknown JS runtime ${JS_RUNTIME}.")
message(FATAL_ERROR "Unknown JS runtime ${JS_RUNTIME}.")
endif()

# Resolves "CMake Warning: Manually-specified variables were not used by the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,38 @@
cmake_minimum_required(VERSION 3.8)

file(GLOB_RECURSE REANIMATED_COMMON_CPP_SOURCES CONFIGURE_DEPENDS "${COMMON_CPP_DIR}/reanimated/*.cpp")
file(GLOB_RECURSE REANIMATED_ANDROID_CPP_SOURCES CONFIGURE_DEPENDS "${ANDROID_CPP_DIR}/reanimated/*.cpp")
file(GLOB_RECURSE REANIMATED_COMMON_CPP_SOURCES CONFIGURE_DEPENDS
"${COMMON_CPP_DIR}/reanimated/*.cpp")
file(GLOB_RECURSE REANIMATED_ANDROID_CPP_SOURCES CONFIGURE_DEPENDS
"${ANDROID_CPP_DIR}/reanimated/*.cpp")

find_package(ReactAndroid REQUIRED CONFIG)

add_library(
reanimated
SHARED
${REANIMATED_COMMON_CPP_SOURCES}
${REANIMATED_ANDROID_CPP_SOURCES}
)
add_library(reanimated SHARED ${REANIMATED_COMMON_CPP_SOURCES}
${REANIMATED_ANDROID_CPP_SOURCES})

target_include_directories(
reanimated
PRIVATE
"${COMMON_CPP_DIR}"
"${ANDROID_CPP_DIR}"
"${REACT_NATIVE_DIR}/ReactCommon"
"${REACT_NATIVE_DIR}/ReactAndroid/src/main/jni/react/turbomodule"
"${REACT_NATIVE_DIR}/ReactCommon/callinvoker"
"${REACT_NATIVE_DIR}/ReactCommon/runtimeexecutor"
)
reanimated
PRIVATE "${COMMON_CPP_DIR}"
"${ANDROID_CPP_DIR}"
"${REACT_NATIVE_DIR}/ReactCommon"
"${REACT_NATIVE_DIR}/ReactAndroid/src/main/jni/react/turbomodule"
"${REACT_NATIVE_DIR}/ReactCommon/callinvoker"
"${REACT_NATIVE_DIR}/ReactCommon/runtimeexecutor")

if(${IS_NEW_ARCHITECTURE_ENABLED})
target_include_directories(
reanimated
PRIVATE
"${REACT_NATIVE_DIR}/ReactCommon/yoga"
"${REACT_NATIVE_DIR}/ReactCommon/react/renderer/graphics/platform/cxx"
)
target_include_directories(
reanimated
PRIVATE
"${REACT_NATIVE_DIR}/ReactCommon/yoga"
"${REACT_NATIVE_DIR}/ReactCommon/react/renderer/graphics/platform/cxx")
endif()

set_target_properties(
reanimated
PROPERTIES
LINKER_LANGUAGE
CXX
)
set_target_properties(reanimated PROPERTIES LINKER_LANGUAGE CXX)

target_link_libraries(
reanimated
worklets
)
target_link_libraries(reanimated worklets)

if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
target_link_libraries(
reanimated
ReactAndroid::reactnative
)
target_link_libraries(reanimated ReactAndroid::reactnative)
else()
target_link_libraries(
reanimated
ReactAndroid::react_nativemodule_core
)
target_link_libraries(reanimated ReactAndroid::react_nativemodule_core)
endif()
Original file line number Diff line number Diff line change
@@ -1,139 +1,88 @@
cmake_minimum_required(VERSION 3.8)

file(GLOB_RECURSE WORKLETS_COMMON_CPP_SOURCES CONFIGURE_DEPENDS "${COMMON_CPP_DIR}/worklets/*.cpp")
file(GLOB_RECURSE WORKLETS_ANDROID_CPP_SOURCES CONFIGURE_DEPENDS "${ANDROID_CPP_DIR}/worklets/*.cpp")
file(GLOB_RECURSE WORKLETS_COMMON_CPP_SOURCES CONFIGURE_DEPENDS
"${COMMON_CPP_DIR}/worklets/*.cpp")
file(GLOB_RECURSE WORKLETS_ANDROID_CPP_SOURCES CONFIGURE_DEPENDS
"${ANDROID_CPP_DIR}/worklets/*.cpp")

# Consume shared libraries and headers from prefabs
find_package(fbjni REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)

if(${JS_RUNTIME} STREQUAL "hermes")
find_package(hermes-engine REQUIRED CONFIG)
find_package(hermes-engine REQUIRED CONFIG)
endif()

add_library(
worklets
SHARED
${WORKLETS_COMMON_CPP_SOURCES}
${WORKLETS_ANDROID_CPP_SOURCES}
)
add_library(worklets SHARED ${WORKLETS_COMMON_CPP_SOURCES}
${WORKLETS_ANDROID_CPP_SOURCES})

# includes
target_include_directories(
worklets
PUBLIC
"${COMMON_CPP_DIR}"
"${ANDROID_CPP_DIR}"
)
target_include_directories(worklets PUBLIC "${COMMON_CPP_DIR}"
"${ANDROID_CPP_DIR}")

target_include_directories(
worklets
PRIVATE
"${REACT_NATIVE_DIR}/ReactCommon"
"${REACT_NATIVE_DIR}/ReactAndroid/src/main/jni/react/turbomodule"
"${REACT_NATIVE_DIR}/ReactCommon/react/nativemodule/core/ReactCommon"
"${REACT_NATIVE_DIR}/ReactCommon/callinvoker"
"${REACT_NATIVE_DIR}/ReactCommon/runtimeexecutor"
)
worklets
PRIVATE "${REACT_NATIVE_DIR}/ReactCommon"
"${REACT_NATIVE_DIR}/ReactAndroid/src/main/jni/react/turbomodule"
"${REACT_NATIVE_DIR}/ReactCommon/react/nativemodule/core/ReactCommon"
"${REACT_NATIVE_DIR}/ReactCommon/callinvoker"
"${REACT_NATIVE_DIR}/ReactCommon/runtimeexecutor")

if(${IS_NEW_ARCHITECTURE_ENABLED})
target_include_directories(
worklets
PRIVATE
"${REACT_NATIVE_DIR}/ReactCommon/yoga"
"${REACT_NATIVE_DIR}/ReactCommon/react/renderer/graphics/platform/cxx"
)
target_include_directories(
worklets
PRIVATE
"${REACT_NATIVE_DIR}/ReactCommon/yoga"
"${REACT_NATIVE_DIR}/ReactCommon/react/renderer/graphics/platform/cxx")

if(ReactAndroid_VERSION_MINOR LESS 76)
target_link_libraries(
worklets
ReactAndroid::fabricjni
ReactAndroid::react_debug
ReactAndroid::react_render_core
ReactAndroid::react_render_componentregistry
ReactAndroid::rrc_view
)
endif()
if(ReactAndroid_VERSION_MINOR LESS 76)
target_link_libraries(
worklets ReactAndroid::fabricjni ReactAndroid::react_debug
ReactAndroid::react_render_core
ReactAndroid::react_render_componentregistry ReactAndroid::rrc_view)
endif()
endif()

# build shared lib
set_target_properties(
worklets
PROPERTIES
LINKER_LANGUAGE
CXX
)
set_target_properties(worklets PROPERTIES LINKER_LANGUAGE CXX)

target_link_libraries(
worklets
log
ReactAndroid::jsi
fbjni::fbjni
)
target_link_libraries(worklets log ReactAndroid::jsi fbjni::fbjni)

if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
target_link_libraries(
worklets
ReactAndroid::reactnative
)
target_link_libraries(worklets ReactAndroid::reactnative)
else()
target_link_libraries(
worklets
ReactAndroid::react_nativemodule_core
ReactAndroid::folly_runtime
ReactAndroid::glog
ReactAndroid::reactnativejni
)
target_link_libraries(
worklets ReactAndroid::react_nativemodule_core ReactAndroid::folly_runtime
ReactAndroid::glog ReactAndroid::reactnativejni)
endif()

if(${JS_RUNTIME} STREQUAL "hermes")
target_link_libraries(
worklets
hermes-engine::libhermes
)
target_link_libraries(worklets hermes-engine::libhermes)

if(${HERMES_ENABLE_DEBUGGER})
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
target_link_libraries(
worklets
ReactAndroid::hermestooling
)
else()
target_link_libraries(
worklets
ReactAndroid::hermes_executor
)
endif()
endif()
elseif(${JS_RUNTIME} STREQUAL "jsc")
if(${HERMES_ENABLE_DEBUGGER})
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
target_link_libraries(
worklets
ReactAndroid::jsctooling
)
target_link_libraries(worklets ReactAndroid::hermestooling)
else()
target_link_libraries(
worklets
ReactAndroid::jscexecutor
)
target_link_libraries(worklets ReactAndroid::hermes_executor)
endif()
endif()
elseif(${JS_RUNTIME} STREQUAL "jsc")
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
target_link_libraries(worklets ReactAndroid::jsctooling)
else()
target_link_libraries(worklets ReactAndroid::jscexecutor)
endif()
elseif(${JS_RUNTIME} STREQUAL "v8")
# TODO: Refactor this when adding support for newest V8
target_include_directories(
worklets
PRIVATE
"${JS_RUNTIME_DIR}/src"
)
file(GLOB V8_SO_DIR "${JS_RUNTIME_DIR}/android/build/intermediates/library_jni/**/jni/${ANDROID_ABI}")
find_library(
V8EXECUTOR_LIB
v8executor
PATHS ${V8_SO_DIR}
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
target_link_libraries(
worklets
${V8EXECUTOR_LIB}
)
# TODO: Refactor this when adding support for newest V8
target_include_directories(worklets PRIVATE "${JS_RUNTIME_DIR}/src")
file(
GLOB
V8_SO_DIR
"${JS_RUNTIME_DIR}/android/build/intermediates/library_jni/**/jni/${ANDROID_ABI}"
)
find_library(
V8EXECUTOR_LIB v8executor
PATHS ${V8_SO_DIR}
NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
target_link_libraries(worklets ${V8EXECUTOR_LIB})
endif()
6 changes: 4 additions & 2 deletions packages/react-native-reanimated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
"lint": "yarn lint:js && yarn lint:plugin && yarn lint:common && yarn lint:android && yarn lint:apple",
"lint:js": "eslint --ext '.js,.ts,.tsx' src __tests__ __typetests__ && yarn prettier --check src __tests__ __typetests__",
"lint:plugin": "cd plugin && yarn lint",
"lint:android": "./scripts/validate-android.sh && ./android/gradlew -p android spotlessCheck -q && ./scripts/cpplint.sh android/src && yarn format:android:cpp --dry-run -Werror",
"lint:android": "./scripts/validate-android.sh && ./android/gradlew -p android spotlessCheck -q && ./scripts/cpplint.sh android/src && yarn format:android:cpp --dry-run -Werror && yarn lint:cmake",
"lint:common": "./scripts/validate-common.sh && ./scripts/cpplint.sh Common && yarn format:common --dry-run -Werror",
"lint:apple": "./scripts/validate-apple.sh && yarn format:apple --dry-run -Werror",
"format": "yarn format:js && yarn format:plugin && yarn format:apple && yarn format:android:java && yarn format:android:cpp && yarn format:common",
"lint:cmake": "find ./android -type d \\( -name build -o -name .cxx \\) -prune -o -type f -name 'CMakeLists.txt' -print | xargs ./scripts/lint-cmake.sh",
"format": "yarn format:js && yarn format:plugin && yarn format:apple && yarn format:android:java && yarn format:android:cpp && yarn format:android:cmake && yarn format:common",
"format:js": "prettier --write --list-different src __tests__ __typetests__",
"format:plugin": "cd plugin && yarn format",
"format:apple": "find apple -iname \"*.h\" -o -iname \"*.m\" -o -iname \"*.mm\" -o -iname \"*.cpp\" | xargs clang-format -i",
"format:android:java": "node ./scripts/format-java.js",
"format:android:cpp": "find android/src -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i",
"format:android:cmake": "find ./android -type d \\( -name build -o -name .cxx \\) -prune -o -type f -name 'CMakeLists.txt' -print | xargs ./scripts/format-cmake.sh",
"format:common": "find Common -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i",
"find-unused-code:js": "yarn ts-prune --ignore \"index|.web.\" --error",
"type:check": "yarn type:check:src && yarn type:check:plugin && ./scripts/test-ts.sh ../../apps/common-app/src/App.tsx __typetests__/common",
Expand Down
8 changes: 8 additions & 0 deletions packages/react-native-reanimated/scripts/format-cmake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

if ! which cmake-format >/dev/null; then
echo "error: cmake-format is not installed"
exit 1
fi

cmake-format -i "$@"
8 changes: 8 additions & 0 deletions packages/react-native-reanimated/scripts/lint-cmake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

if ! which cmake-lint >/dev/null; then
echo "error: cmake-lint is not installed"
exit 1
fi

cmake-lint "$@"

0 comments on commit 2e83da2

Please sign in to comment.