Move malloc support to tests/common directory
[apps/agl-service-can-low-level.git] / tests / SConstruct
1 Help('''
2 Type 'scons' to build and run all the available test cases.
3 It will automatically detect your platform and C compiler and
4 build appropriately.
5
6 You can modify the behavious using following options:
7 CC          Name of C compiler
8 CXX         Name of C++ compiler
9 CCFLAGS     Flags to pass to the C compiler
10 CXXFLAGS    Flags to pass to the C++ compiler
11
12 For example, for a clang build, use:
13 scons CC=clang CXX=clang++
14 ''')
15
16 import os
17 env = Environment(ENV = os.environ, tools = ['default', 'nanopb'])
18
19 # Allow overriding the compiler with scons CC=???
20 if 'CC' in ARGUMENTS: env.Replace(CC = ARGUMENTS['CC'])
21 if 'CXX' in ARGUMENTS: env.Replace(CXX = ARGUMENTS['CXX'])
22 if 'CCFLAGS' in ARGUMENTS: env.Append(CCFLAGS = ARGUMENTS['CCFLAGS'])
23 if 'CXXFLAGS' in ARGUMENTS: env.Append(CXXFLAGS = ARGUMENTS['CXXFLAGS'])
24
25 # Add the builders defined in site_init.py
26 add_nanopb_builders(env)
27
28 # Path to the files shared by tests, and to the nanopb core.
29 env.Append(CPPPATH = ["#../", "$COMMON"])
30
31 # Path for finding nanopb.proto
32 env.Append(PROTOCPATH = '#../generator')
33
34 # Check the compilation environment, unless we are just cleaning up.
35 if 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 if libmudflap is available (only with GCC)
76     if 'gcc' in env['CC']:
77         if conf.CheckLib('mudflap'):
78             conf.env.Append(CCFLAGS = '-fmudflap')
79             conf.env.Append(LINKFLAGS = '-fmudflap')
80     
81     # Check if we can use extra strict warning flags (only with GCC)
82     extra = '-Wcast-qual -Wlogical-op -Wconversion'
83     extra += ' -fstrict-aliasing -Wstrict-aliasing=1'
84     extra += ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls'
85     extra += ' -Wstack-protector '
86     if 'gcc' in env['CC']:
87         if conf.CheckCCFLAGS(extra):
88             conf.env.Append(CORECFLAGS = extra)
89     
90     # Check if we can use undefined behaviour sanitizer (only with clang)
91     extra = '-fsanitize=undefined '
92     if 'clang' in env['CC']:
93         if conf.CheckCCFLAGS(extra, linkflags = extra):
94             conf.env.Append(CORECFLAGS = extra)
95             conf.env.Append(LINKFLAGS = extra)
96     
97     # End the config stuff
98     env = conf.Finish()
99
100 # Initialize the CCFLAGS according to the compiler
101 if 'gcc' in env['CC']:
102     # GNU Compiler Collection
103     
104     # Debug info, warnings as errors
105     env.Append(CFLAGS = '-ansi -pedantic -g -Wall -Werror -fprofile-arcs -ftest-coverage ')
106     env.Append(CORECFLAGS = '-Wextra')
107     env.Append(LINKFLAGS = '-g --coverage')
108     
109     # We currently need uint64_t anyway, even though ANSI C90 otherwise..
110     env.Append(CFLAGS = '-Wno-long-long')
111 elif 'clang' in env['CC']:
112     # CLang
113     env.Append(CFLAGS = '-ansi -g -Wall -Werror')
114     env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
115 elif 'cl' in env['CC']:
116     # Microsoft Visual C++
117     
118     # Debug info on, warning level 2 for tests, warnings as errors
119     env.Append(CFLAGS = '/Zi /W2 /WX')
120     env.Append(LINKFLAGS = '/DEBUG')
121     
122     # More strict checks on the nanopb core
123     env.Append(CORECFLAGS = '/W4')
124     
125     # PB_RETURN_ERROR triggers C4127 because of while(0)
126     env.Append(CFLAGS = '/wd4127')
127 elif 'tcc' in env['CC']:
128     # Tiny C Compiler
129     env.Append(CFLAGS = '-Wall -Werror -g')
130
131 env.SetDefault(CORECFLAGS = '')
132
133 if 'clang' in env['CXX']:
134     env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
135 elif 'g++' in env['CXX'] or 'gcc' in env['CXX']:
136     env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
137 elif 'cl' in env['CXX']:
138     env.Append(CXXFLAGS = '/Zi /W2 /WX')
139
140 # Now include the SConscript files from all subdirectories
141 import os.path
142 env['VARIANT_DIR'] = 'build'
143 env['BUILD'] = '#' + env['VARIANT_DIR']
144 env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common'
145
146 # Include common/SConscript first to make sure its exports are available
147 # to other SConscripts.
148 SConscript("common/SConscript", exports = 'env', variant_dir = env['VARIANT_DIR'] + '/common')
149
150 for subdir in Glob('*/SConscript') + Glob('regression/*/SConscript'):
151     if str(subdir).startswith("common"): continue
152     SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir)))
153