Skip to content

Commit

Permalink
Change how asan libraries are specified to be more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
langmm committed Apr 19, 2024
1 parent 899a427 commit 7cedd06
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 702 deletions.
8 changes: 2 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,12 +1484,8 @@ def init_zmq():
@pytest.fixture(scope="session")
def asan_installed():
r"""Determine if ASAN is available."""
from yggdrasil.drivers.CompiledModelDriver import (
find_compilation_tool, get_compilation_tool)
compiler = find_compilation_tool('compiler', 'c', allow_failure=True)
if compiler:
compiler = get_compilation_tool('compiler', compiler)
return compiler and compiler.asan_library()
from yggdrasil.drivers.CModelDriver import CModelDriver
return CModelDriver.is_library_installed('asan')


@pytest.fixture
Expand Down
50 changes: 1 addition & 49 deletions tests/drivers/test_CompiledModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,53 +63,6 @@ def test_get_compilation_tool():
== 'invalid')


@pytest.mark.language('c')
def test_locate_library_file():
r"""Test locate_file method for compiler."""
from yggdrasil.drivers.CModelDriver import CModelDriver
compiler = CModelDriver.get_tool('compiler').__class__
files = {}
for k in ['shared', 'static']:
fname = CModelDriver.libraries['zmq'].get(k, '', compiler=compiler)
if os.path.isfile(fname):
files[k] = fname
if not files:
pytest.skip("Test library (zmq) doesn't exist")
for libtype, fname in files.items():
assert compiler.locate_file(fname) == fname
assert compiler.locate_file('zmq', libtype=libtype) == fname
if libtype == 'static':
assert compiler.archiver().locate_file('zmq') == fname
if libtype == 'shared':
assert compiler.linker().locate_file('zmq') == fname
if platform._is_win and 'static' in files:
assert (compiler.locate_file('zmq', libtype='windows_import')
== files['static'])


@pytest.mark.parametrize('lang,toolname,lib', [
('fortran', 'gfortran', True),
('c++', 'clang++', True),
('c++', 'g++', True),
('c++', 'cl++', False),
])
def test_find_standard_library(lang, toolname, lib):
r"""Test find_standard_library"""
compiler = CompiledModelDriver.get_compilation_tool(
'compiler', toolname, allow_failure=True,
return_type='class', init_languages=[lang])
if not (compiler and compiler.is_installed()
and compiler.disassembler(allow_uninstalled=True).is_installed()):
pytest.skip(f"No compiler for {toolname}")
out = compiler.find_standard_library(dont_cache=True)
if lib:
if out is None:
out = compiler.find_standard_library(verbose=True)
assert isinstance(out, str) and os.path.isfile(out)
else:
assert out is None


def test_create_windows_import_gcc():
r"""Test create_windows_import for GNU"""
from yggdrasil.drivers.CModelDriver import CModelDriver
Expand Down Expand Up @@ -237,8 +190,7 @@ def test_get_flags(self, python_class):
from yggdrasil import __version__ as yggver
yggver = yggver.split('+')[0].split('v')[-1].split('.')
assert (python_class.get_flags(flags='hello', libtype='object')
== ['hello', '-DWITH_YGGDRASIL', '-D_USE_MATH_DEFINES',
f'-DYGGVER_MAJOR={yggver[0]}'])
== ['hello'])

def test_get_executable_command(self, python_class):
r"""Test get_executable_command."""
Expand Down
32 changes: 7 additions & 25 deletions yggdrasil/drivers/CModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,6 @@ def set_env(cls, *args, **kwargs):
out['MACOSX_DEPLOYMENT_TARGET'] = grp['target']
return out

# @classmethod
# def call(cls, args, **kwargs):
# r"""Call the compiler with the provided arguments. For |yggdrasil| C
# models will always be linked using the C++ linker since some parts of
# the interface library are written in C++."""
# if not kwargs.get('dont_link', False):
# kwargs.setdefault('linker_language', 'c++')
# return super(CCompilerBase, cls).call(args, **kwargs)

