Skip to content

Commit

Permalink
Merge pull request #381 from online-judge-tools/mac-clang-depfile
Browse files Browse the repository at this point in the history
Fix the options to analyze the dependencies of C++ files
  • Loading branch information
kmyk authored Mar 14, 2021
2 parents 28f6bb9 + b77d5de commit 3addf08
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions onlinejudge_verify/languages/cplusplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import shutil
import subprocess
import sys
import tempfile
from logging import getLogger
from typing import *

Expand Down Expand Up @@ -44,22 +43,17 @@ def _is_gcc(self) -> bool:

@functools.lru_cache(maxsize=None)
def _cplusplus_list_depending_files(path: pathlib.Path, *, CXX: pathlib.Path, joined_CXXFLAGS: str) -> List[pathlib.Path]:
# Using /dev/stdout is acceptable because Library Chcker doesn't work on Windows.
is_windows = (platform.uname().system == 'Windows')
with tempfile.TemporaryDirectory() as temp_dir:
temp_file = pathlib.Path(temp_dir) / 'dependencies.txt'
command = [str(CXX), *shlex.split(joined_CXXFLAGS), '-MD', '-MF', str(temp_file), '-MM', str(path)]
try:
subprocess.check_call(command)
except subprocess.CalledProcessError:
logger.error("failed to analyze dependencies with %s: %s (hint: Please check #include directives of the file and its dependencies. The paths must exist, must not contain '\\', and must be case-sensitive.)", CXX, str(path))
print(f'::warning file={str(path)}::failed to analyze dependencies', file=sys.stderr)
raise
with open(temp_file, 'rb') as fp:
data = fp.read()
logger.debug('dependencies of %s: %s', str(path), repr(data))
makefile_rule = shlex.split(data.decode().strip().replace('\\\n', '').replace('\\\r\n', ''), posix=not is_windows)
return [pathlib.Path(path).resolve() for path in makefile_rule[1:]]
command = [str(CXX), *shlex.split(joined_CXXFLAGS), '-MM', str(path)]
try:
data = subprocess.check_output(command)
except subprocess.CalledProcessError:
logger.error("failed to analyze dependencies with %s: %s (hint: Please check #include directives of the file and its dependencies. The paths must exist, must not contain '\\', and must be case-sensitive.)", CXX, str(path))
print(f'::warning file={str(path)}::failed to analyze dependencies', file=sys.stderr)
raise
logger.debug('dependencies of %s: %s', str(path), repr(data))
makefile_rule = shlex.split(data.decode().strip().replace('\\\n', '').replace('\\\r\n', ''), posix=not is_windows)
return [pathlib.Path(path).resolve() for path in makefile_rule[1:]]


@functools.lru_cache(maxsize=None)
Expand Down

0 comments on commit 3addf08

Please sign in to comment.