Skip to content

Commit

Permalink
Added missing aliases for clang linker
Browse files Browse the repository at this point in the history
Handle case where gcc/g++ is actually clang
Catch tool errors in CompiledModelDriver and raise NotImplementedError to match ModelDriver
  • Loading branch information
langmm committed May 23, 2024
1 parent c9cf1cd commit fca2f7b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
4 changes: 4 additions & 0 deletions tests/drivers/test_ModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def test_ModelDriver_implementation():
ModelDriver.is_library_installed(None)
with pytest.raises(InvalidCompilationTool):
CompiledModelDriver.get_tool('compiler')
with pytest.raises(NotImplementedError):
CompiledModelDriver.language_executable()
with pytest.raises(NotImplementedError):
CompiledModelDriver.executable_command(None)
with pytest.raises(NotImplementedError):
InterpretedModelDriver.get_interpreter()

Expand Down
39 changes: 33 additions & 6 deletions yggdrasil/drivers/CModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from yggdrasil import platform, tools, constants, rapidjson
from yggdrasil.drivers.CompiledModelDriver import (
CompiledModelDriver, CompilerBase, LinkerBase, ArchiverBase,
_osx_sysroot)
_osx_sysroot, InvalidCompilationTool)
from yggdrasil.languages import get_language_dir
logger = logging.getLogger(__name__)
if platform._is_win:
Expand Down Expand Up @@ -103,6 +103,22 @@ class GCCCompiler(CCompilerBase):
'specialization': 'with_asan'},
}

@classmethod
def is_clang(cls):
r"""Determine if this tool is actually an alias for clang.
Returns:
bool: True if gcc actually points to clang.
"""
if platform._is_mac:
try:
ver = cls.tool_version()
return ('clang' in ver)
except InvalidCompilationTool:
pass
return False

@classmethod
def is_installed(cls):
r"""Determine if this tool is installed by looking for the executable.
Expand All @@ -113,10 +129,8 @@ def is_installed(cls):
"""
out = super(GCCCompiler, cls).is_installed()
# Disable gcc when it is an alias for clang
if out and platform._is_mac: # pragma: debug
ver = cls.tool_version()
if 'clang' in ver:
out = False
if out and cls.is_clang(): # pragma: debug
out = False
return out

@classmethod
Expand Down Expand Up @@ -160,6 +174,16 @@ class ClangCompiler(CCompilerBase):
'specialization': 'with_asan'},
}

@staticmethod
def before_registration(cls):
r"""Operations that should be performed to modify class attributes prior
to registration including things like platform dependent properties and
checking environment variables for default settings.
"""
if GCCCompiler.is_clang() and 'gcc' not in cls.aliases:
cls.aliases.append('gcc')
CCompilerBase.before_registration(cls)

@classmethod
def get_flags(cls, *args, **kwargs):
r"""Get a list of compiler flags."""
Expand Down Expand Up @@ -255,7 +279,10 @@ class GCCLinker(LDLinker):
class ClangLinker(LDLinker):
r"""Interface class for clang linker (calls to ld)."""
toolname = ClangCompiler.toolname
aliases = ClangCompiler.aliases
aliases = ClangCompiler.aliases + (
[os.path.basename(ClangCompiler.default_executable).replace(
ClangCompiler.toolname, 'ld')]
if ClangCompiler.default_executable else [])
languages = ClangCompiler.languages
platforms = ClangCompiler.platforms
default_executable = ClangCompiler.default_executable
Expand Down
8 changes: 7 additions & 1 deletion yggdrasil/drivers/CPPModelDriver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import logging
from yggdrasil import platform
from yggdrasil.drivers.CModelDriver import (
Expand Down Expand Up @@ -95,6 +96,8 @@ def before_registration(cls):
"""
if platform._is_win: # pragma: windows
cls.default_executable = 'clang'
if GPPCompiler.is_clang() and 'g++' not in cls.aliases:
cls.aliases.append('g++')
CPPCompilerBase.before_registration(cls)

@classmethod
Expand Down Expand Up @@ -174,7 +177,10 @@ class GPPLinker(GCCLinker):
class ClangPPLinker(ClangLinker):
r"""Interface class for clang++ linker (calls to ld)."""
toolname = ClangPPCompiler.toolname
aliases = ClangPPCompiler.aliases
aliases = ClangPPCompiler.aliases + (
[os.path.basename(ClangCompiler.default_executable).replace(
ClangCompiler.toolname, 'ld')]
if ClangCompiler.default_executable else [])
languages = ClangPPCompiler.languages
default_executable = ClangPPCompiler.default_executable
toolset = ClangPPCompiler.toolset
Expand Down
28 changes: 17 additions & 11 deletions yggdrasil/drivers/CompiledModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6479,7 +6479,10 @@ def language_executable(cls, toolname=None):
to run the compiler/interpreter from the command line.
"""
return cls.get_tool('basetool', toolname=toolname).get_executable()
try:
return cls.get_tool('basetool', toolname=toolname).get_executable()
except InvalidCompilationTool as e:
raise NotImplementedError(e)

@classmethod
def language_version(cls, toolname=None, **kwargs):
Expand Down Expand Up @@ -6541,16 +6544,19 @@ def executable_command(cls, args, exec_type='compiler', toolname=None,
ValueError: If exec_type is not 'compiler', 'linker', or 'direct'.
"""
if exec_type == 'direct':
unused_kwargs = kwargs.pop('unused_kwargs', {})
unused_kwargs.update(kwargs)
return args
elif exec_type == 'linker':
exec_cls = cls.get_tool('linker', toolname=toolname)
elif exec_type == 'compiler':
exec_cls = cls.get_tool('compiler', toolname=toolname)
else:
raise ValueError("Invalid exec_type '%s'" % exec_type)
try:
if exec_type == 'direct':
unused_kwargs = kwargs.pop('unused_kwargs', {})
unused_kwargs.update(kwargs)
return args
elif exec_type == 'linker':
exec_cls = cls.get_tool('linker', toolname=toolname)
elif exec_type == 'compiler':
exec_cls = cls.get_tool('compiler', toolname=toolname)
else:
raise ValueError("Invalid exec_type '%s'" % exec_type)
except InvalidCompilationTool as e:
raise NotImplementedError(e)
return exec_cls.get_executable_command(args, **kwargs)

@classmethod
Expand Down

0 comments on commit fca2f7b

Please sign in to comment.