PageRenderTime 43ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/couchjs/scons/scons-local-2.0.1/SCons/Conftest.py

http://github.com/cloudant/bigcouch
Python | 793 lines | 667 code | 13 blank | 113 comment | 12 complexity | ae1e15f92ea399e24a195ed2946945a9 MD5 | raw file
Possible License(s): Apache-2.0
  1. """SCons.Conftest
  2. Autoconf-like configuration support; low level implementation of tests.
  3. """
  4. #
  5. # Copyright (c) 2003 Stichting NLnet Labs
  6. # Copyright (c) 2001, 2002, 2003 Steven Knight
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining
  9. # a copy of this software and associated documentation files (the
  10. # "Software"), to deal in the Software without restriction, including
  11. # without limitation the rights to use, copy, modify, merge, publish,
  12. # distribute, sublicense, and/or sell copies of the Software, and to
  13. # permit persons to whom the Software is furnished to do so, subject to
  14. # the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be included
  17. # in all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  20. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  21. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. #
  27. #
  28. # The purpose of this module is to define how a check is to be performed.
  29. # Use one of the Check...() functions below.
  30. #
  31. #
  32. # A context class is used that defines functions for carrying out the tests,
  33. # logging and messages. The following methods and members must be present:
  34. #
  35. # context.Display(msg) Function called to print messages that are normally
  36. # displayed for the user. Newlines are explicitly used.
  37. # The text should also be written to the logfile!
  38. #
  39. # context.Log(msg) Function called to write to a log file.
  40. #
  41. # context.BuildProg(text, ext)
  42. # Function called to build a program, using "ext" for the
  43. # file extention. Must return an empty string for
  44. # success, an error message for failure.
  45. # For reliable test results building should be done just
  46. # like an actual program would be build, using the same
  47. # command and arguments (including configure results so
  48. # far).
  49. #
  50. # context.CompileProg(text, ext)
  51. # Function called to compile a program, using "ext" for
  52. # the file extention. Must return an empty string for
  53. # success, an error message for failure.
  54. # For reliable test results compiling should be done just
  55. # like an actual source file would be compiled, using the
  56. # same command and arguments (including configure results
  57. # so far).
  58. #
  59. # context.AppendLIBS(lib_name_list)
  60. # Append "lib_name_list" to the value of LIBS.
  61. # "lib_namelist" is a list of strings.
  62. # Return the value of LIBS before changing it (any type
  63. # can be used, it is passed to SetLIBS() later.)
  64. #
  65. # context.PrependLIBS(lib_name_list)
  66. # Prepend "lib_name_list" to the value of LIBS.
  67. # "lib_namelist" is a list of strings.
  68. # Return the value of LIBS before changing it (any type
  69. # can be used, it is passed to SetLIBS() later.)
  70. #
  71. # context.SetLIBS(value)
  72. # Set LIBS to "value". The type of "value" is what
  73. # AppendLIBS() returned.
  74. # Return the value of LIBS before changing it (any type
  75. # can be used, it is passed to SetLIBS() later.)
  76. #
  77. # context.headerfilename
  78. # Name of file to append configure results to, usually
  79. # "confdefs.h".
  80. # The file must not exist or be empty when starting.
  81. # Empty or None to skip this (some tests will not work!).
  82. #
  83. # context.config_h (may be missing). If present, must be a string, which
  84. # will be filled with the contents of a config_h file.
  85. #
  86. # context.vardict Dictionary holding variables used for the tests and
  87. # stores results from the tests, used for the build
  88. # commands.
  89. # Normally contains "CC", "LIBS", "CPPFLAGS", etc.
  90. #
  91. # context.havedict Dictionary holding results from the tests that are to
  92. # be used inside a program.
  93. # Names often start with "HAVE_". These are zero
  94. # (feature not present) or one (feature present). Other
  95. # variables may have any value, e.g., "PERLVERSION" can
  96. # be a number and "SYSTEMNAME" a string.
  97. #
  98. import re
  99. from types import IntType
  100. #
  101. # PUBLIC VARIABLES
  102. #
  103. LogInputFiles = 1 # Set that to log the input files in case of a failed test
  104. LogErrorMessages = 1 # Set that to log Conftest-generated error messages
  105. #
  106. # PUBLIC FUNCTIONS
  107. #
  108. # Generic remarks:
  109. # - When a language is specified which is not supported the test fails. The
  110. # message is a bit different, because not all the arguments for the normal
  111. # message are available yet (chicken-egg problem).
  112. def CheckBuilder(context, text = None, language = None):
  113. """
  114. Configure check to see if the compiler works.
  115. Note that this uses the current value of compiler and linker flags, make
  116. sure $CFLAGS, $CPPFLAGS and $LIBS are set correctly.
  117. "language" should be "C" or "C++" and is used to select the compiler.
  118. Default is "C".
  119. "text" may be used to specify the code to be build.
  120. Returns an empty string for success, an error message for failure.
  121. """
  122. lang, suffix, msg = _lang2suffix(language)
  123. if msg:
  124. context.Display("%s\n" % msg)
  125. return msg
  126. if not text:
  127. text = """
  128. int main() {
  129. return 0;
  130. }
  131. """
  132. context.Display("Checking if building a %s file works... " % lang)
  133. ret = context.BuildProg(text, suffix)
  134. _YesNoResult(context, ret, None, text)
  135. return ret
  136. def CheckCC(context):
  137. """
  138. Configure check for a working C compiler.
  139. This checks whether the C compiler, as defined in the $CC construction
  140. variable, can compile a C source file. It uses the current $CCCOM value
  141. too, so that it can test against non working flags.
  142. """
  143. context.Display("Checking whether the C compiler works")
  144. text = """
  145. int main()
  146. {
  147. return 0;
  148. }
  149. """
  150. ret = _check_empty_program(context, 'CC', text, 'C')
  151. _YesNoResult(context, ret, None, text)
  152. return ret
  153. def CheckSHCC(context):
  154. """
  155. Configure check for a working shared C compiler.
  156. This checks whether the C compiler, as defined in the $SHCC construction
  157. variable, can compile a C source file. It uses the current $SHCCCOM value
  158. too, so that it can test against non working flags.
  159. """
  160. context.Display("Checking whether the (shared) C compiler works")
  161. text = """
  162. int foo()
  163. {
  164. return 0;
  165. }
  166. """
  167. ret = _check_empty_program(context, 'SHCC', text, 'C', use_shared = True)
  168. _YesNoResult(context, ret, None, text)
  169. return ret
  170. def CheckCXX(context):
  171. """
  172. Configure check for a working CXX compiler.
  173. This checks whether the CXX compiler, as defined in the $CXX construction
  174. variable, can compile a CXX source file. It uses the current $CXXCOM value
  175. too, so that it can test against non working flags.
  176. """
  177. context.Display("Checking whether the C++ compiler works")
  178. text = """
  179. int main()
  180. {
  181. return 0;
  182. }
  183. """
  184. ret = _check_empty_program(context, 'CXX', text, 'C++')
  185. _YesNoResult(context, ret, None, text)
  186. return ret
  187. def CheckSHCXX(context):
  188. """
  189. Configure check for a working shared CXX compiler.
  190. This checks whether the CXX compiler, as defined in the $SHCXX construction
  191. variable, can compile a CXX source file. It uses the current $SHCXXCOM value
  192. too, so that it can test against non working flags.
  193. """
  194. context.Display("Checking whether the (shared) C++ compiler works")
  195. text = """
  196. int main()
  197. {
  198. return 0;
  199. }
  200. """
  201. ret = _check_empty_program(context, 'SHCXX', text, 'C++', use_shared = True)
  202. _YesNoResult(context, ret, None, text)
  203. return ret
  204. def _check_empty_program(context, comp, text, language, use_shared = False):
  205. """Return 0 on success, 1 otherwise."""
  206. if comp not in context.env or not context.env[comp]:
  207. # The compiler construction variable is not set or empty
  208. return 1
  209. lang, suffix, msg = _lang2suffix(language)
  210. if msg:
  211. return 1
  212. if use_shared:
  213. return context.CompileSharedObject(text, suffix)
  214. else:
  215. return context.CompileProg(text, suffix)
  216. def CheckFunc(context, function_name, header = None, language = None):
  217. """
  218. Configure check for a function "function_name".
  219. "language" should be "C" or "C++" and is used to select the compiler.
  220. Default is "C".
  221. Optional "header" can be defined to define a function prototype, include a
  222. header file or anything else that comes before main().
  223. Sets HAVE_function_name in context.havedict according to the result.
  224. Note that this uses the current value of compiler and linker flags, make
  225. sure $CFLAGS, $CPPFLAGS and $LIBS are set correctly.
  226. Returns an empty string for success, an error message for failure.
  227. """
  228. # Remarks from autoconf:
  229. # - Don't include <ctype.h> because on OSF/1 3.0 it includes <sys/types.h>
  230. # which includes <sys/select.h> which contains a prototype for select.
  231. # Similarly for bzero.
  232. # - assert.h is included to define __stub macros and hopefully few
  233. # prototypes, which can conflict with char $1(); below.
  234. # - Override any gcc2 internal prototype to avoid an error.
  235. # - We use char for the function declaration because int might match the
  236. # return type of a gcc2 builtin and then its argument prototype would
  237. # still apply.
  238. # - The GNU C library defines this for functions which it implements to
  239. # always fail with ENOSYS. Some functions are actually named something
  240. # starting with __ and the normal name is an alias.
  241. if context.headerfilename:
  242. includetext = '#include "%s"' % context.headerfilename
  243. else:
  244. includetext = ''
  245. if not header:
  246. header = """
  247. #ifdef __cplusplus
  248. extern "C"
  249. #endif
  250. char %s();""" % function_name
  251. lang, suffix, msg = _lang2suffix(language)
  252. if msg:
  253. context.Display("Cannot check for %s(): %s\n" % (function_name, msg))
  254. return msg
  255. text = """
  256. %(include)s
  257. #include <assert.h>
  258. %(hdr)s
  259. int main() {
  260. #if defined (__stub_%(name)s) || defined (__stub___%(name)s)
  261. fail fail fail
  262. #else
  263. %(name)s();
  264. #endif
  265. return 0;
  266. }
  267. """ % { 'name': function_name,
  268. 'include': includetext,
  269. 'hdr': header }
  270. context.Display("Checking for %s function %s()... " % (lang, function_name))
  271. ret = context.BuildProg(text, suffix)
  272. _YesNoResult(context, ret, "HAVE_" + function_name, text,
  273. "Define to 1 if the system has the function `%s'." %\
  274. function_name)
  275. return ret
  276. def CheckHeader(context, header_name, header = None, language = None,
  277. include_quotes = None):
  278. """
  279. Configure check for a C or C++ header file "header_name".
  280. Optional "header" can be defined to do something before including the
  281. header file (unusual, supported for consistency).
  282. "language" should be "C" or "C++" and is used to select the compiler.
  283. Default is "C".
  284. Sets HAVE_header_name in context.havedict according to the result.
  285. Note that this uses the current value of compiler and linker flags, make
  286. sure $CFLAGS and $CPPFLAGS are set correctly.
  287. Returns an empty string for success, an error message for failure.
  288. """
  289. # Why compile the program instead of just running the preprocessor?
  290. # It is possible that the header file exists, but actually using it may
  291. # fail (e.g., because it depends on other header files). Thus this test is
  292. # more strict. It may require using the "header" argument.
  293. #
  294. # Use <> by default, because the check is normally used for system header
  295. # files. SCons passes '""' to overrule this.
  296. # Include "confdefs.h" first, so that the header can use HAVE_HEADER_H.
  297. if context.headerfilename:
  298. includetext = '#include "%s"\n' % context.headerfilename
  299. else:
  300. includetext = ''
  301. if not header:
  302. header = ""
  303. lang, suffix, msg = _lang2suffix(language)
  304. if msg:
  305. context.Display("Cannot check for header file %s: %s\n"
  306. % (header_name, msg))
  307. return msg
  308. if not include_quotes:
  309. include_quotes = "<>"
  310. text = "%s%s\n#include %s%s%s\n\n" % (includetext, header,
  311. include_quotes[0], header_name, include_quotes[1])
  312. context.Display("Checking for %s header file %s... " % (lang, header_name))
  313. ret = context.CompileProg(text, suffix)
  314. _YesNoResult(context, ret, "HAVE_" + header_name, text,
  315. "Define to 1 if you have the <%s> header file." % header_name)
  316. return ret
  317. def CheckType(context, type_name, fallback = None,
  318. header = None, language = None):
  319. """
  320. Configure check for a C or C++ type "type_name".
  321. Optional "header" can be defined to include a header file.
  322. "language" should be "C" or "C++" and is used to select the compiler.
  323. Default is "C".
  324. Sets HAVE_type_name in context.havedict according to the result.
  325. Note that this uses the current value of compiler and linker flags, make
  326. sure $CFLAGS, $CPPFLAGS and $LIBS are set correctly.
  327. Returns an empty string for success, an error message for failure.
  328. """
  329. # Include "confdefs.h" first, so that the header can use HAVE_HEADER_H.
  330. if context.headerfilename:
  331. includetext = '#include "%s"' % context.headerfilename
  332. else:
  333. includetext = ''
  334. if not header:
  335. header = ""
  336. lang, suffix, msg = _lang2suffix(language)
  337. if msg:
  338. context.Display("Cannot check for %s type: %s\n" % (type_name, msg))
  339. return msg
  340. # Remarks from autoconf about this test:
  341. # - Grepping for the type in include files is not reliable (grep isn't
  342. # portable anyway).
  343. # - Using "TYPE my_var;" doesn't work for const qualified types in C++.
  344. # Adding an initializer is not valid for some C++ classes.
  345. # - Using the type as parameter to a function either fails for K&$ C or for
  346. # C++.
  347. # - Using "TYPE *my_var;" is valid in C for some types that are not
  348. # declared (struct something).
  349. # - Using "sizeof(TYPE)" is valid when TYPE is actually a variable.
  350. # - Using the previous two together works reliably.
  351. text = """
  352. %(include)s
  353. %(header)s
  354. int main() {
  355. if ((%(name)s *) 0)
  356. return 0;
  357. if (sizeof (%(name)s))
  358. return 0;
  359. }
  360. """ % { 'include': includetext,
  361. 'header': header,
  362. 'name': type_name }
  363. context.Display("Checking for %s type %s... " % (lang, type_name))
  364. ret = context.BuildProg(text, suffix)
  365. _YesNoResult(context, ret, "HAVE_" + type_name, text,
  366. "Define to 1 if the system has the type `%s'." % type_name)
  367. if ret and fallback and context.headerfilename:
  368. f = open(context.headerfilename, "a")
  369. f.write("typedef %s %s;\n" % (fallback, type_name))
  370. f.close()
  371. return ret
  372. def CheckTypeSize(context, type_name, header = None, language = None, expect = None):
  373. """This check can be used to get the size of a given type, or to check whether
  374. the type is of expected size.
  375. Arguments:
  376. - type : str
  377. the type to check
  378. - includes : sequence
  379. list of headers to include in the test code before testing the type
  380. - language : str
  381. 'C' or 'C++'
  382. - expect : int
  383. if given, will test wether the type has the given number of bytes.
  384. If not given, will automatically find the size.
  385. Returns:
  386. status : int
  387. 0 if the check failed, or the found size of the type if the check succeeded."""
  388. # Include "confdefs.h" first, so that the header can use HAVE_HEADER_H.
  389. if context.headerfilename:
  390. includetext = '#include "%s"' % context.headerfilename
  391. else:
  392. includetext = ''
  393. if not header:
  394. header = ""
  395. lang, suffix, msg = _lang2suffix(language)
  396. if msg:
  397. context.Display("Cannot check for %s type: %s\n" % (type_name, msg))
  398. return msg
  399. src = includetext + header
  400. if not expect is None:
  401. # Only check if the given size is the right one
  402. context.Display('Checking %s is %d bytes... ' % (type_name, expect))
  403. # test code taken from autoconf: this is a pretty clever hack to find that
  404. # a type is of a given size using only compilation. This speeds things up
  405. # quite a bit compared to straightforward code using TryRun
  406. src = src + r"""
  407. typedef %s scons_check_type;
  408. int main()
  409. {
  410. static int test_array[1 - 2 * !(((long int) (sizeof(scons_check_type))) == %d)];
  411. test_array[0] = 0;
  412. return 0;
  413. }
  414. """
  415. st = context.CompileProg(src % (type_name, expect), suffix)
  416. if not st:
  417. context.Display("yes\n")
  418. _Have(context, "SIZEOF_%s" % type_name, expect,
  419. "The size of `%s', as computed by sizeof." % type_name)
  420. return expect
  421. else:
  422. context.Display("no\n")
  423. _LogFailed(context, src, st)
  424. return 0
  425. else:
  426. # Only check if the given size is the right one
  427. context.Message('Checking size of %s ... ' % type_name)
  428. # We have to be careful with the program we wish to test here since
  429. # compilation will be attempted using the current environment's flags.
  430. # So make sure that the program will compile without any warning. For
  431. # example using: 'int main(int argc, char** argv)' will fail with the
  432. # '-Wall -Werror' flags since the variables argc and argv would not be
  433. # used in the program...
  434. #
  435. src = src + """
  436. #include <stdlib.h>
  437. #include <stdio.h>
  438. int main() {
  439. printf("%d", (int)sizeof(""" + type_name + """));
  440. return 0;
  441. }
  442. """
  443. st, out = context.RunProg(src, suffix)
  444. try:
  445. size = int(out)
  446. except ValueError:
  447. # If cannot convert output of test prog to an integer (the size),
  448. # something went wront, so just fail
  449. st = 1
  450. size = 0
  451. if not st:
  452. context.Display("yes\n")
  453. _Have(context, "SIZEOF_%s" % type_name, size,
  454. "The size of `%s', as computed by sizeof." % type_name)
  455. return size
  456. else:
  457. context.Display("no\n")
  458. _LogFailed(context, src, st)
  459. return 0
  460. return 0
  461. def CheckDeclaration(context, symbol, includes = None, language = None):
  462. """Checks whether symbol is declared.
  463. Use the same test as autoconf, that is test whether the symbol is defined
  464. as a macro or can be used as an r-value.
  465. Arguments:
  466. symbol : str
  467. the symbol to check
  468. includes : str
  469. Optional "header" can be defined to include a header file.
  470. language : str
  471. only C and C++ supported.
  472. Returns:
  473. status : bool
  474. True if the check failed, False if succeeded."""
  475. # Include "confdefs.h" first, so that the header can use HAVE_HEADER_H.
  476. if context.headerfilename:
  477. includetext = '#include "%s"' % context.headerfilename
  478. else:
  479. includetext = ''
  480. if not includes:
  481. includes = ""
  482. lang, suffix, msg = _lang2suffix(language)
  483. if msg:
  484. context.Display("Cannot check for declaration %s: %s\n" % (type_name, msg))
  485. return msg
  486. src = includetext + includes
  487. context.Display('Checking whether %s is declared... ' % symbol)
  488. src = src + r"""
  489. int main()
  490. {
  491. #ifndef %s
  492. (void) %s;
  493. #endif
  494. ;
  495. return 0;
  496. }
  497. """ % (symbol, symbol)
  498. st = context.CompileProg(src, suffix)
  499. _YesNoResult(context, st, "HAVE_DECL_" + symbol, src,
  500. "Set to 1 if %s is defined." % symbol)
  501. return st
  502. def CheckLib(context, libs, func_name = None, header = None,
  503. extra_libs = None, call = None, language = None, autoadd = 1,
  504. append = True):
  505. """
  506. Configure check for a C or C++ libraries "libs". Searches through
  507. the list of libraries, until one is found where the test succeeds.
  508. Tests if "func_name" or "call" exists in the library. Note: if it exists
  509. in another library the test succeeds anyway!
  510. Optional "header" can be defined to include a header file. If not given a
  511. default prototype for "func_name" is added.
  512. Optional "extra_libs" is a list of library names to be added after
  513. "lib_name" in the build command. To be used for libraries that "lib_name"
  514. depends on.
  515. Optional "call" replaces the call to "func_name" in the test code. It must
  516. consist of complete C statements, including a trailing ";".
  517. Both "func_name" and "call" arguments are optional, and in that case, just
  518. linking against the libs is tested.
  519. "language" should be "C" or "C++" and is used to select the compiler.
  520. Default is "C".
  521. Note that this uses the current value of compiler and linker flags, make
  522. sure $CFLAGS, $CPPFLAGS and $LIBS are set correctly.
  523. Returns an empty string for success, an error message for failure.
  524. """
  525. # Include "confdefs.h" first, so that the header can use HAVE_HEADER_H.
  526. if context.headerfilename:
  527. includetext = '#include "%s"' % context.headerfilename
  528. else:
  529. includetext = ''
  530. if not header:
  531. header = ""
  532. text = """
  533. %s
  534. %s""" % (includetext, header)
  535. # Add a function declaration if needed.
  536. if func_name and func_name != "main":
  537. if not header:
  538. text = text + """
  539. #ifdef __cplusplus
  540. extern "C"
  541. #endif
  542. char %s();
  543. """ % func_name
  544. # The actual test code.
  545. if not call:
  546. call = "%s();" % func_name
  547. # if no function to test, leave main() blank
  548. text = text + """
  549. int
  550. main() {
  551. %s
  552. return 0;
  553. }
  554. """ % (call or "")
  555. if call:
  556. i = call.find("\n")
  557. if i > 0:
  558. calltext = call[:i] + ".."
  559. elif call[-1] == ';':
  560. calltext = call[:-1]
  561. else:
  562. calltext = call
  563. for lib_name in libs:
  564. lang, suffix, msg = _lang2suffix(language)
  565. if msg:
  566. context.Display("Cannot check for library %s: %s\n" % (lib_name, msg))
  567. return msg
  568. # if a function was specified to run in main(), say it
  569. if call:
  570. context.Display("Checking for %s in %s library %s... "
  571. % (calltext, lang, lib_name))
  572. # otherwise, just say the name of library and language
  573. else:
  574. context.Display("Checking for %s library %s... "
  575. % (lang, lib_name))
  576. if lib_name:
  577. l = [ lib_name ]
  578. if extra_libs:
  579. l.extend(extra_libs)
  580. if append:
  581. oldLIBS = context.AppendLIBS(l)
  582. else:
  583. oldLIBS = context.PrependLIBS(l)
  584. sym = "HAVE_LIB" + lib_name
  585. else:
  586. oldLIBS = -1
  587. sym = None
  588. ret = context.BuildProg(text, suffix)
  589. _YesNoResult(context, ret, sym, text,
  590. "Define to 1 if you have the `%s' library." % lib_name)
  591. if oldLIBS != -1 and (ret or not autoadd):
  592. context.SetLIBS(oldLIBS)
  593. if not ret:
  594. return ret
  595. return ret
  596. #
  597. # END OF PUBLIC FUNCTIONS
  598. #
  599. def _YesNoResult(context, ret, key, text, comment = None):
  600. """
  601. Handle the result of a test with a "yes" or "no" result.
  602. "ret" is the return value: empty if OK, error message when not.
  603. "key" is the name of the symbol to be defined (HAVE_foo).
  604. "text" is the source code of the program used for testing.
  605. "comment" is the C comment to add above the line defining the symbol (the
  606. comment is automatically put inside a /* */). If None, no comment is added.
  607. """
  608. if key:
  609. _Have(context, key, not ret, comment)
  610. if ret:
  611. context.Display("no\n")
  612. _LogFailed(context, text, ret)
  613. else:
  614. context.Display("yes\n")
  615. def _Have(context, key, have, comment = None):
  616. """
  617. Store result of a test in context.havedict and context.headerfilename.
  618. "key" is a "HAVE_abc" name. It is turned into all CAPITALS and non-
  619. alphanumerics are replaced by an underscore.
  620. The value of "have" can be:
  621. 1 - Feature is defined, add "#define key".
  622. 0 - Feature is not defined, add "/* #undef key */".
  623. Adding "undef" is what autoconf does. Not useful for the
  624. compiler, but it shows that the test was done.
  625. number - Feature is defined to this number "#define key have".
  626. Doesn't work for 0 or 1, use a string then.
  627. string - Feature is defined to this string "#define key have".
  628. Give "have" as is should appear in the header file, include quotes
  629. when desired and escape special characters!
  630. """
  631. key_up = key.upper()
  632. key_up = re.sub('[^A-Z0-9_]', '_', key_up)
  633. context.havedict[key_up] = have
  634. if have == 1:
  635. line = "#define %s 1\n" % key_up
  636. elif have == 0:
  637. line = "/* #undef %s */\n" % key_up
  638. elif isinstance(have, IntType):
  639. line = "#define %s %d\n" % (key_up, have)
  640. else:
  641. line = "#define %s %s\n" % (key_up, str(have))
  642. if comment is not None:
  643. lines = "\n/* %s */\n" % comment + line
  644. else:
  645. lines = "\n" + line
  646. if context.headerfilename:
  647. f = open(context.headerfilename, "a")
  648. f.write(lines)
  649. f.close()
  650. elif hasattr(context,'config_h'):
  651. context.config_h = context.config_h + lines
  652. def _LogFailed(context, text, msg):
  653. """
  654. Write to the log about a failed program.
  655. Add line numbers, so that error messages can be understood.
  656. """
  657. if LogInputFiles:
  658. context.Log("Failed program was:\n")
  659. lines = text.split('\n')
  660. if len(lines) and lines[-1] == '':
  661. lines = lines[:-1] # remove trailing empty line
  662. n = 1
  663. for line in lines:
  664. context.Log("%d: %s\n" % (n, line))
  665. n = n + 1
  666. if LogErrorMessages:
  667. context.Log("Error message: %s\n" % msg)
  668. def _lang2suffix(lang):
  669. """
  670. Convert a language name to a suffix.
  671. When "lang" is empty or None C is assumed.
  672. Returns a tuple (lang, suffix, None) when it works.
  673. For an unrecognized language returns (None, None, msg).
  674. Where:
  675. lang = the unified language name
  676. suffix = the suffix, including the leading dot
  677. msg = an error message
  678. """
  679. if not lang or lang in ["C", "c"]:
  680. return ("C", ".c", None)
  681. if lang in ["c++", "C++", "cpp", "CXX", "cxx"]:
  682. return ("C++", ".cpp", None)
  683. return None, None, "Unsupported language: %s" % lang
  684. # vim: set sw=4 et sts=4 tw=79 fo+=l:
  685. # Local Variables:
  686. # tab-width:4
  687. # indent-tabs-mode:nil
  688. # End:
  689. # vim: set expandtab tabstop=4 shiftwidth=4: