File: build_crubit.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (214 lines) | stat: -rwxr-xr-x 7,567 bytes parent folder | download | duplicates (4)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
'''Builds the Crubit tool.

Builds the Crubit tools for generating Rust/C++ bindings.

This script must be run after //tools/rust/build_rust.py as it uses the outputs
of that script in the compilation of Crubit. It uses:
- The LLVM and Clang libraries and headers in `RUST_HOST_LLVM_INSTALL_DIR`.
- The rust toolchain binaries and libraries in `RUST_TOOLCHAIN_OUT_DIR`.

This script:
- Clones the Abseil repository, checks out a defined revision.
- Builds Abseil with Cmake.
- Clones the Crubit repository, checks out a defined revision.
- Builds Crubit's rs_bindings_from_cc with Cargo.
- Adds rs_bindings_from_cc and the Crubit support libraries into the
  toolchain package in `RUST_TOOLCHAIN_OUT_DIR`.

The cc_bindings_from_rs binary is not yet built, as there's no Cargo rules to build it yet.
'''

import argparse
import os
import platform
import shutil
import sys

from pathlib import Path

# Get variables and helpers from Clang update script
sys.path.append(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'clang',
                 'scripts'))

from build import (AddCMakeToPath, AddZlibToPath, CheckoutGitRepo,
                   DownloadDebianSysroot, RunCommand, THIRD_PARTY_DIR)
from update import (RmTree)

from build_rust import (RUST_HOST_LLVM_INSTALL_DIR)
from update_rust import (CHROMIUM_DIR, ABSL_REVISION, CRUBIT_REVISION,
                         RUST_TOOLCHAIN_OUT_DIR)

ABSL_GIT = 'https://github.com/abseil/abseil-cpp'
CRUBIT_GIT = 'https://github.com/google/crubit'

ABSL_SRC_DIR = os.path.join(CHROMIUM_DIR, 'third_party',
                            'rust-toolchain-intermediate', 'absl')
ABSL_INSTALL_DIR = os.path.join(ABSL_SRC_DIR, 'install')
CRUBIT_SRC_DIR = os.path.join(CHROMIUM_DIR, 'third_party',
                              'rust-toolchain-intermediate', 'crubit')

EXE = '.exe' if sys.platform == 'win32' else ''


def BuildAbsl(env, debug):
    os.chdir(ABSL_SRC_DIR)

    configure_cmd = [
        'cmake',
        '-B',
        'out',
        '-GNinja',
        # Because Crubit is built with C++20.
        '-DCMAKE_CXX_STANDARD=20',
        f'-DCMAKE_INSTALL_PREFIX={ABSL_INSTALL_DIR}',
        '-DABSL_PROPAGATE_CXX_STD=ON',
        '-DABSL_BUILD_TESTING=OFF',
        '-DABSL_USE_GOOGLETEST_HEAD=OFF',
        # LLVM is built with static CRT. Make Abseil match it.
        '-DABSL_MSVC_STATIC_RUNTIME=ON',
    ]
    if not debug:
        configure_cmd.append('-DCMAKE_BUILD_TYPE=Release')

    RunCommand(configure_cmd, setenv=True, env=env)
    build_cmd = ['cmake', '--build', 'out', '--target', 'all']
    RunCommand(build_cmd, setenv=True, env=env)
    install_cmd = ['cmake', '--install', 'out']
    RunCommand(install_cmd, setenv=True, env=env)

    os.chdir(CHROMIUM_DIR)


def BuildCrubit(env, debug):
    os.chdir(CRUBIT_SRC_DIR)

    CRUBIT_BINS = ['rs_bindings_from_cc']

    build_cmd = ['cargo', 'build']
    for bin in CRUBIT_BINS:
        build_cmd += ['--bin', bin]
    if not debug:
        build_cmd.append('--release')
    RunCommand(build_cmd, setenv=True, env=env)

    print(f'Installing Crubit to {RUST_TOOLCHAIN_OUT_DIR} ...')
    target_dir = os.path.join(CRUBIT_SRC_DIR, 'target',
                              'debug' if debug else 'release')
    for bin in CRUBIT_BINS:
        bin = bin + EXE
        shutil.copy(os.path.join(target_dir, bin),
                    os.path.join(RUST_TOOLCHAIN_OUT_DIR, 'bin', bin))

    support_build_dir = os.path.join(CRUBIT_SRC_DIR, 'support')
    support_out_dir = os.path.join(RUST_TOOLCHAIN_OUT_DIR, 'lib', 'crubit')
    if os.path.exists(support_out_dir):
        RmTree(support_out_dir)
    shutil.copytree(support_build_dir, support_out_dir)

    os.chdir(CHROMIUM_DIR)


