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.

msvc_sa.py 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. """msvc_sa
  2. Tool-specific initialization for Microsoft Visual C/C++.
  3. Based on SCons.Tool.msvc, without the MSVS detection.
  4. """
  5. #
  6. # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 The SCons Foundation
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining
  9. # a 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, sublicense, 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 shall be included
  17. # in all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  20. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  21. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. #
  27. import os.path
  28. import re
  29. import string
  30. import SCons.Action
  31. import SCons.Builder
  32. import SCons.Errors
  33. import SCons.Platform.win32
  34. import SCons.Tool
  35. import SCons.Util
  36. import SCons.Warnings
  37. import SCons.Scanner.RC
  38. CSuffixes = ['.c', '.C']
  39. CXXSuffixes = ['.cc', '.cpp', '.cxx', '.c++', '.C++']
  40. def validate_vars(env):
  41. """Validate the PCH and PCHSTOP construction variables."""
  42. if env.has_key('PCH') and env['PCH']:
  43. if not env.has_key('PCHSTOP'):
  44. raise SCons.Errors.UserError, "The PCHSTOP construction must be defined if PCH is defined."
  45. if not SCons.Util.is_String(env['PCHSTOP']):
  46. raise SCons.Errors.UserError, "The PCHSTOP construction variable must be a string: %r"%env['PCHSTOP']
  47. def pch_emitter(target, source, env):
  48. """Adds the object file target."""
  49. validate_vars(env)
  50. pch = None
  51. obj = None
  52. for t in target:
  53. if SCons.Util.splitext(str(t))[1] == '.pch':
  54. pch = t
  55. if SCons.Util.splitext(str(t))[1] == '.obj':
  56. obj = t
  57. if not obj:
  58. obj = SCons.Util.splitext(str(pch))[0]+'.obj'
  59. target = [pch, obj] # pch must be first, and obj second for the PCHCOM to work
  60. return (target, source)
  61. def object_emitter(target, source, env, parent_emitter):
  62. """Sets up the PCH dependencies for an object file."""
  63. validate_vars(env)
  64. parent_emitter(target, source, env)
  65. if env.has_key('PCH') and env['PCH']:
  66. env.Depends(target, env['PCH'])
  67. return (target, source)
  68. def static_object_emitter(target, source, env):
  69. return object_emitter(target, source, env,
  70. SCons.Defaults.StaticObjectEmitter)
  71. def shared_object_emitter(target, source, env):
  72. return object_emitter(target, source, env,
  73. SCons.Defaults.SharedObjectEmitter)
  74. pch_action = SCons.Action.Action('$PCHCOM', '$PCHCOMSTR')
  75. pch_builder = SCons.Builder.Builder(action=pch_action, suffix='.pch',
  76. emitter=pch_emitter,
  77. source_scanner=SCons.Tool.SourceFileScanner)
  78. # Logic to build .rc files into .res files (resource files)
  79. res_scanner = SCons.Scanner.RC.RCScan()
  80. res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
  81. res_builder = SCons.Builder.Builder(action=res_action,
  82. src_suffix='.rc',
  83. suffix='.res',
  84. src_builder=[],
  85. source_scanner=res_scanner)
  86. def msvc_batch_key(action, env, target, source):
  87. """
  88. Returns a key to identify unique batches of sources for compilation.
  89. If batching is enabled (via the $MSVC_BATCH setting), then all
  90. target+source pairs that use the same action, defined by the same
  91. environment, and have the same target and source directories, will
  92. be batched.
  93. Returning None specifies that the specified target+source should not
  94. be batched with other compilations.
  95. """
  96. b = env.subst('$MSVC_BATCH')
  97. if b in (None, '', '0'):
  98. # We're not using batching; return no key.
  99. return None
  100. t = target[0]
  101. s = source[0]
  102. if os.path.splitext(t.name)[0] != os.path.splitext(s.name)[0]:
  103. # The base names are different, so this *must* be compiled
  104. # separately; return no key.
  105. return None
  106. return (id(action), id(env), t.dir, s.dir)
  107. def msvc_output_flag(target, source, env, for_signature):
  108. """
  109. Returns the correct /Fo flag for batching.
  110. If batching is disabled or there's only one source file, then we
  111. return an /Fo string that specifies the target explicitly. Otherwise,
  112. we return an /Fo string that just specifies the first target's
  113. directory (where the Visual C/C++ compiler will put the .obj files).
  114. """
  115. b = env.subst('$MSVC_BATCH')
  116. if b in (None, '', '0') or len(source) == 1:
  117. return '/Fo$TARGET'
  118. else:
  119. # The Visual C/C++ compiler requires a \ at the end of the /Fo
  120. # option to indicate an output directory. We use os.sep here so
  121. # that the test(s) for this can be run on non-Windows systems
  122. # without having a hard-coded backslash mess up command-line
  123. # argument parsing.
  124. return '/Fo${TARGET.dir}' + os.sep
  125. CAction = SCons.Action.Action("$CCCOM", "$CCCOMSTR",
  126. batch_key=msvc_batch_key,
  127. targets='$CHANGED_TARGETS')
  128. ShCAction = SCons.Action.Action("$SHCCCOM", "$SHCCCOMSTR",
  129. batch_key=msvc_batch_key,
  130. targets='$CHANGED_TARGETS')
  131. CXXAction = SCons.Action.Action("$CXXCOM", "$CXXCOMSTR",
  132. batch_key=msvc_batch_key,
  133. targets='$CHANGED_TARGETS')
  134. ShCXXAction = SCons.Action.Action("$SHCXXCOM", "$SHCXXCOMSTR",
  135. batch_key=msvc_batch_key,
  136. targets='$CHANGED_TARGETS')
  137. def generate(env):
  138. """Add Builders and construction variables for MSVC++ to an Environment."""
  139. static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
  140. # TODO(batch): shouldn't reach in to cmdgen this way; necessary
  141. # for now to bypass the checks in Builder.DictCmdGenerator.__call__()
  142. # and allow .cc and .cpp to be compiled in the same command line.
  143. static_obj.cmdgen.source_ext_match = False
  144. shared_obj.cmdgen.source_ext_match = False
  145. for suffix in CSuffixes:
  146. static_obj.add_action(suffix, CAction)
  147. shared_obj.add_action(suffix, ShCAction)
  148. static_obj.add_emitter(suffix, static_object_emitter)
  149. shared_obj.add_emitter(suffix, shared_object_emitter)
  150. for suffix in CXXSuffixes:
  151. static_obj.add_action(suffix, CXXAction)
  152. shared_obj.add_action(suffix, ShCXXAction)
  153. static_obj.add_emitter(suffix, static_object_emitter)
  154. shared_obj.add_emitter(suffix, shared_object_emitter)
  155. env['CCPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Z7") or ""}'])
  156. env['CCPCHFLAGS'] = SCons.Util.CLVar(['${(PCH and "/Yu%s /Fp%s"%(PCHSTOP or "",File(PCH))) or ""}'])
  157. env['_MSVC_OUTPUT_FLAG'] = msvc_output_flag
  158. env['_CCCOMCOM'] = '$CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS $CCPCHFLAGS $CCPDBFLAGS'
  159. env['CC'] = 'cl'
  160. env['CCFLAGS'] = SCons.Util.CLVar('/nologo')
  161. env['CFLAGS'] = SCons.Util.CLVar('')
  162. env['CCCOM'] = '$CC $_MSVC_OUTPUT_FLAG /c $CHANGED_SOURCES $CFLAGS $CCFLAGS $_CCCOMCOM'
  163. env['SHCC'] = '$CC'
  164. env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
  165. env['SHCFLAGS'] = SCons.Util.CLVar('$CFLAGS')
  166. env['SHCCCOM'] = '$SHCC $_MSVC_OUTPUT_FLAG /c $CHANGED_SOURCES $SHCFLAGS $SHCCFLAGS $_CCCOMCOM'
  167. env['CXX'] = '$CC'
  168. env['CXXFLAGS'] = SCons.Util.CLVar('$( /TP $)')
  169. env['CXXCOM'] = '$CXX $_MSVC_OUTPUT_FLAG /c $CHANGED_SOURCES $CXXFLAGS $CCFLAGS $_CCCOMCOM'
  170. env['SHCXX'] = '$CXX'
  171. env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
  172. env['SHCXXCOM'] = '$SHCXX $_MSVC_OUTPUT_FLAG /c $CHANGED_SOURCES $SHCXXFLAGS $SHCCFLAGS $_CCCOMCOM'
  173. env['CPPDEFPREFIX'] = '/D'
  174. env['CPPDEFSUFFIX'] = ''
  175. env['INCPREFIX'] = '/I'
  176. env['INCSUFFIX'] = ''
  177. # env.Append(OBJEMITTER = [static_object_emitter])
  178. # env.Append(SHOBJEMITTER = [shared_object_emitter])
  179. env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
  180. env['RC'] = 'rc'
  181. env['RCFLAGS'] = SCons.Util.CLVar('')
  182. env['RCSUFFIXES']=['.rc','.rc2']
  183. env['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS $RCFLAGS /fo$TARGET $SOURCES'
  184. env['BUILDERS']['RES'] = res_builder
  185. env['OBJPREFIX'] = ''
  186. env['OBJSUFFIX'] = '.obj'
  187. env['SHOBJPREFIX'] = '$OBJPREFIX'
  188. env['SHOBJSUFFIX'] = '$OBJSUFFIX'
  189. env['CFILESUFFIX'] = '.c'
  190. env['CXXFILESUFFIX'] = '.cc'
  191. env['PCHPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Yd") or ""}'])
  192. env['PCHCOM'] = '$CXX /Fo${TARGETS[1]} $CXXFLAGS $CCFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS /c $SOURCES /Yc$PCHSTOP /Fp${TARGETS[0]} $CCPDBFLAGS $PCHPDBFLAGS'
  193. env['BUILDERS']['PCH'] = pch_builder
  194. if not env.has_key('ENV'):
  195. env['ENV'] = {}
  196. if not env['ENV'].has_key('SystemRoot'): # required for dlls in the winsxs folders
  197. env['ENV']['SystemRoot'] = SCons.Platform.win32.get_system_root()
  198. def exists(env):
  199. return env.Detect('cl')
  200. # Local Variables:
  201. # tab-width:4
  202. # indent-tabs-mode:nil
  203. # End:
  204. # vim: set expandtab tabstop=4 shiftwidth=4: