Clone of mesa.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

custom.py 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. """custom
  2. Custom builders and methods.
  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 sys
  32. import subprocess
  33. import SCons.Action
  34. import SCons.Builder
  35. import SCons.Scanner
  36. import fixes
  37. import source_list
  38. def quietCommandLines(env):
  39. # Quiet command lines
  40. # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
  41. env['ASCOMSTR'] = " Assembling $SOURCE ..."
  42. env['ASPPCOMSTR'] = " Assembling $SOURCE ..."
  43. env['CCCOMSTR'] = " Compiling $SOURCE ..."
  44. env['SHCCCOMSTR'] = " Compiling $SOURCE ..."
  45. env['CXXCOMSTR'] = " Compiling $SOURCE ..."
  46. env['SHCXXCOMSTR'] = " Compiling $SOURCE ..."
  47. env['ARCOMSTR'] = " Archiving $TARGET ..."
  48. env['RANLIBCOMSTR'] = " Indexing $TARGET ..."
  49. env['LINKCOMSTR'] = " Linking $TARGET ..."
  50. env['SHLINKCOMSTR'] = " Linking $TARGET ..."
  51. env['LDMODULECOMSTR'] = " Linking $TARGET ..."
  52. env['SWIGCOMSTR'] = " Generating $TARGET ..."
  53. env['LEXCOMSTR'] = " Generating $TARGET ..."
  54. env['YACCCOMSTR'] = " Generating $TARGET ..."
  55. env['CODEGENCOMSTR'] = " Generating $TARGET ..."
  56. env['INSTALLSTR'] = " Installing $TARGET ..."
  57. def createConvenienceLibBuilder(env):
  58. """This is a utility function that creates the ConvenienceLibrary
  59. Builder in an Environment if it is not there already.
  60. If it is already there, we return the existing one.
  61. Based on the stock StaticLibrary and SharedLibrary builders.
  62. """
  63. try:
  64. convenience_lib = env['BUILDERS']['ConvenienceLibrary']
  65. except KeyError:
  66. action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
  67. if env.Detect('ranlib'):
  68. ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
  69. action_list.append(ranlib_action)
  70. convenience_lib = SCons.Builder.Builder(action = action_list,
  71. emitter = '$LIBEMITTER',
  72. prefix = '$LIBPREFIX',
  73. suffix = '$LIBSUFFIX',
  74. src_suffix = '$SHOBJSUFFIX',
  75. src_builder = 'SharedObject')
  76. env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
  77. return convenience_lib
  78. # TODO: handle import statements with multiple modules
  79. # TODO: handle from import statements
  80. import_re = re.compile(r'^import\s+(\S+)$', re.M)
  81. def python_scan(node, env, path):
  82. # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
  83. contents = node.get_contents()
  84. source_dir = node.get_dir()
  85. imports = import_re.findall(contents)
  86. results = []
  87. for imp in imports:
  88. for dir in path:
  89. file = os.path.join(str(dir), imp.replace('.', os.sep) + '.py')
  90. if os.path.exists(file):
  91. results.append(env.File(file))
  92. break
  93. file = os.path.join(str(dir), imp.replace('.', os.sep), '__init__.py')
  94. if os.path.exists(file):
  95. results.append(env.File(file))
  96. break
  97. return results
  98. python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
  99. def code_generate(env, script, target, source, command):
  100. """Method to simplify code generation via python scripts.
  101. http://www.scons.org/wiki/UsingCodeGenerators
  102. http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
  103. """
  104. # We're generating code using Python scripts, so we have to be
  105. # careful with our scons elements. This entry represents
  106. # the generator file *in the source directory*.
  107. script_src = env.File(script).srcnode()
  108. # This command creates generated code *in the build directory*.
  109. command = command.replace('$SCRIPT', script_src.path)
  110. action = SCons.Action.Action(command, "$CODEGENCOMSTR")
  111. code = env.Command(target, source, action)
  112. # Explicitly mark that the generated code depends on the generator,
  113. # and on implicitly imported python modules
  114. path = (script_src.get_dir(),)
  115. deps = [script_src]
  116. deps += script_src.get_implicit_deps(env, python_scanner, path)
  117. env.Depends(code, deps)
  118. # Running the Python script causes .pyc files to be generated in the
  119. # source directory. When we clean up, they should go too. So add side
  120. # effects for .pyc files
  121. for dep in deps:
  122. pyc = env.File(str(dep) + 'c')
  123. env.SideEffect(pyc, code)
  124. return code
  125. def createCodeGenerateMethod(env):
  126. env.Append(SCANNERS = python_scanner)
  127. env.AddMethod(code_generate, 'CodeGenerate')
  128. def _pkg_check_modules(env, name, modules):
  129. '''Simple wrapper for pkg-config.'''
  130. env['HAVE_' + name] = False
  131. # For backwards compatability
  132. env[name.lower()] = False
  133. if env['platform'] == 'windows':
  134. return
  135. if not env.Detect('pkg-config'):
  136. return
  137. if subprocess.call(["pkg-config", "--exists", ' '.join(modules)]) != 0:
  138. return
  139. # Other flags may affect the compilation of unrelated targets, so store
  140. # them with a prefix, (e.g., XXX_CFLAGS, XXX_LIBS, etc)
  141. try:
  142. flags = env.ParseFlags('!pkg-config --cflags --libs ' + ' '.join(modules))
  143. except OSError:
  144. return
  145. prefix = name + '_'
  146. for flag_name, flag_value in flags.iteritems():
  147. assert '_' not in flag_name
  148. env[prefix + flag_name] = flag_value
  149. env['HAVE_' + name] = True
  150. def pkg_check_modules(env, name, modules):
  151. sys.stdout.write('Checking for %s...' % name)
  152. _pkg_check_modules(env, name, modules)
  153. result = env['HAVE_' + name]
  154. sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
  155. # XXX: For backwards compatability
  156. env[name.lower()] = result
  157. def pkg_use_modules(env, names):
  158. '''Search for all environment flags that match NAME_FOO and append them to
  159. the FOO environment variable.'''
  160. names = env.Flatten(names)
  161. for name in names:
  162. prefix = name + '_'
  163. if not 'HAVE_' + name in env:
  164. raise Exception('Attempt to use unknown module %s' % name)
  165. if not env['HAVE_' + name]:
  166. raise Exception('Attempt to use unavailable module %s' % name)
  167. flags = {}
  168. for flag_name, flag_value in env.Dictionary().iteritems():
  169. if flag_name.startswith(prefix):
  170. flag_name = flag_name[len(prefix):]
  171. if '_' not in flag_name:
  172. flags[flag_name] = flag_value
  173. if flags:
  174. env.MergeFlags(flags)
  175. def createPkgConfigMethods(env):
  176. env.AddMethod(pkg_check_modules, 'PkgCheckModules')
  177. env.AddMethod(pkg_use_modules, 'PkgUseModules')
  178. def parse_source_list(env, filename, names=None):
  179. # parse the source list file
  180. parser = source_list.SourceListParser()
  181. src = env.File(filename).srcnode()
  182. sym_table = parser.parse(src.abspath)
  183. if names:
  184. if isinstance(names, basestring):
  185. names = [names]
  186. symbols = names
  187. else:
  188. symbols = sym_table.keys()
  189. # convert the symbol table to source lists
  190. src_lists = {}
  191. for sym in symbols:
  192. val = sym_table[sym]
  193. src_lists[sym] = [f for f in val.split(' ') if f]
  194. # if names are given, concatenate the lists
  195. if names:
  196. srcs = []
  197. for name in names:
  198. srcs.extend(src_lists[name])
  199. return srcs
  200. else:
  201. return src_lists
  202. def createParseSourceListMethod(env):
  203. env.AddMethod(parse_source_list, 'ParseSourceList')
  204. def generate(env):
  205. """Common environment generation code"""
  206. verbose = env.get('verbose', False) or not env.get('quiet', True)
  207. if not verbose:
  208. quietCommandLines(env)
  209. # Custom builders and methods
  210. createConvenienceLibBuilder(env)
  211. createCodeGenerateMethod(env)
  212. createPkgConfigMethods(env)
  213. createParseSourceListMethod(env)
  214. # for debugging
  215. #print env.Dump()
  216. def exists(env):
  217. return 1