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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. '_CRT_SECURE_NO_DEPRECATE',
  219. ]
  220. if debug:
  221. cppdefines += ['_DEBUG']
  222. if env['toolchain'] == 'winddk':
  223. # Mimic WINDDK's builtin flags. See also:
  224. # - WINDDK's bin/makefile.new i386mk.inc for more info.
  225. # - buildchk_wxp_x86.log files, generated by the WINDDK's build
  226. # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
  227. if machine == 'x86':
  228. cppdefines += ['_X86_', 'i386']
  229. if machine == 'x86_64':
  230. cppdefines += ['_AMD64_', 'AMD64']
  231. if platform == 'winddk':
  232. cppdefines += [
  233. 'STD_CALL',
  234. ('CONDITION_HANDLING', '1'),
  235. ('NT_INST', '0'),
  236. ('WIN32', '100'),
  237. ('_NT1X_', '100'),
  238. ('WINNT', '1'),
  239. ('_WIN32_WINNT', '0x0501'), # minimum required OS version
  240. ('WINVER', '0x0501'),
  241. ('_WIN32_IE', '0x0603'),
  242. ('WIN32_LEAN_AND_MEAN', '1'),
  243. ('DEVL', '1'),
  244. ('__BUILDMACHINE__', 'WinDDK'),
  245. ('FPO', '0'),
  246. ]
  247. if debug:
  248. cppdefines += [('DBG', 1)]
  249. if platform == 'wince':
  250. cppdefines += [
  251. '_CRT_SECURE_NO_DEPRECATE',
  252. '_USE_32BIT_TIME_T',
  253. 'UNICODE',
  254. '_UNICODE',
  255. ('UNDER_CE', '600'),
  256. ('_WIN32_WCE', '0x600'),
  257. 'WINCEOEM',
  258. 'WINCEINTERNAL',
  259. 'WIN32',
  260. 'STRICT',
  261. 'x86',
  262. '_X86_',
  263. 'INTERNATIONAL',
  264. ('INTLMSG_CODEPAGE', '1252'),
  265. ]
  266. if platform == 'windows':
  267. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
  268. if platform == 'winddk':
  269. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
  270. if platform == 'wince':
  271. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE']
  272. cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE_OGL']
  273. env.Append(CPPDEFINES = cppdefines)
  274. # C compiler options
  275. cflags = [] # C
  276. cxxflags = [] # C++
  277. ccflags = [] # C & C++
  278. if gcc:
  279. if debug:
  280. ccflags += ['-O0', '-g3']
  281. elif env['CCVERSION'].startswith('4.2.'):
  282. # gcc 4.2.x optimizer is broken
  283. print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
  284. ccflags += ['-O0', '-g3']
  285. else:
  286. ccflags += ['-O3', '-g3']
  287. if env['profile']:
  288. # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
  289. ccflags += [
  290. '-fno-omit-frame-pointer',
  291. '-fno-optimize-sibling-calls',
  292. ]
  293. if env['machine'] == 'x86':
  294. ccflags += [
  295. '-m32',
  296. #'-march=pentium4',
  297. '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
  298. '-mstackrealign', # ensure stack is aligned -- do not enabled -msse without it!
  299. #'-mfpmath=sse',
  300. ]
  301. if env['machine'] == 'x86_64':
  302. ccflags += ['-m64']
  303. # See also:
  304. # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  305. ccflags += [
  306. '-Wall',
  307. '-Wmissing-field-initializers',
  308. '-Wpointer-arith',
  309. '-Wno-long-long',
  310. '-ffast-math',
  311. '-fmessage-length=0', # be nice to Eclipse
  312. ]
  313. cflags += [
  314. '-Werror=declaration-after-statement',
  315. '-Wmissing-prototypes',
  316. '-std=gnu99',
  317. ]
  318. if msvc:
  319. # See also:
  320. # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
  321. # - cl /?
  322. if debug:
  323. ccflags += [
  324. '/Od', # disable optimizations
  325. '/Oi', # enable intrinsic functions
  326. '/Oy-', # disable frame pointer omission
  327. '/GL-', # disable whole program optimization
  328. ]
  329. else:
  330. ccflags += [
  331. '/O2', # optimize for speed
  332. #'/fp:fast', # fast floating point
  333. ]
  334. if env['profile']:
  335. ccflags += [
  336. '/Gh', # enable _penter hook function
  337. '/GH', # enable _pexit hook function
  338. ]
  339. ccflags += [
  340. '/W3', # warning level
  341. #'/Wp64', # enable 64 bit porting warnings
  342. ]
  343. if env['machine'] == 'x86':
  344. ccflags += [
  345. #'/QIfist', # Suppress _ftol
  346. #'/arch:SSE2', # use the SSE2 instructions
  347. ]
  348. if platform == 'windows':
  349. ccflags += [
  350. # TODO
  351. ]
  352. if platform == 'winddk':
  353. ccflags += [
  354. '/Zl', # omit default library name in .OBJ
  355. '/Zp8', # 8bytes struct member alignment
  356. '/Gy', # separate functions for linker
  357. '/Gm-', # disable minimal rebuild
  358. '/WX', # treat warnings as errors
  359. '/Gz', # __stdcall Calling convention
  360. '/GX-', # disable C++ EH
  361. '/GR-', # disable C++ RTTI
  362. '/GF', # enable read-only string pooling
  363. '/G6', # optimize for PPro, P-II, P-III
  364. '/Ze', # enable extensions
  365. '/Gi-', # disable incremental compilation
  366. '/QIfdiv-', # disable Pentium FDIV fix
  367. '/hotpatch', # prepares an image for hotpatching.
  368. #'/Z7', #enable old-style debug info
  369. ]
  370. if platform == 'wince':
  371. # See also C:\WINCE600\public\common\oak\misc\makefile.def
  372. ccflags += [
  373. '/Zl', # omit default library name in .OBJ
  374. '/GF', # enable read-only string pooling
  375. '/GR-', # disable C++ RTTI
  376. '/GS', # enable security checks
  377. # Allow disabling language conformance to maintain backward compat
  378. #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
  379. #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
  380. #'/wd4867',
  381. #'/wd4430',
  382. #'/MT',
  383. #'/U_MT',
  384. ]
  385. # Automatic pdb generation
  386. # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
  387. env.EnsureSConsVersion(0, 98, 0)
  388. env['PDB'] = '${TARGET.base}.pdb'
  389. env.Append(CCFLAGS = ccflags)
  390. env.Append(CFLAGS = cflags)
  391. env.Append(CXXFLAGS = cxxflags)
  392. if env['platform'] == 'windows' and msvc:
  393. # Choose the appropriate MSVC CRT
  394. # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  395. if env['debug']:
  396. env.Append(CCFLAGS = ['/MTd'])
  397. env.Append(SHCCFLAGS = ['/LDd'])
  398. else:
  399. env.Append(CCFLAGS = ['/MT'])
  400. env.Append(SHCCFLAGS = ['/LD'])
  401. # Assembler options
  402. if gcc:
  403. if env['machine'] == 'x86':
  404. env.Append(ASFLAGS = ['-m32'])
  405. if env['machine'] == 'x86_64':
  406. env.Append(ASFLAGS = ['-m64'])
  407. # Linker options
  408. linkflags = []
  409. shlinkflags = []
  410. if gcc:
  411. if env['machine'] == 'x86':
  412. linkflags += ['-m32']
  413. if env['machine'] == 'x86_64':
  414. linkflags += ['-m64']
  415. shlinkflags += [
  416. '-Wl,-Bsymbolic',
  417. ]
  418. # Handle circular dependencies in the libraries
  419. env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
  420. if platform == 'windows' and msvc:
  421. # See also:
  422. # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
  423. linkflags += [
  424. '/fixed:no',
  425. '/incremental:no',
  426. ]
  427. if platform == 'winddk':
  428. linkflags += [
  429. '/merge:_PAGE=PAGE',
  430. '/merge:_TEXT=.text',
  431. '/section:INIT,d',
  432. '/opt:ref',
  433. '/opt:icf',
  434. '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
  435. '/incremental:no',
  436. '/fullbuild',
  437. '/release',
  438. '/nodefaultlib',
  439. '/wx',
  440. '/debug',
  441. '/debugtype:cv',
  442. '/version:5.1',
  443. '/osversion:5.1',
  444. '/functionpadmin:5',
  445. '/safeseh',
  446. '/pdbcompress',
  447. '/stack:0x40000,0x1000',
  448. '/driver',
  449. '/align:0x80',
  450. '/subsystem:native,5.01',
  451. '/base:0x10000',
  452. '/entry:DrvEnableDriver',
  453. ]
  454. if env['debug'] or env['profile']:
  455. linkflags += [
  456. '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
  457. ]
  458. if platform == 'wince':
  459. linkflags += [
  460. '/nodefaultlib',
  461. #'/incremental:no',
  462. #'/fullbuild',
  463. '/entry:_DllMainCRTStartup',
  464. ]
  465. env.Append(LINKFLAGS = linkflags)
  466. env.Append(SHLINKFLAGS = shlinkflags)
  467. # Default libs
  468. env.Append(LIBS = [])
  469. # Custom builders and methods
  470. createConvenienceLibBuilder(env)
  471. createCodeGenerateMethod(env)
  472. createInstallMethods(env)
  473. # for debugging
  474. #print env.Dump()
  475. def exists(env):
  476. return 1