Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

custom.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 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. env['CODEGENCOMSTR'] = " Generating $TARGET ..."
  51. def createConvenienceLibBuilder(env):
  52. """This is a utility function that creates the ConvenienceLibrary
  53. Builder in an Environment if it is not there already.
  54. If it is already there, we return the existing one.
  55. Based on the stock StaticLibrary and SharedLibrary builders.
  56. """
  57. try:
  58. convenience_lib = env['BUILDERS']['ConvenienceLibrary']
  59. except KeyError:
  60. action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
  61. if env.Detect('ranlib'):
  62. ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
  63. action_list.append(ranlib_action)
  64. convenience_lib = SCons.Builder.Builder(action = action_list,
  65. emitter = '$LIBEMITTER',
  66. prefix = '$LIBPREFIX',
  67. suffix = '$LIBSUFFIX',
  68. src_suffix = '$SHOBJSUFFIX',
  69. src_builder = 'SharedObject')
  70. env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
  71. return convenience_lib
  72. # TODO: handle import statements with multiple modules
  73. # TODO: handle from import statements
  74. import_re = re.compile(r'^import\s+(\S+)$', re.M)
  75. def python_scan(node, env, path):
  76. # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
  77. contents = node.get_contents()
  78. source_dir = node.get_dir()
  79. imports = import_re.findall(contents)
  80. results = []
  81. for imp in imports:
  82. for dir in path:
  83. file = os.path.join(str(dir), imp.replace('.', os.sep) + '.py')
  84. if os.path.exists(file):
  85. results.append(env.File(file))
  86. break
  87. file = os.path.join(str(dir), imp.replace('.', os.sep), '__init__.py')
  88. if os.path.exists(file):
  89. results.append(env.File(file))
  90. break
  91. return results
  92. python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
  93. def code_generate(env, script, target, source, command):
  94. """Method to simplify code generation via python scripts.
  95. http://www.scons.org/wiki/UsingCodeGenerators
  96. http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
  97. """
  98. # We're generating code using Python scripts, so we have to be
  99. # careful with our scons elements. This entry represents
  100. # the generator file *in the source directory*.
  101. script_src = env.File(script).srcnode()
  102. # This command creates generated code *in the build directory*.
  103. command = command.replace('$SCRIPT', script_src.path)
  104. action = SCons.Action.Action(command, "$CODEGENCOMSTR")
  105. code = env.Command(target, source, action)
  106. # Explicitly mark that the generated code depends on the generator,
  107. # and on implicitly imported python modules
  108. path = (script_src.get_dir(),)
  109. deps = [script_src]
  110. deps += script_src.get_implicit_deps(env, python_scanner, path)
  111. env.Depends(code, deps)
  112. # Running the Python script causes .pyc files to be generated in the
  113. # source directory. When we clean up, they should go too. So add side
  114. # effects for .pyc files
  115. for dep in deps:
  116. pyc = env.File(str(dep) + 'c')
  117. env.SideEffect(pyc, code)
  118. return code
  119. def createCodeGenerateMethod(env):
  120. env.Append(SCANNERS = python_scanner)
  121. env.AddMethod(code_generate, 'CodeGenerate')
  122. def generate(env):
  123. """Common environment generation code"""
  124. if env.get('quiet', True):
  125. quietCommandLines(env)
  126. # Custom builders and methods
  127. createConvenienceLibBuilder(env)
  128. createCodeGenerateMethod(env)
  129. # for debugging
  130. #print env.Dump()
  131. def exists(env):
  132. return 1