xref: /aosp_15_r20/external/nanopb-c/tests/SConstruct (revision c8d645cafcee3f91213d30caa0fe303887010b9b)
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# Limit memory usage. This is to catch problems like issue #338
20try:
21    import resource
22    soft, hard = resource.getrlimit(resourse.RLIMIT_AS)
23    resource.setrlimit(resource.RLIMIT_AS, (100*1024*1024, hard))
24except:
25    pass
26
27# Allow overriding the compiler with scons CC=???
28if 'CC' in ARGUMENTS: env.Replace(CC = ARGUMENTS['CC'])
29if 'CXX' in ARGUMENTS: env.Replace(CXX = ARGUMENTS['CXX'])
30if 'CCFLAGS' in ARGUMENTS: env.Append(CCFLAGS = ARGUMENTS['CCFLAGS'])
31if 'CXXFLAGS' in ARGUMENTS: env.Append(CXXFLAGS = ARGUMENTS['CXXFLAGS'])
32
33# Add the builders defined in site_init.py
34add_nanopb_builders(env)
35
36# Path to the files shared by tests, and to the nanopb core.
37env.Append(CPPPATH = ["#../", "$COMMON"])
38
39# Path for finding nanopb.proto
40env.Append(PROTOCPATH = '#../generator')
41
42# Check the compilation environment, unless we are just cleaning up.
43if not env.GetOption('clean'):
44    def check_ccflags(context, flags, linkflags = ''):
45        '''Check if given CCFLAGS are supported'''
46        context.Message('Checking support for CCFLAGS="%s"... ' % flags)
47        oldflags = context.env['CCFLAGS']
48        oldlinkflags = context.env['CCFLAGS']
49        context.env.Append(CCFLAGS = flags)
50        context.env.Append(LINKFLAGS = linkflags)
51        result = context.TryCompile("int main() {return 0;}", '.c')
52        context.env.Replace(CCFLAGS = oldflags)
53        context.env.Replace(LINKFLAGS = oldlinkflags)
54        context.Result(result)
55        return result
56
57    conf = Configure(env, custom_tests = {'CheckCCFLAGS': check_ccflags})
58
59    # If the platform doesn't support C99, use our own header file instead.
60    stdbool = conf.CheckCHeader('stdbool.h')
61    stdint = conf.CheckCHeader('stdint.h')
62    stddef = conf.CheckCHeader('stddef.h')
63    string = conf.CheckCHeader('string.h')
64    stdlib = conf.CheckCHeader('stdlib.h')
65    if not stdbool or not stdint or not stddef or not string:
66        conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'})
67        conf.env.Append(CPPPATH = "#../extra")
68        conf.env.Append(SYSHDR = '\\"pb_syshdr.h\\"')
69
70        if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1})
71        if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1})
72        if stddef: conf.env.Append(CPPDEFINES = {'HAVE_STDDEF_H': 1})
73        if string: conf.env.Append(CPPDEFINES = {'HAVE_STRING_H': 1})
74        if stdlib: conf.env.Append(CPPDEFINES = {'HAVE_STDLIB_H': 1})
75
76    # Check if we can use pkg-config to find protobuf include path
77    status, output = conf.TryAction('pkg-config protobuf --variable=includedir > $TARGET')
78    if status:
79        conf.env.Append(PROTOCPATH = output.strip())
80    else:
81        conf.env.Append(PROTOCPATH = '/usr/include')
82
83    # Check protoc version
84    status, output = conf.TryAction('$PROTOC --version > $TARGET')
85    if status:
86        conf.env['PROTOC_VERSION'] = output
87
88    # Check if libmudflap is available (only with GCC)
89    if 'gcc' in env['CC']:
90        if conf.CheckLib('mudflap'):
91            conf.env.Append(CCFLAGS = '-fmudflap')
92            conf.env.Append(LINKFLAGS = '-fmudflap')
93
94    # Check if we can use extra strict warning flags (only with GCC)
95    extra = '-Wcast-qual -Wlogical-op -Wconversion'
96    extra += ' -fstrict-aliasing -Wstrict-aliasing=1'
97    extra += ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls'
98    extra += ' -Wstack-protector '
99    if 'gcc' in env['CC']:
100        if conf.CheckCCFLAGS(extra):
101            conf.env.Append(CORECFLAGS = extra)
102
103    # Check if we can use undefined behaviour sanitizer (only with clang)
104    # TODO: Fuzz test triggers the bool sanitizer, figure out whether to
105    #       modify the fuzz test or to keep ignoring the check.
106    extra = '-fsanitize=undefined,integer -fno-sanitize-recover=undefined,integer '
107    if 'clang' in env['CC']:
108        if conf.CheckCCFLAGS(extra, linkflags = extra):
109            conf.env.Append(CORECFLAGS = extra)
110            conf.env.Append(LINKFLAGS = extra)
111
112    # End the config stuff
113    env = conf.Finish()
114
115# Initialize the CCFLAGS according to the compiler
116if 'gcc' in env['CC']:
117    # GNU Compiler Collection
118
119    # Debug info, warnings as errors
120    env.Append(CFLAGS = '-ansi -pedantic -g -Wall -Werror -fprofile-arcs -ftest-coverage ')
121    env.Append(CORECFLAGS = '-Wextra')
122    env.Append(LINKFLAGS = '-g --coverage')
123
124    # We currently need uint64_t anyway, even though ANSI C90 otherwise..
125    env.Append(CFLAGS = '-Wno-long-long')
126elif 'clang' in env['CC']:
127    # CLang
128    env.Append(CFLAGS = '-ansi -g -Wall -Werror')
129    env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
130elif 'cl' in env['CC']:
131    # Microsoft Visual C++
132
133    # Debug info on, warning level 2 for tests, warnings as errors
134    env.Append(CFLAGS = '/Zi /W2 /WX')
135    env.Append(LINKFLAGS = '/DEBUG')
136
137    # More strict checks on the nanopb core
138    env.Append(CORECFLAGS = '/W4')
139
140    # Disable warning about sizeof(union{}) construct that is used in
141    # message size macros, in e.g. multiple_files testcase. The C construct
142    # itself is valid, but quite rare, which causes Visual C++ to give a warning
143    # about it.
144    env.Append(CFLAGS = '/wd4116')
145elif 'tcc' in env['CC']:
146    # Tiny C Compiler
147    env.Append(CFLAGS = '-Wall -Werror -g')
148
149env.SetDefault(CORECFLAGS = '')
150
151if 'clang' in env['CXX']:
152    env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
153elif 'g++' in env['CXX'] or 'gcc' in env['CXX']:
154    env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
155elif 'cl' in env['CXX']:
156    env.Append(CXXFLAGS = '/Zi /W2 /WX /wd4116')
157
158# Now include the SConscript files from all subdirectories
159import os.path
160env['VARIANT_DIR'] = 'build'
161env['BUILD'] = '#' + env['VARIANT_DIR']
162env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common'
163
164# Include common/SConscript first to make sure its exports are available
165# to other SConscripts.
166SConscript("common/SConscript", exports = 'env', variant_dir = env['VARIANT_DIR'] + '/common')
167
168for subdir in Glob('*/SConscript') + Glob('regression/*/SConscript'):
169    if str(subdir).startswith("common"): continue
170    SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir)))
171
172