From 68fcb6fffb39848d0baef95522a5cd02e984f671 Mon Sep 17 00:00:00 2001 From: Joerg Henrichs Date: Tue, 18 Nov 2025 15:19:14 +1100 Subject: [PATCH 1/2] #525 Add code and test to recognise a Fortran compiler wrapper to be Fortran when searching for a default linker. --- source/fab/tools/tool_repository.py | 3 +++ tests/unit_tests/tools/test_tool_repository.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/source/fab/tools/tool_repository.py b/source/fab/tools/tool_repository.py index ed23cab1..a7ed8674 100644 --- a/source/fab/tools/tool_repository.py +++ b/source/fab/tools/tool_repository.py @@ -290,6 +290,9 @@ def get_default(self, category: Category, if category == Category.LINKER: tool = cast(Linker, tool) compiler = tool.compiler + # Find the real compiler if we have a compiler wrapper: + while isinstance(compiler, CompilerWrapper): + compiler = compiler.compiler # Ignore C linker if Fortran is requested and vice versa: if (enforce_fortran_linker and not isinstance(compiler, FortranCompiler)): diff --git a/tests/unit_tests/tools/test_tool_repository.py b/tests/unit_tests/tools/test_tool_repository.py index 3e4a803c..c80efc5a 100644 --- a/tests/unit_tests/tools/test_tool_repository.py +++ b/tests/unit_tests/tools/test_tool_repository.py @@ -16,6 +16,7 @@ from fab.tools.category import Category from fab.tools.compiler import Compiler, FortranCompiler, Gfortran, Ifort from fab.tools.compiler_wrapper import Mpif90 +from fab.tools.linker import Linker from fab.tools.tool_repository import ToolRepository from tests.conftest import call_list @@ -124,6 +125,16 @@ def test_get_default(stub_tool_repository, stub_fortran_compiler, ar = stub_tool_repository.get_default(Category.AR) assert isinstance(ar, Ar) + # Now add a linker around a compiler wrapper, to make sure the compiler + # wrapper is recognised as a Fortran compiler: + linker = Linker(Mpif90(fc)) + linker._is_available = True + stub_tool_repository.add_tool(linker) + for_link = stub_tool_repository.get_default(Category.LINKER, mpi=True, + openmp=False, + enforce_fortran_linker=True) + assert for_link is linker + def test_get_default_error_invalid_category() -> None: """ From 7b8f511c35be8eaee9c084294ece7e0cf78429c7 Mon Sep 17 00:00:00 2001 From: Joerg Henrichs Date: Tue, 18 Nov 2025 15:46:11 +1100 Subject: [PATCH 2/2] #525 Added additional test to cover additional line. --- .../unit_tests/tools/test_tool_repository.py | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tests/unit_tests/tools/test_tool_repository.py b/tests/unit_tests/tools/test_tool_repository.py index c80efc5a..5fb42b5a 100644 --- a/tests/unit_tests/tools/test_tool_repository.py +++ b/tests/unit_tests/tools/test_tool_repository.py @@ -15,7 +15,7 @@ from fab.tools.ar import Ar from fab.tools.category import Category from fab.tools.compiler import Compiler, FortranCompiler, Gfortran, Ifort -from fab.tools.compiler_wrapper import Mpif90 +from fab.tools.compiler_wrapper import Mpicc, Mpif90 from fab.tools.linker import Linker from fab.tools.tool_repository import ToolRepository @@ -125,16 +125,33 @@ def test_get_default(stub_tool_repository, stub_fortran_compiler, ar = stub_tool_repository.get_default(Category.AR) assert isinstance(ar, Ar) - # Now add a linker around a compiler wrapper, to make sure the compiler + +def test_get_default_linker_with_wrapper(stub_tool_repository, + stub_fortran_compiler, + stub_c_compiler) -> None: + """ + Tests that we get the right linker if compiler wrapper are used. + """ + + # Add a linker around a compiler wrapper, to test that the compiler # wrapper is recognised as a Fortran compiler: - linker = Linker(Mpif90(fc)) + linker = Linker(Mpif90(stub_fortran_compiler)) linker._is_available = True stub_tool_repository.add_tool(linker) for_link = stub_tool_repository.get_default(Category.LINKER, mpi=True, - openmp=False, + openmp=True, enforce_fortran_linker=True) assert for_link is linker + # Now the same for a linker around a C compiler wrapper: + linker = Linker(Mpicc(stub_c_compiler)) + linker._is_available = True + stub_tool_repository.add_tool(linker) + cc_link = stub_tool_repository.get_default(Category.LINKER, mpi=True, + openmp=True, + enforce_fortran_linker=False) + assert cc_link is linker + def test_get_default_error_invalid_category() -> None: """ @@ -163,6 +180,11 @@ def test_get_default_error_missing_mpi() -> None: assert str(err.value) == ("Invalid or missing openmp specification " "for 'FORTRAN_COMPILER'.") + with raises(RuntimeError) as err: + tr.get_default(Category.LINKER, mpi=True, openmp=True) + assert str(err.value) == ("Invalid or missing enforce_fortran_linker " + "specification for 'LINKER'.") + def test_get_default_error_missing_openmp() -> None: """