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

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