@classmethod
def get_search_path(cls, *args, **kwargs):
r"""Determine the paths searched by the tool for external library files.
Expand Down Expand Up @@ -162,10 +153,9 @@ class GCCCompiler(CCompilerBase):
is_linker = False
toolset = 'gnu'
aliases = ['gnu-cc', 'gnu-gcc']
asan_flags = ['-fsanitize=address']
libraries = {
'asan': {'executable_flags': ['-fsanitize=address'],
'library_flags': ['-fsanitize=address'],
'asan': {'dep_executable_flags': ['-fsanitize=address'],
'dep_shared_flags': ['-fsanitize=address'],
'preload': True,
'env': {'ASAN_OPTIONS': {
'value': 'verify_asan_link_order=0',
Expand Down Expand Up @@ -212,16 +202,15 @@ class ClangCompiler(CCompilerBase):
'prepend': True}),
('mmacosx-version-min',
'-mmacosx-version-min=%s')])
asan_flags = ['-fsanitize=address']
product_exts = ['.dSYM']
# Set to False since ClangLinker has its own class to handle
# conflict between versions of clang and ld.
is_linker = False
toolset = 'llvm'
libraries = {
'asan': {'executable_flags': ['-fsanitize=address'],
'library_flags': ['-fsanitize=address',
'-shared-libasan'],
'asan': {'dep_executable_flags': ['-fsanitize=address'],
'dep_shared_flags': ['-fsanitize=address',
'-shared-libasan'],
'preload': True,
'env': {'ASAN_OPTIONS': {
'value': 'verify_asan_link_order=0',
Expand Down Expand Up @@ -304,7 +293,6 @@ class LDLinker(LinkerBase):
default_flags_env = 'LDFLAGS'
version_flags = ['-v']
search_path_envvar = ['LIBRARY_PATH', 'LD_LIBRARY_PATH']
asan_flags = ['-fsanitize=address']

@classmethod
def tool_version(cls, **kwargs):
Expand Down Expand Up @@ -367,7 +355,6 @@ class ClangLinker(LDLinker):
**{'linker-version': '-mlinker-version=%s',
'library_rpath': '-rpath',
'library_libs_nonstd': ''})
asan_flags = ['-fsanitize=address']
preload_envvar = 'DYLD_INSERT_LIBRARIES'

@staticmethod
Expand Down Expand Up @@ -420,9 +407,6 @@ def get_flags(cls, *args, **kwargs):
# as existing installs still have this mismatch
kwargs['linker-version'] = ld_version
out = super(ClangLinker, cls).get_flags(*args, **kwargs)
if kwargs.get('with_asan', False):
if kwargs.get('build_library', False):
out.append('-shared-libasan')
if '-fopenmp' in out:
out[out.index('-fopenmp')] = '-lomp'
if 'conda' not in cls.get_executable(full_path=True):
Expand Down Expand Up @@ -486,7 +470,6 @@ class ARArchiver(ArchiverBase):
toolset = 'gnu'
compatible_toolsets = ['llvm']
search_path_envvar = ['LIBRARY_PATH']
asan_flags = []


class LibtoolArchiver(ArchiverBase):
Expand All @@ -497,7 +480,6 @@ class LibtoolArchiver(ArchiverBase):
static_library_flag = '-static' # This is the default
toolset = 'llvm'
search_path_envvar = ['LIBRARY_PATH']
asan_flags = []


class MSVCArchiver(ArchiverBase):
Expand Down Expand Up @@ -569,10 +551,10 @@ class CModelDriver(CompiledModelDriver):
'numpy': {'include': 'arrayobject.h',
'libtype': 'header_only',
'language': 'c',
'for_python_api': True},
'exclude_specialization': 'disable_python_c_api'},
'python': {'include': 'Python.h',
'language': 'c',
'for_python_api': True,
'exclude_specialization': 'disable_python_c_api',
'standard': True}}
internal_libraries = {
'ygg': {'source': 'YggInterface.c',
Expand Down
Loading

0 comments on commit 7cedd06

Please sign in to comment.