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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 os
  29. import os.path
  30. import re
  31. import SCons.Action
  32. import SCons.Builder
  33. import SCons.Scanner
  34. import fixes
  35. def quietCommandLines(env):
  36. # Quiet command lines
  37. # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
  38. env['ASCOMSTR'] = " Assembling $SOURCE ..."
  39. env['ASPPCOMSTR'] = " Assembling $SOURCE ..."
  40. env['CCCOMSTR'] = " Compiling $SOURCE ..."
  41. env['SHCCCOMSTR'] = " Compiling $SOURCE ..."
  42. env['CXXCOMSTR'] = " Compiling $SOURCE ..."
  43. env['SHCXXCOMSTR'] = " Compiling $SOURCE ..."
  44. env['ARCOMSTR'] = " Archiving $TARGET ..."
  45. env['RANLIBCOMSTR'] = " Indexing $TARGET ..."
  46. env['LINKCOMSTR'] = " Linking $TARGET ..."
  47. env['SHLINKCOMSTR'] = " Linking $TARGET ..."
  48. env['LDMODULECOMSTR'] = " Linking $TARGET ..."
  49. env['SWIGCOMSTR'] = " Generating $TARGET ..."
  50. def createConvenienceLibBuilder(env):
  51. """This is a utility function that creates the ConvenienceLibrary
  52. Builder in an Environment if it is not there already.
  53. If it is already there, we return the existing one.
  54. Based on the stock StaticLibrary and SharedLibrary builders.
  55. """
  56. try:
  57. convenience_lib = env['BUILDERS']['ConvenienceLibrary']
  58. except KeyError:
  59. action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
  60. if env.Detect('ranlib'):
  61. ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
  62. action_list.append(ranlib_action)
  63. convenience_lib = SCons.Builder.Builder(action = action_list,
  64. emitter = '$LIBEMITTER',
  65. prefix = '$LIBPREFIX',
  66. suffix = '$LIBSUFFIX',
  67. src_suffix = '$SHOBJSUFFIX',
  68. src_builder = 'SharedObject')
  69. env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
  70. return convenience_lib
  71. # TODO: handle import statements with multiple modules
  72. # TODO: handle from import statements
  73. import_re = re.compile(r'^import\s+(\S+)$', re.M)
  74. def python_scan(node, env, path):
  75. # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
  76. contents = node.get_contents()
  77. source_dir = node.get_dir()
  78. imports = import_re.findall(contents)
  79. results = []
  80. for imp in imports:
  81. for dir in path:
  82. file = os.path.join(str(dir), imp.replace('.', os.sep) + '.py')
  83. if os.path.exists(file):
  84. results.append(env.File(file))
  85. break
  86. file = os.path.join(str(dir), imp.replace('.', os.sep), '__init__.py')
  87. if os.path.exists(file):
  88. results.append(env.File(file))
  89. break
  90. return results
  91. python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
  92. def code_generate(env, script, target, source, command):
  93. """Method to simplify code generation via python scripts.
  94. http://www.scons.org/wiki/UsingCodeGenerators
  95. http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
  96. """
  97. # We're generating code using Python scripts, so we have to be
  98. # careful with our scons elements. This entry represents
  99. # the generator file *in the source directory*.
  100. script_src = env.File(script).srcnode()
  101. # This command creates generated code *in the build directory*.
  102. command = command.replace('$SCRIPT', script_src.path)
  103. code = env.Command(target, source, command)
  104. # Explicitly mark that the generated code depends on the generator,
  105. # and on implicitly imported python modules
  106. path = (script_src.get_dir(),)
  107. deps = [script_src]
  108. deps += script_src.get_implicit_deps(env, python_scanner, path)
  109. env.Depends(code, deps)
  110. # Running the Python script causes .pyc files to be generated in the
  111. # source directory. When we clean up, they should go too. So add side
  112. # effects for .pyc files
  113. for dep in deps:
  114. pyc = env.File(str(dep) + 'c')
  115. env.SideEffect(pyc, code)
  116. return code
  117. def createCodeGenerateMethod(env):
  118. env.Append(SCANNERS = python_scanner)
  119. env.AddMethod(code_generate, 'CodeGenerate')
  120. def symlink(target, source, env):
  121. target = str(target[0])
  122. source = str(source[0])
  123. if os.path.islink(target) or os.path.exists(target):
  124. os.remove(target)
  125. os.symlink(os.path.basename(source), target)
  126. def install_shared_library(env, source, version = ()):
  127. source = str(source[0])
  128. version = tuple(map(str, version))
  129. target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build'], 'lib')
  130. target_name = '.'.join((str(source),) + version)
  131. last = env.InstallAs(os.path.join(target_dir, target_name), source)
  132. while len(version):
  133. version = version[:-1]
  134. target_name = '.'.join((str(source),) + version)
  135. action = SCons.Action.Action(symlink, "$TARGET -> $SOURCE")
  136. last = env.Command(os.path.join(target_dir, target_name), last, action)
  137. def createInstallMethods(env):
  138. env.AddMethod(install_shared_library, 'InstallSharedLibrary')
  139. def num_jobs():
  140. try:
  141. return int(os.environ['NUMBER_OF_PROCESSORS'])
  142. except (ValueError, KeyError):
  143. pass
  144. try:
  145. return os.sysconf('SC_NPROCESSORS_ONLN')
  146. except (ValueError, OSError, AttributeError):
  147. pass
  148. try:
  149. return int(os.popen2("sysctl -n hw.ncpu")[1].read())
  150. except ValueError:
  151. pass
  152. return 1
  153. def generate(env):
  154. """Common environment generation code"""
  155. if env.get('quiet', True):
  156. quietCommandLines(env)
  157. # Toolchain
  158. platform = env['platform']
  159. if env['toolchain'] == 'default':
  160. if platform == 'winddk':
  161. env['toolchain'] = 'winddk'
  162. elif platform == 'wince':
  163. env['toolchain'] = 'wcesdk'
  164. env.Tool(env['toolchain'])
  165. env['gcc'] = 'gcc' in os.path.basename(env['CC']).split('-')
  166. env['msvc'] = env['CC'] == 'cl'
  167. # shortcuts
  168. debug = env['debug']
  169. machine = env['machine']
  170. platform = env['platform']
  171. x86 = env['machine'] == 'x86'
  172. ppc = env['machine'] == 'ppc'
  173. gcc = env['gcc']
  174. msvc = env['msvc']
  175. # Put build output in a separate dir, which depends on the current
  176. # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
  177. build_topdir = 'build'
  178. build_subdir = env['platform']
  179. if env['llvm']:
  180. build_subdir += "-llvm"
  181. if env['machine'] != 'generic':
  182. build_subdir += '-' + env['machine']
  183. if env['debug']:
  184. build_subdir += "-debug"
  185. if env['profile']:
  186. build_subdir += "-profile"
  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'] = build_dir
  191. env.SConsignFile(os.path.join(build_dir, '.sconsign'))
  192. env.CacheDir('build/cache')
  193. # Parallel build
  194. if env.GetOption('num_jobs') <= 1:
  195. env.SetOption('num_jobs', num_jobs())
  196. # C preprocessor options
  197. cppdefines = []
  198. if debug:
  199. cppdefines += ['DEBUG']
  200. else:
  201. cppdefines += ['NDEBUG']
  202. if env['profile']:
  203. cppdefines += ['PROFILE']
  204. if platform == 'windows':
  205. cppdefines += [
  206. 'WIN32',
  207. '_WINDOWS',
  208. #'_UNICODE',
  209. #'UNICODE',
  210. ('_WIN32_WINNT', '0x0501'), # minimum required OS version
  211. ('WINVER', '0x0501'),
  212. # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
  213. 'WIN32_LEAN_AND_MEAN',
  214. ]
  215. if msvc and env['toolchain'] != 'winddk':
  216. cppdefines += [
  217. 'VC_EXTRALEAN',
  218. '_USE_MATH_DEFINES',
  219. '_CRT_SECURE_NO_WARNINGS',
  220. '_CRT_SECURE_NO_DEPRECATE',
  221. '_SCL_SECURE_NO_WARNINGS',
  222. '_SCL_SECURE_NO_DEPRECATE',
  223. ]
  224. if debug:
  225. cppdefines += ['_DEBUG']
  226. if env['toolchain'] == 'winddk':
  227. # Mimic WINDDK's builtin flags. See also:
  228. # - WINDDK's bin/makefile.new i386mk.inc for more info.
  229. # - buildchk_wxp_x86.log files, generated by the WINDDK's build
  230. # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
  231. if machine == 'x86':
  232. cppdefines += ['_X86_', 'i386']
  233. if machine == 'x86_64':
  234. cppdefines += ['_AMD64_', 'AMD64']
  235. if platform == 'winddk':
  236. cppdefines += [
  237. 'STD_CALL',
  238. ('CONDITION_HANDLING', '1'),
  239. ('NT_INST', '0'),
  240. ('WIN32', '100'),
  241. ('_NT1X_', '100'),
  242. ('WINNT', '1'),
  243. ('_WIN32_WINNT', '0x0501'), # minimum required OS version
  244. ('WINVER', '0x0501'),
  245. ('_WIN32_IE', '0x0603'),
  246. ('WIN32_LEAN_AND_MEAN', '1'),
  247. ('DEVL', '1'),
  248. ('__BUILDMACHINE__', 'WinDDK'),
  249. ('FPO', '0'),
  250. ]
  251. if debug:
  252. cppdefines += [('DBG', 1)]
  253. if platform == 'wince':
  254. cppdefines += [
  255. '_CRT_SECURE_NO_DEPRECATE',
  256. '_USE_32BIT_TIME_T',
  257. 'UNICODE',
  258. '_UNICODE',
  259. ('UNDER_CE', '600'),
  260. ('_WIN32_WCE', '0x600'),
  261. 'WINCEOEM',
  262. 'WINCEINTERNAL',
  263. 'WIN32',
  264. 'STRICT',
  265. 'x86',
  266. '_X86_',
  267. 'INTERNATIONAL',
  268. ('INTLMSG_CODEPAGE', '1252'),
  269. ]
  270. if platform == 'windows':
  271. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
  272. if platform == 'winddk':
  273. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
  274. if platform == 'wince':
  275. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE']
  276. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE_OGL']
  277. env.Append(CPPDEFINES = cppdefines)
  278. # C compiler options
  279. cflags = [] # C
  280. cxxflags = [] # C++
  281. ccflags = [] # C & C++
  282. if gcc:
  283. if debug:
  284. ccflags += ['-O0', '-g3']
  285. elif env['CCVERSION'].startswith('4.2.'):
  286. # gcc 4.2.x optimizer is broken
  287. print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
  288. ccflags += ['-O0', '-g3']
  289. else:
  290. ccflags += ['-O3', '-g3']
  291. if env['profile']:
  292. # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
  293. ccflags += [
  294. '-fno-omit-frame-pointer',
  295. '-fno-optimize-sibling-calls',
  296. ]
  297. if env['machine'] == 'x86':
  298. ccflags += [
  299. '-m32',
  300. #'-march=pentium4',
  301. #'-mfpmath=sse',
  302. ]
  303. if platform != 'windows':
  304. # XXX: -mstackrealign causes stack corruption on MinGW. Ditto
  305. # for -mincoming-stack-boundary=2. Still enable it on other
  306. # platforms for now, but we can't rely on it for cross platform
  307. # code. We have to use __attribute__((force_align_arg_pointer))
  308. # instead.
  309. ccflags += [
  310. '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
  311. '-mstackrealign', # ensure stack is aligned
  312. ]
  313. if env['machine'] == 'x86_64':
  314. ccflags += ['-m64']
  315. # See also:
  316. # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  317. ccflags += [
  318. '-Wall',
  319. '-Wmissing-field-initializers',
  320. '-Wpointer-arith',
  321. '-Wno-long-long',
  322. '-ffast-math',
  323. '-fmessage-length=0', # be nice to Eclipse
  324. ]
  325. cflags += [
  326. '-Werror=declaration-after-statement',
  327. '-Wmissing-prototypes',
  328. '-std=gnu99',
  329. ]
  330. if msvc:
  331. # See also:
  332. # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
  333. # - cl /?
  334. if debug:
  335. ccflags += [
  336. '/Od', # disable optimizations
  337. '/Oi', # enable intrinsic functions
  338. '/Oy-', # disable frame pointer omission
  339. '/GL-', # disable whole program optimization
  340. ]
  341. else:
  342. ccflags += [
  343. '/O2', # optimize for speed
  344. #'/fp:fast', # fast floating point
  345. ]
  346. ccflags += [
  347. '/W3', # warning level
  348. #'/Wp64', # enable 64 bit porting warnings
  349. ]
  350. if env['machine'] == 'x86':
  351. ccflags += [
  352. #'/QIfist', # Suppress _ftol
  353. #'/arch:SSE2', # use the SSE2 instructions
  354. ]
  355. if platform == 'windows':
  356. ccflags += [
  357. # TODO
  358. ]
  359. if platform == 'winddk':
  360. ccflags += [
  361. '/Zl', # omit default library name in .OBJ
  362. '/Zp8', # 8bytes struct member alignment
  363. '/Gy', # separate functions for linker
  364. '/Gm-', # disable minimal rebuild
  365. '/WX', # treat warnings as errors
  366. '/Gz', # __stdcall Calling convention
  367. '/GX-', # disable C++ EH
  368. '/GR-', # disable C++ RTTI
  369. '/GF', # enable read-only string pooling
  370. '/G6', # optimize for PPro, P-II, P-III
  371. '/Ze', # enable extensions
  372. '/Gi-', # disable incremental compilation
  373. '/QIfdiv-', # disable Pentium FDIV fix
  374. '/hotpatch', # prepares an image for hotpatching.
  375. #'/Z7', #enable old-style debug info
  376. ]
  377. if platform == 'wince':
  378. # See also C:\WINCE600\public\common\oak\misc\makefile.def
  379. ccflags += [
  380. '/Zl', # omit default library name in .OBJ
  381. '/GF', # enable read-only string pooling
  382. '/GR-', # disable C++ RTTI
  383. '/GS', # enable security checks
  384. # Allow disabling language conformance to maintain backward compat
  385. #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
  386. #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
  387. #'/wd4867',
  388. #'/wd4430',
  389. #'/MT',
  390. #'/U_MT',
  391. ]
  392. # Automatic pdb generation
  393. # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
  394. env.EnsureSConsVersion(0, 98, 0)
  395. env['PDB'] = '${TARGET.base}.pdb'
  396. env.Append(CCFLAGS = ccflags)
  397. env.Append(CFLAGS = cflags)
  398. env.Append(CXXFLAGS = cxxflags)
  399. if env['platform'] == 'windows' and msvc:
  400. # Choose the appropriate MSVC CRT
  401. # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  402. if env['debug']:
  403. env.Append(CCFLAGS = ['/MTd'])
  404. env.Append(SHCCFLAGS = ['/LDd'])
  405. else:
  406. env.Append(CCFLAGS = ['/MT'])
  407. env.Append(SHCCFLAGS = ['/LD'])
  408. # Assembler options
  409. if gcc:
  410. if env['machine'] == 'x86':
  411. env.Append(ASFLAGS = ['-m32'])
  412. if env['machine'] == 'x86_64':
  413. env.Append(ASFLAGS = ['-m64'])
  414. # Linker options
  415. linkflags = []
  416. shlinkflags = []
  417. if gcc:
  418. if env['machine'] == 'x86':
  419. linkflags += ['-m32']
  420. if env['machine'] == 'x86_64':
  421. linkflags += ['-m64']
  422. shlinkflags += [
  423. '-Wl,-Bsymbolic',
  424. ]
  425. # Handle circular dependencies in the libraries
  426. env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
  427. if platform == 'windows' and msvc:
  428. # See also:
  429. # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
  430. linkflags += [
  431. '/fixed:no',
  432. '/incremental:no',
  433. ]
  434. if platform == 'winddk':
  435. linkflags += [
  436. '/merge:_PAGE=PAGE',
  437. '/merge:_TEXT=.text',
  438. '/section:INIT,d',
  439. '/opt:ref',
  440. '/opt:icf',
  441. '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
  442. '/incremental:no',
  443. '/fullbuild',
  444. '/release',
  445. '/nodefaultlib',
  446. '/wx',
  447. '/debug',
  448. '/debugtype:cv',
  449. '/version:5.1',
  450. '/osversion:5.1',
  451. '/functionpadmin:5',
  452. '/safeseh',
  453. '/pdbcompress',
  454. '/stack:0x40000,0x1000',
  455. '/driver',
  456. '/align:0x80',
  457. '/subsystem:native,5.01',
  458. '/base:0x10000',
  459. '/entry:DrvEnableDriver',
  460. ]
  461. if env['debug'] or env['profile']:
  462. linkflags += [
  463. '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
  464. ]
  465. if platform == 'wince':
  466. linkflags += [
  467. '/nodefaultlib',
  468. #'/incremental:no',
  469. #'/fullbuild',
  470. '/entry:_DllMainCRTStartup',
  471. ]
  472. env.Append(LINKFLAGS = linkflags)
  473. env.Append(SHLINKFLAGS = shlinkflags)
  474. # Default libs
  475. env.Append(LIBS = [])
  476. # Custom builders and methods
  477. createConvenienceLibBuilder(env)
  478. createCodeGenerateMethod(env)
  479. createInstallMethods(env)
  480. # for debugging
  481. #print env.Dump()
  482. def exists(env):
  483. return 1