From cddeec37f196eeb279ea9f06ec658d21c0971975 Mon Sep 17 00:00:00 2001 From: William Deegan Date: Sat, 6 Jun 2020 16:25:53 -0700 Subject: Added tests which should cover all the variations of ways to call CompilationDatabase --- CHANGES.txt | 2 + SCons/Tool/compilation_db.xml | 4 ++ test/CompilationDatabase/basic.py | 79 ++++++++++++++++++++++++++--- test/CompilationDatabase/fixture/SConstruct | 27 ++++++++-- 4 files changed, 101 insertions(+), 11 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index b04615f..26df3c5 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -57,6 +57,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER changing the name was required as we wanted to import symbols for use in compilation_db tool. - Add CompilationDatabase() builder in compilation_db tool. Contributed by MongoDB. + Setting COMPILATIONDB_USE_ABSPATH to True|False controls whether the files are absolute or relative + paths. From Jeremy Elson: - Updated design doc to use the correct syntax for Depends() diff --git a/SCons/Tool/compilation_db.xml b/SCons/Tool/compilation_db.xml index 1aec177..47126a7 100644 --- a/SCons/Tool/compilation_db.xml +++ b/SCons/Tool/compilation_db.xml @@ -63,6 +63,10 @@ env.CompilationDatabase('my_output.json') the C, C++, assembly source/target pairs. + NOTE: You must load the compilation_db tool prior to specifying any part of your build or some source/target + files will not show up in your output file. + + New in &SCons; 4.0.0 diff --git a/test/CompilationDatabase/basic.py b/test/CompilationDatabase/basic.py index 927bec7..b06098b 100644 --- a/test/CompilationDatabase/basic.py +++ b/test/CompilationDatabase/basic.py @@ -1,13 +1,40 @@ +#!/usr/bin/env python +# +# __COPYRIGHT__ +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# +""" +Test CompilationDatabase and several variations of ways to call it +and values of COMPILATIONDB_USE_ABSPATH +""" + import sys import TestSCons - test = TestSCons.TestSCons() if sys.platform == 'win32': test.file_fixture('mylink_win32.py', 'mylink.py') else: - test.file_fixture('mylink.py') + test.file_fixture('mylink.py') test.file_fixture('mygcc.py') @@ -15,8 +42,48 @@ test.verbose_set(1) test.file_fixture('fixture/SConstruct') test.file_fixture('test_main.c') test.run() -test.must_exist('compile_commands.json') -# Now test with absolute paths -test.run(arguments='ABSPATH=1') -test.must_exist('compile_commands_abs.json') +rel_files = [ + 'compile_commands_only_arg.json', + 'compile_commands_target.json', + 'compile_commands.json', + 'compile_commands_over_rel.json', + 'compile_commands_over_abs_0.json' +] + +abs_files = [ + 'compile_commands_clone_abs.json', + 'compile_commands_over_abs.json', + 'compile_commands_target_over_abs.json', + 'compile_commands_over_abs_1.json', +] + +example_rel_file = """[ + { + "command": "%s mygcc.py cc -o test_main.o -c test_main.c", + "directory": "%s", + "file": "test_main.c", + "target": "test_main.o" + } +]""" % (sys.executable, test.workdir) + +for f in rel_files: + # print("Checking:%s" % f) + test.must_exist(f) + test.must_match(f, example_rel_file) + +example_abs_file = """[ + { + "command": "%s mygcc.py cc -o test_main.o -c test_main.c", + "directory": "%s", + "file": "%s/test_main.c", + "target": "%s/test_main.o" + } +]""" % (sys.executable, test.workdir, test.workdir, test.workdir) + +for f in abs_files: + test.must_exist(f) + test.must_match(f, example_abs_file) + + +test.pass_test() diff --git a/test/CompilationDatabase/fixture/SConstruct b/test/CompilationDatabase/fixture/SConstruct index 0bbcafa..7453161 100644 --- a/test/CompilationDatabase/fixture/SConstruct +++ b/test/CompilationDatabase/fixture/SConstruct @@ -10,10 +10,27 @@ env = Environment( ) env.Tool('compilation_db') -if ARGUMENTS.get('ABSPATH', False): - env['COMPILATIONDB_USE_ABSPATH'] = True - env.CompilationDatabase('compile_commands_abs.json', source=[]) -else: - env.CompilationDatabase('compile_commands.json', source=[]) +env_abs = env.Clone(COMPILATIONDB_USE_ABSPATH=True) +env_abs.CompilationDatabase('compile_commands_clone_abs.json') + +# Should be relative paths +env.CompilationDatabase('compile_commands_only_arg.json') +env.CompilationDatabase(target='compile_commands_target.json') + +# Should default name compile_commands.json +env.CompilationDatabase() + +# Should be absolute paths +env.CompilationDatabase('compile_commands_over_abs.json', COMPILATIONDB_USE_ABSPATH=True) +env.CompilationDatabase(target='compile_commands_target_over_abs.json', COMPILATIONDB_USE_ABSPATH=True) + +# Should be relative paths +env.CompilationDatabase('compile_commands_over_rel.json', COMPILATIONDB_USE_ABSPATH=False) + + +# Try 1/0 for COMPILATIONDB_USE_ABSPATH +env.CompilationDatabase('compile_commands_over_abs_1.json', COMPILATIONDB_USE_ABSPATH=1) +env.CompilationDatabase('compile_commands_over_abs_0.json', COMPILATIONDB_USE_ABSPATH=0) + env.Program('main', 'test_main.c') -- cgit v0.12