Skip to content

Commit 35465a3

Browse files
committed
feat: setup dotnet CI
1 parent 1c1fc18 commit 35465a3

File tree

9 files changed

+387
-7
lines changed

9 files changed

+387
-7
lines changed

.github/workflows/dotnet.yml

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: .NET
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
permissions:
11+
contents: read
12+
13+
env:
14+
OUTPUT_DIR: bin
15+
PACKAGES_DIR: packages
16+
17+
jobs:
18+
build:
19+
runs-on: ${{ matrix.os }}
20+
21+
strategy:
22+
matrix:
23+
include:
24+
- os: windows-latest
25+
target: x86_64-pc-windows-msvc
26+
- os: windows-latest
27+
target: i686-pc-windows-msvc
28+
# TODO: add more targets
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 #v4
32+
33+
- name: Install rustup
34+
if: matrix.os == 'windows-latest'
35+
run: |
36+
$ProgressPreference = "SilentlyContinue"
37+
Invoke-WebRequest https://win.rustup.rs/ -OutFile rustup-init.exe
38+
.\rustup-init.exe -y --default-host=${{ matrix.target }} --default-toolchain stable --profile minimal
39+
del rustup-init.exe
40+
rustup target add ${{ matrix.target }}
41+
shell: powershell
42+
43+
- name: Build
44+
run: ./windows/build-artifacts.ps1 ${{ env.OUTPUT_DIR }} ${{ matrix.target }}
45+
shell: pwsh
46+
47+
- name: Upload artifacts
48+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 #v4
49+
with:
50+
name: ${{ env.OUTPUT_DIR }}-${{ matrix.target }}
51+
path: |
52+
bin/*
53+
!bin/*/*/build
54+
!bin/*/*/deps
55+
!bin/*/*/examples
56+
!bin/*/*/incremental
57+
!bin/*/*/.fingerprint
58+
!bin/debug
59+
!bin/release
60+
61+
pack:
62+
runs-on: windows-latest
63+
needs: build
64+
outputs:
65+
NUGET_VERSION: ${{ steps.dotnet-pack.outputs.NUGET_VERSION }}
66+
steps:
67+
- name: Checkout code
68+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 #v4
69+
70+
- name: Download x86_64-pc-windows-msvc
71+
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 #v4
72+
with:
73+
name: ${{ env.OUTPUT_DIR }}-x86_64-pc-windows-msvc
74+
path: bin
75+
76+
- name: Download i686-pc-windows-msvc
77+
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 #v4
78+
with:
79+
name: ${{ env.OUTPUT_DIR }}-i686-pc-windows-msvc
80+
path: bin
81+
82+
- name: dotnet pack
83+
id: dotnet-pack
84+
run: |
85+
$cargo_content=Get-Content Cargo.toml -Raw
86+
$cargo_content -match '(?m)^version += +"([^"]+)"'
87+
$current_version=$Matches[1]
88+
$version_suffix="ci.${{ github.event.number }}.${{ github.run_number }}"
89+
$version="$current_version-$version_suffix"
90+
echo "NUGET_VERSION=$version" >> $env:GITHUB_OUTPUT
91+
dotnet pack windows/libdatadog.csproj -p:LibDatadogBinariesOutputDir=../${{ env.OUTPUT_DIR }} -p:LibDatadogVersion=$version -o ${{ env.PACKAGES_DIR }}
92+
93+
- name: Upload package
94+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
95+
with:
96+
name: ${{ env.PACKAGES_DIR }}
97+
path: ${{ env.PACKAGES_DIR }}
98+
99+
test:
100+
runs-on: ${{ matrix.os }}
101+
needs: pack
102+
103+
strategy:
104+
matrix:
105+
include:
106+
- os: windows-latest
107+
target: x86_64-pc-windows-msvc
108+
- os: windows-latest
109+
target: i686-pc-windows-msvc
110+
111+
steps:
112+
- name: Checkout code
113+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 #v4
114+
115+
- name: Download package
116+
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 #v4
117+
with:
118+
name: ${{ env.PACKAGES_DIR }}
119+
path: ${{ env.PACKAGES_DIR }}
120+
121+
- name: Install .NET SDK
122+
uses: actions/setup-dotnet@3e891b0cb619bf60e2c25674b222b8940e2c1c25 #v4
123+
with:
124+
dotnet-version: 7.0.X
125+
126+
- name: Run
127+
run: |
128+
cd tests/nuget_package
129+
dotnet add nuget_package.csproj package libdatadog --version ${{ needs.pack.outputs.NUGET_VERSION }}
130+
dotnet run

nuget.config

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
5+
<clear />
6+
<add key="local" value="packages" />
7+
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
8+
</packageSources>
9+
</configuration>

