-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmeson.build
More file actions
executable file
·713 lines (658 loc) · 31.2 KB
/
Copy pathmeson.build
File metadata and controls
executable file
·713 lines (658 loc) · 31.2 KB
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2021-2026 Seagate Technology LLC and/or its Affiliates, All Rights Reserved
project(
'opensea-common',
'c',
license: 'MPL-2.0',
version: '6.1.0',
meson_version: '>=0.56.0',
default_options: ['warning_level=2', 'default_library=static'],
)
c = meson.get_compiler('c')
global_cpp_args = []
deps = []
# Work around for pre-GCC5 to use a newer standard to compile.
# This generates a warning over using build-in c_std option, but
# we live with that for now since we support older meson versions presently.
if not (target_machine.system() == 'sunos') and c.get_id().contains('gcc')
if c.version().version_compare('<5.0')
# 4.7.4+ has C11 support, but c89 is the default standard so we need to change it.
if c.has_argument('-std=gnu11')
add_project_arguments('-std=gnu11', language: 'c')
elif c.has_argument('-std=gnu99')
# Add this argument to the list since C99 is a minimum required C compiler standard
add_project_arguments('-std=gnu99', language: 'c')
else
error('C99/GNU99 standard is required but was not able to be set!')
endif
endif
endif
genericargs = []
warning_flags = []
linker_flags = [] #additional linker flags to add per-compiler for hardening.
ssp_support_dep = [] #Solaris/illumos SSP support library, propagated to consumers of this library
if c.get_id().contains('gcc') or c.get_id().contains('clang')
#TODO: Add -Wcast-align=strict and fix these issues to help ensure better portability
#NOTE: -Wsign-conversion can be useful while debugging, but there are numerous places this shows up
# and it is not useful, so only add it while debugging.
#NOTE: 4/4/2024 - adding flags from https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++
warning_flags = [
# '-Wcast-align=strict',
'-Wshadow=compatible-local',
'-Wvla',
'-Wfloat-equal',
'-Wnull-dereference',
'-Wunused-const-variable',
'-Wunused-parameter',
'-Wunused-value',
'-Wduplicated-cond',
'-Wjump-misses-init',
'-Wstringop-overflow',
'-Wlogical-op',
'-Wshift-overflow',
'-Wshift-overflow=1',
'-Wshift-overflow=2',
'-Wdouble-promotion',
'-Wformat-security',
'-Wold-style-definition',
'-Wstrict-prototypes',
'-Wmissing-declarations',
'-Wmissing-prototypes',
'-Wchar-subscripts',
'-Wundef',
'-Wformat',
'-Wformat=2',
'-Wint-conversion', #-Warith-conversion
'-Wenum-conversion',
'-Wfloat-conversion',
'-Wint-to-pointer-cast',
'-Wimplicit-fallthrough',
'-D_GLIBCXX_ASSERTIONS',
'-fstrict-flex-arrays=3',
'-fno-delete-null-pointer-checks',
'-fno-strict-overflow',
'-fno-strict-aliasing',
'-ftrivial-auto-var-init=zero',
'-Wtrampolines', #GCC only at this time
'-Werror=implicit',
'-Werror=incompatible-pointer-types',
'-Wincompatible-pointer-types-discards-qualifiers',
'-Werror=int-conversion',
'-Werror=implicit-int',
'-Woverlength-strings',
'-Wnewline-eof',
'-Wno-c23-extensions', #We do not want this warning since we are already checking for when C23 extensions are available before we use them. If not, we use a compiler specific definition, or make it an empty definition.
'-Wparentheses',
'-Wextra-semi',
'-Wcast-qual',
'-Werror=sometimes-uninitialized',
'-Wuninitialized',
'-Wunevaluated-expression',
'-Wunsequenced',
'-Wvarargs',
'-Wwrite-strings',
'-Wrestrict',
'-Wstringop-truncation',
'-Werror=trigraphs',
'-Wunreachable-code',
'-Wcomment',
'-Wsequence-point',
'-Wreturn-type',
'-Wpointer-bool-conversion',
'-Wmismatched-dealloc',
'-Wfree-nonheap-object',
'-fnullability',
'-Wnullability', # Warnings for nullability annotations
'-Wnullability-completeness', # Warn if nullability annotations are incomplete
'-Wnullability-completeness-on-arrays', # Warn if nullability annotations are incomplete
# '-Wnullable-to-nonnull-conversion', # Basically warns so you have to cast to the correct nullability type. Not needed today -TJE
'-Wswitch-enum',
'-Walloc-zero',
'-Walloc-size',
'-Wcalloc-transposed-args',
'-Werror=alloca', # Do not allow use of the alloca function
'-Wstring-compare', # Helps detect when strcmp is used when the programmer likely meant to use strncmp
'-Wfortify-source', # Clang specific (at least for now) but enables additional diagnostics.
]
if get_option('cc-suggest-attribute')
warning_flags += [
'-Wsuggest-attribute=pure',
'-Wsuggest-attribute=const',
'-Wsuggest-attribute=noreturn',
'-Wsuggest-attribute=returns_nonnull',
'-Wmissing-noreturn',
'-Wsuggest-attribute=malloc',
'-Wsuggest-attribute=format',
'-Wmissing-format-attribute',
'-Wsuggest-attribute=cold',
'-fipa-pure-const',
]
endif
if c.get_id().contains('gcc') and c.version().version_compare('>=10.0')
#only enable the sign conversion warning on versions 10 and up because it is way too noisy on earlier GCC versions than it is useful-TJE
warning_flags += '-Wsign-conversion'
endif
# ssp option: 'auto' (default) and 'enabled' both run the automatic
# probing below; 'disabled' skips the whole block and builds SSP-free.
if get_option('ssp').auto() or get_option('ssp').enabled()
# Check if -fstack-protector-strong links or not - some systems support
# the argument but are unable to link with it. Fall back to plain
# -fstack-protector when strong is unavailable.
# GCC's optimize attribute forces canary codegen on the probe function so
# the check does not depend on the compiler's per-function protection
# heuristics deciding that a small test function is not worth protecting.
stack_protector_test_code = '''
#if defined(__GNUC__) && !defined(__clang__)
__attribute__((noinline, optimize("-fstack-protector-all")))
#else
__attribute__((noinline))
#endif
int ssp_probe(void)
{
volatile char buf[8];
buf[0] = 1;
buf[7] = 2;
return buf[0] + buf[7];
}
int main(void) { return ssp_probe(); }
'''
# A link check rather than a compile: on some toolchains the SSP glue
# symbols (__stack_chk_fail[_local], __stack_chk_guard) are only
# provided by a static archive and must already resolve at link time
# (see golang/go#58385).
ssp_strong_ok = c.links(
stack_protector_test_code,
name: 'stack-protector-strong link support',
args: ['-fstack-protector-strong'],
)
ssp_regular_ok = false
if not ssp_strong_ok
ssp_regular_ok = c.links(
stack_protector_test_code,
name: 'stack-protector link support',
args: ['-fstack-protector'],
)
endif
if ssp_strong_ok or ssp_regular_ok
#On Solaris/illumos the SSP glue symbols (__stack_chk*) are only
#resolved through the platform SSP support library and that only
#reaches a ninja link line as an explicit library dependency, so
#SSP is enabled only when the platform ships it.
#ssp_support_dep propagates it to consumers of this library.
ssp_available = true
if target_machine.system() == 'sunos'
#illumos (OmniOS, Tribblix, OpenIndiana) ships libssp_ns.a,
#classic Oracle Solaris ships libssp.a
ssp_available = false
ssp_ns_dep = c.find_library('ssp_ns', required: false)
if ssp_ns_dep.found()
ssp_support_dep = [ssp_ns_dep]
ssp_available = true
else
ssp_dep = c.find_library('ssp', required: false)
if ssp_dep.found()
ssp_support_dep = [ssp_dep]
ssp_available = true
endif
endif
endif
if ssp_available
if ssp_strong_ok
warning_flags += '-fstack-protector-strong'
else
warning_flags += '-fstack-protector'
endif
endif
endif
endif
# Check if -fstack-clash-protection works or not. Some GCC for Windows
# versions (see
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458) accept the flag
# and even link it, but the generated code crashes at runtime, so the
# probe is also run when possible.
# The large local buffer forces the stack probing code path the buggy
# versions break.
stack_clash_test_code = '''
__attribute__((noinline)) int clash_probe(void)
{
volatile char buf[65536];
buf[0] = 1;
return buf[0];
}
int main(void) { return clash_probe() - 1; }
'''
if c.links(
stack_clash_test_code,
name: 'stack-clash-protection link support',
args: ['-fstack-clash-protection'],
)
if meson.can_run_host_binaries()
stack_clash_result = c.run(
stack_clash_test_code,
name: 'stack-clash-protection run support',
args: [
'-fstack-clash-protection',
],
)
if stack_clash_result.returncode() == 0
warning_flags += '-fstack-clash-protection'
endif
else
#Host binaries cannot be executed in this environment - keep the flag, the link check passed
warning_flags += '-fstack-clash-protection'
endif
endif
if target_machine.cpu_family() == 'ppc64'
#power pc builds generate a lot of warnings/notes about ABI changes since GCC-5
#this flag is disabling them because this is way too noisy.
warning_flags += ['-Wno-psabi']
elif target_machine.cpu_family() == 'x86_64'
warning_flags += ['-fcf-protection=full'] #this may be linux only at this time.
elif target_machine.cpu_family() == 'aarch64'
warning_flags += ['-mbranch-protection=standard']
endif
linker_flags += ['-Wl,-z,noexecstack', '-Wl,-z,relro', '-Wl,-z,now']
#test if setting _FORTIFY_SOURCE higher than default generates a warning
#this issue showed up on Rocky Linux 8 and we don't want to generate extra noise
#due to trying to follow openssf best practices
fortifytest = ''' #include <stdio.h>
int main() {
return 0;
}
'''
fortifyresult = c.compiles(
fortifytest,
name: 'Set _FORTIFY_SOURCE to highest possible level without warnings',
args: ['-U_FORTIFY_SOURCE', '-D_FORTIFY_SOURCE=5', '-Werror'],
)
if fortifyresult == true
warning_flags += ['-U_FORTIFY_SOURCE', '-D_FORTIFY_SOURCE=3']
endif
elif c.get_id().contains('msvc')
#See here for enabling/disabling msvc warnings:
#https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170
#warnings off by default: https://learn.microsoft.com/en-us/cpp/preprocessor/compiler-warnings-that-are-off-by-default?view=msvc-170
warning_flags = [
#Turn off the following warnings. If using /wall in Windows, many of these show all over the Windows API
#This is likely not an issue with meson, but matching VS project files for now
'/wd4214', # nonstandard extension used : bit field types other than int
'/wd4201', # nonstandard extension used : nameless struct/union
'/wd4668', # 'symbol' is not defined as a preprocessor macro, replacing with '0' for 'directives'. While like -Wundef, this creates too many warnings in system headers to use
'/wd4820', # 'bytes' bytes padding added after construct 'member_name'
'/wd4710', # 'function' : function not inlined
'/wd5045', # Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
'/wd4711', # function 'function' selected for inline expansion
'/wd4324', # 'struct_name' : structure was padded due to __declspec(align())
'/wd4221', # nonstandard extension used : 'identifier' : cannot be initialized using address of automatic variable
'/wd4204', # nonstandard extension used : non-constant aggregate initializer
'/wd5105', # macro expansion producing 'defined' has undefined behavior
'/wd4746', # volatile access of '<expression>' is subject to /volatile:[iso|ms] setting; consider using __iso_volatile_load/store intrinsic functions.
#Turn on the following warnings to make the output more useful or like GCC/clang
'/w14255', # 'function' : no function prototype given: converting '()' to '(void)'
'/w14062', # enumerator 'identifier' in switch of enum 'enumeration' is not handled
'/w14101', # 'identifier' : unreferenced local variable
'/w14189', # 'identifier' : local variable is initialized but not referenced
'/w15031', # #pragma warning(pop): likely mismatch, popping warning state pushed in different file
'/w15032', # detected #pragma warning(push) with no corresponding #pragma warning(pop)
'/w15262', # implicit fall-through occurs here; are you missing a break statement? Use [[fallthrough]] when a break statement is intentionally omitted between cases
'/w14255', # 'function' : no function prototype given: converting '()' to '(void)' #NOTE: Only needed for /Wall, otherwise enabling can be good-TJE
'/w14242', # identifier conversion from type 1 to type 2, possible loss of data (matches -wconversion above)
'/w14254', # operator conversion from type 1 to type 2, possible loss of data (matches -wconversion above)
'/w14287', # operator: unsigned/negative constant mismatch (matches -wconversion above)
'/w14296', # operator: expression is always false
'/w14365', # action: conversion from type 1 to type 2, signed/unsigned mismatch (matches -wconversion above)
'/w14388', # implicit conversion warning during a comparison (matches -wconversion above)
'/w14545', # expression before comma evaluates to a function which is missing an argument list
'/w14546', # function call before comma missing argument list
'/w14547', # 'operator' : operator before comma has no effect; expected operator with side-effect
'/w14548', # expression before comma has no effect; expected expression with side-effect
'/w14549', # 'operator1': operator before comma has no effect; did you intend 'operator2'?
'/w14574', # 'identifier' is defined to be '0': did you mean to use '#if identifier'?
'/w14605', # '/Dmacro' specified on current command line, but was not specified when precompiled header was built
'/w14555', # expression has no effect; expected expression with side-effect
'/w14774', # 'string' : format string expected in argument number is not a string literal
'/w14777', # 'function' : format string 'string' requires an argument of type 'type1', but variadic argument number has type 'type2'
'/w14826', # Conversion from 'type1' to 'type2' is sign-extended. This may cause unexpected runtime behavior (more -wconversion)
'/w15219', # implicit conversion from 'type-1' to 'type-2', possible loss of data (-wconversion)
'/w15240', # 'attribute-name': attribute is ignored in this syntactic position
'/w15245', # 'function': unreferenced function with internal linkage has been removed
'/w14555', # expression has no effect; expected expression with side-effect
'/w15264', # 'variable-name': 'const' variable is not used
'/w24302', # 'conversion': truncation from 'type1' to 'type2'
'/w14311', # 'variable': pointer truncation from 'type' to 'type'
'/w14312', # 'operation': conversion from 'type1' to 'type2' of greater size
'/w14319', # 'operator': zero extending 'type1' to 'type2' of greater size
'/w14702', # warning for unreachable code
#Treat the following as errors
'/we4431', # missing type specifier - int assumed. Note: C no longer supports default-int
'/we4905', # wide string literal cast to 'LPSTR'
'/we4906', # string literal cast to 'LPWSTR'
'/we4837', # trigraph detected: '??character' replaced by 'character'
'/we4628', # digraphs not supported with -Ze. Character sequence 'digraph' not interpreted as alternate token for 'char'
'/we4289', # nonstandard extension used : 'var' : loop control variable declared in the for-loop is used outside the for-loop scope
'/we4464', # relative include path contains '..'
'/GS', #security cookie for stack protection
'/sdl', #adds recommended security development lifecycle checks
'/Qspectre',
'/guard:cf', #control flow guard
'/d2guard4', #control flow guard
]
if target_machine.cpu_family() == 'x86_64' or target_machine.cpu_family() == 'x86'
# https://learn.microsoft.com/en-us/cpp/build/reference/qintel-jcc-erratum?view=msvc-170
warning_flags += '/QIntel-jcc-erratum'
endif
# MSVC defaults to old MSVC C compatibility which is mostly C89 with some newer C99/C11-ish support
# Since we support older meson versions, we deal with this causing a warning over using the default_options to
# set the C standard for the project.
if c.has_argument('/std:clatest')
genericargs += '/std:clatest'
warning_flags += '/std:clatest'
elif c.has_argument('/std:c17')
genericargs += '/std:c17'
warning_flags += '/std:c17'
elif c.has_argument('/std:c11')
genericargs += '/std:c11'
warning_flags += '/std:c11'
endif
linker_flags += [
'/guard:cf', #control flow guard
'/SafeSEH', #on by default in x64 so it is unrecognized otherwise.
'/NXCOMPAT', #data execution prevention
'/dynamicbase', #address space randomization
]
#TODO: check compiler version to handle warnings that were off by default in earlier versions
#ex: C4431 (level 4) missing type specifier - int assumed. Note: C no longer supports default-int
# This was off by default in compilers before VS2012.
elif c.get_id().contains('xlc')
#This section is for IBM's xlc compiler and warning options it may need.
#NOTE: xlcclang should be handled above
#See following links:
#https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=reference-supported-xl-compiler-options-by-different-invocations
#https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=end-mapping-legacy-xl-compiler-options-gcc-options
#https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=reference-individual-xl-compiler-option-descriptions
warning_flags = []
endif
add_project_arguments(c.get_supported_arguments(warning_flags), language: 'c')
add_project_link_arguments(c.get_supported_link_arguments(linker_flags), language: 'c')
if get_option('debug')
add_project_arguments('-D_DEBUG', language: 'c')
endif
if get_option('username').enabled()
global_cpp_args += ['-DENABLE_READ_USERNAME']
endif
if get_option('directorysecurity').disabled()
global_cpp_args += ['-DDISABLE_SECURE_FILE_PATH_CHECK']
endif
if get_option('memcpy-is-memcpy-not-memmove').enabled()
global_cpp_args += ['-DMEMCPY_IS_MEMCPY_NOT_MEMMOVE']
endif
if get_option('strcpy-is-strcpy-not-strmove').enabled()
global_cpp_args += ['-DSTRCPY_IS_STRCPY_NOT_STRMOVE']
endif
if get_option('allow-no-overlap-suggestions').enabled()
global_cpp_args += ['-DALLOW_NO_OVERLAP_SUGGESTIONS']
endif
if get_option('constraint-handler') == 'abort'
# no flags necessary since this is default
elif get_option('constraint-handler') == 'abortnd'
global_cpp_args += ['-DDEFAULT_CONSTRAINT_HANDLER_ABORTND']
elif get_option('constraint-handler') == 'ignore'
global_cpp_args += ['-DDEFAULT_CONSTRAINT_HANDLER_IGNORE']
elif get_option('constraint-handler') == 'warn'
global_cpp_args += ['-DDEFAULT_CONSTRAINT_HANDLER_WARN']
endif
if get_option('diagnose-if-attr').disabled()
global_cpp_args += ['-DNO_DIAGNOSE_IF_ATTR']
endif
src_files = [
'src/bit_manip.c',
'src/constraint_handling.c',
'src/env_detect.c',
'src/error_translation.c',
'src/getopt_utils.c',
'src/io_utils.c',
'src/math_utils.c',
'src/memory_safety.c',
'src/pattern_utils.c',
'src/precision_timer.c',
'src/prng.c',
'src/secure_file.c',
'src/secured_env_vars.c',
'src/sleep.c',
'src/sort_and_search.c',
'src/safe_qsort.c',
'src/safe_bsearch.c',
'src/safe_lsearch.c',
'src/safe_strtok.c',
'src/string_utils.c',
'src/time_utils.c',
'src/type_conversion.c',
'src/unit_conversion.c',
'src/validate_format.c',
'src/version_sort.c',
]
if c.has_header_symbol('stdlib.h', '__STDC_LIB_EXT1__')
add_project_arguments(['-D__STDC_WANT_LIB_EXT1__=1'], language: 'c')
endif
if c.has_function('getenv_s', prefix: '#define __STDC_WANT_LIB_EXT1__ 1\n#include <stdlib.h>\n')
add_project_arguments(['-DHAVE_GETENV_S'], language: 'c')
elif c.has_function('secure_getenv', prefix: '#define _GNU_SOURCE\n#include <stdlib.h>\n')
#TODO: check if user has disabled using this function through compile option
add_project_arguments(['-D_GNU_SOURCE', '-DHAVE_SECURE_GETENV'], language: 'c')
elif c.has_function('__secure_getenv', prefix: '#define _GNU_SOURCE\n#include <stdlib.h>\n')
#TODO: check if user has disabled using this function through compile option
add_project_arguments(['-D_GNU_SOURCE', '-DHAVE___SECURE_GETENV'], language: 'c')
endif
#check which memset/bzero is available that will not be optimized out in case we do not have the ability to check via preprocessor in the code.
if c.has_function('memset_explicit', prefix: '#include <string.h>')
global_cpp_args += ['-DHAVE_MEMSET_EXPLICIT']
elif c.has_function('memset_s', prefix: '#define __STDC_WANT_LIB_EXT1__ 1\n#include <string.h>\n')
global_cpp_args += ['-DHAVE_MEMSET_S']
elif c.has_function('explicit_bzero', prefix: '#include <strings.h>') or c.has_function(
'explicit_bzero',
prefix: '#include <string.h>',
)
global_cpp_args += ['-DHAVE_EXPLICIT_BZERO']
elif c.has_function('explicit_memset', prefix: '#include <string.h>')
global_cpp_args += ['-DHAVE_EXPLICIT_MEMSET']
elif c.has_function(
'SecureZeroMemory2',
prefix: '#include <windows.h>\n#include <winbase.h>',
dependencies: c.find_library('volatileaccessu', required: false),
)
global_cpp_args += ['-DHAVE_MSFT_SECURE_ZERO_MEMORY2']
deps += [c.find_library('volatileaccessu', required: true)]
elif c.has_function('SecureZeroMemory', prefix: '#include <windows.h>')
global_cpp_args += ['-DHAVE_MSFT_SECURE_ZERO_MEMORY', '-DNO_HAVE_MSFT_SECURE_ZERO_MEMORY2']
endif
if c.has_function('gmtime_r', prefix: '#include <time.h>')
global_cpp_args += ['-DHAVE_GMTIME_R']
endif
if c.has_function('gmtime_s', prefix: '#define __STDC_WANT_LIB_EXT1__ 1\n#include <time.h>\n')
global_cpp_args += ['-DHAVE_GMTIME_S']
endif
if c.has_function('localtime_r', prefix: '#include <time.h>')
global_cpp_args += ['-DHAVE_LOCALTIME_R']
endif
if c.has_function('localtime_s', prefix: '#define __STDC_WANT_LIB_EXT1__ 1\n#include <time.h>\n')
global_cpp_args += ['-DHAVE_LOCALTIME_S']
endif
if c.has_function('getdelim', prefix: '#include <stdio.h>')
global_cpp_args += ['-DHAVE_GETDELIM']
endif
if c.has_function('localtime_s', prefix: '#define __STDC_WANT_LIB_EXT1__ 1\n#include <time.h>\n')
global_cpp_args += ['-DHAVE_LOCALTIME_S']
endif
if c.has_function('getdelim', prefix: '#include <stdio.h>')
global_cpp_args += ['-DHAVE_GETDELIM']
endif
if c.has_function('getline', prefix: '#include <stdio.h>')
global_cpp_args += ['-DHAVE_GETLINE']
endif
# Test support for _Generic keyword. May not be available in some cases.
gernericSelectionTest = '''
long add_long(long x, long y)
{
return x + y;
}
int add_int(int x, int y)
{
return x + y;
}
short add_short(short x, short y)
{
return x + y;
}
char add_char(char x, char y)
{
return x + y;
}
# define add(x, y) \
_Generic((x), \
long: add_long, \
int: add_int, \
short: add_short, \
char: add_char) \
(x, y)
int main() {
long lx = 890, ly = 9741;
int ix = 23, iy = 72354;
short sx = 2324, sy = 634;
char cx = 12, cy = 2;
long lr = add(lx, ly);
int ir = add(ix, iy);
short sr = add(sx, sy);
char cr = add(cx, cy);
(void)lr;
(void)ir;
(void)sr;
(void)cr;
return 0;
}
'''
genericSelectionTestResult = c.compiles(gernericSelectionTest, name: 'Generic Selection Support', args: genericargs)
if genericSelectionTestResult == true
global_cpp_args += ['-DHAVE_C11_GENERIC_SELECTION']
endif
constexprTest = ' int main (void) { constexpr int i = 50; int j = 0; return j / i; }'
constexprTestResult = c.compiles(constexprTest, name: 'Constexpr Support')
if constexprTestResult == true
global_cpp_args += ['-DHAVE_CONSTEXPR']
endif
if c.has_type('nullptr_t', prefix: '#include <stddef.h>')
global_cpp_args += ['-DHAVE_NULLPTR_T']
endif
# Test support for __has_c_attribute for namespaced attribtues (compiler unique)
# Some compilers do not support this properly and throw errors about using the : symbol
# MSVC does not like this and Clang 16.0.6 in openBSD also complains.
attributeTest = ''' #include <stdio.h>
#if defined (__has_c_attribute)
# if __has_c_attribute(clang::unused)
# define HAS_CLANG_UNUSED
# elif __has_c_attribute(gnu::unused)
# define HAS_GCC_UNUSED
# endif
#endif
int main() {
return 0;
}
'''
attributeTestResult = c.compiles(attributeTest, name: '__has_c_attribute for compiler specific attributes')
if attributeTestResult == false
global_cpp_args += ['-DNO_COMPILER_UNIQUE_C_ATTRIBUTES']
if not c.get_id().contains('msvc')
global_cpp_args += ['-DFORCE_GNU_STYLE_ATTRIBUTES']
endif
endif
#check which version of strerror_r is available, posix or GNU
if c.has_function('strerror_r', prefix: '#include <string.h>')
posix_strerror_r_check_code = ''' #include <string.h>
int main() {
int result = strerror_r(0, NULL, 0);
return 0;
}
'''
posix_strerror_r_result = c.compiles(posix_strerror_r_check_code, name: 'POSIX strerror_r')
if posix_strerror_r_result == true
global_cpp_args += ['-DHAVE_POSIX_STRERR_R']
else
global_cpp_args += ['-DHAVE_GNU_STRERR_R']
endif
endif
# 64bit off_t
if c.has_type('off_t', prefix: '#include <sys/types.h>')
if c.sizeof('off_t', prefix: '#include <sys/types.h>') < 8
global_cpp_args += ['-D_FILE_OFFSET_BITS=64']
endif
endif
# Check if time_t is 64-bit. If not, try getting 64bit time_t
if c.sizeof('time_t', prefix: '#include <time.h>') < 8
if c.sizeof('time_t', prefix: '#define _TIME_BITS 64\n#include <time.h>') >= 8
global_cpp_args += ['-D_TIME_BITS=64']
else
if get_option('allow-32bit-time-t').enabled()
message(
'Warning: 32-bit time_t detected and allow-32bit-time-t option enabled. This may cause issues with dates after 2038. It is recommended to use a compiler and platform that supports 64-bit time_t if possible.',
)
global_cpp_args += ['-DALLOW_32BIT_TIME_T']
else
error(
'64bit time_t is required for this project. The allow-32bit-time-t can override this, however it is not recommended.',
)
endif
endif
endif
if target_machine.system() == 'windows'
src_files += ['src/windows_env_detect.c', 'src/windows_secure_file.c', 'src/windows_version_detect.c']
if c.get_define('__MINGW32__') != ''
global_cpp_args += ['-DSTRSAFE_NO_DEPRECATE']
win32_version = c.find_library('version')
advapi32 = c.find_library('advapi32')
deps += [win32_version, advapi32]
endif
if c.check_header('ntifs.h')
global_cpp_args += ['-DHAVE_NTIFS']
endif
global_cpp_args += ['-D_CRT_NONSTDC_NO_DEPRECATE', '-D_CRT_SECURE_NO_WARNINGS']
# Need to define this if not present for mingw64
if not c.has_type('FILE_ID_INFO', prefix: '#include <WinBase.h>')
global_cpp_args += ['-DNO_FILE_ID_INFO']
endif
else
# assuming POSIX if not windows for now.
src_files += ['src/posix_env_detect.c', 'src/posix_secure_file.c']
endif
m_dep = c.find_library('m', required: false)
incdir = include_directories(
'include',
'Test',
'Test/framework',
)
opensea_common_lib = static_library(
'opensea-common',
src_files,
c_args: global_cpp_args,
dependencies: [m_dep, deps],
include_directories: incdir,
gnu_symbol_visibility: 'hidden',
)
opensea_common_dep = declare_dependency(
link_with: opensea_common_lib,
compile_args: global_cpp_args,
dependencies: ssp_support_dep,
include_directories: incdir,
)
# Build tests or fuzzing targets when tests are enabled
if not get_option('tests').disabled()
# Check for LIB_FUZZING_ENGINE environment variable (cross-platform compatible)
if target_machine.system() == 'windows'
is_fuzzing_build = run_command('cmd', '/c', 'echo %LIB_FUZZING_ENGINE%', check: false).stdout().strip() != ''
else
is_fuzzing_build = run_command('sh', '-c', 'echo $LIB_FUZZING_ENGINE', check: false).stdout().strip() != ''
endif
if is_fuzzing_build
subdir('fuzz')
else
subdir('Test')
endif
endif