-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
131 lines (104 loc) · 3.83 KB
/
Copy pathCMakeLists.txt
File metadata and controls
131 lines (104 loc) · 3.83 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
cmake_minimum_required(VERSION 3.20)
project(Reflection VERSION 0.1.0 LANGUAGES CXX)
include(GNUInstallDirs)
option(REFLECTION_BUILD_TESTS "Build test executable" OFF)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(MSVC)
# Match xmake default on Windows: /MD
cmake_policy(SET CMP0091 NEW)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
endif()
# Header-only library target
add_library(reflection INTERFACE)
add_library(reflection::reflection ALIAS reflection)
target_include_directories(reflection
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_features(reflection INTERFACE cxx_std_23)
if(WIN32)
target_compile_definitions(reflection INTERFACE NOMINMAX UNICODE)
endif()
if(MSVC)
target_compile_options(reflection INTERFACE /utf-8 /W4)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
target_compile_options(reflection INTERFACE -Wall -Wextra)
endif()
if(NOT MSVC)
target_compile_options(reflection INTERFACE -fvisibility=hidden)
endif()
include(FetchContent)
# Disable JSONC C API in this project; only C++ API is needed.
set(JSONC_BUILD_C_API OFF CACHE BOOL "Disable JSONC C API" FORCE)
set(JSONC_NO_EXCEPTION ON CACHE BOOL "Disable JSONC exceptions" FORCE)
FetchContent_Declare(
jsonc
GIT_REPOSITORY https://github.com/SculkCatalystMC/JSONC.git
GIT_TAG main
)
FetchContent_Declare(
boost_pfr
GIT_REPOSITORY https://github.com/boostorg/pfr.git
GIT_TAG 2.2.0
)
FetchContent_Declare(
magic_enum
GIT_REPOSITORY https://github.com/Neargye/magic_enum.git
GIT_TAG v0.9.7
)
FetchContent_MakeAvailable(jsonc boost_pfr magic_enum)
if(TARGET magic_enum::magic_enum)
target_link_libraries(reflection INTERFACE $<BUILD_INTERFACE:magic_enum::magic_enum>)
endif()
if(TARGET boost_pfr)
target_link_libraries(reflection INTERFACE $<BUILD_INTERFACE:boost_pfr>)
elseif(TARGET Boost::pfr)
target_link_libraries(reflection INTERFACE $<BUILD_INTERFACE:Boost::pfr>)
endif()
if(TARGET jsonc::jsonc)
target_link_libraries(reflection INTERFACE $<BUILD_INTERFACE:jsonc::jsonc>)
elseif(TARGET jsonc)
target_link_libraries(reflection INTERFACE $<BUILD_INTERFACE:jsonc>)
endif()
if(NOT TARGET magic_enum::magic_enum)
message(FATAL_ERROR "magic_enum fetch failed or target is unavailable.")
endif()
if(NOT TARGET boost_pfr AND NOT TARGET Boost::pfr)
message(FATAL_ERROR "boost_pfr fetch failed or target is unavailable.")
endif()
if(NOT TARGET jsonc::jsonc AND NOT TARGET jsonc)
message(FATAL_ERROR "jsonc fetch failed or target is unavailable.")
endif()
if(REFLECTION_BUILD_TESTS)
add_executable(test
test/main.cpp
test/test_jsonc.cpp
)
target_include_directories(test PRIVATE test)
target_link_libraries(test PRIVATE reflection::reflection)
if(MSVC)
target_compile_options(test PRIVATE /EHsc)
endif()
if(MSVC)
target_compile_options(test PRIVATE $<$<CONFIG:Release>:/O2>)
else()
target_compile_options(test PRIVATE $<$<CONFIG:Release>:-O3>)
endif()
add_custom_command(TARGET test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "$<IF:$<BOOL:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}>,${CMAKE_RUNTIME_OUTPUT_DIRECTORY},${CMAKE_CURRENT_BINARY_DIR}>/../bin"
COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:test>" "$<IF:$<BOOL:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}>,${CMAKE_RUNTIME_OUTPUT_DIRECTORY},${CMAKE_CURRENT_BINARY_DIR}>/../bin/"
COMMENT "Copy test binary to bin directory after build"
)
endif()
install(TARGETS reflection EXPORT reflectionTargets)
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(EXPORT reflectionTargets
FILE reflectionTargets.cmake
NAMESPACE reflection::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflection
)