Skip to content

Commit

Permalink
No longer compile the mockgen as part of the rust build
Browse files Browse the repository at this point in the history
Instead we run a separate rust executable to produce it, and it will be included as a normal .c file in the ddtrace C build

This will allow us to compile the sidecar once for all versions as well as have the debug symbols for the sidecar just once

Signed-off-by: Bob Weinand <[email protected]>
  • Loading branch information
bwoebi committed Feb 12, 2024
1 parent d2d9d2f commit f121e27
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 78 deletions.
24 changes: 15 additions & 9 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["components-rs", "profiling"]
members = ["components-rs", "components-rs/php_sidecar_mockgen", "profiling"]
resolver = "2"

[workspace.package]
Expand Down
2 changes: 0 additions & 2 deletions components-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,3 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [

[build-dependencies]
cbindgen = "0.24"
cc_utils = { path = "../libdatadog/tools/cc_utils" }
sidecar_mockgen = { path = "../libdatadog/tools/sidecar_mockgen" }
53 changes: 0 additions & 53 deletions components-rs/build.rs

This file was deleted.

11 changes: 11 additions & 0 deletions components-rs/php_sidecar_mockgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
edition = "2021"
name = "php_sidecar_mockgen"
version = "0.1.0"

[dependencies]
cc_utils = { path = "../../libdatadog/tools/cc_utils" }
sidecar_mockgen = { path = "../../libdatadog/tools/sidecar_mockgen" }

[[bin]]
name = "php_sidecar_mockgen"
75 changes: 75 additions & 0 deletions components-rs/php_sidecar_mockgen/src/bin/php_sidecar_mockgen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc.

pub use cc_utils::cc;
pub use sidecar_mockgen::generate_mock_symbols;
use std::path::Path;
use std::{env, fs, process};

fn main() {
// This script is necessary to avoid the linker puking when the sidecar tries to load our ddtrace.so
// As php itself is not available within the sidecar, it needs to make sure that all symbols ddtrace.so depends on, are available.
// The mock_generator takes care of generating these symbols.
let args: Vec<_> = env::args_os().collect();
if args.len() < 3 {
eprintln!(
"Needs at least 3 args: the output file, the shared object file followed by at least one object file"
);
process::exit(1);
}

let output_path = Path::new(&args[1]);
let binary_path = Path::new(&args[2]);
let object_paths: Vec<_> = args.iter().skip(3).map(Path::new).collect();
let mock_symbols = match generate_mock_symbols(binary_path, object_paths.as_slice()) {
Ok(symbols) => symbols,
Err(err) => {
eprintln!("Failed generating mock_php_syms.c: {}", err);
process::exit(1);
}
};

if fs::read("mock_php_syms.c")
.ok()
.map(|contents| contents == mock_symbols.as_str().as_bytes())
!= Some(true)
{
if let Err(err) = fs::write("mock_php_syms.c", mock_symbols) {
eprintln!("Failed generating mock_php_syms.c: {}", err);
process::exit(1);
}
}

let source_modified = fs::metadata("mock_php_syms.c").unwrap().modified().unwrap();
if fs::metadata("mock_php.shared_lib").map_or(true, |m| m.modified().unwrap() < source_modified) {
env::set_var("OPT_LEVEL", "2");

cc_utils::ImprovedBuild::new()
.file("mock_php_syms.c")
.link_dynamically("dl")
.warnings(true)
.warnings_into_errors(true)
.emit_rerun_if_env_changed(false)
.try_compile_shared_lib("mock_php.shared_lib")
.unwrap();

let bin = match fs::read("mock_php.shared_lib") {
Ok(bin) => bin,
Err(err) => {
eprintln!("Failed opening generated mock_php.shared_lib: {}", err);
process::exit(1);
}
};

let comma_separated = bin.iter().map(|byte| format!("{byte:#X}")).collect::<Vec<String>>().join(",");
let out = format!(r#"
const unsigned char DDTRACE_MOCK_PHP[] = {{{comma_separated}}};
const void *DDTRACE_MOCK_PHP_SIZE = (void *) sizeof(DDTRACE_MOCK_PHP);
"#);

if let Err(err) = fs::write(output_path, out) {
eprintln!("Failed generating {:?}: {}", output_path, err);
process::exit(1);
}
}
}
8 changes: 6 additions & 2 deletions components-rs/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ fn parse_composer_installed_json(
}

#[cfg(php_shared_build)]
const MOCK_PHP: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/mock_php.shared_lib"));
extern "C" {
static DDTRACE_MOCK_PHP: u8;
static DDTRACE_MOCK_PHP_SIZE: usize;
}

#[cfg(php_shared_build)]
fn run_sidecar(mut cfg: config::Config) -> io::Result<SidecarTransport> {
let mock = unsafe { std::slice::from_raw_parts(&DDTRACE_MOCK_PHP, DDTRACE_MOCK_PHP_SIZE) };
cfg.library_dependencies
.push(LibDependency::Binary(MOCK_PHP));
.push(LibDependency::Binary(mock));
datadog_sidecar::start_or_connect_to_sidecar(cfg)
}

Expand Down
25 changes: 15 additions & 10 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ if test "$PHP_DDTRACE" != "no"; then
ext/startup_logging.c \
ext/telemetry.c \
ext/tracer_tag_propagation/tracer_tag_propagation.c \
ext/user_request.c \
ext/user_request.c \
ext/hook/uhook.c \
ext/hook/uhook_legacy.c \
"
Expand Down Expand Up @@ -222,6 +222,8 @@ if test "$PHP_DDTRACE" != "no"; then
PHP_ADD_INCLUDE([$ext_srcdir])
PHP_ADD_INCLUDE([$ext_srcdir/ext])

PHP_ADD_BUILD_DIR([$ext_builddir/components-rs])

PHP_ADD_BUILD_DIR([$ext_builddir/components])
PHP_ADD_BUILD_DIR([$ext_builddir/components/container_id])
PHP_ADD_BUILD_DIR([$ext_builddir/components/log])
Expand Down Expand Up @@ -277,21 +279,24 @@ if test "$PHP_DDTRACE" != "no"; then
dnl consider it debug if -g is specified (but not -g0)
ddtrace_cargo_profile=$(test "$PHP_DDTRACE_RUST_DEBUG" != "no" && echo debug || echo tracer-release)

cat <<EOT >> Makefile.fragments
\$(builddir)/target/$ddtrace_cargo_profile/libddtrace_php.a: $( (find "$ext_srcdir/components-rs" -name "*.rs" -o -name "Cargo.toml"; find "$ext_srcdir/../../libdatadog" -name "*.rs" -not -path "*/target/*"; find "$ext_srcdir/libdatadog" -name "*.rs" -not -path "*/target/*") 2>/dev/null | xargs )
(cd "$ext_srcdir/components-rs"; CARGO_TARGET_DIR=\$(builddir)/target/ RUSTFLAGS="${RUSTFLAGS:-} --cfg tokio_unstable $(test -z "${host_os##linux*}" && echo "--cfg tokio_taskdump") $(test "$ext_shared" = "yes" && echo "--cfg php_shared_build")" RUSTC_BOOTSTRAP=1 \$(DDTRACE_CARGO) build $(test "$ddtrace_cargo_profile" == debug || echo --profile tracer-release) \$(shell echo "\$(MAKEFLAGS)" | $EGREP -o "[[-]]j[[0-9]]+") && test "$PHP_DDTRACE_RUST_SYMBOLS" == yes || strip -d \$(builddir)/target/$ddtrace_cargo_profile/libddtrace_php.a)
EOT

if test "$ext_shared" = "yes"; then
all_object_files=$(for src in $DD_TRACE_PHP_SOURCES $ZAI_SOURCES; do printf ' %s' "${src%?}lo"; done)
all_object_files_newlines=$(for src in $DD_TRACE_PHP_SOURCES $ZAI_SOURCES; do printf '\\n$(builddir)/%s' "$(dirname "$src")/$objdir/$(basename "${src%?}o")"; done)
all_object_files_absolute=$(for src in $DD_TRACE_PHP_SOURCES $ZAI_SOURCES; do printf ' $(builddir)/%s' "$(dirname "$src")/$objdir/$(basename "${src%?}o")"; done)
php_binary=$("$PHP_CONFIG" --php-binary)
ddtrace_mock_sources='DD_SIDECAR_MOCK_SOURCES="$$(printf "'"$php_binary$all_object_files_newlines"'")"'
else
all_object_files=
ddtrace_mock_sources=
fi
cat <<EOT >> Makefile.fragments

cat <<EOT >> Makefile.fragments
\$(builddir)/target/$ddtrace_cargo_profile/libddtrace_php.a: $( (find "$ext_srcdir/components-rs" -name "*.c" -o -name "*.rs" -o -name "Cargo.toml"; find "$ext_srcdir/../../libdatadog" -name "*.rs" -not -path "*/target/*"; find "$ext_srcdir/libdatadog" -name "*.rs" -not -path "*/target/*"; echo "$all_object_files" ) 2>/dev/null | xargs )
(cd "$ext_srcdir/components-rs"; $ddtrace_mock_sources CARGO_TARGET_DIR=\$(builddir)/target/ RUSTFLAGS="${RUSTFLAGS:-} --cfg tokio_unstable $([[ $host_os == linux* ]] && echo "--cfg tokio_taskdump")" RUSTC_BOOTSTRAP=1 \$(DDTRACE_CARGO) build $(test "$ddtrace_cargo_profile" == debug || echo --profile tracer-release) \$(shell echo "\$(MAKEFLAGS)" | $EGREP -o "[[-]]j[[0-9]]+") && test "$PHP_DDTRACE_RUST_SYMBOLS" == yes || strip -d \$(builddir)/target/$ddtrace_cargo_profile/libddtrace_php.a)
/\$(builddir)/components-rs/mock_php.c: $all_object_files
(cd "$ext_srcdir/components-rs"; HOST= TARGET= CARGO_TARGET_DIR=\$(builddir)/target/ \$(DDTRACE_CARGO) run --manifest-path=$ext_srcdir/components-rs/php_sidecar_mockgen/Cargo.toml -p php_sidecar_mockgen \$(builddir)/components-rs/mock_php.c $php_binary $all_object_files_absolute)
EOT

PHP_ADD_SOURCES_X("/$ext_dir", "\$(builddir)/components-rs/mock_php.c", $ac_extra, shared_objects_ddtrace, yes)
fi

if test "$ext_shared" = "shared" || test "$ext_shared" = "yes"; then
shared_objects_ddtrace="\$(builddir)/target/$ddtrace_cargo_profile/libddtrace_php.a $shared_objects_ddtrace"
else
Expand Down
2 changes: 1 addition & 1 deletion libdatadog

0 comments on commit f121e27

Please sign in to comment.