Skip to content

Commit 0eaea29

Browse files
authored
fix: ignore failure of GetCompilerPredefines (#295)
* fix: ignore failure of GetCompilerPredefines * test: rename to GetCompilerPredefines * test: cover commond fails
1 parent 59b5903 commit 0eaea29

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

pylib/gyp/common.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,9 @@ def EnsureDirExists(path):
421421
except OSError:
422422
pass
423423

424-
def GetCrossCompilerPredefines(): # -> dict
424+
def GetCompilerPredefines(): # -> dict
425425
cmd = []
426+
defines = {}
426427

427428
# shlex.split() will eat '\' in posix mode, but
428429
# setting posix=False will preserve extra '"' cause CreateProcess fail on Windows
@@ -439,7 +440,7 @@ def replace_sep(s):
439440
if CXXFLAGS := os.environ.get("CXXFLAGS"):
440441
cmd += shlex.split(replace_sep(CXXFLAGS))
441442
else:
442-
return {}
443+
return defines
443444

444445
if sys.platform == "win32":
445446
fd, input = tempfile.mkstemp(suffix=".c")
@@ -450,17 +451,33 @@ def replace_sep(s):
450451
real_cmd, shell=True,
451452
capture_output=True, check=True
452453
).stdout
454+
except subprocess.CalledProcessError as e:
455+
print(
456+
"Warning: failed to get compiler predefines\n"
457+
"cmd: %s\n"
458+
"status: %d" % (e.cmd, e.returncode),
459+
file=sys.stderr
460+
)
461+
return defines
453462
finally:
454463
os.unlink(input)
455464
else:
456465
input = "/dev/null"
457466
real_cmd = [*cmd, "-dM", "-E", "-x", "c", input]
458-
stdout = subprocess.run(
459-
real_cmd, shell=False,
460-
capture_output=True, check=True
461-
).stdout
467+
try:
468+
stdout = subprocess.run(
469+
real_cmd, shell=False,
470+
capture_output=True, check=True
471+
).stdout
472+
except subprocess.CalledProcessError as e:
473+
print(
474+
"Warning: failed to get compiler predefines\n"
475+
"cmd: %s\n"
476+
"status: %d" % (e.cmd, e.returncode),
477+
file=sys.stderr
478+
)
479+
return defines
462480

463-
defines = {}
464481
lines = stdout.decode("utf-8").replace("\r\n", "\n").split("\n")
465482
for line in lines:
466483
if (line or "").startswith("#define "):
@@ -499,7 +516,7 @@ def GetFlavor(params):
499516
if "flavor" in params:
500517
return params["flavor"]
501518

502-
defines = GetCrossCompilerPredefines()
519+
defines = GetCompilerPredefines()
503520
if "__EMSCRIPTEN__" in defines:
504521
return "emscripten"
505522
if "__wasm__" in defines:

pylib/gyp/common_test.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""Unit tests for the common.py file."""
88

99
import os
10+
import subprocess
1011
import sys
1112
import unittest
1213
from unittest.mock import MagicMock, patch
@@ -85,22 +86,34 @@ def decode(self, encoding):
8586
@patch("os.close")
8687
@patch("os.unlink")
8788
@patch("tempfile.mkstemp")
88-
def test_GetCrossCompilerPredefines(self, mock_mkstemp, mock_unlink, mock_close):
89+
def test_GetCompilerPredefines(self, mock_mkstemp, mock_unlink, mock_close):
8990
mock_close.return_value = None
9091
mock_unlink.return_value = None
9192
mock_mkstemp.return_value = (0, "temp.c")
9293

93-
def mock_run(env, defines_stdout, expected_cmd):
94+
def mock_run(env, defines_stdout, expected_cmd, throws=False):
9495
with patch("subprocess.run") as mock_run:
95-
mock_process = MagicMock()
96-
mock_process.returncode = 0
97-
mock_process.stdout = TestGetFlavor.MockCommunicate(defines_stdout)
98-
mock_run.return_value = mock_process
9996
expected_input = "temp.c" if sys.platform == "win32" else "/dev/null"
97+
if throws:
98+
mock_run.side_effect = subprocess.CalledProcessError(
99+
returncode=1,
100+
cmd=[
101+
*expected_cmd,
102+
"-dM", "-E", "-x", "c", expected_input
103+
]
104+
)
105+
else:
106+
mock_process = MagicMock()
107+
mock_process.returncode = 0
108+
mock_process.stdout = TestGetFlavor.MockCommunicate(defines_stdout)
109+
mock_run.return_value = mock_process
100110
with patch.dict(os.environ, env):
101-
defines = gyp.common.GetCrossCompilerPredefines()
111+
try:
112+
defines = gyp.common.GetCompilerPredefines()
113+
except Exception as e:
114+
self.fail(f"GetCompilerPredefines raised an exception: {e}")
102115
flavor = gyp.common.GetFlavor({})
103-
if env.get("CC_target"):
116+
if env.get("CC_target") or env.get("CC"):
104117
mock_run.assert_called_with(
105118
[
106119
*expected_cmd,
@@ -110,6 +123,9 @@ def mock_run(env, defines_stdout, expected_cmd):
110123
capture_output=True, check=True)
111124
return [defines, flavor]
112125

126+
[defines0, _] = mock_run({ "CC": "cl.exe" }, "", ["cl.exe"], True)
127+
assert defines0 == {}
128+
113129
[defines1, _] = mock_run({}, "", [])
114130
assert defines1 == {}
115131

0 commit comments

Comments
 (0)