Commit 2c3b674e authored by Ng Zhi An's avatar Ng Zhi An Committed by Commit Bot

[Py3] Make build.py python3 compatible

Couple of fixes here include:
- change `file` to `open`
- add `universal_newlines=True` to subprocess calls, since the output is
later compared to a string
- change buffer to line buffer, this is the only "breaking" change that
might cause a different behavior.

For the buffer behavior, unbuffered output is only allowed in binary
mode, but we are always printing strings. So the compromise here is to
use line buffering (we could also omit it).

Bug: v8:9871,chromium:941669
Change-Id: I5c1e69da022d13f4234921fe5fe717d53c96f058
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2252867
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Reviewed-by: default avatarDirk Pranke <dpranke@google.com>
Cr-Commit-Position: refs/heads/master@{#781911}
parent 2df93999
...@@ -14,6 +14,7 @@ from __future__ import print_function ...@@ -14,6 +14,7 @@ from __future__ import print_function
import argparse import argparse
import glob import glob
import io
import json import json
import os import os
import pipes import pipes
...@@ -215,7 +216,7 @@ def CreateChromeToolsShim(): ...@@ -215,7 +216,7 @@ def CreateChromeToolsShim():
tool detection logic munges them in a weird way.""" tool detection logic munges them in a weird way."""
assert not any(i in os.path.basename(CHROME_TOOLS_SHIM_DIR) for i in '-_') assert not any(i in os.path.basename(CHROME_TOOLS_SHIM_DIR) for i in '-_')
os.mkdir(CHROME_TOOLS_SHIM_DIR) os.mkdir(CHROME_TOOLS_SHIM_DIR)
with file(os.path.join(CHROME_TOOLS_SHIM_DIR, 'CMakeLists.txt'), 'w') as f: with open(os.path.join(CHROME_TOOLS_SHIM_DIR, 'CMakeLists.txt'), 'w') as f:
f.write('# Automatically generated by tools/clang/scripts/update.py. ' + f.write('# Automatically generated by tools/clang/scripts/update.py. ' +
'Do not edit.\n') 'Do not edit.\n')
f.write('# Since tools/clang is located in another directory, use the \n') f.write('# Since tools/clang is located in another directory, use the \n')
...@@ -326,7 +327,8 @@ def VerifyVersionOfBuiltClangMatchesVERSION(): ...@@ -326,7 +327,8 @@ def VerifyVersionOfBuiltClangMatchesVERSION():
clang = os.path.join(LLVM_BUILD_DIR, 'bin', 'clang') clang = os.path.join(LLVM_BUILD_DIR, 'bin', 'clang')
if sys.platform == 'win32': if sys.platform == 'win32':
clang += '-cl.exe' clang += '-cl.exe'
version_out = subprocess.check_output([clang, '--version']) version_out = subprocess.check_output([clang, '--version'],
universal_newlines=True)
version_out = re.match(r'clang version ([0-9.]+)', version_out).group(1) version_out = re.match(r'clang version ([0-9.]+)', version_out).group(1)
if version_out != RELEASE_VERSION: if version_out != RELEASE_VERSION:
print(('unexpected clang version %s (not %s), ' print(('unexpected clang version %s (not %s), '
...@@ -339,9 +341,11 @@ def CopyLibstdcpp(args, build_dir): ...@@ -339,9 +341,11 @@ def CopyLibstdcpp(args, build_dir):
if not args.gcc_toolchain: if not args.gcc_toolchain:
return return
# Find libstdc++.so.6 # Find libstdc++.so.6
libstdcpp = subprocess.check_output( libstdcpp = subprocess.check_output([
[os.path.join(args.gcc_toolchain, 'bin', 'g++'), os.path.join(args.gcc_toolchain, 'bin', 'g++'),
'-print-file-name=libstdc++.so.6']).rstrip() '-print-file-name=libstdc++.so.6'
],
universal_newlines=True).rstrip()
# Copy libstdc++.so.6 into the build dir so that the built binaries can find # Copy libstdc++.so.6 into the build dir so that the built binaries can find
# it. Binaries get their rpath set to $origin/../lib/. For clang, lld, # it. Binaries get their rpath set to $origin/../lib/. For clang, lld,
...@@ -431,7 +435,16 @@ def main(): ...@@ -431,7 +435,16 @@ def main():
# Don't buffer stdout, so that print statements are immediately flushed. # Don't buffer stdout, so that print statements are immediately flushed.
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # LLVM tests print output without newlines, so with buffering they won't be
# immediately printed.
major, _, _, _, _ = sys.version_info
if major == 3:
# Python3 only allows unbuffered output for binary streams. This
# workaround comes from https://stackoverflow.com/a/181654/4052492.
sys.stdout = io.TextIOWrapper(open(sys.stdout.fileno(), 'wb', 0),
write_through=True)
else:
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
# The gnuwin package also includes curl, which is needed to interact with the # The gnuwin package also includes curl, which is needed to interact with the
# github API below. # github API below.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment