@@ -16,33 +16,49 @@ use thiserror::Error;
1616///
1717/// `BuildEnvironment` is responsible for configuring the execution environment to enable
1818/// Bear's command interception capabilities. It supports two main interception modes:
19- /// - **Wrapper mode**: Creates temporary wrapper executables and modifies PATH
20- /// - **Preload mode**: Uses LD_PRELOAD to inject a dynamic library for system call interception
21- ///
22- /// The environment includes all necessary environment variables and maintains any temporary
23- /// resources (like temporary directories) required for the interception to work properly .
19+ /// - **Wrapper mode**: Creates a new directory with copies of wrapper executables that
20+ /// can intercept compiler executions, and manipulates the environment
21+ /// variables so that these executables have precedence.
22+ /// - **Preload mode**: Changes the environment variables to inject a dynamic library
23+ /// for system call interception .
2424pub struct BuildEnvironment {
2525 environment : HashMap < String , String > ,
2626 _temp_dir : Option < TempDir > ,
2727}
2828
2929impl BuildEnvironment {
3030 /// Creates a new `BuildEnvironment` configured for the specified interception method.
31- ///
32- /// This method sets up the execution environment based on the provided configuration
33- /// and establishes communication with the Bear process via the specified socket address.
34- ///
35- /// # Arguments
36- ///
37- /// * `config` - The interception configuration specifying the method and parameters
38- /// * `address` - The socket address where the Bear process is listening for intercepted commands
39- ///
40- /// # Returns
41- ///
42- /// Returns `Ok(BuildEnvironment)` on success, or `Err(ConfigurationError)` if:
43- /// - The configuration is invalid (empty paths, missing executables, etc.)
44- /// - IO operations fail (creating temp directories, hard links, etc.)
45- /// - Environment variable manipulation fails
31+ ///
32+ /// In both preload and wrapper mode, the interceptor will report the execution via
33+ /// TCP sockets. The interceptor is expected to connect to this process, and the
34+ /// address is advertised via a specific environment variable.
35+ ///
36+ /// The preload mode requires altering one of the environment variables that the OS
37+ /// dynamic linker reads and loads the shared library into the memory of the executed
38+ /// process. This will result in all dynamically linked executables reporting back, but
39+ /// post processing will filter the relevant compiler executions.
40+ ///
41+ /// The wrapper mode is slightly more complicated. We create a temporary directory
42+ /// and insert copies of the wrapper executable. To decide how to name the executables
43+ /// in the directory, we consider the config file, but also the current environment
44+ /// variables. Once these are set up, we alter the `PATH` environment variable to ensure the
45+ /// wrappers are executed instead of the real compilers. To instruct the wrappers which
46+ /// executables to call, we also create a JSON file.
47+ ///
48+ /// The wrapper mode reads the current environment and looks for variables that might
49+ /// instruct the build process about the compiler locations. Good candidates for such
50+ /// variables are: `CC`, `CXX`, `LD`, `CPP`, etc.
51+ ///
52+ /// An executed wrapper reports the execution and executes the intended process. To
53+ /// ensure that the wrapper calls the right process, it reads the JSON file from the
54+ /// wrapper directory, which contains a map of executable names and paths. Using the
55+ /// arguments (which contain the process name) it looks up the real executable.
56+ ///
57+ /// Because this process does not execute the compilers, but the build process does,
58+ /// we need to alter the build process to use the wrapper executables. This is achieved
59+ /// by changing the `PATH` variable. But that has limitations; we need to redefine
60+ /// some of the environment variables mentioned above (`CC`, `CXX`, `LD`, `CPP`, etc.),
61+ /// allowing the user to use these variables in the build invocations.
4662 pub fn create (
4763 config : & config:: Intercept ,
4864 address : SocketAddr ,
0 commit comments