summaryrefslogtreecommitdiff
path: root/deps/v8/tools/dev/gen-static-roots.py
blob: 48d9af513ec687ce84e3935a6e09a7fd2b0c4053 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3

# Copyright 2022 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can
# be found in the LICENSE file.

import subprocess
import argparse
import os
import filecmp
import tempfile
import shutil
import platform
from pathlib import Path

# Detect if we have goma


def _Which(cmd):
  for path in os.environ["PATH"].split(os.pathsep):
    if os.path.exists(os.path.join(path, cmd)):
      return os.path.join(path, cmd)
  return None


def DetectGoma():
  if os.environ.get("GOMA_DIR"):
    return os.environ.get("GOMA_DIR")
  if os.environ.get("GOMADIR"):
    return os.environ.get("GOMADIR")
  # There is a copy of goma in depot_tools, but it might not be in use on
  # this machine.
  goma = _Which("goma_ctl")
  if goma is None:
    return None
  cipd_bin = os.path.join(os.path.dirname(goma), ".cipd_bin")
  if not os.path.exists(cipd_bin):
    return None
  goma_auth = os.path.expanduser("~/.goma_client_oauth2_config")
  if not os.path.exists(goma_auth):
    return None
  return cipd_bin


GOMADIR = DetectGoma()
IS_GOMA_MACHINE = GOMADIR is not None

USE_GOMA = "true" if IS_GOMA_MACHINE else "false"

# List of all supported build configurations for static roots

STATIC_ROOT_CONFIGURATIONS = {
    "ptr-cmpr-wasm-intl": {
        "target":
            "src/roots/static-roots.h",
        "gn_args":
            f"""\
is_debug = false
use_goma = {USE_GOMA}
v8_enable_static_roots = false
v8_enable_static_root_generation = true
v8_enable_pointer_compression = true
v8_enable_shared_ro_heap = true
v8_enable_pointer_compression_shared_cage = true
v8_enable_webassembly = true
v8_enable_i18n_support = true
dcheck_always_on = true
"""
    },
}

# Parse args

parser = argparse.ArgumentParser(description='Generates static-roots.h.')
parser.add_argument(
    '--configuration',
    choices=STATIC_ROOT_CONFIGURATIONS.keys(),
    action='extend',
    default='ptr-cmpr-wasm-intl',
    nargs='*',
    help="""Build configuration. Refers to a set of configurations with
identical static-roots.h. Currently there is only one supported configuration.
Future configurations will need to generate multiple target files.""")
parser.add_argument(
    '--out',
    default=Path('out'),
    required=False,
    type=Path,
    help='target build directory')

args = parser.parse_args()

# Some helpers


def run(cmd, **kwargs):
  print(f"# CMD: {cmd} {kwargs}")
  return subprocess.run(cmd, **kwargs, check=True)


def build(path, gn_args):
  if not path.exists():
    path.mkdir(parents=True, exist_ok=True)
  with (path / "args.gn").open("w") as f:
    f.write(gn_args)
  suffix = ".bat" if platform.system() == "Windows" else ""
  run(["gn" + suffix, "gen", path])
  run(["autoninja" + suffix, "-C", path, "mksnapshot"])
  return path.absolute()


# Generate all requested static root headers

v8_path = Path(__file__).parents[2]

changed = False
for target in [args.configuration]:
  build_dir = args.out / f"gen-static-roots.{target}"
  config = STATIC_ROOT_CONFIGURATIONS[target]
  gn_args = config["gn_args"]
  build_path = build(build_dir, gn_args)
  out_file = Path(tempfile.gettempdir()) / f"static-roots-{target}.h"
  run([build_path / "mksnapshot", "--static-roots-src", out_file])
  target_file = v8_path / config["target"]
  if not filecmp.cmp(out_file, target_file):
    shutil.move(out_file, target_file)
    changed = True

if changed:
  exit(1)