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

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