Skip to content

Commit

Permalink
Remove sed invocation from profiling plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoolioh committed Oct 23, 2024
1 parent c0f85b1 commit 4fb2105
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions builder/build/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@

use crate::arch;
use crate::module::Module;
use anyhow::Result;
use anyhow::{anyhow, Result};
use std::ffi::OsStr;
use std::fs;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::rc::Rc;

fn file_replace(file_in: &str, file_out: &str, target: &str, replace: &str) -> Result<()> {
let content = fs::read_to_string(file_in)?;
let new = content.replace(target, replace);

let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(file_out)?;
file.write_all(new.as_bytes())
.map_err(|err| anyhow!("failed to write file: {}", err))
}

pub struct Profiling {
pub source_include: Rc<str>,
pub source_lib: Rc<str>,
Expand Down Expand Up @@ -76,30 +89,19 @@ impl Profiling {
];

//Create directory
// let pc_dir: PathBuf = [&self, "lib/pkgconfig"].iter().collect();
let pc_dir = Path::new(self.target_pkconfig.as_ref());
fs::create_dir_all(pc_dir).expect("Failed to create pkgconfig directory");

// Create files
for file in files.iter() {
let file_in = "../profiling-ffi/".to_string() + file + ".in";
let output = Command::new("sed")
.arg("s/@Datadog_VERSION@/".to_string() + &self.version + "/g")
.arg(&file_in)
.output()
.expect("sed command failed");

let pc_file: PathBuf = [pc_dir.as_os_str(), OsStr::new(file)].iter().collect();
fs::write(&pc_file, &output.stdout).expect("writing pc file failed");
let pc_file = pc_dir.to_str().unwrap().to_string() + "/" + *file;

if *file == files[2] {
let output = Command::new("sed")
.arg("s/@Datadog_LIBRARIES@/".to_string() + arch::NATIVE_LIBS + "/g")
.arg(&file_in)
.output()
.expect("sed command failed");
file_replace(&file_in, &pc_file, "@Datadog_VERSION@", &self.version)?;

fs::write(&pc_file, &output.stdout).expect("writing pc file failed");
if *file == files[2] {
file_replace(&file_in, &pc_file, "@Datadog_LIBRARIES@", arch::NATIVE_LIBS)?;
}
}
Ok(())
Expand Down

0 comments on commit 4fb2105

Please sign in to comment.