mintro.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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
  18. import argparse
  19. import sys, os
  20. import pathlib
  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('--installed', action='store_true', dest='list_installed', default=False,
  25. help='List all installed files and directories.')
  26. parser.add_argument('--target-files', action='store', dest='target_files', default=None,
  27. help='List source files for a given target.')
  28. parser.add_argument('--buildsystem-files', action='store_true', dest='buildsystem_files', default=False,
  29. help='List files that make up the build system.')
  30. parser.add_argument('--buildoptions', action='store_true', dest='buildoptions', default=False,
  31. help='List all build options.')
  32. parser.add_argument('--tests', action='store_true', dest='tests', default=False,
  33. help='List all unit tests.')
  34. parser.add_argument('--benchmarks', action='store_true', dest='benchmarks', default=False,
  35. help='List all benchmarks.')
  36. parser.add_argument('--dependencies', action='store_true', dest='dependencies', default=False,
  37. help='List external dependencies.')
  38. parser.add_argument('--projectinfo', action='store_true', dest='projectinfo', default=False,
  39. help='Information about projects.')
  40. parser.add_argument('builddir', nargs='?', help='The build directory')
  41. def determine_installed_path(target, installdata):
  42. install_target = None
  43. for i in installdata.targets:
  44. if os.path.split(i[0])[1] == target.get_filename(): # FIXME, might clash due to subprojects.
  45. install_target = i
  46. break
  47. if install_target is None:
  48. raise RuntimeError('Something weird happened. File a bug.')
  49. fname = i[0]
  50. outdir = i[1]
  51. outname = os.path.join(installdata.prefix, outdir, os.path.split(fname)[-1])
  52. # Normalize the path by using os.path.sep consistently, etc.
  53. # Does not change the effective path.
  54. return str(pathlib.PurePath(outname))
  55. def list_installed(installdata):
  56. res = {}
  57. if installdata is not None:
  58. for path, installdir, aliases, unknown1, unknown2 in installdata.targets:
  59. res[os.path.join(installdata.build_dir, path)] = os.path.join(installdata.prefix, installdir, os.path.basename(path))
  60. for path, installpath, unused_prefix in installdata.data:
  61. res[path] = os.path.join(installdata.prefix, installpath)
  62. for path, installdir in installdata.headers:
  63. res[path] = os.path.join(installdata.prefix, installdir, os.path.basename(path))
  64. for path, installpath in installdata.man:
  65. res[path] = os.path.join(installdata.prefix, installpath)
  66. print(json.dumps(res))
  67. def list_targets(coredata, builddata, installdata):
  68. tlist = []
  69. for (idname, target) in builddata.get_targets().items():
  70. t = {'name': target.get_basename(), 'id': idname}
  71. fname = target.get_filename()
  72. if isinstance(fname, list):
  73. fname = [os.path.join(target.subdir, x) for x in fname]
  74. else:
  75. fname = os.path.join(target.subdir, fname)
  76. t['filename'] = fname
  77. if isinstance(target, build.Executable):
  78. typename = 'executable'
  79. elif isinstance(target, build.SharedLibrary):
  80. typename = 'shared library'
  81. elif isinstance(target, build.StaticLibrary):
  82. typename = 'static library'
  83. elif isinstance(target, build.CustomTarget):
  84. typename = 'custom'
  85. elif isinstance(target, build.RunTarget):
  86. typename = 'run'
  87. else:
  88. typename = 'unknown'
  89. t['type'] = typename
  90. if installdata and target.should_install():
  91. t['installed'] = True
  92. t['install_filename'] = determine_installed_path(target, installdata)
  93. else:
  94. t['installed'] = False
  95. tlist.append(t)
  96. print(json.dumps(tlist))
  97. def list_target_files(target_name, coredata, builddata):
  98. try:
  99. t = builddata.targets[target_name]
  100. sources = t.sources + t.extra_files
  101. except KeyError:
  102. print("Unknown target %s." % target_name)
  103. sys.exit(1)
  104. sources = [os.path.join(i.subdir, i.fname) for i in sources]
  105. print(json.dumps(sources))
  106. def list_buildoptions(coredata, builddata):
  107. optlist = []
  108. add_keys(optlist, coredata.user_options)
  109. add_keys(optlist, coredata.compiler_options)
  110. add_keys(optlist, coredata.base_options)
  111. add_keys(optlist, coredata.builtins)
  112. print(json.dumps(optlist))
  113. def add_keys(optlist, options):
  114. keys = list(options.keys())
  115. keys.sort()
  116. for key in keys:
  117. opt = options[key]
  118. optdict = {'name': key, 'value': opt.value}
  119. if isinstance(opt, coredata.UserStringOption):
  120. typestr = 'string'
  121. elif isinstance(opt, coredata.UserBooleanOption):
  122. typestr = 'boolean'
  123. elif isinstance(opt, coredata.UserComboOption):
  124. optdict['choices'] = opt.choices
  125. typestr = 'combo'
  126. elif isinstance(opt, coredata.UserStringArrayOption):
  127. typestr = 'stringarray'
  128. else:
  129. raise RuntimeError("Unknown option type")
  130. optdict['type'] = typestr
  131. optdict['description'] = opt.description
  132. optlist.append(optdict)
  133. def list_buildsystem_files(coredata, builddata):
  134. src_dir = builddata.environment.get_source_dir()
  135. # I feel dirty about this. But only slightly.
  136. filelist = []
  137. for root, _, files in os.walk(src_dir):
  138. for f in files:
  139. if f == 'meson.build' or f == 'meson_options.txt':
  140. filelist.append(os.path.relpath(os.path.join(root, f), src_dir))
  141. print(json.dumps(filelist))
  142. def list_deps(coredata):
  143. result = []
  144. for d in coredata.deps.values():
  145. if d.found():
  146. result += [{'name': d.name,
  147. 'compile_args': d.get_compile_args(),
  148. 'link_args': d.get_link_args()}]
  149. print(json.dumps(result))
  150. def list_tests(testdata):
  151. result = []
  152. for t in testdata:
  153. to = {}
  154. if isinstance(t.fname, str):
  155. fname = [t.fname]
  156. else:
  157. fname = t.fname
  158. to['cmd'] = fname + t.cmd_args
  159. if isinstance(t.env, build.EnvironmentVariables):
  160. to['env'] = t.env.get_env(os.environ)
  161. else:
  162. to['env'] = t.env
  163. to['name'] = t.name
  164. to['workdir'] = t.workdir
  165. to['timeout'] = t.timeout
  166. to['suite'] = t.suite
  167. result.append(to)
  168. print(json.dumps(result))
  169. def list_projinfo(builddata):
  170. result = {'name': builddata.project_name, 'version': builddata.project_version}
  171. subprojects = []
  172. for k, v in builddata.subprojects.items():
  173. c = {'name': k,
  174. 'version': v}
  175. subprojects.append(c)
  176. result['subprojects'] = subprojects
  177. print(json.dumps(result))
  178. def run(args):
  179. datadir = 'meson-private'
  180. options = parser.parse_args(args)
  181. if options.builddir is not None:
  182. datadir = os.path.join(options.builddir, datadir)
  183. if not os.path.isdir(datadir):
  184. print('Current directory is not a build dir. Please specify it or '
  185. 'change the working directory to it.')
  186. return 1
  187. corefile = os.path.join(datadir, 'coredata.dat')
  188. buildfile = os.path.join(datadir, 'build.dat')
  189. installfile = os.path.join(datadir, 'install.dat')
  190. testfile = os.path.join(datadir, 'meson_test_setup.dat')
  191. benchmarkfile = os.path.join(datadir, 'meson_benchmark_setup.dat')
  192. # Load all data files
  193. with open(corefile, 'rb') as f:
  194. coredata = pickle.load(f)
  195. with open(buildfile, 'rb') as f:
  196. builddata = pickle.load(f)
  197. with open(testfile, 'rb') as f:
  198. testdata = pickle.load(f)
  199. with open(benchmarkfile, 'rb') as f:
  200. benchmarkdata = pickle.load(f)
  201. # Install data is only available with the Ninja backend
  202. if os.path.isfile(installfile):
  203. with open(installfile, 'rb') as f:
  204. installdata = pickle.load(f)
  205. else:
  206. installdata = None
  207. if options.list_targets:
  208. list_targets(coredata, builddata, installdata)
  209. elif options.list_installed:
  210. list_installed(installdata)
  211. elif options.target_files is not None:
  212. list_target_files(options.target_files, coredata, builddata)
  213. elif options.buildsystem_files:
  214. list_buildsystem_files(coredata, builddata)
  215. elif options.buildoptions:
  216. list_buildoptions(coredata, builddata)
  217. elif options.tests:
  218. list_tests(testdata)
  219. elif options.benchmarks:
  220. list_tests(benchmarkdata)
  221. elif options.dependencies:
  222. list_deps(coredata)
  223. elif options.projectinfo:
  224. list_projinfo(builddata)
  225. else:
  226. print('No command specified')
  227. return 1
  228. return 0
  229. if __name__ == '__main__':
  230. sys.exit(run(sys.argv[1:]))