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.

428 lines
15 KiB

6 years ago
  1. """SCons.Tool.install
  2. Tool-specific initialization for the install tool.
  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/install.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  31. import os
  32. import re
  33. import shutil
  34. import stat
  35. import SCons.Action
  36. import SCons.Tool
  37. import SCons.Util
  38. #
  39. # We keep track of *all* installed files.
  40. _INSTALLED_FILES = []
  41. _UNIQUE_INSTALLED_FILES = None
  42. class CopytreeError(EnvironmentError):
  43. pass
  44. # This is a patched version of shutil.copytree from python 2.5. It
  45. # doesn't fail if the dir exists, which regular copytree does
  46. # (annoyingly). Note the XXX comment in the docstring.
  47. def scons_copytree(src, dst, symlinks=False):
  48. """Recursively copy a directory tree using copy2().
  49. The destination directory must not already exist.
  50. If exception(s) occur, an CopytreeError is raised with a list of reasons.
  51. If the optional symlinks flag is true, symbolic links in the
  52. source tree result in symbolic links in the destination tree; if
  53. it is false, the contents of the files pointed to by symbolic
  54. links are copied.
  55. XXX Consider this example code rather than the ultimate tool.
  56. """
  57. names = os.listdir(src)
  58. # garyo@genarts.com fix: check for dir before making dirs.
  59. if not os.path.exists(dst):
  60. os.makedirs(dst)
  61. errors = []
  62. for name in names:
  63. srcname = os.path.join(src, name)
  64. dstname = os.path.join(dst, name)
  65. try:
  66. if symlinks and os.path.islink(srcname):
  67. linkto = os.readlink(srcname)
  68. os.symlink(linkto, dstname)
  69. elif os.path.isdir(srcname):
  70. scons_copytree(srcname, dstname, symlinks)
  71. else:
  72. shutil.copy2(srcname, dstname)
  73. # XXX What about devices, sockets etc.?
  74. except (IOError, os.error) as why:
  75. errors.append((srcname, dstname, str(why)))
  76. # catch the CopytreeError from the recursive copytree so that we can
  77. # continue with other files
  78. except CopytreeError as err:
  79. errors.extend(err.args[0])
  80. try:
  81. shutil.copystat(src, dst)
  82. except SCons.Util.WinError:
  83. # can't copy file access times on Windows
  84. pass
  85. except OSError as why:
  86. errors.extend((src, dst, str(why)))
  87. if errors:
  88. raise CopytreeError(errors)
  89. #
  90. # Functions doing the actual work of the Install Builder.
  91. #
  92. def copyFunc(dest, source, env):
  93. """Install a source file or directory into a destination by copying,
  94. (including copying permission/mode bits)."""
  95. if os.path.isdir(source):
  96. if os.path.exists(dest):
  97. if not os.path.isdir(dest):
  98. raise SCons.Errors.UserError("cannot overwrite non-directory `%s' with a directory `%s'" % (str(dest), str(source)))
  99. else:
  100. parent = os.path.split(dest)[0]
  101. if not os.path.exists(parent):
  102. os.makedirs(parent)
  103. scons_copytree(source, dest)
  104. else:
  105. shutil.copy2(source, dest)
  106. st = os.stat(source)
  107. os.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
  108. return 0
  109. #
  110. # Functions doing the actual work of the InstallVersionedLib Builder.
  111. #
  112. def copyFuncVersionedLib(dest, source, env):
  113. """Install a versioned library into a destination by copying,
  114. (including copying permission/mode bits) and then creating
  115. required symlinks."""
  116. if os.path.isdir(source):
  117. raise SCons.Errors.UserError("cannot install directory `%s' as a version library" % str(source) )
  118. else:
  119. # remove the link if it is already there
  120. try:
  121. os.remove(dest)
  122. except:
  123. pass
  124. shutil.copy2(source, dest)
  125. st = os.stat(source)
  126. os.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
  127. installShlibLinks(dest, source, env)
  128. return 0
  129. def listShlibLinksToInstall(dest, source, env):
  130. install_links = []
  131. source = env.arg2nodes(source)
  132. dest = env.fs.File(dest)
  133. install_dir = dest.get_dir()
  134. for src in source:
  135. symlinks = getattr(getattr(src,'attributes',None), 'shliblinks', None)
  136. if symlinks:
  137. for link, linktgt in symlinks:
  138. link_base = os.path.basename(link.get_path())
  139. linktgt_base = os.path.basename(linktgt.get_path())
  140. install_link = env.fs.File(link_base, install_dir)
  141. install_linktgt = env.fs.File(linktgt_base, install_dir)
  142. install_links.append((install_link, install_linktgt))
  143. return install_links
  144. def installShlibLinks(dest, source, env):
  145. """If we are installing a versioned shared library create the required links."""
  146. Verbose = False
  147. symlinks = listShlibLinksToInstall(dest, source, env)
  148. if Verbose:
  149. print('installShlibLinks: symlinks={:r}'.format(SCons.Tool.StringizeLibSymlinks(symlinks)))
  150. if symlinks:
  151. SCons.Tool.CreateLibSymlinks(env, symlinks)
  152. return
  153. def installFunc(target, source, env):
  154. """Install a source file into a target using the function specified
  155. as the INSTALL construction variable."""
  156. try:
  157. install = env['INSTALL']
  158. except KeyError:
  159. raise SCons.Errors.UserError('Missing INSTALL construction variable.')
  160. assert len(target)==len(source), \
  161. "Installing source %s into target %s: target and source lists must have same length."%(list(map(str, source)), list(map(str, target)))
  162. for t,s in zip(target,source):
  163. if install(t.get_path(),s.get_path(),env):
  164. return 1
  165. return 0
  166. def installFuncVersionedLib(target, source, env):
  167. """Install a versioned library into a target using the function specified
  168. as the INSTALLVERSIONEDLIB construction variable."""
  169. try:
  170. install = env['INSTALLVERSIONEDLIB']
  171. except KeyError:
  172. raise SCons.Errors.UserError('Missing INSTALLVERSIONEDLIB construction variable.')
  173. assert len(target)==len(source), \
  174. "Installing source %s into target %s: target and source lists must have same length."%(list(map(str, source)), list(map(str, target)))
  175. for t,s in zip(target,source):
  176. if hasattr(t.attributes, 'shlibname'):
  177. tpath = os.path.join(t.get_dir(), t.attributes.shlibname)
  178. else:
  179. tpath = t.get_path()
  180. if install(tpath,s.get_path(),env):
  181. return 1
  182. return 0
  183. def stringFunc(target, source, env):
  184. installstr = env.get('INSTALLSTR')
  185. if installstr:
  186. return env.subst_target_source(installstr, 0, target, source)
  187. target = str(target[0])
  188. source = str(source[0])
  189. if os.path.isdir(source):
  190. type = 'directory'
  191. else:
  192. type = 'file'
  193. return 'Install %s: "%s" as "%s"' % (type, source, target)
  194. #
  195. # Emitter functions
  196. #
  197. def add_targets_to_INSTALLED_FILES(target, source, env):
  198. """ An emitter that adds all target files to the list stored in the
  199. _INSTALLED_FILES global variable. This way all installed files of one
  200. scons call will be collected.
  201. """
  202. global _INSTALLED_FILES, _UNIQUE_INSTALLED_FILES
  203. _INSTALLED_FILES.extend(target)
  204. _UNIQUE_INSTALLED_FILES = None
  205. return (target, source)
  206. def add_versioned_targets_to_INSTALLED_FILES(target, source, env):
  207. """ An emitter that adds all target files to the list stored in the
  208. _INSTALLED_FILES global variable. This way all installed files of one
  209. scons call will be collected.
  210. """
  211. global _INSTALLED_FILES, _UNIQUE_INSTALLED_FILES
  212. Verbose = False
  213. _INSTALLED_FILES.extend(target)
  214. if Verbose:
  215. print("add_versioned_targets_to_INSTALLED_FILES: target={:r}".format(list(map(str, target))))
  216. symlinks = listShlibLinksToInstall(target[0], source, env)
  217. if symlinks:
  218. SCons.Tool.EmitLibSymlinks(env, symlinks, target[0])
  219. _UNIQUE_INSTALLED_FILES = None
  220. return (target, source)
  221. class DESTDIR_factory(object):
  222. """ A node factory, where all files will be relative to the dir supplied
  223. in the constructor.
  224. """
  225. def __init__(self, env, dir):
  226. self.env = env
  227. self.dir = env.arg2nodes( dir, env.fs.Dir )[0]
  228. def Entry(self, name):
  229. name = SCons.Util.make_path_relative(name)
  230. return self.dir.Entry(name)
  231. def Dir(self, name):
  232. name = SCons.Util.make_path_relative(name)
  233. return self.dir.Dir(name)
  234. #
  235. # The Builder Definition
  236. #
  237. install_action = SCons.Action.Action(installFunc, stringFunc)
  238. installas_action = SCons.Action.Action(installFunc, stringFunc)
  239. installVerLib_action = SCons.Action.Action(installFuncVersionedLib, stringFunc)
  240. BaseInstallBuilder = None
  241. def InstallBuilderWrapper(env, target=None, source=None, dir=None, **kw):
  242. if target and dir:
  243. import SCons.Errors
  244. raise SCons.Errors.UserError("Both target and dir defined for Install(), only one may be defined.")
  245. if not dir:
  246. dir=target
  247. import SCons.Script
  248. install_sandbox = SCons.Script.GetOption('install_sandbox')
  249. if install_sandbox:
  250. target_factory = DESTDIR_factory(env, install_sandbox)
  251. else:
  252. target_factory = env.fs
  253. try:
  254. dnodes = env.arg2nodes(dir, target_factory.Dir)
  255. except TypeError:
  256. raise SCons.Errors.UserError("Target `%s' of Install() is a file, but should be a directory. Perhaps you have the Install() arguments backwards?" % str(dir))
  257. sources = env.arg2nodes(source, env.fs.Entry)
  258. tgt = []
  259. for dnode in dnodes:
  260. for src in sources:
  261. # Prepend './' so the lookup doesn't interpret an initial
  262. # '#' on the file name portion as meaning the Node should
  263. # be relative to the top-level SConstruct directory.
  264. target = env.fs.Entry('.'+os.sep+src.name, dnode)
  265. tgt.extend(BaseInstallBuilder(env, target, src, **kw))
  266. return tgt
  267. def InstallAsBuilderWrapper(env, target=None, source=None, **kw):
  268. result = []
  269. for src, tgt in map(lambda x, y: (x, y), source, target):
  270. result.extend(BaseInstallBuilder(env, tgt, src, **kw))
  271. return result
  272. BaseVersionedInstallBuilder = None
  273. def InstallVersionedBuilderWrapper(env, target=None, source=None, dir=None, **kw):
  274. if target and dir:
  275. import SCons.Errors
  276. raise SCons.Errors.UserError("Both target and dir defined for Install(), only one may be defined.")
  277. if not dir:
  278. dir=target
  279. import SCons.Script
  280. install_sandbox = SCons.Script.GetOption('install_sandbox')
  281. if install_sandbox:
  282. target_factory = DESTDIR_factory(env, install_sandbox)
  283. else:
  284. target_factory = env.fs
  285. try:
  286. dnodes = env.arg2nodes(dir, target_factory.Dir)
  287. except TypeError:
  288. raise SCons.Errors.UserError("Target `%s' of Install() is a file, but should be a directory. Perhaps you have the Install() arguments backwards?" % str(dir))
  289. sources = env.arg2nodes(source, env.fs.Entry)
  290. tgt = []
  291. for dnode in dnodes:
  292. for src in sources:
  293. # Prepend './' so the lookup doesn't interpret an initial
  294. # '#' on the file name portion as meaning the Node should
  295. # be relative to the top-level SConstruct directory.
  296. target = env.fs.Entry('.'+os.sep+src.name, dnode)
  297. tgt.extend(BaseVersionedInstallBuilder(env, target, src, **kw))
  298. return tgt
  299. added = None
  300. def generate(env):
  301. from SCons.Script import AddOption, GetOption
  302. global added
  303. if not added:
  304. added = 1
  305. AddOption('--install-sandbox',
  306. dest='install_sandbox',
  307. type="string",
  308. action="store",
  309. help='A directory under which all installed files will be placed.')
  310. global BaseInstallBuilder
  311. if BaseInstallBuilder is None:
  312. install_sandbox = GetOption('install_sandbox')
  313. if install_sandbox:
  314. target_factory = DESTDIR_factory(env, install_sandbox)
  315. else:
  316. target_factory = env.fs
  317. BaseInstallBuilder = SCons.Builder.Builder(
  318. action = install_action,
  319. target_factory = target_factory.Entry,
  320. source_factory = env.fs.Entry,
  321. multi = 1,
  322. emitter = [ add_targets_to_INSTALLED_FILES, ],
  323. source_scanner = SCons.Scanner.Base( {}, name = 'Install', recursive = False ),
  324. name = 'InstallBuilder')
  325. global BaseVersionedInstallBuilder
  326. if BaseVersionedInstallBuilder is None:
  327. install_sandbox = GetOption('install_sandbox')
  328. if install_sandbox:
  329. target_factory = DESTDIR_factory(env, install_sandbox)
  330. else:
  331. target_factory = env.fs
  332. BaseVersionedInstallBuilder = SCons.Builder.Builder(
  333. action = installVerLib_action,
  334. target_factory = target_factory.Entry,
  335. source_factory = env.fs.Entry,
  336. multi = 1,
  337. emitter = [ add_versioned_targets_to_INSTALLED_FILES, ],
  338. name = 'InstallVersionedBuilder')
  339. env['BUILDERS']['_InternalInstall'] = InstallBuilderWrapper
  340. env['BUILDERS']['_InternalInstallAs'] = InstallAsBuilderWrapper
  341. env['BUILDERS']['_InternalInstallVersionedLib'] = InstallVersionedBuilderWrapper
  342. # We'd like to initialize this doing something like the following,
  343. # but there isn't yet support for a ${SOURCE.type} expansion that
  344. # will print "file" or "directory" depending on what's being
  345. # installed. For now we punt by not initializing it, and letting
  346. # the stringFunc() that we put in the action fall back to the
  347. # hand-crafted default string if it's not set.
  348. #
  349. #try:
  350. # env['INSTALLSTR']
  351. #except KeyError:
  352. # env['INSTALLSTR'] = 'Install ${SOURCE.type}: "$SOURCES" as "$TARGETS"'
  353. try:
  354. env['INSTALL']
  355. except KeyError:
  356. env['INSTALL'] = copyFunc
  357. try:
  358. env['INSTALLVERSIONEDLIB']
  359. except KeyError:
  360. env['INSTALLVERSIONEDLIB'] = copyFuncVersionedLib
  361. def exists(env):
  362. return 1
  363. # Local Variables:
  364. # tab-width:4
  365. # indent-tabs-mode:nil
  366. # End:
  367. # vim: set expandtab tabstop=4 shiftwidth=4: