mintro.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # Copyright 2014-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 is a helper script for IDE developers. It allows you to
  12. extract information such as list of targets, files, compiler flags,
  13. tests and so on. All output is in JSON for simple parsing.
  14. Currently only works for the Ninja backend. Others use generated
  15. project files and don't need this info."""
  16. import json, pickle
  17. from . import coredata, build, mesonlib
  18. import argparse
  19. import sys, os
  20. parser = argparse.ArgumentParser()
  21. parser.add_argument('--targets', action='store_true', dest='list_targets', default=False,
  22. help='List top level targets.')
  23. parser.add_argument('--target-files', action='store', dest='target_files', default=None,
  24. help='List source files for a given target.')
  25. parser.add_argument('--buildsystem-files', action='store_true', dest='buildsystem_files', default=False,
  26. help='List files that make up the build system.')
  27. parser.add_argument('--buildoptions', action='store_true', dest='buildoptions', default=False,
  28. help='List all build options.')
  29. parser.add_argument('--tests', action='store_true', dest='tests', default=False,
  30. help='List all unit tests.')
  31. parser.add_argument('--benchmarks', action='store_true', dest='benchmarks', default=False,
  32. help='List all benchmarks.')
  33. parser.add_argument('--dependencies', action='store_true', dest='dependencies', default=False,
  34. help='list external dependencies.')
  35. parser.add_argument('args', nargs='+')
  36. def list_targets(coredata, builddata):
  37. tlist = []
  38. for (idname, target) in builddata.get_targets().items():
  39. t = {}
  40. t['name'] = target.get_basename()
  41. t['id'] = idname
  42. fname = target.get_filename()
  43. if isinstance(fname, list):
  44. fname = [os.path.join(target.subdir, x) for x in fname]
  45. else:
  46. fname = os.path.join(target.subdir, fname)
  47. t['filename'] = fname
  48. if isinstance(target, build.Executable):
  49. typename = 'executable'
  50. elif isinstance(target, build.SharedLibrary):
  51. typename = 'shared library'
  52. elif isinstance(target, build.StaticLibrary):
  53. typename = 'static library'
  54. elif isinstance(target, build.CustomTarget):
  55. typename = 'custom'
  56. elif isinstance(target, build.RunTarget):
  57. typename = 'run'
  58. else:
  59. typename = 'unknown'
  60. t['type'] = typename
  61. if target.should_install():
  62. t['installed'] = True
  63. else:
  64. t['installed'] = False
  65. tlist.append(t)
  66. print(json.dumps(tlist))
  67. def list_target_files(target_name, coredata, builddata):
  68. try:
  69. t = builddata.targets[target_name]
  70. sources = t.sources + t.extra_files
  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', 'minsize'],
  78. 'type' : 'combo',
  79. 'value' : coredata.get_builtin_option('buildtype'),
  80. 'description' : 'Build type',
  81. 'name' : 'type'}
  82. strip = {'value' : coredata.get_builtin_option('strip'),
  83. 'type' : 'boolean',
  84. 'description' : 'Strip on install',
  85. 'name' : 'strip'}
  86. unity = {'value' : coredata.get_builtin_option('unity'),
  87. 'type' : 'boolean',
  88. 'description' : 'Unity build',
  89. 'name' : 'unity'}
  90. optlist = [buildtype, strip, unity]
  91. add_keys(optlist, coredata.user_options)
  92. add_keys(optlist, coredata.compiler_options)
  93. add_keys(optlist, coredata.base_options)
  94. print(json.dumps(optlist))
  95. def add_keys(optlist, options):
  96. keys = list(options.keys())
  97. keys.sort()
  98. for key in keys:
  99. opt = options[key]
  100. optdict = {}
  101. optdict['name'] = key
  102. optdict['value'] = opt.value
  103. if isinstance(opt, coredata.UserStringOption):
  104. typestr = 'string'
  105. elif isinstance(opt, coredata.UserBooleanOption):
  106. typestr = 'boolean'
  107. elif isinstance(opt, coredata.UserComboOption):
  108. optdict['choices'] = opt.choices
  109. typestr = 'combo'
  110. elif isinstance(opt, coredata.UserStringArrayOption):
  111. typestr = 'stringarray'
  112. else:
  113. raise RuntimeError("Unknown option type")
  114. optdict['type'] = typestr
  115. optdict['description'] = opt.description
  116. optlist.append(optdict)
  117. def list_buildsystem_files(coredata, builddata):
  118. src_dir = builddata.environment.get_source_dir()
  119. # I feel dirty about this. But only slightly.
  120. filelist = []
  121. for root, _, files in os.walk(src_dir):
  122. for f in files:
  123. if f == 'meson.build' or f == 'meson_options.txt':
  124. filelist.append(os.path.relpath(os.path.join(root, f), src_dir))
  125. print(json.dumps(filelist))
  126. def list_deps(coredata):
  127. result = {}
  128. for d in coredata.deps.values():
  129. if d.found():
  130. args = {'compile_args': d.get_compile_args(),
  131. 'link_args': d.get_link_args()}
  132. result[d.name] = args
  133. print(json.dumps(result))
  134. def list_tests(testdata):
  135. result = []
  136. for t in testdata:
  137. to = {}
  138. if isinstance(t.fname, str):
  139. fname = [t.fname]
  140. else:
  141. fname = t.fname
  142. to['cmd'] = fname + t.cmd_args
  143. to['env'] = t.env
  144. to['name'] = t.name
  145. to['workdir'] = t.workdir
  146. to['timeout'] = t.timeout
  147. to['suite'] = t.suite
  148. result.append(to)
  149. print(json.dumps(result))
  150. def run(args):
  151. options = parser.parse_args(args)
  152. if len(options.args) > 1:
  153. print('Too many arguments')
  154. return 1
  155. elif len(options.args) == 1:
  156. bdir = options.args[0]
  157. else:
  158. bdir = ''
  159. corefile = os.path.join(bdir, 'meson-private/coredata.dat')
  160. buildfile = os.path.join(bdir, 'meson-private/build.dat')
  161. testfile = os.path.join(bdir, 'meson-private/meson_test_setup.dat')
  162. benchmarkfile = os.path.join(bdir, 'meson-private/meson_benchmark_setup.dat')
  163. with open(corefile, 'rb') as f:
  164. coredata = pickle.load(f)
  165. with open(buildfile, 'rb') as f:
  166. builddata = pickle.load(f)
  167. with open(testfile, 'rb') as f:
  168. testdata = pickle.load(f)
  169. with open(benchmarkfile, 'rb') as f:
  170. benchmarkdata = pickle.load(f)
  171. if options.list_targets:
  172. list_targets(coredata, builddata)
  173. elif options.target_files is not None:
  174. list_target_files(options.target_files, coredata, builddata)
  175. elif options.buildsystem_files:
  176. list_buildsystem_files(coredata, builddata)
  177. elif options.buildoptions:
  178. list_buildoptions(coredata, builddata)
  179. elif options.tests:
  180. list_tests(testdata)
  181. elif options.benchmarks:
  182. list_tests(benchmarkdata)
  183. elif options.dependencies:
  184. list_deps(coredata)
  185. else:
  186. print('No command specified')
  187. return 1
  188. return 0
  189. if __name__ == '__main__':
  190. sys.exit(run(sys.argv[1:]))