-
Notifications
You must be signed in to change notification settings - Fork 6
/
CMakeLists.txt
99 lines (84 loc) · 3.17 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
cmake_minimum_required(VERSION 3.16)
#
# COMPILER/LINKER FLAGS
#
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ENV{CMAKE_EXPORT_COMPILE_COMMANDS} ON)
if(NOT MSVC)
# Warnings
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Wextra -Wcast-align -Wcast-qual -Wdelete-non-virtual-dtor -Wold-style-cast -Woverloaded-virtual -Wpointer-arith -Wfloat-equal -Winit-self -Wredundant-decls -Wsign-promo -Wswitch-default -Wswitch-enum -Wvariadic-macros -Wwrite-strings -Wno-unknown-pragmas"
)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow=local -Wno-pragmas")
endif()
else() # MSVC including clang-cl
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /utf-8")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
# O3 optimization
if(NOT MSVC)
string(REPLACE "-O2" "-O3" CMAKE_CXX_FLAGS_RELWITHDEBINFO
${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
string(REPLACE "-O2" "-O3" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
endif()
# GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
# executables
add_executable("${PROJECT_NAME}_test" test.cpp)
target_link_libraries("${PROJECT_NAME}_test"
PRIVATE ${PROJECT_NAME} GTest::gtest GTest::gtest_main)
# add google test
include(GoogleTest)
gtest_discover_tests("${PROJECT_NAME}_test" DISCOVERY_MODE PRE_TEST)
# benchmark
if(CPPYYJSON_BUILD_BENCH)
# Google benchmark
find_package(benchmark REQUIRED)
# JSON libraries
find_package(simdjson CONFIG REQUIRED)
find_package(RapidJSON CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
# executables
add_executable("${PROJECT_NAME}_bench_read" bench_read.cpp)
target_link_libraries(
"${PROJECT_NAME}_bench_read" PRIVATE ${PROJECT_NAME} benchmark::benchmark
simdjson::simdjson rapidjson)
add_executable("${PROJECT_NAME}_bench_write" bench_write.cpp)
target_link_libraries(
"${PROJECT_NAME}_bench_write"
PRIVATE ${PROJECT_NAME} benchmark::benchmark rapidjson nlohmann_json
nlohmann_json::nlohmann_json)
endif()
# compile commands
if(CMAKE_EXPORT_COMPILE_COMMANDS)
add_custom_target(gen_compile_commands ALL
COMMENT "Generating compile_commands.json")
# Generate a compilation database with header files
find_program(COMPDB compdb)
if(COMPDB)
add_custom_command(
TARGET gen_compile_commands
PRE_BUILD
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${COMPDB} list > ${PROJECT_SOURCE_DIR}/compile_commands.json
COMMENT "Running compdb")
elseif(NOT WIN32)
message(AUTHOR_WARNING "'compdb' not found")
# Create symbolic link instead of compdb
add_custom_command(
TARGET gen_compile_commands
PRE_BUILD
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND
${CMAKE_COMMAND} -E create_symlink
${CMAKE_BINARY_DIR}/compile_commands.json
${PROJECT_SOURCE_DIR}/compile_commands.json
COMMENT "Creating symbolic link")
endif()
endif()