Skip to content

Commit 18a5e9a

Browse files
authored
ci: use Python version specific CARGO_TARGET_DIR (#14953)
## Description Use Python version specific `CARGO_TARGET_DIR` for src/native, Rust extension module. This has a few benefits 1. Parallelizing build_base_venv jobs. While working on #13984, I noticed that since build_base_venv jobs on GitLab run in the same dd-trace-py directory and use the same `CARGO_TARGET_DIR`, and there's cargo lock, only one of them could run at a time. 2. Re-use Rust build artifacts as much as possible. Consider a scenario one builds dd-trace-py using multiple different Python versions in a row. In that case, the build had to spend extra time after switching to a different Python version, as src/native has to be re-linked and re-built with different Python binaries. <!-- Provide an overview of the change and motivation for the change --> ## Testing <!-- Describe your testing strategy or note what tests are included --> ## Risks <!-- Note any risks associated with this change, or "None" if no risks --> ## Additional Notes <!-- Any other information that would be helpful for reviewers -->
1 parent 7451e84 commit 18a5e9a

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,6 @@ tests/appsec/iast/fixtures/taint_sinks/not_exists.txt
198198

199199
*.debug
200200
*.dSYM/
201+
202+
# Rust build artifacts
203+
src/native/target*

ddtrace/internal/datadog/profiling/cmake/FindLibNative.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ message(WARNING "LIBRARY_NAME: ${LIBRARY_NAME}")
2323

2424
# We expect the native extension to be built and installed the headers in the following directory. It is configured in
2525
# setup.py by setting CARGO_TARGET_DIR environment variable.
26-
set(SOURCE_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/../../../../../src/native/target/include)
26+
set(SOURCE_INCLUDE_DIR
27+
${CMAKE_SOURCE_DIR}/../../../../../src/native/target${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}/include)
2728

2829
set(DEST_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR})
2930
set(DEST_INCLUDE_DIR ${DEST_LIB_DIR}/include)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ if(APPLE)
6969
elseif(UNIX)
7070
set_target_properties(${EXTENSION_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/..")
7171
endif()
72-
target_include_directories(${EXTENSION_NAME} PRIVATE ../dd_wrapper/include ../../../../../src/native/target/include/
73-
${Datadog_INCLUDE_DIRS} ${Python3_INCLUDE_DIRS})
72+
target_include_directories(
73+
${EXTENSION_NAME}
74+
PRIVATE ../dd_wrapper/include
75+
../../../../../src/native/target${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}/include/
76+
${Datadog_INCLUDE_DIRS} ${Python3_INCLUDE_DIRS})
7477

7578
target_link_libraries(${EXTENSION_NAME} PRIVATE dd_wrapper)
7679

setup.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
DDUP_DIR = HERE / "ddtrace" / "internal" / "datadog" / "profiling" / "ddup"
9393
STACK_V2_DIR = HERE / "ddtrace" / "internal" / "datadog" / "profiling" / "stack_v2"
9494
NATIVE_CRATE = HERE / "src" / "native"
95+
CARGO_TARGET_DIR = NATIVE_CRATE.absolute() / f"target{sys.version_info.major}.{sys.version_info.minor}"
9596

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

@@ -208,7 +209,7 @@ def __init__(self, attrs=None):
208209
# but at the same time dropped support for Python 3.8. So we'd need to
209210
# make sure that this env var is set to install the ffi headers in the
210211
# right place.
211-
os.environ["CARGO_TARGET_DIR"] = str(NATIVE_CRATE.absolute() / "target")
212+
os.environ["CARGO_TARGET_DIR"] = str(CARGO_TARGET_DIR)
212213
self.rust_extensions = [
213214
RustExtension(
214215
# The Python import path of your extension:
@@ -235,7 +236,9 @@ def run(self):
235236
sources = [
236237
_
237238
for _ in source_path.glob("**/*")
238-
if _.is_file() and _.relative_to(source_path).parts[0] != "target"
239+
if _.is_file()
240+
and _.relative_to(source_path).parts[0]
241+
!= f"target{sys.version_info.major}.{sys.version_info.minor}"
239242
]
240243
else:
241244
sources = [Path(_) for _ in ext.sources]
@@ -318,7 +321,7 @@ def run(self):
318321
dedup_env["PATH"] = cargo_bin + os.pathsep + os.environ["PATH"]
319322

320323
# Run dedup_headers on the generated headers
321-
include_dir = NATIVE_CRATE.absolute() / "target" / "include" / "datadog"
324+
include_dir = CARGO_TARGET_DIR / "include" / "datadog"
322325
if include_dir.exists():
323326
subprocess.run(
324327
["dedup_headers", "common.h", "profiling.h"],
@@ -499,8 +502,7 @@ def remove_artifacts():
499502
@staticmethod
500503
def remove_rust():
501504
"""Clean the Rust crate using cargo clean."""
502-
target_dir = NATIVE_CRATE / "target"
503-
if target_dir.exists():
505+
if CARGO_TARGET_DIR.exists():
504506
subprocess.run(
505507
["cargo", "clean"],
506508
cwd=str(NATIVE_CRATE),

0 commit comments

Comments
 (0)