Skip to content

Commit

Permalink
Add silent replace option to linker.add_lib_flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Hoffmann committed Aug 30, 2024
1 parent 390a2fb commit d8c9db1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
11 changes: 10 additions & 1 deletion source/fab/tools/linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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[:]

Expand Down
27 changes: 25 additions & 2 deletions tests/unit_tests/tools/test_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pathlib import Path
from unittest import mock
import warnings

import pytest

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit d8c9db1

Please sign in to comment.