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.

182 lines
6.5 KiB

6 years ago
  1. """SCons.Tool.gcc
  2. Tool-specific initialization for MinGW (http://www.mingw.org/)
  3. There normally shouldn't be any need to import this module directly.
  4. It will usually be imported through the generic SCons.Tool.Tool()
  5. selection method.
  6. """
  7. #
  8. # Copyright (c) 2001 - 2017 The SCons Foundation
  9. #
  10. # Permission is hereby granted, free of charge, to any person obtaining
  11. # a copy of this software and associated documentation files (the
  12. # "Software"), to deal in the Software without restriction, including
  13. # without limitation the rights to use, copy, modify, merge, publish,
  14. # distribute, sublicense, and/or sell copies of the Software, and to
  15. # permit persons to whom the Software is furnished to do so, subject to
  16. # the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included
  19. # in all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  22. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  23. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. #
  29. __revision__ = "src/engine/SCons/Tool/mingw.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  30. import os
  31. import os.path
  32. import SCons.Action
  33. import SCons.Builder
  34. import SCons.Defaults
  35. import SCons.Tool
  36. import SCons.Util
  37. # This is what we search for to find mingw:
  38. key_program = 'mingw32-gcc'
  39. def find(env):
  40. # First search in the SCons path
  41. path=env.WhereIs(key_program)
  42. if (path):
  43. return path
  44. # then the OS path:
  45. path=SCons.Util.WhereIs(key_program)
  46. if (path):
  47. return path
  48. # If that doesn't work try default location for mingw
  49. save_path=env['ENV']['PATH']
  50. env.AppendENVPath('PATH',r'c:\MinGW\bin')
  51. path =env.WhereIs(key_program)
  52. if not path:
  53. env['ENV']['PATH']=save_path
  54. return path
  55. def shlib_generator(target, source, env, for_signature):
  56. cmd = SCons.Util.CLVar(['$SHLINK', '$SHLINKFLAGS'])
  57. dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
  58. if dll: cmd.extend(['-o', dll])
  59. cmd.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS'])
  60. implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
  61. if implib: cmd.append('-Wl,--out-implib,'+implib.get_string(for_signature))
  62. def_target = env.FindIxes(target, 'WINDOWSDEFPREFIX', 'WINDOWSDEFSUFFIX')
  63. insert_def = env.subst("$WINDOWS_INSERT_DEF")
  64. if not insert_def in ['', '0', 0] and def_target: \
  65. cmd.append('-Wl,--output-def,'+def_target.get_string(for_signature))
  66. return [cmd]
  67. def shlib_emitter(target, source, env):
  68. dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
  69. no_import_lib = env.get('no_import_lib', 0)
  70. if not dll:
  71. raise SCons.Errors.UserError("A shared library should have exactly one target with the suffix: %s Target(s) are:%s" % \
  72. (env.subst("$SHLIBSUFFIX"), ",".join([str(t) for t in target])))
  73. if not no_import_lib and \
  74. not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'):
  75. # Create list of target libraries as strings
  76. targetStrings=env.ReplaceIxes(dll,
  77. 'SHLIBPREFIX', 'SHLIBSUFFIX',
  78. 'LIBPREFIX', 'LIBSUFFIX')
  79. # Now add file nodes to target list
  80. target.append(env.fs.File(targetStrings))
  81. # Append a def file target if there isn't already a def file target
  82. # or a def file source or the user has explicitly asked for the target
  83. # to be emitted.
  84. def_source = env.FindIxes(source, 'WINDOWSDEFPREFIX', 'WINDOWSDEFSUFFIX')
  85. def_target = env.FindIxes(target, 'WINDOWSDEFPREFIX', 'WINDOWSDEFSUFFIX')
  86. skip_def_insert = env.subst("$WINDOWS_INSERT_DEF") in ['', '0', 0]
  87. if not def_source and not def_target and not skip_def_insert:
  88. # Create list of target libraries and def files as strings
  89. targetStrings=env.ReplaceIxes(dll,
  90. 'SHLIBPREFIX', 'SHLIBSUFFIX',
  91. 'WINDOWSDEFPREFIX', 'WINDOWSDEFSUFFIX')
  92. # Now add file nodes to target list
  93. target.append(env.fs.File(targetStrings))
  94. return (target, source)
  95. shlib_action = SCons.Action.Action(shlib_generator, generator=1)
  96. res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
  97. res_builder = SCons.Builder.Builder(action=res_action, suffix='.o',
  98. source_scanner=SCons.Tool.SourceFileScanner)
  99. SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
  100. def generate(env):
  101. mingw = find(env)
  102. if mingw:
  103. dir = os.path.dirname(mingw)
  104. env.PrependENVPath('PATH', dir )
  105. # Most of mingw is the same as gcc and friends...
  106. gnu_tools = ['gcc', 'g++', 'gnulink', 'ar', 'gas', 'gfortran', 'm4']
  107. for tool in gnu_tools:
  108. SCons.Tool.Tool(tool)(env)
  109. #... but a few things differ:
  110. env['CC'] = 'gcc'
  111. env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
  112. env['CXX'] = 'g++'
  113. env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
  114. env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared')
  115. env['SHLINKCOM'] = shlib_action
  116. env['LDMODULECOM'] = shlib_action
  117. env.Append(SHLIBEMITTER = [shlib_emitter])
  118. env.Append(LDMODULEEMITTER = [shlib_emitter])
  119. env['AS'] = 'as'
  120. env['WIN32DEFPREFIX'] = ''
  121. env['WIN32DEFSUFFIX'] = '.def'
  122. env['WINDOWSDEFPREFIX'] = '${WIN32DEFPREFIX}'
  123. env['WINDOWSDEFSUFFIX'] = '${WIN32DEFSUFFIX}'
  124. env['SHOBJSUFFIX'] = '.o'
  125. env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
  126. env['RC'] = 'windres'
  127. env['RCFLAGS'] = SCons.Util.CLVar('')
  128. env['RCINCFLAGS'] = '$( ${_concat(RCINCPREFIX, CPPPATH, RCINCSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
  129. env['RCINCPREFIX'] = '--include-dir '
  130. env['RCINCSUFFIX'] = ''
  131. env['RCCOM'] = '$RC $_CPPDEFFLAGS $RCINCFLAGS ${RCINCPREFIX} ${SOURCE.dir} $RCFLAGS -i $SOURCE -o $TARGET'
  132. env['BUILDERS']['RES'] = res_builder
  133. # Some setting from the platform also have to be overridden:
  134. env['OBJSUFFIX'] = '.o'
  135. env['LIBPREFIX'] = 'lib'
  136. env['LIBSUFFIX'] = '.a'
  137. env['PROGSUFFIX'] = '.exe'
  138. def exists(env):
  139. return find(env)
  140. # Local Variables:
  141. # tab-width:4
  142. # indent-tabs-mode:nil
  143. # End:
  144. # vim: set expandtab tabstop=4 shiftwidth=4: