forked from bitcoin-core/secp256k1
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
463 lines (362 loc) · 14.3 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#
# Copyright (c) 2017-2018 Bitprim Inc.
#
# This file is part of Bitprim.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
cmake_minimum_required(VERSION 3.4)
# secp256k1
#==============================================================================
project(secp256k1
VERSION 0
LANGUAGES C)
# VERSION 0.4.0
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Process options.
#==============================================================================
# Implement --use-conan
#------------------------------------------------------------------------------
option(USE_CONAN "Use Conan Build Tool." OFF)
option(NO_CONAN_AT_ALL "Conan totally disabled." OFF)
if (NO_CONAN_AT_ALL)
set(USE_CONAN OFF)
endif()
# Inherit --enable-shared
#------------------------------------------------------------------------------
option(ENABLE_SHARED "" OFF)
# Implement POSITION_INDEPENDENT_CODE
#------------------------------------------------------------------------------
option(ENABLE_POSITION_INDEPENDENT_CODE "Enable POSITION_INDEPENDENT_CODE property" ON)
# TODO(Bitprim): This options are not reflected on code... Se old Makefiles
option(ENABLE_BENCHMARK "Compile benchmark." OFF)
option(ENABLE_TESTS "Compile tests." ON)
option(ENABLE_OPENSSL_TESTS "Enable OpenSSL tests, if OpenSSL is available" OFF)
option(ENABLE_EXPERIMENTAL "Alloc experimental configure options" OFF)
option(ENABLE_ENDOMORPHISM "Enable endomorphism" OFF)
option(ENABLE_ECMULT_STATIC_PRECOMPUTATION "Enable precomputed ecmult table for signing" ON)
option(ENABLE_MODULE_ECDH "Enable ECDH shared secret computation (experimental)." OFF)
option(ENABLE_MODULE_SCHNORR "Enable Schnorr signature module (experimental)." ON)
option(ENABLE_MODULE_RECOVERY "Enable ECDSA pubkey recovery module." ON)
option(ENABLE_MODULE_MULTISET "Enable multiset operations (experimental)." ON)
# message(${ENABLE_TESTS})
# option(WITH_BIGNUM "")
#TODO(fernando): Implement the following with_... as options
set(WITH_ASM "auto" CACHE STRING "Specify assembly optimizations to use (x86_64|arm|no|auto).")
set(WITH_FIELD "auto" CACHE STRING "Specify Field Implementation (64bit|32bit|auto).")
set(WITH_SCALAR "auto" CACHE STRING "Specify scalar implementation (64bit|32bit|auto).")
set(WITH_BIGNUM "auto" CACHE STRING "Specify Bignum Implementation (gmp|mpir|no|auto).")
set(BITPRIM_PROJECT_VERSION "-" CACHE STRING "Specify the Bitprim Project Version.")
# message(${BITPRIM_PROJECT_VERSION})
set(MICROARCHITECTURE "x86_64" CACHE STRING "Specify the Cryptocurrency (x86_64|...).")
message( STATUS "Bitprim: Compiling for ${MICROARCHITECTURE}")
# add_definitions(-DBITPRIM_MICROARCHITECTURE=${MICROARCHITECTURE})
# add_definitions(-DBITPRIM_MICROARCHITECTURE_STR="${MICROARCHITECTURE}")
if(EXISTS ${CMAKE_SOURCE_DIR}/ci_utils/cmake/bitprimbuildinfo.cmake)
include(${CMAKE_SOURCE_DIR}/ci_utils/cmake/bitprimbuildinfo.cmake)
else()
message( STATUS "bitprimbuildinfo doent exists")
endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/ci_utils/cmake)
include(Secp256k1Tools)
#------------------------------------------------------------------------------
if (ENABLE_ENDOMORPHISM)
add_definitions(-DUSE_ENDOMORPHISM)
endif()
if (ENABLE_ECMULT_STATIC_PRECOMPUTATION)
# add_definitions(-DUSE_ECMULT_STATIC_PRECOMPUTATION)
set(USE_ECMULT_STATIC_PRECOMPUTATION 1)
if(EXISTS ${CMAKE_SOURCE_DIR}/ci_utils/cmake/NativeExecutable.cmake)
include(${CMAKE_SOURCE_DIR}/ci_utils/cmake/NativeExecutable.cmake)
else()
message( STATUS "NativeExecutable.cmake doent exists")
endif()
# # include(NativeExecutable)
add_native_executable(gen_context src/gen_context.c)
target_include_directories(gen_context PRIVATE
${CMAKE_CURRENT_SOURCE_DIR})
# target_include_directories(gen_context PUBLIC
# $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
# $<INSTALL_INTERFACE:include>)
add_custom_command(
OUTPUT ecmult_static_context.h
COMMAND gen_context
)
# target_sources(secp256k1 PRIVATE ecmult_static_context.h)
endif()
# Implement --with-asm
#------------------------------------------------------------------------------
if (WITH_ASM STREQUAL "auto")
_check_has_64bit_asm()
if (HAS_64BIT_ASM)
set(WITH_ASM "x86_64")
else()
set(WITH_ASM "no")
endif()
elseif (WITH_ASM STREQUAL "x86_64")
_check_has_64bit_asm()
if (NOT HAS_64BIT_ASM)
message(FATAL_ERROR "x86_64 assembly optimization requested but not available")
endif()
elseif (NOT WITH_ASM STREQUAL "arm" AND NOT WITH_ASM STREQUAL "no")
message(FATAL_ERROR "Invalid assembly optimization selection: ${WITH_ASM}")
endif()
# Implement --with-field
#------------------------------------------------------------------------------
if (WITH_FIELD STREQUAL "auto")
if (WITH_ASM STREQUAL "x86_64")
set (WITH_FIELD "64bit")
else()
_check_has_int128()
if (HAS_INT128)
set(WITH_FIELD "64bit")
else()
set(WITH_FIELD "32bit")
endif()
endif()
elseif (WITH_FIELD STREQUAL "64bit")
if (NOT WITH_ASM STREQUAL "x86_64")
_check_has_int128()
if (NOT HAS_INT128)
message(FATAL_ERROR "64bit field explicitly requested but neither __int128 support or x86_64 assembly available")
endif()
endif()
elseif (NOT WITH_FIELD STREQUAL "32bit")
message(FATAL_ERROR "Invalid field implementation: ${WITH_FIELD}")
endif()
# Implement --with-scalar
#------------------------------------------------------------------------------
if (WITH_SCALAR STREQUAL "auto")
_check_has_int128()
if (HAS_INT128)
set(WITH_SCALAR "64bit")
else()
set(WITH_SCALAR "32bit")
endif()
elseif (WITH_FIELD STREQUAL "64bit")
_check_has_int128()
if (NOT HAS_INT128)
message(FATAL_ERROR "64bit scalar explicitly requested but __int128 support not available")
endif()
elseif (NOT WITH_FIELD STREQUAL "32bit")
message(FATAL_ERROR "Invalid scalar implementation: ${WITH_SCALAR}")
endif()
#TODO(fernando): Bignum for Windows. See MPR (or something like this) that is a GMP for Windows.
# Implement --with-bignum
#------------------------------------------------------------------------------
if (NO_CONAN_AT_ALL)
if (${WITH_BIGNUM} STREQUAL "auto")
_check_has_gmp()
if (HAS_GMP)
set(WITH_BIGNUM "gmp")
else()
set(WITH_BIGNUM "no")
endif()
elseif (WITH_BIGNUM STREQUAL "gmp")
_check_has_gmp()
if (NOT HAS_GMP)
message(FATAL_ERROR "gmp bignum explicitly requested but libgmp not available")
endif()
elseif (NOT ${WITH_BIGNUM} STREQUAL "no")
message(FATAL_ERROR "Invalid bignum implementation: ${WITH_BIGNUM}")
endif()
endif()
if (ENABLE_SHARED)
set(MODE SHARED)
else()
set(MODE STATIC)
endif()
# Build
#==============================================================================
add_library(secp256k1 ${MODE}
include/secp256k1.h
src/secp256k1.c)
if (ENABLE_POSITION_INDEPENDENT_CODE)
set_property(TARGET secp256k1 PROPERTY POSITION_INDEPENDENT_CODE ON)
endif(ENABLE_POSITION_INDEPENDENT_CODE)
target_include_directories(secp256k1 PRIVATE
${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(secp256k1 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
_check_has_int128()
if (HAS_INT128)
target_compile_definitions(secp256k1 PUBLIC -DHAVE___INT128=1)
endif()
if (WITH_ASM STREQUAL "x86_64")
target_compile_definitions(secp256k1 PUBLIC -DUSE_ASM_X86_64=1)
elseif (WITH_FIELD STREQUAL "arm")
target_compile_definitions(secp256k1 PUBLIC -DUSE_EXTERNAL_ASM=1)
target_sources(secp256k1 PRIVATE "src/asm/field_10x26_arm.s")
endif()
if (WITH_FIELD STREQUAL "64bit")
target_compile_definitions(secp256k1 PUBLIC -DUSE_FIELD_5X52=1)
elseif (WITH_FIELD STREQUAL "32bit")
target_compile_definitions(secp256k1 PUBLIC -DUSE_FIELD_10X26=1)
endif()
if (WITH_SCALAR STREQUAL "64bit")
target_compile_definitions(secp256k1 PUBLIC -DUSE_SCALAR_4X64=1)
elseif (WITH_SCALAR STREQUAL "32bit")
target_compile_definitions(secp256k1 PUBLIC -DUSE_SCALAR_8X32=1)
endif()
# message("fer WITH_BIGNUM: ")
# message(WITH_ASM)
# message(WITH_FIELD)
# message(WITH_SCALAR)
# message(${WITH_BIGNUM})
# if (${WITH_BIGNUM} STREQUAL "auto")
# message("fer WITH_BIGNUM auto")
# else()
# message("fer WITH_BIGNUM auto - FALSE")
# endif()
# message("WITH_BIGNUM--FER-FER")
# message(${WITH_BIGNUM})
if (${WITH_BIGNUM} STREQUAL "gmp")
target_compile_definitions(secp256k1 PUBLIC -DHAVE_LIBGMP=1)
target_compile_definitions(secp256k1 PUBLIC -DUSE_NUM_GMP=1)
target_compile_definitions(secp256k1 PUBLIC -DUSE_FIELD_INV_NUM=1)
target_compile_definitions(secp256k1 PUBLIC -DUSE_SCALAR_INV_NUM=1)
if (NO_CONAN_AT_ALL)
target_include_directories(secp256k1 PUBLIC ${GMP_INCLUDE_DIR})
target_link_libraries(secp256k1 ${GMP_LIBRARIES})
endif()
elseif (${WITH_BIGNUM} STREQUAL "mpir")
target_compile_definitions(secp256k1 PUBLIC -DHAVE_LIBGMP=1)
target_compile_definitions(secp256k1 PUBLIC -DUSE_NUM_GMP=1)
target_compile_definitions(secp256k1 PUBLIC -DUSE_FIELD_INV_NUM=1)
target_compile_definitions(secp256k1 PUBLIC -DUSE_SCALAR_INV_NUM=1)
# if (NO_CONAN_AT_ALL)
# target_include_directories(secp256k1 PUBLIC ${GMP_INCLUDE_DIR})
# target_link_libraries(secp256k1 ${GMP_LIBRARIES})
# endif()
elseif (${WITH_BIGNUM} STREQUAL "no")
target_compile_definitions(secp256k1 PUBLIC -DUSE_NUM_NONE=1)
target_compile_definitions(secp256k1 PUBLIC -DUSE_FIELD_INV_BUILTIN=1)
target_compile_definitions(secp256k1 PUBLIC -DUSE_SCALAR_INV_BUILTIN=1)
endif()
target_compile_definitions(secp256k1 PUBLIC -DBITPRIM_PROJECT_VERSION="\\"${BITPRIM_PROJECT_VERSION}\\"") #TODO(fernando): manage with Conan????
if (ENABLE_ECMULT_STATIC_PRECOMPUTATION)
target_sources(secp256k1 PRIVATE ecmult_static_context.h)
endif()
# message(CONAN_LIBS)
# message(${CONAN_LIBS})
if (NOT NO_CONAN_AT_ALL)
target_link_libraries(secp256k1 PUBLIC ${CONAN_LIBS})
endif()
if (ENABLE_MODULE_ECDH)
target_compile_definitions(secp256k1 PUBLIC -DENABLE_MODULE_ECDH=1)
endif()
if (ENABLE_MODULE_SCHNORR)
target_compile_definitions(secp256k1 PUBLIC -DENABLE_MODULE_SCHNORR=1)
endif()
if (ENABLE_MODULE_RECOVERY)
target_compile_definitions(secp256k1 PUBLIC -DENABLE_MODULE_RECOVERY=1)
endif()
set_target_properties(secp256k1 PROPERTIES FOLDER "secp256k1")
# Benchmark
#==============================================================================
if (ENABLE_BENCHMARK)
add_executable(bench_verify src/bench_verify.c)
target_link_libraries(bench_verify secp256k1)
set_target_properties(bench_verify PROPERTIES FOLDER "secp256k1")
if (ENABLE_OPENSSL_TESTS)
target_compile_definitions(bench_verify PRIVATE -DENABLE_OPENSSL_TESTS)
endif()
add_executable(bench_sign src/bench_sign.c)
target_link_libraries(bench_sign secp256k1)
set_target_properties(bench_sign PROPERTIES FOLDER "secp256k1")
add_executable(bench_internal src/bench_internal.c)
target_link_libraries(bench_internal secp256k1)
set_target_properties(bench_internal PROPERTIES FOLDER "secp256k1")
if (ENABLE_MODULE_ECDH)
add_executable(bench_ecdh src/bench_ecdh.c)
target_link_libraries(bench_ecdh secp256k1)
set_target_properties(bench_ecdh PROPERTIES FOLDER "secp256k1")
endif()
if (ENABLE_MODULE_SCHNORR)
add_executable(bench_schnorr_verify src/bench_schnorr_verify.c)
target_link_libraries(bench_schnorr_verify secp256k1)
set_target_properties(bench_schnorr_verify PROPERTIES FOLDER "secp256k1")
endif()
if (ENABLE_MODULE_RECOVERY)
add_executable(bench_recover src/bench_recover.c)
target_link_libraries(bench_recover secp256k1)
set_target_properties(bench_recover PROPERTIES FOLDER "secp256k1")
endif()
if (ENABLE_MODULE_MULTISET)
add_executable(bench_multiset src/bench_multiset.c)
target_link_libraries(bench_multiset secp256k1)
set_target_properties(bench_multiset PROPERTIES FOLDER "secp256k1")
endif()
endif()
# Tests
#==============================================================================
if (ENABLE_TESTS)
add_executable(tests
src/tests.c)
if (ENABLE_OPENSSL_TESTS)
target_compile_definitions(tests PRIVATE -DENABLE_OPENSSL_TESTS)
endif()
target_compile_definitions(tests PRIVATE -DVERIFY)
target_include_directories(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(tests secp256k1)
enable_testing()
add_test(
NAME test
COMMAND tests)
set_target_properties(tests PROPERTIES FOLDER "secp256k1")
endif()
# Install
#==============================================================================
install(TARGETS secp256k1
EXPORT secp256k1
ARCHIVE DESTINATION lib)
install(FILES include/secp256k1.h DESTINATION include)
if (ENABLE_MODULE_ECDH)
install(FILES include/secp256k1_ecdh.h DESTINATION include)
endif()
if (ENABLE_MODULE_SCHNORR)
install(FILES include/secp256k1_schnorr.h DESTINATION include)
endif()
if (ENABLE_MODULE_RECOVERY)
install(FILES include/secp256k1_recovery.h DESTINATION include)
endif()
# Export
#==============================================================================
include(CMakePackageConfigHelpers)
export(EXPORT secp256k1
FILE "${CMAKE_CURRENT_BINARY_DIR}/secp256k1Targets.cmake")
export(PACKAGE secp256k1)
if (UNIX)
set(_config_package_location "lib/cmake")
elseif (WIN32)
set(_config_package_location "cmake")
endif()
install(EXPORT secp256k1
FILE secp256k1Targets.cmake
DESTINATION ${_config_package_location})
configure_file(secp256k1Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/secp256k1Config.cmake"
@ONLY)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/secp256k1ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/secp256k1Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/secp256k1ConfigVersion.cmake"
DESTINATION ${_config_package_location})