Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gallium.py 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. """gallium
  2. Frontend-tool for Gallium3D architecture.
  3. """
  4. #
  5. # Copyright 2008 VMware, Inc.
  6. # All Rights Reserved.
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining a
  9. # copy of this software and associated documentation files (the
  10. # "Software"), to deal in the Software without restriction, including
  11. # without limitation the rights to use, copy, modify, merge, publish,
  12. # distribute, sub license, and/or sell copies of the Software, and to
  13. # permit persons to whom the Software is furnished to do so, subject to
  14. # the following conditions:
  15. #
  16. # The above copyright notice and this permission notice (including the
  17. # next paragraph) shall be included in all copies or substantial portions
  18. # of the Software.
  19. #
  20. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  21. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  23. # IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  24. # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. #
  28. from __future__ import print_function
  29. import distutils.version
  30. import os
  31. import os.path
  32. import re
  33. import subprocess
  34. import platform as host_platform
  35. import sys
  36. import tempfile
  37. import SCons.Action
  38. import SCons.Builder
  39. import SCons.Scanner
  40. def symlink(target, source, env):
  41. target = str(target[0])
  42. source = str(source[0])
  43. if os.path.islink(target) or os.path.exists(target):
  44. os.remove(target)
  45. os.symlink(os.path.basename(source), target)
  46. def install(env, source, subdir):
  47. target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'], subdir)
  48. return env.Install(target_dir, source)
  49. def install_program(env, source):
  50. return install(env, source, 'bin')
  51. def install_shared_library(env, sources, version = ()):
  52. targets = []
  53. install_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'])
  54. version = tuple(map(str, version))
  55. if env['SHLIBSUFFIX'] == '.dll':
  56. dlls = env.FindIxes(sources, 'SHLIBPREFIX', 'SHLIBSUFFIX')
  57. targets += install(env, dlls, 'bin')
  58. libs = env.FindIxes(sources, 'LIBPREFIX', 'LIBSUFFIX')
  59. targets += install(env, libs, 'lib')
  60. else:
  61. for source in sources:
  62. target_dir = os.path.join(install_dir, 'lib')
  63. target_name = '.'.join((str(source),) + version)
  64. last = env.InstallAs(os.path.join(target_dir, target_name), source)
  65. targets += last
  66. while len(version):
  67. version = version[:-1]
  68. target_name = '.'.join((str(source),) + version)
  69. action = SCons.Action.Action(symlink, " Symlinking $TARGET ...")
  70. last = env.Command(os.path.join(target_dir, target_name), last, action)
  71. targets += last
  72. return targets
  73. def msvc2013_compat(env):
  74. if env['gcc']:
  75. env.Append(CCFLAGS = [
  76. '-Werror=vla',
  77. '-Werror=pointer-arith',
  78. ])
  79. def unit_test(env, test_name, program_target, args=None):
  80. env.InstallProgram(program_target)
  81. cmd = [program_target[0].abspath]
  82. if args is not None:
  83. cmd += args
  84. cmd = ' '.join(cmd)
  85. # http://www.scons.org/wiki/UnitTests
  86. action = SCons.Action.Action(cmd, " Running $SOURCE ...")
  87. alias = env.Alias(test_name, program_target, action)
  88. env.AlwaysBuild(alias)
  89. env.Depends('check', alias)
  90. def num_jobs():
  91. try:
  92. return int(os.environ['NUMBER_OF_PROCESSORS'])
  93. except (ValueError, KeyError):
  94. pass
  95. try:
  96. return os.sysconf('SC_NPROCESSORS_ONLN')
  97. except (ValueError, OSError, AttributeError):
  98. pass
  99. try:
  100. return int(os.popen2("sysctl -n hw.ncpu")[1].read())
  101. except ValueError:
  102. pass
  103. return 1
  104. def check_cc(env, cc, expr, cpp_opt = '-E'):
  105. # Invoke C-preprocessor to determine whether the specified expression is
  106. # true or not.
  107. sys.stdout.write('Checking for %s ... ' % cc)
  108. source = tempfile.NamedTemporaryFile(suffix='.c', delete=False)
  109. source.write(('#if !(%s)\n#error\n#endif\n' % expr).encode())
  110. source.close()
  111. # sys.stderr.write('%r %s %s\n' % (env['CC'], cpp_opt, source.name));
  112. pipe = SCons.Action._subproc(env, env.Split(env['CC']) + [cpp_opt, source.name],
  113. stdin = 'devnull',
  114. stderr = 'devnull',
  115. stdout = 'devnull')
  116. result = pipe.wait() == 0
  117. os.unlink(source.name)
  118. sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
  119. return result
  120. def check_header(env, header):
  121. '''Check if the header exist'''
  122. conf = SCons.Script.Configure(env)
  123. have_header = False
  124. if conf.CheckHeader(header):
  125. have_header = True
  126. env = conf.Finish()
  127. return have_header
  128. def check_functions(env, functions):
  129. '''Check if all of the functions exist'''
  130. conf = SCons.Script.Configure(env)
  131. have_functions = True
  132. for function in functions:
  133. if not conf.CheckFunc(function):
  134. have_functions = False
  135. env = conf.Finish()
  136. return have_functions
  137. def check_prog(env, prog):
  138. """Check whether this program exists."""
  139. sys.stdout.write('Checking for %s ... ' % prog)
  140. result = env.Detect(prog)
  141. sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
  142. return result
  143. def generate(env):
  144. """Common environment generation code"""
  145. # Tell tools which machine to compile for
  146. env['TARGET_ARCH'] = env['machine']
  147. env['MSVS_ARCH'] = env['machine']
  148. # Toolchain
  149. platform = env['platform']
  150. env.Tool(env['toolchain'])
  151. # Allow override compiler and specify additional flags from environment
  152. if 'CC' in os.environ:
  153. env['CC'] = os.environ['CC']
  154. if 'CFLAGS' in os.environ:
  155. env['CCFLAGS'] += SCons.Util.CLVar(os.environ['CFLAGS'])
  156. if 'CXX' in os.environ:
  157. env['CXX'] = os.environ['CXX']
  158. if 'CXXFLAGS' in os.environ:
  159. env['CXXFLAGS'] += SCons.Util.CLVar(os.environ['CXXFLAGS'])
  160. if 'LDFLAGS' in os.environ:
  161. env['LINKFLAGS'] += SCons.Util.CLVar(os.environ['LDFLAGS'])
  162. # Detect gcc/clang not by executable name, but through pre-defined macros
  163. # as autoconf does, to avoid drawing wrong conclusions when using tools
  164. # that overrice CC/CXX like scan-build.
  165. env['gcc_compat'] = 0
  166. env['clang'] = 0
  167. env['msvc'] = 0
  168. if host_platform.system() == 'Windows':
  169. env['msvc'] = check_cc(env, 'MSVC', 'defined(_MSC_VER)', '/E')
  170. if not env['msvc']:
  171. env['gcc_compat'] = check_cc(env, 'GCC', 'defined(__GNUC__)')
  172. env['clang'] = check_cc(env, 'Clang', '__clang__')
  173. env['gcc'] = env['gcc_compat'] and not env['clang']
  174. env['suncc'] = env['platform'] == 'sunos' and os.path.basename(env['CC']) == 'cc'
  175. env['icc'] = 'icc' == os.path.basename(env['CC'])
  176. # shortcuts
  177. machine = env['machine']
  178. platform = env['platform']
  179. x86 = env['machine'] == 'x86'
  180. ppc = env['machine'] == 'ppc'
  181. gcc_compat = env['gcc_compat']
  182. msvc = env['msvc']
  183. suncc = env['suncc']
  184. icc = env['icc']
  185. # Determine whether we are cross compiling; in particular, whether we need
  186. # to compile code generators with a different compiler as the target code.
  187. hosthost_platform = host_platform.system().lower()
  188. if hosthost_platform.startswith('cygwin'):
  189. hosthost_platform = 'cygwin'
  190. # Avoid spurious crosscompilation in MSYS2 environment.
  191. if hosthost_platform.startswith('mingw'):
  192. hosthost_platform = 'windows'
  193. host_machine = os.environ.get('PROCESSOR_ARCHITEW6432', os.environ.get('PROCESSOR_ARCHITECTURE', host_platform.machine()))
  194. host_machine = {
  195. 'x86': 'x86',
  196. 'i386': 'x86',
  197. 'i486': 'x86',
  198. 'i586': 'x86',
  199. 'i686': 'x86',
  200. 'ppc' : 'ppc',
  201. 'AMD64': 'x86_64',
  202. 'x86_64': 'x86_64',
  203. }.get(host_machine, 'generic')
  204. env['crosscompile'] = platform != hosthost_platform
  205. if machine == 'x86_64' and host_machine != 'x86_64':
  206. env['crosscompile'] = True
  207. env['hostonly'] = False
  208. # Backwards compatability with the debug= profile= options
  209. if env['build'] == 'debug':
  210. if not env['debug']:
  211. print('scons: warning: debug option is deprecated and will be removed eventually; use instead')
  212. print('')
  213. print(' scons build=release')
  214. print('')
  215. env['build'] = 'release'
  216. if env['profile']:
  217. print('scons: warning: profile option is deprecated and will be removed eventually; use instead')
  218. print('')
  219. print(' scons build=profile')
  220. print('')
  221. env['build'] = 'profile'
  222. if False:
  223. # Enforce SConscripts to use the new build variable
  224. env.popitem('debug')
  225. env.popitem('profile')
  226. else:
  227. # Backwards portability with older sconscripts
  228. if env['build'] in ('debug', 'checked'):
  229. env['debug'] = True
  230. env['profile'] = False
  231. if env['build'] == 'profile':
  232. env['debug'] = False
  233. env['profile'] = True
  234. if env['build'] == 'release':
  235. env['debug'] = False
  236. env['profile'] = False
  237. # Put build output in a separate dir, which depends on the current
  238. # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
  239. build_topdir = 'build'
  240. build_subdir = env['platform']
  241. if env['embedded']:
  242. build_subdir = 'embedded-' + build_subdir
  243. if env['machine'] != 'generic':
  244. build_subdir += '-' + env['machine']
  245. if env['build'] != 'release':
  246. build_subdir += '-' + env['build']
  247. build_dir = os.path.join(build_topdir, build_subdir)
  248. # Place the .sconsign file in the build dir too, to avoid issues with
  249. # different scons versions building the same source file
  250. env['build_dir'] = build_dir
  251. env.SConsignFile(os.path.join(build_dir, '.sconsign'))
  252. if 'SCONS_CACHE_DIR' in os.environ:
  253. print('scons: Using build cache in %s.' % (os.environ['SCONS_CACHE_DIR'],))
  254. env.CacheDir(os.environ['SCONS_CACHE_DIR'])
  255. env['CONFIGUREDIR'] = os.path.join(build_dir, 'conf')
  256. env['CONFIGURELOG'] = os.path.join(os.path.abspath(build_dir), 'config.log')
  257. # Parallel build
  258. if env.GetOption('num_jobs') <= 1:
  259. env.SetOption('num_jobs', num_jobs())
  260. # Speed up dependency checking. See
  261. # - https://github.com/SCons/scons/wiki/GoFastButton
  262. # - https://bugs.freedesktop.org/show_bug.cgi?id=109443
  263. # Scons version string has consistently been in this format:
  264. # MajorVersion.MinorVersion.Patch[.alpha/beta.yyyymmdd]
  265. # so this formula should cover all versions regardless of type
  266. # stable, alpha or beta.
  267. # For simplicity alpha and beta flags are removed.
  268. scons_version = distutils.version.StrictVersion('.'.join(SCons.__version__.split('.')[:3]))
  269. if scons_version < distutils.version.StrictVersion('3.0.2') or \
  270. scons_version > distutils.version.StrictVersion('3.0.4'):
  271. env.Decider('MD5-timestamp')
  272. env.SetOption('max_drift', 60)
  273. # C preprocessor options
  274. cppdefines = []
  275. cppdefines += [
  276. '__STDC_CONSTANT_MACROS',
  277. '__STDC_FORMAT_MACROS',
  278. '__STDC_LIMIT_MACROS',
  279. 'HAVE_SCONS',
  280. ]
  281. if env['build'] in ('debug', 'checked'):
  282. cppdefines += ['DEBUG']
  283. else:
  284. cppdefines += ['NDEBUG']
  285. if env['build'] == 'profile':
  286. cppdefines += ['PROFILE']
  287. if env['platform'] in ('posix', 'linux', 'freebsd', 'darwin'):
  288. cppdefines += [
  289. '_POSIX_SOURCE',
  290. ('_POSIX_C_SOURCE', '199309L'),
  291. '_SVID_SOURCE',
  292. '_BSD_SOURCE',
  293. '_GNU_SOURCE',
  294. '_DEFAULT_SOURCE',
  295. ]
  296. if env['platform'] == 'darwin':
  297. cppdefines += [
  298. '_DARWIN_C_SOURCE',
  299. 'GLX_USE_APPLEGL',
  300. 'GLX_DIRECT_RENDERING',
  301. 'BUILDING_MESA',
  302. ]
  303. else:
  304. cppdefines += [
  305. 'GLX_DIRECT_RENDERING',
  306. 'GLX_INDIRECT_RENDERING',
  307. ]
  308. if check_header(env, 'xlocale.h'):
  309. cppdefines += ['HAVE_XLOCALE_H']
  310. if check_header(env, 'endian.h'):
  311. cppdefines += ['HAVE_ENDIAN_H']
  312. if check_functions(env, ['strtod_l', 'strtof_l']):
  313. cppdefines += ['HAVE_STRTOD_L']
  314. if check_functions(env, ['random_r']):
  315. cppdefines += ['HAVE_RANDOM_R']
  316. if check_functions(env, ['timespec_get']):
  317. cppdefines += ['HAVE_TIMESPEC_GET']
  318. if check_header(env, 'sys/shm.h'):
  319. cppdefines += ['HAVE_SYS_SHM_H']
  320. if check_functions(env, ['strtok_r']):
  321. cppdefines += ['HAVE_STRTOK_R']
  322. #FIXME: we should really be checking for the major()/minor()
  323. # functions/macros in these headers, but check_functions()'s
  324. # SConf.CheckFunc() doesn't seem to support macros.
  325. if check_header(env, 'sys/mkdev.h'):
  326. cppdefines += ['MAJOR_IN_MKDEV']
  327. if check_header(env, 'sys/sysmacros.h'):
  328. cppdefines += ['MAJOR_IN_SYSMACROS']
  329. if platform == 'windows':
  330. cppdefines += [
  331. 'WIN32',
  332. '_WINDOWS',
  333. #'_UNICODE',
  334. #'UNICODE',
  335. # http://msdn.microsoft.com/en-us/library/aa383745.aspx
  336. ('_WIN32_WINNT', '0x0601'),
  337. ('WINVER', '0x0601'),
  338. ]
  339. if gcc_compat:
  340. cppdefines += [('__MSVCRT_VERSION__', '0x0700')]
  341. if msvc:
  342. cppdefines += [
  343. 'VC_EXTRALEAN',
  344. '_USE_MATH_DEFINES',
  345. '_CRT_SECURE_NO_WARNINGS',
  346. '_CRT_SECURE_NO_DEPRECATE',
  347. '_SCL_SECURE_NO_WARNINGS',
  348. '_SCL_SECURE_NO_DEPRECATE',
  349. '_ALLOW_KEYWORD_MACROS',
  350. '_HAS_EXCEPTIONS=0', # Tell C++ STL to not use exceptions
  351. ]
  352. if env['build'] in ('debug', 'checked'):
  353. cppdefines += ['_DEBUG']
  354. if env['embedded']:
  355. cppdefines += ['EMBEDDED_DEVICE']
  356. env.Append(CPPDEFINES = cppdefines)
  357. # C compiler options
  358. cflags = [] # C
  359. cxxflags = [] # C++
  360. ccflags = [] # C & C++
  361. if gcc_compat:
  362. if env['build'] == 'debug':
  363. ccflags += ['-O0']
  364. else:
  365. ccflags += ['-O3']
  366. if env['gcc']:
  367. # gcc's builtin memcmp is slower than glibc's
  368. # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
  369. ccflags += ['-fno-builtin-memcmp']
  370. # Work around aliasing bugs - developers should comment this out
  371. ccflags += ['-fno-strict-aliasing']
  372. ccflags += ['-g']
  373. if env['build'] in ('checked', 'profile') or env['asan']:
  374. # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
  375. ccflags += [
  376. '-fno-omit-frame-pointer',
  377. ]
  378. if env['gcc']:
  379. ccflags += ['-fno-optimize-sibling-calls']
  380. if env['machine'] == 'x86':
  381. ccflags += [
  382. '-m32',
  383. #'-march=pentium4',
  384. ]
  385. if platform != 'haiku':
  386. # NOTE: We need to ensure stack is realigned given that we
  387. # produce shared objects, and have no control over the stack
  388. # alignment policy of the application. Therefore we need
  389. # -mstackrealign ore -mincoming-stack-boundary=2.
  390. #
  391. # XXX: We could have SSE without -mstackrealign if we always used
  392. # __attribute__((force_align_arg_pointer)), but that's not
  393. # always the case.
  394. ccflags += [
  395. '-mstackrealign', # ensure stack is aligned
  396. '-msse', '-msse2', # enable SIMD intrinsics
  397. '-mfpmath=sse', # generate SSE floating-point arithmetic
  398. ]
  399. if platform in ['windows', 'darwin']:
  400. # Workaround http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37216
  401. ccflags += ['-fno-common']
  402. if platform in ['haiku']:
  403. # Make optimizations compatible with Pentium or higher on Haiku
  404. ccflags += [
  405. '-mstackrealign', # ensure stack is aligned
  406. '-march=i586', # Haiku target is Pentium
  407. '-mtune=i686' # use i686 where we can
  408. ]
  409. if env['machine'] == 'x86_64':
  410. ccflags += ['-m64']
  411. if platform == 'darwin':
  412. ccflags += ['-fno-common']
  413. if env['platform'] not in ('cygwin', 'haiku', 'windows'):
  414. ccflags += ['-fvisibility=hidden']
  415. # See also:
  416. # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  417. ccflags += [
  418. '-Wall',
  419. '-Wno-long-long',
  420. '-fmessage-length=0', # be nice to Eclipse
  421. ]
  422. cflags += [
  423. '-Werror=implicit-function-declaration',
  424. '-Werror=missing-prototypes',
  425. '-Werror=return-type',
  426. '-Werror=incompatible-pointer-types',
  427. ]
  428. if platform == 'darwin' and host_platform.mac_ver()[0] >= '10.15':
  429. cflags += ['-std=gnu11']
  430. else:
  431. cflags += ['-std=gnu99']
  432. if icc:
  433. cflags += [
  434. '-std=gnu99',
  435. ]
  436. if msvc:
  437. # See also:
  438. # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
  439. # - cl /?
  440. if env['build'] == 'debug':
  441. ccflags += [
  442. '/Od', # disable optimizations
  443. '/Oi', # enable intrinsic functions
  444. ]
  445. else:
  446. ccflags += [
  447. '/O2', # optimize for speed
  448. ]
  449. if env['build'] == 'release':
  450. if not env['clang']:
  451. ccflags += [
  452. '/GL', # enable whole program optimization
  453. ]
  454. else:
  455. ccflags += [
  456. '/Oy-', # disable frame pointer omission
  457. ]
  458. ccflags += [
  459. '/W3', # warning level
  460. '/wd4018', # signed/unsigned mismatch
  461. '/wd4056', # overflow in floating-point constant arithmetic
  462. '/wd4244', # conversion from 'type1' to 'type2', possible loss of data
  463. '/wd4267', # 'var' : conversion from 'size_t' to 'type', possible loss of data
  464. '/wd4305', # truncation from 'type1' to 'type2'
  465. '/wd4351', # new behavior: elements of array 'array' will be default initialized
  466. '/wd4756', # overflow in constant arithmetic
  467. '/wd4800', # forcing value to bool 'true' or 'false' (performance warning)
  468. '/wd4996', # disable deprecated POSIX name warnings
  469. ]
  470. if env['clang']:
  471. ccflags += [
  472. '-Wno-microsoft-enum-value', # enumerator value is not representable in underlying type 'int'
  473. ]
  474. if env['machine'] == 'x86':
  475. ccflags += [
  476. '/arch:SSE2', # use the SSE2 instructions (default since MSVC 2012)
  477. ]
  478. if platform == 'windows':
  479. ccflags += [
  480. # TODO
  481. ]
  482. # Automatic pdb generation
  483. # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
  484. env.EnsureSConsVersion(0, 98, 0)
  485. env['PDB'] = '${TARGET.base}.pdb'
  486. env.Append(CCFLAGS = ccflags)
  487. env.Append(CFLAGS = cflags)
  488. env.Append(CXXFLAGS = cxxflags)
  489. if env['platform'] == 'windows' and msvc:
  490. # Choose the appropriate MSVC CRT
  491. # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  492. if env['build'] in ('debug', 'checked'):
  493. env.Append(CCFLAGS = ['/MTd'])
  494. env.Append(SHCCFLAGS = ['/LDd'])
  495. else:
  496. env.Append(CCFLAGS = ['/MT'])
  497. env.Append(SHCCFLAGS = ['/LD'])
  498. # Static code analysis
  499. if env['analyze']:
  500. if env['msvc']:
  501. # http://msdn.microsoft.com/en-us/library/ms173498.aspx
  502. env.Append(CCFLAGS = [
  503. '/analyze',
  504. #'/analyze:log', '${TARGET.base}.xml',
  505. '/wd28251', # Inconsistent annotation for function
  506. ])
  507. if env['clang']:
  508. # scan-build will produce more comprehensive output
  509. env.Append(CCFLAGS = ['--analyze'])
  510. # https://github.com/google/sanitizers/wiki/AddressSanitizer
  511. if env['asan']:
  512. if gcc_compat:
  513. env.Append(CCFLAGS = [
  514. '-fsanitize=address',
  515. ])
  516. env.Append(LINKFLAGS = [
  517. '-fsanitize=address',
  518. ])
  519. # Assembler options
  520. if gcc_compat:
  521. if env['machine'] == 'x86':
  522. env.Append(ASFLAGS = ['-m32'])
  523. if env['machine'] == 'x86_64':
  524. env.Append(ASFLAGS = ['-m64'])
  525. # Linker options
  526. linkflags = []
  527. shlinkflags = []
  528. if gcc_compat:
  529. if env['machine'] == 'x86':
  530. linkflags += ['-m32']
  531. if env['machine'] == 'x86_64':
  532. linkflags += ['-m64']
  533. if env['platform'] not in ('darwin'):
  534. shlinkflags += [
  535. '-Wl,-Bsymbolic',
  536. ]
  537. # Handle circular dependencies in the libraries
  538. if env['platform'] in ('darwin'):
  539. pass
  540. else:
  541. env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
  542. if env['platform'] == 'windows':
  543. linkflags += [
  544. '-Wl,--nxcompat', # DEP
  545. '-Wl,--dynamicbase', # ASLR
  546. ]
  547. # Avoid depending on gcc runtime DLLs
  548. linkflags += ['-static-libgcc']
  549. if 'w64' in env['CC'].split('-'):
  550. linkflags += ['-static-libstdc++']
  551. # Handle the @xx symbol munging of DLL exports
  552. shlinkflags += ['-Wl,--enable-stdcall-fixup']
  553. #shlinkflags += ['-Wl,--kill-at']
  554. if msvc:
  555. if env['build'] == 'release' and not env['clang']:
  556. # enable Link-time Code Generation
  557. linkflags += ['/LTCG']
  558. env.Append(ARFLAGS = ['/LTCG'])
  559. if platform == 'windows' and msvc:
  560. # See also:
  561. # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
  562. linkflags += [
  563. '/fixed:no',
  564. '/incremental:no',
  565. '/dynamicbase', # ASLR
  566. '/nxcompat', # DEP
  567. ]
  568. env.Append(LINKFLAGS = linkflags)
  569. env.Append(SHLINKFLAGS = shlinkflags)
  570. # We have C++ in several libraries, so always link with the C++ compiler
  571. if gcc_compat:
  572. env['LINK'] = env['CXX']
  573. # Default libs
  574. libs = []
  575. if env['platform'] in ('darwin', 'freebsd', 'linux', 'posix', 'sunos'):
  576. libs += ['m', 'pthread', 'dl']
  577. if env['platform'] in ('linux',):
  578. libs += ['rt']
  579. if env['platform'] in ('haiku'):
  580. libs += ['root', 'be', 'network', 'translation']
  581. env.Append(LIBS = libs)
  582. # OpenMP
  583. if env['openmp']:
  584. if env['msvc']:
  585. env.Append(CCFLAGS = ['/openmp'])
  586. # When building openmp release VS2008 link.exe crashes with LNK1103 error.
  587. # Workaround: overwrite PDB flags with empty value as it isn't required anyways
  588. if env['build'] == 'release':
  589. env['PDB'] = ''
  590. if env['gcc']:
  591. env.Append(CCFLAGS = ['-fopenmp'])
  592. env.Append(LIBS = ['gomp'])
  593. # Load tools
  594. env.Tool('lex')
  595. if env['msvc']:
  596. env.Append(LEXFLAGS = [
  597. # Force flex to use const keyword in prototypes, as relies on
  598. # __cplusplus or __STDC__ macro to determine whether it's safe to
  599. # use const keyword, but MSVC never defines __STDC__ unless we
  600. # disable all MSVC extensions.
  601. '-DYY_USE_CONST=',
  602. ])
  603. # Flex relies on __STDC_VERSION__>=199901L to decide when to include
  604. # C99 inttypes.h. We always have inttypes.h available with MSVC
  605. # (either the one bundled with MSVC 2013, or the one we bundle
  606. # ourselves), but we can't just define __STDC_VERSION__ without
  607. # breaking stuff, as MSVC doesn't fully support C99. There's also no
  608. # way to premptively include stdint.
  609. env.Append(CCFLAGS = ['-FIinttypes.h'])
  610. if host_platform.system() == 'Windows':
  611. # Prefer winflexbison binaries, as not only they are easier to install
  612. # (no additional dependencies), but also better Windows support.
  613. if check_prog(env, 'win_flex'):
  614. env["LEX"] = 'win_flex'
  615. env.Append(LEXFLAGS = [
  616. # windows compatibility (uses <io.h> instead of <unistd.h> and
  617. # _isatty, _fileno functions)
  618. '--wincompat'
  619. ])
  620. env.Tool('yacc')
  621. if host_platform.system() == 'Windows':
  622. if check_prog(env, 'win_bison'):
  623. env["YACC"] = 'win_bison'
  624. if env['llvm']:
  625. env.Tool('llvm')
  626. # Custom builders and methods
  627. env.Tool('custom')
  628. env.AddMethod(install_program, 'InstallProgram')
  629. env.AddMethod(install_shared_library, 'InstallSharedLibrary')
  630. env.AddMethod(msvc2013_compat, 'MSVC2013Compat')
  631. env.AddMethod(unit_test, 'UnitTest')
  632. env.PkgCheckModules('X11', ['x11', 'xext', 'xdamage >= 1.1', 'xfixes', 'glproto >= 1.4.13', 'dri2proto >= 2.8'])
  633. env.PkgCheckModules('XCB', ['x11-xcb', 'xcb-glx >= 1.8.1', 'xcb-dri2 >= 1.8'])
  634. env.PkgCheckModules('XF86VIDMODE', ['xxf86vm'])
  635. env.PkgCheckModules('DRM', ['libdrm >= 2.4.75'])
  636. if not os.path.exists("src/util/format_srgb.c"):
  637. print("Checking for Python Mako module (>= 0.8.0)... ", end='')
  638. try:
  639. import mako
  640. except ImportError:
  641. print("no")
  642. exit(1)
  643. if distutils.version.StrictVersion(mako.__version__) < distutils.version.StrictVersion('0.8.0'):
  644. print("no")
  645. exit(1)
  646. print("yes")
  647. if env['x11']:
  648. env.Append(CPPPATH = env['X11_CPPPATH'])
  649. env['dri'] = env['x11'] and env['drm']
  650. # for debugging
  651. #print env.Dump()
  652. def exists(env):
  653. return 1