diff --git a/.gitignore b/.gitignore index 620727ee..3c450366 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build/ dist/ *.egg-info/ +.venv/ \ No newline at end of file diff --git a/cbi.log b/cbi.log new file mode 100644 index 00000000..e69de29b diff --git a/codebasin/__main__.py b/codebasin/__main__.py index 359c4323..5a7b96a7 100755 --- a/codebasin/__main__.py +++ b/codebasin/__main__.py @@ -240,7 +240,8 @@ def _main() -> None: p = analysis_toml["platform"][name]["commands"] db = config.load_database(p, rootdir) args.platforms.append(name) - configuration.update({name: db}) + configuration[name] = db + # Construct a codebase object associated with the root directory. codebase = CodeBase(rootdir, exclude_patterns=args.excludes) diff --git a/codebasin/config.py b/codebasin/config.py index 4030e607..49772270 100644 --- a/codebasin/config.py +++ b/codebasin/config.py @@ -258,36 +258,105 @@ def _load_compilers() -> None: @dataclass + + class PreprocessorConfiguration: + + """ + + Represents the configuration for a specific file, including: + + - Macro definitions + + - Include paths + + - Include files + + - A meaningful pass name + + """ + + + + + file: str + + defines: list[str] + + include_paths: list[str] + + include_files: list[str] + + pass_name: str = "default" + + + + def _update(self, pass_or_mode: _CompilerPass | _CompilerMode) -> None: + + """ + + Update this PreprocessorConfiguration by extending the + + defines, include paths and include files using the values + + contained in the provided _CompilerPass or _CompilerMode. + Parameters + + ---------- + + pass_or_mode: _CompilerPass | _CompilerMode + + The pass or mode to enable. + + """ + + self.defines.extend(pass_or_mode.defines) + + self.include_paths.extend(pass_or_mode.include_paths) + + self.include_files.extend(pass_or_mode.include_files) + + + +@dataclass +class Configuration: + """ + Represents the configuration for a platform, including a list of + configurations for each file processed by the preprocessor. + """ + + files: list[PreprocessorConfiguration] + + class ArgumentParser: """ Represents the behavior of a specific compiler. @@ -331,13 +400,14 @@ def __init__(self, path: str) -> None: f"Compiler '{self.name}' recognized; aliases '{alias}'.", ) - def parse_args(self, argv: list[str]) -> list[PreprocessorConfiguration]: + def parse_args(self, filename: str, argv: list[str]) -> list[PreprocessorConfiguration]: """ Parameters ---------- + filename: str + The name of the file being compiled. argv: list[str] The list of arguments passed to the compiler. - Returns ------- list[PreprocessorConfiguration] @@ -420,6 +490,7 @@ def parse_args(self, argv: list[str]) -> list[PreprocessorConfiguration]: configurations = [] for pass_name in args.passes: config = PreprocessorConfiguration( + filename, args.defines.copy(), args.include_paths.copy(), args.include_files.copy(), @@ -449,7 +520,7 @@ def parse_args(self, argv: list[str]) -> list[PreprocessorConfiguration]: def load_database( dbpath: str | os.PathLike[str], rootdir: str | os.PathLike[str], -) -> list[dict[str, Any]]: +) -> Configuration: """ Load a compilation database. Return a list of compilation commands, where each command is @@ -457,7 +528,7 @@ def load_database( """ db = CompilationDatabase.from_file(dbpath) - configuration = [] + files = [] for command in db: # Skip commands that invoke unsupported tools. if not command.is_supported(): @@ -490,36 +561,26 @@ def load_database( # Parse command-line arguments, emulating compiler-specific behavior. compiler_name = os.path.basename(command.arguments[0]) parser = ArgumentParser(compiler_name) - preprocessor_configs = parser.parse_args(command.arguments[1:]) + preprocessor_configs = parser.parse_args(path, command.arguments[1:]) # Create a configuration entry for each compiler pass. # Each compiler pass may set different defines, etc. for preprocessor_config in preprocessor_configs: - entry = asdict(preprocessor_config) - - entry["file"] = path - - # Include paths may be specified relative to root - entry["include_paths"] = [ - os.path.abspath(os.path.join(rootdir, f)) - for f in entry["include_paths"] - ] - - configuration += [entry] + files.append(preprocessor_config) # Print variables for debugging purposes. if not log.isEnabledFor(logging.DEBUG): continue - pass_name = entry["pass_name"] + pass_name = preprocessor_config.pass_name for v in ["defines", "include_paths", "include_files"]: - if entry[v]: - value = " ".join(entry[v]) + if getattr(preprocessor_config, v): + value = " ".join(getattr(preprocessor_config, v)) log.debug(f"{v} for {path} in pass '{pass_name}': {value}") - if len(configuration) == 0: + if len(files) == 0: log.warning( f"No files found in compilation database at '{dbpath}'.\n" + "Ensure that 'directory' and 'file' are in the root directory.", ) - return configuration + return Configuration(files) diff --git a/codebasin/finder.py b/codebasin/finder.py index b60254ea..2466d4bb 100644 --- a/codebasin/finder.py +++ b/codebasin/finder.py @@ -15,6 +15,7 @@ from tqdm import tqdm from codebasin import CodeBase, file_parser, preprocessor +from codebasin.config import Configuration, PreprocessorConfiguration from codebasin.language import FileLanguage from codebasin.preprocessor import CodeNode, Node, Platform, SourceTree, Visit @@ -150,11 +151,10 @@ def associator(node: Node) -> Visit: tree.visit(associator) -# FIXME: configuration should be refactored to avoid such a complex type. def find( rootdir: str, codebase: CodeBase, - configuration: dict[Any, list[dict[str, Any]]], + configuration: dict[str, Configuration], *, summarize_only: bool = True, show_progress: bool = False, @@ -197,9 +197,9 @@ def _potential_file_generator( ): if f in codebase: filenames.add(f) - for p in configuration: - for e in configuration[p]: - filenames.add(e["file"]) + for platform_name, platform_config in configuration.items(): + for file_config in platform_config.files: + filenames.add(file_config.file) # Build a tree for each unique file for all platforms. state = ParserState(summarize_only) @@ -214,42 +214,39 @@ def _potential_file_generator( state.insert_file(str(f)) # Process each tree, by associating nodes with platforms - for p in tqdm( - configuration, + for platform_name, platform_config in tqdm( + configuration.items(), desc="Preprocessing", unit=" platform", leave=False, disable=not show_progress, ): - for e in tqdm( - configuration[p], - desc=p, + for file_config in tqdm( + platform_config.files, + desc=platform_name, unit=" file", leave=False, disable=not show_progress, ): - file_platform = Platform(p, rootdir) + file_platform = Platform(platform_name, rootdir) - for path in e["include_paths"]: + for path in file_config.include_paths: file_platform.add_include_path(path) - for definition in e["defines"]: + for definition in file_config.defines: macro = preprocessor.macro_from_definition_string(definition) file_platform.define(macro.name, macro) # Process include files. - # These modify the file_platform instance, but we throw away - # the active nodes after processing is complete. - for include in e["include_files"]: + for include in file_config.include_files: include_file = file_platform.find_include_file( include, - os.path.dirname(e["file"]), + os.path.dirname(file_config.file), ) if include_file: state.insert_file(include_file) state.associate(include_file, file_platform) - # Process the file, to build a list of associate nodes - state.associate(e["file"], file_platform) + state.associate(file_config.file, file_platform) return state diff --git a/codebasin/tree.py b/codebasin/tree.py index bf25e173..32f14a80 100755 --- a/codebasin/tree.py +++ b/codebasin/tree.py @@ -8,7 +8,7 @@ import sys from codebasin import CodeBase, __version__, config, finder, report, util - +from codebasin.config import Configuration # TODO: Refactor to avoid imports from __main__ from codebasin.__main__ import Formatter, _help_string @@ -137,7 +137,7 @@ def _tree(args: argparse.Namespace) -> None: if "commands" not in analysis_toml["platform"][name]: raise ValueError(f"Missing 'commands' for platform {name}") p = analysis_toml["platform"][name]["commands"] - db = config.load_database(p, rootdir) + db: Configuration = config.load_database(p, rootdir) args.platforms.append(name) configuration.update({name: db}) diff --git a/tests/basic_asm/test_basic_asm.py b/tests/basic_asm/test_basic_asm.py index 202a080a..d759cca8 100644 --- a/tests/basic_asm/test_basic_asm.py +++ b/tests/basic_asm/test_basic_asm.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestBasicAsm(unittest.TestCase): @@ -24,15 +25,17 @@ def test_yaml(self): codebase = CodeBase(self.rootdir) entries = [] for f in codebase: - entries.append( - { - "file": f, - "defines": [], - "include_paths": [], - "include_files": [], - }, + preprocessor_config = PreprocessorConfiguration( + file=f, + defines=[], + include_paths=[], + include_files=[] ) - configuration = {"CPU": entries} + entries.append(preprocessor_config) + + configuration_obj = Configuration(files=entries) + + configuration = {"CPU": configuration_obj} state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) self.assertDictEqual( @@ -44,13 +47,15 @@ def test_yaml(self): def test_ptx(self): """basic_asm/basic_asm_ptx.yaml""" codebase = CodeBase(self.rootdir) - entry = { - "file": str(self.rootdir / "test.ptx"), - "defines": [], - "include_paths": [], - "include_files": [], - } - configuration = {"GPU": [entry]} + preprocessor_config = PreprocessorConfiguration( + file=str(self.rootdir / "test.ptx"), + defines=[], + include_paths=[], + include_files=[], + ) + configuration_obj = Configuration(files=[preprocessor_config]) + + configuration = {"GPU": configuration_obj} self.assertRaises( RuntimeError, finder.find, @@ -60,5 +65,6 @@ def test_ptx(self): ) + if __name__ == "__main__": unittest.main() diff --git a/tests/basic_fortran/test_basic_fortran.py b/tests/basic_fortran/test_basic_fortran.py index 29de5f44..617840f7 100644 --- a/tests/basic_fortran/test_basic_fortran.py +++ b/tests/basic_fortran/test_basic_fortran.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestBasicFortran(unittest.TestCase): @@ -26,23 +27,21 @@ def setUp(self): def test_yaml(self): """basic_fortran/basic_fortran.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "test.f90"), + defines=["CPU"], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "test.f90"), + defines=["GPU"], + include_paths=[], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "test.f90"), - "defines": ["CPU"], - "include_paths": [], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "test.f90"), - "defines": ["GPU"], - "include_paths": [], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/commented_directive/test_commented_directive.py b/tests/commented_directive/test_commented_directive.py index dbb62108..2f2f4d83 100644 --- a/tests/commented_directive/test_commented_directive.py +++ b/tests/commented_directive/test_commented_directive.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestCommentedDirective(unittest.TestCase): @@ -30,23 +31,21 @@ def count_children_nodes(self, node): def test_yaml(self): """commented_directive/commented_directive.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["CPU"], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["GPU"], + include_paths=[], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["CPU"], - "include_paths": [], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["GPU"], - "include_paths": [], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/compilers/test_compilers.py b/tests/compilers/test_compilers.py index 4fe5fbce..078c15ac 100644 --- a/tests/compilers/test_compilers.py +++ b/tests/compilers/test_compilers.py @@ -45,7 +45,7 @@ def test_common(self): ] parser = ArgumentParser(argv[0]) - passes = parser.parse_args(argv[1:]) + passes = parser.parse_args("test.cpp", argv[1:]) self.assertEqual(len(passes), 1) self.assertEqual(passes[0].pass_name, "default") @@ -63,10 +63,9 @@ def test_common(self): def test_gnu(self): """compilers/gnu""" argv = ["g++", "-fopenmp", "test.cpp"] - parser = ArgumentParser(argv[0]) - passes = parser.parse_args(argv[1:]) + passes = parser.parse_args("test.cpp", argv[1:]) self.assertEqual(len(passes), 1) self.assertEqual(passes[0].pass_name, "default") @@ -77,10 +76,9 @@ def test_gnu(self): def test_clang(self): """compilers/clang""" argv = ["clang", "-fsycl-is-device", "test.cpp"] - parser = ArgumentParser(argv[0]) - passes = parser.parse_args(argv[1:]) + passes = parser.parse_args("test.cpp", argv[1:]) self.assertEqual(len(passes), 1) self.assertEqual(passes[0].pass_name, "default") @@ -94,7 +92,7 @@ def test_intel_sycl(self): parser = ArgumentParser(argv[0]) - passes = parser.parse_args(argv[1:]) + passes = parser.parse_args("test.cpp", argv[1:]) self.assertEqual(len(passes), 2) pass_names = {p.pass_name for p in passes} @@ -124,7 +122,7 @@ def test_intel_targets(self): parser = ArgumentParser(argv[0]) - passes = parser.parse_args(argv[1:]) + passes = parser.parse_args("test.cpp", argv[1:]) pass_names = {p.pass_name for p in passes} self.assertCountEqual( @@ -170,7 +168,7 @@ def test_nvcc(self): parser = ArgumentParser(argv[0]) - passes = parser.parse_args(argv[1:]) + passes = parser.parse_args("test.cpp", argv[1:]) pass_names = {p.pass_name for p in passes} self.assertCountEqual(pass_names, {"default", "sm_70", "sm_75"}) @@ -202,7 +200,7 @@ def test_user_options(self): ] parser = ArgumentParser(argv[0]) - passes = parser.parse_args(argv[1:]) + passes = parser.parse_args("test.cpp", argv[1:]) self.assertEqual(len(passes), 1) self.assertCountEqual(passes[0].defines, ["ASDF"]) @@ -220,7 +218,7 @@ def test_user_alias(self): config._load_compilers() parser = ArgumentParser("c++") - passes = parser.parse_args(["-fopenmp", "test.cpp"]) + passes = parser.parse_args("test.cpp", ["-fopenmp", "test.cpp"]) self.assertEqual(len(passes), 1) self.assertCountEqual(passes[0].defines, ["_OPENMP"]) @@ -239,7 +237,7 @@ def test_nested_aliases(self): config._load_compilers() parser = ArgumentParser("c++") - passes = parser.parse_args(["-fopenmp", "test.cpp"]) + passes = parser.parse_args("test.cpp", ["-fopenmp", "test.cpp"]) self.assertEqual(len(passes), 1) self.assertCountEqual(passes[0].defines, ["_OPENMP"]) @@ -288,7 +286,7 @@ def test_user_parser(self): config._load_compilers() parser = ArgumentParser("c++") - passes = parser.parse_args(["-fasdf", "test.cpp"]) + passes = parser.parse_args("test.cpp", ["-fasdf", "test.cpp"]) self.assertEqual(len(passes), 2) pass_names = {p.pass_name for p in passes} @@ -369,7 +367,7 @@ def test_user_overrides(self): # Verify that user-defined overrides affect passes, etc. parser = ArgumentParser("g++") argv = ["-fopenmp"] - passes = parser.parse_args(argv) + passes = parser.parse_args("test.cpp", argv) pass_names = {p.pass_name for p in passes} self.assertCountEqual(pass_names, {"default", "new-openmp-pass"}) @@ -418,6 +416,7 @@ def test_unrecognized(self): logging.disable(logging.NOTSET) with self.assertLogs("codebasin", level=logging.WARNING) as cm: _ = ArgumentParser("foo").parse_args( + "test.cpp", ["-pass", "-mode", "-unrecognized"], ) logging.disable() diff --git a/tests/define/test_define.py b/tests/define/test_define.py index 5581ed76..7c9939da 100644 --- a/tests/define/test_define.py +++ b/tests/define/test_define.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestDefine(unittest.TestCase): @@ -25,23 +26,21 @@ def setUp(self): def test_yaml(self): """define/define.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["CPU"], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["GPU"], + include_paths=[], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["CPU"], - "include_paths": [], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["GPU"], - "include_paths": [], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/disjoint/test_disjoint.py b/tests/disjoint/test_disjoint.py index 0b7ecbec..5fd9055b 100644 --- a/tests/disjoint/test_disjoint.py +++ b/tests/disjoint/test_disjoint.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestDisjointCodebase(unittest.TestCase): @@ -24,23 +25,21 @@ def setUp(self): def test_yaml(self): """disjoint/disjoint.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "cpu.cpp"), + defines=["CPU"], + include_paths=[str(self.rootdir / "cpu_headers")], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "gpu.cpp"), + defines=["GPU"], + include_paths=[str(self.rootdir / "gpu_headers")], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "cpu.cpp"), - "defines": ["CPU"], - "include_paths": [str(self.rootdir / "cpu_headers")], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "gpu.cpp"), - "defines": ["GPU"], - "include_paths": [str(self.rootdir / "gpu_headers")], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/duplicates/test_duplicates.py b/tests/duplicates/test_duplicates.py index 62550aad..b056e932 100644 --- a/tests/duplicates/test_duplicates.py +++ b/tests/duplicates/test_duplicates.py @@ -8,6 +8,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration from codebasin.report import find_duplicates @@ -28,23 +29,21 @@ def test_duplicates(self): codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=cpufile, + defines=[], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=gpufile, + defines=[], + include_paths=[], + include_files=[], + ) configuration = { - "cpu": [ - { - "file": cpufile, - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], - "gpu": [ - { - "file": gpufile, - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], + "cpu": Configuration(files=[cpu_config]), + "gpu": Configuration(files=[gpu_config]), } expected_setmap = {frozenset(["cpu"]): 1, frozenset(["gpu"]): 1} @@ -61,23 +60,21 @@ def test_symlink_directories(self): codebase = CodeBase(self.rootdir, exclude_patterns=["gpu/"]) + cpu_config = PreprocessorConfiguration( + file=cpufile, + defines=[], + include_paths=[], + include_files=[], + ) + cpu2_config = PreprocessorConfiguration( + file=cpu2file, + defines=[], + include_paths=[], + include_files=[], + ) configuration = { - "cpu": [ - { - "file": cpufile, - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], - "cpu2": [ - { - "file": cpu2file, - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], + "cpu": Configuration(files=[cpu_config]), + "cpu2": Configuration(files=[cpu2_config]), } expected_setmap = {frozenset(["cpu", "cpu2"]): 1} @@ -95,21 +92,20 @@ def test_symlink_files(self): os.symlink(p / "base.cpp", p / "symlink.cpp") codebase = CodeBase(p) + base_config = PreprocessorConfiguration( + file=str(p / "base.cpp"), + defines=[], + include_paths=[], + include_files=[], + ) + symlink_config = PreprocessorConfiguration( + file=str(p / "symlink.cpp"), + defines=[], + include_paths=[], + include_files=[], + ) configuration = { - "test": [ - { - "file": str(p / "base.cpp"), - "defines": [], - "include_paths": [], - "include_files": [], - }, - { - "file": str(p / "symlink.cpp"), - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], + "test": Configuration(files=[base_config, symlink_config]), } expected_setmap = {frozenset(["test"]): 1} diff --git a/tests/files/test_filetree.py b/tests/files/test_filetree.py index 2583a246..fd9d96cb 100644 --- a/tests/files/test_filetree.py +++ b/tests/files/test_filetree.py @@ -9,6 +9,7 @@ from pathlib import Path from codebasin import CodeBase, finder, report +from codebasin.config import Configuration, PreprocessorConfiguration from codebasin.report import FileTree @@ -103,23 +104,21 @@ def test_report(self): """Check report output is accurate""" stream = io.StringIO() codebase = CodeBase(self.path) + x_file_config = PreprocessorConfiguration( + file=str(self.path / "file.cpp"), + defines=[], + include_paths=[], + include_files=[], + ) + y_file_config = PreprocessorConfiguration( + file=str(self.path / "other.cpp"), + defines=[], + include_paths=[], + include_files=[], + ) configuration = { - "X": [ - { - "file": str(self.path / "file.cpp"), - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], - "Y": [ - { - "file": str(self.path / "other.cpp"), - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], + "X": Configuration(files=[x_file_config]), + "Y": Configuration(files=[y_file_config]), } with open(self.path / "file.cpp", mode="w") as f: f.write("void foo();") @@ -162,23 +161,21 @@ def test_levels(self): open(path / "first" / "second" / "two.cpp", mode="w").close() codebase = CodeBase(path) + x_file_config = PreprocessorConfiguration( + file=str(path / "first" / "one.cpp"), + defines=[], + include_paths=[], + include_files=[], + ) + y_file_config = PreprocessorConfiguration( + file=str(path / "first" / "second" / "two.cpp"), + defines=[], + include_paths=[], + include_files=[], + ) configuration = { - "X": [ - { - "file": str(path / "first" / "one.cpp"), - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], - "Y": [ - { - "file": str(path / "first" / "second" / "two.cpp"), - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], + "X": Configuration(files=[x_file_config]), + "Y": Configuration(files=[y_file_config]), } state = finder.find( path, @@ -228,15 +225,14 @@ def test_prune(self): open(path / "unused.cpp", mode="w").close() codebase = CodeBase(path) + x_file_config = PreprocessorConfiguration( + file=str(path / "foo.cpp"), + defines=[], + include_paths=[], + include_files=[], + ) configuration = { - "X": [ - { - "file": str(path / "foo.cpp"), - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], + "X": Configuration(files=[x_file_config]), } state = finder.find( path, diff --git a/tests/include/test_include.py b/tests/include/test_include.py index 05f76068..06bac83a 100644 --- a/tests/include/test_include.py +++ b/tests/include/test_include.py @@ -8,6 +8,7 @@ from pathlib import Path from codebasin import CodeBase, config, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestInclude(unittest.TestCase): @@ -21,9 +22,10 @@ def setUp(self): logging.disable() self.expected_setmap = { - frozenset(["CPU"]): 11, - frozenset(["GPU"]): 12, - frozenset(["CPU", "GPU"]): 16, + frozenset(): 14, + frozenset(["CPU"]): 7, + frozenset(["GPU"]): 8, + frozenset(["CPU", "GPU"]): 10, } def test_include(self): @@ -55,15 +57,14 @@ def test_include_from_symlink(self): os.symlink(p / "test.cpp", p / "symlink.cpp") codebase = CodeBase(p) + symlink_file_config = PreprocessorConfiguration( + file=str(p / "symlink.cpp"), + defines=[], + include_paths=[], + include_files=[], + ) configuration = { - "test": [ - { - "file": str(p / "symlink.cpp"), - "defines": [], - "include_paths": [], - "include_files": [], - }, - ], + "test": Configuration(files=[symlink_file_config]), } _ = finder.find(self.rootdir, codebase, configuration) diff --git a/tests/literals/test_literals.py b/tests/literals/test_literals.py index 991ee461..0262959a 100644 --- a/tests/literals/test_literals.py +++ b/tests/literals/test_literals.py @@ -8,6 +8,7 @@ import numpy as np from codebasin import CodeBase, finder, preprocessor +from codebasin.config import Configuration, PreprocessorConfiguration class TestLiterals(unittest.TestCase): @@ -25,23 +26,21 @@ def setUp(self): def test_literals(self): """literals/literals.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["USE_CPU"], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["USE_GPU"], + include_paths=[], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["USE_CPU"], - "include_paths": [], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["USE_GPU"], - "include_paths": [], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/macro_expansion/test_macro_expansion.py b/tests/macro_expansion/test_macro_expansion.py index 7b8a1b32..961db765 100644 --- a/tests/macro_expansion/test_macro_expansion.py +++ b/tests/macro_expansion/test_macro_expansion.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder, preprocessor +from codebasin.config import Configuration, PreprocessorConfiguration from codebasin.preprocessor import Platform @@ -28,28 +29,28 @@ def setUp(self): def test_macro_expansion(self): """macro_expansion/macro_expansion.yaml""" codebase = CodeBase(self.rootdir) - cpu_entries = [] - gpu_entries = [] + cpu_file_configs = [] + gpu_file_configs = [] for f in list(codebase): - cpu_entries.append( - { - "file": str(f), - "defines": ["CPU"], - "include_paths": [], - "include_files": [], - }, + cpu_file_configs.append( + PreprocessorConfiguration( + file=str(f), + defines=["CPU"], + include_paths=[], + include_files=[], + ) ) - gpu_entries.append( - { - "file": str(f), - "defines": ["GPU"], - "include_paths": [], - "include_files": [], - }, + gpu_file_configs.append( + PreprocessorConfiguration( + file=str(f), + defines=["GPU"], + include_paths=[], + include_files=[], + ) ) configuration = { - "CPU": cpu_entries, - "GPU": gpu_entries, + "CPU": Configuration(files=cpu_file_configs), + "GPU": Configuration(files=gpu_file_configs), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/multi_line/test_multi_line.py b/tests/multi_line/test_multi_line.py index 76a3b534..4f520feb 100644 --- a/tests/multi_line/test_multi_line.py +++ b/tests/multi_line/test_multi_line.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestMultiLine(unittest.TestCase): @@ -25,23 +26,21 @@ def setUp(self): def test_yaml(self): """multi_line/multi_line.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["CPU"], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["GPU"], + include_paths=[], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["CPU"], - "include_paths": [], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["GPU"], - "include_paths": [], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/nesting/test_nesting.py b/tests/nesting/test_nesting.py index 32ae02a8..b7e12796 100644 --- a/tests/nesting/test_nesting.py +++ b/tests/nesting/test_nesting.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestNesting(unittest.TestCase): @@ -26,23 +27,21 @@ def setUp(self): def test_yaml(self): """nesting/nesting.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["CPU"], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["GPU"], + include_paths=[], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["CPU"], - "include_paths": [], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["GPU"], - "include_paths": [], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/once/test_once.py b/tests/once/test_once.py index c8e960b6..d723e66b 100644 --- a/tests/once/test_once.py +++ b/tests/once/test_once.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder +from codebasin.config import Configuration, PreprocessorConfiguration class TestOnce(unittest.TestCase): @@ -25,23 +26,21 @@ def setUp(self): def test_yaml(self): """once/once.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["CPU"], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["GPU"], + include_paths=[], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["CPU"], - "include_paths": [], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["GPU"], - "include_paths": [], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase) diff --git a/tests/operators/test_operators.py b/tests/operators/test_operators.py index 59aa0081..2c30cef0 100644 --- a/tests/operators/test_operators.py +++ b/tests/operators/test_operators.py @@ -6,6 +6,7 @@ from pathlib import Path from codebasin import CodeBase, finder, preprocessor +from codebasin.config import Configuration, PreprocessorConfiguration from codebasin.preprocessor import Platform @@ -24,23 +25,21 @@ def setUp(self): def test_operators(self): """operators/operators.yaml""" codebase = CodeBase(self.rootdir) + cpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["CPU"], + include_paths=[], + include_files=[], + ) + gpu_config = PreprocessorConfiguration( + file=str(self.rootdir / "main.cpp"), + defines=["GPU"], + include_paths=[], + include_files=[], + ) configuration = { - "CPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["CPU"], - "include_paths": [], - "include_files": [], - }, - ], - "GPU": [ - { - "file": str(self.rootdir / "main.cpp"), - "defines": ["GPU"], - "include_paths": [], - "include_files": [], - }, - ], + "CPU": Configuration(files=[cpu_config]), + "GPU": Configuration(files=[gpu_config]), } state = finder.find(self.rootdir, codebase, configuration) setmap = state.get_setmap(codebase)