mesonintrospect.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env python3
  2. # Copyright 2014-2015 The Meson development team
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. """This is a helper script for IDE developers. It allows you to
  13. extract information such as list of targets, files, compiler flags,
  14. tests and so on. All output is in JSON for simple parsing.
  15. Currently only works for the Ninja backend. Others use generated
  16. project files and don't need this info."""
  17. import json, pickle
  18. import coredata, build, optinterpreter
  19. import argparse
  20. import sys, os
  21. parser = argparse.ArgumentParser()
  22. parser.add_argument('--targets', action='store_true', dest='list_targets', default=False,
  23. help='List top level targets.')
  24. parser.add_argument('--target-files', action='store', dest='target_files', default=None,
  25. help='List source files for a given target.')
  26. parser.add_argument('--buildsystem-files', action='store_true', dest='buildsystem_files', default=False,
  27. help='List files that make up the build system.')
  28. parser.add_argument('--buildoptions', action='store_true', dest='buildoptions', default=False,
  29. help='List all build options.')
  30. parser.add_argument('--tests', action='store_true', dest='tests', default=False,
  31. help='List all unit tests.')
  32. parser.add_argument('--dependencies', action='store_true', dest='dependencies', default=False,
  33. help='list external dependencies.')
  34. parser.add_argument('args', nargs='+')
  35. def list_targets(coredata, builddata):
  36. tlist = []
  37. for (idname, target) in builddata.get_targets().items():
  38. t = {}
  39. t['name'] = target.get_basename()
  40. t['id'] = idname
  41. fname = target.get_filename()
  42. if isinstance(fname, list):
  43. fname = [os.path.join(target.subdir, x) for x in fname]
  44. else:
  45. fname = os.path.join(target.subdir, fname)
  46. t['filename'] = fname
  47. if isinstance(target, build.Executable):
  48. typename = 'executable'
  49. elif isinstance(target, build.SharedLibrary):
  50. typename = 'shared library'
  51. elif isinstance(target, build.StaticLibrary):
  52. typename = 'static library'
  53. elif isinstance(target, build.CustomTarget):
  54. typename = 'custom'
  55. elif isinstance(target, build.RunTarget):
  56. typename = 'run'
  57. else:
  58. typename = 'unknown'
  59. t['type'] = typename
  60. if target.should_install():
  61. t['installed'] = True
  62. else:
  63. t['installed'] = False
  64. tlist.append(t)
  65. print(json.dumps(tlist))
  66. def list_target_files(target_name, coredata, builddata):
  67. try:
  68. t = builddata.targets[target_name]
  69. sources = t.sources + t.extra_files
  70. subdir = t.subdir
  71. except KeyError:
  72. print("Unknown target %s." % target_name)
  73. sys.exit(1)
  74. sources = [os.path.join(i.subdir, i.fname) for i in sources]
  75. print(json.dumps(sources))
  76. def list_buildoptions(coredata, builddata):
  77. buildtype= {'choices': ['plain', 'debug', 'debugoptimized', 'release'],
  78. 'type' : 'combo',
  79. 'value' : coredata.buildtype,
  80. 'description' : 'Build type',
  81. 'name' : 'type'}
  82. strip = {'value' : coredata.strip,
  83. 'type' : 'boolean',
  84. 'description' : 'Strip on install',
  85. 'name' : 'strip'}
  86. coverage = {'value': coredata.coverage,
  87. 'type' : 'boolean',
  88. 'description' : 'Enable coverage',
  89. 'name' : 'coverage'}
  90. pch = {'value' : coredata.use_pch,
  91. 'type' : 'boolean',
  92. 'description' : 'Use precompiled headers',
  93. 'name' : 'pch'}
  94. unity = {'value' : coredata.unity,
  95. 'type' : 'boolean',
  96. 'description' : 'Unity build',
  97. 'name' : 'unity'}
  98. optlist = [buildtype, strip, coverage, pch, unity]
  99. options = coredata.user_options
  100. keys = list(options.keys())
  101. keys.sort()
  102. for key in keys:
  103. opt = options[key]
  104. optdict = {}
  105. optdict['name'] = key
  106. optdict['value'] = opt.value
  107. if isinstance(opt, optinterpreter.UserStringOption):
  108. typestr = 'string'
  109. elif isinstance(opt, optinterpreter.UserBooleanOption):
  110. typestr = 'boolean'
  111. elif isinstance(opt, optinterpreter.UserComboOption):
  112. optdict['choices'] = opt.choices
  113. typestr = 'combo'
  114. else:
  115. raise RuntimeError("Unknown option type")
  116. optdict['type'] = typestr
  117. optdict['description'] = opt.description
  118. optlist.append(optdict)
  119. print(json.dumps(optlist))
  120. def list_buildsystem_files(coredata, builddata):
  121. src_dir = builddata.environment.get_source_dir()
  122. # I feel dirty about this. But only slightly.
  123. filelist = []
  124. for root, _, files in os.walk(src_dir):
  125. for f in files:
  126. if f == 'meson.build' or f == 'meson_options.txt':
  127. filelist.append(os.path.relpath(os.path.join(root, f), src_dir))
  128. print(json.dumps(filelist))
  129. def list_deps(coredata):
  130. result = {}
  131. for d in coredata.deps.values():
  132. if d.found():
  133. args = {'compile_args': d.get_compile_args(),
  134. 'link_args': d.get_link_args()}
  135. result[d.name] = args
  136. print(json.dumps(result))
  137. def list_tests(testdata):
  138. result = []
  139. for t in testdata:
  140. to = {}
  141. to['cmd'] = [t.fname] + t.cmd_args
  142. to['env'] = t.env
  143. to['name'] = t.name
  144. result.append(to)
  145. print(json.dumps(result))
  146. if __name__ == '__main__':
  147. options = parser.parse_args()
  148. if len(options.args) > 1:
  149. print('Too many arguments')
  150. sys.exit(1)
  151. elif len(options.args) == 1:
  152. bdir = options.args[0]
  153. else:
  154. bdir = ''
  155. corefile = os.path.join(bdir, 'meson-private/coredata.dat')
  156. buildfile = os.path.join(bdir, 'meson-private/build.dat')
  157. testfile = os.path.join(bdir, 'meson-private/meson_test_setup.dat')
  158. coredata = pickle.load(open(corefile, 'rb'))
  159. builddata = pickle.load(open(buildfile, 'rb'))
  160. testdata = pickle.load(open(testfile, 'rb'))
  161. if options.list_targets:
  162. list_targets(coredata, builddata)
  163. elif options.target_files is not None:
  164. list_target_files(options.target_files, coredata, builddata)
  165. elif options.buildsystem_files:
  166. list_buildsystem_files(coredata, builddata)
  167. elif options.buildoptions:
  168. list_buildoptions(coredata, builddata)
  169. elif options.tests:
  170. list_tests(testdata)
  171. elif options.dependencies:
  172. list_deps(coredata)
  173. else:
  174. print('No command specified')
  175. sys.exit(1)