pkgconfig.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Copyright 2015 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import os
  12. from .. import build
  13. from .. import mesonlib
  14. from .. import mlog
  15. from . import ModuleReturnValue
  16. from . import ExtensionModule
  17. class PkgConfigModule(ExtensionModule):
  18. def _get_lname(self, l, msg, pcfile):
  19. # Nothing special
  20. if not l.name_prefix_set:
  21. return l.name
  22. # Sometimes people want the library to start with 'lib' everywhere,
  23. # which is achieved by setting name_prefix to '' and the target name to
  24. # 'libfoo'. In that case, try to get the pkg-config '-lfoo' arg correct.
  25. if l.prefix == '' and l.name.startswith('lib'):
  26. return l.name[3:]
  27. # If the library is imported via an import library which is always
  28. # named after the target name, '-lfoo' is correct.
  29. if l.import_filename:
  30. return l.name
  31. # In other cases, we can't guarantee that the compiler will be able to
  32. # find the library via '-lfoo', so tell the user that.
  33. mlog.warning(msg.format(l.name, 'name_prefix', l.name, pcfile))
  34. return l.name
  35. def generate_pkgconfig_file(self, state, libraries, subdirs, name, description,
  36. url, version, pcfile, pub_reqs, priv_reqs,
  37. conflicts, priv_libs, variables):
  38. coredata = state.environment.get_coredata()
  39. outdir = state.environment.scratch_dir
  40. fname = os.path.join(outdir, pcfile)
  41. with open(fname, 'w') as ofile:
  42. ofile.write('prefix=%s\n' % coredata.get_builtin_option('prefix'))
  43. # '${prefix}' is ignored if the second path is absolute (see
  44. # 'os.path.join' for details)
  45. ofile.write('libdir=%s\n' % os.path.join('${prefix}', coredata.get_builtin_option('libdir')))
  46. ofile.write('includedir=%s\n' % os.path.join('${prefix}', coredata.get_builtin_option('includedir')))
  47. for k, v in variables:
  48. ofile.write('%s=%s\n' % (k, v))
  49. ofile.write('\n')
  50. ofile.write('Name: %s\n' % name)
  51. if len(description) > 0:
  52. ofile.write('Description: %s\n' % description)
  53. if len(url) > 0:
  54. ofile.write('URL: %s\n' % url)
  55. ofile.write('Version: %s\n' % version)
  56. if len(pub_reqs) > 0:
  57. ofile.write('Requires: {}\n'.format(' '.join(pub_reqs)))
  58. if len(priv_reqs) > 0:
  59. ofile.write(
  60. 'Requires.private: {}\n'.format(' '.join(priv_reqs)))
  61. if len(conflicts) > 0:
  62. ofile.write('Conflicts: {}\n'.format(' '.join(conflicts)))
  63. def generate_libs_flags(libs):
  64. msg = 'Library target {0!r} has {1!r} set. Compilers ' \
  65. 'may not find it from its \'-l{2}\' linker flag in the ' \
  66. '{3!r} pkg-config file.'
  67. for l in libs:
  68. if isinstance(l, str):
  69. yield l
  70. else:
  71. install_dir = l.get_custom_install_dir()[0]
  72. if install_dir:
  73. yield '-L${prefix}/%s ' % install_dir
  74. else:
  75. yield '-L${libdir}'
  76. lname = self._get_lname(l, msg, pcfile)
  77. # If using a custom suffix, the compiler may not be able to
  78. # find the library
  79. if l.name_suffix_set:
  80. mlog.warning(msg.format(l.name, 'name_suffix', lname, pcfile))
  81. yield '-l%s' % lname
  82. if len(libraries) > 0:
  83. ofile.write('Libs: {}\n'.format(' '.join(generate_libs_flags(libraries))))
  84. if len(priv_libs) > 0:
  85. ofile.write('Libs.private: {}\n'.format(' '.join(generate_libs_flags(priv_libs))))
  86. ofile.write('Cflags:')
  87. for h in subdirs:
  88. if h == '.':
  89. h = ''
  90. ofile.write(' ')
  91. ofile.write(os.path.join('-I${includedir}', h))
  92. ofile.write('\n')
  93. def process_libs(self, libs):
  94. if not isinstance(libs, list):
  95. libs = [libs]
  96. processed_libs = []
  97. for l in libs:
  98. if hasattr(l, 'held_object'):
  99. l = l.held_object
  100. if not isinstance(l, (build.SharedLibrary, build.StaticLibrary, str)):
  101. raise mesonlib.MesonException('Library argument not a library object nor a string.')
  102. processed_libs.append(l)
  103. return processed_libs
  104. def generate(self, state, args, kwargs):
  105. if len(args) > 0:
  106. raise mesonlib.MesonException('Pkgconfig_gen takes no positional arguments.')
  107. libs = self.process_libs(kwargs.get('libraries', []))
  108. priv_libs = self.process_libs(kwargs.get('libraries_private', []))
  109. subdirs = mesonlib.stringlistify(kwargs.get('subdirs', ['.']))
  110. version = kwargs.get('version', None)
  111. if not isinstance(version, str):
  112. raise mesonlib.MesonException('Version must be specified.')
  113. name = kwargs.get('name', None)
  114. if not isinstance(name, str):
  115. raise mesonlib.MesonException('Name not specified.')
  116. filebase = kwargs.get('filebase', name)
  117. if not isinstance(filebase, str):
  118. raise mesonlib.MesonException('Filebase must be a string.')
  119. description = kwargs.get('description', None)
  120. if not isinstance(description, str):
  121. raise mesonlib.MesonException('Description is not a string.')
  122. url = kwargs.get('url', '')
  123. if not isinstance(url, str):
  124. raise mesonlib.MesonException('URL is not a string.')
  125. pub_reqs = mesonlib.stringlistify(kwargs.get('requires', []))
  126. priv_reqs = mesonlib.stringlistify(kwargs.get('requires_private', []))
  127. conflicts = mesonlib.stringlistify(kwargs.get('conflicts', []))
  128. def parse_variable_list(stringlist):
  129. reserved = ['prefix', 'libdir', 'includedir']
  130. variables = []
  131. for var in stringlist:
  132. # foo=bar=baz is ('foo', 'bar=baz')
  133. l = var.split('=', 1)
  134. if len(l) < 2:
  135. raise mesonlib.MesonException('Variables must be in \'name=value\' format')
  136. name, value = l[0].strip(), l[1].strip()
  137. if not name or not value:
  138. raise mesonlib.MesonException('Variables must be in \'name=value\' format')
  139. # Variable names must not contain whitespaces
  140. if any(c.isspace() for c in name):
  141. raise mesonlib.MesonException('Invalid whitespace in assignment "{}"'.format(var))
  142. if name in reserved:
  143. raise mesonlib.MesonException('Variable "{}" is reserved'.format(name))
  144. variables.append((name, value))
  145. return variables
  146. variables = parse_variable_list(mesonlib.stringlistify(kwargs.get('variables', [])))
  147. pcfile = filebase + '.pc'
  148. pkgroot = kwargs.get('install_dir', None)
  149. if pkgroot is None:
  150. pkgroot = os.path.join(state.environment.coredata.get_builtin_option('libdir'), 'pkgconfig')
  151. if not isinstance(pkgroot, str):
  152. raise mesonlib.MesonException('Install_dir must be a string.')
  153. self.generate_pkgconfig_file(state, libs, subdirs, name, description, url,
  154. version, pcfile, pub_reqs, priv_reqs,
  155. conflicts, priv_libs, variables)
  156. res = build.Data(mesonlib.File(True, state.environment.get_scratch_dir(), pcfile), pkgroot)
  157. return ModuleReturnValue(res, [res])
  158. def initialize():
  159. return PkgConfigModule()