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.

1053 lines
39 KiB

6 years ago
  1. """SCons.SConf
  2. Autoconf-like configuration support.
  3. In other words, SConf allows to run tests on the build machine to detect
  4. capabilities of system and do some things based on result: generate config
  5. files, header files for C/C++, update variables in environment.
  6. Tests on the build system can detect if compiler sees header files, if
  7. libraries are installed, if some command line options are supported etc.
  8. """
  9. #
  10. # Copyright (c) 2001 - 2017 The SCons Foundation
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining
  13. # a copy of this software and associated documentation files (the
  14. # "Software"), to deal in the Software without restriction, including
  15. # without limitation the rights to use, copy, modify, merge, publish,
  16. # distribute, sublicense, and/or sell copies of the Software, and to
  17. # permit persons to whom the Software is furnished to do so, subject to
  18. # the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included
  21. # in all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  24. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  25. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. #
  31. from __future__ import print_function
  32. __revision__ = "src/engine/SCons/SConf.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  33. import SCons.compat
  34. import io
  35. import os
  36. import re
  37. import sys
  38. import traceback
  39. import SCons.Action
  40. import SCons.Builder
  41. import SCons.Errors
  42. import SCons.Job
  43. import SCons.Node.FS
  44. import SCons.Taskmaster
  45. import SCons.Util
  46. import SCons.Warnings
  47. import SCons.Conftest
  48. from SCons.Debug import Trace
  49. # Turn off the Conftest error logging
  50. SCons.Conftest.LogInputFiles = 0
  51. SCons.Conftest.LogErrorMessages = 0
  52. # Set
  53. build_type = None
  54. build_types = ['clean', 'help']
  55. def SetBuildType(type):
  56. global build_type
  57. build_type = type
  58. # to be set, if we are in dry-run mode
  59. dryrun = 0
  60. AUTO=0 # use SCons dependency scanning for up-to-date checks
  61. FORCE=1 # force all tests to be rebuilt
  62. CACHE=2 # force all tests to be taken from cache (raise an error, if necessary)
  63. cache_mode = AUTO
  64. def SetCacheMode(mode):
  65. """Set the Configure cache mode. mode must be one of "auto", "force",
  66. or "cache"."""
  67. global cache_mode
  68. if mode == "auto":
  69. cache_mode = AUTO
  70. elif mode == "force":
  71. cache_mode = FORCE
  72. elif mode == "cache":
  73. cache_mode = CACHE
  74. else:
  75. raise ValueError("SCons.SConf.SetCacheMode: Unknown mode " + mode)
  76. progress_display = SCons.Util.display # will be overwritten by SCons.Script
  77. def SetProgressDisplay(display):
  78. """Set the progress display to use (called from SCons.Script)"""
  79. global progress_display
  80. progress_display = display
  81. SConfFS = None
  82. _ac_build_counter = 0 # incremented, whenever TryBuild is called
  83. _ac_config_logs = {} # all config.log files created in this build
  84. _ac_config_hs = {} # all config.h files created in this build
  85. sconf_global = None # current sconf object
  86. def _createConfigH(target, source, env):
  87. t = open(str(target[0]), "w")
  88. defname = re.sub('[^A-Za-z0-9_]', '_', str(target[0]).upper())
  89. t.write("""#ifndef %(DEFNAME)s_SEEN
  90. #define %(DEFNAME)s_SEEN
  91. """ % {'DEFNAME' : defname})
  92. t.write(source[0].get_contents().decode())
  93. t.write("""
  94. #endif /* %(DEFNAME)s_SEEN */
  95. """ % {'DEFNAME' : defname})
  96. t.close()
  97. def _stringConfigH(target, source, env):
  98. return "scons: Configure: creating " + str(target[0])
  99. def NeedConfigHBuilder():
  100. if len(_ac_config_hs) == 0:
  101. return False
  102. else:
  103. return True
  104. def CreateConfigHBuilder(env):
  105. """Called if necessary just before the building targets phase begins."""
  106. action = SCons.Action.Action(_createConfigH,
  107. _stringConfigH)
  108. sconfigHBld = SCons.Builder.Builder(action=action)
  109. env.Append( BUILDERS={'SConfigHBuilder':sconfigHBld} )
  110. for k in list(_ac_config_hs.keys()):
  111. env.SConfigHBuilder(k, env.Value(_ac_config_hs[k]))
  112. class SConfWarning(SCons.Warnings.Warning):
  113. pass
  114. SCons.Warnings.enableWarningClass(SConfWarning)
  115. # some error definitions
  116. class SConfError(SCons.Errors.UserError):
  117. def __init__(self,msg):
  118. SCons.Errors.UserError.__init__(self,msg)
  119. class ConfigureDryRunError(SConfError):
  120. """Raised when a file or directory needs to be updated during a Configure
  121. process, but the user requested a dry-run"""
  122. def __init__(self,target):
  123. if not isinstance(target, SCons.Node.FS.File):
  124. msg = 'Cannot create configure directory "%s" within a dry-run.' % str(target)
  125. else:
  126. msg = 'Cannot update configure test "%s" within a dry-run.' % str(target)
  127. SConfError.__init__(self,msg)
  128. class ConfigureCacheError(SConfError):
  129. """Raised when a use explicitely requested the cache feature, but the test
  130. is run the first time."""
  131. def __init__(self,target):
  132. SConfError.__init__(self, '"%s" is not yet built and cache is forced.' % str(target))
  133. # define actions for building text files
  134. def _createSource( target, source, env ):
  135. fd = open(str(target[0]), "w")
  136. fd.write(source[0].get_contents().decode())
  137. fd.close()
  138. def _stringSource( target, source, env ):
  139. return (str(target[0]) + ' <-\n |' +
  140. source[0].get_contents().decode().replace( '\n', "\n |" ) )
  141. class SConfBuildInfo(SCons.Node.FS.FileBuildInfo):
  142. """
  143. Special build info for targets of configure tests. Additional members
  144. are result (did the builder succeed last time?) and string, which
  145. contains messages of the original build phase.
  146. """
  147. __slots__ = ('result', 'string')
  148. def __init__(self):
  149. self.result = None # -> 0/None -> no error, != 0 error
  150. self.string = None # the stdout / stderr output when building the target
  151. def set_build_result(self, result, string):
  152. self.result = result
  153. self.string = string
  154. class Streamer(object):
  155. """
  156. 'Sniffer' for a file-like writable object. Similar to the unix tool tee.
  157. """
  158. def __init__(self, orig):
  159. self.orig = orig
  160. self.s = io.StringIO()
  161. def write(self, str):
  162. if self.orig:
  163. self.orig.write(str)
  164. try:
  165. self.s.write(str)
  166. except TypeError as e:
  167. # "unicode argument expected" bug in IOStream (python 2.x)
  168. self.s.write(str.decode())
  169. def writelines(self, lines):
  170. for l in lines:
  171. self.write(l + '\n')
  172. def getvalue(self):
  173. """
  174. Return everything written to orig since the Streamer was created.
  175. """
  176. return self.s.getvalue()
  177. def flush(self):
  178. if self.orig:
  179. self.orig.flush()
  180. self.s.flush()
  181. class SConfBuildTask(SCons.Taskmaster.AlwaysTask):
  182. """
  183. This is almost the same as SCons.Script.BuildTask. Handles SConfErrors
  184. correctly and knows about the current cache_mode.
  185. """
  186. def display(self, message):
  187. if sconf_global.logstream:
  188. sconf_global.logstream.write("scons: Configure: " + message + "\n")
  189. def display_cached_string(self, bi):
  190. """
  191. Logs the original builder messages, given the SConfBuildInfo instance
  192. bi.
  193. """
  194. if not isinstance(bi, SConfBuildInfo):
  195. SCons.Warnings.warn(SConfWarning,
  196. "The stored build information has an unexpected class: %s" % bi.__class__)
  197. else:
  198. self.display("The original builder output was:\n" +
  199. (" |" + str(bi.string)).replace("\n", "\n |"))
  200. def failed(self):
  201. # check, if the reason was a ConfigureDryRunError or a
  202. # ConfigureCacheError and if yes, reraise the exception
  203. exc_type = self.exc_info()[0]
  204. if issubclass(exc_type, SConfError):
  205. raise
  206. elif issubclass(exc_type, SCons.Errors.BuildError):
  207. # we ignore Build Errors (occurs, when a test doesn't pass)
  208. # Clear the exception to prevent the contained traceback
  209. # to build a reference cycle.
  210. self.exc_clear()
  211. else:
  212. self.display('Caught exception while building "%s":\n' %
  213. self.targets[0])
  214. sys.excepthook(*self.exc_info())
  215. return SCons.Taskmaster.Task.failed(self)
  216. def collect_node_states(self):
  217. # returns (is_up_to_date, cached_error, cachable)
  218. # where is_up_to_date is 1, if the node(s) are up_to_date
  219. # cached_error is 1, if the node(s) are up_to_date, but the
  220. # build will fail
  221. # cachable is 0, if some nodes are not in our cache
  222. T = 0
  223. changed = False
  224. cached_error = False
  225. cachable = True
  226. for t in self.targets:
  227. if T: Trace('%s' % (t))
  228. bi = t.get_stored_info().binfo
  229. if isinstance(bi, SConfBuildInfo):
  230. if T: Trace(': SConfBuildInfo')
  231. if cache_mode == CACHE:
  232. t.set_state(SCons.Node.up_to_date)
  233. if T: Trace(': set_state(up_to-date)')
  234. else:
  235. if T: Trace(': get_state() %s' % t.get_state())
  236. if T: Trace(': changed() %s' % t.changed())
  237. if (t.get_state() != SCons.Node.up_to_date and t.changed()):
  238. changed = True
  239. if T: Trace(': changed %s' % changed)
  240. cached_error = cached_error or bi.result
  241. else:
  242. if T: Trace(': else')
  243. # the node hasn't been built in a SConf context or doesn't
  244. # exist
  245. cachable = False
  246. changed = ( t.get_state() != SCons.Node.up_to_date )
  247. if T: Trace(': changed %s' % changed)
  248. if T: Trace('\n')
  249. return (not changed, cached_error, cachable)
  250. def execute(self):
  251. if not self.targets[0].has_builder():
  252. return
  253. sconf = sconf_global
  254. is_up_to_date, cached_error, cachable = self.collect_node_states()
  255. if cache_mode == CACHE and not cachable:
  256. raise ConfigureCacheError(self.targets[0])
  257. elif cache_mode == FORCE:
  258. is_up_to_date = 0
  259. if cached_error and is_up_to_date:
  260. self.display("Building \"%s\" failed in a previous run and all "
  261. "its sources are up to date." % str(self.targets[0]))
  262. binfo = self.targets[0].get_stored_info().binfo
  263. self.display_cached_string(binfo)
  264. raise SCons.Errors.BuildError # will be 'caught' in self.failed
  265. elif is_up_to_date:
  266. self.display("\"%s\" is up to date." % str(self.targets[0]))
  267. binfo = self.targets[0].get_stored_info().binfo
  268. self.display_cached_string(binfo)
  269. elif dryrun:
  270. raise ConfigureDryRunError(self.targets[0])
  271. else:
  272. # note stdout and stderr are the same here
  273. s = sys.stdout = sys.stderr = Streamer(sys.stdout)
  274. try:
  275. env = self.targets[0].get_build_env()
  276. if cache_mode == FORCE:
  277. # Set up the Decider() to force rebuilds by saying
  278. # that every source has changed. Note that we still
  279. # call the environment's underlying source decider so
  280. # that the correct .sconsign info will get calculated
  281. # and keep the build state consistent.
  282. def force_build(dependency, target, prev_ni,
  283. env_decider=env.decide_source):
  284. env_decider(dependency, target, prev_ni)
  285. return True
  286. if env.decide_source.__code__ is not force_build.__code__:
  287. env.Decider(force_build)
  288. env['PSTDOUT'] = env['PSTDERR'] = s
  289. try:
  290. sconf.cached = 0
  291. self.targets[0].build()
  292. finally:
  293. sys.stdout = sys.stderr = env['PSTDOUT'] = \
  294. env['PSTDERR'] = sconf.logstream
  295. except KeyboardInterrupt:
  296. raise
  297. except SystemExit:
  298. exc_value = sys.exc_info()[1]
  299. raise SCons.Errors.ExplicitExit(self.targets[0],exc_value.code)
  300. except Exception as e:
  301. for t in self.targets:
  302. binfo = SConfBuildInfo()
  303. binfo.merge(t.get_binfo())
  304. binfo.set_build_result(1, s.getvalue())
  305. sconsign_entry = SCons.SConsign.SConsignEntry()
  306. sconsign_entry.binfo = binfo
  307. #sconsign_entry.ninfo = self.get_ninfo()
  308. # We'd like to do this as follows:
  309. # t.store_info(binfo)
  310. # However, we need to store it as an SConfBuildInfo
  311. # object, and store_info() will turn it into a
  312. # regular FileNodeInfo if the target is itself a
  313. # regular File.
  314. sconsign = t.dir.sconsign()
  315. sconsign.set_entry(t.name, sconsign_entry)
  316. sconsign.merge()
  317. raise e
  318. else:
  319. for t in self.targets:
  320. binfo = SConfBuildInfo()
  321. binfo.merge(t.get_binfo())
  322. binfo.set_build_result(0, s.getvalue())
  323. sconsign_entry = SCons.SConsign.SConsignEntry()
  324. sconsign_entry.binfo = binfo
  325. #sconsign_entry.ninfo = self.get_ninfo()
  326. # We'd like to do this as follows:
  327. # t.store_info(binfo)
  328. # However, we need to store it as an SConfBuildInfo
  329. # object, and store_info() will turn it into a
  330. # regular FileNodeInfo if the target is itself a
  331. # regular File.
  332. sconsign = t.dir.sconsign()
  333. sconsign.set_entry(t.name, sconsign_entry)
  334. sconsign.merge()
  335. class SConfBase(object):
  336. """This is simply a class to represent a configure context. After
  337. creating a SConf object, you can call any tests. After finished with your
  338. tests, be sure to call the Finish() method, which returns the modified
  339. environment.
  340. Some words about caching: In most cases, it is not necessary to cache
  341. Test results explicitly. Instead, we use the scons dependency checking
  342. mechanism. For example, if one wants to compile a test program
  343. (SConf.TryLink), the compiler is only called, if the program dependencies
  344. have changed. However, if the program could not be compiled in a former
  345. SConf run, we need to explicitly cache this error.
  346. """
  347. def __init__(self, env, custom_tests = {}, conf_dir='$CONFIGUREDIR',
  348. log_file='$CONFIGURELOG', config_h = None, _depth = 0):
  349. """Constructor. Pass additional tests in the custom_tests-dictionary,
  350. e.g. custom_tests={'CheckPrivate':MyPrivateTest}, where MyPrivateTest
  351. defines a custom test.
  352. Note also the conf_dir and log_file arguments (you may want to
  353. build tests in the VariantDir, not in the SourceDir)
  354. """
  355. global SConfFS
  356. if not SConfFS:
  357. SConfFS = SCons.Node.FS.default_fs or \
  358. SCons.Node.FS.FS(env.fs.pathTop)
  359. if sconf_global is not None:
  360. raise SCons.Errors.UserError
  361. self.env = env
  362. if log_file is not None:
  363. log_file = SConfFS.File(env.subst(log_file))
  364. self.logfile = log_file
  365. self.logstream = None
  366. self.lastTarget = None
  367. self.depth = _depth
  368. self.cached = 0 # will be set, if all test results are cached
  369. # add default tests
  370. default_tests = {
  371. 'CheckCC' : CheckCC,
  372. 'CheckCXX' : CheckCXX,
  373. 'CheckSHCC' : CheckSHCC,
  374. 'CheckSHCXX' : CheckSHCXX,
  375. 'CheckFunc' : CheckFunc,
  376. 'CheckType' : CheckType,
  377. 'CheckTypeSize' : CheckTypeSize,
  378. 'CheckDeclaration' : CheckDeclaration,
  379. 'CheckHeader' : CheckHeader,
  380. 'CheckCHeader' : CheckCHeader,
  381. 'CheckCXXHeader' : CheckCXXHeader,
  382. 'CheckLib' : CheckLib,
  383. 'CheckLibWithHeader' : CheckLibWithHeader,
  384. 'CheckProg' : CheckProg,
  385. }
  386. self.AddTests(default_tests)
  387. self.AddTests(custom_tests)
  388. self.confdir = SConfFS.Dir(env.subst(conf_dir))
  389. if config_h is not None:
  390. config_h = SConfFS.File(config_h)
  391. self.config_h = config_h
  392. self._startup()
  393. def Finish(self):
  394. """Call this method after finished with your tests:
  395. env = sconf.Finish()
  396. """
  397. self._shutdown()
  398. return self.env
  399. def Define(self, name, value = None, comment = None):
  400. """
  401. Define a pre processor symbol name, with the optional given value in the
  402. current config header.
  403. If value is None (default), then #define name is written. If value is not
  404. none, then #define name value is written.
  405. comment is a string which will be put as a C comment in the header, to explain the meaning of the value
  406. (appropriate C comments will be added automatically).
  407. """
  408. lines = []
  409. if comment:
  410. comment_str = "/* %s */" % comment
  411. lines.append(comment_str)
  412. if value is not None:
  413. define_str = "#define %s %s" % (name, value)
  414. else:
  415. define_str = "#define %s" % name
  416. lines.append(define_str)
  417. lines.append('')
  418. self.config_h_text = self.config_h_text + '\n'.join(lines)
  419. def BuildNodes(self, nodes):
  420. """
  421. Tries to build the given nodes immediately. Returns 1 on success,
  422. 0 on error.
  423. """
  424. if self.logstream is not None:
  425. # override stdout / stderr to write in log file
  426. oldStdout = sys.stdout
  427. sys.stdout = self.logstream
  428. oldStderr = sys.stderr
  429. sys.stderr = self.logstream
  430. # the engine assumes the current path is the SConstruct directory ...
  431. old_fs_dir = SConfFS.getcwd()
  432. old_os_dir = os.getcwd()
  433. SConfFS.chdir(SConfFS.Top, change_os_dir=1)
  434. # Because we take responsibility here for writing out our
  435. # own .sconsign info (see SConfBuildTask.execute(), above),
  436. # we override the store_info() method with a null place-holder
  437. # so we really control how it gets written.
  438. for n in nodes:
  439. n.store_info = 0
  440. if not hasattr(n, 'attributes'):
  441. n.attributes = SCons.Node.Node.Attrs()
  442. n.attributes.keep_targetinfo = 1
  443. ret = 1
  444. try:
  445. # ToDo: use user options for calc
  446. save_max_drift = SConfFS.get_max_drift()
  447. SConfFS.set_max_drift(0)
  448. tm = SCons.Taskmaster.Taskmaster(nodes, SConfBuildTask)
  449. # we don't want to build tests in parallel
  450. jobs = SCons.Job.Jobs(1, tm )
  451. jobs.run()
  452. for n in nodes:
  453. state = n.get_state()
  454. if (state != SCons.Node.executed and
  455. state != SCons.Node.up_to_date):
  456. # the node could not be built. we return 0 in this case
  457. ret = 0
  458. finally:
  459. SConfFS.set_max_drift(save_max_drift)
  460. os.chdir(old_os_dir)
  461. SConfFS.chdir(old_fs_dir, change_os_dir=0)
  462. if self.logstream is not None:
  463. # restore stdout / stderr
  464. sys.stdout = oldStdout
  465. sys.stderr = oldStderr
  466. return ret
  467. def pspawn_wrapper(self, sh, escape, cmd, args, env):
  468. """Wrapper function for handling piped spawns.
  469. This looks to the calling interface (in Action.py) like a "normal"
  470. spawn, but associates the call with the PSPAWN variable from
  471. the construction environment and with the streams to which we
  472. want the output logged. This gets slid into the construction
  473. environment as the SPAWN variable so Action.py doesn't have to
  474. know or care whether it's spawning a piped command or not.
  475. """
  476. return self.pspawn(sh, escape, cmd, args, env, self.logstream, self.logstream)
  477. def TryBuild(self, builder, text = None, extension = ""):
  478. """Low level TryBuild implementation. Normally you don't need to
  479. call that - you can use TryCompile / TryLink / TryRun instead
  480. """
  481. global _ac_build_counter
  482. # Make sure we have a PSPAWN value, and save the current
  483. # SPAWN value.
  484. try:
  485. self.pspawn = self.env['PSPAWN']
  486. except KeyError:
  487. raise SCons.Errors.UserError('Missing PSPAWN construction variable.')
  488. try:
  489. save_spawn = self.env['SPAWN']
  490. except KeyError:
  491. raise SCons.Errors.UserError('Missing SPAWN construction variable.')
  492. nodesToBeBuilt = []
  493. f = "conftest_" + str(_ac_build_counter)
  494. pref = self.env.subst( builder.builder.prefix )
  495. suff = self.env.subst( builder.builder.suffix )
  496. target = self.confdir.File(pref + f + suff)
  497. try:
  498. # Slide our wrapper into the construction environment as
  499. # the SPAWN function.
  500. self.env['SPAWN'] = self.pspawn_wrapper
  501. sourcetext = self.env.Value(text)
  502. if text is not None:
  503. textFile = self.confdir.File(f + extension)
  504. textFileNode = self.env.SConfSourceBuilder(target=textFile,
  505. source=sourcetext)
  506. nodesToBeBuilt.extend(textFileNode)
  507. source = textFileNode
  508. else:
  509. source = None
  510. nodes = builder(target = target, source = source)
  511. if not SCons.Util.is_List(nodes):
  512. nodes = [nodes]
  513. nodesToBeBuilt.extend(nodes)
  514. result = self.BuildNodes(nodesToBeBuilt)
  515. finally:
  516. self.env['SPAWN'] = save_spawn
  517. _ac_build_counter = _ac_build_counter + 1
  518. if result:
  519. self.lastTarget = nodes[0]
  520. else:
  521. self.lastTarget = None
  522. return result
  523. def TryAction(self, action, text = None, extension = ""):
  524. """Tries to execute the given action with optional source file
  525. contents <text> and optional source file extension <extension>,
  526. Returns the status (0 : failed, 1 : ok) and the contents of the
  527. output file.
  528. """
  529. builder = SCons.Builder.Builder(action=action)
  530. self.env.Append( BUILDERS = {'SConfActionBuilder' : builder} )
  531. ok = self.TryBuild(self.env.SConfActionBuilder, text, extension)
  532. del self.env['BUILDERS']['SConfActionBuilder']
  533. if ok:
  534. outputStr = self.lastTarget.get_contents().decode()
  535. return (1, outputStr)
  536. return (0, "")
  537. def TryCompile( self, text, extension):
  538. """Compiles the program given in text to an env.Object, using extension
  539. as file extension (e.g. '.c'). Returns 1, if compilation was
  540. successful, 0 otherwise. The target is saved in self.lastTarget (for
  541. further processing).
  542. """
  543. return self.TryBuild(self.env.Object, text, extension)
  544. def TryLink( self, text, extension ):
  545. """Compiles the program given in text to an executable env.Program,
  546. using extension as file extension (e.g. '.c'). Returns 1, if
  547. compilation was successful, 0 otherwise. The target is saved in
  548. self.lastTarget (for further processing).
  549. """
  550. return self.TryBuild(self.env.Program, text, extension )
  551. def TryRun(self, text, extension ):
  552. """Compiles and runs the program given in text, using extension
  553. as file extension (e.g. '.c'). Returns (1, outputStr) on success,
  554. (0, '') otherwise. The target (a file containing the program's stdout)
  555. is saved in self.lastTarget (for further processing).
  556. """
  557. ok = self.TryLink(text, extension)
  558. if( ok ):
  559. prog = self.lastTarget
  560. pname = prog.get_internal_path()
  561. output = self.confdir.File(os.path.basename(pname)+'.out')
  562. node = self.env.Command(output, prog, [ [ pname, ">", "${TARGET}"] ])
  563. ok = self.BuildNodes(node)
  564. if ok:
  565. outputStr = SCons.Util.to_str(output.get_contents())
  566. return( 1, outputStr)
  567. return (0, "")
  568. class TestWrapper(object):
  569. """A wrapper around Tests (to ensure sanity)"""
  570. def __init__(self, test, sconf):
  571. self.test = test
  572. self.sconf = sconf
  573. def __call__(self, *args, **kw):
  574. if not self.sconf.active:
  575. raise SCons.Errors.UserError
  576. context = CheckContext(self.sconf)
  577. ret = self.test(context, *args, **kw)
  578. if self.sconf.config_h is not None:
  579. self.sconf.config_h_text = self.sconf.config_h_text + context.config_h
  580. context.Result("error: no result")
  581. return ret
  582. def AddTest(self, test_name, test_instance):
  583. """Adds test_class to this SConf instance. It can be called with
  584. self.test_name(...)"""
  585. setattr(self, test_name, SConfBase.TestWrapper(test_instance, self))
  586. def AddTests(self, tests):
  587. """Adds all the tests given in the tests dictionary to this SConf
  588. instance
  589. """
  590. for name in list(tests.keys()):
  591. self.AddTest(name, tests[name])
  592. def _createDir( self, node ):
  593. dirName = str(node)
  594. if dryrun:
  595. if not os.path.isdir( dirName ):
  596. raise ConfigureDryRunError(dirName)
  597. else:
  598. if not os.path.isdir( dirName ):
  599. os.makedirs( dirName )
  600. def _startup(self):
  601. """Private method. Set up logstream, and set the environment
  602. variables necessary for a piped build
  603. """
  604. global _ac_config_logs
  605. global sconf_global
  606. global SConfFS
  607. self.lastEnvFs = self.env.fs
  608. self.env.fs = SConfFS
  609. self._createDir(self.confdir)
  610. self.confdir.up().add_ignore( [self.confdir] )
  611. if self.logfile is not None and not dryrun:
  612. # truncate logfile, if SConf.Configure is called for the first time
  613. # in a build
  614. if self.logfile in _ac_config_logs:
  615. log_mode = "a"
  616. else:
  617. _ac_config_logs[self.logfile] = None
  618. log_mode = "w"
  619. fp = open(str(self.logfile), log_mode)
  620. self.logstream = SCons.Util.Unbuffered(fp)
  621. # logfile may stay in a build directory, so we tell
  622. # the build system not to override it with a eventually
  623. # existing file with the same name in the source directory
  624. self.logfile.dir.add_ignore( [self.logfile] )
  625. tb = traceback.extract_stack()[-3-self.depth]
  626. old_fs_dir = SConfFS.getcwd()
  627. SConfFS.chdir(SConfFS.Top, change_os_dir=0)
  628. self.logstream.write('file %s,line %d:\n\tConfigure(confdir = %s)\n' %
  629. (tb[0], tb[1], str(self.confdir)) )
  630. SConfFS.chdir(old_fs_dir)
  631. else:
  632. self.logstream = None
  633. # we use a special builder to create source files from TEXT
  634. action = SCons.Action.Action(_createSource,
  635. _stringSource)
  636. sconfSrcBld = SCons.Builder.Builder(action=action)
  637. self.env.Append( BUILDERS={'SConfSourceBuilder':sconfSrcBld} )
  638. self.config_h_text = _ac_config_hs.get(self.config_h, "")
  639. self.active = 1
  640. # only one SConf instance should be active at a time ...
  641. sconf_global = self
  642. def _shutdown(self):
  643. """Private method. Reset to non-piped spawn"""
  644. global sconf_global, _ac_config_hs
  645. if not self.active:
  646. raise SCons.Errors.UserError("Finish may be called only once!")
  647. if self.logstream is not None and not dryrun:
  648. self.logstream.write("\n")
  649. self.logstream.close()
  650. self.logstream = None
  651. # remove the SConfSourceBuilder from the environment
  652. blds = self.env['BUILDERS']
  653. del blds['SConfSourceBuilder']
  654. self.env.Replace( BUILDERS=blds )
  655. self.active = 0
  656. sconf_global = None
  657. if not self.config_h is None:
  658. _ac_config_hs[self.config_h] = self.config_h_text
  659. self.env.fs = self.lastEnvFs
  660. class CheckContext(object):
  661. """Provides a context for configure tests. Defines how a test writes to the
  662. screen and log file.
  663. A typical test is just a callable with an instance of CheckContext as
  664. first argument:
  665. def CheckCustom(context, ...):
  666. context.Message('Checking my weird test ... ')
  667. ret = myWeirdTestFunction(...)
  668. context.Result(ret)
  669. Often, myWeirdTestFunction will be one of
  670. context.TryCompile/context.TryLink/context.TryRun. The results of
  671. those are cached, for they are only rebuild, if the dependencies have
  672. changed.
  673. """
  674. def __init__(self, sconf):
  675. """Constructor. Pass the corresponding SConf instance."""
  676. self.sconf = sconf
  677. self.did_show_result = 0
  678. # for Conftest.py:
  679. self.vardict = {}
  680. self.havedict = {}
  681. self.headerfilename = None
  682. self.config_h = "" # config_h text will be stored here
  683. # we don't regenerate the config.h file after each test. That means,
  684. # that tests won't be able to include the config.h file, and so
  685. # they can't do an #ifdef HAVE_XXX_H. This shouldn't be a major
  686. # issue, though. If it turns out, that we need to include config.h
  687. # in tests, we must ensure, that the dependencies are worked out
  688. # correctly. Note that we can't use Conftest.py's support for config.h,
  689. # cause we will need to specify a builder for the config.h file ...
  690. def Message(self, text):
  691. """Inform about what we are doing right now, e.g.
  692. 'Checking for SOMETHING ... '
  693. """
  694. self.Display(text)
  695. self.sconf.cached = 1
  696. self.did_show_result = 0
  697. def Result(self, res):
  698. """Inform about the result of the test. If res is not a string, displays
  699. 'yes' or 'no' depending on whether res is evaluated as true or false.
  700. The result is only displayed when self.did_show_result is not set.
  701. """
  702. if isinstance(res, str):
  703. text = res
  704. elif res:
  705. text = "yes"
  706. else:
  707. text = "no"
  708. if self.did_show_result == 0:
  709. # Didn't show result yet, do it now.
  710. self.Display(text + "\n")
  711. self.did_show_result = 1
  712. def TryBuild(self, *args, **kw):
  713. return self.sconf.TryBuild(*args, **kw)
  714. def TryAction(self, *args, **kw):
  715. return self.sconf.TryAction(*args, **kw)
  716. def TryCompile(self, *args, **kw):
  717. return self.sconf.TryCompile(*args, **kw)
  718. def TryLink(self, *args, **kw):
  719. return self.sconf.TryLink(*args, **kw)
  720. def TryRun(self, *args, **kw):
  721. return self.sconf.TryRun(*args, **kw)
  722. def __getattr__( self, attr ):
  723. if( attr == 'env' ):
  724. return self.sconf.env
  725. elif( attr == 'lastTarget' ):
  726. return self.sconf.lastTarget
  727. else:
  728. raise AttributeError("CheckContext instance has no attribute '%s'" % attr)
  729. #### Stuff used by Conftest.py (look there for explanations).
  730. def BuildProg(self, text, ext):
  731. self.sconf.cached = 1
  732. # TODO: should use self.vardict for $CC, $CPPFLAGS, etc.
  733. return not self.TryBuild(self.env.Program, text, ext)
  734. def CompileProg(self, text, ext):
  735. self.sconf.cached = 1
  736. # TODO: should use self.vardict for $CC, $CPPFLAGS, etc.
  737. return not self.TryBuild(self.env.Object, text, ext)
  738. def CompileSharedObject(self, text, ext):
  739. self.sconf.cached = 1
  740. # TODO: should use self.vardict for $SHCC, $CPPFLAGS, etc.
  741. return not self.TryBuild(self.env.SharedObject, text, ext)
  742. def RunProg(self, text, ext):
  743. self.sconf.cached = 1
  744. # TODO: should use self.vardict for $CC, $CPPFLAGS, etc.
  745. st, out = self.TryRun(text, ext)
  746. return not st, out
  747. def AppendLIBS(self, lib_name_list):
  748. oldLIBS = self.env.get( 'LIBS', [] )
  749. self.env.Append(LIBS = lib_name_list)
  750. return oldLIBS
  751. def PrependLIBS(self, lib_name_list):
  752. oldLIBS = self.env.get( 'LIBS', [] )
  753. self.env.Prepend(LIBS = lib_name_list)
  754. return oldLIBS
  755. def SetLIBS(self, val):
  756. oldLIBS = self.env.get( 'LIBS', [] )
  757. self.env.Replace(LIBS = val)
  758. return oldLIBS
  759. def Display(self, msg):
  760. if self.sconf.cached:
  761. # We assume that Display is called twice for each test here
  762. # once for the Checking for ... message and once for the result.
  763. # The self.sconf.cached flag can only be set between those calls
  764. msg = "(cached) " + msg
  765. self.sconf.cached = 0
  766. progress_display(msg, append_newline=0)
  767. self.Log("scons: Configure: " + msg + "\n")
  768. def Log(self, msg):
  769. if self.sconf.logstream is not None:
  770. self.sconf.logstream.write(msg)
  771. #### End of stuff used by Conftest.py.
  772. def SConf(*args, **kw):
  773. if kw.get(build_type, True):
  774. kw['_depth'] = kw.get('_depth', 0) + 1
  775. for bt in build_types:
  776. try:
  777. del kw[bt]
  778. except KeyError:
  779. pass
  780. return SConfBase(*args, **kw)
  781. else:
  782. return SCons.Util.Null()
  783. def CheckFunc(context, function_name, header = None, language = None):
  784. res = SCons.Conftest.CheckFunc(context, function_name, header = header, language = language)
  785. context.did_show_result = 1
  786. return not res
  787. def CheckType(context, type_name, includes = "", language = None):
  788. res = SCons.Conftest.CheckType(context, type_name,
  789. header = includes, language = language)
  790. context.did_show_result = 1
  791. return not res
  792. def CheckTypeSize(context, type_name, includes = "", language = None, expect = None):
  793. res = SCons.Conftest.CheckTypeSize(context, type_name,
  794. header = includes, language = language,
  795. expect = expect)
  796. context.did_show_result = 1
  797. return res
  798. def CheckDeclaration(context, declaration, includes = "", language = None):
  799. res = SCons.Conftest.CheckDeclaration(context, declaration,
  800. includes = includes,
  801. language = language)
  802. context.did_show_result = 1
  803. return not res
  804. def createIncludesFromHeaders(headers, leaveLast, include_quotes = '""'):
  805. # used by CheckHeader and CheckLibWithHeader to produce C - #include
  806. # statements from the specified header (list)
  807. if not SCons.Util.is_List(headers):
  808. headers = [headers]
  809. l = []
  810. if leaveLast:
  811. lastHeader = headers[-1]
  812. headers = headers[:-1]
  813. else:
  814. lastHeader = None
  815. for s in headers:
  816. l.append("#include %s%s%s\n"
  817. % (include_quotes[0], s, include_quotes[1]))
  818. return ''.join(l), lastHeader
  819. def CheckHeader(context, header, include_quotes = '<>', language = None):
  820. """
  821. A test for a C or C++ header file.
  822. """
  823. prog_prefix, hdr_to_check = \
  824. createIncludesFromHeaders(header, 1, include_quotes)
  825. res = SCons.Conftest.CheckHeader(context, hdr_to_check, prog_prefix,
  826. language = language,
  827. include_quotes = include_quotes)
  828. context.did_show_result = 1
  829. return not res
  830. def CheckCC(context):
  831. res = SCons.Conftest.CheckCC(context)
  832. context.did_show_result = 1
  833. return not res
  834. def CheckCXX(context):
  835. res = SCons.Conftest.CheckCXX(context)
  836. context.did_show_result = 1
  837. return not res
  838. def CheckSHCC(context):
  839. res = SCons.Conftest.CheckSHCC(context)
  840. context.did_show_result = 1
  841. return not res
  842. def CheckSHCXX(context):
  843. res = SCons.Conftest.CheckSHCXX(context)
  844. context.did_show_result = 1
  845. return not res
  846. # Bram: Make this function obsolete? CheckHeader() is more generic.
  847. def CheckCHeader(context, header, include_quotes = '""'):
  848. """
  849. A test for a C header file.
  850. """
  851. return CheckHeader(context, header, include_quotes, language = "C")
  852. # Bram: Make this function obsolete? CheckHeader() is more generic.
  853. def CheckCXXHeader(context, header, include_quotes = '""'):
  854. """
  855. A test for a C++ header file.
  856. """
  857. return CheckHeader(context, header, include_quotes, language = "C++")
  858. def CheckLib(context, library = None, symbol = "main",
  859. header = None, language = None, autoadd = 1):
  860. """
  861. A test for a library. See also CheckLibWithHeader.
  862. Note that library may also be None to test whether the given symbol
  863. compiles without flags.
  864. """
  865. if library == []:
  866. library = [None]
  867. if not SCons.Util.is_List(library):
  868. library = [library]
  869. # ToDo: accept path for the library
  870. res = SCons.Conftest.CheckLib(context, library, symbol, header = header,
  871. language = language, autoadd = autoadd)
  872. context.did_show_result = 1
  873. return not res
  874. # XXX
  875. # Bram: Can only include one header and can't use #ifdef HAVE_HEADER_H.
  876. def CheckLibWithHeader(context, libs, header, language,
  877. call = None, autoadd = 1):
  878. # ToDo: accept path for library. Support system header files.
  879. """
  880. Another (more sophisticated) test for a library.
  881. Checks, if library and header is available for language (may be 'C'
  882. or 'CXX'). Call maybe be a valid expression _with_ a trailing ';'.
  883. As in CheckLib, we support library=None, to test if the call compiles
  884. without extra link flags.
  885. """
  886. prog_prefix, dummy = \
  887. createIncludesFromHeaders(header, 0)
  888. if libs == []:
  889. libs = [None]
  890. if not SCons.Util.is_List(libs):
  891. libs = [libs]
  892. res = SCons.Conftest.CheckLib(context, libs, None, prog_prefix,
  893. call = call, language = language, autoadd = autoadd)
  894. context.did_show_result = 1
  895. return not res
  896. def CheckProg(context, prog_name):
  897. """Simple check if a program exists in the path. Returns the path
  898. for the application, or None if not found.
  899. """
  900. res = SCons.Conftest.CheckProg(context, prog_name)
  901. context.did_show_result = 1
  902. return res
  903. # Local Variables:
  904. # tab-width:4
  905. # indent-tabs-mode:nil
  906. # End:
  907. # vim: set expandtab tabstop=4 shiftwidth=4: