1Help('''
2Type 'scons' to build and run all the available test cases.
3It will automatically detect your platform and C compiler and
4build appropriately.
5
6You can modify the behavious using following options:
7CC          Name of C compiler
8CXX         Name of C++ compiler
9CCFLAGS     Flags to pass to the C compiler
10CXXFLAGS    Flags to pass to the C++ compiler
11
12For example, for a clang build, use:
13scons CC=clang CXX=clang++
14''')
15
16import os
17env = Environment(ENV = os.environ, tools = ['default', 'nanopb'])
18
19# Allow overriding the compiler with scons CC=???
20if 'CC' in ARGUMENTS: env.Replace(CC = ARGUMENTS['CC'])
21if 'CXX' in ARGUMENTS: env.Replace(CXX = ARGUMENTS['CXX'])
22if 'CCFLAGS' in ARGUMENTS: env.Append(CCFLAGS = ARGUMENTS['CCFLAGS'])
23if 'CXXFLAGS' in ARGUMENTS: env.Append(CXXFLAGS = ARGUMENTS['CXXFLAGS'])
24
25# Add the builders defined in site_init.py
26add_nanopb_builders(env)
27
28# Path to the files shared by tests, and to the nanopb core.
29env.Append(CPPPATH = ["#../", "$COMMON"])
30
31# Path for finding nanopb.proto
32env.Append(PROTOCPATH = '#../generator')
33
34# Check the compilation environment, unless we are just cleaning up.
35if not env.GetOption('clean'):
36    def check_ccflags(context, flags, linkflags = ''):
37        '''Check if given CCFLAGS are supported'''
38        context.Message('Checking support for CCFLAGS="%s"... ' % flags)
39        oldflags = context.env['CCFLAGS']
40        oldlinkflags = context.env['CCFLAGS']
41        context.env.Append(CCFLAGS = flags)
42        context.env.Append(LINKFLAGS = linkflags)
43        result = context.TryCompile("int main() {return 0;}", '.c')
44        context.env.Replace(CCFLAGS = oldflags)
45        context.env.Replace(LINKFLAGS = oldlinkflags)
46        context.Result(result)
47        return result
48
49    conf = Configure(env, custom_tests = {'CheckCCFLAGS': check_ccflags})
50
51    # If the platform doesn't support C99, use our own header file instead.
52    stdbool = conf.CheckCHeader('stdbool.h')
53    stdint = conf.CheckCHeader('stdint.h')
54    stddef = conf.CheckCHeader('stddef.h')
55    string = conf.CheckCHeader('string.h')
56    stdlib = conf.CheckCHeader('stdlib.h')
57    if not stdbool or not stdint or not stddef or not string:
58        conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'})
59        conf.env.Append(CPPPATH = "#../extra")
60        conf.env.Append(SYSHDR = '\\"pb_syshdr.h\\"')
61
62        if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1})
63        if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1})
64        if stddef: conf.env.Append(CPPDEFINES = {'HAVE_STDDEF_H': 1})
65        if string: conf.env.Append(CPPDEFINES = {'HAVE_STRING_H': 1})
66        if stdlib: conf.env.Append(CPPDEFINES = {'HAVE_STDLIB_H': 1})
67
68    # Check if we can use pkg-config to find protobuf include path
69    status, output = conf.TryAction('pkg-config protobuf --variable=includedir > $TARGET')
70    if status:
71        conf.env.Append(PROTOCPATH = output.strip())
72    else:
73        conf.env.Append(PROTOCPATH = '/usr/include')
74
75    # Check protoc version
76    status, output = conf.TryAction('$PROTOC --version > $TARGET')
77    if status:
78        conf.env['PROTOC_VERSION'] = output
79
80    # Check if libmudflap is available (only with GCC)
81    if 'gcc' in env['CC']:
82        if conf.CheckLib('mudflap'):
83            conf.env.Append(CCFLAGS = '-fmudflap')
84            conf.env.Append(LINKFLAGS = '-fmudflap')
85
86    # Check if we can use extra strict warning flags (only with GCC)
87    extra = '-Wcast-qual -Wlogical-op -Wconversion'
88    extra += ' -fstrict-aliasing -Wstrict-aliasing=1'
89    extra += ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls'
90    extra += ' -Wstack-protector '
91    if 'gcc' in env['CC']:
92        if conf.CheckCCFLAGS(extra):
93            conf.env.Append(CORECFLAGS = extra)
94
95    # Check if we can use undefined behaviour sanitizer (only with clang)
96    # TODO: Fuzz test triggers the bool sanitizer, figure out whether to
97    #       modify the fuzz test or to keep ignoring the check.
98    extra = '-fsanitize=undefined,integer -fno-sanitize-recover=undefined,integer -fsanitize-recover=bool '
99    if 'clang' in env['CC']:
100        if conf.CheckCCFLAGS(extra, linkflags = extra):
101            conf.env.Append(CORECFLAGS = extra)
102            conf.env.Append(LINKFLAGS = extra)
103
104    # End the config stuff
105    env = conf.Finish()
106
107# Initialize the CCFLAGS according to the compiler
108if 'gcc' in env['CC']:
109    # GNU Compiler Collection
110
111    # Debug info, warnings as errors
112    env.Append(CFLAGS = '-ansi -pedantic -g -Wall -Werror -fprofile-arcs -ftest-coverage ')
113    env.Append(CORECFLAGS = '-Wextra')
114    env.Append(LINKFLAGS = '-g --coverage')
115
116    # We currently need uint64_t anyway, even though ANSI C90 otherwise..
117    env.Append(CFLAGS = '-Wno-long-long')
118elif 'clang' in env['CC']:
119    # CLang
120    env.Append(CFLAGS = '-ansi -g -Wall -Werror')
121    env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
122elif 'cl' in env['CC']:
123    # Microsoft Visual C++
124
125    # Debug info on, warning level 2 for tests, warnings as errors
126    env.Append(CFLAGS = '/Zi /W2 /WX')
127    env.Append(LINKFLAGS = '/DEBUG')
128
129    # More strict checks on the nanopb core
130    env.Append(CORECFLAGS = '/W4')
131elif 'tcc' in env['CC']:
132    # Tiny C Compiler
133    env.Append(CFLAGS = '-Wall -Werror -g')
134
135env.SetDefault(CORECFLAGS = '')
136
137if 'clang' in env['CXX']:
138    env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
139elif 'g++' in env['CXX'] or 'gcc' in env['CXX']:
140    env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
141elif 'cl' in env['CXX']:
142    env.Append(CXXFLAGS = '/Zi /W2 /WX')
143
144# Now include the SConscript files from all subdirectories
145import os.path
146env['VARIANT_DIR'] = 'build'
147env['BUILD'] = '#' + env['VARIANT_DIR']
148env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common'
149
150# Include common/SConscript first to make sure its exports are available
151# to other SConscripts.
152SConscript("common/SConscript", exports = 'env', variant_dir = env['VARIANT_DIR'] + '/common')
153
154for subdir in Glob('*/SConscript') + Glob('regression/*/SConscript'):
155    if str(subdir).startswith("common"): continue
156    SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir)))
157
158