-
Notifications
You must be signed in to change notification settings - Fork 283
/
CMakeLists.txt
268 lines (219 loc) · 8.34 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
CMAKE_MINIMUM_REQUIRED(VERSION 3.14 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE "RELEASE")
ENDIF()
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()
project(chiapos C CXX ASM)
# CMake 3.14+
include(FetchContent)
if (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
include(${CMAKE_INSTALL_PREFIX}/share/cmake/pybind11/pybind11Config.cmake)
else()
FetchContent_Declare(
pybind11-src
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.11.1
)
FetchContent_MakeAvailable(pybind11-src)
endif()
FetchContent_Declare(
cxxopts
GIT_REPOSITORY https://github.com/jarro2783/cxxopts.git
GIT_TAG v3.1.1
)
FetchContent_MakeAvailable(cxxopts)
option(CP_LINK_BLADEBIT_HARVESTER "Links libbladebit_harvester at build time instead of dynamically loading it." OFF)
option(CP_BUILD_BLADEBIT_HARVESTER "Pulls bladebit harvester target from git and builds it as a dependency.")
if (${CP_BUILD_BLADEBIT_HARVESTER} AND NOT ${CP_LINK_BLADEBIT_HARVESTER})
set(CP_LINK_BLADEBIT_HARVESTER ON)
endif()
if (${CP_BUILD_BLADEBIT_HARVESTER})
FetchContent_Declare(
bladebit
GIT_REPOSITORY https://github.com/Chia-Network/bladebit.git
GIT_TAG cuda-compression
)
set(BB_HARVESTER_ONLY ON)
set(BB_HARVESTER_STATIC ON)
FetchContent_MakeAvailable(bladebit)
endif()
set(FSE_LIB ${CMAKE_CURRENT_SOURCE_DIR}/lib/FiniteStateEntropy/lib)
set(FSE_FILES
${FSE_LIB}/fse_compress.c
${FSE_LIB}/fse_decompress.c
${FSE_LIB}/entropy_common.c
${FSE_LIB}/hist.c
)
include_directories(
${INCLUDE_DIRECTORIES}
${CMAKE_CURRENT_SOURCE_DIR}/../lib/include
${cxxopts_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../lib/FiniteStateEntropy/lib
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/test
)
IF (${CP_LINK_BLADEBIT_HARVESTER})
message ("Bladebit Harvesting Enabled")
ENDIF ()
add_library(fse ${FSE_FILES})
IF (MSVC)
IF (CMAKE_BUILD_TYPE STREQUAL "RELEASE")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2 /Oy")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /O2 /Oy")
ELSE()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Ob1")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Ob1")
ENDIF()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Zi")
ELSE()
IF (CMAKE_BUILD_TYPE STREQUAL "RELEASE")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
ELSE()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
ENDIF()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -g")
ENDIF()
IF (CMAKE_BUILD_TYPE STREQUAL "ASAN")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -fno-omit-frame-pointer -fsanitize=address -fsanitize=undefined")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -fno-omit-frame-pointer -fsanitize=address -fsanitize=undefined")
set (CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address -fsanitize=undefined")
ENDIF()
IF (CMAKE_BUILD_TYPE STREQUAL "TSAN")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -fno-omit-frame-pointer -fsanitize=thread")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -fno-omit-frame-pointer -fsanitize=thread")
set (CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
ENDIF()
pybind11_add_module(chiapos ${CMAKE_CURRENT_SOURCE_DIR}/python-bindings/chiapos.cpp src/chacha8.c)
add_executable(ProofOfSpace
src/cli.cpp
src/chacha8.c
)
FetchContent_Declare(
blake3
GIT_REPOSITORY https://github.com/BLAKE3-team/BLAKE3.git
GIT_TAG 1.5.0
)
FetchContent_GetProperties(blake3)
if(NOT blake3_POPULATED)
FetchContent_Populate(blake3)
# Set BLAKE3 to build as a static library
set(BUILD_SHARED_LIBS FALSE CACHE BOOL "Build static libraries" FORCE)
add_subdirectory(${blake3_SOURCE_DIR}/c ${blake3_BINARY_DIR})
endif()
set(BLAKE3_SRC ${blake3_SOURCE_DIR}/c)
set(BLAKE3_INCLUDE_DIR ${blake3_SOURCE_DIR}/c)
set_target_properties(blake3 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib$<0:>")
target_link_libraries(chiapos PRIVATE blake3)
target_link_libraries(ProofOfSpace PRIVATE blake3)
include_directories(
${INCLUDE_DIRECTORIES}
${BLAKE3_INCLUDE_DIR}
)
option(BUILD_PROOF_OF_SPACE_STATICALLY "Build ProofOfSpace target statically" OFF)
IF (BUILD_PROOF_OF_SPACE_STATICALLY)
message("Statically build ProofOfSpace")
target_link_libraries(ProofOfSpace PUBLIC -static -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)
ENDIF()
option(BUILD_STATIC_CHIAPOS_LIBRARY "Build chiapos static library (verify-only)" OFF)
IF (BUILD_STATIC_CHIAPOS_LIBRARY)
message("Build chiapos static library (verify-only)")
add_library(chiapos_static STATIC src/chacha8.c c-bindings/wrapper.cpp)
target_link_libraries(chiapos_static PRIVATE blake3 $<$<CXX_COMPILER_ID:MSVC>:uint128>)
target_include_directories(chiapos_static PUBLIC lib/include uint128_t)
set_target_properties(chiapos_static PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib$<0:>")
ENDIF()
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.5.2
)
FetchContent_MakeAvailable(Catch2)
add_executable(RunTests
tests/test.cpp
src/chacha8.c
)
target_link_libraries(RunTests
PRIVATE
fse
Threads::Threads
Catch2::Catch2
blake3
)
if(WITH_COVERAGE)
target_compile_options(RunTests PRIVATE --coverage)
target_link_options(RunTests PRIVATE --coverage)
endif()
find_package(Threads REQUIRED)
add_library(uint128 STATIC uint128_t/uint128_t.cpp)
target_include_directories(uint128 PUBLIC uint128_t)
set_target_properties(uint128 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib$<0:>")
target_compile_features(fse PUBLIC cxx_std_17)
target_compile_features(chiapos PUBLIC cxx_std_17)
if (WIN32)
# workaround for constexpr mutex constructor change in MSVC 2022
# https://stackoverflow.com/questions/78598141/first-stdmutexlock-crashes-in-application-built-with-latest-visual-studio
target_compile_definitions(chiapos PUBLIC _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
endif()
# target_compile_features(RunTests PUBLIC cxx_std_17)
target_link_libraries(chiapos PRIVATE fse Threads::Threads
$<$<CXX_COMPILER_ID:MSVC>:uint128>
$<$<NOT:$<PLATFORM_ID:Darwin,OpenBSD,FreeBSD,Windows>>:stdc++fs>
)
target_link_libraries(ProofOfSpace PRIVATE fse Threads::Threads
$<$<CXX_COMPILER_ID:MSVC>:uint128>
$<$<NOT:$<PLATFORM_ID:Darwin,OpenBSD,FreeBSD,Windows>>:stdc++fs>
)
target_link_libraries(RunTests PRIVATE fse Threads::Threads Catch2::Catch2WithMain
$<$<CXX_COMPILER_ID:MSVC>:uint128>
$<$<NOT:$<PLATFORM_ID:Darwin,OpenBSD,FreeBSD,Windows>>:stdc++fs>
)
if (${CP_LINK_BLADEBIT_HARVESTER})
set(bb_defs
USE_GREEN_REAPER=1
BLADEBIT_HARVESTER_LINKED=1
$<$<BOOL:${CP_BUILD_BLADEBIT_HARVESTER}>:BLADEBIT_IS_PROJECT_DEPENDENCY=1>
)
set(bb_libs
bladebit_harvester
$<$<NOT:$<PLATFORM_ID:Windows>>:dl>
)
include_directories(
${INCLUDE_DIRECTORIES}
${CMAKE_CURRENT_SOURCE_DIR}/libs/green_reaper/include
)
link_directories(
${LINK_DIRECTORIES}
${CMAKE_SOURCE_DIR}/libs/green_reaper/lib
)
target_compile_definitions(chiapos PUBLIC ${bb_defs})
target_compile_definitions(ProofOfSpace PUBLIC ${bb_defs})
target_compile_definitions(RunTests PUBLIC ${bb_defs})
target_link_libraries(chiapos PUBLIC ${bb_libs})
target_link_libraries(ProofOfSpace PUBLIC ${bb_libs})
target_link_libraries(RunTests PUBLIC ${bb_libs})
target_link_directories(chiapos PUBLIC ${CMAKE_SOURCE_DIR}/libs/green_reaper/lib)
target_link_directories(ProofOfSpace PUBLIC ${CMAKE_SOURCE_DIR}/libs/green_reaper/lib)
target_link_directories(RunTests PUBLIC ${CMAKE_SOURCE_DIR}/libs/green_reaper/lib)
set_property(TARGET chiapos APPEND PROPERTY BUILD_RPATH "$ORIGIN")
set_property(TARGET ProofOfSpace APPEND PROPERTY BUILD_RPATH "$ORIGIN")
set_property(TARGET RunTests APPEND PROPERTY BUILD_RPATH "$ORIGIN")
if (WIN32)
add_custom_command(TARGET chiapos POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${CMAKE_SOURCE_DIR}/libs/green_reaper/lib/bladebit_harvester.dll"
"$<TARGET_FILE_DIR:chiapos>/bladebit_harvester.dll"
)
message("The bladebit dll was copied to: $<TARGET_FILE_DIR:chiapos>/bladebit_harvester.dll")
endif()
endif()
enable_testing()
add_test(NAME RunTests COMMAND RunTests)