MacBook M1 编译 v8 问题记录

image

主要参考了 单独编译 V8 引擎Building V8 on an M1 MacBook, 下面记录了一下构建过程中遇见的其他坑点, M1 编译 v8 为啥这么多坑 😢。

Failed to fetch file gs://chromium-gn/xxx

  • 问题简述: 运行 gclient sync 命令下载更新构建工具链时抛的错误, 谷歌了一下相关的 Issue 较少, 没有有效的解决办法。
    1
    2
    3
    0> Failed to fetch file gs://chromium-gn/f08024240631f4974bb924b2f05712df185263ea for /Users/xxx/v8/buildtools/mac/gn, skipping. [Err: Traceback (most recent call last):
    File "/Users/xxx/depot_tools/gsutil.py", line 182, in <module>
    sys.exit(main())

报错堆栈的代码在 depot_tools/gsutil.py 文件,也许是代理的问题
image
所以这里应该是要给 urllib 加上 http 代理, 然后查阅一下 python 如何给 urllib 设置代理

1
2
3
4
5
6
7
8
9
10
// depot_tools/gsutil.py

try:
import urllib2 as urllib
except ImportError: # For Py3 compatibility
import urllib.request as urllib

proxy_support = urllib.ProxyHandler({'http' : 'http://127.0.0.1:8118' })
opener = urllib.build_opener(proxy_support)
urllib.install_opener(opener)
  • 问题解决: 最后手动修改 gsutil.py 代码给 urllib 设置了 ProxyHandler 后, 不过 depot_tools 会检测文件的完整性, 即发现如上修改了代码就会报错, 此时我们还需要注释掉这部分检测的代码, 最后再次运行 gclient sync 命令就 ok 了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// depot_tools/gclient_scm.py

- Make sure the tree is clean; see git-rebase.sh for reference
- try:
- scm.GIT.Capture(['update-index', '--ignore-submodules', '--refresh'],
- cwd=self.checkout_path)
- except subprocess2.CalledProcessError:
- raise gclient_utils.Error('\n____ %s at %s\n'
- '\tYou have unstaged changes.\n'
- '\tPlease commit, stash, or reset.\n'
- % (self.relpath, revision))
- try:
- scm.GIT.Capture(['diff-index', '--cached', '--name-status', '-r',
- '--ignore-submodules', 'HEAD', '--'],
- cwd=self.checkout_path)
- except subprocess2.CalledProcessError:
- raise gclient_utils.Error('\n____ %s at %s\n'
- '\tYour index contains uncommitted changes\n'
- '\tPlease commit, stash, or reset.\n'
- % (self.relpath, revision))

NOTICE: You have PROXY values set in your environment

1
2
3
NOTICE: You have PROXY values set in your environment, but gsutilin depot_tools does not (yet) obey them.
Also, --no_auth prevents the normal BOTO_CONFIG environmentvariable from being used.
To use a proxy in this situation, please supply those settingsin a .boto file pointed to by the NO_AUTH_BOTO_CONFIG environmentvariable.
  • 问题解决: 新增 .boto 文件, 然后设置 NO_AUTH_BOTO_CONFIG 环境变量再接着运行
    1
    2
    3
    4
    [Boto]
    proxy = 127.0.0.1
    proxy_port = 8118
    proxy_type = http
1
export NO_AUTH_BOTO_CONFIG=/Users/xxx/.boto

https://commondatastorage.googleapis.com/**

  • 问题简述: pythone 脚本的请求的流量不会走系统代理, 导致连接不到 google 服务, 灵机一动就先转发到本地服务, 本地服务再通过设置 httpClientAent 为系统全局代理地址来实现
  • 问题解决: 全局替换 https://commondatastorage.googleapis.comhttp://127.0.0.1:3000 , 然后通过本地服务转发请求
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    const Koa = require("koa");
    const proxy = require("koa-proxies");
    const httpsProxyAgent = require('https-proxy-agent')

    const app = new Koa();

    app.use(
    proxy(/^\//, {
    target: "https://commondatastorage.googleapis.com",
    agent: new httpsProxyAgent('http://127.0.0.1:8118'),

    logs: false,
    changeOrigin: true,
    headers: {
    Origin: "https://commondatastorage.googleapis.com",
    Host: "commondatastorage.googleapis.com",
    Referer: "https://commondatastorage.googleapis.com",
    },
    secure: false,
    events: {
    // proxyRes: (proxyRes, _req, res) => {
    // let removeSecure = (str) => str.replace(/;Secure/i, "");
    // let set = proxyRes.headers["set-cookie"];
    // if (set) {
    // let result = Array.isArray(set)
    // ? set.map(removeSecure)
    // : removeSecure(set);
    // res.setHeader("set-cookie", result);
    // proxyRes.headers["set-cookie"] = result;
    // }
    // },
    },
    })
    );

    app.listen(3000)

clang++: error: argument unused during compilation

  • 问题解决: 增加 -Qunused-arguments 参数使编译器忽略这个错误
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // v8/build/config/mac/BUILD.gn

    if (host_os == "mac") {
    common_mac_flags += [
    "-arch",
    clang_arch,
    + "-Qunused-arguments"
    ]
    } else {
    common_mac_flags += [ "--target=$clang_arch-apple-macos" ]
    }

Embedder-vs-V8 build configuration mismatch.

1
2
3
4
5
6
# Fatal error in , line 0
# Embedder-vs-V8 build configuration mismatch. On embedder side pointer compression is DISABLED while on V8 side it's ENABLED.
#
#
#
#FailureMessage Object: 0x16fc2a918
  • 问题解决: 编译参数加上 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX
    1
    g++ src/pure.cc -g -I deps/v8/include/ -o pure -L lib/ -lv8_monolith -pthread -std=c++17 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX

unknown current_cpu $current_cpu

  • 问题简述: 还不支持 arm64 平台编译 ?
  • 问题解决: 确保如下几步都做到
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

    # v8 目录下运行如下命令, 支持 arm64
    echo "mac-arm64" > .cipd_client_platform

    # v8 目录下运行如下命令, 支持 arm64
    mkdir -p out.gn/arm64.release/
    cat >> out.gn/arm64.release/args.gn <<EOF

    dcheck_always_on = false
    is_debug = false
    target_cpu = "arm64"
    v8_target_cpu = "arm64"

    cc_wrapper="ccache"
    EOF

    # 使用 v8gen 生成 arm64 平台的编译配置
    /Users/xxx/v8/tools/dev/v8gen.py arm64.release.sample

    # 编译 arm64 平台的 V8 的静态库
    ninja -C out.gn/arm64.release.sample v8_monolith