tests/nuget_package/Program.cs

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using System.Runtime.InteropServices;
3+
4+
var url = new CharSlice("http://localhost:8126");
5+
var tracerVersion = new CharSlice("1.0.0");
6+
var language = new CharSlice(".NET");
7+
var languageVersion = new CharSlice("5.0.0");
8+
var languageInterpreter = new CharSlice(".NET");
9+
var hostname = new CharSlice("localhost");
10+
var env = new CharSlice("development");
11+
var service = new CharSlice("dotnet-test");
12+
var serviceVersion = new CharSlice("1.0.0");
13+
14+
var handle = IntPtr.Zero;
15+
16+
Console.WriteLine("Creating exporter");
17+
var error = Native.ddog_trace_exporter_new(
18+
outHandle: ref handle,
19+
url: url,
20+
tracerVersion: tracerVersion,
21+
language: language,
22+
languageVersion: languageVersion,
23+
languageInterpreter: languageInterpreter,
24+
hostname: hostname,
25+
env: env,
26+
version: serviceVersion,
27+
service: service,
28+
inputFormat: TraceExporterInputFormat.V04,
29+
outputFormat: TraceExporterOutputFormat.V04,
30+
computeStats: false,
31+
agentResponseCallback: (IntPtr chars) =>
32+
{
33+
var response = Marshal.PtrToStringUni(chars);
34+
Console.WriteLine(response);
35+
}
36+
);
37+
38+
if (error.Tag == ErrorTag.Some)
39+
{
40+
Console.WriteLine("Error creating exporter");
41+
Console.WriteLine(Marshal.PtrToStringUni(error.Message.Ptr));
42+
Native.ddog_MaybeError_drop(error);
43+
return;
44+
}
45+
46+
if (handle == IntPtr.Zero)
47+
{
48+
Console.WriteLine("Error creating exporter");
49+
return;
50+
}
51+
52+
Console.WriteLine("Exporter created");
53+
54+
Console.WriteLine("Freeing exporter");
55+
Native.ddog_trace_exporter_free(handle);
56+
Console.WriteLine("Exporter freed");
57+
Console.WriteLine("Done");
58+
59+
internal enum TraceExporterInputFormat
60+
{
61+
Proxy = 0,
62+
V04 = 1,
63+
}
64+
65+
internal enum TraceExporterOutputFormat
66+
{
67+
V04 = 0,
68+
V07 = 1,
69+
}
70+
71+
internal delegate void AgentResponseCallback(IntPtr response);
72+
73+
internal static class Native
74+
{
75+
private const string DllName = "datadog_profiling_ffi";
76+
77+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
78+
internal static extern MaybeError ddog_trace_exporter_new(
79+
ref IntPtr outHandle,
80+
CharSlice url,
81+
CharSlice tracerVersion,
82+
CharSlice language,
83+
CharSlice languageVersion,
84+
CharSlice languageInterpreter,
85+
CharSlice hostname,
86+
CharSlice env,
87+
CharSlice version,
88+
CharSlice service,
89+
TraceExporterInputFormat inputFormat,
90+
TraceExporterOutputFormat outputFormat,
91+
bool computeStats,
92+
AgentResponseCallback agentResponseCallback);
93+
94+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
95+
internal static extern void ddog_MaybeError_drop(MaybeError error);
96+
97+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
98+
internal static extern void ddog_trace_exporter_free(IntPtr handle);
99+
}
100+
101+
[StructLayout(LayoutKind.Sequential)]
102+
internal struct CharSlice
103+
{
104+
internal IntPtr Ptr;
105+
internal UIntPtr Len;
106+
107+
internal CharSlice(string str)
108+
{
109+
var bytes = System.Text.Encoding.UTF8.GetBytes(str);
110+
Ptr = Marshal.AllocHGlobal(bytes.Length);
111+
Marshal.Copy(bytes, 0, Ptr, bytes.Length);
112+
Len = (UIntPtr)bytes.Length;
113+
}
114+
}
115+
116+
internal enum ErrorTag
117+
{
118+
Some = 0,
119+
None = 1,
120+
}
121+
122+
[StructLayout(LayoutKind.Sequential)]
123+
internal struct MaybeError
124+
{
125+
internal ErrorTag Tag;
126+
internal CharSlice Message;
127+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="libdatadog" Version="1.0.0-ci.1.1" />
12+
</ItemGroup>
13+
14+
</Project>

windows/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# libdatadog
2+
3+
## About
4+
5+
This package is binary distribution of [libdatadog](https://github.com/DataDog/libdatadog). It supports both native (C/C++) and .NET projects.
6+
7+
## Getting started
8+
9+
### Native (C/C++) projects
10+
11+
For Native projects, `libdatadog` supports both static and dynamic linking. The dynamic linking is the default option. To use the static linking, you need to disable the dynamic linking by setting the msbuild property `LibDatadogDynamicLink` to `false`. Both `debug` and `release` congfiguration libraries are provided to allow better debugging experience.
12+
13+
Depending on the target platform, binaries are compied to project output directory.
14+
15+
### .NET projects
16+
17+
For .NET projects, `libdatadog` supports only dynamic linking targetting .NET Standard 2.0.
18+
19+
Depending on the target platform, binaries are copied to project output directory under `runtimes` directory as per [.NET conventions](https://learn.microsoft.com/en-us/nuget/create-packages/native-files-in-net-packages#native-assets) (`runtimes/{RID}/native`).
20+
21+
> [!IMPORTANT]
22+
> `nuget` fails to resolve the shared library if same directory is not used in the nuget package and the project output directory. Hence, only `release` configuration is supported for .NET projects.
23+
24+
## Package content
25+
26+
```
27+
libdatadog.14.3.1-ci.771.90/
28+
├── build
29+
│   ├── native
30+
│   │   ├── lib
31+
│   │   │   ├── x64
32+
│   │   │   │   ├── debug
33+
│   │   │   │   │   ├── libdatadog win-x64 shared library and pdb (debug)
34+
│   │   │   │   │   └── static
35+
│   │   │   │   │   └── libdatadog win-x64 static library (debug)
36+
│   │   │   │   └── release
37+
│   │   │   │   ├── libdatadog win-x64 shared library and pdb (release)
38+
│   │   │   │   └── static
39+
│   │   │   │   └── libdatadog win-x64 static library (release)
40+
│   │   │   └── x86
41+
│   │   │   ├── debug
42+
│   │   │   │   ├── libdatadog win-x86 shared library and pdb (debug)
43+
│   │   │   │   └── static
44+
│   │   │   │   └── libdatadog win-x86 static library (debug)
45+
│   │   │   └── release
46+
│   │   │   ├── libdatadog win-x86 shared library and pdb (release)
47+
│   │   │   └── static
48+
│   │   │   └── libdatadog win-x86 static library (release)
49+
│   │   └── libdatadog.props
50+
│   └── netstandard2.0
51+
│   └── libdatadog.props
52+
├── include
53+
│   └── native
54+
│   └── datadog
55+
│   ├── C headers
56+
└── runtimes
57+
├── win-x64
58+
│   └── native
59+
│   └── libdatadog win-x64 shared library and pdb
60+
└── win-x86
61+
└── native
62+
└── libdatadog win-x86 shared library and pdb
63+
```
64+
65+
## License
66+
67+
Apache-2.0

windows/build-artifacts.ps1

+10-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ function Invoke-Call {
99
}
1010

1111
$output_dir = $args[0]
12+
$target = $args[1]
1213

1314
if ([string]::IsNullOrEmpty($output_dir)) {
1415
throw "You must specify an output directory. Ex: $($myInvocation.InvocationName) my_rust_project/ bin"
1516
}
1617

18+
$targets = @("i686-pc-windows-msvc", "x86_64-pc-windows-msvc")
19+
if (![string]::IsNullOrEmpty($target)) {
20+
$targets = @($target)
21+
}
22+
1723
if (![System.IO.Path]::IsPathRooted($output_dir)) {
1824
$output_dir = [System.IO.Path]::Combine($(Get-Location), $output_dir)
1925
}
@@ -32,10 +38,10 @@ $features = @(
3238
Write-Host "Building for features: $features" -ForegroundColor Magenta
3339

3440
pushd profiling-ffi
35-
Invoke-Call -ScriptBlock { cargo build --features $features --target i686-pc-windows-msvc --release --target-dir $output_dir }
36-
Invoke-Call -ScriptBlock { cargo build --features $features --target i686-pc-windows-msvc --target-dir $output_dir }
37-
Invoke-Call -ScriptBlock { cargo build --features $features --target x86_64-pc-windows-msvc --release --target-dir $output_dir }
38-
Invoke-Call -ScriptBlock { cargo build --features $features --target x86_64-pc-windows-msvc --target-dir $output_dir }
41+
foreach ($target in $targets) {
42+
Invoke-Call -ScriptBlock { cargo build --features $features --target $target --release --target-dir $output_dir }
43+
Invoke-Call -ScriptBlock { cargo build --features $features --target $target --target-dir $output_dir }
44+
}
3945
popd
4046

4147
Write-Host "Building tools" -ForegroundColor Magenta

0 commit comments

Comments
 (0)