|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# pylint: disable=invalid-name |
| 3 | + |
| 4 | +from argparse import ArgumentParser, RawTextHelpFormatter |
| 5 | +from pathlib import Path |
| 6 | +import textwrap |
| 7 | +import time |
| 8 | + |
| 9 | +import tc_build.utils |
| 10 | + |
| 11 | +from tc_build.rust import RustBuilder, RustSourceManager |
| 12 | + |
| 13 | +# This is a known good revision of Rust for building the kernel |
| 14 | +GOOD_REVISION = '69b3959afec9b5468d5de15133b199553f6e55d2' |
| 15 | + |
| 16 | +parser = ArgumentParser(formatter_class=RawTextHelpFormatter) |
| 17 | +clone_options = parser.add_mutually_exclusive_group() |
| 18 | + |
| 19 | +parser.add_argument('--debug', |
| 20 | + help=textwrap.dedent('''\ |
| 21 | + Build a debug compiler and standard library. This enables debug assertions, |
| 22 | + debug logging, overflow checks and debug info. The debug assertions and overflow |
| 23 | + checks can help catch issues when compiling. |
| 24 | +
|
| 25 | + '''), |
| 26 | + action='store_true') |
| 27 | +parser.add_argument('-b', |
| 28 | + '--build-folder', |
| 29 | + help=textwrap.dedent('''\ |
| 30 | + By default, the script will create a "build/rust" folder in the same folder as this |
| 31 | + script and build each requested stage within that containing folder. To change the |
| 32 | + location of the containing build folder, pass it to this parameter. This can be either |
| 33 | + an absolute or relative path. If it is provided, then a custom LLVM install folder |
| 34 | + needs to be provided as well to prevent mistakes. |
| 35 | +
|
| 36 | + '''), |
| 37 | + type=str) |
| 38 | +parser.add_argument('-i', |
| 39 | + '--install-folder', |
| 40 | + help=textwrap.dedent('''\ |
| 41 | + By default, the script will leave the toolchain in its build folder. To install it |
| 42 | + outside the build folder for persistent use, pass the installation location that you |
| 43 | + desire to this parameter. This can be either an absolute or relative path. |
| 44 | +
|
| 45 | + '''), |
| 46 | + type=str) |
| 47 | +parser.add_argument('-l', |
| 48 | + '--llvm-install-folder', |
| 49 | + help=textwrap.dedent('''\ |
| 50 | + By default, the script will try to use a built LLVM by './build-llvm.py'. To use |
| 51 | + another LLVM installation (perhaps from './build-llvm.py --install-folder'), pass |
| 52 | + it to this parameter. |
| 53 | +
|
| 54 | + '''), |
| 55 | + type=str) |
| 56 | +parser.add_argument('-R', |
| 57 | + '--rust-folder', |
| 58 | + help=textwrap.dedent('''\ |
| 59 | + By default, the script will clone the Rust project into the tc-build repo. If you have |
| 60 | + another Rust checkout that you would like to work out of, pass it to this parameter. |
| 61 | + This can either be an absolute or relative path. Implies '--no-update'. When this |
| 62 | + option is supplied, '--ref' and '--use-good-revision' do nothing, as the script does |
| 63 | + not manipulate a repository it does not own. |
| 64 | +
|
| 65 | + '''), |
| 66 | + type=str) |
| 67 | +parser.add_argument('-n', |
| 68 | + '--no-update', |
| 69 | + help=textwrap.dedent('''\ |
| 70 | + By default, the script always updates the Rust repo before building. This prevents |
| 71 | + that, which can be helpful during something like bisecting or manually managing the |
| 72 | + repo to pin it to a particular revision. |
| 73 | +
|
| 74 | + '''), |
| 75 | + action='store_true') |
| 76 | +parser.add_argument('-r', |
| 77 | + '--ref', |
| 78 | + help=textwrap.dedent('''\ |
| 79 | + By default, the script builds the main branch (tip of tree) of Rust. If you would |
| 80 | + like to build an older branch, use this parameter. This may be helpful in tracking |
| 81 | + down an older bug to properly bisect. This value is just passed along to 'git checkout' |
| 82 | + so it can be a branch name, tag name, or hash. This will have no effect if |
| 83 | + '--rust-folder' is provided, as the script does not manipulate a repository that it |
| 84 | + does not own. |
| 85 | +
|
| 86 | + '''), |
| 87 | + default='master', |
| 88 | + type=str) |
| 89 | +parser.add_argument('--show-build-commands', |
| 90 | + help=textwrap.dedent('''\ |
| 91 | + By default, the script only shows the output of the comands it is running. When this option |
| 92 | + is enabled, the invocations of the build tools will be shown to help with reproducing |
| 93 | + issues outside of the script. |
| 94 | +
|
| 95 | + '''), |
| 96 | + action='store_true') |
| 97 | +clone_options.add_argument('--use-good-revision', |
| 98 | + help=textwrap.dedent('''\ |
| 99 | + By default, the script updates Rust to the latest tip of tree revision, which may at times be |
| 100 | + broken or not work right. With this option, it will checkout a known good revision of Rust |
| 101 | + that builds and works properly. If you use this option often, please remember to update the |
| 102 | + script as the known good revision will change. This option may work best with a matching good |
| 103 | + revision used to build LLVM by './build-llvm.py'. |
| 104 | +
|
| 105 | + '''), |
| 106 | + action='store_const', |
| 107 | + const=GOOD_REVISION, |
| 108 | + dest='ref') |
| 109 | +parser.add_argument('--vendor-string', |
| 110 | + help=textwrap.dedent('''\ |
| 111 | + Add this value to the Rust version string (like "rustc ... (ClangBuiltLinux)"). Useful when |
| 112 | + reverting or applying patches on top of upstream Rust to differentiate a toolchain built |
| 113 | + with this script from upstream Rust or to distinguish a toolchain built with this script |
| 114 | + from the system's Rust. Defaults to ClangBuiltLinux, can be set to an empty string to |
| 115 | + override this and have no vendor in the version string. |
| 116 | +
|
| 117 | + '''), |
| 118 | + type=str, |
| 119 | + default='ClangBuiltLinux') |
| 120 | +args = parser.parse_args() |
| 121 | + |
| 122 | +# Start tracking time that the script takes |
| 123 | +script_start = time.time() |
| 124 | + |
| 125 | +# Folder validation |
| 126 | +tc_build_folder = Path(__file__).resolve().parent |
| 127 | +src_folder = Path(tc_build_folder, 'src') |
| 128 | + |
| 129 | +if args.build_folder: |
| 130 | + build_folder = Path(args.build_folder).resolve() |
| 131 | + |
| 132 | + if not args.llvm_install_folder: |
| 133 | + raise RuntimeError( |
| 134 | + 'Build folder customized, but no custom LLVM install folder provided -- this is likely a mistake. Provide both if you want to build in a custom folder?' |
| 135 | + ) |
| 136 | +else: |
| 137 | + build_folder = Path(tc_build_folder, 'build/rust') |
| 138 | + |
| 139 | +if args.llvm_install_folder: |
| 140 | + llvm_install_folder = Path(args.llvm_install_folder).resolve() |
| 141 | +else: |
| 142 | + llvm_install_folder = Path(tc_build_folder, 'build/llvm/final') |
| 143 | + |
| 144 | +# Validate and configure Rust source |
| 145 | +if args.rust_folder: |
| 146 | + if not (rust_folder := Path(args.rust_folder).resolve()).exists(): |
| 147 | + raise RuntimeError(f"Provided Rust folder ('{args.rust_folder}') does not exist?") |
| 148 | +else: |
| 149 | + rust_folder = Path(src_folder, 'rust') |
| 150 | +rust_source = RustSourceManager(rust_folder) |
| 151 | +rust_source.download(args.ref) |
| 152 | +if not (args.rust_folder or args.no_update): |
| 153 | + rust_source.update(args.ref) |
| 154 | + |
| 155 | +# Build Rust |
| 156 | +tc_build.utils.print_header('Building Rust') |
| 157 | + |
| 158 | +# Final build |
| 159 | +final = RustBuilder() |
| 160 | +final.folders.source = rust_folder |
| 161 | +final.folders.build = Path(build_folder, 'final') |
| 162 | +final.folders.install = Path(args.install_folder).resolve() if args.install_folder else None |
| 163 | +final.llvm_install_folder = llvm_install_folder |
| 164 | +final.debug = args.debug |
| 165 | +final.vendor_string = args.vendor_string |
| 166 | +final.show_commands = args.show_build_commands |
| 167 | + |
| 168 | +final.configure() |
| 169 | +final.build() |
| 170 | +final.show_install_info() |
| 171 | + |
| 172 | +print(f"Script duration: {tc_build.utils.get_duration(script_start)}") |
0 commit comments