Skip to content

Commit 1a17635

Browse files
committed
fix: require RapidJSON exported CMake target
1 parent 583c5dc commit 1a17635

3 files changed

Lines changed: 87 additions & 79 deletions

File tree

CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ endfunction()
160160

161161
# enable json support
162162
if(ENABLE_RAPIDJSON)
163-
include(cmake/AbacusRapidJSON.cmake)
164-
abacus_configure_rapidjson(abacus_external_deps)
163+
find_package(RapidJSON CONFIG REQUIRED)
164+
if(NOT TARGET RapidJSON)
165+
message(FATAL_ERROR "RapidJSON package configuration did not define the RapidJSON target")
166+
endif()
167+
abacus_add_feature_definitions(__RAPIDJSON)
168+
target_link_libraries(abacus_external_deps INTERFACE RapidJSON)
165169
endif()
166170

167171
# get commit info

cmake/AbacusRapidJSON.cmake

Lines changed: 0 additions & 23 deletions
This file was deleted.

toolchain/tests/test_rapidjson_cmake.sh

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ fail() {
99
FAILURES=$((FAILURES + 1))
1010
}
1111

12-
write_lowercase_target_package() {
12+
write_target_package() {
1313
local prefix="$1"
1414
mkdir -p "${prefix}/include/rapidjson" "${prefix}/lib/cmake/RapidJSON"
1515
printf '#pragma once\n' >"${prefix}/include/rapidjson/document.h"
1616
cat >"${prefix}/lib/cmake/RapidJSON/RapidJSONConfig.cmake" <<'EOF'
1717
get_filename_component(RapidJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
1818
get_filename_component(_rapidjson_prefix "${RapidJSON_CMAKE_DIR}/../../.." ABSOLUTE)
19-
set(RapidJSON_INCLUDE_DIR "${_rapidjson_prefix}/include")
20-
set(RapidJSON_INCLUDE_DIRS "${RapidJSON_INCLUDE_DIR}")
21-
if(NOT TARGET rapidjson)
22-
add_library(rapidjson INTERFACE IMPORTED)
23-
set_property(TARGET rapidjson PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${RapidJSON_INCLUDE_DIRS}")
19+
if(NOT TARGET RapidJSON)
20+
add_library(RapidJSON INTERFACE IMPORTED)
21+
set_property(TARGET RapidJSON PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${_rapidjson_prefix}/include")
2422
endif()
2523
EOF
2624
}
@@ -37,7 +35,7 @@ set(RapidJSON_INCLUDE_DIRS "${RapidJSON_INCLUDE_DIR}")
3735
EOF
3836
}
3937

40-
write_target_test_project() {
38+
write_test_project() {
4139
local project_dir="$1"
4240
mkdir -p "$project_dir"
4341
cat >"${project_dir}/CMakeLists.txt" <<'EOF'
@@ -48,42 +46,18 @@ function(abacus_add_feature_definitions)
4846
set_property(GLOBAL APPEND PROPERTY ABACUS_FEATURE_DEFINITIONS ${ARGN})
4947
endfunction()
5048
51-
include("${ABACUS_SOURCE_DIR}/cmake/AbacusRapidJSON.cmake")
52-
53-
add_library(deps INTERFACE)
54-
abacus_configure_rapidjson(deps)
55-
56-
get_target_property(_links deps INTERFACE_LINK_LIBRARIES)
57-
if(NOT _links MATCHES "(^|;)rapidjson($|;)")
58-
message(FATAL_ERROR "Expected deps to link to rapidjson target, got '${_links}'")
59-
endif()
60-
61-
get_property(_defs GLOBAL PROPERTY ABACUS_FEATURE_DEFINITIONS)
62-
if(NOT "__RAPIDJSON" IN_LIST _defs)
63-
message(FATAL_ERROR "Expected __RAPIDJSON feature definition")
49+
find_package(RapidJSON CONFIG REQUIRED)
50+
if(NOT TARGET RapidJSON)
51+
message(FATAL_ERROR "RapidJSON package configuration did not define the RapidJSON target")
6452
endif()
65-
EOF
66-
}
67-
68-
write_variable_test_project() {
69-
local project_dir="$1"
70-
mkdir -p "$project_dir"
71-
cat >"${project_dir}/CMakeLists.txt" <<'EOF'
72-
cmake_minimum_required(VERSION 3.16)
73-
project(check_rapidjson_variables LANGUAGES CXX)
74-
75-
function(abacus_add_feature_definitions)
76-
set_property(GLOBAL APPEND PROPERTY ABACUS_FEATURE_DEFINITIONS ${ARGN})
77-
endfunction()
78-
79-
include("${ABACUS_SOURCE_DIR}/cmake/AbacusRapidJSON.cmake")
8053
8154
add_library(deps INTERFACE)
82-
abacus_configure_rapidjson(deps)
55+
target_link_libraries(deps INTERFACE RapidJSON)
56+
abacus_add_feature_definitions(__RAPIDJSON)
8357
84-
get_target_property(_includes deps INTERFACE_INCLUDE_DIRECTORIES)
85-
if(NOT _includes STREQUAL "${EXPECTED_RAPIDJSON_INCLUDE}")
86-
message(FATAL_ERROR "Expected include '${EXPECTED_RAPIDJSON_INCLUDE}', got '${_includes}'")
58+
get_target_property(_links deps INTERFACE_LINK_LIBRARIES)
59+
if(NOT "RapidJSON" IN_LIST _links)
60+
message(FATAL_ERROR "Expected deps to link to RapidJSON target, got '${_links}'")
8761
endif()
8862
8963
get_property(_defs GLOBAL PROPERTY ABACUS_FEATURE_DEFINITIONS)
@@ -97,64 +71,117 @@ run_cmake_configure() {
9771
local source_dir="$1"
9872
local build_dir="$2"
9973
local prefix="$3"
100-
local expected_include="$4"
10174

10275
cmake -S "$source_dir" -B "$build_dir" \
103-
-DABACUS_SOURCE_DIR="${REPO_ROOT}" \
10476
-DCMAKE_PREFIX_PATH="$prefix" \
105-
-DEXPECTED_RAPIDJSON_INCLUDE="$expected_include" \
10677
>"${build_dir}.log" 2>&1
10778
}
10879

109-
test_lowercase_target_package() {
80+
run_top_level_configure() {
81+
local build_dir="$1"
82+
local prefix="$2"
83+
84+
cmake -S "$REPO_ROOT" -B "$build_dir" \
85+
-DENABLE_RAPIDJSON=ON \
86+
-DCMAKE_PREFIX_PATH="$prefix" \
87+
>"${build_dir}.log" 2>&1
88+
}
89+
90+
test_rapidjson_target_package_configures() {
11091
local tmpdir prefix source_dir build_dir status
11192
tmpdir="$(mktemp -d)"
11293
prefix="${tmpdir}/prefix"
11394
source_dir="${tmpdir}/target-project"
11495
build_dir="${tmpdir}/target-build"
11596

116-
write_lowercase_target_package "$prefix"
117-
write_target_test_project "$source_dir"
118-
run_cmake_configure "$source_dir" "$build_dir" "$prefix" "${prefix}/include"
97+
write_target_package "$prefix"
98+
write_test_project "$source_dir"
99+
run_cmake_configure "$source_dir" "$build_dir" "$prefix"
119100
status=$?
120101

121102
if [[ "$status" -ne 0 ]]; then
122103
cat "${build_dir}.log" >&2
123-
fail "lowercase RapidJSON target package did not configure"
104+
fail "RapidJSON target package did not configure"
124105
fi
125106

126107
rm -rf "$tmpdir"
127108
}
128109

129-
test_variable_only_package() {
110+
test_variable_only_package_fails() {
130111
local tmpdir prefix source_dir build_dir status
131112
tmpdir="$(mktemp -d)"
132113
prefix="${tmpdir}/prefix"
133114
source_dir="${tmpdir}/variable-project"
134115
build_dir="${tmpdir}/variable-build"
135116

136117
write_variable_only_package "$prefix"
137-
write_variable_test_project "$source_dir"
138-
run_cmake_configure "$source_dir" "$build_dir" "$prefix" "${prefix}/include"
118+
write_test_project "$source_dir"
119+
run_cmake_configure "$source_dir" "$build_dir" "$prefix"
139120
status=$?
140121

141-
if [[ "$status" -ne 0 ]]; then
122+
if [[ "$status" -eq 0 ]]; then
123+
cat "${build_dir}.log" >&2
124+
fail "variable-only RapidJSON package configured; expected target-missing failure"
125+
elif ! grep -Fq "RapidJSON package configuration did not define the RapidJSON target" "${build_dir}.log"; then
142126
cat "${build_dir}.log" >&2
143-
fail "variable-only RapidJSON package did not configure"
127+
fail "variable-only RapidJSON package failed for the wrong reason"
144128
fi
145129

146130
rm -rf "$tmpdir"
147131
}
148132

133+
test_top_level_rejects_variable_only_package() {
134+
local tmpdir prefix build_dir status
135+
tmpdir="$(mktemp -d)"
136+
prefix="${tmpdir}/prefix"
137+
build_dir="${tmpdir}/top-level-build"
138+
139+
write_variable_only_package "$prefix"
140+
run_top_level_configure "$build_dir" "$prefix"
141+
status=$?
142+
143+
if [[ "$status" -eq 0 ]]; then
144+
cat "${build_dir}.log" >&2
145+
fail "top-level CMake configured with variable-only RapidJSON package; expected target-missing failure"
146+
elif ! grep -Fq "RapidJSON package configuration did not define the RapidJSON target" "${build_dir}.log"; then
147+
cat "${build_dir}.log" >&2
148+
fail "top-level CMake failed for the wrong reason with variable-only RapidJSON package"
149+
fi
150+
151+
rm -rf "$tmpdir"
152+
}
153+
154+
test_top_level_uses_rapidjson_target_directly() {
155+
local cmakelists="${REPO_ROOT}/CMakeLists.txt"
156+
157+
if [[ -e "${REPO_ROOT}/cmake/AbacusRapidJSON.cmake" ]]; then
158+
fail "cmake/AbacusRapidJSON.cmake still exists; RapidJSON config should provide the target"
159+
fi
160+
161+
if ! grep -Fq "find_package(RapidJSON CONFIG REQUIRED)" "$cmakelists"; then
162+
fail "top-level CMakeLists.txt does not call find_package(RapidJSON CONFIG REQUIRED)"
163+
fi
164+
165+
if ! grep -Fq "target_link_libraries(abacus_external_deps INTERFACE RapidJSON)" "$cmakelists"; then
166+
fail "top-level CMakeLists.txt does not link abacus_external_deps to RapidJSON target"
167+
fi
168+
169+
if grep -Fq "include(cmake/AbacusRapidJSON.cmake)" "$cmakelists"; then
170+
fail "top-level CMakeLists.txt still includes the RapidJSON compatibility helper"
171+
fi
172+
}
173+
149174
test_aocc_build_script_does_not_override_rapidjson_dir() {
150175
local script="${REPO_ROOT}/toolchain/build_abacus_aocc-aocl.sh"
151176
if grep -Fq -- '-DRapidJSON_DIR=$RAPIDJSON' "$script"; then
152177
fail "${script} still passes RapidJSON_DIR as the package prefix"
153178
fi
154179
}
155180

156-
test_lowercase_target_package
157-
test_variable_only_package
181+
test_rapidjson_target_package_configures
182+
test_variable_only_package_fails
183+
test_top_level_rejects_variable_only_package
184+
test_top_level_uses_rapidjson_target_directly
158185
test_aocc_build_script_does_not_override_rapidjson_dir
159186

160187
if [[ "$FAILURES" -ne 0 ]]; then

0 commit comments

Comments
 (0)