-
Notifications
You must be signed in to change notification settings - Fork 18
/
CMakeLists.txt
416 lines (358 loc) · 15.2 KB
/
CMakeLists.txt
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File: CMakeLists used for generating a Makefile (or whatever your build system is).
#
# Copyright 2015-2018 by Travis Gockel
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
cmake_minimum_required(VERSION 3.2)
project(json-voorhees)
set(PACKAGE_NAME "jsonv"
CACHE STRING "The packaging name of this library."
)
################################################################################
# Configuration #
################################################################################
if (CMAKE_VERSION VERSION_GREATER "3.0")
cmake_policy(SET CMP0042 NEW) # MACOSX_RPATH
cmake_policy(SET CMP0037 OLD) # Allow use of reserved names
endif()
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
message(STATUS "No build type selected, default to ${CMAKE_BUILD_TYPE}")
endif()
message(STATUS "Configuration: ${CMAKE_BUILD_TYPE}")
# Extract version information from the config file. It can still be overridden by the user setting JSONV_VERSION, but
# that is probably a bad idea.
file(READ include/jsonv/config.hpp JSONV_CONFIG_H_STR)
string(REGEX REPLACE ".*#define +JSONV_VERSION_MAJOR +([0-9]+).*" "\\1" JSONV_VERSION_MAJOR "${JSONV_CONFIG_H_STR}")
string(REGEX REPLACE ".*#define +JSONV_VERSION_MINOR +([0-9]+).*" "\\1" JSONV_VERSION_MINOR "${JSONV_CONFIG_H_STR}")
string(REGEX REPLACE ".*#define +JSONV_VERSION_PATCH +([0-9]+).*" "\\1" JSONV_VERSION_PATCH "${JSONV_CONFIG_H_STR}")
set(JSONV_VERSION "${JSONV_VERSION_MAJOR}.${JSONV_VERSION_MINOR}.${JSONV_VERSION_PATCH}")
set(JSONV_SO_VERSION "${JSONV_VERSION_MAJOR}.${JSONV_VERSION_MINOR}")
message(STATUS "JSONV version: ${JSONV_VERSION} (${JSONV_SO_VERSION})")
set(CXX_STANDARD_DEFAULT "c++14")
if (CMAKE_COMPILER_IS_GNUCC)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
set(CXX_STANDARD_DEFAULT "c++1y")
endif()
endif()
set(CXX_STANDARD ${CXX_STANDARD_DEFAULT}
CACHE STRING "The version of the language to use (only applies to GCC and Clang)."
)
option(JSONV_BUILD_TESTS
"Should the unit test library get built?"
OFF
)
set(REQUIRED_BOOST_LIBRARIES)
if (JSONV_BUILD_TESTS)
list(APPEND REQUIRED_BOOST_LIBRARIES "filesystem" "system")
endif()
#############################
# String View Configuration #
#############################
option(USE_BOOST_STRING_VIEW
"Controls the variable JSONV_STRING_VIEW_USE_BOOST (see C++ documentation)."
OFF
)
if (USE_BOOST_STRING_VIEW)
add_definitions("-DJSONV_STRING_VIEW_USE_BOOST=1")
endif(USE_BOOST_STRING_VIEW)
option(USE_STD_STRING_VIEW
"Controls the variable JSONV_STRING_VIEW_USE_STD (see C++ documentation)."
OFF
)
if (USE_STD_STRING_VIEW)
add_definitions("-DJSONV_STRING_VIEW_USE_STD=1")
endif(USE_STD_STRING_VIEW)
##########################
# Optional Configuration #
##########################
option(USE_STD_OPTIONAL
"Controls the variable JSONV_OPTIONAL_USE_STD (see C++ documentation)."
OFF
)
if (USE_STD_OPTIONAL)
add_definitions("-DJSONV_OPTIONAL_USE_STD=1")
endif()
option(USE_STD_EXPERIMENTAL_OPTIONAL
"Controls the variable JSONV_OPTIONAL_USE_STD_EXPERIMENTAL (see C++ documentation)."
OFF
)
if (USE_STD_EXPERIMENTAL_OPTIONAL)
add_definitions("-DJSONV_OPTIONAL_USE_STD_EXPERIMENTAL=1")
endif()
option(USE_BOOST_OPTIONAL
"Controls the variable JSONV_OPTIONAL_USE_BOOST (see C++ documentation)."
OFF
)
if (USE_BOOST_OPTIONAL)
add_definitions("-DJSONV_OPTIONAL_USE_BOOST=1")
endif()
##########################
# Coverage Configuration #
##########################
option(COVERAGE
"Enable building with code coverage."
OFF
)
if (COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
endif(COVERAGE)
if (WIN32)
# DLLs in Windows appear to have not been fully thought through
set(DEFAULT_LIBRARY_TYPE "STATIC")
else()
set(DEFAULT_LIBRARY_TYPE "SHARED")
endif()
set(LIBRARY_TYPE ${DEFAULT_LIBRARY_TYPE}
CACHE STRING "The type of library to build (SHARED or STATIC)"
)
option(BENCHMARK
"Enable compilation of the json-benchmark program."
OFF
)
if(WIN32)
else(WIN32)
# Reasonable compilers...
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=${CXX_STANDARD} -pedantic -Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb -DJSONV_DEBUG=1")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ggdb")
endif(WIN32)
include_directories("${PROJECT_SOURCE_DIR}/include"
"${PROJECT_SOURCE_DIR}/src"
)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost
COMPONENTS "${REQUIRED_BOOST_LIBRARIES}"
REQUIRED
)
include_directories(${Boost_INCLUDE_DIRS})
add_definitions("-DJSONV_TEST_DATA_DIR=\"${CMAKE_SOURCE_DIR}/src/jsonv-tests/data\"")
configure_file(libjsonv.pc.in libjsonv.pc)
################################################################################
# Building #
################################################################################
file(GLOB_RECURSE jsonv_cpps RELATIVE_PATH "." "src/jsonv/*.cpp")
add_library(jsonv ${LIBRARY_TYPE} ${jsonv_cpps})
set_target_properties(jsonv
PROPERTIES
SOVERSION ${JSONV_SO_VERSION}
VERSION ${JSONV_SO_VERSION}
)
if (Boost_LIBRARIES)
target_link_libraries(jsonv ${Boost_LIBRARIES})
endif()
if (JSONV_BUILD_TESTS)
file(GLOB_RECURSE jsonv_tests_cpps RELATIVE_PATH "." "src/jsonv-tests/*.cpp")
add_executable(jsonv-tests ${jsonv_tests_cpps})
target_link_libraries(jsonv-tests
"jsonv"
${Boost_LIBRARIES}
)
add_custom_target(check
COMMAND $<TARGET_FILE:jsonv-tests>
DEPENDS jsonv-tests
USES_TERMINAL
)
endif()
if (COVERAGE)
target_link_libraries(jsonv "gcov")
if (JSONV_BUILD_TESTS)
target_link_libraries(jsonv-tests "gcov")
endif()
endif(COVERAGE)
################################################################################
# Benchmarking Tool #
################################################################################
option(BENCHMARK_JSONV
"Enable benchmark suite for JSON Voorhees?"
YES
)
option(BENCHMARK_JSONCPP
"Enable benchmark suite for JsonCpp?"
YES
)
option(BENCHMARK_JQ
"Enable benchmark suite for jq?"
NO
)
option(BENCHMARK_JANSSON
"Enable benchmark suite for jansson?"
NO
)
if (BENCHMARK)
set(BENCHMARK_CPPS src/json-benchmark/core.cpp src/json-benchmark/main.cpp)
set(BENCHMARK_LIBS "")
macro(add_benchmark_suite CPP_FILE LIB_NAME)
list(APPEND BENCHMARK_CPPS "src/json-benchmark/${CPP_FILE}")
list(APPEND BENCHMARK_LIBS "${LIB_NAME}")
endmacro()
if (BENCHMARK_JSONV)
add_benchmark_suite("jsonv_benchmark.cpp" "jsonv")
endif()
if (BENCHMARK_JSONCPP)
add_benchmark_suite("jsoncpp_benchmark.cpp" "jsoncpp")
include_directories("/usr/include/jsoncpp")
endif()
if (BENCHMARK_JQ)
add_benchmark_suite("jq_benchmark.cpp" "jq")
endif()
if (BENCHMARK_JANSSON)
add_benchmark_suite("jansson_benchmark.cpp" "jansson")
endif()
add_executable(json-benchmark ${BENCHMARK_CPPS})
target_link_libraries(json-benchmark
${BENCHMARK_LIBS}
${Boost_LIBRARIES}
)
endif(BENCHMARK)
################################################################################
# Installation #
################################################################################
# directories
if (OS_WINDOWS)
set(RUNTIME_INSTALL_DIR Bin)
set(INCLUDE_INSTALL_DIR Include)
set(LIBRARY_INSTALL_DIR Lib)
set(CONFIG_INSTALL_DIR CMake)
else()
set(RUNTIME_INSTALL_DIR bin)
set(INCLUDE_INSTALL_DIR include)
set(LIBRARY_INSTALL_DIR_DEFAULT lib)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(LIBRARY_INSTALL_DIR_DEFAULT lib64)
endif ()
set(LIBRARY_INSTALL_DIR ${LIBRARY_INSTALL_DIR_DEFAULT}
CACHE STRING "Directory of installed libraries, e.g., \"lib64\""
)
mark_as_advanced(LIBRARY_INSTALL_DIR)
set(CONFIG_INSTALL_DIR "${LIBRARY_INSTALL_DIR}/cmake/${PACKAGE_NAME}")
endif ()
foreach(area RUNTIME LIBRARY INCLUDE CONFIG)
if (NOT IS_ABSOLUTE "${${area}_INSTALL_DIR}")
set(${area}_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${${area}_INSTALL_DIR}")
endif()
message(STATUS "${area}_INSTALL_DIR == ${${area}_INSTALL_DIR}")
endforeach()
install(TARGETS jsonv
EXPORT exported
LIBRARY DESTINATION "${LIBRARY_INSTALL_DIR}"
ARCHIVE DESTINATION "${LIBRARY_INSTALL_DIR}"
)
install(DIRECTORY "include/jsonv"
DESTINATION "${INCLUDE_INSTALL_DIR}"
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjsonv.pc DESTINATION "${LIBRARY_INSTALL_DIR}/pkgconfig")
install(EXPORT exported DESTINATION "${CONFIG_INSTALL_DIR}" FILE "${PACKAGE_NAME}-export.cmake")
################################################################################
# Packaging #
################################################################################
set(JSONV_PACKAGE_SYSTEM ""
CACHE STRING "The packaging system to generate a package build file for (leave blank to not build a package)"
)
if (JSONV_PACKAGE_SYSTEM)
message(STATUS "Creating packaging for ${JSONV_PACKAGE_SYSTEM}...")
set(PROJECT_PACKAGE_VERSION "${JSONV_VERSION}-1") # TODO: Allow arbitrary tags here
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(PROJECT_BUILD_ARCHITECTURE "amd64")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
set(PROJECT_BUILD_ARCHITECTURE "i386")
else()
message(WARNING " Unknown architecture \"${CMAKE_SYSTEM_PROCESSOR}\" -- assuming packaging is okay with that")
set(PROJECT_BUILD_ARCHITECTURE "${CMAKE_SYSTEM_PROCESSOR}")
endif()
set(all_packages)
if(JSONV_PACKAGE_SYSTEM STREQUAL "DEB")
set(pkg_basename "libjsonv")
set(pkg_fullname "libjsonv${JSONV_SO_VERSION}")
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/installer/deb/${pkg_basename}")
message(WARNING "Could not find packager \"${CMAKE_CURRENT_SOURCE_DIR}/installer/deb/${pkg_basename}\"")
else()
message(STATUS " + ${pkg_fullname}.deb")
set(library_pkg_base "${CMAKE_CURRENT_SOURCE_DIR}/installer/deb/${pkg_basename}")
set(library_pkg_stage "${CMAKE_CURRENT_BINARY_DIR}/intermediate/deb/${pkg_fullname}")
set(library_pkg_dest "${CMAKE_CURRENT_BINARY_DIR}/installer/${pkg_fullname}")
configure_file("${library_pkg_base}/control.in"
"${library_pkg_dest}/DEBIAN/control"
@ONLY IMMEDIATE
)
if(EXISTS "${library_pkg_base}/postinst.in")
configure_file("${library_pkg_base}/postinst.in"
"${library_pkg_stage}/DEBIAN/postinst"
@ONLY IMMEDIATE
)
file(COPY "${library_pkg_stage}/DEBIAN/postinst"
DESTINATION "${library_pkg_dest}/DEBIAN"
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)
endif()
add_custom_target("${pkg_fullname}.deb"
COMMAND rm -rf "installer/${pkg_fullname}/usr/local"
COMMAND mkdir -p "installer/${pkg_fullname}/usr/local/lib"
COMMAND cp "$<TARGET_FILE:jsonv>" "installer/${pkg_fullname}/usr/local/lib"
COMMAND dpkg --build "installer/${pkg_fullname}"
COMMAND mv "installer/${pkg_fullname}.deb" "."
DEPENDS
"${library_pkg_dest}/DEBIAN/control"
jsonv
)
list(APPEND all_packages "${pkg_fullname}.deb")
endif()
set(pkg_basename "libjsonv-dev")
set(pkg_fullname "libjsonv${JSONV_SO_VERSION}-dev")
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/installer/deb/${pkg_basename}")
message(WARNING "Could not find packager \"${CMAKE_CURRENT_SOURCE_DIR}/installer/deb/${pkg_basename}\"")
else()
message(STATUS " + ${pkg_fullname}.deb")
set(library_pkg_base "${CMAKE_CURRENT_SOURCE_DIR}/installer/deb/${pkg_basename}")
set(library_pkg_stage "${CMAKE_CURRENT_BINARY_DIR}/intermediate/deb/${pkg_fullname}")
set(library_pkg_dest "${CMAKE_CURRENT_BINARY_DIR}/installer/${pkg_fullname}")
configure_file("${library_pkg_base}/control.in"
"${library_pkg_dest}/DEBIAN/control"
@ONLY IMMEDIATE
)
file(GLOB_RECURSE jsonv_headers RELATIVE_PATH "." "include/jsonv/*")
add_custom_target("${pkg_fullname}.deb"
COMMAND rm -rf "installer/${pkg_fullname}/usr/local"
COMMAND mkdir -p "installer/${pkg_fullname}/usr/local/include"
COMMAND cp -r "${PROJECT_SOURCE_DIR}/include/jsonv" "installer/${pkg_fullname}/usr/local/include"
COMMAND dpkg --build "installer/${pkg_fullname}"
COMMAND mv "installer/${pkg_fullname}.deb" "."
DEPENDS
"${library_pkg_dest}/DEBIAN/control"
${jsonv_headers}
)
list(APPEND all_packages "${pkg_fullname}.deb")
endif()
if(all_packages)
add_custom_target(package
DEPENDS ${all_packages}
COMMENT "Built all packages"
)
else()
message(SEND_ERROR "Packaging specified as \"${JSONV_PACKAGE_SYSTEM}\", but did not find any actual packages.")
endif()
elseif(JSONV_PACKAGE_SYSTEM STREQUAL "RPM")
set(CPACK_PACKAGE_VERSION "${JSONV_SO_VERSION}")
set(CPACK_GENERATOR "${JSONV_PACKAGE_SYSTEM}")
set(CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
set(CPACK_PACKAGE_RELEASE 1)
set(CPACK_PACKAGE_CONTACT "Travis Gockel")
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_RELEASE}.${CMAKE_SYSTEM_PROCESSOR}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/installer/rpm/jsonv.spec.in"
"${CMAKE_CURRENT_BINARY_DIR}/installer/rpm/jsonv.spec"
@ONLY IMMEDIATE
)
set(CPACK_RPM_USER_BINARY_SPECFILE "${CMAKE_CURRENT_BINARY_DIR}/installer/rpm/jsonv.spec")
include(CPack)
else()
message(SEND_ERROR "Unknown JSONV_PACKAGE_SYSTEM=${JSONV_PACKAGE_SYSTEM}")
endif()
endif()