forked from jedisct1/libsodium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meson.build
664 lines (609 loc) · 17.4 KB
/
meson.build
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
project(
'libsodium', 'c',
version : '1.0.14',
license : 'ISC',
default_options : ['c_std=c99', 'b_staticpic=true', 'warning_level=2'],
)
version_conf = configuration_data()
version_conf.set('SODIUM_LIBRARY_VERSION_MAJOR', 9)
version_conf.set('SODIUM_LIBRARY_VERSION_MINOR', 6)
version_conf.set_quoted('SODIUM_VERSION_STRING', meson.project_version())
dll_version = 8
sodium_library_version = '18.4.0'
compiler = meson.get_compiler('c')
add_project_arguments('-DCONFIGURED=1', language : 'c')
add_project_arguments('-D__EXTENSIONS__=1', language : 'c')
add_project_arguments('-D_ALL_SOURCE=1', language : 'c')
add_project_arguments('-D_GNU_SOURCE=1', language : 'c')
add_project_arguments('-D_POSIX_PTHREAD_SEMANTICS=1', language : 'c')
add_project_arguments('-D_TANDEM_SOURCE=1', language : 'c')
is_nativeclient = compiler.get_define('__native_client__') != ''
is_pnacl = is_nativeclient and compiler.get_define('__pnacl__') != ''
is_emscripten = compiler.get_define('__EMSCRIPTEN__') != ''
is_win_posix = false # TODO: Detection
if is_nativeclient
add_project_arguments('-DNATIVECLIENT=1', language : 'c')
endif
if is_emscripten
add_project_arguments('-DEMSCRIPTEN=1', language : 'c')
endif
# TODO: Safecode
enable_asm = get_option('asm')
if is_nativeclient or is_emscripten
enable_asm = false
message('Compiling to Emscripten/Native Client - asm implementations disabled')
endif
if get_option('minimal')
version_conf.set('SODIUM_LIBRARY_MINIMAL', 1, description: 'Minimal build')
endif
if get_option('pthread')
threads = dependency('threads')
add_project_arguments('-DHAVE_PTHREAD=1', language : 'c')
else
threads = declare_dependency()
endif
# Set optimization flags
if get_option('optimize')
if compiler.has_argument('-Ofast')
add_project_arguments('-Ofast', language : 'c')
endif
if compiler.has_argument('-march=native')
add_project_arguments('-march=native', language : 'c')
endif
endif
# _FORTIFY_SOURCE support
if compiler.get_define('_FORTIFY_SOURCE') == ''
if compiler.has_argument('-D_FORTIFY_SOURCE=2')
add_project_arguments('-D_FORTIFY_SOURCE=2', language : 'c')
endif
endif
if get_option('pie') and not is_win_posix and compiler.has_argument('-fPIE')
add_project_arguments('-fPIE', language : 'c')
endif
wanted_arguments = [
'-fvisibility=hidden',
'-fno-strict-aliasing',
'-Wwrite-strings',
'-Wdiv-by-zero',
'-Wsometimes-uninitialized',
'-Wbad-function-cast',
'-Wcast-align',
'-Wcast-qual',
'-Wduplicated-cond',
'-Wfloat-equal',
'-Wformat=2',
'-Wmissing-declarations',
'-Wmissing-prototypes',
'-Wnested-externs',
'-Wno-type-limits',
'-Wno-unknown-pragmas',
'-Wnormalized=id',
'-Wnull-dereference',
'-Wold-style-declaration',
'-Wpointer-arith',
'-Wredundant-decls',
'-Wshorten-64-to-32',
'-Wstrict-prototypes',
'-Wswitch-enum',
'-Wvariable-decl',
]
if compiler.get_id() == 'clang'
wanted_arguments += '-Wno-unknown-warning-option'
endif
foreach wanted : wanted_arguments
if compiler.has_argument(wanted)
add_project_arguments(wanted, language : 'c')
endif
endforeach
wanted_arguments = [
'-Wl,-z,relro',
'-Wl,-z,now',
'-Wl,-z,noexecstack',
]
foreach wanted : wanted_arguments
if compiler.has_argument(wanted)
add_project_link_arguments(wanted, language : 'c')
endif
endforeach
if is_win_posix
wanted_arguments = ['-Wl,--dynamicbase', '-Wl,--high-entropy-va', '-Wl,--nxcompat', '-fno-asynchronous-unwind-tables']
foreach wanted : wanted_arguments
if compiler.has_argument(wanted)
add_project_arguments(wanted, language : 'c')
endif
endforeach
endif
supported_overflow_args = compiler.first_supported_argument(['-fno-strict-overflow', '-fwrapv'])
add_project_arguments(supported_overflow_args, language : 'c')
enable_ssp = get_option('ssp')
if enable_ssp and not is_win_posix and compiler.has_argument('-fstack-protector')
# TODO: Test linker against -fstack-protector
add_project_arguments('-fstack-protector', language : 'c')
endif
if compiler.get_id() == 'clang' and compiler.version().version_compare('< 4') and compiler.get_define('__AVX512F__') != ''
message('Broken clang AVX512 optimization - optimization disabled')
add_project_arguments('-mno-avx512f', language : 'c')
endif
test_catchable_segv = compiler.run('''
#include <signal.h>
#include <stdlib.h>
static void sig(int _) { exit(0); }
int main(){
volatile unsigned char * volatile x = (volatile unsigned char *) malloc(8);
size_t i;
signal(SIGSEGV, sig);
signal(SIGBUS, sig);
#if !defined(__SANITIZE_ADDRESS__) && !defined(__EMSCRIPTEN__)
for (i = 0; i < 10000000; i += 1024) { x[-i] = x[i] = (unsigned char) i; }
#endif
free((void *) x);
exit(1);
}
''')
if test_catchable_segv.returncode() == 0
message('Segmentation violations can be caught when using the _AC_LANG compiler')
add_project_arguments('-DHAVE_CATCHABLE_SEGV=1', language : 'c')
else
message(test_catchable_segv.returncode())
message('On this platform, segmentation violations cannot be caught using signal handlers. This is expected if you enabled a tool such as Address Sanitizer (-fsanitize=address), but be aware that using Address Sanitizer may also significantly reduce performance.')
endif
test_catchable_abrt = compiler.run('''
#include <signal.h>
#include <stdlib.h>
#ifndef SIGABRT
# error SIGABRT is not defined
#endif
static void sigabrt_handler_3(int _)
{
exit(0);
}
static void sigabrt_handler_2(int _)
{
signal(SIGABRT, sigabrt_handler_3);
abort();
exit(1);
}
static void sigabrt_handler_1(int _)
{
signal(SIGABRT, sigabrt_handler_2);
abort();
exit(1);
}
int main(){
signal(SIGABRT, sigabrt_handler_1);
abort();
exit(1);
}
''')
if test_catchable_abrt.returncode() == 0
message('SIGABRT can be caught using signal handlers')
add_project_arguments('-DHAVE_CATCHABLE_ABRT=1', language : 'c')
else
message(test_catchable_abrt.returncode())
message('On this platform, SIGABRT cannot be caught using signal handlers')
endif
if not is_emscripten and not is_pnacl
mmx = declare_dependency()
compiler_args = []
if compiler.has_argument('-mmmx')
mmx = declare_dependency(compile_args : '-mmmx')
compiler_args += '-mmmx'
endif
has_mmx = compiler.compiles('''
#pragma GCC target("mmx")
#include <mmintrin.h>
int main(){
__m64 x = _mm_setzero_si64();
}
''', args : compiler_args)
if has_mmx
message('MMX is available')
add_project_arguments('-DHAVE_MMINTRIN_H=1', language : 'c')
else
message('MMX is NOT available')
endif
sse2 = declare_dependency()
compiler_args = []
if compiler.has_argument('-msse2')
sse2 = declare_dependency(compile_args : '-msse2')
compiler_args += '-msse2'
endif
has_sse2 = compiler.compiles('''
#pragma GCC target("sse2")
#ifndef __SSE2__
# define __SSE2__
#endif
#include <emmintrin.h>
int main(){
__m128d x = _mm_setzero_pd();
__m128i z = _mm_srli_epi64(_mm_setzero_si128(), 26);
}
''', args: compiler_args)
if has_sse2
message('SSE2 is available')
add_project_arguments('-DHAVE_EMMINTRIN_H=1', language : 'c')
else
message('SSE2 is NOT available')
endif
sse3 = declare_dependency()
compiler_args = []
if compiler.has_argument('-msse3')
sse3 = declare_dependency(compile_args : '-msse3')
compiler_args += '-msse3'
endif
has_sse3 = compiler.compiles('''
#pragma GCC target("sse3")
#include <pmmintrin.h>
int main(){
__m128 x = _mm_addsub_ps(_mm_cvtpd_ps(_mm_setzero_pd()), _mm_cvtpd_ps(_mm_setzero_pd()));
}
''', args: compiler_args)
if has_sse3
message('SSE3 is available')
add_project_arguments('-DHAVE_PMMINTRIN_H=1', language : 'c')
else
message('SSE3 is NOT available')
endif
ssse3 = declare_dependency()
compiler_args = []
if compiler.has_argument('-mssse3')
ssse3 = declare_dependency(compile_args : '-mssse3')
compiler_args += '-mssse3'
endif
has_ssse3 = compiler.compiles('''
#pragma GCC target("ssse3")
#include <tmmintrin.h>
int main(){
__m64 x = _mm_abs_pi32(_m_from_int(0));
}
''', args : compiler_args)
if has_ssse3
message('SSSE3 is available')
add_project_arguments('-DHAVE_TMMINTRIN_H=1', language : 'c')
else
message('SSSE3 is NOT available')
endif
sse4_1 = declare_dependency()
compiler_args = []
if compiler.has_argument('-msse4.1')
sse4_1 = declare_dependency(compile_args : '-msse4.1')
compiler_args += '-msse4.1'
endif
has_sse4_1 = compiler.compiles('''
#pragma GCC target("sse4.1")
#include <smmintrin.h>
int main(){
__m128i x = _mm_minpos_epu16(_mm_setzero_si128());
}
''', args : compiler_args)
if has_sse4_1
message('SSE4.1 is available')
add_project_arguments('-DHAVE_SMMINTRIN_H=1', language : 'c')
else
message('SSE4.1 is NOT available')
endif
avx = declare_dependency()
if not is_nativeclient
compiler_args = []
if compiler.has_argument('-mavx')
avx = declare_dependency(compile_args : '-mavx')
compiler_args += '-mavx'
endif
has_avx = compiler.compiles('''
#pragma GCC target("avx")
#include <immintrin.h>
int main(){
_mm256_zeroall();
}
''', args : compiler_args)
if has_avx
message('AVX is available')
add_project_arguments('-DHAVE_AVXINTRIN_H=1', language : 'c')
else
message('AVX is NOT available')
endif
endif
avx2 = declare_dependency()
if not is_nativeclient
compiler_args = []
if compiler.has_argument('-mavx2')
avx2 = declare_dependency(compile_args : '-mavx2')
compiler_args += '-mavx2'
endif
has_avx2 = compiler.compiles('''
#pragma GCC target("avx2")
#include <immintrin.h>
int main(){
__m256 x = _mm256_set1_ps(3.14);
__m256 y = _mm256_permutevar8x32_ps(x, _mm256_set1_epi32(42));
return _mm256_movemask_ps(_mm256_cmp_ps(x, y, _CMP_NEQ_OQ));
}
''', args : compiler_args)
if has_avx2
message('AVX2 is available')
add_project_arguments('-DHAVE_AVX2INTRIN_H=1', language : 'c')
if not compiler.compiles('''
#pragma GCC target("avx2")
#include <immintrin.h>
int main(){
__m256i y = _mm256_broadcastsi128_si256(_mm_setzero_si128());
}
''', args : compiler_args)
add_project_arguments('-D_mm256_broadcastsi128_si256=_mm_broadcastsi128_si256', language : 'c')
endif
else
message('AVX2 is NOT available')
endif
endif
avx512f = declare_dependency()
if not is_nativeclient
compiler_args = []
if compiler.has_argument('-mavx512f')
avx512f = declare_dependency(compile_args : '-mavx512f')
compiler_args += '-mavx512f'
endif
has_mavx512f = compiler.compiles('''
#pragma GCC target("avx512f")
#include <immintrin.h>
int main(){
__m512i x = _mm512_setzero_epi32();
__m512i y = _mm512_permutexvar_epi64(_mm512_setr_epi64(0, 1, 4, 5, 2, 3, 6, 7), x);
}
''', args : compiler_args)
if has_mavx512f
message('AVX512F is available')
add_project_arguments('-DHAVE_AVX512FINTRIN_H=1', language : 'c')
else
message('AVX512F is NOT available')
endif
endif
aes = declare_dependency()
if not is_nativeclient
compiler_args = []
if compiler.has_argument('-maes')
aes = declare_dependency(compile_args : '-maes')
compiler_args += '-maes'
endif
pclmul = declare_dependency()
if compiler.has_argument('-mpclmul')
pclmul = declare_dependency(compile_args : '-mpclmul')
compiler_args += '-mpclmul'
endif
has_aes = compiler.compiles('''
#pragma GCC target("aes")
#pragma GCC target("pclmul")
#include <wmmintrin.h>
int main(){
__m128i x = _mm_aesimc_si128(_mm_setzero_si128());
__m128i y = _mm_clmulepi64_si128(_mm_setzero_si128(), _mm_setzero_si128(), 0);
}
''', args : compiler_args)
if has_aes
message('AESNI and PCLMULQDQ are available')
add_project_arguments('-DHAVE_WMMINTRIN_H=1', language : 'c')
else
message('AESNI and PCLMULQDQ are NOT available')
endif
endif
endif
if compiler.has_header('sys/mman.h')
add_project_arguments('-DHAVE_SYS_MMAN_H=1', language : 'c')
endif
if compiler.has_header('intrin.h')
add_project_arguments('-DHAVE_INTRIN_H=1', language : 'c')
has_xgetbv = compiler.links('''#include <intrin.h>
int main() { (void) _xgetbv(0) }''')
if has_xgetbv
add_project_arguments('-DHAVE__XGETBV=1', language : 'c')
message('_xgetbv() is available')
else
message('_xgetbv() is NOT available')
endif
endif
if host_machine.endian() == 'big'
add_project_arguments('-DNATIVE_BIG_ENDIAN=1', language : 'c')
else
add_project_arguments('-DNATIVE_LITTLE_ENDIAN=1', language : 'c')
endif
is_stdc_limit_macro_required = compiler.compiles('''
#include <limits.h>
#include <stdint.h>
int main(){
(void) SIZE_MAX;
(void) UINT64_MAX;
}
''')
if not is_stdc_limit_macro_required
message('__STDC_LIMIT_MACROS is required')
add_project_arguments(['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS'], language : 'c')
else
message('__STDC_LIMIT_MACROS is NOT required')
endif
if compiler.get_id() == 'clang' and host_machine.cpu_family() == 'x86'
# clang does not properly handle the 128-bit type on 32-bit systems
can_use_128_bits = false
elif host_machine.endian() != 'little'
# libsodium currently expects a little endian CPU for the 128-bit type
can_use_128_bits = false
elif is_emscripten
# emscripten currently supports only shift operations on integers larger than 64 bits
can_use_128_bits = false
else
can_use_128_bits = compiler.compiles('''
#if !defined(__GNUC__) && !defined(__SIZEOF_INT128__)
# error mode(TI) is a gcc extension, and __int128 is not available
#endif
#include <stddef.h>
#include <stdint.h>
#if defined(__SIZEOF_INT128__)
typedef unsigned __int128 uint128_t;
#else
typedef unsigned uint128_t __attribute__((mode(TI)));
#endif
void fcontract(uint128_t *t) {
*t += 0x8000000000000 - 1;
}
int main(){
(void) fcontract;
}
''')
endif
if can_use_128_bits
add_project_arguments('-DHAVE_TI_MODE=1', language : 'c')
message('gcc TI mode is available')
else
message('gcc TI mode is NOT available')
endif
can_use_x86_64_asm = false
can_use_avx = false
can_use_cpuid = false
if enable_asm
can_use_x86_64_asm = compiler.compiles('''
int main(){
#if defined(__amd64) || defined(__amd64__) || defined(__x86_64__)
# if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(_WIN32) || defined(_WIN64)
# error Windows x86_64 calling conventions are not supported yet
# endif
/* neat */
#else
# error !x86_64
#endif
unsigned char i = 0, o = 0, t;
__asm__ __volatile__ ("pxor %%xmm12, %%xmm6 \n"
"movb (%[i]), %[t] \n"
"addb %[t], (%[o]) \n"
: [t] "=&r"(t)
: [o] "D"(&o), [i] "S"(&i)
: "memory", "flags", "cc");
}
''')
if can_use_x86_64_asm
add_project_arguments('-DHAVE_AMD64_ASM=1', language : 'c')
message('x86_64 asm code can be used')
else
message('x86_64 asm code CANNOT be used')
endif
can_use_avx = compiler.compiles('''
int main(){
#if defined(__amd64) || defined(__amd64__) || defined(__x86_64__)
# if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(_WIN32) || defined(_WIN64)
# error Windows x86_64 calling conventions are not supported yet
# endif
/* neat */
#else
# error !x86_64
#endif
__asm__ __volatile__ ("vpunpcklqdq %xmm0,%xmm13,%xmm0");
}
''')
if can_use_avx
add_project_arguments('-DHAVE_AVX_ASM=1', language : 'c')
message('AVX opcodes are supported')
else
message('AVX opcodes are NOT supported')
endif
endif
if enable_asm or (is_nativeclient and host_machine.cpu_family() == 'x86_64')
can_use_cpuid = compiler.compiles('''
int main(){
unsigned int cpu_info[4];
__asm__ __volatile__ ("xchgl %%ebx, %k1; cpuid; xchgl %%ebx, %k1" :
"=a" (cpu_info[0]), "=&r" (cpu_info[1]),
"=c" (cpu_info[2]), "=d" (cpu_info[3]) :
"0" (0U), "2" (0U));
}
''')
if can_use_cpuid
add_project_arguments('-DHAVE_CPUID=1', language : 'c')
message('cpuid instruction is available')
else
message('cpuid instruction is NOT available')
endif
endif
if enable_asm or is_nativeclient
can_use_private_extern = compiler.links('''
int main(){
__asm__ __volatile__ (".private_extern dummy_symbol \n"
".private_extern _dummy_symbol \n"
".globl dummy_symbol \n"
".globl _dummy_symbol \n"
"dummy_symbol: \n"
"_dummy_symbol: \n"
" nop \n"
);
}
''')
if can_use_private_extern
add_project_arguments('-DASM_HIDE_SYMBOL=.private_extern', language : 'c')
else
can_use_hidden_asm = compiler.links('''
int main(){
__asm__ __volatile__ (".hidden dummy_symbol \n"
".hidden _dummy_symbol \n"
".globl dummy_symbol \n"
".globl _dummy_symbol \n"
"dummy_symbol: \n"
"_dummy_symbol: \n"
" nop \n"
);
}
''')
if can_use_hidden_asm
add_project_arguments('-DASM_HIDE_SYMBOL=.hidden', language : 'c')
else
message('Unable to reliably tag symbols as private')
endif
endif
endif
can_use_weak_symbols = compiler.links('''
#if !defined(__ELF__) && !defined(__APPLE_CC__)
# error Support for weak symbols may not be available
#endif
__attribute__((weak)) void __dummy(void *x) { }
void f(void *x) { __dummy(x); }
int main(){}
''')
if can_use_weak_symbols
add_project_arguments('-DHAVE_WEAK_SYMBOLS=1', language : 'c')
message('Weak symbols are supported')
else
message('Weak symbols are NOT supported')
endif
can_use_atomic_ops = compiler.links('''
int main(){
static volatile int _sodium_lock;
__sync_lock_test_and_set(&_sodium_lock, 1);
__sync_lock_release(&_sodium_lock);
}
''')
if can_use_atomic_ops
add_project_arguments('-DHAVE_ATOMIC_OPS=1', language : 'c')
message('Atomic operations are supported')
else
message('Atomic operations are NOT supported')
endif
check_functions = [
'posix_memalign',
'getpid',
]
if not is_emscripten
check_functions += [
'arc4random',
'arc4random_buf',
'mmap',
'mlock',
'madvise',
'mprotect',
'memset_s',
'explicit_bzero',
'nanosleep',
]
endif
foreach func : check_functions
if compiler.has_function(func)
add_project_arguments('-DHAVE_' + func.to_upper() + '=1', language : 'c')
endif
endforeach
# TODO: Emscripten LDFlags (use target file?)
subdir('src/libsodium')
# subdir('test/default')