compilers-util.configure 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. @template
  6. @imports('textwrap')
  7. @imports(_from='mozbuild.configure', _import='SandboxDependsFunction')
  8. def compiler_class(compiler):
  9. class Compiler(SandboxDependsFunction):
  10. # Generates a test program and attempts to compile it. In case of
  11. # failure, the resulting check will return None. If the test program
  12. # succeeds, it will return the output of the test program.
  13. # - `includes` are the includes (as file names) that will appear at the
  14. # top of the generated test program.
  15. # - `body` is the code that will appear in the main function of the
  16. # generated test program. `return 0;` is appended to the function
  17. # body automatically.
  18. # - `flags` are the flags to be passed to the compiler, in addition to
  19. # `-c`.
  20. # - `check_msg` is the message to be printed to accompany compiling the
  21. # test program.
  22. def try_compile(self, includes=None, body='', flags=None,
  23. check_msg=None, when=None, onerror=lambda: None):
  24. includes = includes or []
  25. source_lines = ['#include <%s>' % f for f in includes]
  26. source = '\n'.join(source_lines) + '\n'
  27. source += textwrap.dedent('''\
  28. int
  29. main(void)
  30. {
  31. %s
  32. ;
  33. return 0;
  34. }
  35. ''' % body)
  36. if check_msg:
  37. def checking_fn(fn):
  38. return checking(check_msg)(fn)
  39. else:
  40. def checking_fn(fn):
  41. return fn
  42. @depends_when(self, dependable(flags), extra_toolchain_flags, when=when)
  43. @checking_fn
  44. def func(compiler, flags, extra_flags):
  45. flags = flags or []
  46. flags += extra_flags or []
  47. flags.append('-c')
  48. if try_invoke_compiler(
  49. compiler.wrapper + [compiler.compiler] + compiler.flags,
  50. compiler.language, source, flags,
  51. onerror=onerror) is not None:
  52. return True
  53. return func
  54. compiler.__class__ = Compiler
  55. return compiler