File: gcmole_args.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (80 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download | duplicates (6)
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
# Copyright 2023 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.
"""Calculate arguments for the gcmole plugin based on flags passed to the
compiler for a typical target in V8.
"""

from pathlib import Path

import os
import re
import sys

DEFINES_RE = re.compile(r'^defines = (.*)$', re.M)
INCLUDES_RE = re.compile(r'^include_dirs = (.*)$', re.M)
CFLAGS_CC_RE = re.compile(r'^cflags_cc = (.*)$', re.M)

BASE_DIR = Path(__file__).resolve().parents[2].absolute()

# This script is always called relative to the build directory root
# by ninja.
BUILD_DIR_ABS = Path.cwd()
BUILD_DIR_REL = BUILD_DIR_ABS.relative_to(BASE_DIR)


def search_flags(regexp, ninja_config):
  match = regexp.search(ninja_config)
  assert match
  result = match.group(1)
  assert result
  return result

def main():
  assert len(sys.argv) == 2, 'Expecting sysroot arg'
  gn_sysroot_var = sys.argv[1]
  assert gn_sysroot_var.startswith('//'), 'Expecting root-dir gn path'
  rel_sysroot = gn_sysroot_var[len('//'):]

  assert BUILD_DIR_ABS.exists()

  ninja_file = BUILD_DIR_ABS / 'obj' / 'v8_base_without_compiler.ninja'
  assert ninja_file.exists()

  with ninja_file.open() as f:
    ninja_config = f.read()

  defines = search_flags(DEFINES_RE, ninja_config)
  # ninja files escape special characters (e.g. '"', '\', '(', etc).
  defines = defines.replace('\\', '')
  includes = search_flags(INCLUDES_RE, ninja_config)
  cflags_cc = search_flags(CFLAGS_CC_RE, ninja_config)

  flags = []
  flags += defines.strip().split()

  # Include flags are relative to the build root. Make them relative to the
  # base directory for gcmole.
  # E.g. BUILD_DIR_REL = out/build and -I../../include gives -Iinclude.
  for flag in includes.strip().split():
    prefix, suffix = flag[:2], flag[2:]
    assert prefix == '-I'
    flags.append(prefix + os.path.normpath(BUILD_DIR_REL / suffix))

  # System include flags are in the ccflags
  for flag in cflags_cc.strip().split():
    if flag == "-nostdinc++":
      flags.append(flag)
    elif flag.startswith('-isystem'):
      prefix, suffix = flag[:len('-isystem')], flag[len('-isystem'):]
      assert prefix == '-isystem'
      flags.append(prefix + os.path.normpath(BUILD_DIR_REL / suffix))

  flags.append(f'--sysroot={rel_sysroot}')

  with open('v8_gcmole.args', 'w') as f:
    f.write(' '.join(flags))


if __name__ == '__main__':
  main()