jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.8KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Christoph Koke, 2013
  4. """
  5. Writes the c and cpp compile commands into build/compile_commands.json
  6. see http://clang.llvm.org/docs/JSONCompilationDatabase.html
  7. Usage:
  8. def configure(conf):
  9. conf.load('compiler_cxx')
  10. ...
  11. conf.load('clang_compilation_database')
  12. """
  13. import sys, os, json, shlex, pipes
  14. from waflib import Logs, TaskGen
  15. from waflib.Tools import c, cxx
  16. if sys.hexversion >= 0x3030000:
  17. quote = shlex.quote
  18. else:
  19. quote = pipes.quote
  20. @TaskGen.feature('*')
  21. @TaskGen.after_method('process_use')
  22. def collect_compilation_db_tasks(self):
  23. "Add a compilation database entry for compiled tasks"
  24. try:
  25. clang_db = self.bld.clang_compilation_database_tasks
  26. except AttributeError:
  27. clang_db = self.bld.clang_compilation_database_tasks = []
  28. self.bld.add_post_fun(write_compilation_database)
  29. for task in getattr(self, 'compiled_tasks', []):
  30. if isinstance(task, (c.c, cxx.cxx)):
  31. clang_db.append(task)
  32. def write_compilation_database(ctx):
  33. "Write the clang compilation database as JSON"
  34. database_file = ctx.bldnode.make_node('compile_commands.json')
  35. Logs.info("Build commands will be stored in %s" % database_file.path_from(ctx.path))
  36. try:
  37. root = json.load(database_file)
  38. except IOError:
  39. root = []
  40. clang_db = dict((x["file"], x) for x in root)
  41. for task in getattr(ctx, 'clang_compilation_database_tasks', []):
  42. try:
  43. cmd = task.last_cmd
  44. except AttributeError:
  45. continue
  46. directory = getattr(task, 'cwd', ctx.variant_dir)
  47. f_node = task.inputs[0]
  48. filename = os.path.relpath(f_node.abspath(), directory)
  49. cmd = " ".join(map(quote, cmd))
  50. entry = {
  51. "directory": directory,
  52. "command": cmd,
  53. "file": filename,
  54. }
  55. clang_db[filename] = entry
  56. root = list(clang_db.values())
  57. database_file.write(json.dumps(root, indent=2))