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.

331 lines
13 KiB

6 years ago
  1. """SCons.Tool.link
  2. Tool-specific initialization for the generic Posix linker.
  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. from __future__ import print_function
  30. __revision__ = "src/engine/SCons/Tool/link.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  31. import sys
  32. import re
  33. import os
  34. import SCons.Tool
  35. import SCons.Util
  36. import SCons.Warnings
  37. from SCons.Tool.FortranCommon import isfortran
  38. from SCons.Tool.DCommon import isD
  39. import SCons.Tool.cxx
  40. cplusplus = SCons.Tool.cxx
  41. # cplusplus = __import__(__package__+'.cxx', globals(), locals(), ['*'])
  42. issued_mixed_link_warning = False
  43. def smart_link(source, target, env, for_signature):
  44. has_cplusplus = cplusplus.iscplusplus(source)
  45. has_fortran = isfortran(env, source)
  46. has_d = isD(env, source)
  47. if has_cplusplus and has_fortran and not has_d:
  48. global issued_mixed_link_warning
  49. if not issued_mixed_link_warning:
  50. msg = "Using $CXX to link Fortran and C++ code together.\n\t" + \
  51. "This may generate a buggy executable if the '%s'\n\t" + \
  52. "compiler does not know how to deal with Fortran runtimes."
  53. SCons.Warnings.warn(SCons.Warnings.FortranCxxMixWarning,
  54. msg % env.subst('$CXX'))
  55. issued_mixed_link_warning = True
  56. return '$CXX'
  57. elif has_d:
  58. env['LINKCOM'] = env['DLINKCOM']
  59. env['SHLINKCOM'] = env['SHDLINKCOM']
  60. return '$DC'
  61. elif has_fortran:
  62. return '$FORTRAN'
  63. elif has_cplusplus:
  64. return '$CXX'
  65. return '$CC'
  66. def _lib_emitter(target, source, env, **kw):
  67. Verbose = False
  68. if Verbose:
  69. print("_lib_emitter: target[0]={:r}".format(target[0].get_path()))
  70. for tgt in target:
  71. tgt.attributes.shared = 1
  72. try:
  73. symlink_generator = kw['symlink_generator']
  74. except KeyError:
  75. pass
  76. else:
  77. if Verbose:
  78. print("_lib_emitter: symlink_generator={:r}".format(symlink_generator))
  79. symlinks = symlink_generator(env, target[0])
  80. if Verbose:
  81. print("_lib_emitter: symlinks={:r}".format(symlinks))
  82. if symlinks:
  83. SCons.Tool.EmitLibSymlinks(env, symlinks, target[0])
  84. target[0].attributes.shliblinks = symlinks
  85. return (target, source)
  86. def shlib_emitter(target, source, env):
  87. return _lib_emitter(target, source, env, symlink_generator = SCons.Tool.ShLibSymlinkGenerator)
  88. def ldmod_emitter(target, source, env):
  89. return _lib_emitter(target, source, env, symlink_generator = SCons.Tool.LdModSymlinkGenerator)
  90. # This is generic enough to be included here...
  91. def _versioned_lib_name(env, libnode, version, prefix, suffix, prefix_generator, suffix_generator, **kw):
  92. """For libnode='/optional/dir/libfoo.so.X.Y.Z' it returns 'libfoo.so'"""
  93. Verbose = False
  94. if Verbose:
  95. print("_versioned_lib_name: libnode={:r}".format(libnode.get_path()))
  96. print("_versioned_lib_name: version={:r}".format(version))
  97. print("_versioned_lib_name: prefix={:r}".format(prefix))
  98. print("_versioned_lib_name: suffix={:r}".format(suffix))
  99. print("_versioned_lib_name: suffix_generator={:r}".format(suffix_generator))
  100. versioned_name = os.path.basename(libnode.get_path())
  101. if Verbose:
  102. print("_versioned_lib_name: versioned_name={:r}".format(versioned_name))
  103. versioned_prefix = prefix_generator(env, **kw)
  104. versioned_suffix = suffix_generator(env, **kw)
  105. if Verbose:
  106. print("_versioned_lib_name: versioned_prefix={:r}".format(versioned_prefix))
  107. print("_versioned_lib_name: versioned_suffix={:r}".format(versioned_suffix))
  108. versioned_prefix_re = '^' + re.escape(versioned_prefix)
  109. versioned_suffix_re = re.escape(versioned_suffix) + '$'
  110. name = re.sub(versioned_prefix_re, prefix, versioned_name)
  111. name = re.sub(versioned_suffix_re, suffix, name)
  112. if Verbose:
  113. print("_versioned_lib_name: name={:r}".format(name))
  114. return name
  115. def _versioned_shlib_name(env, libnode, version, prefix, suffix, **kw):
  116. pg = SCons.Tool.ShLibPrefixGenerator
  117. sg = SCons.Tool.ShLibSuffixGenerator
  118. return _versioned_lib_name(env, libnode, version, prefix, suffix, pg, sg, **kw)
  119. def _versioned_ldmod_name(env, libnode, version, prefix, suffix, **kw):
  120. pg = SCons.Tool.LdModPrefixGenerator
  121. sg = SCons.Tool.LdModSuffixGenerator
  122. return _versioned_lib_name(env, libnode, version, prefix, suffix, pg, sg, **kw)
  123. def _versioned_lib_suffix(env, suffix, version):
  124. """For suffix='.so' and version='0.1.2' it returns '.so.0.1.2'"""
  125. Verbose = False
  126. if Verbose:
  127. print("_versioned_lib_suffix: suffix={:r}".format(suffix))
  128. print("_versioned_lib_suffix: version={:r}".format(version))
  129. if not suffix.endswith(version):
  130. suffix = suffix + '.' + version
  131. if Verbose:
  132. print("_versioned_lib_suffix: return suffix={:r}".format(suffix))
  133. return suffix
  134. def _versioned_lib_soname(env, libnode, version, prefix, suffix, name_func):
  135. """For libnode='/optional/dir/libfoo.so.X.Y.Z' it returns 'libfoo.so.X'"""
  136. Verbose = False
  137. if Verbose:
  138. print("_versioned_lib_soname: version={:r}".format(version))
  139. name = name_func(env, libnode, version, prefix, suffix)
  140. if Verbose:
  141. print("_versioned_lib_soname: name={:r}".format(name))
  142. major = version.split('.')[0]
  143. soname = name + '.' + major
  144. if Verbose:
  145. print("_versioned_lib_soname: soname={:r}".format(soname))
  146. return soname
  147. def _versioned_shlib_soname(env, libnode, version, prefix, suffix):
  148. return _versioned_lib_soname(env, libnode, version, prefix, suffix, _versioned_shlib_name)
  149. def _versioned_ldmod_soname(env, libnode, version, prefix, suffix):
  150. return _versioned_lib_soname(env, libnode, version, prefix, suffix, _versioned_ldmod_name)
  151. def _versioned_lib_symlinks(env, libnode, version, prefix, suffix, name_func, soname_func):
  152. """Generate link names that should be created for a versioned shared lirbrary.
  153. Returns a dictionary in the form { linkname : linktarget }
  154. """
  155. Verbose = False
  156. if Verbose:
  157. print("_versioned_lib_symlinks: libnode={:r}".format(libnode.get_path()))
  158. print("_versioned_lib_symlinks: version={:r}".format(version))
  159. if sys.platform.startswith('openbsd'):
  160. # OpenBSD uses x.y shared library versioning numbering convention
  161. # and doesn't use symlinks to backwards-compatible libraries
  162. if Verbose:
  163. print("_versioned_lib_symlinks: return symlinks={:r}".format(None))
  164. return None
  165. linkdir = libnode.get_dir()
  166. if Verbose:
  167. print("_versioned_lib_symlinks: linkdir={:r}".format(linkdir.get_path()))
  168. name = name_func(env, libnode, version, prefix, suffix)
  169. if Verbose:
  170. print("_versioned_lib_symlinks: name={:r}".format(name))
  171. soname = soname_func(env, libnode, version, prefix, suffix)
  172. link0 = env.fs.File(soname, linkdir)
  173. link1 = env.fs.File(name, linkdir)
  174. # We create direct symlinks, not daisy-chained.
  175. if link0 == libnode:
  176. # This enables SHLIBVERSION without periods (e.g. SHLIBVERSION=1)
  177. symlinks = [ (link1, libnode) ]
  178. else:
  179. # This handles usual SHLIBVERSION, i.e. '1.2', '1.2.3', etc.
  180. symlinks = [ (link0, libnode), (link1, libnode) ]
  181. if Verbose:
  182. print("_versioned_lib_symlinks: return symlinks={:r}".format(SCons.Tool.StringizeLibSymlinks(symlinks)))
  183. return symlinks
  184. def _versioned_shlib_symlinks(env, libnode, version, prefix, suffix):
  185. nf = _versioned_shlib_name
  186. sf = _versioned_shlib_soname
  187. return _versioned_lib_symlinks(env, libnode, version, prefix, suffix, nf, sf)
  188. def _versioned_ldmod_symlinks(env, libnode, version, prefix, suffix):
  189. nf = _versioned_ldmod_name
  190. sf = _versioned_ldmod_soname
  191. return _versioned_lib_symlinks(env, libnode, version, prefix, suffix, nf, sf)
  192. def _versioned_lib_callbacks():
  193. return {
  194. 'VersionedShLibSuffix' : _versioned_lib_suffix,
  195. 'VersionedLdModSuffix' : _versioned_lib_suffix,
  196. 'VersionedShLibSymlinks' : _versioned_shlib_symlinks,
  197. 'VersionedLdModSymlinks' : _versioned_ldmod_symlinks,
  198. 'VersionedShLibName' : _versioned_shlib_name,
  199. 'VersionedLdModName' : _versioned_ldmod_name,
  200. 'VersionedShLibSoname' : _versioned_shlib_soname,
  201. 'VersionedLdModSoname' : _versioned_ldmod_soname,
  202. }.copy()
  203. def _setup_versioned_lib_variables(env, **kw):
  204. """
  205. Setup all variables required by the versioning machinery
  206. """
  207. tool = None
  208. try: tool = kw['tool']
  209. except KeyError: pass
  210. use_soname = False
  211. try: use_soname = kw['use_soname']
  212. except KeyError: pass
  213. # The $_SHLIBVERSIONFLAGS define extra commandline flags used when
  214. # building VERSIONED shared libraries. It's always set, but used only
  215. # when VERSIONED library is built (see __SHLIBVERSIONFLAGS in SCons/Defaults.py).
  216. if use_soname:
  217. # If the linker uses SONAME, then we need this little automata
  218. if tool == 'sunlink':
  219. env['_SHLIBVERSIONFLAGS'] = '$SHLIBVERSIONFLAGS -h $_SHLIBSONAME'
  220. env['_LDMODULEVERSIONFLAGS'] = '$LDMODULEVERSIONFLAGS -h $_LDMODULESONAME'
  221. else:
  222. env['_SHLIBVERSIONFLAGS'] = '$SHLIBVERSIONFLAGS -Wl,-soname=$_SHLIBSONAME'
  223. env['_LDMODULEVERSIONFLAGS'] = '$LDMODULEVERSIONFLAGS -Wl,-soname=$_LDMODULESONAME'
  224. env['_SHLIBSONAME'] = '${ShLibSonameGenerator(__env__,TARGET)}'
  225. env['_LDMODULESONAME'] = '${LdModSonameGenerator(__env__,TARGET)}'
  226. env['ShLibSonameGenerator'] = SCons.Tool.ShLibSonameGenerator
  227. env['LdModSonameGenerator'] = SCons.Tool.LdModSonameGenerator
  228. else:
  229. env['_SHLIBVERSIONFLAGS'] = '$SHLIBVERSIONFLAGS'
  230. env['_LDMODULEVERSIONFLAGS'] = '$LDMODULEVERSIONFLAGS'
  231. # LDOMDULVERSIONFLAGS should always default to $SHLIBVERSIONFLAGS
  232. env['LDMODULEVERSIONFLAGS'] = '$SHLIBVERSIONFLAGS'
  233. def generate(env):
  234. """Add Builders and construction variables for gnulink to an Environment."""
  235. SCons.Tool.createSharedLibBuilder(env)
  236. SCons.Tool.createProgBuilder(env)
  237. env['SHLINK'] = '$LINK'
  238. env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared')
  239. env['SHLINKCOM'] = '$SHLINK -o $TARGET $SHLINKFLAGS $__SHLIBVERSIONFLAGS $__RPATH $SOURCES $_LIBDIRFLAGS $_LIBFLAGS'
  240. # don't set up the emitter, cause AppendUnique will generate a list
  241. # starting with None :-(
  242. env.Append(SHLIBEMITTER = [shlib_emitter])
  243. env['SMARTLINK'] = smart_link
  244. env['LINK'] = "$SMARTLINK"
  245. env['LINKFLAGS'] = SCons.Util.CLVar('')
  246. # __RPATH is only set to something ($_RPATH typically) on platforms that support it.
  247. env['LINKCOM'] = '$LINK -o $TARGET $LINKFLAGS $__RPATH $SOURCES $_LIBDIRFLAGS $_LIBFLAGS'
  248. env['LIBDIRPREFIX']='-L'
  249. env['LIBDIRSUFFIX']=''
  250. env['_LIBFLAGS']='${_stripixes(LIBLINKPREFIX, LIBS, LIBLINKSUFFIX, LIBPREFIXES, LIBSUFFIXES, __env__)}'
  251. env['LIBLINKPREFIX']='-l'
  252. env['LIBLINKSUFFIX']=''
  253. if env['PLATFORM'] == 'hpux':
  254. env['SHLIBSUFFIX'] = '.sl'
  255. elif env['PLATFORM'] == 'aix':
  256. env['SHLIBSUFFIX'] = '.a'
  257. # For most platforms, a loadable module is the same as a shared
  258. # library. Platforms which are different can override these, but
  259. # setting them the same means that LoadableModule works everywhere.
  260. SCons.Tool.createLoadableModuleBuilder(env)
  261. env['LDMODULE'] = '$SHLINK'
  262. env.Append(LDMODULEEMITTER = [ldmod_emitter])
  263. env['LDMODULEPREFIX'] = '$SHLIBPREFIX'
  264. env['LDMODULESUFFIX'] = '$SHLIBSUFFIX'
  265. env['LDMODULEFLAGS'] = '$SHLINKFLAGS'
  266. env['LDMODULECOM'] = '$LDMODULE -o $TARGET $LDMODULEFLAGS $__LDMODULEVERSIONFLAGS $__RPATH $SOURCES $_LIBDIRFLAGS $_LIBFLAGS'
  267. env['LDMODULEVERSION'] = '$SHLIBVERSION'
  268. env['LDMODULENOVERSIONSYMLINKS'] = '$SHLIBNOVERSIONSYMLINKS'
  269. def exists(env):
  270. # This module isn't really a Tool on its own, it's common logic for
  271. # other linkers.
  272. return None
  273. # Local Variables:
  274. # tab-width:4
  275. # indent-tabs-mode:nil
  276. # End:
  277. # vim: set expandtab tabstop=4 shiftwidth=4: