Skip to content

Commit 1442832

Browse files
committed
[tests] Better GCC fixture enumeration.
1 parent 8276d93 commit 1442832

2 files changed

Lines changed: 38 additions & 29 deletions

File tree

examples/gcc_autotuning/tune_test.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# This source code is licensed under the MIT license found in the
44
# LICENSE file in the root directory of this source tree.
5+
import shutil
56
import subprocess
67
import sys
78
from functools import lru_cache
@@ -24,33 +25,34 @@ def docker_is_available() -> bool:
2425
return False
2526

2627

27-
@lru_cache(maxsize=2)
28-
def system_gcc_is_available() -> bool:
28+
def system_has_functional_gcc(gcc_path: str) -> bool:
2929
"""Return whether there is a system GCC available."""
3030
try:
3131
stdout = subprocess.check_output(
32-
["gcc", "--version"], universal_newlines=True, stderr=subprocess.DEVNULL
32+
[gcc_path, "--version"],
33+
universal_newlines=True,
34+
stderr=subprocess.DEVNULL,
35+
timeout=30,
3336
)
3437
# On some systems "gcc" may alias to a different compiler, so check for
3538
# the presence of the name "gcc" in the first line of output.
3639
return "gcc" in stdout.split("\n")[0].lower()
37-
except (subprocess.CalledProcessError, FileNotFoundError):
40+
except (
41+
subprocess.CalledProcessError,
42+
FileNotFoundError,
43+
subprocess.TimeoutExpired,
44+
):
3845
return False
3946

4047

41-
def system_gcc_path() -> str:
42-
"""Return the path of the system GCC as a string."""
43-
return subprocess.check_output(
44-
["which", "gcc"], universal_newlines=True, stderr=subprocess.DEVNULL
45-
).strip()
46-
47-
48+
@lru_cache
4849
def gcc_bins() -> Iterable[str]:
4950
"""Return a list of available GCCs."""
5051
if docker_is_available():
5152
yield "docker:gcc:11.2.0"
52-
if system_gcc_is_available():
53-
yield system_gcc_path()
53+
system_gcc = shutil.which("gcc")
54+
if system_gcc and system_has_functional_gcc(system_gcc):
55+
yield system_gcc
5456

5557

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

tests/pytest_plugins/gcc.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# LICENSE file in the root directory of this source tree.
55
"""Pytest fixtures for the GCC CompilerGym environments."""
66

7+
import shutil
78
import subprocess
89
from functools import lru_cache
910
from typing import Iterable
@@ -13,38 +14,44 @@
1314
from tests.pytest_plugins.common import docker_is_available
1415

1516

16-
@lru_cache(maxsize=2)
17-
def system_gcc_is_available() -> bool:
17+
def system_has_functional_gcc(gcc_path: str) -> bool:
1818
"""Return whether there is a system GCC available."""
1919
try:
2020
stdout = subprocess.check_output(
21-
["gcc", "--version"], universal_newlines=True, stderr=subprocess.DEVNULL
21+
[gcc_path, "--version"],
22+
universal_newlines=True,
23+
stderr=subprocess.DEVNULL,
24+
timeout=30,
2225
)
2326
# On some systems "gcc" may alias to a different compiler, so check for
2427
# the presence of the name "gcc" in the first line of output.
2528
return "gcc" in stdout.split("\n")[0].lower()
26-
except (subprocess.CalledProcessError, FileNotFoundError):
29+
except (
30+
subprocess.CalledProcessError,
31+
FileNotFoundError,
32+
subprocess.TimeoutExpired,
33+
):
2734
return False
2835

2936

30-
def system_gcc_path() -> str:
31-
"""Return the path of the system GCC as a string."""
32-
return subprocess.check_output(
33-
["which", "gcc"], universal_newlines=True, stderr=subprocess.DEVNULL
34-
).strip()
35-
36-
37-
def gcc_environment_is_supported() -> bool:
38-
"""Return whether the requirements for the GCC environment are met."""
39-
return docker_is_available() or system_gcc_is_available()
37+
@lru_cache
38+
def system_gcc_is_available():
39+
return system_has_functional_gcc(shutil.which("gcc"))
4040

4141

42+
@lru_cache
4243
def gcc_bins() -> Iterable[str]:
4344
"""Return a list of available GCCs."""
4445
if docker_is_available():
4546
yield "docker:gcc:11.2.0"
46-
if system_gcc_is_available():
47-
yield system_gcc_path()
47+
system_gcc = shutil.which("gcc")
48+
if system_gcc and system_has_functional_gcc(system_gcc):
49+
yield system_gcc
50+
51+
52+
def gcc_environment_is_supported() -> bool:
53+
"""Return whether the requirements for the GCC environment are met."""
54+
return len(list(gcc_bins())) > 0
4855

4956

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

0 commit comments

Comments
 (0)