mirror of https://gitlab.com/ita1024/waf.git
clang_compilation_database: fix #2247, add clangdb command to generate database by request without rebuilding, add tests (WIP)
This commit is contained in:
parent
4389e4400e
commit
b8fd6a0515
|
@ -0,0 +1,68 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# encoding: utf-8
|
||||||
|
# Alibek Omarov, 2019 (a1batross)
|
||||||
|
|
||||||
|
import os
|
||||||
|
from waflib import ConfigSet, Logs
|
||||||
|
|
||||||
|
VERSION='0.0.1'
|
||||||
|
APPNAME='clang_compilation_database_test'
|
||||||
|
|
||||||
|
top = '.'
|
||||||
|
out = 'build'
|
||||||
|
|
||||||
|
INCLUDES_TO_TEST = ['common'] # check if include flag appeared in result json
|
||||||
|
DEFINES_TO_TEST = ['TEST'] # check if definition flag will appear in result json
|
||||||
|
SOURCE_FILES_TO_TEST = ['a.c', 'b.cpp'] # check if source files are persist in database
|
||||||
|
|
||||||
|
def actual_test(bld):
|
||||||
|
db = bld.bldnode.find_node('compile_commands.json').read_json()
|
||||||
|
|
||||||
|
for entry in db:
|
||||||
|
env = ConfigSet.ConfigSet()
|
||||||
|
line = ' '.join(entry['arguments'][1:]) # ignore compiler exe, unneeded
|
||||||
|
directory = entry['directory']
|
||||||
|
srcname = entry['file'].split(os.sep)[-1] # file name only
|
||||||
|
|
||||||
|
bld.parse_flags(line, 'test', env) # ignore unhandled flag, it's harmless for test
|
||||||
|
|
||||||
|
if bld.bldnode.abspath() in directory:
|
||||||
|
Logs.info('Directory test passed')
|
||||||
|
else:
|
||||||
|
Logs.error('Directory test failed')
|
||||||
|
|
||||||
|
if srcname in SOURCE_FILES_TO_TEST:
|
||||||
|
Logs.info('Source file test passed')
|
||||||
|
else:
|
||||||
|
Logs.error('Source file test failed')
|
||||||
|
|
||||||
|
passed = True
|
||||||
|
for inc in INCLUDES_TO_TEST:
|
||||||
|
if inc not in env.INCLUDES_test:
|
||||||
|
passed = False
|
||||||
|
|
||||||
|
if passed: Logs.info('Includes test passed')
|
||||||
|
else: Logs.error('Includes test failed')
|
||||||
|
|
||||||
|
passed = True
|
||||||
|
for define in DEFINES_TO_TEST:
|
||||||
|
if define not in env.DEFINES_test:
|
||||||
|
passed = False
|
||||||
|
if passed: Logs.info('Defines test passed')
|
||||||
|
else: Logs.error('Defines test failed')
|
||||||
|
|
||||||
|
def options(opt):
|
||||||
|
# check by ./waf clangdb
|
||||||
|
opt.load('compiler_c compiler_cxx clang_compilation_database')
|
||||||
|
|
||||||
|
def configure(conf):
|
||||||
|
# check if database always generated before build
|
||||||
|
conf.load('compiler_c compiler_cxx clang_compilation_database')
|
||||||
|
|
||||||
|
def build(bld):
|
||||||
|
bld.shlib(features = 'c cxx', source = SOURCE_FILES_TO_TEST,
|
||||||
|
defines = DEFINES_TO_TEST,
|
||||||
|
includes = INCLUDES_TO_TEST,
|
||||||
|
target = 'test')
|
||||||
|
|
||||||
|
bld.add_post_fun(actual_test)
|
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
# Christoph Koke, 2013
|
# Christoph Koke, 2013
|
||||||
|
# Alibek Omarov, 2019
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Writes the c and cpp compile commands into build/compile_commands.json
|
Writes the c and cpp compile commands into build/compile_commands.json
|
||||||
|
@ -8,14 +9,24 @@ see http://clang.llvm.org/docs/JSONCompilationDatabase.html
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
|
Load this tool in `options` to be able to generate database
|
||||||
|
by request in command-line and before build:
|
||||||
|
|
||||||
|
$ waf clangdb
|
||||||
|
|
||||||
|
def options(opt):
|
||||||
|
opt.load('clang_compilation_database')
|
||||||
|
|
||||||
|
Otherwise, load only in `configure` to generate it always before build.
|
||||||
|
|
||||||
def configure(conf):
|
def configure(conf):
|
||||||
conf.load('compiler_cxx')
|
conf.load('compiler_cxx')
|
||||||
...
|
...
|
||||||
conf.load('clang_compilation_database')
|
conf.load('clang_compilation_database')
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys, os, json, shlex, pipes
|
import os
|
||||||
from waflib import Logs, TaskGen, Task
|
from waflib import Logs, TaskGen, Task, Build, Scripting
|
||||||
|
|
||||||
Task.Task.keep_last_cmd = True
|
Task.Task.keep_last_cmd = True
|
||||||
|
|
||||||
|
@ -23,32 +34,36 @@ Task.Task.keep_last_cmd = True
|
||||||
@TaskGen.after_method('process_use')
|
@TaskGen.after_method('process_use')
|
||||||
def collect_compilation_db_tasks(self):
|
def collect_compilation_db_tasks(self):
|
||||||
"Add a compilation database entry for compiled tasks"
|
"Add a compilation database entry for compiled tasks"
|
||||||
try:
|
if not isinstance(self.bld, ClangDbContext):
|
||||||
clang_db = self.bld.clang_compilation_database_tasks
|
return
|
||||||
except AttributeError:
|
|
||||||
clang_db = self.bld.clang_compilation_database_tasks = []
|
|
||||||
self.bld.add_post_fun(write_compilation_database)
|
|
||||||
|
|
||||||
tup = tuple(y for y in [Task.classes.get(x) for x in ('c', 'cxx')] if y)
|
tup = tuple(y for y in [Task.classes.get(x) for x in ('c', 'cxx')] if y)
|
||||||
for task in getattr(self, 'compiled_tasks', []):
|
for task in getattr(self, 'compiled_tasks', []):
|
||||||
if isinstance(task, tup):
|
if isinstance(task, tup):
|
||||||
clang_db.append(task)
|
self.bld.clang_compilation_database_tasks.append(task)
|
||||||
|
|
||||||
def write_compilation_database(ctx):
|
class ClangDbContext(Build.BuildContext):
|
||||||
"Write the clang compilation database as JSON"
|
'''generates compile_commands.json by request'''
|
||||||
database_file = ctx.bldnode.make_node('compile_commands.json')
|
cmd = 'clangdb'
|
||||||
Logs.info('Build commands will be stored in %s', database_file.path_from(ctx.path))
|
clang_compilation_database_tasks = []
|
||||||
|
|
||||||
|
def write_compilation_database(self):
|
||||||
|
"""
|
||||||
|
Write the clang compilation database as JSON
|
||||||
|
"""
|
||||||
|
database_file = self.bldnode.make_node('compile_commands.json')
|
||||||
|
Logs.info('Build commands will be stored in %s', database_file.path_from(self.path))
|
||||||
try:
|
try:
|
||||||
root = json.load(database_file)
|
root = database_file.read_json()
|
||||||
except IOError:
|
except IOError:
|
||||||
root = []
|
root = []
|
||||||
clang_db = dict((x['file'], x) for x in root)
|
clang_db = dict((x['file'], x) for x in root)
|
||||||
for task in getattr(ctx, 'clang_compilation_database_tasks', []):
|
for task in self.clang_compilation_database_tasks:
|
||||||
try:
|
try:
|
||||||
cmd = task.last_cmd
|
cmd = task.last_cmd
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
continue
|
continue
|
||||||
directory = getattr(task, 'cwd', ctx.variant_dir)
|
directory = getattr(task, 'cwd', self.variant_dir)
|
||||||
f_node = task.inputs[0]
|
f_node = task.inputs[0]
|
||||||
filename = os.path.relpath(f_node.abspath(), directory)
|
filename = os.path.relpath(f_node.abspath(), directory)
|
||||||
entry = {
|
entry = {
|
||||||
|
@ -58,28 +73,65 @@ def write_compilation_database(ctx):
|
||||||
}
|
}
|
||||||
clang_db[filename] = entry
|
clang_db[filename] = entry
|
||||||
root = list(clang_db.values())
|
root = list(clang_db.values())
|
||||||
database_file.write(json.dumps(root, indent=2))
|
database_file.write_json(root)
|
||||||
|
|
||||||
# Override the runnable_status function to do a dummy/dry run when the file doesn't need to be compiled.
|
def execute(self):
|
||||||
# This will make sure compile_commands.json is always fully up to date.
|
"""
|
||||||
# Previously you could end up with a partial compile_commands.json if the build failed.
|
Build dry run
|
||||||
for x in ('c', 'cxx'):
|
"""
|
||||||
if x not in Task.classes:
|
self.restore()
|
||||||
continue
|
|
||||||
|
|
||||||
t = Task.classes[x]
|
if not self.all_envs:
|
||||||
|
self.load_envs()
|
||||||
|
|
||||||
def runnable_status(self):
|
self.recurse([self.run_dir])
|
||||||
def exec_command(cmd, **kw):
|
self.pre_build()
|
||||||
|
|
||||||
|
# we need only to generate last_cmd, so override
|
||||||
|
# exec_command temporarily
|
||||||
|
def exec_command(self, *k, **kw):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
for g in self.groups:
|
||||||
|
for tg in g:
|
||||||
|
try:
|
||||||
|
f = tg.post
|
||||||
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
|
f()
|
||||||
|
|
||||||
run_status = self.old_runnable_status()
|
if isinstance(tg, Task.Task):
|
||||||
if run_status == Task.SKIP_ME:
|
lst = [tg]
|
||||||
setattr(self, 'old_exec_command', getattr(self, 'exec_command', None))
|
else: lst = tg.tasks
|
||||||
setattr(self, 'exec_command', exec_command)
|
for tsk in lst:
|
||||||
self.run()
|
tup = tuple(y for y in [Task.classes.get(x) for x in ('c', 'cxx')] if y)
|
||||||
setattr(self, 'exec_command', getattr(self, 'old_exec_command', None))
|
if isinstance(tsk, tup):
|
||||||
return run_status
|
old_exec = tsk.exec_command
|
||||||
|
tsk.exec_command = exec_command
|
||||||
|
tsk.run()
|
||||||
|
tsk.exec_command = old_exec
|
||||||
|
|
||||||
setattr(t, 'old_runnable_status', getattr(t, 'runnable_status', None))
|
self.write_compilation_database()
|
||||||
setattr(t, 'runnable_status', runnable_status)
|
|
||||||
|
EXECUTE_PATCHED = False
|
||||||
|
def patch_execute():
|
||||||
|
global EXECUTE_PATCHED
|
||||||
|
|
||||||
|
if EXECUTE_PATCHED:
|
||||||
|
return
|
||||||
|
|
||||||
|
def new_execute_build(self):
|
||||||
|
"""
|
||||||
|
Invoke clangdb command before build
|
||||||
|
"""
|
||||||
|
if type(self) == Build.BuildContext:
|
||||||
|
Scripting.run_command('clangdb')
|
||||||
|
|
||||||
|
old_execute_build(self)
|
||||||
|
|
||||||
|
old_execute_build = getattr(Build.BuildContext, 'execute_build', None)
|
||||||
|
setattr(Build.BuildContext, 'execute_build', new_execute_build)
|
||||||
|
EXECUTE_PATCHED = True
|
||||||
|
|
||||||
|
patch_execute()
|
||||||
|
|
Loading…
Reference in New Issue