Home | History | Annotate | Download | only in tests
      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         
     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
    100 if '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')
    110 elif 'clang' in env['CC']:
    111     # CLang
    112     env.Append(CFLAGS = '-ansi -g -Wall -Werror')
    113     env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
    114 elif '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')
    126 elif 'tcc' in env['CC']:
    127     # Tiny C Compiler
    128     env.Append(CFLAGS = '-Wall -Werror -g')
    129 
    130 env.SetDefault(CORECFLAGS = '')
    131 
    132 if 'clang' in env['CXX']:
    133     env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
    134 elif 'g++' in env['CXX'] or 'gcc' in env['CXX']:
    135     env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
    136 elif 'cl' in env['CXX']:
    137     env.Append(CXXFLAGS = '/Zi /W2 /WX')
    138     
    139 # Now include the SConscript files from all subdirectories
    140 import os.path
    141 env['VARIANT_DIR'] = 'build'
    142 env['BUILD'] = '#' + env['VARIANT_DIR']
    143 env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common'
    144 for subdir in Glob('*/SConscript'):
    145     SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir)))
    146 
    147