Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gn scripts are run with the wrong Python version ("a bytes-like object is required, not 'str'")

When building Chromium or libwebrtc with gn on macOS (Catalina 10.15), I get errors from the Python build scripts about bytes and str. For example:

src [heads/master●] % gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64"'
ERROR at //build/config/ios/ios_sdk.gni:109:21: Script returned non-zero exit code.
  _ios_sdk_result = exec_script(script_name, ios_sdk_info_args, "scope")
                    ^----------
Current dir: /Users/lynn/code/webrtc_ios/src/out/ios_64/
Command: python /Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py --get_sdk_info iphoneos
Returned 1.
stderr:

Traceback (most recent call last):
  File "/Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py", line 107, in <module>
    FillXcodeVersion(settings, args.developer_dir)
  File "/Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py", line 59, in FillXcodeVersion
    settings['xcode_version'] = FormatVersion(lines[0].split()[-1])
  File "/Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py", line 43, in FormatVersion
    major, minor, patch = SplitVersion(version)
  File "/Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py", line 30, in SplitVersion
    version = version.split('.')
TypeError: a bytes-like object is required, not 'str'

See //build/config/sysroot.gni:67:3: whence it was imported.
  import("//build/config/ios/ios_sdk.gni")
  ^--------------------------------------
See //build/config/linux/pkg_config.gni:5:1: whence it was imported.
import("//build/config/sysroot.gni")
^----------------------------------
See //BUILD.gn:15:1: whence it was imported.
import("//build/config/linux/pkg_config.gni")
^-------------------------------------------

When I edit the offending Python script to print(sys.version), it shows that it is running Python 3, even though the scripts are supposed to running with the bundled virtual Python 2.7 environment defined in .vpython.

How do I configure gn to run these scripts with the appropriate Python version?

like image 834
Lynn Avatar asked Jan 01 '23 11:01

Lynn


1 Answers

It seems exec_script is running these scripts with the machine Python version. gn help exec_script says:

The default script interpreter is Python ("python" on POSIX, "python.exe" or "python.bat" on Windows). This can be configured by the script_executable variable, see "gn help dotfile".

For me, python points to Python 3. So I had to add this line to the end of the .gn dotfile:

script_executable = "vpython"

Now the build uses the virtual Python defined in .vpython, which is Python 2.7.

(The vpython executable is provided by Chromium depot_tools, just like gn.)

like image 113
Lynn Avatar answered May 11 '23 20:05

Lynn