-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprofiling.rs
More file actions
221 lines (187 loc) · 6.83 KB
/
profiling.rs
File metadata and controls
221 lines (187 loc) · 6.83 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0
use crate::arch;
use crate::module::Module;
use crate::utils::{adjust_extern_symbols, file_replace, project_root, wait_for_success};
use anyhow::Result;
use serde::Deserialize;
use std::ffi::OsStr;
use std::fs;
use std::ops::Add;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::rc::Rc;
use tools::headers::dedup_headers;
const CRATE_FOLDER: &str = "libdd-profiling-ffi";
#[derive(Deserialize)]
struct CargoFile {
lib: LibSection,
}
#[derive(Deserialize)]
struct LibSection {
#[serde(rename = "crate-type")]
crate_type: Vec<String>,
}
pub struct Profiling {
pub arch: Rc<str>,
pub base_header: Rc<str>,
pub features: Rc<str>,
pub profile: Rc<str>,
pub source_include: Rc<str>,
pub source_lib: Rc<str>,
pub target_include: Rc<str>,
pub target_lib: Rc<str>,
pub target_pkconfig: Rc<str>,
pub version: Rc<str>,
}
impl Profiling {
fn add_headers(&self) -> Result<()> {
// Allowing unused_mut due to the methods mutating the vector are behind a feature flag.
#[allow(unused_mut)]
let mut headers = vec!["profiling.h"];
#[cfg(feature = "telemetry")]
headers.push("telemetry.h");
#[cfg(feature = "data-pipeline")]
headers.push("data-pipeline.h");
#[cfg(feature = "symbolizer")]
headers.push("blazesym.h");
#[cfg(feature = "library-config")]
headers.push("library-config.h");
#[cfg(feature = "log")]
headers.push("log.h");
#[cfg(feature = "ddsketch")]
headers.push("ddsketch.h");
#[cfg(feature = "ffe")]
headers.push("ffe.h");
let mut origin_path: PathBuf = [&self.source_include, "dummy.h"].iter().collect();
let mut target_path: PathBuf = [&self.target_include, "dummy.h"].iter().collect();
let mut to_dedup = vec![];
for header in &headers {
origin_path.set_file_name(header);
target_path.set_file_name(header);
adjust_extern_symbols(&origin_path, &target_path)
.expect("Failed to adjust extern symbols");
// Exclude blazesym header from deduplication
if !target_path.to_str().unwrap().contains("blazesym.h") {
to_dedup.push(target_path.clone());
}
}
dedup_headers(
self.base_header.as_ref(),
&(to_dedup
.iter()
.map(|i| i.to_str().unwrap())
.collect::<Vec<&str>>()),
);
Ok(())
}
fn add_libs(&self) -> Result<()> {
//Create directory
let lib_dir = Path::new(self.target_lib.as_ref());
fs::create_dir_all(lib_dir).expect("Failed to create pkgconfig directory");
let from_dyn: PathBuf = [&self.source_lib, arch::PROF_DYNAMIC_LIB_FFI]
.iter()
.collect();
let to_dyn: PathBuf = [lib_dir.as_os_str(), OsStr::new(arch::PROF_DYNAMIC_LIB)]
.iter()
.collect();
fs::copy(from_dyn, to_dyn).expect("unable to copy dynamic lib");
let from_static: PathBuf = [&self.source_lib, arch::PROF_STATIC_LIB_FFI]
.iter()
.collect();
let to_static: PathBuf = [lib_dir.as_os_str(), OsStr::new(arch::PROF_STATIC_LIB)]
.iter()
.collect();
fs::copy(from_static, to_static).expect("unable to copy static lib");
arch::add_additional_files(&self.source_lib, lib_dir.as_os_str());
// Generate debug information
arch::strip_libraries(&self.target_lib);
Ok(())
}
fn add_pkg_config(&self) -> Result<()> {
#[cfg(target_os = "windows")]
return Ok(());
let files: [&str; 3] = [
"datadog_profiling.pc",
"datadog_profiling_with_rpath.pc",
"datadog_profiling-static.pc",
];
//Create directory
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 = file.to_string() + ".in";
let mut pc_origin: PathBuf = project_root();
pc_origin.push(CRATE_FOLDER);
pc_origin.push(file_in);
let pc_target: PathBuf = [pc_dir.as_os_str(), OsStr::new(file)].iter().collect();
file_replace(
pc_origin.to_str().unwrap(),
pc_target.to_str().unwrap(),
"@Datadog_VERSION@",
&self.version,
)?;
if *file == files[2] {
file_replace(
pc_origin.to_str().unwrap(),
pc_target.to_str().unwrap(),
"@Datadog_LIBRARIES@",
arch::NATIVE_LIBS,
)?;
}
}
Ok(())
}
}
impl Module for Profiling {
fn build(&self) -> Result<()> {
let features = self.features.to_string() + "," + "cbindgen";
#[cfg(feature = "crashtracker")]
let features = features.add(",crashtracker-collector,crashtracker-receiver,demangler");
// Using rustc instead of build in order to overcome issues with LTO optimization.
let mut cargo_args = vec![
"rustc",
"-p",
CRATE_FOLDER,
"--features",
&features,
"--target",
&self.arch,
];
if self.profile.as_ref() == "release" {
cargo_args.push("--release");
}
// Parse profiling-ffi manifest in order to get the crate-type array.
let prof_path: PathBuf = [project_root().to_str().unwrap(), CRATE_FOLDER, "Cargo.toml"]
.iter()
.collect();
// Buffer the manifest file.
let cargo_toml = fs::read_to_string(prof_path).unwrap();
// Use serde to get access to the lib section.
let parsed: CargoFile = toml::from_str(&cargo_toml).unwrap();
// Iterate over all the crate types in order to build the artifacts for each one.
for crate_type in parsed.lib.crate_type.iter() {
// Ignore lib crate-type
if crate_type == "lib" {
continue;
}
let mut args = cargo_args.clone();
args.append(&mut vec!["--crate-type", crate_type]);
let cargo = Command::new("cargo")
.env("RUSTFLAGS", arch::RUSTFLAGS.join(" "))
.current_dir(project_root())
.args(args)
.spawn()
.expect("failed to spawn cargo");
wait_for_success(cargo, "Cargo");
}
Ok(())
}
fn install(&self) -> Result<()> {
self.add_headers()?;
self.add_libs()?;
self.add_pkg_config()?;
Ok(())
}
}