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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. """gallium
  2. Frontend-tool for Gallium3D architecture.
  3. """
  4. #
  5. # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
  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 TUNGSTEN GRAPHICS 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. import distutils.version
  29. import os
  30. import os.path
  31. import re
  32. import subprocess
  33. import platform as _platform
  34. import SCons.Action
  35. import SCons.Builder
  36. import SCons.Scanner
  37. def symlink(target, source, env):
  38. target = str(target[0])
  39. source = str(source[0])
  40. if os.path.islink(target) or os.path.exists(target):
  41. os.remove(target)
  42. os.symlink(os.path.basename(source), target)
  43. def install(env, source, subdir):
  44. target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'], subdir)
  45. return env.Install(target_dir, source)
  46. def install_program(env, source):
  47. return install(env, source, 'bin')
  48. def install_shared_library(env, sources, version = ()):
  49. targets = []
  50. install_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'])
  51. version = tuple(map(str, version))
  52. if env['SHLIBSUFFIX'] == '.dll':
  53. dlls = env.FindIxes(sources, 'SHLIBPREFIX', 'SHLIBSUFFIX')
  54. targets += install(env, dlls, 'bin')
  55. libs = env.FindIxes(sources, 'LIBPREFIX', 'LIBSUFFIX')
  56. targets += install(env, libs, 'lib')
  57. else:
  58. for source in sources:
  59. target_dir = os.path.join(install_dir, 'lib')
  60. target_name = '.'.join((str(source),) + version)
  61. last = env.InstallAs(os.path.join(target_dir, target_name), source)
  62. targets += last
  63. while len(version):
  64. version = version[:-1]
  65. target_name = '.'.join((str(source),) + version)
  66. action = SCons.Action.Action(symlink, " Symlinking $TARGET ...")
  67. last = env.Command(os.path.join(target_dir, target_name), last, action)
  68. targets += last
  69. return targets
  70. def createInstallMethods(env):
  71. env.AddMethod(install_program, 'InstallProgram')
  72. env.AddMethod(install_shared_library, 'InstallSharedLibrary')
  73. def num_jobs():
  74. try:
  75. return int(os.environ['NUMBER_OF_PROCESSORS'])
  76. except (ValueError, KeyError):
  77. pass
  78. try:
  79. return os.sysconf('SC_NPROCESSORS_ONLN')
  80. except (ValueError, OSError, AttributeError):
  81. pass
  82. try:
  83. return int(os.popen2("sysctl -n hw.ncpu")[1].read())
  84. except ValueError:
  85. pass
  86. return 1
  87. def generate(env):
  88. """Common environment generation code"""
  89. # Tell tools which machine to compile for
  90. env['TARGET_ARCH'] = env['machine']
  91. env['MSVS_ARCH'] = env['machine']
  92. # Toolchain
  93. platform = env['platform']
  94. env.Tool(env['toolchain'])
  95. # Allow override compiler and specify additional flags from environment
  96. if os.environ.has_key('CC'):
  97. env['CC'] = os.environ['CC']
  98. # Update CCVERSION to match
  99. pipe = SCons.Action._subproc(env, [env['CC'], '--version'],
  100. stdin = 'devnull',
  101. stderr = 'devnull',
  102. stdout = subprocess.PIPE)
  103. if pipe.wait() == 0:
  104. line = pipe.stdout.readline()
  105. match = re.search(r'[0-9]+(\.[0-9]+)+', line)
  106. if match:
  107. env['CCVERSION'] = match.group(0)
  108. if os.environ.has_key('CFLAGS'):
  109. env['CCFLAGS'] += SCons.Util.CLVar(os.environ['CFLAGS'])
  110. if os.environ.has_key('CXX'):
  111. env['CXX'] = os.environ['CXX']
  112. if os.environ.has_key('CXXFLAGS'):
  113. env['CXXFLAGS'] += SCons.Util.CLVar(os.environ['CXXFLAGS'])
  114. if os.environ.has_key('LDFLAGS'):
  115. env['LINKFLAGS'] += SCons.Util.CLVar(os.environ['LDFLAGS'])
  116. env['gcc'] = 'gcc' in os.path.basename(env['CC']).split('-')
  117. env['msvc'] = env['CC'] == 'cl'
  118. if env['msvc'] and env['toolchain'] == 'default' and env['machine'] == 'x86_64':
  119. # MSVC x64 support is broken in earlier versions of scons
  120. env.EnsurePythonVersion(2, 0)
  121. # shortcuts
  122. machine = env['machine']
  123. platform = env['platform']
  124. x86 = env['machine'] == 'x86'
  125. ppc = env['machine'] == 'ppc'
  126. gcc = env['gcc']
  127. msvc = env['msvc']
  128. # Determine whether we are cross compiling; in particular, whether we need
  129. # to compile code generators with a different compiler as the target code.
  130. host_platform = _platform.system().lower()
  131. if host_platform.startswith('cygwin'):
  132. host_platform = 'cygwin'
  133. host_machine = os.environ.get('PROCESSOR_ARCHITEW6432', os.environ.get('PROCESSOR_ARCHITECTURE', _platform.machine()))
  134. host_machine = {
  135. 'x86': 'x86',
  136. 'i386': 'x86',
  137. 'i486': 'x86',
  138. 'i586': 'x86',
  139. 'i686': 'x86',
  140. 'ppc' : 'ppc',
  141. 'AMD64': 'x86_64',
  142. 'x86_64': 'x86_64',
  143. }.get(host_machine, 'generic')
  144. env['crosscompile'] = platform != host_platform
  145. if machine == 'x86_64' and host_machine != 'x86_64':
  146. env['crosscompile'] = True
  147. env['hostonly'] = False
  148. # Backwards compatability with the debug= profile= options
  149. if env['build'] == 'debug':
  150. if not env['debug']:
  151. print 'scons: warning: debug option is deprecated and will be removed eventually; use instead'
  152. print
  153. print ' scons build=release'
  154. print
  155. env['build'] = 'release'
  156. if env['profile']:
  157. print 'scons: warning: profile option is deprecated and will be removed eventually; use instead'
  158. print
  159. print ' scons build=profile'
  160. print
  161. env['build'] = 'profile'
  162. if False:
  163. # Enforce SConscripts to use the new build variable
  164. env.popitem('debug')
  165. env.popitem('profile')
  166. else:
  167. # Backwards portability with older sconscripts
  168. if env['build'] in ('debug', 'checked'):
  169. env['debug'] = True
  170. env['profile'] = False
  171. if env['build'] == 'profile':
  172. env['debug'] = False
  173. env['profile'] = True
  174. if env['build'] == 'release':
  175. env['debug'] = False
  176. env['profile'] = False
  177. # Put build output in a separate dir, which depends on the current
  178. # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
  179. build_topdir = 'build'
  180. build_subdir = env['platform']
  181. if env['embedded']:
  182. build_subdir = 'embedded-' + build_subdir
  183. if env['machine'] != 'generic':
  184. build_subdir += '-' + env['machine']
  185. if env['build'] != 'release':
  186. build_subdir += '-' + env['build']
  187. build_dir = os.path.join(build_topdir, build_subdir)
  188. # Place the .sconsign file in the build dir too, to avoid issues with
  189. # different scons versions building the same source file
  190. env['build_dir'] = build_dir
  191. env.SConsignFile(os.path.join(build_dir, '.sconsign'))
  192. if 'SCONS_CACHE_DIR' in os.environ:
  193. print 'scons: Using build cache in %s.' % (os.environ['SCONS_CACHE_DIR'],)
  194. env.CacheDir(os.environ['SCONS_CACHE_DIR'])
  195. env['CONFIGUREDIR'] = os.path.join(build_dir, 'conf')
  196. env['CONFIGURELOG'] = os.path.join(os.path.abspath(build_dir), 'config.log')
  197. # Parallel build
  198. if env.GetOption('num_jobs') <= 1:
  199. env.SetOption('num_jobs', num_jobs())
  200. env.Decider('MD5-timestamp')
  201. env.SetOption('max_drift', 60)
  202. # C preprocessor options
  203. cppdefines = []
  204. if env['build'] in ('debug', 'checked'):
  205. cppdefines += ['DEBUG']
  206. else:
  207. cppdefines += ['NDEBUG']
  208. if env['build'] == 'profile':
  209. cppdefines += ['PROFILE']
  210. if env['platform'] in ('posix', 'linux', 'freebsd', 'darwin'):
  211. cppdefines += [
  212. '_POSIX_SOURCE',
  213. ('_POSIX_C_SOURCE', '199309L'),
  214. '_SVID_SOURCE',
  215. '_BSD_SOURCE',
  216. '_GNU_SOURCE',
  217. 'PTHREADS',
  218. 'HAVE_POSIX_MEMALIGN',
  219. ]
  220. if env['platform'] == 'darwin':
  221. cppdefines += [
  222. '_DARWIN_C_SOURCE',
  223. 'GLX_USE_APPLEGL',
  224. 'GLX_DIRECT_RENDERING',
  225. ]
  226. else:
  227. cppdefines += [
  228. 'GLX_DIRECT_RENDERING',
  229. 'GLX_INDIRECT_RENDERING',
  230. ]
  231. if env['platform'] in ('linux', 'freebsd'):
  232. cppdefines += ['HAVE_ALIAS']
  233. else:
  234. cppdefines += ['GLX_ALIAS_UNSUPPORTED']
  235. if platform == 'windows':
  236. cppdefines += [
  237. 'WIN32',
  238. '_WINDOWS',
  239. #'_UNICODE',
  240. #'UNICODE',
  241. # http://msdn.microsoft.com/en-us/library/aa383745.aspx
  242. ('_WIN32_WINNT', '0x0601'),
  243. ('WINVER', '0x0601'),
  244. ]
  245. if gcc:
  246. cppdefines += [('__MSVCRT_VERSION__', '0x0700')]
  247. if msvc:
  248. cppdefines += [
  249. 'VC_EXTRALEAN',
  250. '_USE_MATH_DEFINES',
  251. '_CRT_SECURE_NO_WARNINGS',
  252. '_CRT_SECURE_NO_DEPRECATE',
  253. '_SCL_SECURE_NO_WARNINGS',
  254. '_SCL_SECURE_NO_DEPRECATE',
  255. ]
  256. if env['build'] in ('debug', 'checked'):
  257. cppdefines += ['_DEBUG']
  258. if platform == 'windows':
  259. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
  260. if platform == 'haiku':
  261. cppdefines += ['BEOS_THREADS']
  262. if env['embedded']:
  263. cppdefines += ['PIPE_SUBSYSTEM_EMBEDDED']
  264. env.Append(CPPDEFINES = cppdefines)
  265. # C compiler options
  266. cflags = [] # C
  267. cxxflags = [] # C++
  268. ccflags = [] # C & C++
  269. if gcc:
  270. ccversion = env['CCVERSION']
  271. if env['build'] == 'debug':
  272. ccflags += ['-O0']
  273. elif ccversion.startswith('4.2.'):
  274. # gcc 4.2.x optimizer is broken
  275. print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
  276. ccflags += ['-O0']
  277. else:
  278. ccflags += ['-O3']
  279. # gcc's builtin memcmp is slower than glibc's
  280. # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
  281. ccflags += ['-fno-builtin-memcmp']
  282. # Work around aliasing bugs - developers should comment this out
  283. ccflags += ['-fno-strict-aliasing']
  284. ccflags += ['-g']
  285. if env['build'] in ('checked', 'profile'):
  286. # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
  287. ccflags += [
  288. '-fno-omit-frame-pointer',
  289. '-fno-optimize-sibling-calls',
  290. ]
  291. if env['machine'] == 'x86':
  292. ccflags += [
  293. '-m32',
  294. #'-march=pentium4',
  295. ]
  296. if distutils.version.LooseVersion(ccversion) >= distutils.version.LooseVersion('4.2') \
  297. and (platform != 'windows' or env['build'] == 'debug' or True):
  298. # NOTE: We need to ensure stack is realigned given that we
  299. # produce shared objects, and have no control over the stack
  300. # alignment policy of the application. Therefore we need
  301. # -mstackrealign ore -mincoming-stack-boundary=2.
  302. #
  303. # XXX: -O and -mstackrealign causes stack corruption on MinGW
  304. #
  305. # XXX: We could have SSE without -mstackrealign if we always used
  306. # __attribute__((force_align_arg_pointer)), but that's not
  307. # always the case.
  308. ccflags += [
  309. '-mstackrealign', # ensure stack is aligned
  310. '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
  311. #'-mfpmath=sse',
  312. ]
  313. if platform in ['windows', 'darwin']:
  314. # Workaround http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37216
  315. ccflags += ['-fno-common']
  316. if env['machine'] == 'x86_64':
  317. ccflags += ['-m64']
  318. if platform == 'darwin':
  319. ccflags += ['-fno-common']
  320. if env['platform'] not in ('windows', 'haiku'):
  321. ccflags += ['-fvisibility=hidden']
  322. # See also:
  323. # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  324. ccflags += [
  325. '-Wall',
  326. '-Wno-long-long',
  327. '-ffast-math',
  328. '-fmessage-length=0', # be nice to Eclipse
  329. ]
  330. cflags += [
  331. '-Wmissing-prototypes',
  332. '-std=gnu99',
  333. ]
  334. if distutils.version.LooseVersion(ccversion) >= distutils.version.LooseVersion('4.2'):
  335. ccflags += [
  336. '-Wpointer-arith',
  337. ]
  338. cflags += [
  339. '-Wdeclaration-after-statement',
  340. ]
  341. if msvc:
  342. # See also:
  343. # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
  344. # - cl /?
  345. if env['build'] == 'debug':
  346. ccflags += [
  347. '/Od', # disable optimizations
  348. '/Oi', # enable intrinsic functions
  349. '/Oy-', # disable frame pointer omission
  350. ]
  351. else:
  352. ccflags += [
  353. '/O2', # optimize for speed
  354. ]
  355. if env['build'] == 'release':
  356. ccflags += [
  357. '/GL', # enable whole program optimization
  358. ]
  359. else:
  360. ccflags += [
  361. '/GL-', # disable whole program optimization
  362. ]
  363. ccflags += [
  364. '/fp:fast', # fast floating point
  365. '/W3', # warning level
  366. #'/Wp64', # enable 64 bit porting warnings
  367. '/wd4996', # disable deprecated POSIX name warnings
  368. ]
  369. if env['machine'] == 'x86':
  370. ccflags += [
  371. #'/arch:SSE2', # use the SSE2 instructions
  372. ]
  373. if platform == 'windows':
  374. ccflags += [
  375. # TODO
  376. ]
  377. # Automatic pdb generation
  378. # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
  379. env.EnsureSConsVersion(0, 98, 0)
  380. env['PDB'] = '${TARGET.base}.pdb'
  381. env.Append(CCFLAGS = ccflags)
  382. env.Append(CFLAGS = cflags)
  383. env.Append(CXXFLAGS = cxxflags)
  384. if env['platform'] == 'windows' and msvc:
  385. # Choose the appropriate MSVC CRT
  386. # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  387. if env['build'] in ('debug', 'checked'):
  388. env.Append(CCFLAGS = ['/MTd'])
  389. env.Append(SHCCFLAGS = ['/LDd'])
  390. else:
  391. env.Append(CCFLAGS = ['/MT'])
  392. env.Append(SHCCFLAGS = ['/LD'])
  393. # Assembler options
  394. if gcc:
  395. if env['machine'] == 'x86':
  396. env.Append(ASFLAGS = ['-m32'])
  397. if env['machine'] == 'x86_64':
  398. env.Append(ASFLAGS = ['-m64'])
  399. # Linker options
  400. linkflags = []
  401. shlinkflags = []
  402. if gcc:
  403. if env['machine'] == 'x86':
  404. linkflags += ['-m32']
  405. if env['machine'] == 'x86_64':
  406. linkflags += ['-m64']
  407. if env['platform'] not in ('darwin'):
  408. shlinkflags += [
  409. '-Wl,-Bsymbolic',
  410. ]
  411. # Handle circular dependencies in the libraries
  412. if env['platform'] in ('darwin'):
  413. pass
  414. else:
  415. env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
  416. if env['platform'] == 'windows':
  417. # Avoid depending on gcc runtime DLLs
  418. linkflags += ['-static-libgcc']
  419. if 'w64' in env['CC'].split('-'):
  420. linkflags += ['-static-libstdc++']
  421. # Handle the @xx symbol munging of DLL exports
  422. shlinkflags += ['-Wl,--enable-stdcall-fixup']
  423. #shlinkflags += ['-Wl,--kill-at']
  424. if msvc:
  425. if env['build'] == 'release':
  426. # enable Link-time Code Generation
  427. linkflags += ['/LTCG']
  428. env.Append(ARFLAGS = ['/LTCG'])
  429. if platform == 'windows' and msvc:
  430. # See also:
  431. # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
  432. linkflags += [
  433. '/fixed:no',
  434. '/incremental:no',
  435. ]
  436. env.Append(LINKFLAGS = linkflags)
  437. env.Append(SHLINKFLAGS = shlinkflags)
  438. # We have C++ in several libraries, so always link with the C++ compiler
  439. if env['gcc']:
  440. env['LINK'] = env['CXX']
  441. # Default libs
  442. libs = []
  443. if env['platform'] in ('posix', 'linux', 'freebsd', 'darwin'):
  444. libs += ['m', 'pthread', 'dl']
  445. env.Append(LIBS = libs)
  446. # OpenMP
  447. if env['openmp']:
  448. if env['msvc']:
  449. env.Append(CCFLAGS = ['/openmp'])
  450. # When building openmp release VS2008 link.exe crashes with LNK1103 error.
  451. # Workaround: overwrite PDB flags with empty value as it isn't required anyways
  452. if env['build'] == 'release':
  453. env['PDB'] = ''
  454. if env['gcc']:
  455. env.Append(CCFLAGS = ['-fopenmp'])
  456. env.Append(LIBS = ['gomp'])
  457. # Load tools
  458. env.Tool('lex')
  459. env.Tool('yacc')
  460. if env['llvm']:
  461. env.Tool('llvm')
  462. # Custom builders and methods
  463. env.Tool('custom')
  464. createInstallMethods(env)
  465. env.PkgCheckModules('X11', ['x11', 'xext', 'xdamage', 'xfixes'])
  466. env.PkgCheckModules('XCB', ['x11-xcb', 'xcb-glx'])
  467. env.PkgCheckModules('XF86VIDMODE', ['xxf86vm'])
  468. env.PkgCheckModules('DRM', ['libdrm >= 2.4.24'])
  469. env.PkgCheckModules('DRM_INTEL', ['libdrm_intel >= 2.4.30'])
  470. env.PkgCheckModules('DRM_RADEON', ['libdrm_radeon >= 2.4.31'])
  471. env.PkgCheckModules('XORG', ['xorg-server >= 1.6.0'])
  472. env.PkgCheckModules('KMS', ['libkms >= 2.4.24'])
  473. env.PkgCheckModules('UDEV', ['libudev > 150'])
  474. env['dri'] = env['x11'] and env['drm']
  475. # for debugging
  476. #print env.Dump()
  477. def exists(env):
  478. return 1