Skip to content

Commit ef5d308

Browse files
committed
Isolate libnative building in a new module so it can be reused from build_standalone.sh
1 parent 61dd601 commit ef5d308

File tree

5 files changed

+88
-48
lines changed

5 files changed

+88
-48
lines changed

build_libnative.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import argparse
2+
import os
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
def is_installed(bin_file):
8+
for path in os.environ.get("PATH", "").split(os.pathsep):
9+
if os.path.isfile(os.path.join(path, bin_file)):
10+
return True
11+
12+
return False
13+
14+
def install_dedup_headers():
15+
if not is_installed("dedup_headers"):
16+
subprocess.run(
17+
["cargo", "install", "--git", "https://github.com/DataDog/libdatadog", "--bin", "dedup_headers", "tools"],
18+
check=True,
19+
)
20+
21+
22+
def build_crate(crate_dir: Path, release: bool, features: list[str] = []):
23+
env = os.environ.copy()
24+
abs_dir = crate_dir.absolute()
25+
env["CARGO_TARGET_DIR"] = str(abs_dir / "target")
26+
27+
build_cmd = ["cargo", "build"]
28+
29+
if release:
30+
build_cmd.append("--release")
31+
32+
if features:
33+
cmd_features = ', '.join(features)
34+
build_cmd += ["--features", cmd_features]
35+
36+
subprocess.run(
37+
build_cmd,
38+
cwd=str(crate_dir),
39+
check=True,
40+
env=env,
41+
)
42+
43+
if 'profiling' in features:
44+
install_dedup_headers()
45+
46+
subprocess.run(
47+
["dedup_headers", "common.h", "crashtracker.h", "profiling.h"],
48+
cwd=str(abs_dir / "target" / "include" / "datadog"),
49+
check=True,
50+
)
51+
52+
def clean_crate(crate_dir: Path):
53+
target_dir = crate_dir.absolute() / "target"
54+
if target_dir.exists():
55+
subprocess.run(
56+
["cargo", "clean"],
57+
cwd=str(crate_dir),
58+
check=True,
59+
)
60+
61+
def main():
62+
try:
63+
parser = argparse.ArgumentParser(description="Build Rust module")
64+
parser.add_argument("--crate", required=True, help="Rust crate location")
65+
parser.add_argument("--release", action="store_true", help="Release profile")
66+
parser.add_argument("--features", nargs='*', help="List of features")
67+
68+
args = parser.parse_args()
69+
70+
build_crate(Path(args.crate), args.release, args.features)
71+
except Exception as e:
72+
print(f"Error: {e}")
73+
sys.exit(1)
74+
75+
if __name__ == "__main__":
76+
main()

ddtrace/internal/datadog/profiling/build_standalone.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ add_target() {
365365
esac
366366
}
367367

368+
#Build rust dependencies
369+
build_rust() {
370+
echo "Building Rust dependencies"
371+
python3 ../../../../build_libnative.py --crate ../../../../src/native --release --features profiling
372+
}
373+
368374

369375
### ENTRYPOINT
370376
# Check for basic input validity
@@ -383,6 +389,8 @@ print_cmake_args
383389

384390
print_ctest_args
385391

392+
build_rust
393+
386394
# Run cmake
387395
for target in "${targets[@]}"; do
388396
run_cmake $target

ddtrace/internal/datadog/profiling/dd_wrapper/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ include(CheckSymbolExists)
2525

2626
# Load libdatadog
2727
#include(FindLibdatadog)
28-
#include(FindLibNative)
29-
#include(FindLibNative)
3028
find_package(LibNative)
3129

3230
# Set verbose mode so compiler and args are shown

setup.py

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
from urllib.error import HTTPError
4444
from urllib.request import urlretrieve
4545

46+
from build_libnative import build_crate, clean_crate
47+
4648

4749
HERE = Path(__file__).resolve().parent
4850

@@ -76,7 +78,6 @@
7678
CRASHTRACKER_DIR = HERE / "ddtrace" / "internal" / "datadog" / "profiling" / "crashtracker"
7779
STACK_V2_DIR = HERE / "ddtrace" / "internal" / "datadog" / "profiling" / "stack_v2"
7880
NATIVE_CRATE = HERE / "src" / "native"
79-
NATIVE_CRATE_TARGET_DIR = NATIVE_CRATE / "target"
8081

8182
BUILD_PROFILING_NATIVE_TESTS = os.getenv("DD_PROFILING_NATIVE_TESTS", "0").lower() in ("1", "yes", "on", "true")
8283

@@ -311,12 +312,7 @@ def remove_artifacts():
311312

312313
@staticmethod
313314
def remove_rust():
314-
if NATIVE_CRATE_TARGET_DIR.exists():
315-
subprocess.run(
316-
["cargo", "clean"],
317-
cwd=str(NATIVE_CRATE),
318-
check=True,
319-
)
315+
clean_crate(NATIVE_CRATE)
320316

321317
def run(self):
322318
CleanLibraries.remove_rust()
@@ -333,44 +329,7 @@ def run(self):
333329

334330
def build_rust(self):
335331

336-
if not self.is_installed("dedup_headers"):
337-
subprocess.run(
338-
["cargo", "install", "--git", "https://github.com/DataDog/libdatadog", "--bin", "dedup_headers", "tools"],
339-
cwd=str(NATIVE_CRATE),
340-
check=True,
341-
)
342-
343-
if not NATIVE_CRATE_TARGET_DIR.exists():
344-
dd_include_dir = NATIVE_CRATE_TARGET_DIR / "include" / "datadog"
345-
# Setting env var so headers are placed in the proper target directory.
346-
env = os.environ.copy()
347-
env["CARGO_TARGET_DIR"] = str(NATIVE_CRATE_TARGET_DIR)
348-
349-
build_cmd = ["cargo", "build", "--release"]
350-
351-
if native_features:
352-
features = ', '.join(native_features)
353-
build_cmd += ["--features", features]
354-
355-
subprocess.run(
356-
build_cmd,
357-
cwd=str(NATIVE_CRATE),
358-
check=True,
359-
env=env,
360-
)
361-
362-
subprocess.run(
363-
["dedup_headers", "common.h", "crashtracker.h", "profiling.h"],
364-
cwd=str(dd_include_dir),
365-
check=True,
366-
)
367-
368-
@staticmethod
369-
def is_installed(bin_file):
370-
for path in os.environ.get("PATH", "").split(os.pathsep):
371-
return os.path.isfile(os.path.join(path, bin_file)
372-
)
373-
return False
332+
build_crate(NATIVE_CRATE, True, native_features)
374333

375334
@staticmethod
376335
def try_strip_symbols(so_file):

src/native/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[package]
22
name = "ddtrace-native"
3-
build = "build/main.rs"
43
version = "0.1.0"
54
edition = "2021"
65
resolver = "2"

0 commit comments

Comments
 (0)