Skip to content

[Code scan] Use discovered RapidJSON include paths instead of FetchContent-only variables #7569

Description

@njzjz

This issue is a result of a Codex global repository scan.

When ENABLE_RAPIDJSON is on, the top-level CMake first calls find_package(RapidJSON). Even if that succeeds, it then sets RapidJSON_INCLUDE_PATH from ${rapidjson_SOURCE_DIR}/include, which is only defined for the FetchContent fallback. A package found from an installed RapidJSON config can therefore be ignored and leave the include path empty or incorrect.

Top-level CMake:

# enable json support
if(ENABLE_RAPIDJSON)
find_package(RapidJSON)
if(NOT RapidJSON_FOUND)
message(
WARNING
"Rapidjson is not found, trying downloading from github, or you can install Rapidjson first and reinstall abacus."
)
include(FetchContent)
FetchContent_Declare(
rapidjson
URL https://codeload.github.com/Tencent/rapidjson/tar.gz/24b5e7a
)
set(RAPIDJSON_BUILD_TESTS
OFF
CACHE INTERNAL "")
set(RAPIDJSON_BUILD_EXAMPLES
OFF
CACHE INTERNAL "")
FetchContent_MakeAvailable(rapidjson)
endif()
set(RapidJSON_INCLUDE_PATH "${rapidjson_SOURCE_DIR}/include")
add_compile_definitions(__RAPIDJSON)
add_definitions(-DRAPIDJSON_HAS_CXX11_NOEXCEPT=0)
include_directories(${RapidJSON_INCLUDE_PATH})

The toolchain-generated config also writes a literal placeholder instead of the installed include directory:

mkdir -p "${pkg_install_dir}"
cp -r $dirname/* "${pkg_install_dir}/"
# for rapidjson found in cmake
cat << EOF > "${pkg_install_dir}/RapidJSONConfig.cmake"
get_filename_component(RAPIDJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(RAPIDJSON_INCLUDE_DIRS "@INCLUDE_INSTALL_DIR@")
message(STATUS "RapidJSON found. Headers: ${RAPIDJSON_INCLUDE_DIRS}")
EOF

Relevant code:

find_package(RapidJSON)
if(NOT RapidJSON_FOUND)
  FetchContent_Declare(rapidjson ...)
  FetchContent_MakeAvailable(rapidjson)
endif()
set(RapidJSON_INCLUDE_PATH "${rapidjson_SOURCE_DIR}/include")
include_directories(${RapidJSON_INCLUDE_PATH})

Suggested fix:

When RapidJSON_FOUND is true, use the include variable or imported target exported by the package. Only use ${rapidjson_SOURCE_DIR}/include after the FetchContent fallback. Also generate a valid RapidJSONConfig.cmake in the toolchain with the real install include path.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions