waf-tools/clang_compilation_database.py
author Sébastien Deronne <sebastien.deronne@gmail.com>
Sun, 09 Dec 2018 12:14:03 +0100
changeset 13895 5df69eea72d6
parent 11457 5f76cbf2850b
permissions -rw-r--r--
wifi: Do not allow S-MPDU transmissions as long as ADDBA handshake is not established
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11457
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     2
# encoding: utf-8
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     3
# Christoph Koke, 2013
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     4
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     5
"""
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     6
Writes the c and cpp compile commands into build/compile_commands.json
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     7
see http://clang.llvm.org/docs/JSONCompilationDatabase.html
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     8
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
     9
Usage:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    10
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    11
    def configure(conf):
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    12
        conf.load('compiler_cxx')
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    13
        ...
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    14
        conf.load('clang_compilation_database')
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    15
"""
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    16
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    17
import sys, os, json, shlex, pipes
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    18
from waflib import Logs, TaskGen
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    19
from waflib.Tools import c, cxx
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    20
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    21
if sys.hexversion >= 0x3030000:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    22
	quote = shlex.quote
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    23
else:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    24
	quote = pipes.quote
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    25
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    26
@TaskGen.feature('*')
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    27
@TaskGen.after_method('process_use')
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    28
def collect_compilation_db_tasks(self):
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    29
	"Add a compilation database entry for compiled tasks"
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    30
	try:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    31
		clang_db = self.bld.clang_compilation_database_tasks
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    32
	except AttributeError:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    33
		clang_db = self.bld.clang_compilation_database_tasks = []
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    34
		self.bld.add_post_fun(write_compilation_database)
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    35
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    36
	for task in getattr(self, 'compiled_tasks', []):
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    37
		if isinstance(task, (c.c, cxx.cxx)):
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    38
			clang_db.append(task)
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    39
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    40
def write_compilation_database(ctx):
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    41
	"Write the clang compilation database as JSON"
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    42
	database_file = ctx.bldnode.make_node('compile_commands.json')
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    43
	Logs.info("Build commands will be stored in %s" % database_file.path_from(ctx.path))
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    44
	try:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    45
		root = json.load(database_file)
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    46
	except IOError:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    47
		root = []
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    48
	clang_db = dict((x["file"], x) for x in root)
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    49
	for task in getattr(ctx, 'clang_compilation_database_tasks', []):
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    50
		try:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    51
			cmd = task.last_cmd
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    52
		except AttributeError:
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    53
			continue
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    54
		directory = getattr(task, 'cwd', ctx.variant_dir)
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    55
		f_node = task.inputs[0]
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    56
		filename = os.path.relpath(f_node.abspath(), directory)
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    57
		cmd = " ".join(map(quote, cmd))
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    58
		entry = {
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    59
			"directory": directory,
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    60
			"command": cmd,
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    61
			"file": filename,
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    62
		}
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    63
		clang_db[filename] = entry
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    64
	root = list(clang_db.values())
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    65
	database_file.write(json.dumps(root, indent=2))
5f76cbf2850b Update Waf to 1.8.11
Matthieu Coudron <mattator@gmail.com>
parents:
diff changeset
    66