PageRenderTime 67ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/couchjs/scons/scons-local-2.0.1/SCons/Script/Main.py

http://github.com/cloudant/bigcouch
Python | 1334 lines | 1249 code | 36 blank | 49 comment | 66 complexity | 77fdebef8b1832eaebd29f94cfe02d5d MD5 | raw file
Possible License(s): Apache-2.0
  1. """SCons.Script
  2. This file implements the main() function used by the scons script.
  3. Architecturally, this *is* the scons script, and will likely only be
  4. called from the external "scons" wrapper. Consequently, anything here
  5. should not be, or be considered, part of the build engine. If it's
  6. something that we expect other software to want to use, it should go in
  7. some other module. If it's specific to the "scons" script invocation,
  8. it goes here.
  9. """
  10. unsupported_python_version = (2, 3, 0)
  11. deprecated_python_version = (2, 4, 0)
  12. # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation
  13. #
  14. # Permission is hereby granted, free of charge, to any person obtaining
  15. # a copy of this software and associated documentation files (the
  16. # "Software"), to deal in the Software without restriction, including
  17. # without limitation the rights to use, copy, modify, merge, publish,
  18. # distribute, sublicense, and/or sell copies of the Software, and to
  19. # permit persons to whom the Software is furnished to do so, subject to
  20. # the following conditions:
  21. #
  22. # The above copyright notice and this permission notice shall be included
  23. # in all copies or substantial portions of the Software.
  24. #
  25. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  26. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  27. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. __revision__ = "src/engine/SCons/Script/Main.py 5134 2010/08/16 23:02:40 bdeegan"
  33. import SCons.compat
  34. import os
  35. import sys
  36. import time
  37. import traceback
  38. # Strip the script directory from sys.path() so on case-insensitive
  39. # (Windows) systems Python doesn't think that the "scons" script is the
  40. # "SCons" package. Replace it with our own version directory so, if
  41. # if they're there, we pick up the right version of the build engine
  42. # modules.
  43. #sys.path = [os.path.join(sys.prefix,
  44. # 'lib',
  45. # 'scons-%d' % SCons.__version__)] + sys.path[1:]
  46. import SCons.CacheDir
  47. import SCons.Debug
  48. import SCons.Defaults
  49. import SCons.Environment
  50. import SCons.Errors
  51. import SCons.Job
  52. import SCons.Node
  53. import SCons.Node.FS
  54. import SCons.SConf
  55. import SCons.Script
  56. import SCons.Taskmaster
  57. import SCons.Util
  58. import SCons.Warnings
  59. import SCons.Script.Interactive
  60. def fetch_win32_parallel_msg():
  61. # A subsidiary function that exists solely to isolate this import
  62. # so we don't have to pull it in on all platforms, and so that an
  63. # in-line "import" statement in the _main() function below doesn't
  64. # cause warnings about local names shadowing use of the 'SCons'
  65. # globl in nest scopes and UnboundLocalErrors and the like in some
  66. # versions (2.1) of Python.
  67. import SCons.Platform.win32
  68. return SCons.Platform.win32.parallel_msg
  69. #
  70. class SConsPrintHelpException(Exception):
  71. pass
  72. display = SCons.Util.display
  73. progress_display = SCons.Util.DisplayEngine()
  74. first_command_start = None
  75. last_command_end = None
  76. class Progressor(object):
  77. prev = ''
  78. count = 0
  79. target_string = '$TARGET'
  80. def __init__(self, obj, interval=1, file=None, overwrite=False):
  81. if file is None:
  82. file = sys.stdout
  83. self.obj = obj
  84. self.file = file
  85. self.interval = interval
  86. self.overwrite = overwrite
  87. if callable(obj):
  88. self.func = obj
  89. elif SCons.Util.is_List(obj):
  90. self.func = self.spinner
  91. elif obj.find(self.target_string) != -1:
  92. self.func = self.replace_string
  93. else:
  94. self.func = self.string
  95. def write(self, s):
  96. self.file.write(s)
  97. self.file.flush()
  98. self.prev = s
  99. def erase_previous(self):
  100. if self.prev:
  101. length = len(self.prev)
  102. if self.prev[-1] in ('\n', '\r'):
  103. length = length - 1
  104. self.write(' ' * length + '\r')
  105. self.prev = ''
  106. def spinner(self, node):
  107. self.write(self.obj[self.count % len(self.obj)])
  108. def string(self, node):
  109. self.write(self.obj)
  110. def replace_string(self, node):
  111. self.write(self.obj.replace(self.target_string, str(node)))
  112. def __call__(self, node):
  113. self.count = self.count + 1
  114. if (self.count % self.interval) == 0:
  115. if self.overwrite:
  116. self.erase_previous()
  117. self.func(node)
  118. ProgressObject = SCons.Util.Null()
  119. def Progress(*args, **kw):
  120. global ProgressObject
  121. ProgressObject = Progressor(*args, **kw)
  122. # Task control.
  123. #
  124. _BuildFailures = []
  125. def GetBuildFailures():
  126. return _BuildFailures
  127. class BuildTask(SCons.Taskmaster.OutOfDateTask):
  128. """An SCons build task."""
  129. progress = ProgressObject
  130. def display(self, message):
  131. display('scons: ' + message)
  132. def prepare(self):
  133. self.progress(self.targets[0])
  134. return SCons.Taskmaster.OutOfDateTask.prepare(self)
  135. def needs_execute(self):
  136. if SCons.Taskmaster.OutOfDateTask.needs_execute(self):
  137. return True
  138. if self.top and self.targets[0].has_builder():
  139. display("scons: `%s' is up to date." % str(self.node))
  140. return False
  141. def execute(self):
  142. if print_time:
  143. start_time = time.time()
  144. global first_command_start
  145. if first_command_start is None:
  146. first_command_start = start_time
  147. SCons.Taskmaster.OutOfDateTask.execute(self)
  148. if print_time:
  149. global cumulative_command_time
  150. global last_command_end
  151. finish_time = time.time()
  152. last_command_end = finish_time
  153. cumulative_command_time = cumulative_command_time+finish_time-start_time
  154. sys.stdout.write("Command execution time: %f seconds\n"%(finish_time-start_time))
  155. def do_failed(self, status=2):
  156. _BuildFailures.append(self.exception[1])
  157. global exit_status
  158. global this_build_status
  159. if self.options.ignore_errors:
  160. SCons.Taskmaster.OutOfDateTask.executed(self)
  161. elif self.options.keep_going:
  162. SCons.Taskmaster.OutOfDateTask.fail_continue(self)
  163. exit_status = status
  164. this_build_status = status
  165. else:
  166. SCons.Taskmaster.OutOfDateTask.fail_stop(self)
  167. exit_status = status
  168. this_build_status = status
  169. def executed(self):
  170. t = self.targets[0]
  171. if self.top and not t.has_builder() and not t.side_effect:
  172. if not t.exists():
  173. if t.__class__.__name__ in ('File', 'Dir', 'Entry'):
  174. errstr="Do not know how to make %s target `%s' (%s)." % (t.__class__.__name__, t, t.abspath)
  175. else: # Alias or Python or ...
  176. errstr="Do not know how to make %s target `%s'." % (t.__class__.__name__, t)
  177. sys.stderr.write("scons: *** " + errstr)
  178. if not self.options.keep_going:
  179. sys.stderr.write(" Stop.")
  180. sys.stderr.write("\n")
  181. try:
  182. raise SCons.Errors.BuildError(t, errstr)
  183. except KeyboardInterrupt:
  184. raise
  185. except:
  186. self.exception_set()
  187. self.do_failed()
  188. else:
  189. print "scons: Nothing to be done for `%s'." % t
  190. SCons.Taskmaster.OutOfDateTask.executed(self)
  191. else:
  192. SCons.Taskmaster.OutOfDateTask.executed(self)
  193. def failed(self):
  194. # Handle the failure of a build task. The primary purpose here
  195. # is to display the various types of Errors and Exceptions
  196. # appropriately.
  197. exc_info = self.exc_info()
  198. try:
  199. t, e, tb = exc_info
  200. except ValueError:
  201. t, e = exc_info
  202. tb = None
  203. if t is None:
  204. # The Taskmaster didn't record an exception for this Task;
  205. # see if the sys module has one.
  206. try:
  207. t, e, tb = sys.exc_info()[:]
  208. except ValueError:
  209. t, e = exc_info
  210. tb = None
  211. # Deprecated string exceptions will have their string stored
  212. # in the first entry of the tuple.
  213. if e is None:
  214. e = t
  215. buildError = SCons.Errors.convert_to_BuildError(e)
  216. if not buildError.node:
  217. buildError.node = self.node
  218. node = buildError.node
  219. if not SCons.Util.is_List(node):
  220. node = [ node ]
  221. nodename = ', '.join(map(str, node))
  222. errfmt = "scons: *** [%s] %s\n"
  223. sys.stderr.write(errfmt % (nodename, buildError))
  224. if (buildError.exc_info[2] and buildError.exc_info[1] and
  225. not isinstance(
  226. buildError.exc_info[1],
  227. (EnvironmentError, SCons.Errors.StopError,
  228. SCons.Errors.UserError))):
  229. type, value, trace = buildError.exc_info
  230. traceback.print_exception(type, value, trace)
  231. elif tb and print_stacktrace:
  232. sys.stderr.write("scons: internal stack trace:\n")
  233. traceback.print_tb(tb, file=sys.stderr)
  234. self.exception = (e, buildError, tb) # type, value, traceback
  235. self.do_failed(buildError.exitstatus)
  236. self.exc_clear()
  237. def postprocess(self):
  238. if self.top:
  239. t = self.targets[0]
  240. for tp in self.options.tree_printers:
  241. tp.display(t)
  242. if self.options.debug_includes:
  243. tree = t.render_include_tree()
  244. if tree:
  245. print
  246. print tree
  247. SCons.Taskmaster.OutOfDateTask.postprocess(self)
  248. def make_ready(self):
  249. """Make a task ready for execution"""
  250. SCons.Taskmaster.OutOfDateTask.make_ready(self)
  251. if self.out_of_date and self.options.debug_explain:
  252. explanation = self.out_of_date[0].explain()
  253. if explanation:
  254. sys.stdout.write("scons: " + explanation)
  255. class CleanTask(SCons.Taskmaster.AlwaysTask):
  256. """An SCons clean task."""
  257. def fs_delete(self, path, pathstr, remove=1):
  258. try:
  259. if os.path.lexists(path):
  260. if os.path.isfile(path) or os.path.islink(path):
  261. if remove: os.unlink(path)
  262. display("Removed " + pathstr)
  263. elif os.path.isdir(path) and not os.path.islink(path):
  264. # delete everything in the dir
  265. for e in sorted(os.listdir(path)):
  266. p = os.path.join(path, e)
  267. s = os.path.join(pathstr, e)
  268. if os.path.isfile(p):
  269. if remove: os.unlink(p)
  270. display("Removed " + s)
  271. else:
  272. self.fs_delete(p, s, remove)
  273. # then delete dir itself
  274. if remove: os.rmdir(path)
  275. display("Removed directory " + pathstr)
  276. else:
  277. errstr = "Path '%s' exists but isn't a file or directory."
  278. raise SCons.Errors.UserError(errstr % (pathstr))
  279. except SCons.Errors.UserError, e:
  280. print e
  281. except (IOError, OSError), e:
  282. print "scons: Could not remove '%s':" % pathstr, e.strerror
  283. def show(self):
  284. target = self.targets[0]
  285. if (target.has_builder() or target.side_effect) and not target.noclean:
  286. for t in self.targets:
  287. if not t.isdir():
  288. display("Removed " + str(t))
  289. if target in SCons.Environment.CleanTargets:
  290. files = SCons.Environment.CleanTargets[target]
  291. for f in files:
  292. self.fs_delete(f.abspath, str(f), 0)
  293. def remove(self):
  294. target = self.targets[0]
  295. if (target.has_builder() or target.side_effect) and not target.noclean:
  296. for t in self.targets:
  297. try:
  298. removed = t.remove()
  299. except OSError, e:
  300. # An OSError may indicate something like a permissions
  301. # issue, an IOError would indicate something like
  302. # the file not existing. In either case, print a
  303. # message and keep going to try to remove as many
  304. # targets aa possible.
  305. print "scons: Could not remove '%s':" % str(t), e.strerror
  306. else:
  307. if removed:
  308. display("Removed " + str(t))
  309. if target in SCons.Environment.CleanTargets:
  310. files = SCons.Environment.CleanTargets[target]
  311. for f in files:
  312. self.fs_delete(f.abspath, str(f))
  313. execute = remove
  314. # We want the Taskmaster to update the Node states (and therefore
  315. # handle reference counts, etc.), but we don't want to call
  316. # back to the Node's post-build methods, which would do things
  317. # we don't want, like store .sconsign information.
  318. executed = SCons.Taskmaster.Task.executed_without_callbacks
  319. # Have the taskmaster arrange to "execute" all of the targets, because
  320. # we'll figure out ourselves (in remove() or show() above) whether
  321. # anything really needs to be done.
  322. make_ready = SCons.Taskmaster.Task.make_ready_all
  323. def prepare(self):
  324. pass
  325. class QuestionTask(SCons.Taskmaster.AlwaysTask):
  326. """An SCons task for the -q (question) option."""
  327. def prepare(self):
  328. pass
  329. def execute(self):
  330. if self.targets[0].get_state() != SCons.Node.up_to_date or \
  331. (self.top and not self.targets[0].exists()):
  332. global exit_status
  333. global this_build_status
  334. exit_status = 1
  335. this_build_status = 1
  336. self.tm.stop()
  337. def executed(self):
  338. pass
  339. class TreePrinter(object):
  340. def __init__(self, derived=False, prune=False, status=False):
  341. self.derived = derived
  342. self.prune = prune
  343. self.status = status
  344. def get_all_children(self, node):
  345. return node.all_children()
  346. def get_derived_children(self, node):
  347. children = node.all_children(None)
  348. return [x for x in children if x.has_builder()]
  349. def display(self, t):
  350. if self.derived:
  351. func = self.get_derived_children
  352. else:
  353. func = self.get_all_children
  354. s = self.status and 2 or 0
  355. SCons.Util.print_tree(t, func, prune=self.prune, showtags=s)
  356. def python_version_string():
  357. return sys.version.split()[0]
  358. def python_version_unsupported(version=sys.version_info):
  359. return version < unsupported_python_version
  360. def python_version_deprecated(version=sys.version_info):
  361. return version < deprecated_python_version
  362. # Global variables
  363. print_objects = 0
  364. print_memoizer = 0
  365. print_stacktrace = 0
  366. print_time = 0
  367. sconscript_time = 0
  368. cumulative_command_time = 0
  369. exit_status = 0 # final exit status, assume success by default
  370. this_build_status = 0 # "exit status" of an individual build
  371. num_jobs = None
  372. delayed_warnings = []
  373. class FakeOptionParser(object):
  374. """
  375. A do-nothing option parser, used for the initial OptionsParser variable.
  376. During normal SCons operation, the OptionsParser is created right
  377. away by the main() function. Certain tests scripts however, can
  378. introspect on different Tool modules, the initialization of which
  379. can try to add a new, local option to an otherwise uninitialized
  380. OptionsParser object. This allows that introspection to happen
  381. without blowing up.
  382. """
  383. class FakeOptionValues(object):
  384. def __getattr__(self, attr):
  385. return None
  386. values = FakeOptionValues()
  387. def add_local_option(self, *args, **kw):
  388. pass
  389. OptionsParser = FakeOptionParser()
  390. def AddOption(*args, **kw):
  391. if 'default' not in kw:
  392. kw['default'] = None
  393. result = OptionsParser.add_local_option(*args, **kw)
  394. return result
  395. def GetOption(name):
  396. return getattr(OptionsParser.values, name)
  397. def SetOption(name, value):
  398. return OptionsParser.values.set_option(name, value)
  399. #
  400. class Stats(object):
  401. def __init__(self):
  402. self.stats = []
  403. self.labels = []
  404. self.append = self.do_nothing
  405. self.print_stats = self.do_nothing
  406. def enable(self, outfp):
  407. self.outfp = outfp
  408. self.append = self.do_append
  409. self.print_stats = self.do_print
  410. def do_nothing(self, *args, **kw):
  411. pass
  412. class CountStats(Stats):
  413. def do_append(self, label):
  414. self.labels.append(label)
  415. self.stats.append(SCons.Debug.fetchLoggedInstances())
  416. def do_print(self):
  417. stats_table = {}
  418. for s in self.stats:
  419. for n in [t[0] for t in s]:
  420. stats_table[n] = [0, 0, 0, 0]
  421. i = 0
  422. for s in self.stats:
  423. for n, c in s:
  424. stats_table[n][i] = c
  425. i = i + 1
  426. self.outfp.write("Object counts:\n")
  427. pre = [" "]
  428. post = [" %s\n"]
  429. l = len(self.stats)
  430. fmt1 = ''.join(pre + [' %7s']*l + post)
  431. fmt2 = ''.join(pre + [' %7d']*l + post)
  432. labels = self.labels[:l]
  433. labels.append(("", "Class"))
  434. self.outfp.write(fmt1 % tuple([x[0] for x in labels]))
  435. self.outfp.write(fmt1 % tuple([x[1] for x in labels]))
  436. for k in sorted(stats_table.keys()):
  437. r = stats_table[k][:l] + [k]
  438. self.outfp.write(fmt2 % tuple(r))
  439. count_stats = CountStats()
  440. class MemStats(Stats):
  441. def do_append(self, label):
  442. self.labels.append(label)
  443. self.stats.append(SCons.Debug.memory())
  444. def do_print(self):
  445. fmt = 'Memory %-32s %12d\n'
  446. for label, stats in zip(self.labels, self.stats):
  447. self.outfp.write(fmt % (label, stats))
  448. memory_stats = MemStats()
  449. # utility functions
  450. def _scons_syntax_error(e):
  451. """Handle syntax errors. Print out a message and show where the error
  452. occurred.
  453. """
  454. etype, value, tb = sys.exc_info()
  455. lines = traceback.format_exception_only(etype, value)
  456. for line in lines:
  457. sys.stderr.write(line+'\n')
  458. sys.exit(2)
  459. def find_deepest_user_frame(tb):
  460. """
  461. Find the deepest stack frame that is not part of SCons.
  462. Input is a "pre-processed" stack trace in the form
  463. returned by traceback.extract_tb() or traceback.extract_stack()
  464. """
  465. tb.reverse()
  466. # find the deepest traceback frame that is not part
  467. # of SCons:
  468. for frame in tb:
  469. filename = frame[0]
  470. if filename.find(os.sep+'SCons'+os.sep) == -1:
  471. return frame
  472. return tb[0]
  473. def _scons_user_error(e):
  474. """Handle user errors. Print out a message and a description of the
  475. error, along with the line number and routine where it occured.
  476. The file and line number will be the deepest stack frame that is
  477. not part of SCons itself.
  478. """
  479. global print_stacktrace
  480. etype, value, tb = sys.exc_info()
  481. if print_stacktrace:
  482. traceback.print_exception(etype, value, tb)
  483. filename, lineno, routine, dummy = find_deepest_user_frame(traceback.extract_tb(tb))
  484. sys.stderr.write("\nscons: *** %s\n" % value)
  485. sys.stderr.write('File "%s", line %d, in %s\n' % (filename, lineno, routine))
  486. sys.exit(2)
  487. def _scons_user_warning(e):
  488. """Handle user warnings. Print out a message and a description of
  489. the warning, along with the line number and routine where it occured.
  490. The file and line number will be the deepest stack frame that is
  491. not part of SCons itself.
  492. """
  493. etype, value, tb = sys.exc_info()
  494. filename, lineno, routine, dummy = find_deepest_user_frame(traceback.extract_tb(tb))
  495. sys.stderr.write("\nscons: warning: %s\n" % e)
  496. sys.stderr.write('File "%s", line %d, in %s\n' % (filename, lineno, routine))
  497. def _scons_internal_warning(e):
  498. """Slightly different from _scons_user_warning in that we use the
  499. *current call stack* rather than sys.exc_info() to get our stack trace.
  500. This is used by the warnings framework to print warnings."""
  501. filename, lineno, routine, dummy = find_deepest_user_frame(traceback.extract_stack())
  502. sys.stderr.write("\nscons: warning: %s\n" % e.args[0])
  503. sys.stderr.write('File "%s", line %d, in %s\n' % (filename, lineno, routine))
  504. def _scons_internal_error():
  505. """Handle all errors but user errors. Print out a message telling
  506. the user what to do in this case and print a normal trace.
  507. """
  508. print 'internal error'
  509. traceback.print_exc()
  510. sys.exit(2)
  511. def _SConstruct_exists(dirname='', repositories=[], filelist=None):
  512. """This function checks that an SConstruct file exists in a directory.
  513. If so, it returns the path of the file. By default, it checks the
  514. current directory.
  515. """
  516. if not filelist:
  517. filelist = ['SConstruct', 'Sconstruct', 'sconstruct']
  518. for file in filelist:
  519. sfile = os.path.join(dirname, file)
  520. if os.path.isfile(sfile):
  521. return sfile
  522. if not os.path.isabs(sfile):
  523. for rep in repositories:
  524. if os.path.isfile(os.path.join(rep, sfile)):
  525. return sfile
  526. return None
  527. def _set_debug_values(options):
  528. global print_memoizer, print_objects, print_stacktrace, print_time
  529. debug_values = options.debug
  530. if "count" in debug_values:
  531. # All of the object counts are within "if __debug__:" blocks,
  532. # which get stripped when running optimized (with python -O or
  533. # from compiled *.pyo files). Provide a warning if __debug__ is
  534. # stripped, so it doesn't just look like --debug=count is broken.
  535. enable_count = False
  536. if __debug__: enable_count = True
  537. if enable_count:
  538. count_stats.enable(sys.stdout)
  539. else:
  540. msg = "--debug=count is not supported when running SCons\n" + \
  541. "\twith the python -O option or optimized (.pyo) modules."
  542. SCons.Warnings.warn(SCons.Warnings.NoObjectCountWarning, msg)
  543. if "dtree" in debug_values:
  544. options.tree_printers.append(TreePrinter(derived=True))
  545. options.debug_explain = ("explain" in debug_values)
  546. if "findlibs" in debug_values:
  547. SCons.Scanner.Prog.print_find_libs = "findlibs"
  548. options.debug_includes = ("includes" in debug_values)
  549. print_memoizer = ("memoizer" in debug_values)
  550. if "memory" in debug_values:
  551. memory_stats.enable(sys.stdout)
  552. print_objects = ("objects" in debug_values)
  553. if "presub" in debug_values:
  554. SCons.Action.print_actions_presub = 1
  555. if "stacktrace" in debug_values:
  556. print_stacktrace = 1
  557. if "stree" in debug_values:
  558. options.tree_printers.append(TreePrinter(status=True))
  559. if "time" in debug_values:
  560. print_time = 1
  561. if "tree" in debug_values:
  562. options.tree_printers.append(TreePrinter())
  563. def _create_path(plist):
  564. path = '.'
  565. for d in plist:
  566. if os.path.isabs(d):
  567. path = d
  568. else:
  569. path = path + '/' + d
  570. return path
  571. def _load_site_scons_dir(topdir, site_dir_name=None):
  572. """Load the site_scons dir under topdir.
  573. Adds site_scons to sys.path, imports site_scons/site_init.py,
  574. and adds site_scons/site_tools to default toolpath."""
  575. if site_dir_name:
  576. err_if_not_found = True # user specified: err if missing
  577. else:
  578. site_dir_name = "site_scons"
  579. err_if_not_found = False
  580. site_dir = os.path.join(topdir.path, site_dir_name)
  581. if not os.path.exists(site_dir):
  582. if err_if_not_found:
  583. raise SCons.Errors.UserError("site dir %s not found."%site_dir)
  584. return
  585. site_init_filename = "site_init.py"
  586. site_init_modname = "site_init"
  587. site_tools_dirname = "site_tools"
  588. sys.path = [os.path.abspath(site_dir)] + sys.path
  589. site_init_file = os.path.join(site_dir, site_init_filename)
  590. site_tools_dir = os.path.join(site_dir, site_tools_dirname)
  591. if os.path.exists(site_init_file):
  592. import imp
  593. # TODO(2.4): turn this into try:-except:-finally:
  594. try:
  595. try:
  596. fp, pathname, description = imp.find_module(site_init_modname,
  597. [site_dir])
  598. # Load the file into SCons.Script namespace. This is
  599. # opaque and clever; m is the module object for the
  600. # SCons.Script module, and the exec ... in call executes a
  601. # file (or string containing code) in the context of the
  602. # module's dictionary, so anything that code defines ends
  603. # up adding to that module. This is really short, but all
  604. # the error checking makes it longer.
  605. try:
  606. m = sys.modules['SCons.Script']
  607. except Exception, e:
  608. fmt = 'cannot import site_init.py: missing SCons.Script module %s'
  609. raise SCons.Errors.InternalError(fmt % repr(e))
  610. try:
  611. # This is the magic.
  612. exec fp in m.__dict__
  613. except KeyboardInterrupt:
  614. raise
  615. except Exception, e:
  616. fmt = '*** Error loading site_init file %s:\n'
  617. sys.stderr.write(fmt % repr(site_init_file))
  618. raise
  619. except KeyboardInterrupt:
  620. raise
  621. except ImportError, e:
  622. fmt = '*** cannot import site init file %s:\n'
  623. sys.stderr.write(fmt % repr(site_init_file))
  624. raise
  625. finally:
  626. if fp:
  627. fp.close()
  628. if os.path.exists(site_tools_dir):
  629. SCons.Tool.DefaultToolpath.append(os.path.abspath(site_tools_dir))
  630. def version_string(label, module):
  631. version = module.__version__
  632. build = module.__build__
  633. if build:
  634. if build[0] != '.':
  635. build = '.' + build
  636. version = version + build
  637. fmt = "\t%s: v%s, %s, by %s on %s\n"
  638. return fmt % (label,
  639. version,
  640. module.__date__,
  641. module.__developer__,
  642. module.__buildsys__)
  643. def _main(parser):
  644. global exit_status
  645. global this_build_status
  646. options = parser.values
  647. # Here's where everything really happens.
  648. # First order of business: set up default warnings and then
  649. # handle the user's warning options, so that we can issue (or
  650. # suppress) appropriate warnings about anything that might happen,
  651. # as configured by the user.
  652. default_warnings = [ SCons.Warnings.WarningOnByDefault,
  653. SCons.Warnings.DeprecatedWarning,
  654. ]
  655. for warning in default_warnings:
  656. SCons.Warnings.enableWarningClass(warning)
  657. SCons.Warnings._warningOut = _scons_internal_warning
  658. SCons.Warnings.process_warn_strings(options.warn)
  659. # Now that we have the warnings configuration set up, we can actually
  660. # issue (or suppress) any warnings about warning-worthy things that
  661. # occurred while the command-line options were getting parsed.
  662. try:
  663. dw = options.delayed_warnings
  664. except AttributeError:
  665. pass
  666. else:
  667. delayed_warnings.extend(dw)
  668. for warning_type, message in delayed_warnings:
  669. SCons.Warnings.warn(warning_type, message)
  670. if options.diskcheck:
  671. SCons.Node.FS.set_diskcheck(options.diskcheck)
  672. # Next, we want to create the FS object that represents the outside
  673. # world's file system, as that's central to a lot of initialization.
  674. # To do this, however, we need to be in the directory from which we
  675. # want to start everything, which means first handling any relevant
  676. # options that might cause us to chdir somewhere (-C, -D, -U, -u).
  677. if options.directory:
  678. script_dir = os.path.abspath(_create_path(options.directory))
  679. else:
  680. script_dir = os.getcwd()
  681. target_top = None
  682. if options.climb_up:
  683. target_top = '.' # directory to prepend to targets
  684. while script_dir and not _SConstruct_exists(script_dir,
  685. options.repository,
  686. options.file):
  687. script_dir, last_part = os.path.split(script_dir)
  688. if last_part:
  689. target_top = os.path.join(last_part, target_top)
  690. else:
  691. script_dir = ''
  692. if script_dir and script_dir != os.getcwd():
  693. display("scons: Entering directory `%s'" % script_dir)
  694. try:
  695. os.chdir(script_dir)
  696. except OSError:
  697. sys.stderr.write("Could not change directory to %s\n" % script_dir)
  698. # Now that we're in the top-level SConstruct directory, go ahead
  699. # and initialize the FS object that represents the file system,
  700. # and make it the build engine default.
  701. fs = SCons.Node.FS.get_default_fs()
  702. for rep in options.repository:
  703. fs.Repository(rep)
  704. # Now that we have the FS object, the next order of business is to
  705. # check for an SConstruct file (or other specified config file).
  706. # If there isn't one, we can bail before doing any more work.
  707. scripts = []
  708. if options.file:
  709. scripts.extend(options.file)
  710. if not scripts:
  711. sfile = _SConstruct_exists(repositories=options.repository,
  712. filelist=options.file)
  713. if sfile:
  714. scripts.append(sfile)
  715. if not scripts:
  716. if options.help:
  717. # There's no SConstruct, but they specified -h.
  718. # Give them the options usage now, before we fail
  719. # trying to read a non-existent SConstruct file.
  720. raise SConsPrintHelpException
  721. raise SCons.Errors.UserError("No SConstruct file found.")
  722. if scripts[0] == "-":
  723. d = fs.getcwd()
  724. else:
  725. d = fs.File(scripts[0]).dir
  726. fs.set_SConstruct_dir(d)
  727. _set_debug_values(options)
  728. SCons.Node.implicit_cache = options.implicit_cache
  729. SCons.Node.implicit_deps_changed = options.implicit_deps_changed
  730. SCons.Node.implicit_deps_unchanged = options.implicit_deps_unchanged
  731. if options.no_exec:
  732. SCons.SConf.dryrun = 1
  733. SCons.Action.execute_actions = None
  734. if options.question:
  735. SCons.SConf.dryrun = 1
  736. if options.clean:
  737. SCons.SConf.SetBuildType('clean')
  738. if options.help:
  739. SCons.SConf.SetBuildType('help')
  740. SCons.SConf.SetCacheMode(options.config)
  741. SCons.SConf.SetProgressDisplay(progress_display)
  742. if options.no_progress or options.silent:
  743. progress_display.set_mode(0)
  744. if options.site_dir:
  745. _load_site_scons_dir(d, options.site_dir)
  746. elif not options.no_site_dir:
  747. _load_site_scons_dir(d)
  748. if options.include_dir:
  749. sys.path = options.include_dir + sys.path
  750. # That should cover (most of) the options. Next, set up the variables
  751. # that hold command-line arguments, so the SConscript files that we
  752. # read and execute have access to them.
  753. targets = []
  754. xmit_args = []
  755. for a in parser.largs:
  756. if a[:1] == '-':
  757. continue
  758. if '=' in a:
  759. xmit_args.append(a)
  760. else:
  761. targets.append(a)
  762. SCons.Script._Add_Targets(targets + parser.rargs)
  763. SCons.Script._Add_Arguments(xmit_args)
  764. # If stdout is not a tty, replace it with a wrapper object to call flush
  765. # after every write.
  766. #
  767. # Tty devices automatically flush after every newline, so the replacement
  768. # isn't necessary. Furthermore, if we replace sys.stdout, the readline
  769. # module will no longer work. This affects the behavior during
  770. # --interactive mode. --interactive should only be used when stdin and
  771. # stdout refer to a tty.
  772. if not hasattr(sys.stdout, 'isatty') or not sys.stdout.isatty():
  773. sys.stdout = SCons.Util.Unbuffered(sys.stdout)
  774. if not hasattr(sys.stderr, 'isatty') or not sys.stderr.isatty():
  775. sys.stderr = SCons.Util.Unbuffered(sys.stderr)
  776. memory_stats.append('before reading SConscript files:')
  777. count_stats.append(('pre-', 'read'))
  778. # And here's where we (finally) read the SConscript files.
  779. progress_display("scons: Reading SConscript files ...")
  780. start_time = time.time()
  781. try:
  782. for script in scripts:
  783. SCons.Script._SConscript._SConscript(fs, script)
  784. except SCons.Errors.StopError, e:
  785. # We had problems reading an SConscript file, such as it
  786. # couldn't be copied in to the VariantDir. Since we're just
  787. # reading SConscript files and haven't started building
  788. # things yet, stop regardless of whether they used -i or -k
  789. # or anything else.
  790. sys.stderr.write("scons: *** %s Stop.\n" % e)
  791. exit_status = 2
  792. sys.exit(exit_status)
  793. global sconscript_time
  794. sconscript_time = time.time() - start_time
  795. progress_display("scons: done reading SConscript files.")
  796. memory_stats.append('after reading SConscript files:')
  797. count_stats.append(('post-', 'read'))
  798. # Re-{enable,disable} warnings in case they disabled some in
  799. # the SConscript file.
  800. #
  801. # We delay enabling the PythonVersionWarning class until here so that,
  802. # if they explicity disabled it in either in the command line or in
  803. # $SCONSFLAGS, or in the SConscript file, then the search through
  804. # the list of deprecated warning classes will find that disabling
  805. # first and not issue the warning.
  806. #SCons.Warnings.enableWarningClass(SCons.Warnings.PythonVersionWarning)
  807. SCons.Warnings.process_warn_strings(options.warn)
  808. # Now that we've read the SConscript files, we can check for the
  809. # warning about deprecated Python versions--delayed until here
  810. # in case they disabled the warning in the SConscript files.
  811. if python_version_deprecated():
  812. msg = "Support for pre-2.4 Python (%s) is deprecated.\n" + \
  813. " If this will cause hardship, contact dev@scons.tigris.org."
  814. SCons.Warnings.warn(SCons.Warnings.PythonVersionWarning,
  815. msg % python_version_string())
  816. if not options.help:
  817. SCons.SConf.CreateConfigHBuilder(SCons.Defaults.DefaultEnvironment())
  818. # Now re-parse the command-line options (any to the left of a '--'
  819. # argument, that is) with any user-defined command-line options that
  820. # the SConscript files may have added to the parser object. This will
  821. # emit the appropriate error message and exit if any unknown option
  822. # was specified on the command line.
  823. parser.preserve_unknown_options = False
  824. parser.parse_args(parser.largs, options)
  825. if options.help:
  826. help_text = SCons.Script.help_text
  827. if help_text is None:
  828. # They specified -h, but there was no Help() inside the
  829. # SConscript files. Give them the options usage.
  830. raise SConsPrintHelpException
  831. else:
  832. print help_text
  833. print "Use scons -H for help about command-line options."
  834. exit_status = 0
  835. return
  836. # Change directory to the top-level SConstruct directory, then tell
  837. # the Node.FS subsystem that we're all done reading the SConscript
  838. # files and calling Repository() and VariantDir() and changing
  839. # directories and the like, so it can go ahead and start memoizing
  840. # the string values of file system nodes.
  841. fs.chdir(fs.Top)
  842. SCons.Node.FS.save_strings(1)
  843. # Now that we've read the SConscripts we can set the options
  844. # that are SConscript settable:
  845. SCons.Node.implicit_cache = options.implicit_cache
  846. SCons.Node.FS.set_duplicate(options.duplicate)
  847. fs.set_max_drift(options.max_drift)
  848. SCons.Job.explicit_stack_size = options.stack_size
  849. if options.md5_chunksize:
  850. SCons.Node.FS.File.md5_chunksize = options.md5_chunksize
  851. platform = SCons.Platform.platform_module()
  852. if options.interactive:
  853. SCons.Script.Interactive.interact(fs, OptionsParser, options,
  854. targets, target_top)
  855. else:
  856. # Build the targets
  857. nodes = _build_targets(fs, options, targets, target_top)
  858. if not nodes:
  859. exit_status = 2
  860. def _build_targets(fs, options, targets, target_top):
  861. global this_build_status
  862. this_build_status = 0
  863. progress_display.set_mode(not (options.no_progress or options.silent))
  864. display.set_mode(not options.silent)
  865. SCons.Action.print_actions = not options.silent
  866. SCons.Action.execute_actions = not options.no_exec
  867. SCons.Node.FS.do_store_info = not options.no_exec
  868. SCons.SConf.dryrun = options.no_exec
  869. if options.diskcheck:
  870. SCons.Node.FS.set_diskcheck(options.diskcheck)
  871. SCons.CacheDir.cache_enabled = not options.cache_disable
  872. SCons.CacheDir.cache_debug = options.cache_debug
  873. SCons.CacheDir.cache_force = options.cache_force
  874. SCons.CacheDir.cache_show = options.cache_show
  875. if options.no_exec:
  876. CleanTask.execute = CleanTask.show
  877. else:
  878. CleanTask.execute = CleanTask.remove
  879. lookup_top = None
  880. if targets or SCons.Script.BUILD_TARGETS != SCons.Script._build_plus_default:
  881. # They specified targets on the command line or modified
  882. # BUILD_TARGETS in the SConscript file(s), so if they used -u,
  883. # -U or -D, we have to look up targets relative to the top,
  884. # but we build whatever they specified.
  885. if target_top:
  886. lookup_top = fs.Dir(target_top)
  887. target_top = None
  888. targets = SCons.Script.BUILD_TARGETS
  889. else:
  890. # There are no targets specified on the command line,
  891. # so if they used -u, -U or -D, we may have to restrict
  892. # what actually gets built.
  893. d = None
  894. if target_top:
  895. if options.climb_up == 1:
  896. # -u, local directory and below
  897. target_top = fs.Dir(target_top)
  898. lookup_top = target_top
  899. elif options.climb_up == 2:
  900. # -D, all Default() targets
  901. target_top = None
  902. lookup_top = None
  903. elif options.climb_up == 3:
  904. # -U, local SConscript Default() targets
  905. target_top = fs.Dir(target_top)
  906. def check_dir(x, target_top=target_top):
  907. if hasattr(x, 'cwd') and not x.cwd is None:
  908. cwd = x.cwd.srcnode()
  909. return cwd == target_top
  910. else:
  911. # x doesn't have a cwd, so it's either not a target,
  912. # or not a file, so go ahead and keep it as a default
  913. # target and let the engine sort it out:
  914. return 1
  915. d = list(filter(check_dir, SCons.Script.DEFAULT_TARGETS))
  916. SCons.Script.DEFAULT_TARGETS[:] = d
  917. target_top = None
  918. lookup_top = None
  919. targets = SCons.Script._Get_Default_Targets(d, fs)
  920. if not targets:
  921. sys.stderr.write("scons: *** No targets specified and no Default() targets found. Stop.\n")
  922. return None
  923. def Entry(x, ltop=lookup_top, ttop=target_top, fs=fs):
  924. if isinstance(x, SCons.Node.Node):
  925. node = x
  926. else:
  927. node = None
  928. # Why would ltop be None? Unfortunately this happens.
  929. if ltop is None: ltop = ''
  930. # Curdir becomes important when SCons is called with -u, -C,
  931. # or similar option that changes directory, and so the paths
  932. # of targets given on the command line need to be adjusted.
  933. curdir = os.path.join(os.getcwd(), str(ltop))
  934. for lookup in SCons.Node.arg2nodes_lookups:
  935. node = lookup(x, curdir=curdir)
  936. if node is not None:
  937. break
  938. if node is None:
  939. node = fs.Entry(x, directory=ltop, create=1)
  940. if ttop and not node.is_under(ttop):
  941. if isinstance(node, SCons.Node.FS.Dir) and ttop.is_under(node):
  942. node = ttop
  943. else:
  944. node = None
  945. return node
  946. nodes = [_f for _f in map(Entry, targets) if _f]
  947. task_class = BuildTask # default action is to build targets
  948. opening_message = "Building targets ..."
  949. closing_message = "done building targets."
  950. if options.keep_going:
  951. failure_message = "done building targets (errors occurred during build)."
  952. else:
  953. failure_message = "building terminated because of errors."
  954. if options.question:
  955. task_class = QuestionTask
  956. try:
  957. if options.clean:
  958. task_class = CleanTask
  959. opening_message = "Cleaning targets ..."
  960. closing_message = "done cleaning targets."
  961. if options.keep_going:
  962. failure_message = "done cleaning targets (errors occurred during clean)."
  963. else:
  964. failure_message = "cleaning terminated because of errors."
  965. except AttributeError:
  966. pass
  967. task_class.progress = ProgressObject
  968. if options.random:
  969. def order(dependencies):
  970. """Randomize the dependencies."""
  971. import random
  972. # This is cribbed from the implementation of
  973. # random.shuffle() in Python 2.X.
  974. d = dependencies
  975. for i in range(len(d)-1, 0, -1):
  976. j = int(random.random() * (i+1))
  977. d[i], d[j] = d[j], d[i]
  978. return d
  979. else:
  980. def order(dependencies):
  981. """Leave the order of dependencies alone."""
  982. return dependencies
  983. if options.taskmastertrace_file == '-':
  984. tmtrace = sys.stdout
  985. elif options.taskmastertrace_file:
  986. tmtrace = open(options.taskmastertrace_file, 'wb')
  987. else:
  988. tmtrace = None
  989. taskmaster = SCons.Taskmaster.Taskmaster(nodes, task_class, order, tmtrace)
  990. # Let the BuildTask objects get at the options to respond to the
  991. # various print_* settings, tree_printer list, etc.
  992. BuildTask.options = options
  993. global num_jobs
  994. num_jobs = options.num_jobs
  995. jobs = SCons.Job.Jobs(num_jobs, taskmaster)
  996. if num_jobs > 1:
  997. msg = None
  998. if jobs.num_jobs == 1:
  999. msg = "parallel builds are unsupported by this version of Python;\n" + \
  1000. "\tignoring -j or num_jobs option.\n"
  1001. elif sys.platform == 'win32':
  1002. msg = fetch_win32_parallel_msg()
  1003. if msg:
  1004. SCons.Warnings.warn(SCons.Warnings.NoParallelSupportWarning, msg)
  1005. memory_stats.append('before building targets:')
  1006. count_stats.append(('pre-', 'build'))
  1007. def jobs_postfunc(
  1008. jobs=jobs,
  1009. options=options,
  1010. closing_message=closing_message,
  1011. failure_message=failure_message
  1012. ):
  1013. if jobs.were_interrupted():
  1014. if not options.no_progress and not options.silent:
  1015. sys.stderr.write("scons: Build interrupted.\n")
  1016. global exit_status
  1017. global this_build_status
  1018. exit_status = 2
  1019. this_build_status = 2
  1020. if this_build_status:
  1021. progress_display("scons: " + failure_message)
  1022. else:
  1023. progress_display("scons: " + closing_message)
  1024. if not options.no_exec:
  1025. if jobs.were_interrupted():
  1026. progress_display("scons: writing .sconsign file.")
  1027. SCons.SConsign.write()
  1028. progress_display("scons: " + opening_message)
  1029. jobs.run(postfunc = jobs_postfunc)
  1030. memory_stats.append('after building targets:')
  1031. count_stats.append(('post-', 'build'))
  1032. return nodes
  1033. def _exec_main(parser, values):
  1034. sconsflags = os.environ.get('SCONSFLAGS', '')
  1035. all_args = sconsflags.split() + sys.argv[1:]
  1036. options, args = parser.parse_args(all_args, values)
  1037. if isinstance(options.debug, list) and "pdb" in options.debug:
  1038. import pdb
  1039. pdb.Pdb().runcall(_main, parser)
  1040. elif options.profile_file:
  1041. # compat layer imports "cProfile" for us if it's available.
  1042. from profile import Profile
  1043. # Some versions of Python 2.4 shipped a profiler that had the
  1044. # wrong 'c_exception' entry in its dispatch table. Make sure
  1045. # we have the right one. (This may put an unnecessary entry
  1046. # in the table in earlier versions of Python, but its presence
  1047. # shouldn't hurt anything).
  1048. try:
  1049. dispatch = Profile.dispatch
  1050. except AttributeError:
  1051. pass
  1052. else:
  1053. dispatch['c_exception'] = Profile.trace_dispatch_return
  1054. prof = Profile()
  1055. try:
  1056. prof.runcall(_main, parser)
  1057. except SConsPrintHelpException, e:
  1058. prof.dump_stats(options.profile_file)
  1059. raise e
  1060. except SystemExit:
  1061. pass
  1062. prof.dump_stats(options.profile_file)
  1063. else:
  1064. _main(parser)
  1065. def main():
  1066. global OptionsParser
  1067. global exit_status
  1068. global first_command_start
  1069. # Check up front for a Python version we do not support. We
  1070. # delay the check for deprecated Python versions until later,
  1071. # after the SConscript files have been read, in case they
  1072. # disable that warning.
  1073. if python_version_unsupported():
  1074. msg = "scons: *** SCons version %s does not run under Python version %s.\n"
  1075. sys.stderr.write(msg % (SCons.__version__, python_version_string()))
  1076. sys.exit(1)
  1077. parts = ["SCons by Steven Knight et al.:\n"]
  1078. try:
  1079. import __main__
  1080. parts.append(version_string("script", __main__))
  1081. except (ImportError, AttributeError):
  1082. # On Windows there is no scons.py, so there is no
  1083. # __main__.__version__, hence there is no script version.
  1084. pass
  1085. parts.append(version_string("engine", SCons))
  1086. parts.append("Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation")
  1087. version = ''.join(parts)
  1088. import SConsOptions
  1089. parser = SConsOptions.Parser(version)
  1090. values = SConsOptions.SConsValues(parser.get_default_values())
  1091. OptionsParser = parser
  1092. try:
  1093. _exec_main(parser, values)
  1094. except SystemExit, s:
  1095. if s:
  1096. exit_status = s
  1097. except KeyboardInterrupt:
  1098. print("scons: Build interrupted.")
  1099. sys.exit(2)
  1100. except SyntaxError, e:
  1101. _scons_syntax_error(e)
  1102. except SCons.Errors.InternalError:
  1103. _scons_internal_error()
  1104. except SCons.Errors.UserError, e:
  1105. _scons_user_error(e)
  1106. except SConsPrintHelpException:
  1107. parser.print_help()
  1108. exit_status = 0
  1109. except SCons.Errors.BuildError, e:
  1110. exit_status = e.exitstatus
  1111. except:
  1112. # An exception here is likely a builtin Python exception Python
  1113. # code in an SConscript file. Show them precisely what the
  1114. # problem was and where it happened.
  1115. SCons.Script._SConscript.SConscript_exception()
  1116. sys.exit(2)
  1117. memory_stats.print_stats()
  1118. count_stats.print_stats()
  1119. if print_objects:
  1120. SCons.Debug.listLoggedInstances('*')
  1121. #SCons.Debug.dumpLoggedInstances('*')
  1122. if print_memoizer:
  1123. SCons.Memoize.Dump("Memoizer (memory cache) hits and misses:")
  1124. # Dump any development debug info that may have been enabled.
  1125. # These are purely for internal debugging during development, so
  1126. # there's no need to control them with --debug= options; they're
  1127. # controlled by changing the source code.
  1128. SCons.Debug.dump_caller_counts()
  1129. SCons.Taskmaster.dump_stats()
  1130. if print_time:
  1131. total_time = time.time() - SCons.Script.start_time
  1132. if num_jobs == 1:
  1133. ct = cumulative_command_time
  1134. else:
  1135. if last_command_end is None or first_command_start is None:
  1136. ct = 0.0
  1137. else:
  1138. ct = last_command_end - first_command_start
  1139. scons_time = total_time - sconscript_time - ct
  1140. print "Total build time: %f seconds"%total_time
  1141. print "Total SConscript file execution time: %f seconds"%sconscript_time
  1142. print "Total SCons execution time: %f seconds"%scons_time
  1143. print "Total command execution time: %f seconds"%ct
  1144. sys.exit(exit_status)
  1145. # Local Variables:
  1146. # tab-width:4
  1147. # indent-tabs-mode:nil
  1148. # End:
  1149. # vim: set expandtab tabstop=4 shiftwidth=4: