Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 5330d1f3bb4ff9d6907a566c3f27d939927cf64c [file] [log] [blame]
Lukasz Anforowicza394bbd2022-06-21 18:21:061#!/usr/bin/env python3
Avi Drissmandfd880852022-09-15 20:11:092# Copyright 2022 The Chromium Authors
Lukasz Anforowicza394bbd2022-06-21 18:21:063# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5'''Builds the Crubit tool.
6
danakj9a318f62024-10-01 17:56:397Builds the Crubit tools for generating Rust/C++ bindings.
Lukasz Anforowicza394bbd2022-06-21 18:21:068
danakj9a318f62024-10-01 17:56:399This script must be run after //tools/rust/build_rust.py as it uses the outputs
10of that script in the compilation of Crubit. It uses:
11- The LLVM and Clang libraries and headers in `RUST_HOST_LLVM_INSTALL_DIR`.
12- The rust toolchain binaries and libraries in `RUST_TOOLCHAIN_OUT_DIR`.
13
14This script:
15- Clones the Abseil repository, checks out a defined revision.
16- Builds Abseil with Cmake.
17- Clones the Crubit repository, checks out a defined revision.
18- Builds Crubit's rs_bindings_from_cc with Cargo.
19- Adds rs_bindings_from_cc and the Crubit support libraries into the
20 toolchain package in `RUST_TOOLCHAIN_OUT_DIR`.
21
22The cc_bindings_from_rs binary is not yet built, as there's no Cargo rules to build it yet.
Lukasz Anforowicza394bbd2022-06-21 18:21:0623'''
24
25import argparse
Lukasz Anforowicza394bbd2022-06-21 18:21:0626import os
danakjfd4dee72023-01-05 14:42:2427import platform
Lukasz Anforowicza394bbd2022-06-21 18:21:0628import shutil
Lukasz Anforowicza394bbd2022-06-21 18:21:0629import sys
30
31from pathlib import Path
32
33# Get variables and helpers from Clang update script
34sys.path.append(
35 os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'clang',
36 'scripts'))
37
danakj9a318f62024-10-01 17:56:3938from build import (AddCMakeToPath, AddZlibToPath, CheckoutGitRepo,
39 DownloadDebianSysroot, RunCommand, THIRD_PARTY_DIR)
40from update import (RmTree)
Lukasz Anforowicza394bbd2022-06-21 18:21:0641
danakj9a318f62024-10-01 17:56:3942from build_rust import (RUST_HOST_LLVM_INSTALL_DIR)
43from update_rust import (CHROMIUM_DIR, ABSL_REVISION, CRUBIT_REVISION,
44 RUST_TOOLCHAIN_OUT_DIR)
Lukasz Anforowicza394bbd2022-06-21 18:21:0645
danakj9a318f62024-10-01 17:56:3946ABSL_GIT = 'https://github.com/abseil/abseil-cpp'
47CRUBIT_GIT = 'https://github.com/google/crubit'
48
49ABSL_SRC_DIR = os.path.join(CHROMIUM_DIR, 'third_party',
50 'rust-toolchain-intermediate', 'absl')
51ABSL_INSTALL_DIR = os.path.join(ABSL_SRC_DIR, 'install')
52CRUBIT_SRC_DIR = os.path.join(CHROMIUM_DIR, 'third_party',
53 'rust-toolchain-intermediate', 'crubit')
54
55EXE = '.exe' if sys.platform == 'win32' else ''
Lukasz Anforowicza394bbd2022-06-21 18:21:0656
57
danakj9a318f62024-10-01 17:56:3958def BuildAbsl(env, debug):
59 os.chdir(ABSL_SRC_DIR)
danakjfd4dee72023-01-05 14:42:2460
danakj9a318f62024-10-01 17:56:3961 configure_cmd = [
62 'cmake',
63 '-B',
64 'out',
65 '-GNinja',
66 # Because Crubit is built with C++20.
67 '-DCMAKE_CXX_STANDARD=20',
68 f'-DCMAKE_INSTALL_PREFIX={ABSL_INSTALL_DIR}',
69 '-DABSL_PROPAGATE_CXX_STD=ON',
70 '-DABSL_BUILD_TESTING=OFF',
71 '-DABSL_USE_GOOGLETEST_HEAD=OFF',
72 # LLVM is built with static CRT. Make Abseil match it.
73 '-DABSL_MSVC_STATIC_RUNTIME=ON',
Lukasz Anforowicza394bbd2022-06-21 18:21:0674 ]
danakj9a318f62024-10-01 17:56:3975 if not debug:
76 configure_cmd.append('-DCMAKE_BUILD_TYPE=Release')
Lukasz Anforowicza394bbd2022-06-21 18:21:0677
danakj9a318f62024-10-01 17:56:3978 RunCommand(configure_cmd, setenv=True, env=env)
79 build_cmd = ['cmake', '--build', 'out', '--target', 'all']
80 RunCommand(build_cmd, setenv=True, env=env)
81 install_cmd = ['cmake', '--install', 'out']
82 RunCommand(install_cmd, setenv=True, env=env)
Lukasz Anforowicz17a23392022-07-06 17:26:1883
danakj9a318f62024-10-01 17:56:3984 os.chdir(CHROMIUM_DIR)
Lukasz Anforowicza394bbd2022-06-21 18:21:0685
86
danakj9a318f62024-10-01 17:56:3987def BuildCrubit(env, debug):
88 os.chdir(CRUBIT_SRC_DIR)
Lukasz Anforowicz401dc9c2022-10-20 18:45:0089
danakj9a318f62024-10-01 17:56:3990 CRUBIT_BINS = ['rs_bindings_from_cc']
Lukasz Anforowicz401dc9c2022-10-20 18:45:0091
danakj9a318f62024-10-01 17:56:3992 build_cmd = ['cargo', 'build']
93 for bin in CRUBIT_BINS:
94 build_cmd += ['--bin', bin]
95 if not debug:
96 build_cmd.append('--release')
97 RunCommand(build_cmd, setenv=True, env=env)
Lukasz Anforowicz401dc9c2022-10-20 18:45:0098
danakj9a318f62024-10-01 17:56:3999 print(f'Installing Crubit to {RUST_TOOLCHAIN_OUT_DIR} ...')
100 target_dir = os.path.join(CRUBIT_SRC_DIR, 'target',
101 'debug' if debug else 'release')
102 for bin in CRUBIT_BINS:
103 bin = bin + EXE
104 shutil.copy(os.path.join(target_dir, bin),
105 os.path.join(RUST_TOOLCHAIN_OUT_DIR, 'bin', bin))
Lukasz Anforowicz401dc9c2022-10-20 18:45:00106
danakj9a318f62024-10-01 17:56:39107 support_build_dir = os.path.join(CRUBIT_SRC_DIR, 'support')
108 support_out_dir = os.path.join(RUST_TOOLCHAIN_OUT_DIR, 'lib', 'crubit')
109 if os.path.exists(support_out_dir):
110 RmTree(support_out_dir)
111 shutil.copytree(support_build_dir, support_out_dir)
Lukasz Anforowicz401dc9c2022-10-20 18:45:00112
danakj9a318f62024-10-01 17:56:39113 os.chdir(CHROMIUM_DIR)
Lukasz Anforowicz401dc9c2022-10-20 18:45:00114
115
Lukasz Anforowicza394bbd2022-06-21 18:21:06116def main():
117 parser = argparse.ArgumentParser(
118 description='Build and package Crubit tools')
Lukasz Anforowicz401dc9c2022-10-20 18:45:00119 parser.add_argument(
Lukasz Anforowicza394bbd2022-06-21 18:21:06120 '--skip-checkout',
121 action='store_true',
danakj9a318f62024-10-01 17:56:39122 help=('skip checking out source code. Useful for trying local'
123 'changes'))
124 parser.add_argument('--debug',
125 action='store_true',
126 help=('build Crubit in debug mode'))
Lukasz Anforowicza394bbd2022-06-21 18:21:06127 args, rest = parser.parse_known_args()
danakj9a318f62024-10-01 17:56:39128 assert (not rest)
Lukasz Anforowicza394bbd2022-06-21 18:21:06129
130 if not args.skip_checkout:
danakj9a318f62024-10-01 17:56:39131 CheckoutGitRepo("absl", ABSL_GIT, ABSL_REVISION, ABSL_SRC_DIR)
132 CheckoutGitRepo("crubit", CRUBIT_GIT, CRUBIT_REVISION, CRUBIT_SRC_DIR)
133 if sys.platform.startswith('linux'):
134 arch = 'arm64' if platform.machine() == 'aarch64' else 'amd64'
135 sysroot = DownloadDebianSysroot(arch, args.skip_checkout)
Lukasz Anforowicza394bbd2022-06-21 18:21:06136
danakj9a318f62024-10-01 17:56:39137 llvm_bin_dir = os.path.join(RUST_HOST_LLVM_INSTALL_DIR, 'bin')
138 rust_bin_dir = os.path.join(RUST_TOOLCHAIN_OUT_DIR, 'bin')
Lukasz Anforowicz401dc9c2022-10-20 18:45:00139
danakj9a318f62024-10-01 17:56:39140 AddCMakeToPath()
Lukasz Anforowicz401dc9c2022-10-20 18:45:00141
danakj9a318f62024-10-01 17:56:39142 env = os.environ
143
144 path_trailing_sep = os.pathsep if env['PATH'] else ''
145 env['PATH'] = (f'{llvm_bin_dir}{os.pathsep}'
146 f'{rust_bin_dir}{path_trailing_sep}'
147 f'{env["PATH"]}')
148
149 if sys.platform == 'win32':
150 # CMake on Windows doesn't like depot_tools's ninja.bat wrapper.
151 ninja_dir = os.path.join(THIRD_PARTY_DIR, 'ninja')
152 env['PATH'] = f'{ninja_dir}{os.pathsep}{env["PATH"]}'
153
154 env['CXXFLAGS'] = ''
155 env['RUSTFLAGS'] = ''
156
157 if sys.platform == 'win32':
158 env['CC'] = 'clang-cl'
159 env['CXX'] = 'clang-cl'
160 else:
161 env['CC'] = 'clang'
162 env['CXX'] = 'clang++'
163
164 # We link with lld via clang, except on windows where we point to lld-link
165 # directly.
166 if sys.platform == 'win32':
167 env['RUSTFLAGS'] += f' -Clinker=lld-link'
168 else:
169 env['RUSTFLAGS'] += f' -Clinker=clang'
170 env['RUSTFLAGS'] += f' -Clink-arg=-fuse-ld=lld'
171
172 if sys.platform == 'win32':
173 # LLVM is built with static CRT. Make Rust match it.
174 env['RUSTFLAGS'] += f' -Ctarget-feature=+crt-static'
175
176 if sys.platform.startswith('linux'):
177 sysroot_flag = (f'--sysroot={sysroot}' if sysroot else '')
178 env['CXXFLAGS'] += f" {sysroot_flag}"
179 env['RUSTFLAGS'] += f" -Clink-arg={sysroot_flag}"
180
181 if sys.platform == 'darwin':
182 import subprocess
183 # The system/xcode compiler would find system SDK correctly, but
184 # the Clang we've built does not. See
185 # https://github.com/llvm/llvm-project/issues/45225
186 sdk_path = subprocess.check_output(['xcrun', '--show-sdk-path'],
187 text=True).rstrip()
188 env['CXXFLAGS'] += f' -isysroot {sdk_path}'
189 env['RUSTFLAGS'] += f' -Clink-arg=-isysroot -Clink-arg={sdk_path}'
190
191 if sys.platform == 'win32':
192 # LLVM depends on Zlib.
193 zlib_dir = AddZlibToPath(dry_run=args.skip_checkout)
194 env['CXXFLAGS'] += f' /I{zlib_dir}'
195 env['RUSTFLAGS'] += f' -Clink-arg=/LIBPATH:{zlib_dir}'
196 # Prevent deprecation warnings.
197 env['CXXFLAGS'] += ' /D_CRT_SECURE_NO_DEPRECATE'
198
199 BuildAbsl(env, args.debug)
200
201 env['ABSL_INCLUDE_PATH'] = os.path.join(ABSL_INSTALL_DIR, 'include')
202 env['ABSL_LIB_STATIC_PATH'] = os.path.join(ABSL_INSTALL_DIR, 'lib')
203 env['CLANG_INCLUDE_PATH'] = os.path.join(RUST_HOST_LLVM_INSTALL_DIR,
204 'include')
205 env['CLANG_LIB_STATIC_PATH'] = os.path.join(RUST_HOST_LLVM_INSTALL_DIR,
206 'lib')
207
208 BuildCrubit(env, args.debug)
Lukasz Anforowicza394bbd2022-06-21 18:21:06209
210 return 0
211
212
213if __name__ == '__main__':
214 sys.exit(main())