-
Notifications
You must be signed in to change notification settings - Fork 0
/
SConstruct
773 lines (676 loc) · 24.7 KB
/
SConstruct
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
"""zlib library built with SCons"""
import os
import sys
import re
import platform
import SCons
from SCons.Environment import Environment
from SCons.Script import Variables
from SCons.Script.SConscript import Configure
from SCons.Script.Main import SetOption, AddOption, GetOption, Progress
from BuildUtils.SconsUtils import SetBuildJobs, SetupBuildEnv, ProgressCounter
from BuildUtils.ColorPrinter import ColorPrinter
def create_new_env():
""" Entry point for build, could be called by other builds"""
SetupOptions()
env = Environment(
DEBUG_BUILD=GetOption('option_debug'),
ZPREFIX=GetOption('option_zprefix'),
SOLO=GetOption('option_solo'),
COVER=GetOption('option_cover'),
GCC_CLASSIC=GetOption('option_gcc_classic'),
VERBOSE_COMPILE=GetOption('option_verbose'),
)
env['PROJECT_DIR'] = os.path.abspath(
env.Dir('.').abspath).replace('\\', '/')
SetBuildJobs(env)
base_source_files = [
'repo/adler32.c',
'repo/compress.c',
'repo/crc32.c',
'repo/deflate.c',
'repo/gzclose.c',
'repo/gzlib.c',
'repo/gzread.c',
'repo/gzwrite.c',
'repo/inflate.c',
'repo/infback.c',
'repo/inftrees.c',
'repo/inffast.c',
'repo/trees.c',
'repo/uncompr.c',
'repo/zutil.c',
]
env = configure_env(env)
env.Append(CPPPATH=['.'])
prog_name = 'z'
prog_static_name = prog_name
if "Windows" in platform.system():
prog_static_name = prog_name + "_static"
progress = ProgressCounter()
_static_env, _z_static = SetupBuildEnv(
env, progress, 'static', prog_static_name,
base_source_files, 'build/build_static', 'deploy')
shared_env, _z_shared = SetupBuildEnv(
env, progress, 'shared', prog_name,
base_source_files, 'build/build_shared', 'deploy')
if "Windows" in platform.system():
shared_env.Append(CPPDEFINES=['ZLIB_DLL'])
Progress(progress, interval=1)
zlib_static_lib = env.subst('$LIBPREFIX') + \
prog_static_name + env.subst('$LIBSUFFIX')
if not env['COVER']:
example_env, _example_bin = SetupBuildEnv(env, progress, 'exec', 'example', [
'repo/test/example.c'], 'build/build_static', 'build')
minizip_env, _minizip_bin = SetupBuildEnv(env, progress, 'exec', 'minigzip', [
'repo/test/minigzip.c'], 'build/build_static', 'build')
example_env.Append(CPPPATH=[example_env.Dir('repo').abspath], LIBS=[
example_env.File('./deploy/' + zlib_static_lib)])
minizip_env.Append(CPPPATH=[minizip_env.Dir('repo').abspath], LIBS=[
minizip_env.File('./deploy/' + zlib_static_lib)])
else:
infcover_env, _infcover_bin = SetupBuildEnv(env, progress, 'exec', 'infcover', [
'repo/test/infcover.c'], 'build/build_static', 'build')
infcover_env.Append(CPPPATH=[infcover_env.Dir('repo').abspath], LIBS=[
infcover_env.File('./deploy/' + zlib_static_lib)])
#env = SetupInstalls(env)
#env = ConfigPlatformIDE(env, source_files, headerFiles, resourceFiles, prog)
def configure_env(env):
"""
This function is just for organization really, groups all
the configure stuff together.
"""
printer = ColorPrinter()
def check_large_file64(context):
"""Test to check if the off64_t c type is avialable."""
context.Message(printer.ConfigString('Checking for off64_t... '))
prev_defines = ""
if 'CPPDEFINES' in context.env:
prev_defines = context.env['CPPDEFINES']
context.env.Append(CPPDEFINES=['_LARGEFILE64_SOURCE=1'])
result = context.TryCompile("""
#include <sys/types.h>
off64_t dummy = 0;
""",
'.c')
if not result:
context.env.Replace(CPPDEFINES=prev_defines)
context.Result(result)
return result
def check_fseeko(context):
"""Test to see if fseeko function is available."""
context.Message(printer.ConfigString('Checking for fseeko... '))
result = context.TryCompile("""
#include <stdio.h>
int main(void) {
fseeko(NULL, 0, 0);
return 0;
}
""",
'.c')
if not result:
context.env.Append(CPPDEFINES=['NO_FSEEKO'])
context.Result(result)
return result
def CheckSizeT(context):
context.Message(printer.ConfigString('Checking for size_t... '))
result = context.TryCompile("""
#include <stdio.h>
#include <stdlib.h>
size_t dummy = 0;
""",
'.c')
context.Result(result)
return result
def CheckSizeTLongLong(context):
context.Message(printer.ConfigString('Checking for long long... '))
result = context.TryCompile("""
long long dummy = 0;
""",
'.c')
context.Result(result)
return result
def CheckSizeTPointerSize(context, longlong_result):
result = []
context.Message(printer.ConfigString(
'Checking for a pointer-size integer type... '))
if longlong_result:
result = context.TryRun("""
#include <stdio.h>
int main(void) {
if (sizeof(void *) <= sizeof(int)) puts("int");
else if (sizeof(void *) <= sizeof(long)) puts("long");
else puts("z_longlong");
return 0;
}
""",
'.c')
else:
result = context.TryRun("""
#include <stdio.h>
int main(void) {
if (sizeof(void *) <= sizeof(int)) puts("int");
else puts("long");
return 0;
}
""",
'.c')
if result[1] == "":
context.Result("Failed.")
return False
else:
context.env.Append(CPPDEFINES=['NO_SIZE_T='+result[1]])
context.Result(result[1] + ".")
return True
def CheckSharedLibrary(context):
context.Message(printer.ConfigString(
'Checking for shared library support... '))
result = context.TryBuild(context.env.SharedLibrary, """
extern int getchar();
int hello() {return getchar();}
""",
'.c')
context.Result(result)
return result
def CheckUnistdH(context):
context.Message(printer.ConfigString('Checking for unistd.h... '))
result = context.TryCompile("""
#include <unistd.h>
int main() { return 0; }
""",
'.c')
if result:
context.env["ZCONFH"] = re.sub(
r"def\sHAVE_UNISTD_H(.*)\smay\sbe", r" 1\1 was", context.env["ZCONFH"], re.MULTILINE)
context.Result(result)
return result
def CheckStrerror(context):
context.Message(printer.ConfigString('Checking for strerror... '))
result = context.TryCompile("""
#include <string.h>
#include <errno.h>
int main() { return strlen(strerror(errno)); }
""",
'.c')
if not result:
context.env.Append(CPPDEFINES=['NO_STRERROR'])
context.Result(result)
return result
def CheckStdargH(context):
context.Message(printer.ConfigString('Checking for stdarg.h... '))
result = context.TryCompile("""
#include <stdarg.h>
int main() { return 0; }
""",
'.c')
if result:
context.env["ZCONFH"] = re.sub(
r"def\sHAVE_STDARG_H(.*)\smay\sbe", r" 1\1 was", context.env["ZCONFH"], re.MULTILINE)
context.Result(result)
return result
def AddZPrefix(context):
context.Message(printer.ConfigString(
'Using z_ prefix on all symbols... '))
result = context.env['ZPREFIX']
if result:
context.env["ZCONFH"] = re.sub(
r"def\sZ_PREFIX(.*)\smay\sbe", r" 1\1 was", context.env["ZCONFH"], re.MULTILINE)
context.Result(result)
return result
def AddSolo(context):
context.Message(printer.ConfigString('Using Z_SOLO to build... '))
result = context.env['SOLO']
if result:
context.env["ZCONFH"] = re.sub(
r"\#define\sZCONF_H", r"#define ZCONF_H\n#define Z_SOLO", context.env["ZCONFH"], re.MULTILINE)
context.Result(result)
return result
def AddCover(context):
context.Message(printer.ConfigString('Using code coverage flags... '))
result = context.env['COVER']
if result:
context.env.Append(CCFLAGS=[
'-fprofile-arcs',
'-ftest-coverage',
])
context.env.Append(LINKFLAGS=[
'-fprofile-arcs',
'-ftest-coverage',
])
context.env.Append(LIBS=[
'gcov',
])
if not context.env['GCC_CLASSIC'] == "":
context.env.Replace(CC=context.env['GCC_CLASSIC'])
context.Result(result)
return result
def CheckVsnprintf(context):
context.Message(printer.ConfigString(
"Checking whether to use vs[n]printf() or s[n]printf()... "))
result = context.TryCompile("""
#include <stdio.h>
#include <stdarg.h>
#include "zconf.h"
int main()
{
#ifndef STDC
choke me
#endif
return 0;
}
""",
'.c')
if result:
context.Result("using vs[n]printf().")
else:
context.Result("using s[n]printf().")
return result
def CheckVsnStdio(context):
context.Message(printer.ConfigString(
"Checking for vsnprintf() in stdio.h... "))
result = context.TryCompile("""
#include <stdio.h>
#include <stdarg.h>
int mytest(const char *fmt, ...)
{
char buf[20];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
return 0;
}
int main()
{
return (mytest("Hello%d\\n", 1));
}
""",
'.c')
context.Result(result)
if not result:
context.env.Append(CPPDEFINES=["NO_vsnprintf"])
print(printer.ConfigString(
" WARNING: vsnprintf() not found, falling back to vsprintf(). zlib"))
print(printer.ConfigString(
" can build but will be open to possible buffer-overflow security"))
print(printer.ConfigString(" vulnerabilities."))
return result
def CheckVsnprintfReturn(context):
context.Message(printer.ConfigString(
"Checking for return value of vsnprintf()... "))
result = context.TryCompile("""
#include <stdio.h>
#include <stdarg.h>
int mytest(const char *fmt, ...)
{
int n;
char buf[20];
va_list ap;
va_start(ap, fmt);
n = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
return n;
}
int main()
{
return (mytest("Hello%d\\n", 1));
}
""",
'.c')
context.Result(result)
if not result:
context.env.Append(CPPDEFINES=["HAS_vsnprintf_void"])
print(printer.ConfigString(
" WARNING: apparently vsnprintf() does not return a value. zlib"))
print(printer.ConfigString(
" can build but will be open to possible string-format security"))
print(printer.ConfigString(" vulnerabilities."))
return result
def CheckVsprintfReturn(context):
context.Message(printer.ConfigString(
"Checking for return value of vsnprintf()... "))
result = context.TryCompile("""
#include <stdio.h>
#include <stdarg.h>
int mytest(const char *fmt, ...)
{
int n;
char buf[20];
va_list ap;
va_start(ap, fmt);
n = vsprintf(buf, fmt, ap);
va_end(ap);
return n;
}
int main()
{
return (mytest("Hello%d\\n", 1));
}
""",
'.c')
context.Result(result)
if not result:
context.env.Append(CPPDEFINES=["HAS_vsprintf_void"])
print(printer.ConfigString(
" WARNING: apparently vsprintf() does not return a value. zlib"))
print(printer.ConfigString(
" can build but will be open to possible string-format security"))
print(printer.ConfigString(" vulnerabilities."))
return result
def CheckSnStdio(context):
context.Message(printer.ConfigString(
"Checking for snprintf() in stdio.h... "))
result = context.TryCompile("""
#include <stdio.h>
int mytest()
{
char buf[20];
snprintf(buf, sizeof(buf), "%s", "foo");
return 0;
}
int main()
{
return (mytest());
}
""",
'.c')
context.Result(result)
if not result:
context.env.Append(CPPDEFINES=["NO_snprintf"])
print(printer.ConfigString(
" WARNING: snprintf() not found, falling back to sprintf(). zlib"))
print(printer.ConfigString(
" can build but will be open to possible buffer-overflow security"))
print(printer.ConfigString(" vulnerabilities."))
return result
def CheckSnprintfReturn(context):
context.Message(printer.ConfigString(
"Checking for return value of snprintf()... "))
result = context.TryCompile("""
#include <stdio.h>
int mytest()
{
char buf[20];
return snprintf(buf, sizeof(buf), "%s", "foo");
}
int main()
{
return (mytest());
}
""",
'.c')
context.Result(result)
if not result:
context.env.Append(CPPDEFINES=["HAS_snprintf_void"])
print(printer.ConfigString(
" WARNING: apparently snprintf() does not return a value. zlib"))
print(printer.ConfigString(
" can build but will be open to possible string-format security"))
print(printer.ConfigString(" vulnerabilities."))
return result
def CheckSprintfReturn(context):
context.Message(printer.ConfigString(
"Checking for return value of sprintf()... "))
result = context.TryCompile("""
#include <stdio.h>
int mytest()
{
char buf[20];
return sprintf(buf, "%s", "foo");
}
int main()
{
return (mytest());
}
""",
'.c')
context.Result(result)
if not result:
context.env.Append(CPPDEFINES=["HAS_sprintf_void"])
print(printer.ConfigString(
" WARNING: apparently sprintf() does not return a value. zlib"))
print(printer.ConfigString(
" can build but will be open to possible string-format security"))
print(printer.ConfigString(" vulnerabilities."))
return result
def CheckHidden(context):
context.Message(printer.ConfigString(
"Checking for attribute(visibility) support... "))
result = context.TryCompile("""
#define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
int ZLIB_INTERNAL foo;
int main()
{
return 0;
}
""",
'.c')
context.Result(result)
if result:
context.env.Append(CPPDEFINES=["HAVE_HIDDEN"])
return result
if not env.GetOption('clean'):
if(GetOption('option_reconfigure')):
os.remove('build.conf')
conf_vars = Variables('build.conf')
conf_vars.Add('ZPREFIX', '')
conf_vars.Add('SOLO', '')
conf_vars.Add('COVER', '')
conf_vars.Update(env)
if('--zprefix' in sys.argv):
env['ZPREFIX'] = GetOption('option_zprefix')
if('--solo' in sys.argv):
env['SOLO'] = GetOption('option_solo')
if('--cover' in sys.argv):
env['COVER'] = GetOption('option_cover')
conf_vars.Save('build.conf', env)
configureString = ""
if env['ZPREFIX']:
configureString += "--zprefix "
if env['SOLO']:
configureString += "--solo "
if env['COVER']:
configureString += "--cover "
if configureString == "":
configureString = "Configuring... "
else:
configureString = "Configuring with " + configureString
printer.InfoPrint(configureString)
SCons.Script.Main.progress_display.set_mode(1)
conf = Configure(env, conf_dir="build/conf_tests", log_file="conf.log",
custom_tests={
'check_large_file64': check_large_file64,
'check_fseeko': check_fseeko,
'CheckSizeT': CheckSizeT,
'CheckSizeTLongLong': CheckSizeTLongLong,
'CheckSizeTPointerSize': CheckSizeTPointerSize,
'CheckSharedLibrary': CheckSharedLibrary,
'CheckUnistdH': CheckUnistdH,
'CheckStrerror': CheckStrerror,
'CheckStdargH': CheckStdargH,
'CheckVsnprintf': CheckVsnprintf,
'CheckVsnStdio': CheckVsnStdio,
'CheckVsnprintfReturn': CheckVsnprintfReturn,
'CheckVsprintfReturn': CheckVsprintfReturn,
'CheckSnStdio': CheckSnStdio,
'CheckSnprintfReturn': CheckSnprintfReturn,
'CheckSprintfReturn': CheckSprintfReturn,
'CheckHidden': CheckHidden,
'AddZPrefix': AddZPrefix,
'AddSolo': AddSolo,
'AddCover': AddCover,
})
with open('repo/zconf.h.in', 'r') as content_file:
conf.env["ZCONFH"] = str(content_file.read())
conf.CheckSharedLibrary()
# conf.CheckExternalNames()
if not conf.CheckSizeT():
conf.CheckSizeTPointerSize(conf.CheckSizeTLongLong())
if not conf.check_large_file64():
conf.check_fseeko()
conf.CheckStrerror()
conf.CheckUnistdH()
conf.CheckStdargH()
if conf.env['ZPREFIX']:
conf.AddZPrefix()
if conf.env['SOLO']:
conf.AddSolo()
if conf.env['COVER']:
conf.AddCover()
with open('repo/zconf.h', 'w') as content_file:
content_file.write(conf.env["ZCONFH"])
if conf.CheckVsnprintf():
if conf.CheckVsnStdio():
conf.CheckVsnprintfReturn()
else:
conf.CheckVsprintfReturn()
else:
if conf.CheckSnStdio():
conf.CheckSnprintfReturn()
else:
conf.CheckSprintfReturn()
conf.CheckHidden()
SCons.Script.Main.progress_display.set_mode(0)
env = conf.Finish()
if("linux" in platform.system().lower()):
debugFlag = ""
if(env['DEBUG_BUILD']):
debugFlag = "-g"
env.Append(CPPPATH=[
])
env.Append(CCFLAGS=[
debugFlag,
'-O3',
# '-fPIC',
# "-rdynamic",
])
env.Append(CXXFLAGS=[
# "-std=c++11",
])
env.Append(LDFLAGS=[
debugFlag,
# "-rdynamic",
])
env.Append(LIBPATH=[
# "/usr/lib/gnome-settings-daemon-3.0/",
])
env.Append(LIBS=[
# "GL",
])
elif("darwin" in platform.system().lower()):
print("XCode project not implemented yet")
elif("win" in platform.system().lower()):
pass
return env
def ConfigPlatformIDE(env, sourceFiles, headerFiles, resources, program):
if platform == "linux" or platform == "linux2":
print("Eclipse C++ project not implemented yet")
elif("darwin" in platform.system().lower()):
print("XCode project not implemented yet")
elif("win" in platform.system().lower()):
variantSourceFiles = []
for file in sourceFiles:
variantSourceFiles.append(re.sub("^build", "../Source", file))
variantHeaderFiles = []
for file in headerFiles:
variantHeaderFiles.append(re.sub("^Source", "../Source", file))
buildSettings = {
'LocalDebuggerCommand': os.path.abspath(env['PROJECT_DIR']).replace('\\', '/') + "/build/MyLifeApp.exe",
'LocalDebuggerWorkingDirectory': os.path.abspath(env['PROJECT_DIR']).replace('\\', '/') + "/build/",
}
buildVariant = 'Release|x64'
cmdargs = ''
if(env['DEBUG_BUILD']):
buildVariant = 'Debug|x64'
cmdargs = '--debug_build'
env.MSVSProject(target='VisualStudio/MyLifeApp' + env['MSVSPROJECTSUFFIX'],
srcs=variantSourceFiles,
localincs=variantHeaderFiles,
resources=resources,
buildtarget=program,
DebugSettings=buildSettings,
variant=buildVariant,
cmdargs=cmdargs)
return env
def SetupInstalls(env):
return env
def SetupOptions():
AddOption(
'--debug-build',
dest='option_debug',
action='store_true',
metavar='BOOL',
default=False,
help='Build debug libraries'
)
AddOption(
'--ASM686',
dest='option_asm686',
action='store_true',
metavar='BOOL',
default=False,
help='Enable building i686 assembly implementation'
)
AddOption(
'--AMD64',
dest='option_amd64',
action='store_true',
metavar='BOOL',
default=False,
help='Enable building amd64 assembly implementation'
)
AddOption(
'--zprefix',
dest='option_zprefix',
action='store_true',
metavar='BOOL',
default=False,
help='Precedes all external symbols with z_ to reduce probability of a symbol name collision'
)
AddOption(
'--solo',
dest='option_solo',
action='store_true',
metavar='BOOL',
default=False,
help='Used to compile and use zlib without the use of any external libraries.'
)
AddOption(
'--cover',
dest='option_cover',
action='store_true',
metavar='BOOL',
default=False,
help='code coverage testing was requested'
)
AddOption(
'--gcc-classic',
dest='option_gcc_classic',
action='store',
type='string',
metavar='STRING',
default='',
help='An alternate GCC to use in some cases such as Code Coverage testing.'
)
AddOption(
'--verbose',
dest='option_verbose',
action='store_true',
metavar='BOOL',
default=False,
help='View compiler output.'
)
AddOption(
'--reconfigure',
dest='option_reconfigure',
action='store_true',
metavar='BOOL',
default=False,
help='Clear out configuration and reconfigure with new options.'
)
if(not GetOption('option_verbose')):
scons_ver = SCons.__version__
if int(scons_ver[0]) >= 3:
SetOption('silent', 1)
SCons.Script.Main.progress_display.set_mode(0)
create_new_env()