pkg.configure 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. @depends('--enable-compile-environment')
  6. def pkg_config(compile_env):
  7. if compile_env:
  8. return ('pkg-config',)
  9. pkg_config = check_prog('PKG_CONFIG', pkg_config, allow_missing=True)
  10. @depends_if(pkg_config)
  11. @checking('for pkg-config version')
  12. @imports('subprocess')
  13. def pkg_config_version(pkg_config):
  14. return Version(check_cmd_output(pkg_config, '--version').rstrip())
  15. # Locates the given module using pkg-config.
  16. # - `var` determines the name of variables to set when the package is found.
  17. # <var>_CFLAGS and <var>_LIBS are set with corresponding values.
  18. # - `package_desc` package name and version requirement string, list of
  19. # strings describing packages to locate, or depends function that will
  20. # resolve to such a string or list of strings.
  21. # - `when` a depends function that will determine whether to perform
  22. # any checks (default is to always perform checks).
  23. # - `allow_missing` If set, failure to fulfill the package description
  24. # will not result in an error or logged message, and any error message
  25. # will be returned to the caller.
  26. # Returns `True` when the package description is fulfilled.
  27. @template
  28. def pkg_check_modules(var, package_desc, when=always,
  29. allow_missing=False):
  30. if isinstance(package_desc, (tuple, list)):
  31. package_desc = ' '.join(package_desc)
  32. package_desc = dependable(package_desc)
  33. @depends(when, '--enable-compile-environment')
  34. def when_and_compile_environment(when, compile_environment):
  35. return when and compile_environment
  36. @depends_when(pkg_config, pkg_config_version,
  37. when=when_and_compile_environment)
  38. def check_pkg_config(pkg_config, version):
  39. min_version = '0.9.0'
  40. if pkg_config is None:
  41. die("*** The pkg-config script could not be found. Make sure it is\n"
  42. "*** in your path, or set the PKG_CONFIG environment variable\n"
  43. "*** to the full path to pkg-config.")
  44. if version < min_version:
  45. die("*** Your version of pkg-config is too old. You need version %s or newer.",
  46. min_version)
  47. @depends_when(pkg_config, package_desc, when=when_and_compile_environment)
  48. @imports('subprocess')
  49. @imports('sys')
  50. @imports(_from='mozbuild.configure.util', _import='LineIO')
  51. def package(pkg_config, package_desc):
  52. # package_desc may start as a depends function, so we can't use
  53. # @checking here.
  54. log.info("checking for %s... " % package_desc)
  55. with log.queue_debug():
  56. try:
  57. subprocess.check_output([pkg_config, '--errors-to-stdout',
  58. '--print-errors', package_desc])
  59. log.info("yes")
  60. return True
  61. except subprocess.CalledProcessError as e:
  62. log.info("no")
  63. log_writer = log.warning if allow_missing else log.error
  64. with LineIO(lambda l: log_writer(l)) as o:
  65. o.write(e.output)
  66. if not allow_missing:
  67. sys.exit(1)
  68. @depends_when(pkg_config, package_desc, when=package)
  69. @checking('%s_CFLAGS' % var, callback=lambda t: ' '.join(t))
  70. def pkg_cflags(pkg_config, package_desc):
  71. flags = check_cmd_output(pkg_config, '--cflags', package_desc)
  72. return tuple(flags.split())
  73. @depends_when(pkg_config, package_desc, when=package)
  74. @checking('%s_LIBS' % var, callback=lambda t: ' '.join(t))
  75. def pkg_libs(pkg_config, package_desc):
  76. libs = check_cmd_output(pkg_config, '--libs', package_desc)
  77. # Remove evil flags like -Wl,--export-dynamic
  78. return tuple(libs.replace('-Wl,--export-dynamic', '').split())
  79. @depends_when(pkg_cflags, pkg_libs, when=package)
  80. def pkg_info(cflags, libs):
  81. return namespace(cflags=cflags, libs=libs)
  82. set_config('%s_CFLAGS' % var, pkg_cflags)
  83. set_config('%s_LIBS' % var, pkg_libs)
  84. return pkg_info