Skip to content

Commit

Permalink
test new patch
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Apr 2, 2024
1 parent bbcb1e1 commit 030a099
Showing 1 changed file with 178 additions and 8 deletions.
186 changes: 178 additions & 8 deletions patches/gyp-next-2.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
From 4c049a0a283951886636aedbcbe629ae6443286c Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 19:26:10 +0800
Subject: [PATCH] fix: failed to detect flavor if compiler path include white
spaces
Subject: [PATCH 1/2] fix: failed to detect flavor if compiler path include
white spaces

---
pylib/gyp/common.py | 9 +++++----
Expand All @@ -13,7 +13,7 @@ diff --git a/pylib/gyp/common.py b/pylib/gyp/common.py
index db64dc5..ce498fb 100644
--- a/pylib/gyp/common.py
+++ b/pylib/gyp/common.py
@@ -9,6 +9,7 @@ import re
@@ -9,6 +9,7 @@
import tempfile
import sys
import subprocess
Expand Down Expand Up @@ -43,15 +43,15 @@ diff --git a/pylib/gyp/common_test.py b/pylib/gyp/common_test.py
index 43b2254..da78d39 100755
--- a/pylib/gyp/common_test.py
+++ b/pylib/gyp/common_test.py
@@ -11,6 +11,7 @@ import unittest
@@ -11,6 +11,7 @@
import sys
import os
import subprocess
+import shlex
from unittest.mock import patch, MagicMock

class TestTopologicallySorted(unittest.TestCase):
@@ -103,7 +104,11 @@ class TestGetFlavor(unittest.TestCase):
@@ -103,7 +104,11 @@ def mock_run(env, defines_stdout):
flavor = gyp.common.GetFlavor({})
if env.get("CC_target"):
mock_popen.assert_called_with(
Expand All @@ -64,7 +64,7 @@ index 43b2254..da78d39 100755
shell=sys.platform == "win32",
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return [defines, flavor]
@@ -132,5 +137,20 @@ class TestGetFlavor(unittest.TestCase):
@@ -132,5 +137,20 @@ def mock_run(env, defines_stdout):
self.assertDictEqual({ "__EMSCRIPTEN__": "1" }, defines4)
self.assertEqual("emscripten", flavor4)

Expand All @@ -85,6 +85,176 @@ index 43b2254..da78d39 100755
+
if __name__ == "__main__":
unittest.main()
--
2.39.3 (Apple Git-145)

From b6b68d64d8c9e3e597f1b7022872d02a11cc0240 Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 20:38:41 +0800
Subject: [PATCH 2/2] raise error if exit code is not 0

---
pylib/gyp/common.py | 26 ++++++++++++++++--------
pylib/gyp/common_test.py | 44 ++++++++++++++++++++++++++++++----------
2 files changed, 51 insertions(+), 19 deletions(-)

diff --git a/pylib/gyp/common.py b/pylib/gyp/common.py
index ce498fb..189f2a2 100644
--- a/pylib/gyp/common.py
+++ b/pylib/gyp/common.py
@@ -425,38 +425,48 @@ def EnsureDirExists(path):

def GetCrossCompilerPredefines(): # -> dict
cmd = []
+
+ # shlex.split() will eat '\' in posix mode
+ # but setting posix=False will preserve extra '"' cause CreateProcess fail on Windows
+ # this makes '\' in %CC_target% and %CFLAGS% work
+ replace_sep = lambda s : s.replace("\\", "/") if sys.platform == "win32" else s
+
if CC := os.environ.get("CC_target") or os.environ.get("CC"):
- cmd += shlex.split(CC)
+ cmd += shlex.split(replace_sep(CC))
if CFLAGS := os.environ.get("CFLAGS"):
- cmd += shlex.split(CFLAGS)
+ cmd += shlex.split(replace_sep(CFLAGS))
elif CXX := os.environ.get("CXX_target") or os.environ.get("CXX"):
- cmd += shlex.split(CXX)
+ cmd += shlex.split(replace_sep(CXX))
if CXXFLAGS := os.environ.get("CXXFLAGS"):
- cmd += shlex.split(CXXFLAGS)
+ cmd += shlex.split(replace_sep(CXXFLAGS))
else:
return {}

if sys.platform == "win32":
fd, input = tempfile.mkstemp(suffix=".c")
+ real_cmd = [*cmd, "-dM", "-E", "-x", "c", input]
try:
os.close(fd)
out = subprocess.Popen(
- [*cmd, "-dM", "-E", "-x", "c", input],
+ real_cmd,
shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
- stdout = out.communicate()[0]
+ stdout, stderr = out.communicate()
finally:
os.unlink(input)
else:
input = "/dev/null"
+ real_cmd = [*cmd, "-dM", "-E", "-x", "c", input]
out = subprocess.Popen(
- [*cmd, "-dM", "-E", "-x", "c", input],
+ real_cmd,
shell=False,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
- stdout = out.communicate()[0]
+ stdout, stderr = out.communicate()

+ if out.returncode != 0:
+ raise subprocess.CalledProcessError(out.returncode, real_cmd, stdout, stderr)
defines = {}
lines = stdout.decode("utf-8").replace("\r\n", "\n").split("\n")
for line in lines:
diff --git a/pylib/gyp/common_test.py b/pylib/gyp/common_test.py
index da78d39..a0cb2ed 100755
--- a/pylib/gyp/common_test.py
+++ b/pylib/gyp/common_test.py
@@ -91,11 +91,14 @@ def test_GetCrossCompilerPredefines(self, mock_mkstemp, mock_unlink, mock_close)
mock_unlink.return_value = None
mock_mkstemp.return_value = (0, "temp.c")

- def mock_run(env, defines_stdout):
+ def mock_run(env, defines_stdout, expected_cmd):
with patch("subprocess.Popen") as mock_popen:
mock_process = MagicMock()
mock_process.communicate.return_value = (
- TestGetFlavor.MockCommunicate(defines_stdout), None)
+ TestGetFlavor.MockCommunicate(defines_stdout),
+ TestGetFlavor.MockCommunicate("")
+ )
+ mock_process.returncode = 0
mock_process.stdout = MagicMock()
mock_popen.return_value = mock_process
expected_input = "temp.c" if sys.platform == "win32" else "/dev/null"
@@ -105,34 +108,36 @@ def mock_run(env, defines_stdout):
if env.get("CC_target"):
mock_popen.assert_called_with(
[
- *shlex.split(env["CC_target"]),
- *(shlex.split(env["CFLAGS"]) if env.get("CFLAGS") else []),
+ *expected_cmd,
"-dM", "-E", "-x", "c", expected_input
],
shell=sys.platform == "win32",
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return [defines, flavor]

- [defines1, _] = mock_run({}, "")
+ [defines1, _] = mock_run({}, "", [])
self.assertDictEqual({}, defines1)

[defines2, flavor2] = mock_run(
{ "CC_target": "/opt/wasi-sdk/bin/clang" },
- "#define __wasm__ 1\n#define __wasi__ 1\n"
+ "#define __wasm__ 1\n#define __wasi__ 1\n",
+ ["/opt/wasi-sdk/bin/clang"]
)
self.assertDictEqual({ "__wasm__": "1", "__wasi__": "1" }, defines2)
self.assertEqual("wasi", flavor2)

[defines3, flavor3] = mock_run(
- { "CC_target": "/opt/wasi-sdk/bin/clang" },
- "#define __wasm__ 1\n"
+ { "CC_target": "/opt/wasi-sdk/bin/clang --target=wasm32" },
+ "#define __wasm__ 1\n",
+ ["/opt/wasi-sdk/bin/clang", "--target=wasm32"]
)
self.assertDictEqual({ "__wasm__": "1" }, defines3)
self.assertEqual("wasm", flavor3)

[defines4, flavor4] = mock_run(
{ "CC_target": "/emsdk/upstream/emscripten/emcc" },
- "#define __EMSCRIPTEN__ 1\n"
+ "#define __EMSCRIPTEN__ 1\n",
+ ["/emsdk/upstream/emscripten/emcc"]
)
self.assertDictEqual({ "__EMSCRIPTEN__": "1" }, defines4)
self.assertEqual("emscripten", flavor4)
@@ -140,10 +145,16 @@ def mock_run(env, defines_stdout):
# Test path which include white space
[defines5, flavor5] = mock_run(
{
- "CC_target": "\"/Users/Toyo Li/wasi-sdk/bin/clang\"",
+ "CC_target": "\"/Users/Toyo Li/wasi-sdk/bin/clang\" -O3",
"CFLAGS": "--target=wasm32-wasi-threads -pthread"
},
- "#define __wasm__ 1\n#define __wasi__ 1\n#define _REENTRANT 1\n"
+ "#define __wasm__ 1\n#define __wasi__ 1\n#define _REENTRANT 1\n",
+ [
+ "/Users/Toyo Li/wasi-sdk/bin/clang",
+ "-O3",
+ "--target=wasm32-wasi-threads",
+ "-pthread"
+ ]
)
self.assertDictEqual({
"__wasm__": "1",
@@ -152,5 +163,16 @@ def mock_run(env, defines_stdout):
}, defines5)
self.assertEqual("wasi", flavor5)

+ original_platform = sys.platform
+ sys.platform = "win32"
+ [defines6, flavor6] = mock_run(
+ { "CC_target": "\"C:\\Program Files\\wasi-sdk\\clang.exe\"" },
+ "#define __wasm__ 1\n#define __wasi__ 1\n",
+ ["C:/Program Files/wasi-sdk/clang.exe"]
+ )
+ sys.platform = original_platform
+ self.assertDictEqual({ "__wasm__": "1", "__wasi__": "1" }, defines6)
+ self.assertEqual("wasi", flavor6)
+
if __name__ == "__main__":
unittest.main()

0 comments on commit 030a099

Please sign in to comment.