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.

593 lines
20 KiB

6 years ago
  1. """SCons.Defaults
  2. Builders and other things for the local site. Here's where we'll
  3. duplicate the functionality of autoconf until we move it into the
  4. installation procedure or use something like qmconf.
  5. The code that reads the registry to find MSVC components was borrowed
  6. from distutils.msvccompiler.
  7. """
  8. #
  9. # Copyright (c) 2001 - 2017 The SCons Foundation
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining
  12. # a copy of this software and associated documentation files (the
  13. # "Software"), to deal in the Software without restriction, including
  14. # without limitation the rights to use, copy, modify, merge, publish,
  15. # distribute, sublicense, and/or sell copies of the Software, and to
  16. # permit persons to whom the Software is furnished to do so, subject to
  17. # the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included
  20. # in all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  23. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  24. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. #
  30. from __future__ import division
  31. __revision__ = "src/engine/SCons/Defaults.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  32. import os
  33. import errno
  34. import shutil
  35. import stat
  36. import time
  37. import sys
  38. import SCons.Action
  39. import SCons.Builder
  40. import SCons.CacheDir
  41. import SCons.Environment
  42. import SCons.PathList
  43. import SCons.Subst
  44. import SCons.Tool
  45. # A placeholder for a default Environment (for fetching source files
  46. # from source code management systems and the like). This must be
  47. # initialized later, after the top-level directory is set by the calling
  48. # interface.
  49. _default_env = None
  50. # Lazily instantiate the default environment so the overhead of creating
  51. # it doesn't apply when it's not needed.
  52. def _fetch_DefaultEnvironment(*args, **kw):
  53. """
  54. Returns the already-created default construction environment.
  55. """
  56. global _default_env
  57. return _default_env
  58. def DefaultEnvironment(*args, **kw):
  59. """
  60. Initial public entry point for creating the default construction
  61. Environment.
  62. After creating the environment, we overwrite our name
  63. (DefaultEnvironment) with the _fetch_DefaultEnvironment() function,
  64. which more efficiently returns the initialized default construction
  65. environment without checking for its existence.
  66. (This function still exists with its _default_check because someone
  67. else (*cough* Script/__init__.py *cough*) may keep a reference
  68. to this function. So we can't use the fully functional idiom of
  69. having the name originally be a something that *only* creates the
  70. construction environment and then overwrites the name.)
  71. """
  72. global _default_env
  73. if not _default_env:
  74. import SCons.Util
  75. _default_env = SCons.Environment.Environment(*args, **kw)
  76. if SCons.Util.md5:
  77. _default_env.Decider('MD5')
  78. else:
  79. _default_env.Decider('timestamp-match')
  80. global DefaultEnvironment
  81. DefaultEnvironment = _fetch_DefaultEnvironment
  82. _default_env._CacheDir_path = None
  83. return _default_env
  84. # Emitters for setting the shared attribute on object files,
  85. # and an action for checking that all of the source files
  86. # going into a shared library are, in fact, shared.
  87. def StaticObjectEmitter(target, source, env):
  88. for tgt in target:
  89. tgt.attributes.shared = None
  90. return (target, source)
  91. def SharedObjectEmitter(target, source, env):
  92. for tgt in target:
  93. tgt.attributes.shared = 1
  94. return (target, source)
  95. def SharedFlagChecker(source, target, env):
  96. same = env.subst('$STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME')
  97. if same == '0' or same == '' or same == 'False':
  98. for src in source:
  99. try:
  100. shared = src.attributes.shared
  101. except AttributeError:
  102. shared = None
  103. if not shared:
  104. raise SCons.Errors.UserError("Source file: %s is static and is not compatible with shared target: %s" % (src, target[0]))
  105. SharedCheck = SCons.Action.Action(SharedFlagChecker, None)
  106. # Some people were using these variable name before we made
  107. # SourceFileScanner part of the public interface. Don't break their
  108. # SConscript files until we've given them some fair warning and a
  109. # transition period.
  110. CScan = SCons.Tool.CScanner
  111. DScan = SCons.Tool.DScanner
  112. LaTeXScan = SCons.Tool.LaTeXScanner
  113. ObjSourceScan = SCons.Tool.SourceFileScanner
  114. ProgScan = SCons.Tool.ProgramScanner
  115. # These aren't really tool scanners, so they don't quite belong with
  116. # the rest of those in Tool/__init__.py, but I'm not sure where else
  117. # they should go. Leave them here for now.
  118. import SCons.Scanner.Dir
  119. DirScanner = SCons.Scanner.Dir.DirScanner()
  120. DirEntryScanner = SCons.Scanner.Dir.DirEntryScanner()
  121. # Actions for common languages.
  122. CAction = SCons.Action.Action("$CCCOM", "$CCCOMSTR")
  123. ShCAction = SCons.Action.Action("$SHCCCOM", "$SHCCCOMSTR")
  124. CXXAction = SCons.Action.Action("$CXXCOM", "$CXXCOMSTR")
  125. ShCXXAction = SCons.Action.Action("$SHCXXCOM", "$SHCXXCOMSTR")
  126. DAction = SCons.Action.Action("$DCOM", "$DCOMSTR")
  127. ShDAction = SCons.Action.Action("$SHDCOM", "$SHDCOMSTR")
  128. ASAction = SCons.Action.Action("$ASCOM", "$ASCOMSTR")
  129. ASPPAction = SCons.Action.Action("$ASPPCOM", "$ASPPCOMSTR")
  130. LinkAction = SCons.Action.Action("$LINKCOM", "$LINKCOMSTR")
  131. ShLinkAction = SCons.Action.Action("$SHLINKCOM", "$SHLINKCOMSTR")
  132. LdModuleLinkAction = SCons.Action.Action("$LDMODULECOM", "$LDMODULECOMSTR")
  133. # Common tasks that we allow users to perform in platform-independent
  134. # ways by creating ActionFactory instances.
  135. ActionFactory = SCons.Action.ActionFactory
  136. def get_paths_str(dest):
  137. # If dest is a list, we need to manually call str() on each element
  138. if SCons.Util.is_List(dest):
  139. elem_strs = []
  140. for element in dest:
  141. elem_strs.append('"' + str(element) + '"')
  142. return '[' + ', '.join(elem_strs) + ']'
  143. else:
  144. return '"' + str(dest) + '"'
  145. permission_dic = {
  146. 'u':{
  147. 'r':stat.S_IRUSR,
  148. 'w':stat.S_IWUSR,
  149. 'x':stat.S_IXUSR
  150. },
  151. 'g':{
  152. 'r':stat.S_IRGRP,
  153. 'w':stat.S_IWGRP,
  154. 'x':stat.S_IXGRP
  155. },
  156. 'o':{
  157. 'r':stat.S_IROTH,
  158. 'w':stat.S_IWOTH,
  159. 'x':stat.S_IXOTH
  160. }
  161. }
  162. def chmod_func(dest, mode):
  163. import SCons.Util
  164. from string import digits
  165. SCons.Node.FS.invalidate_node_memos(dest)
  166. if not SCons.Util.is_List(dest):
  167. dest = [dest]
  168. if SCons.Util.is_String(mode) and not 0 in [i in digits for i in mode]:
  169. mode = int(mode, 8)
  170. if not SCons.Util.is_String(mode):
  171. for element in dest:
  172. os.chmod(str(element), mode)
  173. else:
  174. mode = str(mode)
  175. for operation in mode.split(","):
  176. if "=" in operation:
  177. operator = "="
  178. elif "+" in operation:
  179. operator = "+"
  180. elif "-" in operation:
  181. operator = "-"
  182. else:
  183. raise SyntaxError("Could not find +, - or =")
  184. operation_list = operation.split(operator)
  185. if len(operation_list) is not 2:
  186. raise SyntaxError("More than one operator found")
  187. user = operation_list[0].strip().replace("a", "ugo")
  188. permission = operation_list[1].strip()
  189. new_perm = 0
  190. for u in user:
  191. for p in permission:
  192. try:
  193. new_perm = new_perm | permission_dic[u][p]
  194. except KeyError:
  195. raise SyntaxError("Unrecognized user or permission format")
  196. for element in dest:
  197. curr_perm = os.stat(str(element)).st_mode
  198. if operator == "=":
  199. os.chmod(str(element), new_perm)
  200. elif operator == "+":
  201. os.chmod(str(element), curr_perm | new_perm)
  202. elif operator == "-":
  203. os.chmod(str(element), curr_perm & ~new_perm)
  204. def chmod_strfunc(dest, mode):
  205. import SCons.Util
  206. if not SCons.Util.is_String(mode):
  207. return 'Chmod(%s, 0%o)' % (get_paths_str(dest), mode)
  208. else:
  209. return 'Chmod(%s, "%s")' % (get_paths_str(dest), str(mode))
  210. Chmod = ActionFactory(chmod_func, chmod_strfunc)
  211. def copy_func(dest, src, symlinks=True):
  212. """
  213. If symlinks (is true), then a symbolic link will be
  214. shallow copied and recreated as a symbolic link; otherwise, copying
  215. a symbolic link will be equivalent to copying the symbolic link's
  216. final target regardless of symbolic link depth.
  217. """
  218. dest = str(dest)
  219. src = str(src)
  220. SCons.Node.FS.invalidate_node_memos(dest)
  221. if SCons.Util.is_List(src) and os.path.isdir(dest):
  222. for file in src:
  223. shutil.copy2(file, dest)
  224. return 0
  225. elif os.path.islink(src):
  226. if symlinks:
  227. return os.symlink(os.readlink(src), dest)
  228. else:
  229. return copy_func(dest, os.path.realpath(src))
  230. elif os.path.isfile(src):
  231. shutil.copy2(src, dest)
  232. return 0
  233. else:
  234. shutil.copytree(src, dest, symlinks)
  235. # copytree returns None in python2 and destination string in python3
  236. # A error is raised in both cases, so we can just return 0 for success
  237. return 0
  238. Copy = ActionFactory(
  239. copy_func,
  240. lambda dest, src, symlinks=True: 'Copy("%s", "%s")' % (dest, src)
  241. )
  242. def delete_func(dest, must_exist=0):
  243. SCons.Node.FS.invalidate_node_memos(dest)
  244. if not SCons.Util.is_List(dest):
  245. dest = [dest]
  246. for entry in dest:
  247. entry = str(entry)
  248. # os.path.exists returns False with broken links that exist
  249. entry_exists = os.path.exists(entry) or os.path.islink(entry)
  250. if not entry_exists and not must_exist:
  251. continue
  252. # os.path.isdir returns True when entry is a link to a dir
  253. if os.path.isdir(entry) and not os.path.islink(entry):
  254. shutil.rmtree(entry, 1)
  255. continue
  256. os.unlink(entry)
  257. def delete_strfunc(dest, must_exist=0):
  258. return 'Delete(%s)' % get_paths_str(dest)
  259. Delete = ActionFactory(delete_func, delete_strfunc)
  260. def mkdir_func(dest):
  261. SCons.Node.FS.invalidate_node_memos(dest)
  262. if not SCons.Util.is_List(dest):
  263. dest = [dest]
  264. for entry in dest:
  265. try:
  266. os.makedirs(str(entry))
  267. except os.error as e:
  268. p = str(entry)
  269. if (e.args[0] == errno.EEXIST or
  270. (sys.platform=='win32' and e.args[0]==183)) \
  271. and os.path.isdir(str(entry)):
  272. pass # not an error if already exists
  273. else:
  274. raise
  275. Mkdir = ActionFactory(mkdir_func,
  276. lambda dir: 'Mkdir(%s)' % get_paths_str(dir))
  277. def move_func(dest, src):
  278. SCons.Node.FS.invalidate_node_memos(dest)
  279. SCons.Node.FS.invalidate_node_memos(src)
  280. shutil.move(src, dest)
  281. Move = ActionFactory(move_func,
  282. lambda dest, src: 'Move("%s", "%s")' % (dest, src),
  283. convert=str)
  284. def touch_func(dest):
  285. SCons.Node.FS.invalidate_node_memos(dest)
  286. if not SCons.Util.is_List(dest):
  287. dest = [dest]
  288. for file in dest:
  289. file = str(file)
  290. mtime = int(time.time())
  291. if os.path.exists(file):
  292. atime = os.path.getatime(file)
  293. else:
  294. open(file, 'w')
  295. atime = mtime
  296. os.utime(file, (atime, mtime))
  297. Touch = ActionFactory(touch_func,
  298. lambda file: 'Touch(%s)' % get_paths_str(file))
  299. # Internal utility functions
  300. def _concat(prefix, list, suffix, env, f=lambda x: x, target=None, source=None):
  301. """
  302. Creates a new list from 'list' by first interpolating each element
  303. in the list using the 'env' dictionary and then calling f on the
  304. list, and finally calling _concat_ixes to concatenate 'prefix' and
  305. 'suffix' onto each element of the list.
  306. """
  307. if not list:
  308. return list
  309. l = f(SCons.PathList.PathList(list).subst_path(env, target, source))
  310. if l is not None:
  311. list = l
  312. return _concat_ixes(prefix, list, suffix, env)
  313. def _concat_ixes(prefix, list, suffix, env):
  314. """
  315. Creates a new list from 'list' by concatenating the 'prefix' and
  316. 'suffix' arguments onto each element of the list. A trailing space
  317. on 'prefix' or leading space on 'suffix' will cause them to be put
  318. into separate list elements rather than being concatenated.
  319. """
  320. result = []
  321. # ensure that prefix and suffix are strings
  322. prefix = str(env.subst(prefix, SCons.Subst.SUBST_RAW))
  323. suffix = str(env.subst(suffix, SCons.Subst.SUBST_RAW))
  324. for x in list:
  325. if isinstance(x, SCons.Node.FS.File):
  326. result.append(x)
  327. continue
  328. x = str(x)
  329. if x:
  330. if prefix:
  331. if prefix[-1] == ' ':
  332. result.append(prefix[:-1])
  333. elif x[:len(prefix)] != prefix:
  334. x = prefix + x
  335. result.append(x)
  336. if suffix:
  337. if suffix[0] == ' ':
  338. result.append(suffix[1:])
  339. elif x[-len(suffix):] != suffix:
  340. result[-1] = result[-1]+suffix
  341. return result
  342. def _stripixes(prefix, itms, suffix, stripprefixes, stripsuffixes, env, c=None):
  343. """
  344. This is a wrapper around _concat()/_concat_ixes() that checks for
  345. the existence of prefixes or suffixes on list items and strips them
  346. where it finds them. This is used by tools (like the GNU linker)
  347. that need to turn something like 'libfoo.a' into '-lfoo'.
  348. """
  349. if not itms:
  350. return itms
  351. if not callable(c):
  352. env_c = env['_concat']
  353. if env_c != _concat and callable(env_c):
  354. # There's a custom _concat() method in the construction
  355. # environment, and we've allowed people to set that in
  356. # the past (see test/custom-concat.py), so preserve the
  357. # backwards compatibility.
  358. c = env_c
  359. else:
  360. c = _concat_ixes
  361. stripprefixes = list(map(env.subst, SCons.Util.flatten(stripprefixes)))
  362. stripsuffixes = list(map(env.subst, SCons.Util.flatten(stripsuffixes)))
  363. stripped = []
  364. for l in SCons.PathList.PathList(itms).subst_path(env, None, None):
  365. if isinstance(l, SCons.Node.FS.File):
  366. stripped.append(l)
  367. continue
  368. if not SCons.Util.is_String(l):
  369. l = str(l)
  370. for stripprefix in stripprefixes:
  371. lsp = len(stripprefix)
  372. if l[:lsp] == stripprefix:
  373. l = l[lsp:]
  374. # Do not strip more than one prefix
  375. break
  376. for stripsuffix in stripsuffixes:
  377. lss = len(stripsuffix)
  378. if l[-lss:] == stripsuffix:
  379. l = l[:-lss]
  380. # Do not strip more than one suffix
  381. break
  382. stripped.append(l)
  383. return c(prefix, stripped, suffix, env)
  384. def processDefines(defs):
  385. """process defines, resolving strings, lists, dictionaries, into a list of
  386. strings
  387. """
  388. if SCons.Util.is_List(defs):
  389. l = []
  390. for d in defs:
  391. if d is None:
  392. continue
  393. elif SCons.Util.is_List(d) or isinstance(d, tuple):
  394. if len(d) >= 2:
  395. l.append(str(d[0]) + '=' + str(d[1]))
  396. else:
  397. l.append(str(d[0]))
  398. elif SCons.Util.is_Dict(d):
  399. for macro,value in d.items():
  400. if value is not None:
  401. l.append(str(macro) + '=' + str(value))
  402. else:
  403. l.append(str(macro))
  404. elif SCons.Util.is_String(d):
  405. l.append(str(d))
  406. else:
  407. raise SCons.Errors.UserError("DEFINE %s is not a list, dict, string or None."%repr(d))
  408. elif SCons.Util.is_Dict(defs):
  409. # The items in a dictionary are stored in random order, but
  410. # if the order of the command-line options changes from
  411. # invocation to invocation, then the signature of the command
  412. # line will change and we'll get random unnecessary rebuilds.
  413. # Consequently, we have to sort the keys to ensure a
  414. # consistent order...
  415. l = []
  416. for k,v in sorted(defs.items()):
  417. if v is None:
  418. l.append(str(k))
  419. else:
  420. l.append(str(k) + '=' + str(v))
  421. else:
  422. l = [str(defs)]
  423. return l
  424. def _defines(prefix, defs, suffix, env, c=_concat_ixes):
  425. """A wrapper around _concat_ixes that turns a list or string
  426. into a list of C preprocessor command-line definitions.
  427. """
  428. return c(prefix, env.subst_path(processDefines(defs)), suffix, env)
  429. class NullCmdGenerator(object):
  430. """This is a callable class that can be used in place of other
  431. command generators if you don't want them to do anything.
  432. The __call__ method for this class simply returns the thing
  433. you instantiated it with.
  434. Example usage:
  435. env["DO_NOTHING"] = NullCmdGenerator
  436. env["LINKCOM"] = "${DO_NOTHING('$LINK $SOURCES $TARGET')}"
  437. """
  438. def __init__(self, cmd):
  439. self.cmd = cmd
  440. def __call__(self, target, source, env, for_signature=None):
  441. return self.cmd
  442. class Variable_Method_Caller(object):
  443. """A class for finding a construction variable on the stack and
  444. calling one of its methods.
  445. We use this to support "construction variables" in our string
  446. eval()s that actually stand in for methods--specifically, use
  447. of "RDirs" in call to _concat that should actually execute the
  448. "TARGET.RDirs" method. (We used to support this by creating a little
  449. "build dictionary" that mapped RDirs to the method, but this got in
  450. the way of Memoizing construction environments, because we had to
  451. create new environment objects to hold the variables.)
  452. """
  453. def __init__(self, variable, method):
  454. self.variable = variable
  455. self.method = method
  456. def __call__(self, *args, **kw):
  457. try: 1//0
  458. except ZeroDivisionError:
  459. # Don't start iterating with the current stack-frame to
  460. # prevent creating reference cycles (f_back is safe).
  461. frame = sys.exc_info()[2].tb_frame.f_back
  462. variable = self.variable
  463. while frame:
  464. if variable in frame.f_locals:
  465. v = frame.f_locals[variable]
  466. if v:
  467. method = getattr(v, self.method)
  468. return method(*args, **kw)
  469. frame = frame.f_back
  470. return None
  471. # if $version_var is not empty, returns env[flags_var], otherwise returns None
  472. def __libversionflags(env, version_var, flags_var):
  473. try:
  474. if env.subst('$'+version_var):
  475. return env[flags_var]
  476. except KeyError:
  477. pass
  478. return None
  479. ConstructionEnvironment = {
  480. 'BUILDERS' : {},
  481. 'SCANNERS' : [ SCons.Tool.SourceFileScanner ],
  482. 'CONFIGUREDIR' : '#/.sconf_temp',
  483. 'CONFIGURELOG' : '#/config.log',
  484. 'CPPSUFFIXES' : SCons.Tool.CSuffixes,
  485. 'DSUFFIXES' : SCons.Tool.DSuffixes,
  486. 'ENV' : {},
  487. 'IDLSUFFIXES' : SCons.Tool.IDLSuffixes,
  488. # 'LATEXSUFFIXES' : SCons.Tool.LaTeXSuffixes, # moved to the TeX tools generate functions
  489. '_concat' : _concat,
  490. '_defines' : _defines,
  491. '_stripixes' : _stripixes,
  492. '_LIBFLAGS' : '${_concat(LIBLINKPREFIX, LIBS, LIBLINKSUFFIX, __env__)}',
  493. '_LIBDIRFLAGS' : '$( ${_concat(LIBDIRPREFIX, LIBPATH, LIBDIRSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)',
  494. '_CPPINCFLAGS' : '$( ${_concat(INCPREFIX, CPPPATH, INCSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)',
  495. '_CPPDEFFLAGS' : '${_defines(CPPDEFPREFIX, CPPDEFINES, CPPDEFSUFFIX, __env__)}',
  496. '__libversionflags' : __libversionflags,
  497. '__SHLIBVERSIONFLAGS' : '${__libversionflags(__env__,"SHLIBVERSION","_SHLIBVERSIONFLAGS")}',
  498. '__LDMODULEVERSIONFLAGS' : '${__libversionflags(__env__,"LDMODULEVERSION","_LDMODULEVERSIONFLAGS")}',
  499. '__DSHLIBVERSIONFLAGS' : '${__libversionflags(__env__,"DSHLIBVERSION","_DSHLIBVERSIONFLAGS")}',
  500. 'TEMPFILE' : NullCmdGenerator,
  501. 'Dir' : Variable_Method_Caller('TARGET', 'Dir'),
  502. 'Dirs' : Variable_Method_Caller('TARGET', 'Dirs'),
  503. 'File' : Variable_Method_Caller('TARGET', 'File'),
  504. 'RDirs' : Variable_Method_Caller('TARGET', 'RDirs'),
  505. }
  506. # Local Variables:
  507. # tab-width:4
  508. # indent-tabs-mode:nil
  509. # End:
  510. # vim: set expandtab tabstop=4 shiftwidth=4: