Skip to content

Commit 01042af

Browse files
committed
windows support
1 parent 3342af7 commit 01042af

File tree

9 files changed

+288
-282
lines changed

9 files changed

+288
-282
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ jobs:
257257
run: |
258258
cd examples/cxx
259259
.\build-and-run-crashinfo.ps1
260+
- name: "Test building CXX bindings - Profiling (Windows)"
261+
shell: pwsh
262+
if: matrix.platform == 'windows-latest'
263+
run: |
264+
cd examples/cxx
265+
.\build-profiling.ps1
260266
261267
cross-centos7:
262268
name: build and test using cross - on centos7

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ docker-sync.yml
2525
libtest.so
2626
libtest_cpp.so
2727
examples/cxx/crashinfo
28+
examples/cxx/crashinfo.exe
2829
examples/cxx/profiling
30+
examples/cxx/profiling.exe
2931
profile.pprof

examples/cxx/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@ Windows:
4848
Demonstrates building profiling data using the CXX bindings for `libdd-profiling`.
4949

5050
**Build and run:**
51+
52+
Unix (Linux/macOS):
5153
```bash
5254
./build-profiling.sh
5355
```
5456

57+
Windows:
58+
```powershell
59+
.\build-profiling.ps1
60+
```
61+
5562
**Key features:**
5663
- Type-safe API for building profiles
5764
- Support for samples, locations, mappings, and labels
@@ -61,6 +68,17 @@ Demonstrates building profiling data using the CXX bindings for `libdd-profiling
6168
- Exception-based error handling
6269
- Modern C++20 syntax with designated initializers and `std::format`
6370

71+
## Build Scripts
72+
73+
The examples use a consolidated build system:
74+
75+
- **Unix (Linux/macOS)**: `build-and-run.sh <crate-name> <example-name>`
76+
- **Windows**: `build-and-run.ps1 -CrateName <crate-name> -ExampleName <example-name>`
77+
78+
Convenience wrappers are provided for each example:
79+
- `build-and-run-crashinfo.sh` / `build-and-run-crashinfo.ps1`
80+
- `build-profiling.sh` / `build-profiling.ps1`
81+
6482
## Requirements
6583

6684
- C++20 or later
Lines changed: 3 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,7 @@
11
# Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
22
# SPDX-License-Identifier: Apache-2.0
33

4-
# Build and run the CXX crashinfo example on Windows
5-
$ErrorActionPreference = "Stop"
6-
4+
# Build and run the CXX crashinfo example
75
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path
8-
$PROJECT_ROOT = (Get-Item (Join-Path $SCRIPT_DIR ".." "..")).FullName
9-
Set-Location $PROJECT_ROOT
10-
11-
Write-Host "🔨 Building libdd-crashtracker with cxx feature..." -ForegroundColor Cyan
12-
cargo build -p libdd-crashtracker --features cxx --release
13-
14-
Write-Host "🔍 Finding CXX bridge headers..." -ForegroundColor Cyan
15-
$CXX_BRIDGE_INCLUDE = Get-ChildItem -Path "target\release\build\libdd-crashtracker-*\out\cxxbridge\include" -Directory -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName
16-
$CXX_BRIDGE_CRATE = Get-ChildItem -Path "target\release\build\libdd-crashtracker-*\out\cxxbridge\crate" -Directory -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName
17-
$RUST_CXX_INCLUDE = Get-ChildItem -Path "target\release\build\cxx-*\out" -Directory -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName
18-
19-
if (-not $CXX_BRIDGE_INCLUDE -or -not $CXX_BRIDGE_CRATE -or -not $RUST_CXX_INCLUDE) {
20-
Write-Host "❌ Error: Could not find CXX bridge directories" -ForegroundColor Red
21-
exit 1
22-
}
23-
24-
Write-Host "📁 CXX include: $CXX_BRIDGE_INCLUDE" -ForegroundColor Green
25-
Write-Host "📁 CXX crate: $CXX_BRIDGE_CRATE" -ForegroundColor Green
26-
Write-Host "📁 Rust CXX: $RUST_CXX_INCLUDE" -ForegroundColor Green
27-
28-
# Check if we have MSVC (cl.exe) or MinGW (g++/clang++)
29-
# Note: Prefer MSVC on Windows as it's the default Rust toolchain
30-
$MSVC = Get-Command cl.exe -ErrorAction SilentlyContinue
31-
$GPP = Get-Command g++.exe -ErrorAction SilentlyContinue
32-
$CLANGPP = Get-Command clang++.exe -ErrorAction SilentlyContinue
33-
34-
# Auto-detect which toolchain Rust used by checking which library exists
35-
# Note: On Windows, Rust still uses 'lib' prefix even for MSVC .lib files
36-
$HAS_MSVC_LIB = Test-Path (Join-Path $PROJECT_ROOT "target\release\libdd_crashtracker.lib")
37-
$HAS_GNU_LIB = (Test-Path (Join-Path $PROJECT_ROOT "target\release\libdd_crashtracker.a")) -or `
38-
(Test-Path (Join-Path $PROJECT_ROOT "target\release\liblibdd_crashtracker.a"))
39-
40-
if ($HAS_MSVC_LIB -and $MSVC) {
41-
$USE_MSVC = $true
42-
Write-Host "Detected MSVC Rust toolchain" -ForegroundColor Cyan
43-
} elseif ($HAS_GNU_LIB -and ($GPP -or $CLANGPP)) {
44-
$USE_MSVC = $false
45-
Write-Host "Detected GNU Rust toolchain" -ForegroundColor Cyan
46-
} elseif ($MSVC) {
47-
$USE_MSVC = $true
48-
Write-Host "Defaulting to MSVC (library not found yet, will check after)" -ForegroundColor Yellow
49-
} elseif ($GPP -or $CLANGPP) {
50-
$USE_MSVC = $false
51-
Write-Host "Defaulting to GNU toolchain (library not found yet, will check after)" -ForegroundColor Yellow
52-
} else {
53-
Write-Host "❌ Error: No C++ compiler found. Please install MSVC (via Visual Studio) or MinGW/LLVM" -ForegroundColor Red
54-
exit 1
55-
}
56-
57-
Write-Host "🔨 Finding libraries..." -ForegroundColor Cyan
58-
# Note: Rust library naming varies by platform and toolchain
59-
if ($USE_MSVC) {
60-
# MSVC: libdd_crashtracker.lib (Rust keeps the lib prefix even on Windows)
61-
$CRASHTRACKER_LIB = Join-Path $PROJECT_ROOT "target\release\libdd_crashtracker.lib"
62-
$CXX_BRIDGE_LIB = Get-ChildItem -Path "target\release\build\libdd-crashtracker-*\out" -Filter "libdd-crashtracker-cxx.lib" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName
63-
} else {
64-
# MinGW: Try both possible naming patterns
65-
$CRASHTRACKER_LIB_1 = Join-Path $PROJECT_ROOT "target\release\libdd_crashtracker.a"
66-
$CRASHTRACKER_LIB_2 = Join-Path $PROJECT_ROOT "target\release\liblibdd_crashtracker.a"
67-
68-
if (Test-Path $CRASHTRACKER_LIB_1) {
69-
$CRASHTRACKER_LIB = $CRASHTRACKER_LIB_1
70-
} elseif (Test-Path $CRASHTRACKER_LIB_2) {
71-
$CRASHTRACKER_LIB = $CRASHTRACKER_LIB_2
72-
} else {
73-
$CRASHTRACKER_LIB = $CRASHTRACKER_LIB_1 # Use this for error message
74-
}
75-
76-
# Try both naming patterns for CXX bridge
77-
$CXX_BRIDGE_LIB = Get-ChildItem -Path "target\release\build\libdd-crashtracker-*\out" -Filter "libdd-crashtracker-cxx.a" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName
78-
if (-not $CXX_BRIDGE_LIB) {
79-
$CXX_BRIDGE_LIB = Get-ChildItem -Path "target\release\build\libdd-crashtracker-*\out" -Filter "liblibdd-crashtracker-cxx.a" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName
80-
}
81-
}
82-
83-
if (-not (Test-Path $CRASHTRACKER_LIB)) {
84-
Write-Host "❌ Error: Could not find libdd-crashtracker library at $CRASHTRACKER_LIB" -ForegroundColor Red
85-
if (-not $MSVC) {
86-
Write-Host "Searched for: libdd_crashtracker.a and liblibdd_crashtracker.a" -ForegroundColor Yellow
87-
Write-Host "Files in target/release/:" -ForegroundColor Yellow
88-
Get-ChildItem -Path "target\release" -Filter "*crashtracker*" | Select-Object -First 10 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
89-
}
90-
exit 1
91-
}
92-
93-
if (-not $CXX_BRIDGE_LIB) {
94-
if ($USE_MSVC) {
95-
Write-Host "❌ Error: Could not find CXX bridge library (looking for libdd-crashtracker-cxx.lib)" -ForegroundColor Red
96-
} else {
97-
Write-Host "❌ Error: Could not find CXX bridge library" -ForegroundColor Red
98-
Write-Host "Searched for: libdd-crashtracker-cxx.a and liblibdd-crashtracker-cxx.a" -ForegroundColor Yellow
99-
}
100-
exit 1
101-
}
102-
103-
Write-Host "📚 Crashtracker library: $CRASHTRACKER_LIB" -ForegroundColor Green
104-
Write-Host "📚 CXX bridge library: $CXX_BRIDGE_LIB" -ForegroundColor Green
105-
106-
Write-Host "🔨 Compiling C++ example..." -ForegroundColor Cyan
107-
108-
if ($USE_MSVC) {
109-
Write-Host "Using MSVC compiler" -ForegroundColor Yellow
110-
111-
# MSVC compilation
112-
cl.exe /std:c++20 /EHsc `
113-
/I"$CXX_BRIDGE_INCLUDE" `
114-
/I"$CXX_BRIDGE_CRATE" `
115-
/I"$RUST_CXX_INCLUDE" `
116-
/I"$PROJECT_ROOT" `
117-
examples\cxx\crashinfo.cpp `
118-
"$CRASHTRACKER_LIB" `
119-
"$CXX_BRIDGE_LIB" `
120-
ws2_32.lib advapi32.lib userenv.lib ntdll.lib bcrypt.lib `
121-
/Fe:examples\cxx\crashinfo.exe
122-
123-
if ($LASTEXITCODE -ne 0) {
124-
Write-Host "❌ Compilation failed" -ForegroundColor Red
125-
exit 1
126-
}
127-
} elseif ($GPP -or $CLANGPP) {
128-
$COMPILER = if ($GPP) { "g++" } else { "clang++" }
129-
Write-Host "Using $COMPILER compiler" -ForegroundColor Yellow
130-
131-
# MinGW/Clang compilation - needs proper library ordering and Rust std lib
132-
& $COMPILER -std=c++20 `
133-
-I"$CXX_BRIDGE_INCLUDE" `
134-
-I"$CXX_BRIDGE_CRATE" `
135-
-I"$RUST_CXX_INCLUDE" `
136-
-I"$PROJECT_ROOT" `
137-
examples/cxx/crashinfo.cpp `
138-
"$CXX_BRIDGE_LIB" `
139-
"$CRASHTRACKER_LIB" `
140-
-lws2_32 -ladvapi32 -luserenv -lntdll -lbcrypt -lgcc_eh -lpthread `
141-
-o examples/cxx/crashinfo.exe
142-
143-
if ($LASTEXITCODE -ne 0) {
144-
Write-Host "❌ Compilation failed" -ForegroundColor Red
145-
exit 1
146-
}
147-
}
148-
149-
Write-Host "🚀 Running example..." -ForegroundColor Cyan
150-
& ".\examples\cxx\crashinfo.exe"
151-
152-
if ($LASTEXITCODE -ne 0) {
153-
Write-Host "❌ Example failed with exit code $LASTEXITCODE" -ForegroundColor Red
154-
exit 1
155-
}
156-
157-
Write-Host ""
158-
Write-Host "✅ Success!" -ForegroundColor Green
159-
6+
& "$SCRIPT_DIR\build-and-run.ps1" -CrateName "libdd-crashtracker" -ExampleName "crashinfo"
7+
exit $LASTEXITCODE

