gnome.py 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. # Copyright 2015-2016 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. '''This module provides helper functions for Gnome/GLib related
  12. functionality such as gobject-introspection and gresources.'''
  13. from .. import build
  14. import os
  15. import sys
  16. import copy
  17. import subprocess
  18. from . import ModuleReturnValue
  19. from ..mesonlib import MesonException, OrderedSet, Popen_safe
  20. from ..dependencies import Dependency, PkgConfigDependency, InternalDependency
  21. from .. import mlog
  22. from .. import mesonlib
  23. from .. import compilers
  24. from .. import interpreter
  25. from . import GResourceTarget, GResourceHeaderTarget, GirTarget, TypelibTarget, VapiTarget
  26. from . import find_program, get_include_args
  27. from . import ExtensionModule
  28. # gresource compilation is broken due to the way
  29. # the resource compiler and Ninja clash about it
  30. #
  31. # https://github.com/ninja-build/ninja/issues/1184
  32. # https://bugzilla.gnome.org/show_bug.cgi?id=774368
  33. gresource_dep_needed_version = '>= 2.51.1'
  34. native_glib_version = None
  35. girwarning_printed = False
  36. gdbuswarning_printed = False
  37. gresource_warning_printed = False
  38. _gir_has_extra_lib_arg = None
  39. def gir_has_extra_lib_arg():
  40. global _gir_has_extra_lib_arg
  41. if _gir_has_extra_lib_arg is not None:
  42. return _gir_has_extra_lib_arg
  43. _gir_has_extra_lib_arg = False
  44. try:
  45. g_ir_scanner = find_program('g-ir-scanner', '').get_command()
  46. opts = Popen_safe(g_ir_scanner + ['--help'], stderr=subprocess.STDOUT)[1]
  47. _gir_has_extra_lib_arg = '--extra-library' in opts
  48. except (MesonException, FileNotFoundError, subprocess.CalledProcessError):
  49. pass
  50. return _gir_has_extra_lib_arg
  51. class GnomeModule(ExtensionModule):
  52. gir_dep = None
  53. @staticmethod
  54. def _get_native_glib_version(state):
  55. global native_glib_version
  56. if native_glib_version is None:
  57. glib_dep = PkgConfigDependency('glib-2.0', state.environment,
  58. {'native': True})
  59. native_glib_version = glib_dep.get_version()
  60. return native_glib_version
  61. def __print_gresources_warning(self, state):
  62. global gresource_warning_printed
  63. if not gresource_warning_printed:
  64. if not mesonlib.version_compare(self._get_native_glib_version(state), gresource_dep_needed_version):
  65. mlog.warning('GLib compiled dependencies do not work reliably with \n'
  66. 'the current version of GLib. See the following upstream issue:',
  67. mlog.bold('https://bugzilla.gnome.org/show_bug.cgi?id=774368'))
  68. gresource_warning_printed = True
  69. return []
  70. @staticmethod
  71. def _print_gdbus_warning():
  72. global gdbuswarning_printed
  73. if not gdbuswarning_printed:
  74. mlog.warning('Code generated with gdbus_codegen() requires the root directory be added to\n'
  75. ' include_directories of targets with GLib < 2.51.3:',
  76. mlog.bold('https://github.com/mesonbuild/meson/issues/1387'))
  77. gdbuswarning_printed = True
  78. def compile_resources(self, state, args, kwargs):
  79. self.__print_gresources_warning(state)
  80. glib_version = self._get_native_glib_version(state)
  81. cmd = ['glib-compile-resources', '@INPUT@']
  82. source_dirs = kwargs.pop('source_dir', [])
  83. if not isinstance(source_dirs, list):
  84. source_dirs = [source_dirs]
  85. if len(args) < 2:
  86. raise MesonException('Not enough arguments; the name of the resource '
  87. 'and the path to the XML file are required')
  88. dependencies = kwargs.pop('dependencies', [])
  89. if not isinstance(dependencies, list):
  90. dependencies = [dependencies]
  91. # Validate dependencies
  92. for (ii, dep) in enumerate(dependencies):
  93. if hasattr(dep, 'held_object'):
  94. dependencies[ii] = dep = dep.held_object
  95. if not isinstance(dep, (mesonlib.File, build.CustomTarget)):
  96. m = 'Unexpected dependency type {!r} for gnome.compile_resources() ' \
  97. '"dependencies" argument.\nPlease pass the return value of ' \
  98. 'custom_target() or configure_file()'
  99. raise MesonException(m.format(dep))
  100. if isinstance(dep, build.CustomTarget):
  101. if not mesonlib.version_compare(glib_version, gresource_dep_needed_version):
  102. m = 'The "dependencies" argument of gnome.compile_resources() can not\n' \
  103. 'be used with the current version of glib-compile-resources due to\n' \
  104. '<https://bugzilla.gnome.org/show_bug.cgi?id=774368>'
  105. raise MesonException(m)
  106. ifile = args[1]
  107. if isinstance(ifile, mesonlib.File):
  108. # glib-compile-resources will be run inside the source dir,
  109. # so we need either 'src_to_build' or the absolute path.
  110. # Absolute path is the easiest choice.
  111. if ifile.is_built:
  112. ifile = os.path.join(state.environment.get_build_dir(), ifile.subdir, ifile.fname)
  113. else:
  114. ifile = os.path.join(ifile.subdir, ifile.fname)
  115. elif isinstance(ifile, str):
  116. ifile = os.path.join(state.subdir, ifile)
  117. elif isinstance(ifile, (interpreter.CustomTargetHolder,
  118. interpreter.GeneratedObjectsHolder)):
  119. m = 'Resource xml files generated at build-time cannot be used ' \
  120. 'with gnome.compile_resources() because we need to scan ' \
  121. 'the xml for dependencies. Use configure_file() instead ' \
  122. 'to generate it at configure-time.'
  123. raise MesonException(m)
  124. else:
  125. raise MesonException('Invalid file argument: {!r}'.format(ifile))
  126. depend_files, depends, subdirs = self._get_gresource_dependencies(
  127. state, ifile, source_dirs, dependencies)
  128. # Make source dirs relative to build dir now
  129. source_dirs = [os.path.join(state.build_to_src, state.subdir, d) for d in source_dirs]
  130. # Always include current directory, but after paths set by user
  131. source_dirs.append(os.path.join(state.build_to_src, state.subdir))
  132. # Ensure build directories of generated deps are included
  133. source_dirs += subdirs
  134. for source_dir in OrderedSet(source_dirs):
  135. cmd += ['--sourcedir', source_dir]
  136. if 'c_name' in kwargs:
  137. cmd += ['--c-name', kwargs.pop('c_name')]
  138. export = kwargs.pop('export', False)
  139. if not export:
  140. cmd += ['--internal']
  141. cmd += ['--generate', '--target', '@OUTPUT@']
  142. cmd += mesonlib.stringlistify(kwargs.pop('extra_args', []))
  143. gresource = kwargs.pop('gresource_bundle', False)
  144. if gresource:
  145. output = args[0] + '.gresource'
  146. name = args[0] + '_gresource'
  147. else:
  148. output = args[0] + '.c'
  149. name = args[0] + '_c'
  150. if kwargs.get('install', False) and not gresource:
  151. raise MesonException('The install kwarg only applies to gresource bundles, see install_header')
  152. install_header = kwargs.pop('install_header', False)
  153. if install_header and gresource:
  154. raise MesonException('The install_header kwarg does not apply to gresource bundles')
  155. if install_header and not export:
  156. raise MesonException('GResource header is installed yet export is not enabled')
  157. kwargs['input'] = args[1]
  158. kwargs['output'] = output
  159. kwargs['depends'] = depends
  160. if not mesonlib.version_compare(glib_version, gresource_dep_needed_version):
  161. # This will eventually go out of sync if dependencies are added
  162. kwargs['depend_files'] = depend_files
  163. kwargs['command'] = cmd
  164. else:
  165. depfile = kwargs['output'] + '.d'
  166. kwargs['depfile'] = depfile
  167. kwargs['command'] = copy.copy(cmd) + ['--dependency-file', '@DEPFILE@']
  168. target_c = GResourceTarget(name, state.subdir, kwargs)
  169. if gresource: # Only one target for .gresource files
  170. return ModuleReturnValue(target_c, [target_c])
  171. h_kwargs = {
  172. 'command': cmd,
  173. 'input': args[1],
  174. 'output': args[0] + '.h',
  175. # The header doesn't actually care about the files yet it errors if missing
  176. 'depends': depends
  177. }
  178. if install_header:
  179. h_kwargs['install'] = install_header
  180. h_kwargs['install_dir'] = kwargs.get('install_dir',
  181. state.environment.coredata.get_builtin_option('includedir'))
  182. target_h = GResourceHeaderTarget(args[0] + '_h', state.subdir, h_kwargs)
  183. rv = [target_c, target_h]
  184. return ModuleReturnValue(rv, rv)
  185. def _get_gresource_dependencies(self, state, input_file, source_dirs, dependencies):
  186. cmd = ['glib-compile-resources',
  187. input_file,
  188. '--generate-dependencies']
  189. for source_dir in source_dirs:
  190. cmd += ['--sourcedir', os.path.join(state.subdir, source_dir)]
  191. cmd += ['--sourcedir', state.subdir] # Current dir
  192. pc, stdout, stderr = Popen_safe(cmd, cwd=state.environment.get_source_dir())
  193. if pc.returncode != 0:
  194. m = 'glib-compile-resources failed to get dependencies for {}:\n{}'
  195. mlog.warning(m.format(cmd[1], stderr))
  196. raise subprocess.CalledProcessError(pc.returncode, cmd)
  197. dep_files = stdout.split('\n')[:-1]
  198. # In generate-dependencies mode, glib-compile-resources doesn't raise
  199. # an error for missing resources but instead prints whatever filename
  200. # was listed in the input file. That's good because it means we can
  201. # handle resource files that get generated as part of the build, as
  202. # follows.
  203. #
  204. # If there are multiple generated resource files with the same basename
  205. # then this code will get confused.
  206. def exists_in_srcdir(f):
  207. return os.path.exists(os.path.join(state.environment.get_source_dir(), f))
  208. missing_dep_files = [f for f in dep_files if not exists_in_srcdir(f)]
  209. depends = []
  210. subdirs = []
  211. for missing in missing_dep_files:
  212. found = False
  213. missing_basename = os.path.basename(missing)
  214. for dep in dependencies:
  215. if hasattr(dep, 'held_object'):
  216. dep = dep.held_object
  217. if isinstance(dep, mesonlib.File):
  218. if dep.fname == missing_basename:
  219. found = True
  220. dep_files.remove(missing)
  221. dep_files.append(dep)
  222. subdirs.append(dep.subdir)
  223. break
  224. elif isinstance(dep, build.CustomTarget):
  225. if dep.get_basename() == missing_basename:
  226. found = True
  227. dep_files.remove(missing)
  228. dep_files.append(
  229. mesonlib.File(
  230. is_built=True,
  231. subdir=dep.get_subdir(),
  232. fname=dep.get_basename()))
  233. depends.append(dep)
  234. subdirs.append(dep.get_subdir())
  235. break
  236. else:
  237. raise RuntimeError('Unreachable code.')
  238. if not found:
  239. raise MesonException(
  240. 'Resource "%s" listed in "%s" was not found. If this is a '
  241. 'generated file, pass the target that generates it to '
  242. 'gnome.compile_resources() using the "dependencies" '
  243. 'keyword argument.' % (missing, input_file))
  244. return dep_files, depends, subdirs
  245. def _get_link_args(self, state, lib, depends=None, include_rpath=False,
  246. use_gir_args=False):
  247. # Construct link args
  248. if gir_has_extra_lib_arg() and use_gir_args:
  249. link_command = ['--extra-library=' + lib.name]
  250. else:
  251. link_command = ['-l' + lib.name]
  252. if isinstance(lib, build.SharedLibrary):
  253. libdir = state.backend.get_target_dir(lib)
  254. link_command.append('-L' + libdir)
  255. # Needed for the following binutils bug:
  256. # https://github.com/mesonbuild/meson/issues/1911
  257. # However, g-ir-scanner does not understand -Wl,-rpath
  258. # so we need to use -L instead
  259. for d in state.backend.determine_rpath_dirs(lib):
  260. d = os.path.join(state.environment.get_build_dir(), d)
  261. link_command.append('-L' + d)
  262. if include_rpath:
  263. link_command.append('-Wl,-rpath,' + libdir)
  264. if depends:
  265. depends.append(lib)
  266. return link_command
  267. def _get_dependencies_flags(self, deps, state, depends=None, include_rpath=False,
  268. use_gir_args=False):
  269. cflags = OrderedSet()
  270. ldflags = OrderedSet()
  271. gi_includes = OrderedSet()
  272. if not isinstance(deps, list):
  273. deps = [deps]
  274. for dep in deps:
  275. if hasattr(dep, 'held_object'):
  276. dep = dep.held_object
  277. if isinstance(dep, InternalDependency):
  278. cflags.update(get_include_args(dep.include_directories))
  279. for lib in dep.libraries:
  280. ldflags.update(self._get_link_args(state, lib.held_object, depends, include_rpath))
  281. libdepflags = self._get_dependencies_flags(lib.held_object.get_external_deps(), state, depends, include_rpath,
  282. use_gir_args)
  283. cflags.update(libdepflags[0])
  284. ldflags.update(libdepflags[1])
  285. gi_includes.update(libdepflags[2])
  286. extdepflags = self._get_dependencies_flags(dep.ext_deps, state, depends, include_rpath,
  287. use_gir_args)
  288. cflags.update(extdepflags[0])
  289. ldflags.update(extdepflags[1])
  290. gi_includes.update(extdepflags[2])
  291. for source in dep.sources:
  292. if hasattr(source, 'held_object') and isinstance(source.held_object, GirTarget):
  293. gi_includes.update([os.path.join(state.environment.get_build_dir(),
  294. source.held_object.get_subdir())])
  295. # This should be any dependency other than an internal one.
  296. elif isinstance(dep, Dependency):
  297. cflags.update(dep.get_compile_args())
  298. for lib in dep.get_link_args():
  299. if (os.path.isabs(lib) and
  300. # For PkgConfigDependency only:
  301. getattr(dep, 'is_libtool', False)):
  302. lib_dir = os.path.dirname(lib)
  303. ldflags.update(["-L%s" % lib_dir])
  304. if include_rpath:
  305. ldflags.update(['-Wl,-rpath {}'.format(lib_dir)])
  306. libname = os.path.basename(lib)
  307. if libname.startswith("lib"):
  308. libname = libname[3:]
  309. libname = libname.split(".so")[0]
  310. lib = "-l%s" % libname
  311. # Hack to avoid passing some compiler options in
  312. if lib.startswith("-W"):
  313. continue
  314. if gir_has_extra_lib_arg() and use_gir_args:
  315. lib = lib.replace('-l', '--extra-library=')
  316. ldflags.update([lib])
  317. if isinstance(dep, PkgConfigDependency):
  318. girdir = dep.get_pkgconfig_variable("girdir")
  319. if girdir:
  320. gi_includes.update([girdir])
  321. elif isinstance(dep, (build.StaticLibrary, build.SharedLibrary)):
  322. for incd in dep.get_include_dirs():
  323. for idir in incd.get_incdirs():
  324. cflags.update(["-I%s" % idir])
  325. else:
  326. mlog.log('dependency %s not handled to build gir files' % dep)
  327. continue
  328. return cflags, ldflags, gi_includes
  329. def generate_gir(self, state, args, kwargs):
  330. if len(args) != 1:
  331. raise MesonException('Gir takes one argument')
  332. if kwargs.get('install_dir'):
  333. raise MesonException('install_dir is not supported with generate_gir(), see "install_dir_gir" and "install_dir_typelib"')
  334. giscanner = find_program('g-ir-scanner', 'Gir')
  335. gicompiler = find_program('g-ir-compiler', 'Gir')
  336. girtarget = args[0]
  337. while hasattr(girtarget, 'held_object'):
  338. girtarget = girtarget.held_object
  339. if not isinstance(girtarget, (build.Executable, build.SharedLibrary)):
  340. raise MesonException('Gir target must be an executable or shared library')
  341. try:
  342. if not self.gir_dep:
  343. self.gir_dep = PkgConfigDependency('gobject-introspection-1.0',
  344. state.environment,
  345. {'native': True})
  346. pkgargs = self.gir_dep.get_compile_args()
  347. except Exception:
  348. raise MesonException('gobject-introspection dependency was not found, gir cannot be generated.')
  349. ns = kwargs.pop('namespace')
  350. nsversion = kwargs.pop('nsversion')
  351. libsources = kwargs.pop('sources')
  352. girfile = '%s-%s.gir' % (ns, nsversion)
  353. depends = [girtarget]
  354. gir_inc_dirs = []
  355. scan_command = [giscanner, '@INPUT@']
  356. scan_command += pkgargs
  357. scan_command += ['--no-libtool', '--namespace=' + ns, '--nsversion=' + nsversion, '--warn-all',
  358. '--output', '@OUTPUT@']
  359. extra_args = mesonlib.stringlistify(kwargs.pop('extra_args', []))
  360. scan_command += extra_args
  361. scan_command += ['-I' + os.path.join(state.environment.get_source_dir(), state.subdir),
  362. '-I' + os.path.join(state.environment.get_build_dir(), state.subdir)]
  363. scan_command += get_include_args(girtarget.get_include_dirs())
  364. if 'link_with' in kwargs:
  365. link_with = kwargs.pop('link_with')
  366. if not isinstance(link_with, list):
  367. link_with = [link_with]
  368. for link in link_with:
  369. scan_command += self._get_link_args(state, link.held_object, depends,
  370. use_gir_args=True)
  371. if 'includes' in kwargs:
  372. includes = kwargs.pop('includes')
  373. if not isinstance(includes, list):
  374. includes = [includes]
  375. for inc in includes:
  376. if hasattr(inc, 'held_object'):
  377. inc = inc.held_object
  378. if isinstance(inc, str):
  379. scan_command += ['--include=%s' % (inc, )]
  380. elif isinstance(inc, GirTarget):
  381. gir_inc_dirs += [
  382. os.path.join(state.environment.get_build_dir(),
  383. inc.get_subdir()),
  384. ]
  385. scan_command += [
  386. "--include=%s" % (inc.get_basename()[:-4], ),
  387. ]
  388. depends += [inc]
  389. else:
  390. raise MesonException(
  391. 'Gir includes must be str, GirTarget, or list of them')
  392. cflags = []
  393. for lang, compiler in girtarget.compilers.items():
  394. # XXX: Can you use g-i with any other language?
  395. if lang in ('c', 'cpp', 'objc', 'objcpp', 'd'):
  396. break
  397. else:
  398. lang = None
  399. compiler = None
  400. if lang and compiler:
  401. if state.global_args.get(lang):
  402. cflags += state.global_args[lang]
  403. if state.project_args.get(lang):
  404. cflags += state.project_args[lang]
  405. sanitize = compiler.get_options().get('b_sanitize')
  406. if sanitize:
  407. cflags += compilers.sanitizer_compile_args(sanitize)
  408. if kwargs.get('symbol_prefix'):
  409. sym_prefix = kwargs.pop('symbol_prefix')
  410. if not isinstance(sym_prefix, str):
  411. raise MesonException('Gir symbol prefix must be str')
  412. scan_command += ['--symbol-prefix=%s' % sym_prefix]
  413. if kwargs.get('identifier_prefix'):
  414. identifier_prefix = kwargs.pop('identifier_prefix')
  415. if not isinstance(identifier_prefix, str):
  416. raise MesonException('Gir identifier prefix must be str')
  417. scan_command += ['--identifier-prefix=%s' % identifier_prefix]
  418. if kwargs.get('export_packages'):
  419. pkgs = kwargs.pop('export_packages')
  420. if isinstance(pkgs, str):
  421. scan_command += ['--pkg-export=%s' % pkgs]
  422. elif isinstance(pkgs, list):
  423. scan_command += ['--pkg-export=%s' % pkg for pkg in pkgs]
  424. else:
  425. raise MesonException('Gir export packages must be str or list')
  426. deps = kwargs.pop('dependencies', [])
  427. if not isinstance(deps, list):
  428. deps = [deps]
  429. deps = (girtarget.get_all_link_deps() + girtarget.get_external_deps() +
  430. deps)
  431. # Need to recursively add deps on GirTarget sources from our
  432. # dependencies and also find the include directories needed for the
  433. # typelib generation custom target below.
  434. typelib_includes = []
  435. for dep in deps:
  436. if hasattr(dep, 'held_object'):
  437. dep = dep.held_object
  438. # Add a dependency on each GirTarget listed in dependencies and add
  439. # the directory where it will be generated to the typelib includes
  440. if isinstance(dep, InternalDependency):
  441. for source in dep.sources:
  442. if hasattr(source, 'held_object'):
  443. source = source.held_object
  444. if isinstance(source, GirTarget) and source not in depends:
  445. depends.append(source)
  446. subdir = os.path.join(state.environment.get_build_dir(),
  447. source.get_subdir())
  448. if subdir not in typelib_includes:
  449. typelib_includes.append(subdir)
  450. # Do the same, but for dependencies of dependencies. These are
  451. # stored in the list of generated sources for each link dep (from
  452. # girtarget.get_all_link_deps() above).
  453. # FIXME: Store this in the original form from declare_dependency()
  454. # so it can be used here directly.
  455. elif isinstance(dep, build.SharedLibrary):
  456. for source in dep.generated:
  457. if isinstance(source, GirTarget):
  458. subdir = os.path.join(state.environment.get_build_dir(),
  459. source.get_subdir())
  460. if subdir not in typelib_includes:
  461. typelib_includes.append(subdir)
  462. elif isinstance(dep, PkgConfigDependency):
  463. girdir = dep.get_pkgconfig_variable("girdir")
  464. if girdir and girdir not in typelib_includes:
  465. typelib_includes.append(girdir)
  466. # ldflags will be misinterpreted by gir scanner (showing
  467. # spurious dependencies) but building GStreamer fails if they
  468. # are not used here.
  469. dep_cflags, ldflags, gi_includes = self._get_dependencies_flags(deps, state, depends,
  470. use_gir_args=True)
  471. cflags += list(dep_cflags)
  472. scan_command += ['--cflags-begin']
  473. scan_command += cflags
  474. scan_command += ['--cflags-end']
  475. # need to put our output directory first as we need to use the
  476. # generated libraries instead of any possibly installed system/prefix
  477. # ones.
  478. if isinstance(girtarget, build.SharedLibrary):
  479. scan_command += ["-L@PRIVATE_OUTDIR_ABS_%s@" % girtarget.get_id()]
  480. scan_command += list(ldflags)
  481. for i in gi_includes:
  482. scan_command += ['--add-include-path=%s' % i]
  483. inc_dirs = kwargs.pop('include_directories', [])
  484. if not isinstance(inc_dirs, list):
  485. inc_dirs = [inc_dirs]
  486. for incd in inc_dirs:
  487. if not isinstance(incd.held_object, (str, build.IncludeDirs)):
  488. raise MesonException(
  489. 'Gir include dirs should be include_directories().')
  490. scan_command += get_include_args(inc_dirs)
  491. scan_command += get_include_args(gir_inc_dirs + inc_dirs, prefix='--add-include-path=')
  492. if isinstance(girtarget, build.Executable):
  493. scan_command += ['--program', girtarget]
  494. elif isinstance(girtarget, build.SharedLibrary):
  495. libname = girtarget.get_basename()
  496. # Needed for the following binutils bug:
  497. # https://github.com/mesonbuild/meson/issues/1911
  498. # However, g-ir-scanner does not understand -Wl,-rpath
  499. # so we need to use -L instead
  500. for d in state.backend.determine_rpath_dirs(girtarget):
  501. d = os.path.join(state.environment.get_build_dir(), d)
  502. scan_command.append('-L' + d)
  503. scan_command += ['--library', libname]
  504. scankwargs = {'output': girfile,
  505. 'input': libsources,
  506. 'command': scan_command,
  507. 'depends': depends}
  508. if kwargs.get('install'):
  509. scankwargs['install'] = kwargs['install']
  510. scankwargs['install_dir'] = kwargs.get('install_dir_gir',
  511. os.path.join(state.environment.get_datadir(), 'gir-1.0'))
  512. scan_target = GirTarget(girfile, state.subdir, scankwargs)
  513. typelib_output = '%s-%s.typelib' % (ns, nsversion)
  514. typelib_cmd = [gicompiler, scan_target, '--output', '@OUTPUT@']
  515. typelib_cmd += get_include_args(gir_inc_dirs, prefix='--includedir=')
  516. for incdir in typelib_includes:
  517. typelib_cmd += ["--includedir=" + incdir]
  518. typelib_kwargs = {
  519. 'output': typelib_output,
  520. 'command': typelib_cmd,
  521. }
  522. if kwargs.get('install'):
  523. typelib_kwargs['install'] = kwargs['install']
  524. typelib_kwargs['install_dir'] = kwargs.get('install_dir_typelib',
  525. os.path.join(state.environment.get_libdir(), 'girepository-1.0'))
  526. typelib_target = TypelibTarget(typelib_output, state.subdir, typelib_kwargs)
  527. rv = [scan_target, typelib_target]
  528. return ModuleReturnValue(rv, rv)
  529. def compile_schemas(self, state, args, kwargs):
  530. if args:
  531. raise MesonException('Compile_schemas does not take positional arguments.')
  532. srcdir = os.path.join(state.build_to_src, state.subdir)
  533. outdir = state.subdir
  534. cmd = [find_program('glib-compile-schemas', 'gsettings-compile')]
  535. cmd += ['--targetdir', outdir, srcdir]
  536. kwargs['command'] = cmd
  537. kwargs['input'] = []
  538. kwargs['output'] = 'gschemas.compiled'
  539. if state.subdir == '':
  540. targetname = 'gsettings-compile'
  541. else:
  542. targetname = 'gsettings-compile-' + state.subdir
  543. target_g = build.CustomTarget(targetname, state.subdir, kwargs)
  544. return ModuleReturnValue(target_g, [target_g])
  545. def yelp(self, state, args, kwargs):
  546. if len(args) < 1:
  547. raise MesonException('Yelp requires a project id')
  548. project_id = args[0]
  549. sources = mesonlib.stringlistify(kwargs.pop('sources', []))
  550. if not sources:
  551. if len(args) > 1:
  552. sources = mesonlib.stringlistify(args[1:])
  553. if not sources:
  554. raise MesonException('Yelp requires a list of sources')
  555. source_str = '@@'.join(sources)
  556. langs = mesonlib.stringlistify(kwargs.pop('languages', []))
  557. media = mesonlib.stringlistify(kwargs.pop('media', []))
  558. symlinks = kwargs.pop('symlink_media', True)
  559. if not isinstance(symlinks, bool):
  560. raise MesonException('symlink_media must be a boolean')
  561. if kwargs:
  562. raise MesonException('Unknown arguments passed: {}'.format(', '.join(kwargs.keys())))
  563. script = [sys.executable, state.environment.get_build_command()]
  564. args = ['--internal',
  565. 'yelphelper',
  566. 'install',
  567. '--subdir=' + state.subdir,
  568. '--id=' + project_id,
  569. '--installdir=' + os.path.join(state.environment.get_datadir(), 'help'),
  570. '--sources=' + source_str]
  571. if symlinks:
  572. args.append('--symlinks=true')
  573. if media:
  574. args.append('--media=' + '@@'.join(media))
  575. if langs:
  576. args.append('--langs=' + '@@'.join(langs))
  577. inscript = build.RunScript(script, args)
  578. potargs = [state.environment.get_build_command(), '--internal', 'yelphelper', 'pot',
  579. '--subdir=' + state.subdir,
  580. '--id=' + project_id,
  581. '--sources=' + source_str]
  582. pottarget = build.RunTarget('help-' + project_id + '-pot', sys.executable,
  583. potargs, [], state.subdir)
  584. poargs = [state.environment.get_build_command(), '--internal', 'yelphelper', 'update-po',
  585. '--subdir=' + state.subdir,
  586. '--id=' + project_id,
  587. '--sources=' + source_str,
  588. '--langs=' + '@@'.join(langs)]
  589. potarget = build.RunTarget('help-' + project_id + '-update-po', sys.executable,
  590. poargs, [], state.subdir)
  591. rv = [inscript, pottarget, potarget]
  592. return ModuleReturnValue(None, rv)
  593. def gtkdoc(self, state, args, kwargs):
  594. if len(args) != 1:
  595. raise MesonException('Gtkdoc must have one positional argument.')
  596. modulename = args[0]
  597. if not isinstance(modulename, str):
  598. raise MesonException('Gtkdoc arg must be string.')
  599. if 'src_dir' not in kwargs:
  600. raise MesonException('Keyword argument src_dir missing.')
  601. main_file = kwargs.get('main_sgml', '')
  602. if not isinstance(main_file, str):
  603. raise MesonException('Main sgml keyword argument must be a string.')
  604. main_xml = kwargs.get('main_xml', '')
  605. if not isinstance(main_xml, str):
  606. raise MesonException('Main xml keyword argument must be a string.')
  607. if main_xml != '':
  608. if main_file != '':
  609. raise MesonException('You can only specify main_xml or main_sgml, not both.')
  610. main_file = main_xml
  611. targetname = modulename + '-doc'
  612. command = [sys.executable, state.environment.get_build_command()]
  613. namespace = kwargs.get('namespace', '')
  614. mode = kwargs.get('mode', 'auto')
  615. VALID_MODES = ('xml', 'sgml', 'none', 'auto')
  616. if mode not in VALID_MODES:
  617. raise MesonException('gtkdoc: Mode {} is not a valid mode: {}'.format(mode, VALID_MODES))
  618. src_dirs = kwargs['src_dir']
  619. if not isinstance(src_dirs, list):
  620. src_dirs = [src_dirs]
  621. header_dirs = []
  622. for src_dir in src_dirs:
  623. if hasattr(src_dir, 'held_object'):
  624. src_dir = src_dir.held_object
  625. if not isinstance(src_dir, build.IncludeDirs):
  626. raise MesonException('Invalid keyword argument for src_dir.')
  627. for inc_dir in src_dir.get_incdirs():
  628. header_dirs.append(os.path.join(state.environment.get_source_dir(),
  629. src_dir.get_curdir(), inc_dir))
  630. else:
  631. header_dirs.append(src_dir)
  632. args = ['--internal', 'gtkdoc',
  633. '--sourcedir=' + state.environment.get_source_dir(),
  634. '--builddir=' + state.environment.get_build_dir(),
  635. '--subdir=' + state.subdir,
  636. '--headerdirs=' + '@@'.join(header_dirs),
  637. '--mainfile=' + main_file,
  638. '--modulename=' + modulename,
  639. '--mode=' + mode]
  640. if namespace:
  641. args.append('--namespace=' + namespace)
  642. args += self._unpack_args('--htmlargs=', 'html_args', kwargs)
  643. args += self._unpack_args('--scanargs=', 'scan_args', kwargs)
  644. args += self._unpack_args('--scanobjsargs=', 'scanobjs_args', kwargs)
  645. args += self._unpack_args('--gobjects-types-file=', 'gobject_typesfile', kwargs, state)
  646. args += self._unpack_args('--fixxrefargs=', 'fixxref_args', kwargs)
  647. args += self._unpack_args('--mkdbargs=', 'mkdb_args', kwargs)
  648. args += self._unpack_args('--html-assets=', 'html_assets', kwargs, state)
  649. args += self._unpack_args('--content-files=', 'content_files', kwargs, state)
  650. args += self._unpack_args('--expand-content-files=', 'expand_content_files', kwargs, state)
  651. args += self._unpack_args('--ignore-headers=', 'ignore_headers', kwargs)
  652. args += self._unpack_args('--installdir=', 'install_dir', kwargs, state)
  653. args += self._get_build_args(kwargs, state)
  654. res = [build.RunTarget(targetname, command[0], command[1:] + args, [], state.subdir)]
  655. if kwargs.get('install', True):
  656. res.append(build.RunScript(command, args))
  657. return ModuleReturnValue(None, res)
  658. def _get_build_args(self, kwargs, state):
  659. args = []
  660. cflags, ldflags, gi_includes = self._get_dependencies_flags(kwargs.get('dependencies', []), state, include_rpath=True)
  661. inc_dirs = kwargs.get('include_directories', [])
  662. if not isinstance(inc_dirs, list):
  663. inc_dirs = [inc_dirs]
  664. for incd in inc_dirs:
  665. if not isinstance(incd.held_object, (str, build.IncludeDirs)):
  666. raise MesonException(
  667. 'Gir include dirs should be include_directories().')
  668. cflags.update(get_include_args(inc_dirs))
  669. if cflags:
  670. args += ['--cflags=%s' % ' '.join(cflags)]
  671. if ldflags:
  672. args += ['--ldflags=%s' % ' '.join(ldflags)]
  673. compiler = state.environment.coredata.compilers.get('c')
  674. if compiler:
  675. args += ['--cc=%s' % ' '.join(compiler.get_exelist())]
  676. args += ['--ld=%s' % ' '.join(compiler.get_linker_exelist())]
  677. return args
  678. def gtkdoc_html_dir(self, state, args, kwargs):
  679. if len(args) != 1:
  680. raise MesonException('Must have exactly one argument.')
  681. modulename = args[0]
  682. if not isinstance(modulename, str):
  683. raise MesonException('Argument must be a string')
  684. return ModuleReturnValue(os.path.join('share/gtkdoc/html', modulename), [])
  685. @staticmethod
  686. def _unpack_args(arg, kwarg_name, kwargs, expend_file_state=None):
  687. if kwarg_name not in kwargs:
  688. return []
  689. new_args = kwargs[kwarg_name]
  690. if not isinstance(new_args, list):
  691. new_args = [new_args]
  692. args = []
  693. for i in new_args:
  694. if expend_file_state and isinstance(i, mesonlib.File):
  695. i = os.path.join(expend_file_state.environment.get_build_dir(), i.subdir, i.fname)
  696. elif not isinstance(i, str):
  697. raise MesonException(kwarg_name + ' values must be strings.')
  698. args.append(i)
  699. if args:
  700. return [arg + '@@'.join(args)]
  701. return []
  702. def gdbus_codegen(self, state, args, kwargs):
  703. if len(args) != 2:
  704. raise MesonException('Gdbus_codegen takes two arguments, name and xml file.')
  705. namebase = args[0]
  706. xml_file = args[1]
  707. target_name = namebase + '-gdbus'
  708. cmd = [find_program('gdbus-codegen', target_name)]
  709. if 'interface_prefix' in kwargs:
  710. cmd += ['--interface-prefix', kwargs.pop('interface_prefix')]
  711. if 'namespace' in kwargs:
  712. cmd += ['--c-namespace', kwargs.pop('namespace')]
  713. if kwargs.get('object_manager', False):
  714. cmd += ['--c-generate-object-manager']
  715. # https://git.gnome.org/browse/glib/commit/?id=ee09bb704fe9ccb24d92dd86696a0e6bb8f0dc1a
  716. if mesonlib.version_compare(self._get_native_glib_version(state), '>= 2.51.3'):
  717. cmd += ['--output-directory', '@OUTDIR@', '--generate-c-code', namebase, '@INPUT@']
  718. else:
  719. self._print_gdbus_warning()
  720. cmd += ['--generate-c-code', '@OUTDIR@/' + namebase, '@INPUT@']
  721. outputs = [namebase + '.c', namebase + '.h']
  722. custom_kwargs = {'input': xml_file,
  723. 'output': outputs,
  724. 'command': cmd
  725. }
  726. ct = build.CustomTarget(target_name, state.subdir, custom_kwargs)
  727. return ModuleReturnValue(ct, [ct])
  728. def mkenums(self, state, args, kwargs):
  729. if len(args) != 1:
  730. raise MesonException('Mkenums requires one positional argument.')
  731. basename = args[0]
  732. if 'sources' not in kwargs:
  733. raise MesonException('Missing keyword argument "sources".')
  734. sources = kwargs.pop('sources')
  735. if isinstance(sources, str):
  736. sources = [sources]
  737. elif not isinstance(sources, list):
  738. raise MesonException(
  739. 'Sources keyword argument must be a string or array.')
  740. cmd = []
  741. known_kwargs = ['comments', 'eprod', 'fhead', 'fprod', 'ftail',
  742. 'identifier_prefix', 'symbol_prefix', 'template',
  743. 'vhead', 'vprod', 'vtail']
  744. known_custom_target_kwargs = ['install_dir', 'build_always',
  745. 'depends', 'depend_files']
  746. c_template = h_template = None
  747. install_header = False
  748. for arg, value in kwargs.items():
  749. if arg == 'sources':
  750. raise AssertionError("sources should've already been handled")
  751. elif arg == 'c_template':
  752. c_template = value
  753. if 'template' in kwargs:
  754. raise MesonException('Mkenums does not accept both '
  755. 'c_template and template keyword '
  756. 'arguments at the same time.')
  757. elif arg == 'h_template':
  758. h_template = value
  759. if 'template' in kwargs:
  760. raise MesonException('Mkenums does not accept both '
  761. 'h_template and template keyword '
  762. 'arguments at the same time.')
  763. elif arg == 'install_header':
  764. install_header = value
  765. elif arg in known_kwargs:
  766. cmd += ['--' + arg.replace('_', '-'), value]
  767. elif arg not in known_custom_target_kwargs:
  768. raise MesonException(
  769. 'Mkenums does not take a %s keyword argument.' % (arg, ))
  770. cmd = [find_program('glib-mkenums', 'mkenums')] + cmd
  771. custom_kwargs = {}
  772. for arg in known_custom_target_kwargs:
  773. if arg in kwargs:
  774. custom_kwargs[arg] = kwargs[arg]
  775. targets = []
  776. if h_template is not None:
  777. h_output = os.path.splitext(h_template)[0]
  778. # We always set template as the first element in the source array
  779. # so --template consumes it.
  780. h_cmd = cmd + ['--template', '@INPUT@']
  781. h_sources = [h_template] + sources
  782. custom_kwargs['install'] = install_header
  783. if 'install_dir' not in custom_kwargs:
  784. custom_kwargs['install_dir'] = \
  785. state.environment.coredata.get_builtin_option('includedir')
  786. h_target = self._make_mkenum_custom_target(state, h_sources,
  787. h_output, h_cmd,
  788. custom_kwargs)
  789. targets.append(h_target)
  790. if c_template is not None:
  791. c_output = os.path.splitext(c_template)[0]
  792. # We always set template as the first element in the source array
  793. # so --template consumes it.
  794. c_cmd = cmd + ['--template', '@INPUT@']
  795. c_sources = [c_template] + sources
  796. # Never install the C file. Complain on bug tracker if you need it.
  797. custom_kwargs['install'] = False
  798. if h_template is not None:
  799. if 'depends' in custom_kwargs:
  800. custom_kwargs['depends'] += [h_target]
  801. else:
  802. custom_kwargs['depends'] = h_target
  803. c_target = self._make_mkenum_custom_target(state, c_sources,
  804. c_output, c_cmd,
  805. custom_kwargs)
  806. targets.insert(0, c_target)
  807. if c_template is None and h_template is None:
  808. generic_cmd = cmd + ['@INPUT@']
  809. custom_kwargs['install'] = install_header
  810. if 'install_dir' not in custom_kwargs:
  811. custom_kwargs['install_dir'] = \
  812. state.environment.coredata.get_builtin_option('includedir')
  813. target = self._make_mkenum_custom_target(state, sources, basename,
  814. generic_cmd, custom_kwargs)
  815. return ModuleReturnValue(target, [target])
  816. elif len(targets) == 1:
  817. return ModuleReturnValue(targets[0], [targets[0]])
  818. else:
  819. return ModuleReturnValue(targets, targets)
  820. @staticmethod
  821. def _make_mkenum_custom_target(state, sources, output, cmd, kwargs):
  822. custom_kwargs = {
  823. 'input': sources,
  824. 'output': output,
  825. 'capture': True,
  826. 'command': cmd
  827. }
  828. custom_kwargs.update(kwargs)
  829. return build.CustomTarget(output, state.subdir, custom_kwargs,
  830. # https://github.com/mesonbuild/meson/issues/973
  831. absolute_paths=True)
  832. def genmarshal(self, state, args, kwargs):
  833. if len(args) != 1:
  834. raise MesonException(
  835. 'Genmarshal requires one positional argument.')
  836. output = args[0]
  837. if 'sources' not in kwargs:
  838. raise MesonException('Missing keyword argument "sources".')
  839. sources = kwargs.pop('sources')
  840. if isinstance(sources, str):
  841. sources = [sources]
  842. elif not isinstance(sources, list):
  843. raise MesonException(
  844. 'Sources keyword argument must be a string or array.')
  845. cmd = [find_program('glib-genmarshal', output + '_genmarshal')]
  846. known_kwargs = ['internal', 'nostdinc', 'skip_source', 'stdinc',
  847. 'valist_marshallers']
  848. known_custom_target_kwargs = ['build_always', 'depends',
  849. 'depend_files', 'install_dir',
  850. 'install_header']
  851. for arg, value in kwargs.items():
  852. if arg == 'prefix':
  853. cmd += ['--prefix', value]
  854. elif arg in known_kwargs and value:
  855. cmd += ['--' + arg.replace('_', '-')]
  856. elif arg not in known_custom_target_kwargs:
  857. raise MesonException(
  858. 'Genmarshal does not take a %s keyword argument.' % (
  859. arg, ))
  860. install_header = kwargs.pop('install_header', False)
  861. install_dir = kwargs.pop('install_dir', None)
  862. custom_kwargs = {
  863. 'input': sources,
  864. }
  865. # https://github.com/GNOME/glib/commit/0fbc98097fac4d3e647684f344e508abae109fdf
  866. if mesonlib.version_compare(self._get_native_glib_version(state), '>= 2.51.0'):
  867. cmd += ['--output', '@OUTPUT@']
  868. else:
  869. custom_kwargs['capture'] = True
  870. for arg in known_custom_target_kwargs:
  871. if arg in kwargs:
  872. custom_kwargs[arg] = kwargs[arg]
  873. custom_kwargs['command'] = cmd + ['--body', '@INPUT@']
  874. custom_kwargs['output'] = output + '.c'
  875. body = build.CustomTarget(output + '_c', state.subdir, custom_kwargs)
  876. custom_kwargs['install'] = install_header
  877. if install_dir is not None:
  878. custom_kwargs['install_dir'] = install_dir
  879. custom_kwargs['command'] = cmd + ['--header', '@INPUT@']
  880. custom_kwargs['output'] = output + '.h'
  881. header = build.CustomTarget(output + '_h', state.subdir, custom_kwargs)
  882. rv = [body, header]
  883. return ModuleReturnValue(rv, rv)
  884. @staticmethod
  885. def _vapi_args_to_command(prefix, variable, kwargs, accept_vapi=False):
  886. arg_list = kwargs.get(variable)
  887. if not arg_list:
  888. return []
  889. ret = []
  890. if not isinstance(arg_list, list):
  891. arg_list = [arg_list]
  892. for arg in arg_list:
  893. if not isinstance(arg, str):
  894. types = 'strings' + ' or InternalDependencys' if accept_vapi else ''
  895. raise MesonException('All {} must be {}'.format(variable, types))
  896. ret.append(prefix + arg)
  897. return ret
  898. def _extract_vapi_packages(self, state, kwargs):
  899. '''
  900. Packages are special because we need to:
  901. - Get a list of packages for the .deps file
  902. - Get a list of depends for any VapiTargets
  903. - Get package name from VapiTargets
  904. - Add include dirs for any VapiTargets
  905. '''
  906. arg_list = kwargs.get('packages')
  907. if not arg_list:
  908. return [], [], [], []
  909. if not isinstance(arg_list, list):
  910. arg_list = [arg_list]
  911. vapi_depends = []
  912. vapi_packages = []
  913. vapi_includes = []
  914. ret = []
  915. remaining_args = []
  916. for arg in arg_list:
  917. if hasattr(arg, 'held_object'):
  918. arg = arg.held_object
  919. if isinstance(arg, InternalDependency):
  920. targets = [t for t in arg.sources if isinstance(t, VapiTarget)]
  921. for target in targets:
  922. srcdir = os.path.join(state.environment.get_source_dir(),
  923. target.get_subdir())
  924. outdir = os.path.join(state.environment.get_build_dir(),
  925. target.get_subdir())
  926. outfile = target.get_outputs()[0][:-5] # Strip .vapi
  927. ret.append('--vapidir=' + outdir)
  928. ret.append('--girdir=' + outdir)
  929. ret.append('--pkg=' + outfile)
  930. vapi_depends.append(target)
  931. vapi_packages.append(outfile)
  932. vapi_includes.append(srcdir)
  933. else:
  934. vapi_packages.append(arg)
  935. remaining_args.append(arg)
  936. kwargs['packages'] = remaining_args
  937. vapi_args = ret + self._vapi_args_to_command('--pkg=', 'packages', kwargs, accept_vapi=True)
  938. return vapi_args, vapi_depends, vapi_packages, vapi_includes
  939. def _generate_deps(self, state, library, packages, install_dir):
  940. outdir = state.environment.scratch_dir
  941. fname = os.path.join(outdir, library + '.deps')
  942. with open(fname, 'w') as ofile:
  943. for package in packages:
  944. ofile.write(package + '\n')
  945. return build.Data(mesonlib.File(True, outdir, fname), install_dir)
  946. def _get_vapi_link_with(self, target):
  947. link_with = []
  948. for dep in target.get_target_dependencies():
  949. if isinstance(dep, build.SharedLibrary):
  950. link_with.append(dep)
  951. elif isinstance(dep, GirTarget):
  952. link_with += self._get_vapi_link_with(dep)
  953. return link_with
  954. def generate_vapi(self, state, args, kwargs):
  955. if len(args) != 1:
  956. raise MesonException('The library name is required')
  957. if not isinstance(args[0], str):
  958. raise MesonException('The first argument must be the name of the library')
  959. created_values = []
  960. library = args[0]
  961. build_dir = os.path.join(state.environment.get_build_dir(), state.subdir)
  962. source_dir = os.path.join(state.environment.get_source_dir(), state.subdir)
  963. pkg_cmd, vapi_depends, vapi_packages, vapi_includes = self._extract_vapi_packages(state, kwargs)
  964. cmd = [find_program('vapigen', 'Vaapi')]
  965. cmd += ['--quiet', '--library=' + library, '--directory=' + build_dir]
  966. cmd += self._vapi_args_to_command('--vapidir=', 'vapi_dirs', kwargs)
  967. cmd += self._vapi_args_to_command('--metadatadir=', 'metadata_dirs', kwargs)
  968. cmd += self._vapi_args_to_command('--girdir=', 'gir_dirs', kwargs)
  969. cmd += pkg_cmd
  970. cmd += ['--metadatadir=' + source_dir]
  971. inputs = kwargs.get('sources')
  972. if not inputs:
  973. raise MesonException('sources are required to generate the vapi file')
  974. if not isinstance(inputs, list):
  975. inputs = [inputs]
  976. link_with = []
  977. for i in inputs:
  978. if isinstance(i, str):
  979. cmd.append(os.path.join(source_dir, i))
  980. elif hasattr(i, 'held_object') and isinstance(i.held_object, GirTarget):
  981. link_with += self._get_vapi_link_with(i.held_object)
  982. subdir = os.path.join(state.environment.get_build_dir(),
  983. i.held_object.get_subdir())
  984. gir_file = os.path.join(subdir, i.held_object.get_outputs()[0])
  985. cmd.append(gir_file)
  986. else:
  987. raise MesonException('Input must be a str or GirTarget')
  988. vapi_output = library + '.vapi'
  989. custom_kwargs = {
  990. 'command': cmd,
  991. 'input': inputs,
  992. 'output': vapi_output,
  993. 'depends': vapi_depends,
  994. }
  995. install_dir = kwargs.get('install_dir',
  996. os.path.join(state.environment.coredata.get_builtin_option('datadir'),
  997. 'vala', 'vapi'))
  998. if kwargs.get('install'):
  999. custom_kwargs['install'] = kwargs['install']
  1000. custom_kwargs['install_dir'] = install_dir
  1001. # We shouldn't need this locally but we install it
  1002. deps_target = self._generate_deps(state, library, vapi_packages, install_dir)
  1003. created_values.append(deps_target)
  1004. vapi_target = VapiTarget(vapi_output, state.subdir, custom_kwargs)
  1005. # So to try our best to get this to just work we need:
  1006. # - link with with the correct library
  1007. # - include the vapi and dependent vapi files in sources
  1008. # - add relevant directories to include dirs
  1009. incs = [build.IncludeDirs(state.subdir, ['.'] + vapi_includes, False)]
  1010. sources = [vapi_target] + vapi_depends
  1011. rv = InternalDependency(None, incs, [], [], link_with, sources, [])
  1012. created_values.append(rv)
  1013. return ModuleReturnValue(rv, created_values)
  1014. def initialize():
  1015. return GnomeModule()