Skip to content

Commit

Permalink
fixme
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Apr 26, 2024
1 parent b21e289 commit 9256067
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 34 deletions.
2 changes: 1 addition & 1 deletion rust/intercept/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ path = "src/lib.rs"

[[bin]]
name = "intercept"
path = "src/main.rs"
path = "src/bin/intercept.rs"

[[bin]]
name = "wrapper"
Expand Down
115 changes: 115 additions & 0 deletions rust/intercept/src/bin/intercept.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* Copyright (C) 2012-2023 by László Nagy
This file is part of Bear.
Bear is a tool to generate compilation database for clang tooling.
Bear is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bear is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

extern crate core;

use std::io::Write;

use anyhow::Result;
use clap::{arg, ArgAction, command};
use crossbeam_channel::bounded;

use intercept::ipc::{Envelope, Event, ReporterId};
use intercept::collector::{EventCollector, EventCollectorOnTcp};

#[derive(Debug, PartialEq)]
struct Arguments {
command: Vec<String>,
output: String,
config: Option<String>,
verbose: u8,
}

impl Arguments {
fn parse() -> Result<Self> {
let matches = command!()
.args(&[
arg!(<COMMAND> "Build command")
.action(ArgAction::Append)
.value_terminator("--")
.multiple_values(true)
.last(true)
.required(true),
arg!(-o --output <FILE> "Path of the result file")
.default_value("events.json")
.hide_default_value(false),
arg!(-c --config <FILE> "Path of the config file"),
arg!(-v --verbose ... "Sets the level of verbosity")
.action(ArgAction::Count),
])
.get_matches();

Arguments {
command: matches.get_values_of("COMMAND")
.expect("command is required")
.map(String::to_string)
.collect(),
output: matches.get_one::<String>("output")
.expect("output is defaulted")
.clone(),
config: matches.get_one::<String>("config")
.map(String::to_string),
verbose: matches.get_count("verbose"),
}
}
}

fn run() -> Result<i32> {
let arguments = Arguments::parse()?;

let collector = EventCollectorOnTcp::new()?;
let destination = collector.address()?;

std::env::set_var("INTERCEPT_REPORT_DESTINATION", &destination.0);
std::env::set_var("INTERCEPT_VERBOSE", arguments.verbose.to_string());
let mut build = std::process::Command::new(arguments.command[0].clone())
.args(&arguments.command[1..])
.envs(std::env::vars())
.spawn()?;

let (sender, mut receiver) = bounded::<Envelope>(10);
let collector_loop = std::thread::spawn(move || {
collector.collect(sender)
});
let writer_loop = std::thread::spawn(move || {
let mut writer = std::fs::File::create(arguments.output)?;
loop {
let envelope = receiver.recv()?;
let _ = envelope.write_into(&mut writer)?;
writer.flush()?;
}
});

let build_status = build.wait()?;
collector.stop()?;

collector_loop.join().unwrap()?;
writer_loop.join().unwrap()?;

Ok(build_status.code().unwrap())
}

fn main() {
let exit_code = run().unwrap_or_else(|error| {
eprintln!("Error: {}", error);
1
});

std::process::exit(exit_code);
}
6 changes: 3 additions & 3 deletions rust/intercept/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use crossbeam_channel::bounded;

use super::ipc::{Envelope, SessionLocator};

trait EventCollector {
pub trait EventCollector {
fn address(&self) -> Result<SessionLocator, anyhow::Error>;
fn collect(&self, destination: Sender<Envelope>) -> Result<(), anyhow::Error>;
fn stop(&self) -> Result<(), anyhow::Error>;
}

struct EventCollectorOnTcp {
pub struct EventCollectorOnTcp {
control_input: Sender<bool>,
control_output: Receiver<bool>,
listener: TcpListener,
Expand All @@ -46,7 +46,7 @@ impl EventCollectorOnTcp {
Ok(result)
}

pub fn send(
fn send(
&self,
mut socket: TcpStream,
destination: Sender<Envelope>,
Expand Down
4 changes: 2 additions & 2 deletions rust/intercept/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ impl Envelope {
Ok(envelope)
}

pub fn write_into(&self, mut writer: impl Write) -> Result<(), anyhow::Error> {
pub fn write_into(&self, mut writer: impl Write) -> Result<u32, anyhow::Error> {
let serialized_envelope = serde_json::to_string(&self)?;
let bytes = serialized_envelope.into_bytes();
let length = bytes.len() as u32;

writer.write_all(&length.to_be_bytes())?;
writer.write_all(&bytes)?;

Ok(())
Ok(length)
}
}
28 changes: 0 additions & 28 deletions rust/intercept/src/main.rs

This file was deleted.

0 comments on commit 9256067

Please sign in to comment.