examples/cxx/build-and-run-crashinfo.sh

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,5 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
# Build and run the CXX crashinfo example
6-
set -e
7-
86
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9-
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
10-
cd "$PROJECT_ROOT"
11-
12-
echo "🔨 Building libdd-crashtracker with cxx feature..."
13-
cargo build -p libdd-crashtracker --features cxx --release
14-
15-
echo "🔍 Finding CXX bridge headers..."
16-
CXX_BRIDGE_INCLUDE=$(find target/release/build/libdd-crashtracker-*/out/cxxbridge/include -type d 2>/dev/null | head -n 1)
17-
CXX_BRIDGE_CRATE=$(find target/release/build/libdd-crashtracker-*/out/cxxbridge/crate -type d 2>/dev/null | head -n 1)
18-
RUST_CXX_INCLUDE=$(find target/release/build/cxx-*/out -type d 2>/dev/null | head -n 1)
19-
20-
if [ -z "$CXX_BRIDGE_INCLUDE" ] || [ -z "$CXX_BRIDGE_CRATE" ] || [ -z "$RUST_CXX_INCLUDE" ]; then
21-
echo "❌ Error: Could not find CXX bridge directories"
22-
exit 1
23-
fi
24-
25-
echo "📁 CXX include: $CXX_BRIDGE_INCLUDE"
26-
echo "📁 CXX crate: $CXX_BRIDGE_CRATE"
27-
echo "📁 Rust CXX: $RUST_CXX_INCLUDE"
28-
29-
echo "🔨 Finding libraries..."
30-
CRASHTRACKER_LIB="$PROJECT_ROOT/target/release/liblibdd_crashtracker.a"
31-
CXX_BRIDGE_LIB=$(find target/release/build/libdd-crashtracker-*/out -name "liblibdd-crashtracker-cxx.a" | head -n 1)
32-
33-
if [ ! -f "$CRASHTRACKER_LIB" ]; then
34-
echo "❌ Error: Could not find libdd-crashtracker library at $CRASHTRACKER_LIB"
35-
exit 1
36-
fi
37-
38-
if [ ! -f "$CXX_BRIDGE_LIB" ]; then
39-
echo "❌ Error: Could not find CXX bridge library"
40-
exit 1
41-
fi
42-
43-
echo "📚 Crashtracker library: $CRASHTRACKER_LIB"
44-
echo "📚 CXX bridge library: $CXX_BRIDGE_LIB"
45-
46-
echo "🔨 Compiling C++ example..."
47-
# Platform-specific linker flags
48-
if [[ "$OSTYPE" == "darwin"* ]]; then
49-
PLATFORM_LIBS="-framework Security -framework CoreFoundation"
50-
else
51-
PLATFORM_LIBS=""
52-
fi
53-
54-
c++ -std=c++20 \
55-
-I"$CXX_BRIDGE_INCLUDE" \
56-
-I"$CXX_BRIDGE_CRATE" \
57-
-I"$RUST_CXX_INCLUDE" \
58-
-I"$PROJECT_ROOT" \
59-
examples/cxx/crashinfo.cpp \
60-
"$CRASHTRACKER_LIB" \
61-
"$CXX_BRIDGE_LIB" \
62-
-lpthread -ldl $PLATFORM_LIBS \
63-
-o examples/cxx/crashinfo
64-
65-
echo "🚀 Running example..."
66-
./examples/cxx/crashinfo
67-
68-
echo ""
69-
echo "✅ Success!"
7+
exec "$SCRIPT_DIR/build-and-run.sh" libdd-crashtracker crashinfo

0 commit comments

Comments
 (0)