Skip to content

Commit

Permalink
[tests] Better GCC fixture enumeration.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisCummins committed Mar 7, 2022
1 parent 839876d commit 7a8f5a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
28 changes: 15 additions & 13 deletions examples/gcc_autotuning/tune_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import shutil
import subprocess
import sys
from functools import lru_cache
Expand All @@ -24,33 +25,34 @@ def docker_is_available() -> bool:
return False


@lru_cache(maxsize=2)
def system_gcc_is_available() -> bool:
def system_has_functional_gcc(gcc_path: str) -> bool:
"""Return whether there is a system GCC available."""
try:
stdout = subprocess.check_output(
["gcc", "--version"], universal_newlines=True, stderr=subprocess.DEVNULL
[gcc_path, "--version"],
universal_newlines=True,
stderr=subprocess.DEVNULL,
timeout=30,
)
# On some systems "gcc" may alias to a different compiler, so check for
# the presence of the name "gcc" in the first line of output.
return "gcc" in stdout.split("\n")[0].lower()
except (subprocess.CalledProcessError, FileNotFoundError):
except (
subprocess.CalledProcessError,
FileNotFoundError,
subprocess.TimeoutExpired,
):
return False


def system_gcc_path() -> str:
"""Return the path of the system GCC as a string."""
return subprocess.check_output(
["which", "gcc"], universal_newlines=True, stderr=subprocess.DEVNULL
).strip()


@lru_cache
def gcc_bins() -> Iterable[str]:
"""Return a list of available GCCs."""
if docker_is_available():
yield "docker:gcc:11.2.0"
if system_gcc_is_available():
yield system_gcc_path()
system_gcc = shutil.which("gcc")
if system_gcc and system_has_functional_gcc(system_gcc):
yield system_gcc


@pytest.fixture(scope="module", params=gcc_bins())
Expand Down
39 changes: 23 additions & 16 deletions tests/pytest_plugins/gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# LICENSE file in the root directory of this source tree.
"""Pytest fixtures for the GCC CompilerGym environments."""

import shutil
import subprocess
from functools import lru_cache
from typing import Iterable
Expand All @@ -13,38 +14,44 @@
from tests.pytest_plugins.common import docker_is_available


@lru_cache(maxsize=2)
def system_gcc_is_available() -> bool:
def system_has_functional_gcc(gcc_path: str) -> bool:
"""Return whether there is a system GCC available."""
try:
stdout = subprocess.check_output(
["gcc", "--version"], universal_newlines=True, stderr=subprocess.DEVNULL
[gcc_path, "--version"],
universal_newlines=True,
stderr=subprocess.DEVNULL,
timeout=30,
)
# On some systems "gcc" may alias to a different compiler, so check for
# the presence of the name "gcc" in the first line of output.
return "gcc" in stdout.split("\n")[0].lower()
except (subprocess.CalledProcessError, FileNotFoundError):
except (
subprocess.CalledProcessError,
FileNotFoundError,
subprocess.TimeoutExpired,
):
return False


def system_gcc_path() -> str:
"""Return the path of the system GCC as a string."""
return subprocess.check_output(
["which", "gcc"], universal_newlines=True, stderr=subprocess.DEVNULL
).strip()


def gcc_environment_is_supported() -> bool:
"""Return whether the requirements for the GCC environment are met."""
return docker_is_available() or system_gcc_is_available()
@lru_cache
def system_gcc_is_available():
return system_has_functional_gcc(shutil.which("gcc"))


@lru_cache
def gcc_bins() -> Iterable[str]:
"""Return a list of available GCCs."""
if docker_is_available():
yield "docker:gcc:11.2.0"
if system_gcc_is_available():
yield system_gcc_path()
system_gcc = shutil.which("gcc")
if system_gcc and system_has_functional_gcc(system_gcc):
yield system_gcc


def gcc_environment_is_supported() -> bool:
"""Return whether the requirements for the GCC environment are met."""
return len(list(gcc_bins())) > 0


@pytest.fixture(scope="module", params=gcc_bins())
Expand Down

0 comments on commit 7a8f5a6

Please sign in to comment.