Skip to content

Commit

Permalink
Remove sed from main.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoolioh committed Oct 25, 2024
1 parent b9e3e24 commit 8b11d6a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
18 changes: 10 additions & 8 deletions builder/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ mod profiling;
#[cfg(feature = "symbolizer")]
mod symbolizer;

mod utils;

use anyhow::Result;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::rc::Rc;
use std::{env, fs};
use utils::file_replace;

use build_common::{determine_paths, HEADER_PATH};
use tools::headers::dedup_headers;
Expand Down Expand Up @@ -151,14 +153,14 @@ impl Builder {

fn add_cmake(&self) {
let libs = arch::NATIVE_LIBS.to_owned();
let output = Command::new("sed")
.arg("s/@Datadog_LIBRARIES@/".to_string() + libs.trim() + "/g")
.arg("../cmake/DatadogConfig.cmake.in")
.output()
.expect("Failed to modify cmake");

let cmake_path: PathBuf = [&self.target_dir, "DatadogConfig.cmake"].iter().collect();
fs::write(cmake_path, output.stdout).expect("writing cmake file failed");
file_replace(
"../cmake/DatadogConfig.cmake.in",
cmake_path.to_str().unwrap(),
"@Datadog_LIBRARIES@",
libs.trim(),
)
.expect("Failed to modify the cmake");
}

/// Builds the final artifact by going through all modules and instancing their install method.
Expand Down
35 changes: 16 additions & 19 deletions builder/build/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,13 @@

use crate::arch;
use crate::module::Module;
use anyhow::{anyhow, Result};
use crate::utils::file_replace;
use anyhow::Result;
use std::ffi::OsStr;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::fs;
use std::path::{Path, PathBuf};
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 @@ -96,12 +83,22 @@ impl Profiling {
for file in files.iter() {
let file_in = "../profiling-ffi/".to_string() + file + ".in";

let pc_file = pc_dir.to_str().unwrap().to_string() + "/" + *file;
let pc_file: PathBuf = [pc_dir.as_os_str(), OsStr::new(file)].iter().collect();

file_replace(&file_in, &pc_file, "@Datadog_VERSION@", &self.version)?;
file_replace(
&file_in,
pc_file.to_str().unwrap(),
"@Datadog_VERSION@",
&self.version,
)?;

if *file == files[2] {
file_replace(&file_in, &pc_file, "@Datadog_LIBRARIES@", arch::NATIVE_LIBS)?;
file_replace(
&file_in,
pc_file.to_str().unwrap(),
"@Datadog_LIBRARIES@",
arch::NATIVE_LIBS,
)?;
}
}
Ok(())
Expand Down
19 changes: 19 additions & 0 deletions builder/build/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use anyhow::{anyhow, Result};
use std::fs::{self, OpenOptions};
use std::io::Write;

pub 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))
}

0 comments on commit 8b11d6a

Please sign in to comment.