This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
forked from jedisct1/libsodium
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMakeLists.txt
535 lines (436 loc) · 12.2 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# Part of the work taken from
# * https://github.com/jedisct1/libsodium/pull/156
# * https://github.com/jedisct1/libsodium/pull/181
# * https://github.com/Cyberunner23/libsodium-CMake
cmake_minimum_required(VERSION 3.2) # minimal for Hunter
include("cmake/HunterGate.cmake")
HunterGate(
URL "https://github.com/ruslo/hunter/archive/v0.23.0.tar.gz"
SHA1 "4c27150185a31de4f65cfbb787c9561473c5b435"
)
project(libsodium VERSION 1.0.16)
find_package(Threads REQUIRED)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/module")
include(libsodium_add_c_flag)
include(libsodium_add_have_macro)
include(libsodium_try_compile)
include(TestBigEndian) # test_big_endian
include(CheckIncludeFile) # check_include_file
option(
LIBSODIUM_ENABLE_BLOCKING_RANDOM
"Enable this switch only if /dev/urandom is totally broken on the target platform."
OFF
)
option(
LIBSODIUM_ENABLE_MINIMAL_BUILD
"Only compile the minimum set of functions required for the high-level API."
OFF
)
option(LIBSODIUM_ENABLE_TESTS "Enable testing." OFF)
if(EMSCRIPTEN)
set(default YES)
else()
set(default NO)
endif()
option(
LIBSODIUM_DISABLE_ASM "Disable AMD64 assembly implementations." ${default}
)
option(LIBSODIUM_DISABLE_SSP "Do not compile with -fstack-protector." NO)
if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4146")
endif()
if(LIBSODIUM_DISABLE_ASM)
set(LIBSODIUM_AMD64_ASM FALSE)
else()
libsodium_try_compile(
SOURCE amd64_asm.c
CMAKE_VAR LIBSODIUM_AMD64_ASM
CPP_MACRO HAVE_AMD64_ASM
)
if(LIBSODIUM_AMD64_ASM)
enable_language(ASM)
endif()
endif()
if(MSVC)
if(MSVC_VERSION GREATER 1900)
set(LIBSODIUM_MSVC_BROKEN FALSE)
else()
# Visual Studio 14 2015 internal compiler error
set(LIBSODIUM_MSVC_BROKEN TRUE)
endif()
else()
set(LIBSODIUM_MSVC_BROKEN FALSE)
endif()
if(NOT LIBSODIUM_MSVC_BROKEN)
libsodium_try_compile(
SOURCE avxintrin.c
CMAKE_VAR LIBSODIUM_HAVE_AVXINTRIN_H
CPP_MACRO HAVE_AVXINTRIN_H
)
endif()
libsodium_try_compile(
SOURCE avx_asm.c
CMAKE_VAR LIBSODIUM_HAVE_AVX_ASM
CPP_MACRO HAVE_AVX_ASM
)
if(LIBSODIUM_HAVE_AVX_ASM)
enable_language(ASM)
endif()
libsodium_try_compile(
SOURCE native_client.c
CMAKE_VAR LIBSODIUM_NATIVE
)
# Variables from configure.ac {
set(SODIUM_LIBRARY_VERSION_MAJOR 10)
set(SODIUM_LIBRARY_VERSION_MINOR 1)
set(SODIUM_LIBRARY_VERSION_PATCH 0) # ???
set(SODIUM_LIBRARY_VERSION "24:0:1")
if(LIBSODIUM_ENABLE_MINIMAL_BUILD)
set(SODIUM_LIBRARY_MINIMAL_DEF "#define SODIUM_LIBRARY_MINIMAL 1")
add_definitions("-DMINIMAL=1")
else()
set(SODIUM_LIBRARY_MINIMAL_DEF "")
endif()
# }
set(VERSION ${PROJECT_VERSION})
# Use:
# * SODIUM_LIBRARY_MINIMAL_DEF
# * SODIUM_LIBRARY_VERSION_MAJOR
# * SODIUM_LIBRARY_VERSION_MINOR
# * VERSION
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/src/libsodium/include/sodium/version.h.in
${CMAKE_CURRENT_BINARY_DIR}/generated/sodium/version.h
)
test_big_endian(LIBSODIUM_BIG_ENDIAN)
if(LIBSODIUM_BIG_ENDIAN)
set(libsodium_endian_macro "-DNATIVE_BIG_ENDIAN=1")
else()
set(libsodium_endian_macro "-DNATIVE_LITTLE_ENDIAN=1")
endif()
add_definitions(${libsodium_endian_macro})
if(MSVC)
add_definitions("-DUNICODE")
add_definitions("-D_UNICODE")
endif()
libsodium_try_compile(
SOURCE ti_mode.c
CMAKE_VAR LIBSODIUM_TI_MODE
CPP_MACRO HAVE_TI_MODE
COMPILE_DEFINITIONS ${libsodium_endian_macro}
)
libsodium_try_compile(
SOURCE weak_symbols.c
CMAKE_VAR LIBSODIUM_HAVE_WEAK_SYMBOLS
CPP_MACRO HAVE_WEAK_SYMBOLS
)
libsodium_try_compile(
SOURCE cpuid.c
CMAKE_VAR LIBSODIUM_HAVE_CPUID
CPP_MACRO HAVE_CPUID
)
libsodium_try_compile(
SOURCE limit_macros.c
CMAKE_VAR LIBSODIUM_LIMIT_MACROS_NOT_NEEDED
)
if(LIBSODIUM_LIMIT_MACROS_NOT_NEEDED)
# If this works then we dont need to define __STDC_LIMIT_MACROS
# and __STDC_CONSTANT_MACROS.
else()
add_definitions("-D__STDC_LIMIT_MACROS")
add_definitions("-D__STDC_CONSTANT_MACROS")
endif()
if(NOT LIBSODIUM_MSVC_BROKEN)
libsodium_try_compile(
SOURCE emmintrin.c
CMAKE_VAR LIBSODIUM_HAVE_EMMINTRIN_H
CPP_MACRO HAVE_EMMINTRIN_H
)
libsodium_try_compile(
SOURCE pmmintrin.c
CMAKE_VAR LIBSODIUM_HAVE_PMMINTRIN_H
CPP_MACRO HAVE_PMMINTRIN_H
)
libsodium_try_compile(
SOURCE tmmintrin.c
CMAKE_VAR LIBSODIUM_HAVE_TMMINTRIN_H
CPP_MACRO HAVE_TMMINTRIN_H
)
libsodium_try_compile(
SOURCE smmintrin.c
CMAKE_VAR LIBSODIUM_HAVE_SMMINTRIN_H
CPP_MACRO HAVE_SMMINTRIN_H
)
libsodium_try_compile(
SOURCE wmmintrin.c
CMAKE_VAR LIBSODIUM_HAVE_WMMINTRIN_H
CPP_MACRO HAVE_WMMINTRIN_H
)
endif()
libsodium_try_compile(
SOURCE memset.c
CMAKE_VAR LIBSODIUM_HAVE_MEMSET_S
CPP_MACRO HAVE_MEMSET_S
)
if(NOT LIBSODIUM_MSVC_BROKEN)
libsodium_try_compile(
SOURCE avx2intrin.c
CMAKE_VAR LIBSODIUM_HAVE_AVX2INTRIN_H
CPP_MACRO HAVE_AVX2INTRIN_H
)
endif()
libsodium_try_compile(
SOURCE avx512fintrin.c
CMAKE_VAR LIBSODIUM_HAVE_AVX512FINTRIN_H
CPP_MACRO HAVE_AVX512FINTRIN_H
)
libsodium_try_compile(
SOURCE rdrand.c
CMAKE_VAR LIBSODIUM_HAVE_RDRAND
CPP_MACRO HAVE_RDRAND
)
libsodium_try_compile(
SOURCE xgetbv.c
CMAKE_VAR LIBSODIUM_HAVE__XGETBV
CPP_MACRO HAVE__XGETBV
)
libsodium_try_compile(
SOURCE atomic_ops.c
CMAKE_VAR LIBSODIUM_HAVE_ATOMIC_OPS
CPP_MACRO HAVE_ATOMIC_OPS
)
libsodium_add_have_macro(
FUNCTION arc4random
CMAKE_VAR LIBSODIUM_HAVE_ARC4RANDOM
CPP_MACRO HAVE_ARC4RANDOM
)
libsodium_add_have_macro(
FUNCTION explicit_bzero
CMAKE_VAR LIBSODIUM_HAVE_EXPLICIT_BZERO
CPP_MACRO HAVE_EXPLICIT_BZERO
)
libsodium_add_have_macro(
FUNCTION madvise
CMAKE_VAR LIBSODIUM_HAVE_MADVISE
CPP_MACRO HAVE_MADVISE
)
libsodium_add_have_macro(
FUNCTION mlock
CMAKE_VAR LIBSODIUM_HAVE_MLOCK
CPP_MACRO HAVE_MLOCK
)
libsodium_add_have_macro(
FUNCTION mmap
CMAKE_VAR LIBSODIUM_HAVE_MMAP
CPP_MACRO HAVE_MMAP
)
libsodium_add_have_macro(
FUNCTION mprotect
CMAKE_VAR LIBSODIUM_HAVE_MPROTECT
CPP_MACRO HAVE_MPROTECT
)
libsodium_add_have_macro(
FUNCTION posix_memalign
CMAKE_VAR LIBSODIUM_HAVE_POSIX_MEMALIGN
CPP_MACRO HAVE_POSIX_MEMALIGN
)
# Update CMAKE_C_FLAGS {
libsodium_add_c_flag(-std=c99)
libsodium_add_c_flag(-D_FORTIFY_SOURCE=2)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
libsodium_add_c_flag(-Wno-unknown-warning-option)
endif()
if(MSYS OR MINGW OR CYGWIN)
libsodium_add_c_flag(-Wl,--dynamicbase)
libsodium_add_c_flag(-Wl,--nxcompat)
endif()
libsodium_add_c_flag(-Wall)
libsodium_add_c_flag(-Wextra)
libsodium_add_c_flag(-fno-strict-aliasing)
libsodium_add_c_flag(-fno-strict-overflow available)
# If -fno-strict-overflow is not available, try with -fwrapv.
if(NOT available)
libsodium_add_c_flag(-fwrapv)
endif()
if(NOT LIBSODIUM_DISABLE_SSP)
libsodium_add_c_flag(-fstack-protector)
endif()
libsodium_add_c_flag(-Winit-self)
libsodium_add_c_flag(-Wwrite-strings)
libsodium_add_c_flag(-Wdiv-by-zero)
libsodium_add_c_flag(-Wsometimes-uninitialized)
libsodium_add_c_flag(-Wbad-function-cast)
libsodium_add_c_flag(-Wcast-align)
libsodium_add_c_flag(-Wcast-qual)
libsodium_add_c_flag(-Wchar-subscripts)
libsodium_add_c_flag(-Wcomment)
libsodium_add_c_flag(-Wfloat-equal)
libsodium_add_c_flag(-Wformat=2)
libsodium_add_c_flag(-Wimplicit)
libsodium_add_c_flag(-Wmissing-declarations)
libsodium_add_c_flag(-Wmissing-prototypes)
libsodium_add_c_flag(-Wnormalized=id)
libsodium_add_c_flag(-Woverride-init)
libsodium_add_c_flag(-Wparentheses)
libsodium_add_c_flag(-Wpointer-arith)
libsodium_add_c_flag(-Wredundant-decls)
libsodium_add_c_flag(-Wstrict-prototypes)
libsodium_add_c_flag(-Wswitch-enum)
libsodium_add_c_flag(-Wvariable-decl)
if(NOT WIN32 AND NOT APPLE)
libsodium_add_c_flag(-Wl,-z,relro)
libsodium_add_c_flag(-Wl,-z,now)
libsodium_add_c_flag(-Wl,-z,noexecstack)
endif()
# }
if(LIBSODIUM_ENABLE_BLOCKING_RANDOM)
add_definitions("-DUSE_BLOCKING_RANDOM")
endif()
# Collect sources {
hunter_add_package(sugar)
find_package(sugar CONFIG REQUIRED)
# Use:
# * LIBSODIUM_AMD64_ASM
# * LIBSODIUM_MSVC_BROKEN
# * LIBSODIUM_ENABLE_MINIMAL_BUILD
# * LIBSODIUM_HAVE_AVX_ASM
# * LIBSODIUM_NATIVE
# Out:
# * LIBSODIUM_SOURCES
# * LIBSODIUM_TESTS_SOURCES
sugar_include(src/libsodium)
sugar_include(test/default)
# }
if(MSVC AND BUILD_SHARED_LIBS)
configure_file(
"src/msvc/resource.rc.in"
"${CMAKE_CURRENT_BINARY_DIR}/generated/resource.rc"
@ONLY
)
# Use:
# * SODIUM_LIBRARY_VERSION_MAJOR
# * SODIUM_LIBRARY_VERSION_MINOR
# * SODIUM_LIBRARY_VERSION_PATCH
configure_file(
"src/msvc/resource.h"
"${CMAKE_CURRENT_BINARY_DIR}/generated/resource.h"
COPYONLY
)
# append resource to DLL
list(
APPEND
LIBSODIUM_SOURCES
"${CMAKE_CURRENT_BINARY_DIR}/generated/resource.rc"
)
endif()
add_library(libsodium ${LIBSODIUM_SOURCES})
target_link_libraries(libsodium PUBLIC Threads::Threads)
if(CMAKE_USE_PTHREADS_INIT)
target_compile_definitions(libsodium PRIVATE "-DHAVE_PTHREAD")
endif()
if(BUILD_SHARED_LIBS)
target_compile_definitions(libsodium PRIVATE "-DSODIUM_DLL_EXPORT")
else()
target_compile_definitions(libsodium PUBLIC "-DSODIUM_STATIC")
endif()
target_compile_definitions(libsodium PRIVATE "$<$<CONFIG:Debug>:DEBUG=1>")
check_include_file("sys/mman.h" LIBSODIUM_SYS_MMAN_H)
if(LIBSODIUM_SYS_MMAN_H)
target_compile_definitions(libsodium PUBLIC $<BUILD_INTERFACE:HAVE_SYS_MMAN_H>)
endif()
target_include_directories(
libsodium
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated/sodium>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/libsodium/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/libsodium/include/sodium>
)
### Installation (https://github.com/forexample/package-example) {
# Introduce variables:
# * CMAKE_INSTALL_LIBDIR
# * CMAKE_INSTALL_BINDIR
# * CMAKE_INSTALL_INCLUDEDIR
include(GNUInstallDirs)
set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(namespace "${PROJECT_NAME}::")
include(CMakePackageConfigHelpers)
# Use:
# * PROJECT_VERSION
write_basic_package_version_file(
"${version_config}" COMPATIBILITY SameMajorVersion
)
# Use:
# * TARGETS_EXPORT_NAME
# * PROJECT_NAME
configure_package_config_file(
"cmake/template/Config.cmake.in"
"${project_config}"
INSTALL_DESTINATION "${config_install_dir}"
)
install(
DIRECTORY "src/libsodium/include/sodium"
DESTINATION "include"
FILES_MATCHING
PATTERN "*.h"
PATTERN "*.h.in" EXCLUDE
REGEX "^${CMAKE_CURRENT_LIST_DIR}/src/libsodium/include/sodium/private/" EXCLUDE
)
install(
FILES "src/libsodium/include/sodium.h"
DESTINATION "include"
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/generated/sodium/version.h
DESTINATION "include/sodium"
)
install(
FILES "${project_config}" "${version_config}"
DESTINATION "${config_install_dir}"
)
install(
EXPORT "${TARGETS_EXPORT_NAME}"
NAMESPACE "${namespace}"
DESTINATION "${config_install_dir}"
)
install(
TARGETS libsodium
EXPORT "${TARGETS_EXPORT_NAME}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
### }
if(LIBSODIUM_ENABLE_TESTS)
enable_testing()
foreach(src ${LIBSODIUM_TESTS_SOURCES})
get_filename_component(target_name "${src}" NAME_WE)
add_executable(${target_name} ${src})
target_link_libraries(${target_name} PUBLIC libsodium)
target_compile_definitions(
${target_name}
PUBLIC
"-DTEST_SRCDIR=\"${CMAKE_CURRENT_LIST_DIR}/test/default\""
)
if(MSVC)
target_compile_definitions(
${target_name}
PUBLIC
"-D_CRT_SECURE_NO_WARNINGS"
)
endif()
target_include_directories(
${target_name} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/test/quirks
)
add_test(NAME ${target_name} COMMAND ${target_name})
endforeach()
endif()
# TODO:
# * HAVE_CATCHABLE_ABRT
# * HAVE_CATCHABLE_SEGV
# * HAVE_MMINTRIN_H