SConstruct 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. You can modify the behaviour using following options:
  6. BUILDDIR Directory to build into (default "build")
  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. LINK Name of linker (usually same as CC)
  12. LINKFLAGS Flags to pass to linker
  13. LINKLIBS Flags to pass to linker after object files
  14. PROTOC Path to protoc binary
  15. PROTOCFLAGS Arguments to pass protoc
  16. NODEFARGS Do not add the default CCFLAGS
  17. NOVALGRIND Do not use valgrind for memory checks
  18. For example, for a clang build, use:
  19. scons CC=clang CXX=clang++
  20. ''')
  21. import os
  22. import platform
  23. env = Environment(ENV = os.environ)
  24. # Allow giving environment flags on command line.
  25. list_vars = ['CCFLAGS', 'CXXFLAGS', 'LINKFLAGS', 'LINKLIBS', 'PROTOCFLAGS']
  26. for var,val in ARGUMENTS.items():
  27. if var in list_vars:
  28. env.Append(**{var: val})
  29. else:
  30. env.Replace(**{var: val})
  31. env.SetDefault(BUILDDIR = "build")
  32. env.SConsignFile(env['BUILDDIR'] + "/sconsign")
  33. env.Replace(CONFIGUREDIR = env['BUILDDIR'] + "/config")
  34. # If a cross compilation platform is given, apply the environment settings
  35. # from site_scons/platforms/X/X.py
  36. if ARGUMENTS.get('PLATFORM'):
  37. platform_func = platforms.get(ARGUMENTS.get('PLATFORM'))
  38. if not platform_func:
  39. print("Supported platforms: " + str(platforms.keys()))
  40. raise Exception("Unsupported platform: " + ARGUMENTS.get('PLATFORM'))
  41. else:
  42. platform_func(env)
  43. # Load nanopb generator build rules from site_scons/site_tools/nanopb.py
  44. env.Tool("nanopb")
  45. # Limit memory usage. This is to catch problems like issue #338
  46. try:
  47. import resource
  48. soft, hard = resource.getrlimit(resourse.RLIMIT_AS)
  49. resource.setrlimit(resource.RLIMIT_AS, (100*1024*1024, hard))
  50. except:
  51. pass
  52. # On Mac OS X, gcc is usually alias for clang.
  53. # To avoid problem with default options, use clang directly.
  54. if platform.system() == "Darwin" and 'CC' not in ARGUMENTS:
  55. env.Replace(CC = "clang", CXX = "clang++")
  56. # Add the builders defined in site_init.py
  57. add_nanopb_builders(env)
  58. # Path to the files shared by tests, and to the nanopb core.
  59. env.Append(CPPPATH = ["#../", "$COMMON"])
  60. # Path for finding nanopb.proto
  61. env.Append(PROTOCPATH = '#../generator')
  62. # Check the compilation environment, unless we are just cleaning up.
  63. if not env.GetOption('clean'):
  64. def check_ccflags(context, flags, linkflags = ''):
  65. '''Check if given CCFLAGS are supported'''
  66. context.Message('Checking support for CCFLAGS="%s"... ' % flags)
  67. oldflags = context.env['CCFLAGS']
  68. oldlinkflags = context.env['LINKFLAGS']
  69. context.env.Append(CCFLAGS = flags)
  70. context.env.Append(LINKFLAGS = linkflags)
  71. result = context.TryCompile("int main() {return 0;}", '.c')
  72. context.env.Replace(CCFLAGS = oldflags)
  73. context.env.Replace(LINKFLAGS = oldlinkflags)
  74. context.Result(result)
  75. return result
  76. def check_protocversion(context):
  77. context.Display("Checking protoc version... ")
  78. status, output = context.TryAction('$PROTOC --version > $TARGET')
  79. if status:
  80. context.Result(str(output.strip()))
  81. return str(output.strip())
  82. else:
  83. context.Display("error: %s\n" % output.strip())
  84. context.did_show_result = 1
  85. context.Result("unknown, assuming 3.6.1")
  86. return "3.6.1"
  87. conf = Configure(env, custom_tests =
  88. {'CheckCCFLAGS': check_ccflags,
  89. 'CheckProtocVersion': check_protocversion})
  90. # If the platform doesn't support C99, use our own header file instead.
  91. stdbool = conf.CheckCHeader('stdbool.h')
  92. stdint = conf.CheckCHeader('stdint.h')
  93. stddef = conf.CheckCHeader('stddef.h')
  94. string = conf.CheckCHeader('string.h')
  95. stdlib = conf.CheckCHeader('stdlib.h')
  96. limits = conf.CheckCHeader('limits.h')
  97. if not stdbool or not stdint or not stddef or not string or not limits:
  98. conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'})
  99. conf.env.Append(CPPPATH = "#../extra")
  100. conf.env.Append(SYSHDR = '\\"pb_syshdr.h\\"')
  101. if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1})
  102. if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1})
  103. if stddef: conf.env.Append(CPPDEFINES = {'HAVE_STDDEF_H': 1})
  104. if string: conf.env.Append(CPPDEFINES = {'HAVE_STRING_H': 1})
  105. if stdlib: conf.env.Append(CPPDEFINES = {'HAVE_STDLIB_H': 1})
  106. if limits: conf.env.Append(CPPDEFINES = {'HAVE_LIMITS_H': 1})
  107. # Check protoc version
  108. conf.env['PROTOC_VERSION'] = conf.CheckProtocVersion()
  109. # Initialize compiler arguments with defaults, unless overridden on
  110. # command line.
  111. if not env.get('NODEFARGS'):
  112. # Check if libmudflap is available (only with GCC)
  113. if 'gcc' in env['CC']:
  114. if conf.CheckLib('mudflap'):
  115. conf.env.Append(CCFLAGS = '-fmudflap')
  116. conf.env.Append(LINKFLAGS = '-fmudflap')
  117. # Check if we can use extra strict warning flags (only with GCC)
  118. extra = '-Wcast-qual -Wlogical-op -Wconversion'
  119. extra += ' -fstrict-aliasing -Wstrict-aliasing=1'
  120. extra += ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls'
  121. extra += ' -Wstack-protector '
  122. if 'gcc' in env['CC']:
  123. if conf.CheckCCFLAGS(extra):
  124. conf.env.Append(CORECFLAGS = extra)
  125. # Check if we can use undefined behaviour sanitizer (only with clang)
  126. # TODO: Fuzz test triggers the bool sanitizer, figure out whether to
  127. # modify the fuzz test or to keep ignoring the check.
  128. extra = '-fsanitize=undefined,integer -fno-sanitize-recover=undefined,integer '
  129. if 'clang' in env['CC']:
  130. if conf.CheckCCFLAGS(extra, linkflags = extra):
  131. conf.env.Append(CORECFLAGS = extra)
  132. conf.env.Append(LINKFLAGS = extra)
  133. # End the config stuff
  134. env = conf.Finish()
  135. if not env.get('NODEFARGS'):
  136. # Initialize the CCFLAGS according to the compiler
  137. if 'gcc' in env['CC']:
  138. # GNU Compiler Collection
  139. # Debug info, warnings as errors
  140. env.Append(CFLAGS = '-g -Wall -Werror ')
  141. env.Append(CORECFLAGS = '-Wextra')
  142. # Pedantic ANSI C. On AVR this doesn't work because we use large
  143. # enums in some of the tests.
  144. if env.get("EMBEDDED") != "AVR":
  145. env.Append(CFLAGS = '-ansi -pedantic')
  146. # Profiling and coverage
  147. if not env.get("EMBEDDED"):
  148. env.Append(CFLAGS = '-fprofile-arcs -ftest-coverage ')
  149. env.Append(LINKFLAGS = '-g --coverage')
  150. # We currently need uint64_t anyway, even though ANSI C90 otherwise..
  151. env.Append(CFLAGS = '-Wno-long-long')
  152. elif 'clang' in env['CC']:
  153. # CLang
  154. env.Append(CFLAGS = '-ansi -g -Wall -Werror')
  155. env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
  156. elif 'cl' in env['CC']:
  157. # Microsoft Visual C++
  158. # Debug info on, warning level 2 for tests, warnings as errors
  159. env.Append(CFLAGS = '/Zi /W2 /WX')
  160. env.Append(LINKFLAGS = '/DEBUG')
  161. # More strict checks on the nanopb core
  162. env.Append(CORECFLAGS = '/W4')
  163. # Disable warning about sizeof(union{}) construct that is used in
  164. # message size macros, in e.g. multiple_files testcase. The C construct
  165. # itself is valid, but quite rare, which causes Visual C++ to give a warning
  166. # about it.
  167. env.Append(CFLAGS = '/wd4116')
  168. elif 'tcc' in env['CC']:
  169. # Tiny C Compiler
  170. env.Append(CFLAGS = '-Wall -Werror -g')
  171. if 'clang' in env['CXX']:
  172. env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
  173. elif 'g++' in env['CXX'] or 'gcc' in env['CXX']:
  174. env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
  175. elif 'cl' in env['CXX']:
  176. env.Append(CXXFLAGS = '/Zi /W2 /WX /wd4116 /wd4127')
  177. env.SetDefault(CORECFLAGS = '')
  178. if not env.get("EMBEDDED") and not env.get("NOVALGRIND"):
  179. valgrind = env.WhereIs('valgrind')
  180. if valgrind:
  181. env.SetDefault(VALGRIND = valgrind)
  182. # Make it possible to add commands to the end of linker line
  183. env.SetDefault(LINKLIBS = '')
  184. env.Replace(LINKCOM = env['LINKCOM'] + " $LINKLIBS")
  185. # Place build files under separate folder
  186. import os.path
  187. env['VARIANT_DIR'] = env['BUILDDIR']
  188. env['BUILD'] = '#' + env['VARIANT_DIR']
  189. env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common'
  190. # Include common/SConscript first to make sure its exports are available
  191. # to other SConscripts.
  192. SConscript("common/SConscript", exports = 'env', variant_dir = env['VARIANT_DIR'] + '/common')
  193. # Now include the SConscript files from all subdirectories
  194. for subdir in Glob('*/SConscript') + Glob('regression/*/SConscript'):
  195. if str(subdir).startswith("common/"): continue
  196. if str(subdir).startswith("common\\"): continue
  197. SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir)))