forked from facebook/buck2
/
buck2.rs
107 lines (91 loc) · 4.2 KB
/
buck2.rs
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/
#![feature(async_closure)]
// Plugins
#![cfg_attr(feature = "gazebo_lint", feature(plugin))]
#![cfg_attr(feature = "gazebo_lint", allow(deprecated))] // :(
#![cfg_attr(feature = "gazebo_lint", plugin(gazebo_lint))]
use std::fs;
use std::io;
use std::path::PathBuf;
use buck2::exec;
use buck2::panic;
use buck2::TracingLogFile;
use buck2_client_ctx::exit_result::ExitResult;
use buck2_core::fs::working_dir::WorkingDir;
use buck2_core::logging::init_tracing_for_writer;
use buck2_core::logging::LogConfigurationReloadHandle;
use fbinit::FacebookInit;
// fbcode likes to set its own allocator in fbcode.default_allocator
// So when we set our own allocator, buck build buck2 or buck2 build buck2 often breaks.
// Making jemalloc the default only when we do a cargo build.
#[cfg_attr(all(unix, not(fbcode_build), not(buck_oss_build)), global_allocator)]
#[cfg(all(unix, not(fbcode_build), not(buck_oss_build)), not(illumos))]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
fn init_logging(_fb: FacebookInit) -> anyhow::Result<Box<dyn LogConfigurationReloadHandle>> {
static ENV_TRACING_LOG_FILE_PATH: &str = "BUCK_LOG_TO_FILE_PATH";
let handle = match std::env::var_os(ENV_TRACING_LOG_FILE_PATH) {
Some(path) => {
let path = PathBuf::from(path);
// we set the writer to stderr first until later, when we have the logdir, set the
// tracing log sink to that file
fs::create_dir_all(&path)?;
let tracing_log = path.join("tracing_log");
let file = TracingLogFile::new(tracing_log)?;
init_tracing_for_writer(file)
}
_ => init_tracing_for_writer(io::stderr),
}?;
#[cfg(fbcode_build)]
{
use buck2_client_ctx::subscribers::should_upload_log;
use buck2_events::sink::scribe;
use gflags::GflagValue;
// There are two sources of log spew when building buck2 with Buck and linking against fbcode:
// 1. folly/logging/xlog, which can be configured via a special configuration string, which we use to
// log only critical-level logs. https://github.com/facebook/folly/blob/master/folly/logging/docs/Config.md
// 2. google log (glog), which is older but still used, which can configured using a flag at runtime.
//
// This first call handles the folly config.
logging::update_logging_config(_fb, "CRITICAL");
gflags::set_gflag_value(_fb, "minloglevel", GflagValue::U32(5))?;
gflags::set_gflag_value(_fb, "stderrthreshold", GflagValue::U32(5))?;
if !should_upload_log()? {
scribe::disable();
}
}
Ok(handle)
}
// When using a cargo build, some essential services (e.g. RE, scribe)
// fall back to slow paths that give terrible performance.
// Therefore, if we are using cargo, warn strongly.
fn check_cargo() {
if !cfg!(fbcode_build) && !buck2_core::is_open_source() {
eprintln!("=====================================================================");
eprintln!("WARNING: You are using Buck v2 compiled with `cargo`, not `buck`.");
eprintln!(" Some operations may go slower and logging may be impaired.");
eprintln!("=====================================================================");
eprintln!();
}
}
// As this main() is used as the entry point for the `buck daemon` command,
// it must be single-threaded. Commands that want to be multi-threaded/async
// will start up their own tokio runtime.
#[fbinit::main]
fn main(init: fbinit::FacebookInit) -> ! {
fn main_with_result(init: fbinit::FacebookInit) -> ExitResult {
panic::initialize(init)?;
check_cargo();
let log_reload_handle = init_logging(init)?;
let args = std::env::args().collect::<Vec<String>>();
let cwd = WorkingDir::current_dir()?;
exec(args, cwd, init, log_reload_handle, None)
}
main_with_result(init).report()
}