From d8c9db19fb68f9b720c407afe8933c65f2ea23d6 Mon Sep 17 00:00:00 2001 From: Luke Hoffmann Date: Fri, 30 Aug 2024 17:32:40 +1000 Subject: [PATCH] Add silent replace option to linker.add_lib_flags --- source/fab/tools/linker.py | 11 ++++++++++- tests/unit_tests/tools/test_linker.py | 27 +++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/source/fab/tools/linker.py b/source/fab/tools/linker.py index 5b8b474b..1e96b405 100644 --- a/source/fab/tools/linker.py +++ b/source/fab/tools/linker.py @@ -10,6 +10,7 @@ import os from pathlib import Path from typing import cast, Dict, List, Optional +import warnings from fab.tools.category import Category from fab.tools.compiler import Compiler @@ -83,12 +84,20 @@ def get_lib_flags(self, lib: str) -> List[str]: except KeyError: raise RuntimeError(f"Unknown library name: '{lib}'") - def add_lib_flags(self, lib: str, flags: List[str]): + def add_lib_flags(self, lib: str, flags: List[str], + silent_replace: bool = False): '''Add a set of flags for a standard library :param lib: the library name :param flags: the flags to use with the library + :param silent_replace: if set, no warning will be printed when an + existing lib is overwritten. ''' + if lib in self._lib_flags and not silent_replace: + warnings.warn(f"Replacing existing flags for library {lib}: " + f"'{self._lib_flags[lib]}' with " + f"'{flags}'.") + # Make a copy to avoid modifying the caller's list self._lib_flags[lib] = flags[:] diff --git a/tests/unit_tests/tools/test_linker.py b/tests/unit_tests/tools/test_linker.py index 1e38d797..97c8a6a5 100644 --- a/tests/unit_tests/tools/test_linker.py +++ b/tests/unit_tests/tools/test_linker.py @@ -9,6 +9,7 @@ from pathlib import Path from unittest import mock +import warnings import pytest @@ -115,7 +116,7 @@ def test_linker_add_lib_flags(mock_c_compiler): assert result == ["-L", "xios/lib", "-I", "xios/inc"] -def test_linker_add_lib_flags_overwrite(mock_c_compiler): +def test_linker_add_lib_flags_overwrite_defaults(mock_c_compiler): """Linker should provide a way to replace the default flags for a library""" linker = Linker(compiler=mock_c_compiler) @@ -124,13 +125,35 @@ def test_linker_add_lib_flags_overwrite(mock_c_compiler): assert result == ["$(nf-config --flibs)", "($nc-config --libs)"] # Replace them with another set of flags. - linker.add_lib_flags("netcdf", ["-L", "netcdf/lib", "-I", "netcdf/inc"]) + warn_message = 'Replacing existing flags for library netcdf' + with pytest.warns(UserWarning, match=warn_message): + linker.add_lib_flags("netcdf", ["-L", "netcdf/lib", "-I", "netcdf/inc"]) # Test that we can see our custom flags result = linker.get_lib_flags("netcdf") assert result == ["-L", "netcdf/lib", "-I", "netcdf/inc"] +def test_linker_add_lib_flags_overwrite_silent(mock_c_compiler): + """Linker should provide the option to replace flags for a library without + generating a warning + """ + linker = Linker(compiler=mock_c_compiler) + + # Initially we have the default netcdf flags + linker.add_lib_flags("customlib", ["-q", "/tmp", "-j"]) + assert linker.get_lib_flags("customlib") == ["-q", "/tmp", "-j"] + + # Replace them with another set of flags. + with warnings.catch_warnings(): + warnings.simplefilter("error") + linker.add_lib_flags("customlib", ["-t", "-b"], silent_replace=True) + + # Test that we can see our custom flags + result = linker.get_lib_flags("customlib") + assert result == ["-t", "-b"] + + def test_linker_remove_lib_flags(mock_c_compiler): """Linker should provide a way to remove the flags for a library""" linker = Linker(compiler=mock_c_compiler)