Skip to content

Commit

Permalink
Add macro to detect circular dependencies on Cmake
Browse files Browse the repository at this point in the history
Summary:
This is a prototype to add circular dependencies detection on CMake for ReactCommon and ReactAndroid.
It can be enabled per module and works as follows:

```
set(ALLOWED_HEADER_IMPORT_PATHS
        react/renderer/graphics
        react/debug)
check_for_circular_dependencies("${ALLOWED_HEADER_IMPORT_PATHS}")
```

The allowed header import path must be manually specified as libraries are exposing wrong header search paths (so can't be reused).
The CI will be red till the circular dependency on `graphics` is resolved.

Changelog:
[Internal] [Changed] - Add macro to detect circular dependencies on Cmake

Reviewed By: cipolleschi

Differential Revision: D42927036

fbshipit-source-id: b1393dfd43fd042e2ebf1d5b46b24bd9f5e20d58
  • Loading branch information
cortinico authored and facebook-github-bot committed Feb 2, 2023
1 parent 37171ec commit 147a1f6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ReactAndroid/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ endif(CCACHE_FOUND)
add_link_options(-Wl,--build-id)
add_compile_options(-Wall -Werror -std=c++17)

# This exposes the check_for_circular_dependencies macro to all the submodules
include(CircularDepsValidator.cmake)

function(add_react_android_subdir relative_path)
add_subdirectory(${REACT_ANDROID_DIR}/${relative_path} ReactAndroid/${relative_path})
endfunction()
Expand Down
32 changes: 32 additions & 0 deletions ReactAndroid/src/main/jni/CircularDepsValidator.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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)

# Util function that help us verify that you're not including a header file
# which has an invalid import path. Most of the time this is causing a C++ circular dependency.
function(check_for_circular_dependencies allowed_imports_paths)
file(GLOB headers CONFIGURE_DEPENDS *.h)
foreach(file ${headers})
file(STRINGS ${file} header_file)
while(header_file)
list(POP_FRONT header_file line)
if (line MATCHES "^#include <react")
set(matched_import false)
foreach(allowed_import ${allowed_imports_paths})
if (line MATCHES "^#include <${allowed_import}")
set(matched_import true)
continue()
endif()
endforeach()
if (NOT matched_import)
message(FATAL_ERROR "!!!!!\nDiscovered an invalid include on file\n${file}\nfor include\n${line}\n")
break()
endif()
endif()
endwhile()
endforeach()
endfunction()
1 change: 1 addition & 0 deletions ReactCommon/react/renderer/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ target_include_directories(react_render_graphics
)

target_link_libraries(react_render_graphics glog fb fbjni folly_runtime react_debug)

0 comments on commit 147a1f6

Please sign in to comment.