-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
210 lines (176 loc) · 7.03 KB
/
CMakeLists.txt
File metadata and controls
210 lines (176 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# ====================================================================
# @file CMakeLists.txt
# @author Gaspard Kirira
#
# Copyright 2025, Gaspard Kirira. All rights reserved.
# https://github.com/vixcpp/vix
# Use of this source code is governed by a MIT license
# that can be found in the License file.
#
# ====================================================================
# Vix.cpp - Json Module
# ====================================================================
# Purpose:
# Provides the JSON abstraction layer for Vix using nlohmann_json.
# Builds as a header-only INTERFACE target and can reuse an installed
# provider or fetch a fallback header-only copy for local builds.
#
# Public Targets:
# - vix_json : The actual interface library target
# - vix::json : Namespaced alias for consumers
#
# Public Dependencies:
# - nlohmann_json
#
# Installation/Export:
# - Umbrella build exports into: VixTargets
# - Standalone build exports into: vix_jsonTargets
# ====================================================================
cmake_minimum_required(VERSION 3.20)
project(vix_json VERSION 1.4.1 LANGUAGES CXX)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(FetchContent)
# --------------------------------------------------------------------
# Global settings
# --------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# --------------------------------------------------------------------
# Export set selection
# --------------------------------------------------------------------
if (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD)
set(VIX_JSON_EXPORT_SET VixTargets)
else()
set(VIX_JSON_EXPORT_SET vix_jsonTargets)
endif()
# --------------------------------------------------------------------
# Target
# --------------------------------------------------------------------
add_library(vix_json INTERFACE)
add_library(vix::json ALIAS vix_json)
set_target_properties(vix_json PROPERTIES
EXPORT_NAME json
)
target_compile_features(vix_json INTERFACE cxx_std_20)
target_include_directories(vix_json INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
set(_VIX_JSON_PROVIDER "none")
set(_VIX_JSON_INSTALL_FALLBACK OFF)
# --------------------------------------------------------------------
# Provider resolution
# Resolution policy:
# 1) Try installed CONFIG package
# 2) Try Find-module mode
# 3) Fallback to fetched header-only copy for local build
# --------------------------------------------------------------------
find_package(nlohmann_json QUIET CONFIG)
if (nlohmann_json_FOUND AND TARGET nlohmann_json::nlohmann_json)
target_link_libraries(vix_json INTERFACE nlohmann_json::nlohmann_json)
set(_VIX_JSON_PROVIDER "nlohmann_json (CONFIG)")
else()
find_package(nlohmann_json QUIET MODULE)
if (nlohmann_json_FOUND AND TARGET nlohmann_json::nlohmann_json)
target_link_libraries(vix_json INTERFACE nlohmann_json::nlohmann_json)
set(_VIX_JSON_PROVIDER "nlohmann_json (MODULE)")
else()
message(STATUS "[json] nlohmann_json not found, fetching header-only fallback")
FetchContent_Declare(
nlohmann_json_fallback
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(nlohmann_json_fallback)
# Build-time fallback only. Do not export fetched target itself.
target_include_directories(vix_json INTERFACE
$<BUILD_INTERFACE:${nlohmann_json_fallback_SOURCE_DIR}/single_include>
)
set(_VIX_JSON_PROVIDER "nlohmann_json (fetched header-only)")
set(_VIX_JSON_INSTALL_FALLBACK ON)
endif()
endif()
# --------------------------------------------------------------------
# Examples
# --------------------------------------------------------------------
option(VIX_JSON_BUILD_EXAMPLES "Build json examples" OFF)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
if (VIX_JSON_BUILD_EXAMPLES)
add_executable(vix_json_quick examples/quick_start.cpp)
target_link_libraries(vix_json_quick PRIVATE vix::json)
add_executable(vix_json_builders examples/builders.cpp)
target_link_libraries(vix_json_builders PRIVATE vix::json)
add_executable(vix_json_jpath examples/jpath.cpp)
target_link_libraries(vix_json_jpath PRIVATE vix::json)
add_executable(vix_json_io examples/io.cpp)
target_link_libraries(vix_json_io PRIVATE vix::json)
add_custom_target(vix_json_examples
DEPENDS
vix_json_quick
vix_json_builders
vix_json_jpath
vix_json_io
)
endif()
# --------------------------------------------------------------------
# Install headers (standalone only)
# --------------------------------------------------------------------
if (NOT (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD))
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h"
)
if (_VIX_JSON_INSTALL_FALLBACK)
install(
DIRECTORY "${nlohmann_json_fallback_SOURCE_DIR}/single_include/nlohmann"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
FILES_MATCHING PATTERN "*.hpp"
)
endif()
endif()
# --------------------------------------------------------------------
# Install target
# --------------------------------------------------------------------
install(TARGETS vix_json
EXPORT ${VIX_JSON_EXPORT_SET}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
# --------------------------------------------------------------------
# Standalone package config/export
# Only for non-umbrella builds
# --------------------------------------------------------------------
if (NOT (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD))
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/vix_jsonConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/vix_jsonConfig.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/vix_json"
)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/vix_jsonConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/vix_jsonConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/vix_jsonConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/vix_json"
)
install(EXPORT vix_jsonTargets
FILE vix_jsonTargets.cmake
NAMESPACE vix::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/vix_json"
)
endif()
# --------------------------------------------------------------------
# Summary
# --------------------------------------------------------------------
message(STATUS "------------------------------------------------------")
message(STATUS "vix::json configured (${PROJECT_VERSION})")
message(STATUS "Export set: ${VIX_JSON_EXPORT_SET}")
message(STATUS "Provider: ${_VIX_JSON_PROVIDER}")
message(STATUS "Examples: ${VIX_JSON_BUILD_EXAMPLES}")
message(STATUS "Install dir: ${CMAKE_INSTALL_INCLUDEDIR}")
message(STATUS "------------------------------------------------------")