def main():
    parser = argparse.ArgumentParser(
        description='Build and package Crubit tools')
    parser.add_argument(
        '--skip-checkout',
        action='store_true',
        help=('skip checking out source code. Useful for trying local'
              'changes'))
    parser.add_argument('--debug',
                        action='store_true',
                        help=('build Crubit in debug mode'))
    args, rest = parser.parse_known_args()
    assert (not rest)

    if not args.skip_checkout:
        CheckoutGitRepo("absl", ABSL_GIT, ABSL_REVISION, ABSL_SRC_DIR)
        CheckoutGitRepo("crubit", CRUBIT_GIT, CRUBIT_REVISION, CRUBIT_SRC_DIR)
    if sys.platform.startswith('linux'):
        arch = 'arm64' if platform.machine() == 'aarch64' else 'amd64'
        sysroot = DownloadDebianSysroot(arch, args.skip_checkout)

    llvm_bin_dir = os.path.join(RUST_HOST_LLVM_INSTALL_DIR, 'bin')
    rust_bin_dir = os.path.join(RUST_TOOLCHAIN_OUT_DIR, 'bin')

    AddCMakeToPath()

    env = os.environ

    path_trailing_sep = os.pathsep if env['PATH'] else ''
    env['PATH'] = (f'{llvm_bin_dir}{os.pathsep}'
                   f'{rust_bin_dir}{path_trailing_sep}'
                   f'{env["PATH"]}')

    if sys.platform == 'win32':
        # CMake on Windows doesn't like depot_tools's ninja.bat wrapper.
        ninja_dir = os.path.join(THIRD_PARTY_DIR, 'ninja')
        env['PATH'] = f'{ninja_dir}{os.pathsep}{env["PATH"]}'

    env['CXXFLAGS'] = ''
    env['RUSTFLAGS'] = ''

    if sys.platform == 'win32':
        env['CC'] = 'clang-cl'
        env['CXX'] = 'clang-cl'
    else:
        env['CC'] = 'clang'
        env['CXX'] = 'clang++'

    # We link with lld via clang, except on windows where we point to lld-link
    # directly.
    if sys.platform == 'win32':
        env['RUSTFLAGS'] += f' -Clinker=lld-link'
    else:
        env['RUSTFLAGS'] += f' -Clinker=clang'
        env['RUSTFLAGS'] += f' -Clink-arg=-fuse-ld=lld'

    if sys.platform == 'win32':
        # LLVM is built with static CRT. Make Rust match it.
        env['RUSTFLAGS'] += f' -Ctarget-feature=+crt-static'

    if sys.platform.startswith('linux'):
        sysroot_flag = (f'--sysroot={sysroot}' if sysroot else '')
        env['CXXFLAGS'] += f" {sysroot_flag}"
        env['RUSTFLAGS'] += f" -Clink-arg={sysroot_flag}"

    if sys.platform == 'darwin':
        import subprocess
        # The system/xcode compiler would find system SDK correctly, but
        # the Clang we've built does not. See
        # https://github.com/llvm/llvm-project/issues/45225
        sdk_path = subprocess.check_output(['xcrun', '--show-sdk-path'],
                                           text=True).rstrip()
        env['CXXFLAGS'] += f' -isysroot {sdk_path}'
        env['RUSTFLAGS'] += f' -Clink-arg=-isysroot -Clink-arg={sdk_path}'

    if sys.platform == 'win32':
        # LLVM depends on Zlib.
        zlib_dir = AddZlibToPath(dry_run=args.skip_checkout)
        env['CXXFLAGS'] += f' /I{zlib_dir}'
        env['RUSTFLAGS'] += f' -Clink-arg=/LIBPATH:{zlib_dir}'
        # Prevent deprecation warnings.
        env['CXXFLAGS'] += ' /D_CRT_SECURE_NO_DEPRECATE'

    BuildAbsl(env, args.debug)

    env['ABSL_INCLUDE_PATH'] = os.path.join(ABSL_INSTALL_DIR, 'include')
    env['ABSL_LIB_STATIC_PATH'] = os.path.join(ABSL_INSTALL_DIR, 'lib')
    env['CLANG_INCLUDE_PATH'] = os.path.join(RUST_HOST_LLVM_INSTALL_DIR,
                                             'include')
    env['CLANG_LIB_STATIC_PATH'] = os.path.join(RUST_HOST_LLVM_INSTALL_DIR,
                                                'lib')

    BuildCrubit(env, args.debug)

    return 0


if __name__ == '__main__':
    sys.exit(main())