-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathsetup.py
More file actions
1774 lines (1516 loc) · 75.3 KB
/
setup.py
File metadata and controls
1774 lines (1516 loc) · 75.3 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import atexit
import contextlib
from dataclasses import dataclass
import hashlib
from itertools import chain
import os
import platform
import random
import re
import shlex
import shutil
import subprocess
import sys
import sysconfig
import tarfile
import time
import typing as t
import warnings
from setuptools_rust import Binding
from setuptools_rust import RustExtension
from setuptools_rust import build_rust
import cmake
from setuptools import Distribution, Extension, find_packages, setup # isort: skip
from setuptools.command.build_ext import build_ext # isort: skip
from setuptools.command.build_py import build_py as BuildPyCommand # isort: skip
from pathlib import Path # isort: skip
from distutils.command.clean import clean as CleanCommand # isort: skip
from distutils.dep_util import newer_group # isort: skip
from distutils.util import get_platform # isort: skip
try:
# ORDER MATTERS
# Import this after setuptools or it will fail
from Cython.Build import cythonize
from Cython.Distutils.extension import Extension as CythonExtension
except ImportError:
raise ImportError(
"Failed to import Cython modules. This can happen under versions of pip older than 18 that don't "
"support installing build requirements during setup. If you're using pip, make sure it's a "
"version >=18.\nSee the quickstart documentation for more information:\n"
"https://ddtrace.readthedocs.io/en/stable/installation_quickstart.html"
)
from functools import wraps
from urllib.error import HTTPError
from urllib.error import URLError
from urllib.request import urlretrieve
HERE = Path(__file__).resolve().parent
CURRENT_OS = platform.system()
# What's meant by each build mode is similar to that from CMake, except that
# non-CMake extensions are by default built with debug symbols. And we build
# with Release by default for Windows.
# Released wheels on Linux and macOS are stripped of debug symbols. We use
# scripts/extract_debug_symbols.py to extract the debug symbols from the wheels.
# C/C++ and Cython extensions built with setuptools.Extension, and
# Cython.Distutils.Extension by default inherits CFLAGS from the Python
# interpreter, and it usually has -O3 -g. So they're built with debug symbols
# by default.
# RustExtension src/native has two build profiles, release and debug, and only
# DD_COMPILE_MODE=Debug will build with debug profile, and rest will build with
# release profile, which also has debug symbols by default.
# And when MinSizeRel or Release is used, we strip the debug symbols from the
# wheels, see try_strip_symbols() below.
COMPILE_MODE = "Release" if CURRENT_OS == "Windows" else "RelWithDebInfo"
if "DD_COMPILE_DEBUG" in os.environ:
warnings.warn(
"The DD_COMPILE_DEBUG environment variable is deprecated and will be deleted, "
"use DD_COMPILE_MODE=Debug|Release|RelWithDebInfo|MinSizeRel.",
)
COMPILE_MODE = "Debug"
else:
COMPILE_MODE = os.environ.get("DD_COMPILE_MODE", COMPILE_MODE)
FAST_BUILD = os.getenv("DD_FAST_BUILD", "false").lower() in ("1", "yes", "on", "true")
if FAST_BUILD:
print("WARNING: DD_FAST_BUILD is enabled, some optimizations will be disabled")
else:
print("INFO: DD_FAST_BUILD not enabled")
if FAST_BUILD:
os.environ["DD_COMPILE_ABSEIL"] = "0"
# Trade binary size for compilation speed in dev environments by disabling
# LTO and increasing codegen parallelism. Never used for release wheels.
os.environ.setdefault("CARGO_PROFILE_RELEASE_LTO", "off")
os.environ.setdefault("CARGO_PROFILE_RELEASE_CODEGEN_UNITS", "16")
os.environ.setdefault("CARGO_PROFILE_RELEASE_OPT_LEVEL", "2")
SCCACHE_COMPILE = os.getenv("DD_USE_SCCACHE", "0").lower() in ("1", "yes", "on", "true")
# Default CMAKE_BUILD_PARALLEL_LEVEL to the number of CPUs so that cmake
# builds use all available cores instead of a single thread.
# process_cpu_count (3.13+) respects cgroup limits in containers;
# fall back to cpu_count on older Pythons.
_cpu_count = getattr(os, "process_cpu_count", os.cpu_count)() or 1
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
os.environ["CMAKE_BUILD_PARALLEL_LEVEL"] = str(_cpu_count)
# Retry configuration for downloads (handles GitHub API failures like 503, 429)
DOWNLOAD_MAX_RETRIES = int(os.getenv("DD_DOWNLOAD_MAX_RETRIES", "10"))
DOWNLOAD_INITIAL_DELAY = float(os.getenv("DD_DOWNLOAD_INITIAL_DELAY", "1.0"))
DOWNLOAD_MAX_DELAY = float(os.getenv("DD_DOWNLOAD_MAX_DELAY", "120"))
IS_PYSTON = hasattr(sys, "pyston_version_info")
IS_EDITABLE = False # Set to True if the package is being installed in editable mode
NATIVE_CRATE = HERE / "src" / "native"
DDTRACE_DIR = HERE / "ddtrace"
LIBDDWAF_DOWNLOAD_DIR = DDTRACE_DIR / "appsec" / "_ddwaf" / "libddwaf"
IAST_DIR = DDTRACE_DIR / "appsec" / "_iast" / "_taint_tracking"
DDUP_DIR = DDTRACE_DIR / "internal" / "datadog" / "profiling" / "ddup"
STACK_DIR = DDTRACE_DIR / "internal" / "datadog" / "profiling" / "stack"
VENDOR_DIR = DDTRACE_DIR / "vendor"
CARGO_TARGET_DIR = NATIVE_CRATE.absolute() / f"target{sys.version_info.major}.{sys.version_info.minor}"
DD_CARGO_ARGS = shlex.split(os.getenv("DD_CARGO_ARGS", ""))
BUILD_PROFILING_NATIVE_TESTS = os.getenv("DD_PROFILING_NATIVE_TESTS", "0").lower() in ("1", "yes", "on", "true")
CURRENT_OS = platform.system()
SERVERLESS_BUILD = os.getenv("DD_SERVERLESS_BUILD", "0").lower() in ("1", "yes", "on", "true")
WHEEL_FLAVOR = "-serverless" if SERVERLESS_BUILD else ""
LIBDDWAF_VERSION = "1.30.1"
# DEV: update this accordingly when src/native upgrades libdatadog dependency.
# libdatadog v35.0.0 requires rust 1.87.0.
RUST_MINIMUM_VERSION = "1.87.0"
def interpose_sccache():
"""
Injects sccache into the relevant build commands if it's allowed and we think it'll work
"""
if not SCCACHE_COMPILE:
return
# Check for sccache. We don't do multi-step failover (e.g., if the path is set, but the binary is invalid)
# Honor both SCCACHE_PATH and SCCACHE env vars for compatibility with docs
_sccache_path = os.getenv("SCCACHE_PATH") or os.getenv("SCCACHE") or shutil.which("sccache")
if _sccache_path is None:
print("WARNING: sccache not found in SCCACHE_PATH, SCCACHE, or PATH, skipping sccache interposition")
return
sccache_path = Path(_sccache_path)
if sccache_path.is_file() and os.access(sccache_path, os.X_OK):
# Both the cmake and rust toolchains allow the caller to interpose sccache into the compiler commands, but this
# misses calls from native extension builds. So we do the normal Rust thing, but modify CC and CXX to point to
# a wrapper
os.environ["DD_SCCACHE_PATH"] = str(sccache_path.resolve())
os.environ["RUSTC_WRAPPER"] = str(sccache_path.resolve())
cc_path = next(
(shutil.which(cmd) for cmd in [os.getenv("CC", ""), "cc", "gcc", "clang"] if shutil.which(cmd)), None
)
if cc_path:
os.environ["DD_CC_OLD"] = cc_path
os.environ["CC"] = str(sccache_path) + " " + str(cc_path)
cxx_path = next(
(shutil.which(cmd) for cmd in [os.getenv("CXX", ""), "c++", "g++", "clang++"] if shutil.which(cmd)), None
)
if cxx_path:
os.environ["DD_CXX_OLD"] = cxx_path
os.environ["CXX"] = str(sccache_path) + " " + str(cxx_path)
def retry_download(
max_attempts=DOWNLOAD_MAX_RETRIES,
initial_delay=DOWNLOAD_INITIAL_DELAY,
max_delay=DOWNLOAD_MAX_DELAY,
backoff_factor=1.618,
):
"""
Decorator to retry downloads with exponential backoff.
Handles HTTP 503, 429, network errors from GitHub API, and cargo install failures.
Retriable errors: HTTP 429 (rate limit), 502, 503, 504, network timeouts, and subprocess errors.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(max_attempts):
try:
return func(*args, **kwargs)
except (HTTPError, URLError, TimeoutError, OSError, subprocess.CalledProcessError) as e:
# Check if it's a retriable error
is_retriable = False
if isinstance(e, HTTPError):
# Retry on 429 (rate limit), 502/503/504 (server errors)
is_retriable = e.code in (429, 502, 503, 504)
error_code = f"HTTP {e.code}"
elif isinstance(e, (URLError, TimeoutError)):
# Retry on network errors and timeouts
is_retriable = True
error_code = type(e).__name__
elif isinstance(e, OSError):
# Retry on connection errors
is_retriable = True
error_code = type(e).__name__
elif isinstance(e, subprocess.CalledProcessError):
# Retry on subprocess errors (e.g., cargo install network failures)
# These often indicate temporary network issues
is_retriable = True
error_code = f"subprocess exit code {e.returncode}"
if not is_retriable:
print(f"ERROR: Operation failed (non-retriable {error_code}): {e}")
raise
if attempt == max_attempts - 1:
print(f"ERROR: Operation failed after {max_attempts} attempts (last error: {error_code})")
raise
# Calculate delay with jitter
delay = min(initial_delay * (backoff_factor**attempt), max_delay)
jitter = random.uniform(0, delay * 0.1)
total_delay = delay + jitter
print(f"WARNING: Operation failed (attempt {attempt + 1}/{max_attempts}): {error_code} - {e}")
print(f" Retrying in {total_delay:.1f} seconds...")
time.sleep(total_delay)
return func(*args, **kwargs)
return wrapper
return decorator
def verify_checksum_from_file(sha256_filename, filename):
# sha256 File format is ``checksum`` followed by two whitespaces, then ``filename`` then ``\n``
expected_checksum, expected_filename = list(filter(None, open(sha256_filename, "r").read().strip().split(" ")))
actual_checksum = hashlib.sha256(open(filename, "rb").read()).hexdigest()
try:
assert expected_filename.endswith(Path(filename).name)
assert expected_checksum == actual_checksum
except AssertionError:
print("Checksum verification error: Checksum and/or filename don't match:")
print("expected checksum: %s" % expected_checksum)
print("actual checksum: %s" % actual_checksum)
print("expected filename: %s" % expected_filename)
print("actual filename: %s" % filename)
sys.exit(1)
def verify_checksum_from_hash(expected_checksum, filename):
# sha256 File format is ``checksum`` followed by two whitespaces, then ``filename`` then ``\n``
actual_checksum = hashlib.sha256(open(filename, "rb").read()).hexdigest()
try:
assert expected_checksum == actual_checksum
except AssertionError:
print("Checksum verification error: Checksum mismatch:")
print("expected checksum: %s" % expected_checksum)
print("actual checksum: %s" % actual_checksum)
sys.exit(1)
def load_module_from_project_file(mod_name, fname):
"""
Helper used to load a module from a file in this project
DEV: Loading this way will by-pass loading all parent modules
e.g. importing `ddtrace.vendor.psutil.setup` will load `ddtrace/__init__.py`
which has side effects like loading the tracer
"""
fpath = HERE / fname
import importlib.util
spec = importlib.util.spec_from_file_location(mod_name, fpath)
if spec is None:
raise ImportError(f"Could not find module {mod_name} in {fpath}")
mod = importlib.util.module_from_spec(spec)
if spec.loader is None:
raise ImportError(f"Could not load module {mod_name} from {fpath}")
spec.loader.exec_module(mod)
return mod
def is_64_bit_python():
return sys.maxsize > (1 << 32)
rust_features = ["stats"]
if CURRENT_OS in ("Linux", "Darwin") and is_64_bit_python() and sys.version_info < (3, 15):
rust_features.append("profiling")
if not SERVERLESS_BUILD:
rust_features.append("crashtracker")
if not SERVERLESS_BUILD:
rust_features.append("ffe")
class PatchedDistribution(Distribution):
def __init__(self, attrs: t.Optional[dict[str, t.Any]] = None) -> None:
super().__init__(attrs)
# Tell ext_hashes about your manually-built Rust artifact
rust_env = os.environ.copy()
rust_env["CARGO_TARGET_DIR"] = str(CARGO_TARGET_DIR)
self.rust_extensions = [
RustExtension(
# The Python import path of your extension:
"ddtrace.internal.native._native",
# Path to your Cargo.toml so setuptools-rust can infer names
path=str(Path(__file__).parent / "src" / "native" / "Cargo.toml"),
py_limited_api="auto",
binding=Binding.PyO3,
debug=COMPILE_MODE.lower() == "debug",
features=rust_features,
env=rust_env,
args=DD_CARGO_ARGS,
)
]
class ExtensionHashes(build_ext):
def run(self) -> None:
try:
dist = self.distribution
for ext in chain(dist.ext_modules, getattr(dist, "rust_extensions", [])):
if isinstance(ext, CMakeExtension):
sources = ext.get_sources()
elif isinstance(ext, RustExtension):
source_path = Path(ext.path).parent
sources = [
_
for _ in source_path.glob("**/*")
if _.is_file()
and _.relative_to(source_path).parts[0]
!= f"target{sys.version_info.major}.{sys.version_info.minor}"
]
else:
# Hash the explicit .pyx sources plus all .pxd files found
# under ddtrace/. .pxd files act like C headers — a change
# in any of them can affect compiled output for any Cython
# extension that imports it.
sources = [Path(_) for _ in ext.sources]
sources.extend(p for p in (HERE / "ddtrace").glob("**/*.pxd") if p.is_file())
sources_hash = hashlib.sha256()
# DEV: Make sure to include the rust features since changing them changes what gets built
for feature in rust_features:
sources_hash.update(feature.encode())
for source in sorted(sources):
sources_hash.update(source.read_bytes())
hash_digest = sources_hash.hexdigest()
entries: list[tuple[str, str, str]] = []
entries.append((ext.name, hash_digest, str(Path(self.get_ext_fullpath(ext.name)))))
# For profiling, these headers are generated by the Rust extension
# and they are used to build dd_wrapper shared library. We need
# to persist in the extension hash cache so that we can handle
# the case where Rust extension is not rebuilt but the dd_wrapper
# needs to be rebuilt when its sources changed.
if isinstance(ext, RustExtension) and "profiling" in ext.features:
for f in ["common.h", "profiling.h"]:
entries.append(
(
ext.name,
hash_digest,
str(CARGO_TARGET_DIR / "include" / "datadog" / f),
)
)
# Include any dependencies that might have been built alongside
# the extension.
if isinstance(ext, CMakeExtension):
entries.extend(
(f"{ext.name}-{dependency.name}", hash_digest, str(dependency) + "*")
for dependency in ext.dependencies
)
for entry in entries:
print("#EXTHASH:", entry)
# Emit shared dependency metadata so ext_cache.py can cache and
# restore install trees without any per-dependency special-casing.
for dep in SHARED_DEPS:
print("#SHAREDEPINFO:", (dep.name, dep.config_hash(), str(dep.install_dir)))
except Exception as e:
print("WARNING: Failed to compute extension hashes: %s" % e)
raise e
class CustomBuildRust(build_rust):
"""Custom build_rust command that handles dedup_headers and header copying."""
def initialize_options(self) -> None:
super().initialize_options()
def is_installed(self, bin_file):
"""Check if a binary is installed in PATH."""
for path in os.environ.get("PATH", "").split(os.pathsep):
if os.path.isfile(os.path.join(path, bin_file)):
return True
return False
def install_dedup_headers(self):
"""Install dedup_headers if not already installed."""
if not self.is_installed("dedup_headers"):
# Create retry-wrapped cargo install function
@retry_download(max_attempts=DOWNLOAD_MAX_RETRIES, initial_delay=2.0)
def cargo_install_with_retry():
"""Run cargo install with retry on network failures."""
subprocess.run(
[
"cargo",
"install",
"--git",
"https://github.com/DataDog/libdatadog",
"--bin",
"dedup_headers",
"tools",
],
check=True,
)
cargo_install_with_retry()
def run(self) -> None:
"""Run the build process with additional post-processing."""
has_profiling_feature = False
for ext in self.distribution.rust_extensions:
if ext.features and "profiling" in ext.features:
has_profiling_feature = True
break
if IS_EDITABLE or getattr(self, "inplace", False):
self.inplace = True
super().run()
# Check if profiling is enabled and run dedup_headers
if has_profiling_feature:
self.install_dedup_headers()
# Add cargo binary folder to PATH
home = os.path.expanduser("~")
cargo_bin = os.path.join(home, ".cargo", "bin")
dedup_env = os.environ.copy()
dedup_env["PATH"] = cargo_bin + os.pathsep + os.environ["PATH"]
# Run dedup_headers on the generated headers
include_dir = CARGO_TARGET_DIR / "include" / "datadog"
if include_dir.exists():
subprocess.run(
["dedup_headers", "common.h", "profiling.h"],
cwd=str(include_dir),
check=True,
env=dedup_env,
)
class LibraryDownload:
CACHE_DIR = Path(os.getenv("DD_SETUP_CACHE_DIR", HERE / ".download_cache"))
USE_CACHE = os.getenv("DD_SETUP_CACHE_DOWNLOADS", "1").lower() in ("1", "yes", "on", "true")
name: t.Optional[str] = None
download_dir: Path = Path.cwd()
version: t.Optional[str] = None
url_root: t.Optional[str] = None
available_releases: dict[str, list[str]] = {}
expected_checksums: t.Optional[dict[str, dict[str, str]]] = None
translate_suffix: dict[str, tuple[str, ...]] = {}
@classmethod
def download_artifacts(cls):
suffixes = cls.translate_suffix[CURRENT_OS]
download_dir = Path(cls.download_dir)
download_dir.mkdir(parents=True, exist_ok=True) # No need to check if it exists
# If the version has changed since the last download, wipe and re-fetch.
# This ensures version bumps are picked up even in incremental builds where
# CleanLibraries.remove_artifacts() is skipped.
version_sentinel = download_dir / ".version"
if cls.version and version_sentinel.exists() and version_sentinel.read_text().strip() != cls.version:
shutil.rmtree(download_dir)
download_dir.mkdir(parents=True, exist_ok=True)
# If the directory is nonempty (beyond the sentinel), assume we're done
non_sentinel = [p for p in download_dir.iterdir() if p.name != ".version"]
if non_sentinel:
return
for arch in cls.available_releases[CURRENT_OS]:
if CURRENT_OS == "Linux" and not get_platform().endswith(arch):
# We cannot include the dynamic libraries for other architectures here.
continue
elif CURRENT_OS == "Darwin":
# Detect build type for macos:
# https://github.com/pypa/cibuildwheel/blob/main/cibuildwheel/macos.py#L250
target_platform = os.getenv("PLAT")
# Darwin Universal2 should bundle both architectures
if target_platform and not target_platform.endswith(("universal2", arch)):
continue
elif CURRENT_OS == "Windows":
if arch == "win32" and is_64_bit_python():
continue # Skip 32-bit builds on 64-bit Python
elif arch in ["x64", "arm64"] and not is_64_bit_python():
continue # Skip 64-bit builds on 32-bit Python
elif arch == "arm64" and platform.machine().lower() not in ["arm64", "aarch64"]:
continue # Skip ARM64 builds on non-ARM64 machines
elif arch == "x64" and platform.machine().lower() not in ["amd64", "x86_64"]:
continue # Skip x64 builds on non-x64 machines
arch_dir = download_dir / arch
# If the directory for the architecture exists and is nonempty, assume we're done
if arch_dir.is_dir() and any(arch_dir.iterdir()):
continue
archive_dir = cls.get_package_name(arch, CURRENT_OS)
archive_name = cls.get_archive_name(arch, CURRENT_OS)
download_address = "%s/%s/%s" % (
cls.url_root,
cls.version,
archive_name,
)
download_dest = cls.CACHE_DIR / archive_name if cls.USE_CACHE else Path(archive_name)
if cls.USE_CACHE and not cls.CACHE_DIR.exists():
cls.CACHE_DIR.mkdir(parents=True)
if not (cls.USE_CACHE and download_dest.exists()):
print(f"Downloading {archive_name} to {download_dest}")
start_ns = time.time_ns()
# Create retry-wrapped download function
@retry_download()
def download_file(url, dest):
"""Download file with automatic retry on transient errors."""
return urlretrieve(url, str(dest))
filename, _ = download_file(download_address, download_dest)
# Verify checksum of downloaded file
if cls.expected_checksums is None:
sha256_address = download_address + ".sha256"
sha256_dest = str(download_dest) + ".sha256"
sha256_filename, _ = download_file(sha256_address, sha256_dest)
verify_checksum_from_file(sha256_filename, str(download_dest))
else:
expected_checksum = cls.expected_checksums[CURRENT_OS][arch]
verify_checksum_from_hash(expected_checksum, str(download_dest))
DebugMetadata.download_times[archive_name] = time.time_ns() - start_ns
else:
# If the file exists in the cache, we will use it
filename = str(download_dest)
print(f"Using cached {filename}")
# Open the tarfile first to get the files needed.
# This could be solved with "r:gz" mode, that allows random access
# but that approach does not work on Windows
with tarfile.open(filename, mode="r|gz", errorlevel=2) as tar:
dynfiles = [c for c in tar.getmembers() if c.name.endswith(suffixes)]
with tarfile.open(filename, mode="r|gz", errorlevel=2) as tar:
tar.extractall(members=dynfiles, path=HERE)
Path(HERE / archive_dir).rename(arch_dir)
# Rename <name>.xxx to lib<name>.xxx so the filename is the same for every OS
lib_dir = arch_dir / "lib"
for suffix in suffixes:
original_file = lib_dir / "{}{}".format(cls.name, suffix)
if original_file.exists():
renamed_file = lib_dir / "lib{}{}".format(cls.name, suffix)
original_file.rename(renamed_file)
if not cls.USE_CACHE:
Path(filename).unlink()
# Record the version so future incremental runs can detect bumps.
if cls.version:
(download_dir / ".version").write_text(cls.version)
@classmethod
def run(cls) -> None:
cls.download_artifacts()
@classmethod
def get_package_name(cls, arch, os) -> str:
raise NotImplementedError()
@classmethod
def get_archive_name(cls, arch, os):
return cls.get_package_name(arch, os) + ".tar.gz"
class LibDDWafDownload(LibraryDownload):
name = "ddwaf"
download_dir = LIBDDWAF_DOWNLOAD_DIR
version = LIBDDWAF_VERSION
url_root = "https://github.com/DataDog/libddwaf/releases/download"
available_releases = {
"Windows": ["arm64", "win32", "x64"],
"Darwin": ["arm64", "x86_64"],
"Linux": ["aarch64", "x86_64"],
}
translate_suffix = {"Windows": (".dll",), "Darwin": (".dylib",), "Linux": (".so",)}
@classmethod
def get_package_name(cls, arch, os):
archive_dir = "lib%s-%s-%s-%s" % (cls.name, cls.version, os.lower(), arch)
return archive_dir
@classmethod
def get_archive_name(cls, arch, os):
os_name = os.lower()
if os_name == "linux":
archive_dir = "lib%s-%s-%s-linux-musl.tar.gz" % (cls.name, cls.version, arch)
else:
archive_dir = "lib%s-%s-%s-%s.tar.gz" % (cls.name, cls.version, os_name, arch)
return archive_dir
# Source/build file extensions that should never appear in a binary wheel.
# These live alongside .py files in package dirs but are only needed for compiling.
_WHEEL_EXCLUDED_EXTENSIONS = frozenset(
[
# C/C++ source and headers (compiled into .so extensions)
".c",
".h",
".cpp",
".cc",
".hpp",
# Cython source (compiled into .so extensions; .pxd kept for sdist only)
".pyx",
".pxd",
# Build system files
".cmake",
".sh",
# Developer tooling
".plantuml",
".supp",
]
)
class LibraryDownloader(BuildPyCommand):
def run(self) -> None:
# The setuptools docs indicate the `editable_mode` attribute of the build_py command class
# is set to True when the package is being installed in editable mode, which we need to know
# for some extensions
global IS_EDITABLE
if self.editable_mode:
IS_EDITABLE = True
# Skip wiping native extensions when incremental builds are enabled.
# The skip checks in build_extension / build_extension_cmake rely on the
# existing .so files (restored from ext_cache or left from the previous
# build) to determine whether recompilation is needed. Deleting them
# here defeats those checks and forces a full rebuild every time.
# LibraryDownload.download_artifacts() handles version bumps internally
# via a .version sentinel, so libddwaf is always re-fetched when its
# version changes even when CleanLibraries.remove_artifacts() is skipped.
if not CustomBuildExt.INCREMENTAL:
CleanLibraries.remove_artifacts()
LibDDWafDownload.run()
BuildPyCommand.run(self)
self._strip_build_artifacts()
def find_data_files(self, package, src_dir):
"""Strip build/source artifacts from wheel data files."""
files = BuildPyCommand.find_data_files(self, package, src_dir)
return [f for f in files if os.path.splitext(f)[1].lower() not in _WHEEL_EXCLUDED_EXTENSIONS]
def _strip_build_artifacts(self):
"""Remove source/build artifacts from the build_lib directory after copying.
find_data_files() handles most setuptools code paths, but the PEP 517
build backend may populate build_lib via a different route. This post-
processing pass guarantees the artifacts are absent from the final wheel.
"""
if not self.build_lib:
return
build_lib = Path(self.build_lib)
removed = 0
for path in build_lib.rglob("*"):
if path.is_file() and path.suffix.lower() in _WHEEL_EXCLUDED_EXTENSIONS:
path.unlink()
removed += 1
if removed:
print(f"Stripped {removed} build artifact(s) from wheel", flush=True)
class CleanLibraries(CleanCommand):
@staticmethod
def remove_native_extensions() -> None:
"""Remove native extensions and shared libraries installed by setup.py."""
for pattern in ("*.so", "*.pyd", "*.dylib", "*.dll"):
for path in DDTRACE_DIR.rglob(pattern):
# Avoid modifying vendored directories
if path.is_file() and not path.is_relative_to(VENDOR_DIR):
try:
path.unlink()
except OSError as e:
print(f"WARNING: could not remove {path}: {e}")
@staticmethod
def remove_artifacts() -> None:
shutil.rmtree(LIBDDWAF_DOWNLOAD_DIR, True)
CleanLibraries.remove_native_extensions()
@staticmethod
def remove_rust_targets() -> None:
"""Remove all Rust target dirs (target, target3.9, target3.10, etc.)."""
# rmtree is a superset of `cargo clean`; target* catches plain target and versioned
for target_dir in NATIVE_CRATE.glob("target*"):
if target_dir.is_dir():
shutil.rmtree(target_dir, True)
@staticmethod
def remove_build_artifacts() -> None:
"""Remove egg-info, dist, .eggs, *.egg, and CMake FetchContent cache.
The base distutils clean command does not remove these. They can cause
stale metadata and odd behavior on reinstall. Invoked only for
``clean --all`` to give a full reset before a fresh build.
"""
for path in (HERE / "ddtrace.egg-info", HERE / "dist", HERE / ".eggs"):
if path.exists():
shutil.rmtree(path, True)
for egg in HERE.glob("*.egg"):
if egg.is_file():
egg.unlink(missing_ok=True)
elif egg.is_dir():
shutil.rmtree(egg, True)
cmake_deps = LibraryDownload.CACHE_DIR / "_cmake_deps"
if cmake_deps.exists():
shutil.rmtree(cmake_deps, True)
@staticmethod
def remove_build_dir() -> None:
"""Remove the entire build/ tree for a clean slate.
The base CleanCommand only removes specific subdirs (build_temp, build_lib, etc.)
per runtime. We remove build/ wholesale so all build output is cleared.
"""
build_dir = HERE / "build"
if build_dir.exists():
shutil.rmtree(build_dir, True)
def run(self) -> None:
CleanLibraries.remove_rust_targets()
CleanLibraries.remove_artifacts()
CleanLibraries.remove_build_dir()
if self.all:
CleanLibraries.remove_build_artifacts()
@dataclass
class SharedDep:
"""Declarative description of a C++ library built once and shared across extensions.
Adding a new shared dependency requires:
1. A standalone ``cmake/<name>/CMakeLists.txt`` that fetches and installs the library.
2. An entry in ``SHARED_DEPS`` below.
3. An ``elseif(DEFINED <cmake_var>)`` branch using ``find_package()`` in each
consuming extension's CMakeLists.txt.
``ext_cache.py`` discovers all entries via the ``#SHAREDEPINFO:`` lines emitted by
``setup.py ext_hashes`` and caches or restores each install tree without any
per-dependency special-casing.
"""
name: str # short identifier used for cache paths and log messages
cmake_dir: Path # directory containing the standalone CMakeLists.txt
version: str # version string included in the configuration hash
cmake_var: str # cmake variable forwarded to consuming extensions (-D<cmake_var>=<install_dir>)
install_dir: Path # CMAKE_INSTALL_PREFIX for this dependency
# Platforms where this dep is needed; build_shared_deps() skips all others.
platforms: tuple[str, ...] = ("Linux", "Darwin")
# Optional callable: if it returns True the build is skipped (after the platform
# check). Encode environment-specific conditions here (e.g. debug mode, env flags).
should_skip: t.Optional[t.Callable[[], bool]] = None
def config_hash(self) -> str:
"""Stable hash of the build configuration, used as the cache key.
Covers dependency version, compile mode, platform, machine arch, and any
macOS ``ARCHFLAGS`` targets. Any change that would produce a different
binary invalidates the hash and triggers a rebuild.
"""
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
parts = [self.version, COMPILE_MODE, sys.platform, platform.machine()] + archs
return hashlib.sha256("|".join(parts).encode()).hexdigest()[:16]
def is_built(self) -> bool:
"""True if the install directory contains an up-to-date build."""
sentinel = self.install_dir / ".dep_build_info"
return sentinel.exists() and sentinel.read_text().strip() == self.config_hash()
def mark_built(self) -> None:
"""Write the sentinel file after a successful build."""
(self.install_dir / ".dep_build_info").write_text(self.config_hash())
# ---------------------------------------------------------------------------
# Shared C++ dependency declarations
# ---------------------------------------------------------------------------
def _absl_should_skip() -> bool:
"""Skip abseil in fast builds (DD_COMPILE_ABSEIL=0) and Debug mode."""
if os.environ.get("DD_COMPILE_ABSEIL", "1") in ("0", "false"):
return True
return COMPILE_MODE.lower() == "debug"
SHARED_DEPS: list[SharedDep] = [
SharedDep(
name="absl",
cmake_dir=HERE / "cmake" / "abseil",
version="20250127.1",
cmake_var="ABSL_INSTALL_DIR",
install_dir=LibraryDownload.CACHE_DIR / "_cmake_deps" / f"absl_install_{platform.machine()}",
platforms=("Linux", "Darwin"),
should_skip=_absl_should_skip,
),
]
class CustomBuildExt(build_ext):
INCREMENTAL = os.getenv("DD_CMAKE_INCREMENTAL_BUILD", "1").lower() in ("1", "yes", "on", "true")
def run(self) -> None:
with _time_phase("build_rust"):
self.build_rust()
# Build libdd_wrapper before building other extensions that depend on it
if CURRENT_OS in ("Linux", "Darwin") and is_64_bit_python() and sys.version_info < (3, 15):
with _time_phase("build_libdd_wrapper"):
self.build_libdd_wrapper()
# Build all declared shared C++ dependencies before extension builds.
with _time_phase("build_shared_deps"):
self.build_shared_deps()
# super().run() iterates self.extensions and calls self.build_extension()
# for each one — that is sufficient; no second loop needed.
with _time_phase("build_extensions"):
super().run()
def build_extensions(self) -> None:
# Enable parallel extension builds by default. All extensions are
# independent at this point (Rust and libdd_wrapper are already built
# in run()), so they can safely compile concurrently. The user can
# override via ``--parallel N`` / ``-j N`` on the command line, or
# set DD_BUILD_PARALLEL=0 to disable.
dd_build_parallel = os.getenv("DD_BUILD_PARALLEL")
if dd_build_parallel is not None:
try:
requested = int(dd_build_parallel)
except ValueError:
print(f"WARNING: DD_BUILD_PARALLEL={dd_build_parallel!r} is not a valid integer, ignoring")
requested = 0
self.parallel = requested if requested > 0 else False
elif not self.parallel:
self.parallel = _cpu_count
super().build_extensions()
def build_rust(self):
"""Build the Rust component using CustomBuildRust command."""
self.suffix = sysconfig.get_config_var("EXT_SUFFIX")
native_name = f"_native{self.suffix}"
if IS_EDITABLE or getattr(self, "inplace", False):
self.output_dir = Path(__file__).parent / "ddtrace" / "internal" / "native"
else:
self.output_dir = Path(__file__).parent / Path(self.build_lib) / "ddtrace" / "internal" / "native"
library = self.output_dir / native_name
# Determine the Rust source binary path - need to handle different architectures
rust_source = NATIVE_CRATE / "target" / "release" / f"lib_native{self.suffix}"
if not rust_source.exists():
# Fallback to generic .so extension for cross-platform compatibility
rust_source = NATIVE_CRATE / "target" / "release" / "lib_native.so"
# Check if we need to run the Rust build by checking if sources are newer than the destination
should_build = True
if library.exists():
library_mtime = library.stat().st_mtime
# Check if any Rust source files are newer than the destination library
cargo_files = [NATIVE_CRATE / "Cargo.toml", NATIVE_CRATE / "Cargo.lock"]
# Get all source files (including subdirectories)
source_files: list[Path] = []
# Find all .rs files in the crate (including subdirectories)
source_files.extend(NATIVE_CRATE.glob("**/*.rs"))
# Add cargo files
source_files.extend([f for f in cargo_files if f.exists()])
# Check if any source file is newer than the library
newest_source_time: float = 0.0
for src_file in source_files:
if src_file.exists():
newest_source_time = max(newest_source_time, src_file.stat().st_mtime)
required_headers = ["common.h"]
if "profiling" in rust_features:
required_headers.append("profiling.h")
include_dir = CARGO_TARGET_DIR / "include" / "datadog"
headers_exist = include_dir.exists() and all((include_dir / header).exists() for header in required_headers)
# Only rebuild if source files are newer than the destination OR if any required header is missing
should_build = newest_source_time > library_mtime or not headers_exist
if should_build:
# Create and run the CustomBuildRust command
build_rust_cmd = CustomBuildRust(self.distribution)
build_rust_cmd.initialize_options()
build_rust_cmd.finalize_options()
# Propagate the inplace flag to the rust build command
build_rust_cmd.inplace = getattr(self, "inplace", False)
build_rust_cmd.run()
if not library.exists():
raise RuntimeError("Not able to find native library")
print(f"Built and copied Rust extension: {native_name}")
self.built_native = True
else:
print(f"Skipping Rust extension build (no changes): {native_name}")
self.built_native = False
# Set SONAME (always do this as it's idempotent)
if CURRENT_OS == "Linux":
subprocess.run(["patchelf", "--set-soname", native_name, library], check=True)
elif CURRENT_OS == "Darwin":
subprocess.run(["install_name_tool", "-id", native_name, library], check=True)
def build_libdd_wrapper(self):
"""Build libdd_wrapper shared library as a dependency for profiling extensions."""
dd_wrapper_dir = DDUP_DIR.parent / "dd_wrapper"
# Determine output directory (profiling directory).
# Store as self.wrapper_output_dir so _get_common_cmake_args can pass
# DD_WRAPPER_DIR to downstream extensions (ddup, stack, memalloc).
if IS_EDITABLE or getattr(self, "inplace", False):
wrapper_output_dir = Path(__file__).parent / "ddtrace" / "internal" / "datadog" / "profiling"
else:
wrapper_output_dir = (
Path(__file__).parent / Path(self.build_lib) / "ddtrace" / "internal" / "datadog" / "profiling"
)
self.wrapper_output_dir = wrapper_output_dir
wrapper_name = f"libdd_wrapper{self.suffix}"
wrapper_library = wrapper_output_dir / wrapper_name
# Check if we need to build libdd_wrapper by checking if sources are newer
should_build = True
if wrapper_library.exists():
wrapper_mtime = wrapper_library.stat().st_mtime
# Check dd_wrapper source files
source_files: list[Path] = []
source_files.extend(dd_wrapper_dir.glob("**/*.cpp"))
source_files.extend(dd_wrapper_dir.glob("**/*.hpp"))
source_files.extend([dd_wrapper_dir / "CMakeLists.txt"])
# Check if any source file is newer than the wrapper library
newest_source_time: float = 0.0
for src_file in source_files:
if src_file.exists():
newest_source_time = max(newest_source_time, src_file.stat().st_mtime)
# Rebuild if source files changed OR if _native.so was rebuilt (our dependency)
source_files_changed = newest_source_time > wrapper_mtime
should_build = source_files_changed or getattr(self, "built_native", False)
if should_build:
# Build libdd_wrapper using CMake
cmake_build_dir = Path(self.build_lib.replace("lib.", "cmake."), "libdd_wrapper_build").resolve()
cmake_build_dir.mkdir(parents=True, exist_ok=True)
cmake_args = self._get_common_cmake_args(dd_wrapper_dir, cmake_build_dir, wrapper_output_dir, wrapper_name)
build_args = [f"--config {COMPILE_MODE}"]
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
if hasattr(self, "parallel") and self.parallel:
build_args += [f"-j{self.parallel}"]
install_args = [f"--config {COMPILE_MODE}"]
cmake_command = (Path(cmake.CMAKE_BIN_DIR) / "cmake").resolve() # type: ignore[attr-defined]