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        
61        if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1})
62        if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1})
63        if stddef: conf.env.Append(CPPDEFINES = {'HAVE_STDDEF_H': 1})
64        if string: conf.env.Append(CPPDEFINES = {'HAVE_STRING_H': 1})
65        if stdlib: conf.env.Append(CPPDEFINES = {'HAVE_STDLIB_H': 1})
66    
67    # Check if we can use pkg-config to find protobuf include path
68    status, output = conf.TryAction('pkg-config protobuf --variable=includedir > $TARGET')
69    if status:
70        conf.env.Append(PROTOCPATH = output.strip())
71    else:
72        conf.env.Append(PROTOCPATH = '/usr/include')
73    
74    # Check if libmudflap is available (only with GCC)
75    if 'gcc' in env['CC']:
76        if conf.CheckLib('mudflap'):
77            conf.env.Append(CCFLAGS = '-fmudflap')
78            conf.env.Append(LINKFLAGS = '-fmudflap')
79    
80    # Check if we can use extra strict warning flags (only with GCC)
81    extra = '-Wcast-qual -Wlogical-op -Wconversion'
82    extra += ' -fstrict-aliasing -Wstrict-aliasing=1'
83    extra += ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls'
84    extra += ' -Wstack-protector '
85    if 'gcc' in env['CC']:
86        if conf.CheckCCFLAGS(extra):
87            conf.env.Append(CORECFLAGS = extra)
88    
89    # Check if we can use undefined behaviour sanitizer (only with clang)
90    extra = '-fsanitize=undefined '
91    if 'clang' in env['CC']:
92        if conf.CheckCCFLAGS(extra, linkflags = extra):
93            conf.env.Append(CORECFLAGS = extra)
94            conf.env.Append(LINKFLAGS = extra)
95    
96    # End the config stuff
97    env = conf.Finish()
98
99# Initialize the CCFLAGS according to the compiler
100if 'gcc' in env['CC']:
101    # GNU Compiler Collection
102    
103    # Debug info, warnings as errors
104    env.Append(CFLAGS = '-ansi -pedantic -g -Wall -Werror -fprofile-arcs -ftest-coverage -fstack-protector-all')
105    env.Append(CORECFLAGS = '-Wextra')
106    env.Append(LINKFLAGS = '-g --coverage')
107    
108    # We currently need uint64_t anyway, even though ANSI C90 otherwise..
109    env.Append(CFLAGS = '-Wno-long-long')
110elif 'clang' in env['CC']:
111    # CLang
112    env.Append(CFLAGS = '-ansi -g -Wall -Werror')
113    env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
114elif 'cl' in env['CC']:
115    # Microsoft Visual C++
116    
117    # Debug info on, warning level 2 for tests, warnings as errors
118    env.Append(CFLAGS = '/Zi /W2 /WX')
119    env.Append(LINKFLAGS = '/DEBUG')
120    
121    # More strict checks on the nanopb core
122    env.Append(CORECFLAGS = '/W4')
123    
124    # PB_RETURN_ERROR triggers C4127 because of while(0)
125    env.Append(CFLAGS = '/wd4127')
126elif 'tcc' in env['CC']:
127    # Tiny C Compiler
128    env.Append(CFLAGS = '-Wall -Werror -g')
129
130env.SetDefault(CORECFLAGS = '')
131
132if 'clang' in env['CXX']:
133    env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
134elif 'g++' in env['CXX'] or 'gcc' in env['CXX']:
135    env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
136elif 'cl' in env['CXX']:
137    env.Append(CXXFLAGS = '/Zi /W2 /WX')
138    
139# Now include the SConscript files from all subdirectories
140import os.path
141env['VARIANT_DIR'] = 'build'
142env['BUILD'] = '#' + env['VARIANT_DIR']
143env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common'
144for subdir in Glob('*/SConscript'):
145    SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir)))
146
147