-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
245 lines (222 loc) · 7.56 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
cmake_minimum_required(VERSION 3.16...3.27)
# -- Configure project compiler options
project(spectrum LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(ENV{CC} ${CMAKE_C_COMPILER})
set(ENV{CXX} ${CMAKE_CXX_COMPILER})
option(NDEBUG ON)
find_package(Threads REQUIRED) # we need threads support for OS.
# -- Manage External Dependencies Using Github
function(Execute)
cmake_parse_arguments(EX "OUTPUT_QUIET;ERROR_QUIET" "WORKING_DIRECTORY;COMMAND_ERROR_IS_FATAL" "COMMAND" "${ARGV}")
list(JOIN EX_COMMAND " " EX_COMMAND)
message(STATUS "+ " ${EX_COMMAND})
execute_process(
${ARGN}
COMMAND_ERROR_IS_FATAL ANY
OUTPUT_QUIET ERROR_QUIET
)
endfunction()
function(GitHub)
cmake_parse_arguments(GH "" "USER;REPO;BRANCH;AS;PACK;FLAGS;DIR" "" "${ARGV}")
message(STATUS "GITHUB " ${GH_USER}/${GH_REPO}/${GH_BRANCH})
# download package from github
if (NOT EXISTS "${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/.git")
Execute(
COMMAND git clone --quiet --depth 1 --branch "${GH_BRANCH}" "https://github.com/${GH_USER}/${GH_REPO}" "${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND_ERROR_IS_FATAL ANY
OUTPUT_QUIET ERROR_QUIET
)
endif()
# configure how this package is added to current project
if(${GH_AS} STREQUAL "CMAKE SUBDIR")
set(CMAKE_MESSAGE_LOG_LEVEL__ ${CMAKE_MESSAGE_LOG_LEVEL})
set(CMAKE_MESSAGE_LOG_LEVEL ERROR)
add_subdirectory(${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO} EXCLUDE_FROM_ALL)
set(CMAKE_MESSAGE_LOG_LEVEL ${CMAKE_MESSAGE_LOG_LEVEL__})
elseif(${GH_AS} STREQUAL "CMAKE INSTALL" OR ${GH_AS} STREQUAL "CMAKE SCANLIB")
# run the build command if it targeted directory don't exists
if(NOT EXISTS ${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install)
Execute(
COMMAND cmake -S . -B build ${GH_FLAGS} -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}
)
Execute(
COMMAND cmake --build build -j
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}
)
Execute(
COMMAND cmake --build build --target install
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}
)
endif()
set(CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install")
if("${GH_PACK}" STREQUAL "")
set(GH_PACK ${GH_REPO})
endif()
if(${GH_AS} STREQUAL "CMAKE INSTALL")
find_package(${GH_PACK} REQUIRED QUIET)
elseif(${GH_AS} STREQUAL "CMAKE SCANLIB")
include_directories(${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install/include)
link_directories(${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install/lib)
endif()
elseif(${GH_AS} STREQUAL "AUTOMAKE INSTALL")
# run the build command if it targeted directory don't exists
if(NOT EXISTS ${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install)
Execute(
COMMAND ./autogen.sh --prefix ${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}
)
Execute(
COMMAND make install -j4
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}
)
endif()
include_directories(${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install/include)
link_directories(${CMAKE_BINARY_DIR}/_deps/${GH_USER}/${GH_REPO}/install/lib)
else()
message(FATAL_ERROR "UNKNOWN \"AUTOFIND OPTION\"")
endif()
endfunction()
GitHub(
USER "ethereum"
REPO "evmc"
BRANCH "v11.0.1"
AS "CMAKE SUBDIR"
)
GitHub(
USER "fmtlib"
REPO "fmt"
BRANCH "10.2.1"
AS "CMAKE INSTALL"
FLAGS "-DBUILD_SHARED_LIBS=TRUE"
)
GitHub(
USER "chfast"
REPO "ethash"
BRANCH "v1.0.1"
AS "CMAKE INSTALL"
)
GitHub(
USER "chfast"
REPO "intx"
BRANCH "v0.10.1"
AS "CMAKE INSTALL"
)
GitHub(
USER "gflags"
REPO "gflags"
BRANCH "v2.2.2"
AS "CMAKE INSTALL"
FLAGS "-DBUILD_SHARED_LIBS=ON"
)
GitHub(
USER "google"
REPO "glog"
BRANCH "v0.7.0"
AS "CMAKE INSTALL"
FLAGS "-Dgflags_DIR=${gflags_DIR}"
)
GitHub(
USER "google"
REPO "googletest"
BRANCH "v1.14.0"
PACK "GTest"
AS "CMAKE SUBDIR"
)
GitHub(
USER "gperftools"
REPO "gperftools"
BRANCH "gperftools-2.15"
AS "CMAKE SCANLIB"
)
GitHub(
USER "jemalloc"
REPO "jemalloc"
BRANCH "5.3.0"
AS "AUTOMAKE INSTALL"
)
# -- Manage Compile Options w/ ASAN flag
if(NDEBUG)
add_compile_options(
-O3 -Werror -Wno-attributes
-DNDEBUG=1
-DPROJECT_VERSION=""
-DEVMONE_X86_64_ARCH_LEVEL=2
)
else()
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
add_compile_options(
-O0 -ggdb -Werror -Wno-attributes
-DPROJECT_VERSION=""
-DEVMONE_X86_64_ARCH_LEVEL=2
)
endif()
# -- Process Main Library & Unit Tests
file(GLOB_RECURSE SRC lib/*.cpp lib/*.c)
file(GLOB_RECURSE INC lib/*.hpp lib/*.h)
file(GLOB_RECURSE SRC_TEST lib/*.test.cpp)
file(GLOB_RECURSE INC_TEST lib/*.test.hpp lib/*.test.h)
list(REMOVE_ITEM SRC ${SRC_TEST}) # remove *.test.cpp and *.test.hpp from target
list(REMOVE_ITEM INC ${INC_TEST})
foreach(F ${INC}) # copy headers
file(RELATIVE_PATH R ${CMAKE_CURRENT_SOURCE_DIR}/lib ${F})
configure_file(lib/${R} include/spectrum/${R} COPYONLY)
endforeach(F R)
add_library(spectrum STATIC ${SRC})
target_include_directories(
spectrum
PUBLIC ${CMAKE_BINARY_DIR}/include
)
target_compile_features(
spectrum
PUBLIC cxx_std_20
)
target_link_libraries(
spectrum
PUBLIC fmt::fmt evmc intx::intx glog::glog gflags
PRIVATE ethash::keccak Threads::Threads jemalloc
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
message(STATUS "SPECTRUM LIBRARY")
foreach(F ${SRC})
message(STATUS "+ " ${F})
endforeach(F R)
message(STATUS "UNIT TESTS")
enable_testing()
include(GoogleTest)
foreach(F ${SRC_TEST}) # unit tests
file(RELATIVE_PATH R ${CMAKE_CURRENT_SOURCE_DIR} ${F})
string(REPLACE "/" "-" R ${R})
string(REPLACE ".cpp" "" R ${R})
message(STATUS "+ " ${F})
add_executable(${R} ${F})
target_link_libraries(${R} GTest::gtest_main spectrum)
set_target_properties(${R}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Testing
)
gtest_add_tests(TARGET ${R})
endforeach(F N)
unset(SRC_TEST)
unset(INC_TEST)
unset(INC)
unset(SRC)
# -- Process Executables
file(GLOB_RECURSE SRC bin/*.cpp)
message(STATUS "EXECUTABLES")
foreach(F ${SRC})
message(STATUS "+ " ${F})
file(RELATIVE_PATH R ${CMAKE_CURRENT_SOURCE_DIR}/bin ${F})
string(REPLACE ".cpp" "" R ${R})
add_executable(${R} ${F})
target_link_libraries(${R} PRIVATE spectrum Threads::Threads)
endforeach(F R)
unset(SRC)