]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Rollup merge of #39604 - est31:i128_tests, r=alexcrichton
[rust.git] / src / bootstrap / config.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Serialized configuration of a build.
12 //!
13 //! This module implements parsing `config.mk` and `config.toml` configuration
14 //! files to tweak how the build runs.
15
16 use std::collections::HashMap;
17 use std::env;
18 use std::fs::File;
19 use std::io::prelude::*;
20 use std::path::PathBuf;
21 use std::process;
22
23 use num_cpus;
24 use rustc_serialize::Decodable;
25 use toml::{Parser, Decoder, Value};
26 use util::push_exe_path;
27
28 /// Global configuration for the entire build and/or bootstrap.
29 ///
30 /// This structure is derived from a combination of both `config.toml` and
31 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
32 /// is used all that much, so this is primarily filled out by `config.mk` which
33 /// is generated from `./configure`.
34 ///
35 /// Note that this structure is not decoded directly into, but rather it is
36 /// filled out from the decoded forms of the structs below. For documentation
37 /// each field, see the corresponding fields in
38 /// `src/bootstrap/config.toml.example`.
39 #[derive(Default)]
40 pub struct Config {
41     pub ccache: Option<String>,
42     pub ninja: bool,
43     pub verbose: usize,
44     pub submodules: bool,
45     pub compiler_docs: bool,
46     pub docs: bool,
47     pub vendor: bool,
48     pub target_config: HashMap<String, Target>,
49     pub full_bootstrap: bool,
50     pub extended: bool,
51     pub sanitizers: bool,
52
53     // llvm codegen options
54     pub llvm_assertions: bool,
55     pub llvm_optimize: bool,
56     pub llvm_release_debuginfo: bool,
57     pub llvm_version_check: bool,
58     pub llvm_static_stdcpp: bool,
59     pub llvm_link_shared: bool,
60     pub llvm_targets: Option<String>,
61
62     // rust codegen options
63     pub rust_optimize: bool,
64     pub rust_codegen_units: u32,
65     pub rust_debug_assertions: bool,
66     pub rust_debuginfo: bool,
67     pub rust_debuginfo_lines: bool,
68     pub rust_debuginfo_only_std: bool,
69     pub rust_rpath: bool,
70     pub rustc_default_linker: Option<String>,
71     pub rustc_default_ar: Option<String>,
72     pub rust_optimize_tests: bool,
73     pub rust_debuginfo_tests: bool,
74
75     pub build: String,
76     pub host: Vec<String>,
77     pub target: Vec<String>,
78     pub rustc: Option<PathBuf>,
79     pub cargo: Option<PathBuf>,
80     pub local_rebuild: bool,
81
82     // dist misc
83     pub dist_sign_folder: Option<PathBuf>,
84     pub dist_upload_addr: Option<String>,
85     pub dist_gpg_password_file: Option<PathBuf>,
86
87     // libstd features
88     pub debug_jemalloc: bool,
89     pub use_jemalloc: bool,
90     pub backtrace: bool, // support for RUST_BACKTRACE
91
92     // misc
93     pub channel: String,
94     pub quiet_tests: bool,
95     // Fallback musl-root for all targets
96     pub musl_root: Option<PathBuf>,
97     pub prefix: Option<PathBuf>,
98     pub docdir: Option<PathBuf>,
99     pub libdir: Option<PathBuf>,
100     pub libdir_relative: Option<PathBuf>,
101     pub mandir: Option<PathBuf>,
102     pub codegen_tests: bool,
103     pub nodejs: Option<PathBuf>,
104     pub gdb: Option<PathBuf>,
105     pub python: Option<PathBuf>,
106     pub configure_args: Vec<String>,
107 }
108
109 /// Per-target configuration stored in the global configuration structure.
110 #[derive(Default)]
111 pub struct Target {
112     pub llvm_config: Option<PathBuf>,
113     pub jemalloc: Option<PathBuf>,
114     pub cc: Option<PathBuf>,
115     pub cxx: Option<PathBuf>,
116     pub ndk: Option<PathBuf>,
117     pub musl_root: Option<PathBuf>,
118     pub qemu_rootfs: Option<PathBuf>,
119 }
120
121 /// Structure of the `config.toml` file that configuration is read from.
122 ///
123 /// This structure uses `Decodable` to automatically decode a TOML configuration
124 /// file into this format, and then this is traversed and written into the above
125 /// `Config` structure.
126 #[derive(RustcDecodable, Default)]
127 struct TomlConfig {
128     build: Option<Build>,
129     install: Option<Install>,
130     llvm: Option<Llvm>,
131     rust: Option<Rust>,
132     target: Option<HashMap<String, TomlTarget>>,
133     dist: Option<Dist>,
134 }
135
136 /// TOML representation of various global build decisions.
137 #[derive(RustcDecodable, Default, Clone)]
138 struct Build {
139     build: Option<String>,
140     host: Vec<String>,
141     target: Vec<String>,
142     cargo: Option<String>,
143     rustc: Option<String>,
144     compiler_docs: Option<bool>,
145     docs: Option<bool>,
146     submodules: Option<bool>,
147     gdb: Option<String>,
148     vendor: Option<bool>,
149     nodejs: Option<String>,
150     python: Option<String>,
151     full_bootstrap: Option<bool>,
152     extended: Option<bool>,
153     sanitizers: Option<bool>,
154 }
155
156 /// TOML representation of various global install decisions.
157 #[derive(RustcDecodable, Default, Clone)]
158 struct Install {
159     prefix: Option<String>,
160     mandir: Option<String>,
161     docdir: Option<String>,
162     libdir: Option<String>,
163 }
164
165 /// TOML representation of how the LLVM build is configured.
166 #[derive(RustcDecodable, Default)]
167 struct Llvm {
168     ccache: Option<StringOrBool>,
169     ninja: Option<bool>,
170     assertions: Option<bool>,
171     optimize: Option<bool>,
172     release_debuginfo: Option<bool>,
173     version_check: Option<bool>,
174     static_libstdcpp: Option<bool>,
175     targets: Option<String>,
176 }
177
178 #[derive(RustcDecodable, Default, Clone)]
179 struct Dist {
180     sign_folder: Option<String>,
181     gpg_password_file: Option<String>,
182     upload_addr: Option<String>,
183 }
184
185 #[derive(RustcDecodable)]
186 enum StringOrBool {
187     String(String),
188     Bool(bool),
189 }
190
191 impl Default for StringOrBool {
192     fn default() -> StringOrBool {
193         StringOrBool::Bool(false)
194     }
195 }
196
197 /// TOML representation of how the Rust build is configured.
198 #[derive(RustcDecodable, Default)]
199 struct Rust {
200     optimize: Option<bool>,
201     codegen_units: Option<u32>,
202     debug_assertions: Option<bool>,
203     debuginfo: Option<bool>,
204     debuginfo_lines: Option<bool>,
205     debuginfo_only_std: Option<bool>,
206     debug_jemalloc: Option<bool>,
207     use_jemalloc: Option<bool>,
208     backtrace: Option<bool>,
209     default_linker: Option<String>,
210     default_ar: Option<String>,
211     channel: Option<String>,
212     musl_root: Option<String>,
213     rpath: Option<bool>,
214     optimize_tests: Option<bool>,
215     debuginfo_tests: Option<bool>,
216     codegen_tests: Option<bool>,
217 }
218
219 /// TOML representation of how each build target is configured.
220 #[derive(RustcDecodable, Default)]
221 struct TomlTarget {
222     llvm_config: Option<String>,
223     jemalloc: Option<String>,
224     cc: Option<String>,
225     cxx: Option<String>,
226     android_ndk: Option<String>,
227     musl_root: Option<String>,
228     qemu_rootfs: Option<String>,
229 }
230
231 impl Config {
232     pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
233         let mut config = Config::default();
234         config.llvm_optimize = true;
235         config.use_jemalloc = true;
236         config.backtrace = true;
237         config.rust_optimize = true;
238         config.rust_optimize_tests = true;
239         config.submodules = true;
240         config.docs = true;
241         config.rust_rpath = true;
242         config.rust_codegen_units = 1;
243         config.build = build.to_string();
244         config.channel = "dev".to_string();
245         config.codegen_tests = true;
246
247         let toml = file.map(|file| {
248             let mut f = t!(File::open(&file));
249             let mut toml = String::new();
250             t!(f.read_to_string(&mut toml));
251             let mut p = Parser::new(&toml);
252             let table = match p.parse() {
253                 Some(table) => table,
254                 None => {
255                     println!("failed to parse TOML configuration:");
256                     for err in p.errors.iter() {
257                         let (loline, locol) = p.to_linecol(err.lo);
258                         let (hiline, hicol) = p.to_linecol(err.hi);
259                         println!("{}:{}-{}:{}: {}", loline, locol, hiline,
260                                  hicol, err.desc);
261                     }
262                     process::exit(2);
263                 }
264             };
265             let mut d = Decoder::new(Value::Table(table));
266             match Decodable::decode(&mut d) {
267                 Ok(cfg) => cfg,
268                 Err(e) => {
269                     println!("failed to decode TOML: {}", e);
270                     process::exit(2);
271                 }
272             }
273         }).unwrap_or_else(|| TomlConfig::default());
274
275         let build = toml.build.clone().unwrap_or(Build::default());
276         set(&mut config.build, build.build.clone());
277         config.host.push(config.build.clone());
278         for host in build.host.iter() {
279             if !config.host.contains(host) {
280                 config.host.push(host.clone());
281             }
282         }
283         for target in config.host.iter().chain(&build.target) {
284             if !config.target.contains(target) {
285                 config.target.push(target.clone());
286             }
287         }
288         config.rustc = build.rustc.map(PathBuf::from);
289         config.cargo = build.cargo.map(PathBuf::from);
290         config.nodejs = build.nodejs.map(PathBuf::from);
291         config.gdb = build.gdb.map(PathBuf::from);
292         config.python = build.python.map(PathBuf::from);
293         set(&mut config.compiler_docs, build.compiler_docs);
294         set(&mut config.docs, build.docs);
295         set(&mut config.submodules, build.submodules);
296         set(&mut config.vendor, build.vendor);
297         set(&mut config.full_bootstrap, build.full_bootstrap);
298         set(&mut config.extended, build.extended);
299         set(&mut config.sanitizers, build.sanitizers);
300
301         if let Some(ref install) = toml.install {
302             config.prefix = install.prefix.clone().map(PathBuf::from);
303             config.mandir = install.mandir.clone().map(PathBuf::from);
304             config.docdir = install.docdir.clone().map(PathBuf::from);
305             config.libdir = install.libdir.clone().map(PathBuf::from);
306         }
307
308         if let Some(ref llvm) = toml.llvm {
309             match llvm.ccache {
310                 Some(StringOrBool::String(ref s)) => {
311                     config.ccache = Some(s.to_string())
312                 }
313                 Some(StringOrBool::Bool(true)) => {
314                     config.ccache = Some("ccache".to_string());
315                 }
316                 Some(StringOrBool::Bool(false)) | None => {}
317             }
318             set(&mut config.ninja, llvm.ninja);
319             set(&mut config.llvm_assertions, llvm.assertions);
320             set(&mut config.llvm_optimize, llvm.optimize);
321             set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
322             set(&mut config.llvm_version_check, llvm.version_check);
323             set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
324             config.llvm_targets = llvm.targets.clone();
325         }
326
327         if let Some(ref rust) = toml.rust {
328             set(&mut config.rust_debug_assertions, rust.debug_assertions);
329             set(&mut config.rust_debuginfo, rust.debuginfo);
330             set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
331             set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
332             set(&mut config.rust_optimize, rust.optimize);
333             set(&mut config.rust_optimize_tests, rust.optimize_tests);
334             set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
335             set(&mut config.codegen_tests, rust.codegen_tests);
336             set(&mut config.rust_rpath, rust.rpath);
337             set(&mut config.debug_jemalloc, rust.debug_jemalloc);
338             set(&mut config.use_jemalloc, rust.use_jemalloc);
339             set(&mut config.backtrace, rust.backtrace);
340             set(&mut config.channel, rust.channel.clone());
341             config.rustc_default_linker = rust.default_linker.clone();
342             config.rustc_default_ar = rust.default_ar.clone();
343             config.musl_root = rust.musl_root.clone().map(PathBuf::from);
344
345             match rust.codegen_units {
346                 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
347                 Some(n) => config.rust_codegen_units = n,
348                 None => {}
349             }
350         }
351
352         if let Some(ref t) = toml.target {
353             for (triple, cfg) in t {
354                 let mut target = Target::default();
355
356                 if let Some(ref s) = cfg.llvm_config {
357                     target.llvm_config = Some(env::current_dir().unwrap().join(s));
358                 }
359                 if let Some(ref s) = cfg.jemalloc {
360                     target.jemalloc = Some(env::current_dir().unwrap().join(s));
361                 }
362                 if let Some(ref s) = cfg.android_ndk {
363                     target.ndk = Some(env::current_dir().unwrap().join(s));
364                 }
365                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
366                 target.cc = cfg.cc.clone().map(PathBuf::from);
367                 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
368                 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
369
370                 config.target_config.insert(triple.clone(), target);
371             }
372         }
373
374         if let Some(ref t) = toml.dist {
375             config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
376             config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
377             config.dist_upload_addr = t.upload_addr.clone();
378         }
379
380         return config
381     }
382
383     /// "Temporary" routine to parse `config.mk` into this configuration.
384     ///
385     /// While we still have `./configure` this implements the ability to decode
386     /// that configuration into this. This isn't exactly a full-blown makefile
387     /// parser, but hey it gets the job done!
388     pub fn update_with_config_mk(&mut self) {
389         let mut config = String::new();
390         File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
391         for line in config.lines() {
392             let mut parts = line.splitn(2, ":=").map(|s| s.trim());
393             let key = parts.next().unwrap();
394             let value = match parts.next() {
395                 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
396                 Some(n) => n,
397                 None => continue
398             };
399
400             macro_rules! check {
401                 ($(($name:expr, $val:expr),)*) => {
402                     if value == "1" {
403                         $(
404                             if key == concat!("CFG_ENABLE_", $name) {
405                                 $val = true;
406                                 continue
407                             }
408                             if key == concat!("CFG_DISABLE_", $name) {
409                                 $val = false;
410                                 continue
411                             }
412                         )*
413                     }
414                 }
415             }
416
417             check! {
418                 ("MANAGE_SUBMODULES", self.submodules),
419                 ("COMPILER_DOCS", self.compiler_docs),
420                 ("DOCS", self.docs),
421                 ("LLVM_ASSERTIONS", self.llvm_assertions),
422                 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
423                 ("OPTIMIZE_LLVM", self.llvm_optimize),
424                 ("LLVM_VERSION_CHECK", self.llvm_version_check),
425                 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
426                 ("LLVM_LINK_SHARED", self.llvm_link_shared),
427                 ("OPTIMIZE", self.rust_optimize),
428                 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
429                 ("DEBUGINFO", self.rust_debuginfo),
430                 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
431                 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
432                 ("JEMALLOC", self.use_jemalloc),
433                 ("DEBUG_JEMALLOC", self.debug_jemalloc),
434                 ("RPATH", self.rust_rpath),
435                 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
436                 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
437                 ("QUIET_TESTS", self.quiet_tests),
438                 ("LOCAL_REBUILD", self.local_rebuild),
439                 ("NINJA", self.ninja),
440                 ("CODEGEN_TESTS", self.codegen_tests),
441                 ("VENDOR", self.vendor),
442                 ("FULL_BOOTSTRAP", self.full_bootstrap),
443                 ("EXTENDED", self.extended),
444                 ("SANITIZERS", self.sanitizers),
445             }
446
447             match key {
448                 "CFG_BUILD" => self.build = value.to_string(),
449                 "CFG_HOST" => {
450                     self.host = value.split(" ").map(|s| s.to_string())
451                                      .collect();
452                 }
453                 "CFG_TARGET" => {
454                     self.target = value.split(" ").map(|s| s.to_string())
455                                        .collect();
456                 }
457                 "CFG_MUSL_ROOT" if value.len() > 0 => {
458                     self.musl_root = Some(parse_configure_path(value));
459                 }
460                 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
461                     let target = "x86_64-unknown-linux-musl".to_string();
462                     let target = self.target_config.entry(target)
463                                      .or_insert(Target::default());
464                     target.musl_root = Some(parse_configure_path(value));
465                 }
466                 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
467                     let target = "i686-unknown-linux-musl".to_string();
468                     let target = self.target_config.entry(target)
469                                      .or_insert(Target::default());
470                     target.musl_root = Some(parse_configure_path(value));
471                 }
472                 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
473                     let target = "arm-unknown-linux-musleabi".to_string();
474                     let target = self.target_config.entry(target)
475                                      .or_insert(Target::default());
476                     target.musl_root = Some(parse_configure_path(value));
477                 }
478                 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
479                     let target = "arm-unknown-linux-musleabihf".to_string();
480                     let target = self.target_config.entry(target)
481                                      .or_insert(Target::default());
482                     target.musl_root = Some(parse_configure_path(value));
483                 }
484                 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
485                     let target = "armv7-unknown-linux-musleabihf".to_string();
486                     let target = self.target_config.entry(target)
487                                      .or_insert(Target::default());
488                     target.musl_root = Some(parse_configure_path(value));
489                 }
490                 "CFG_DEFAULT_AR" if value.len() > 0 => {
491                     self.rustc_default_ar = Some(value.to_string());
492                 }
493                 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
494                     self.rustc_default_linker = Some(value.to_string());
495                 }
496                 "CFG_GDB" if value.len() > 0 => {
497                     self.gdb = Some(parse_configure_path(value));
498                 }
499                 "CFG_RELEASE_CHANNEL" => {
500                     self.channel = value.to_string();
501                 }
502                 "CFG_PREFIX" => {
503                     self.prefix = Some(PathBuf::from(value));
504                 }
505                 "CFG_DOCDIR" => {
506                     self.docdir = Some(PathBuf::from(value));
507                 }
508                 "CFG_LIBDIR" => {
509                     self.libdir = Some(PathBuf::from(value));
510                 }
511                 "CFG_LIBDIR_RELATIVE" => {
512                     self.libdir_relative = Some(PathBuf::from(value));
513                 }
514                 "CFG_MANDIR" => {
515                     self.mandir = Some(PathBuf::from(value));
516                 }
517                 "CFG_LLVM_ROOT" if value.len() > 0 => {
518                     let target = self.target_config.entry(self.build.clone())
519                                      .or_insert(Target::default());
520                     let root = parse_configure_path(value);
521                     target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
522                 }
523                 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
524                     let target = self.target_config.entry(self.build.clone())
525                                      .or_insert(Target::default());
526                     target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
527                 }
528                 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
529                     let target = "arm-linux-androideabi".to_string();
530                     let target = self.target_config.entry(target)
531                                      .or_insert(Target::default());
532                     target.ndk = Some(parse_configure_path(value));
533                 }
534                 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
535                     let target = "armv7-linux-androideabi".to_string();
536                     let target = self.target_config.entry(target)
537                                      .or_insert(Target::default());
538                     target.ndk = Some(parse_configure_path(value));
539                 }
540                 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
541                     let target = "i686-linux-android".to_string();
542                     let target = self.target_config.entry(target)
543                                      .or_insert(Target::default());
544                     target.ndk = Some(parse_configure_path(value));
545                 }
546                 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
547                     let target = "aarch64-linux-android".to_string();
548                     let target = self.target_config.entry(target)
549                                      .or_insert(Target::default());
550                     target.ndk = Some(parse_configure_path(value));
551                 }
552                 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
553                     let path = parse_configure_path(value);
554                     self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
555                     self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
556                 }
557                 "CFG_PYTHON" if value.len() > 0 => {
558                     let path = parse_configure_path(value);
559                     self.python = Some(path);
560                 }
561                 "CFG_ENABLE_CCACHE" if value == "1" => {
562                     self.ccache = Some("ccache".to_string());
563                 }
564                 "CFG_ENABLE_SCCACHE" if value == "1" => {
565                     self.ccache = Some("sccache".to_string());
566                 }
567                 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
568                     self.configure_args = value.split_whitespace()
569                                                .map(|s| s.to_string())
570                                                .collect();
571                 }
572                 "CFG_QEMU_ARMHF_ROOTFS" if value.len() > 0 => {
573                     let target = "arm-unknown-linux-gnueabihf".to_string();
574                     let target = self.target_config.entry(target)
575                                      .or_insert(Target::default());
576                     target.qemu_rootfs = Some(parse_configure_path(value));
577                 }
578                 _ => {}
579             }
580         }
581     }
582
583     pub fn verbose(&self) -> bool {
584         self.verbose > 0
585     }
586
587     pub fn very_verbose(&self) -> bool {
588         self.verbose > 1
589     }
590 }
591
592 #[cfg(not(windows))]
593 fn parse_configure_path(path: &str) -> PathBuf {
594     path.into()
595 }
596
597 #[cfg(windows)]
598 fn parse_configure_path(path: &str) -> PathBuf {
599     // on windows, configure produces unix style paths e.g. /c/some/path but we
600     // only want real windows paths
601
602     use std::process::Command;
603     use build_helper;
604
605     // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
606     if !path.contains('/') {
607         return path.into();
608     }
609
610     let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
611     let win_path = win_path.trim();
612
613     win_path.into()
614 }
615
616 fn set<T>(field: &mut T, val: Option<T>) {
617     if let Some(v) = val {
618         *field = v;
619     }
620 }