Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ tar = "0.4.41"
tools = { path = "../tools" }
toml = "0.8.19"
serde = "1.0.209"
regex = "1.10"

[[bin]]
name = "release"
Expand Down
6 changes: 3 additions & 3 deletions builder/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

use crate::arch;
use crate::module::Module;
use crate::utils::project_root;
use crate::utils::{adjust_extern_symbols, project_root};
use anyhow::Result;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::rc::Rc;
Expand Down Expand Up @@ -41,7 +40,8 @@ impl Module for Common {
let target_path: PathBuf = [self.target_include.as_ref(), "common.h"].iter().collect();

let origin_path: PathBuf = [self.source_include.as_ref(), "common.h"].iter().collect();
fs::copy(origin_path, target_path).expect("Failed to copy common.h");
adjust_extern_symbols(&origin_path, &target_path).expect("Failed to adjust extern symbols");

Ok(())
}
}
5 changes: 3 additions & 2 deletions builder/src/crashtracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::arch;
use crate::module::Module;
use crate::utils::project_root;
use crate::utils::{adjust_extern_symbols, project_root};
use anyhow::Result;
use std::fs;
use std::path::PathBuf;
Expand Down Expand Up @@ -106,7 +106,8 @@ impl CrashTracker {
.collect();

let headers = vec![target_path.to_str().unwrap()];
fs::copy(origin_path, &target_path).expect("Failed to copy crashtracker.h");
adjust_extern_symbols(&origin_path, &target_path)
.expect("Failed to adjust extern symbols for crashtracker.h");

dedup_headers(self.base_header.as_ref(), &headers);

Expand Down
5 changes: 3 additions & 2 deletions builder/src/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::arch;
use crate::module::Module;
use crate::utils::{file_replace, project_root};
use crate::utils::{adjust_extern_symbols, file_replace, project_root};
use anyhow::Result;
use serde::Deserialize;
use std::ffi::OsStr;
Expand Down Expand Up @@ -67,7 +67,8 @@ impl Profiling {
for header in &headers {
origin_path.set_file_name(header);
target_path.set_file_name(header);
fs::copy(&origin_path, &target_path).expect("Failed to copy the 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") {
Expand Down
35 changes: 35 additions & 0 deletions builder/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::{anyhow, Result};
use regex::Regex;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
Expand All @@ -26,3 +27,37 @@ pub fn project_root() -> PathBuf {
.unwrap()
.to_path_buf()
}

pub(crate) fn adjust_extern_symbols(
file_in: impl AsRef<Path>,
file_out: impl AsRef<Path>,
) -> Result<()> {
let content = fs::read_to_string(file_in)?;
let re = Regex::new(r#"(?m)^(\s*)extern\s+(.+;)$"#).unwrap();

// Replace function using captures
let new_content = re.replace_all(&content, |caps: &regex::Captures| {
let full_match = caps.get(0).unwrap().as_str();
let indent = &caps[1];
let declaration = &caps[2];

// Skip if it's extern "C", already has LIBDD_DLLIMPORT, or contains '(' (function)
if full_match.contains("extern \"C\"")
|| full_match.contains("LIBDD_DLLIMPORT")
|| full_match.contains('(')
{
return full_match.to_string();
}

// Keep indent + "extern " + "LIBDD_DLL_IMPORT " + declaration
format!("{}extern LIBDD_DLLIMPORT {}", indent, declaration)
});

let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(file_out)?;
file.write_all(new_content.as_bytes())
.map_err(|err| anyhow!("failed to write file: {}", err))
}
6 changes: 6 additions & 0 deletions libdd-common-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ after_includes = """
# define DDOG_CHECK_RETURN __attribute__((__warn_unused_result__))
#else
# define DDOG_CHECK_RETURN
#endif

#ifdef _WIN32
#define LIBDD_DLLIMPORT __declspec(dllimport)
#else
#define LIBDD_DLLIMPORT
#endif"""

[export]
Expand Down
23 changes: 23 additions & 0 deletions windows/build-artifacts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ function Invoke-Call {
}
}

function Add-DllImportToGlobals {
param (
[string]$HeaderPath
)
$content = [System.IO.File]::ReadAllText($HeaderPath)
$pattern = '(?m)^(\s*)extern\s+(?!\"C\")(?!.*LIBDD_DLLIMPORT)(?!.*\()(.+;)$'
$updated = [System.Text.RegularExpressions.Regex]::Replace(
$content,
$pattern,
'$1extern LIBDD_DLLIMPORT $2'
)
if ($updated -ne $content) {
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($HeaderPath, $updated, $utf8NoBom)
}
}

$output_dir = $args[0]

if ([string]::IsNullOrEmpty($output_dir)) {
Expand Down Expand Up @@ -61,6 +78,12 @@ Invoke-Call -ScriptBlock { cbindgen --crate libdd-telemetry-ffi --config libdd-t
Invoke-Call -ScriptBlock { cbindgen --crate libdd-data-pipeline-ffi --config libdd-data-pipeline-ffi/cbindgen.toml --output $output_dir"\data-pipeline.h" }
Invoke-Call -ScriptBlock { cbindgen --crate libdd-crashtracker-ffi --config libdd-crashtracker-ffi/cbindgen.toml --output $output_dir"\crashtracker.h" }
Invoke-Call -ScriptBlock { cbindgen --crate libdd-library-config-ffi --config libdd-library-config-ffi/cbindgen.toml --output $output_dir"\library-config.h" }
Add-DllImportToGlobals $output_dir"\common.h"
Add-DllImportToGlobals $output_dir"\profiling.h"
Add-DllImportToGlobals $output_dir"\telemetry.h"
Add-DllImportToGlobals $output_dir"\data-pipeline.h"
Add-DllImportToGlobals $output_dir"\crashtracker.h"
Add-DllImportToGlobals $output_dir"\library-config.h"
Invoke-Call -ScriptBlock { .\target\release\dedup_headers $output_dir"\common.h" $output_dir"\profiling.h" $output_dir"\telemetry.h" $output_dir"\data-pipeline.h" $output_dir"\crashtracker.h" $output_dir"\library-config.h"}

Write-Host "Build finished" -ForegroundColor Magenta
Loading