Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/mccode_antlr/translators/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,32 @@ def extract_declares(name, raw_c_obj):
self.instrument_uservars = declares
self.component_uservars = comp_declares

def _libc_registry(self):
candidates = [reg for reg in self.source.registries if 'libc' == reg.name]
return next(iter(candidates), None)

def _libc_before(self, version: str) -> bool:
from packaging.version import Version
libc_registry = self._libc_registry()
if libc_registry is None:
return False
return Version(libc_registry.version) < Version(version)

def __post_init__(self):
"""Before doing *anything* else. We need to search through all raw C code for %include statements.
This could not be done earlier if the goal is to have a clear separation between the parsing and target
languages. (A different target language would not include the same libraries in its raw blocks)
"""
# Make sure the registry list contains the C library registry, so that we can find and include files
from packaging.version import Version
from ..reader.registry import ordered_registries, ensure_registries
# make sure that the libc registry is present before sorting:
self.source.registries = tuple(ordered_registries(ensure_registries(Flavor.BASE, list(self.source.registries))))

# Check that the LIBC registry is not too old for current translation
for reg in self.source.registries:
# NeXus interface changed in v3.5.20 release of McStas/McXtrace == McCode
if 'libc' == reg.name and Version(reg.version) < Version("3.5.20"):
logger.warning(f"Requested McCode libc version {reg.version} may"
" cause errors since it is less than v3.5.20")
# NeXus interface changed in v3.5.20 release of McStas/McXtrace == McCode
if self._libc_before('3.5.20'):
logger.warning(f"Requested McCode libc version {self._libc_registry().version} may"
Comment thread
willend marked this conversation as resolved.
Resolved
" cause errors since it is less than v3.5.20")

includes = []
inst = self.source
Expand Down Expand Up @@ -311,6 +320,7 @@ def _instrument_and_component_uservars(self):
return uuv

def visit_header(self):
from mccode_antlr.config import config
from .c_header import header_pre_runtime, header_post_runtime
# Get the unique list of USERVAR declarations:
# logger.debug(f'Instrument uservars = {self.instrument_uservars}')
Expand All @@ -333,7 +343,17 @@ def visit_header(self):
self.embed_file("mccode-r.c")
self.embed_file(f'{name}-r.c')
if self.verbose:
logger.info("Compile with flags '-DUSE_NEXUS -lNeXus' to enable NeXus support")
flags = config['flags']['nexus'].as_str_expanded()
# libNeXus for runtimes before mccode-dev/McCode#2895, HDF5 after; see #405.
# Match on the library name only (not e.g. '-lhdf5' or 'hdf5.lib') since the
# linker-flag syntax differs per platform/compiler (gcc/clang vs MSVC).
expect_hdf5 = not self._libc_before('3.10.1')
library = 'hdf5' if expect_hdf5 else 'nexus'
if library not in flags.lower():
required = 'HDF5' if expect_hdf5 else 'NeXus'
logger.warning(f"McCode library code changed after v3.10.0 to require linking against "
f"{required}, but configured NeXus flags ('{flags}') do not mention it")
logger.info(f"Compile with flags '{flags}' to enable NeXus support")
else:
# This only works if the module is *not* a compressed archive
# If it is, we would need to do some trickery to ... write out the
Expand Down
107 changes: 107 additions & 0 deletions tests/test_nexus_version_hint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""The --verbose NeXus compile hint must warn when the configured flags name
the wrong library for the in-use McCode libc: NeXus before mccode-dev/McCode#2895,
HDF5 after (the change the libc made at v3.10.1), regardless of whether the
flags use gcc/clang '-l' syntax or MSVC '.lib' filenames. See #405.
"""
import pytest

from mccode_antlr.translators.c import CTargetVisitor


class FakeRegistry:
def __init__(self, name, version):
self.name = name
self.version = version


class FakeSource:
def __init__(self, registries):
self.registries = registries


def visitor_with_registries(*registries):
visitor = CTargetVisitor.__new__(CTargetVisitor)
visitor.source = FakeSource(list(registries))
return visitor


@pytest.mark.parametrize('version, boundary, expected', [
('3.10.0', '3.10.1', True),
('3.10.1', '3.10.1', False),
('3.11.0', '3.10.1', False),
('3.5.19', '3.5.20', True),
('3.5.20', '3.5.20', False),
])
def test_libc_before_compares_the_libc_registrys_own_version(version, boundary, expected):
visitor = visitor_with_registries(FakeRegistry('libc', version), FakeRegistry('mcstas', '9.9.9'))
assert visitor._libc_before(boundary) is expected


def test_libc_before_is_false_when_no_libc_registry_is_present():
visitor = visitor_with_registries(FakeRegistry('mcstas', '9.9.9'))
assert visitor._libc_before('3.10.1') is False


INSTR = """DEFINE INSTRUMENT quiet(dummy=0)
TRACE
COMPONENT origin = Arm() AT (0, 0, 0) ABSOLUTE
END
"""


def build_with_libc_version(libc_version, caplog):
from mccode_antlr import Flavor
from mccode_antlr.loader import parse_mcstas_instr

caplog.set_level('INFO', logger='mccode_antlr')
instr = parse_mcstas_instr(INSTR)
for reg in instr.registries:
if reg.name == 'libc':
reg.version = libc_version
config = dict(default_main=True, enable_trace=False, portable=False,
include_runtime=True, embed_instrument_file=False)
CTargetVisitor(instr, flavor=Flavor.MCSTAS, config=config, verbose=True).contents()


def run_with_flags(nexus_flags, libc_version, caplog):
from mccode_antlr.config import config

config.set({'flags': {'nexus': nexus_flags}})
try:
build_with_libc_version(libc_version, caplog)
finally:
config.sources.pop(0)
# ignore unrelated warnings, e.g. registry.py's "specified more than once" from
# overwriting the real libc registry's version for this test
return [r.getMessage() for r in caplog.records
if r.levelname == 'WARNING' and r.name == 'mccode_antlr.translators.c']


# gcc/clang (Linux, macOS) spell the library as a '-l' linker flag; MSVC (Windows)
# names the .lib file directly -- the hint must not assume either syntax, only that
# the library's name ('NeXus' or 'hdf5') appears somewhere in the configured flags.
@pytest.mark.parametrize('nexus_flags', [
'-DUSE_NEXUS -lNeXus', # Linux/macOS, pre-#2895 McCode
'/DUSE_NEXUS NeXus.lib', # Windows, pre-#2895 McCode
])
def test_hint_warns_when_post_2895_libc_is_paired_with_nexus_flags(nexus_flags, caplog):
warnings = run_with_flags(nexus_flags, '3.10.1', caplog)
assert any('HDF5' in m for m in warnings), warnings


@pytest.mark.parametrize('hdf5_flags', [
'-DUSE_NEXUS -lhdf5', # Linux/macOS, post-#2895 McCode
'/DUSE_NEXUS hdf5.lib', # Windows, post-#2895 McCode
])
def test_hint_is_quiet_when_post_2895_libc_is_paired_with_hdf5_flags(hdf5_flags, caplog):
warnings = run_with_flags(hdf5_flags, '3.10.1', caplog)
assert warnings == []


@pytest.mark.parametrize('nexus_flags', [
'-DUSE_NEXUS -lNeXus', # Linux/macOS, pre-#2895 McCode
'/DUSE_NEXUS NeXus.lib', # Windows, pre-#2895 McCode
])
def test_hint_is_quiet_when_pre_2895_libc_is_paired_with_nexus_flags(nexus_flags, caplog):
warnings = run_with_flags(nexus_flags, '3.10.0', caplog)
assert warnings == []
Loading