Skip to content

Commit 4e8fea0

Browse files
refactor: gained ability to be used as subproject
e.g. via add_subdirectory or FetchContent Signed-off-by: Coelacanthus <[email protected]>
1 parent 8f2beb5 commit 4e8fea0

File tree

8 files changed

+37
-15
lines changed

8 files changed

+37
-15
lines changed

CMakeLists.txt

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it
111111
#cJSON
112112
set(CJSON_LIB cjson)
113113

114-
file(GLOB HEADERS cJSON.h)
115-
set(SOURCES cJSON.c)
114+
set(SOURCES cjson/cJSON.c)
116115

117116
option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off)
118117
option(CJSON_OVERRIDE_BUILD_SHARED_LIBS "Override BUILD_SHARED_LIBS with CJSON_BUILD_SHARED_LIBS" OFF)
@@ -127,11 +126,23 @@ endif()
127126

128127

129128
if (NOT BUILD_SHARED_AND_STATIC_LIBS)
130-
add_library("${CJSON_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS}" "${SOURCES}")
129+
add_library("${CJSON_LIB}" "${CJSON_LIBRARY_TYPE}" "${SOURCES}")
130+
target_include_directories("${CJSON_LIB}" INTERFACE
131+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
132+
$<INSTALL_INTERFACE:include/cjson>
133+
)
131134
else()
132135
# See https://cmake.org/Wiki/CMake_FAQ#How_do_I_make_my_shared_and_static_libraries_have_the_same_root_name.2C_but_different_suffixes.3F
133-
add_library("${CJSON_LIB}" SHARED "${HEADERS}" "${SOURCES}")
134-
add_library("${CJSON_LIB}-static" STATIC "${HEADERS}" "${SOURCES}")
136+
add_library("${CJSON_LIB}" SHARED "${SOURCES}")
137+
target_include_directories("${CJSON_LIB}" INTERFACE
138+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
139+
$<INSTALL_INTERFACE:include/cjson>
140+
)
141+
add_library("${CJSON_LIB}-static" STATIC "${SOURCES}")
142+
target_include_directories("${CJSON_LIB}-static" INTERFACE
143+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
144+
$<INSTALL_INTERFACE:include/cjson>
145+
)
135146
set_target_properties("${CJSON_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_LIB}")
136147
set_target_properties("${CJSON_LIB}-static" PROPERTIES PREFIX "lib")
137148
endif()
@@ -142,7 +153,7 @@ endif()
142153
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
143154
"${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
144155

145-
install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
156+
install(FILES cjson/cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
146157
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
147158
install(TARGETS "${CJSON_LIB}"
148159
EXPORT "${CJSON_LIB}"
@@ -175,16 +186,27 @@ option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
175186
if(ENABLE_CJSON_UTILS)
176187
set(CJSON_UTILS_LIB cjson_utils)
177188

178-
file(GLOB HEADERS_UTILS cJSON_Utils.h)
179-
set(SOURCES_UTILS cJSON_Utils.c)
189+
set(SOURCES_UTILS cjson/cJSON_Utils.c)
180190

181191
if (NOT BUILD_SHARED_AND_STATIC_LIBS)
182-
add_library("${CJSON_UTILS_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
192+
add_library("${CJSON_UTILS_LIB}" "${CJSON_LIBRARY_TYPE}" "${SOURCES_UTILS}")
183193
target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
194+
target_include_directories("${CJSON_UTILS_LIB}" INTERFACE
195+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
196+
$<INSTALL_INTERFACE:include/cjson>
197+
)
184198
else()
185-
add_library("${CJSON_UTILS_LIB}" SHARED "${HEADERS_UTILS}" "${SOURCES_UTILS}")
199+
add_library("${CJSON_UTILS_LIB}" SHARED "${SOURCES_UTILS}")
200+
target_include_directories("${CJSON_UTILS_LIB}" INTERFACE
201+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
202+
$<INSTALL_INTERFACE:include/cjson>
203+
)
186204
target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
187-
add_library("${CJSON_UTILS_LIB}-static" STATIC "${HEADERS_UTILS}" "${SOURCES_UTILS}")
205+
add_library("${CJSON_UTILS_LIB}-static" STATIC "${SOURCES_UTILS}")
206+
target_include_directories("${CJSON_UTILS_LIB}-static" INTERFACE
207+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
208+
$<INSTALL_INTERFACE:include/cjson>
209+
)
188210
target_link_libraries("${CJSON_UTILS_LIB}-static" "${CJSON_LIB}-static")
189211
set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_UTILS_LIB}")
190212
set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES PREFIX "lib")
@@ -207,7 +229,7 @@ if(ENABLE_CJSON_UTILS)
207229
INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
208230
)
209231
endif()
210-
install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
232+
install(FILES cjson/cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
211233
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
212234
if(ENABLE_TARGET_EXPORT)
213235
# export library information for CMake projects
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

fuzzing/cjson_read_fuzzer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
extern "C" {
77
#endif
88

9-
#include "../cJSON.h"
9+
#include "../cjson/cJSON.h"
1010

1111
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); /* required by C89 */
1212

test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <stdio.h>
2424
#include <stdlib.h>
2525
#include <string.h>
26-
#include "cJSON.h"
26+
#include "cjson/cJSON.h"
2727

2828
/* Used by some code below as an example datatype. */
2929
struct record

tests/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#ifndef CJSON_TESTS_COMMON_H
2424
#define CJSON_TESTS_COMMON_H
2525

26-
#include "../cJSON.c"
26+
#include "../cjson/cJSON.c"
2727

2828
void reset(cJSON *item);
2929
void reset(cJSON *item) {

0 commit comments

Comments
 (0)