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

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