mesonrewriter.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. # Copyright 2016 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 class contains the basic functionality needed to run any interpreter
  13. # or an interpreter-based tool.
  14. # This tool is used to manipulate an existing Meson build definition.
  15. #
  16. # - add a file to a target
  17. # - remove files from a target
  18. # - move targets
  19. # - reindent?
  20. import mesonbuild.astinterpreter
  21. from mesonbuild.mesonlib import MesonException
  22. from mesonbuild import mlog
  23. import sys, traceback
  24. import argparse
  25. parser = argparse.ArgumentParser()
  26. parser.add_argument('--sourcedir', default='.',
  27. help='Path to source directory.')
  28. parser.add_argument('--target', default=None,
  29. help='Name of target to edit.')
  30. parser.add_argument('--filename', default=None,
  31. help='Name of source file to add or remove to target.')
  32. parser.add_argument('commands', nargs='+')
  33. if __name__ == '__main__':
  34. options = parser.parse_args()
  35. if options.target is None or options.filename is None:
  36. sys.exit("Must specify both target and filename.")
  37. print('This tool is highly experimental, use with care.')
  38. rewriter = mesonbuild.astinterpreter.AstInterpreter(options.sourcedir, '')
  39. try:
  40. if options.commands[0] == 'add':
  41. rewriter.add_source(options.target, options.filename)
  42. elif options.commands[0] == 'remove':
  43. rewriter.remove_source(options.target, options.filename)
  44. else:
  45. sys.exit('Unknown command: ' + options.commands[0])
  46. except Exception as e:
  47. if isinstance(e, MesonException):
  48. if hasattr(e, 'file') and hasattr(e, 'lineno') and hasattr(e, 'colno'):
  49. mlog.log(mlog.red('\nMeson encountered an error in file %s, line %d, column %d:' % (e.file, e.lineno, e.colno)))
  50. else:
  51. mlog.log(mlog.red('\nMeson encountered an error:'))
  52. mlog.log(e)
  53. else:
  54. traceback.print_exc()
  55. sys.exit(1)