Skip to content

Commit abd3e73

Browse files
committed
Add script to build Ultra.Sampler NativeAOT library
1 parent a5bc97a commit abd3e73

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

src/Ultra.Sampler/MacOS/NativeModuleEvent.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the BSD-Clause 2 license.
33
// See license.txt file in the project root for full license information.
44

5+
using System.Text;
6+
57
namespace Ultra.Sampler.MacOS;
68

79
internal struct NativeModuleEvent
@@ -13,6 +15,6 @@ internal struct NativeModuleEvent
1315

1416
public override string ToString()
1517
{
16-
return $"{nameof(LoadAddress)}: 0x{LoadAddress:X8}, {nameof(Path)}: {Path}, {nameof(TimestampUtc)}: {TimestampUtc:O}";
18+
return $"{nameof(LoadAddress)}: 0x{LoadAddress:X8}, {nameof(Path)}: {Encoding.UTF8.GetString(Path ?? [])}, {nameof(TimestampUtc)}: {TimestampUtc:O}";
1719
}
1820
}

src/Ultra.Sampler/Ultra.Sampler.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<IsPackable>false</IsPackable>
9+
<PublishAot>true</PublishAot>
10+
<AssemblyName>libUltraSampler</AssemblyName>
11+
<EventSourceSupport>true</EventSourceSupport>
12+
<_SuppressNativeLibEventSourceWarning>true</_SuppressNativeLibEventSourceWarning>
13+
<InvariantGlobalization>true</InvariantGlobalization>
914
</PropertyGroup>
1015

1116
<ItemGroup>

src/Ultra.Sampler/build_native.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env sh
2+
dotnet publish -c Release -r osx-arm64
3+
LIB_NAME=libUltraSampler.dylib
4+
LIB_OUTPUT_PATH=./bin/Release/net8.0/osx-arm64/publish/$LIB_NAME
5+
install_name_tool -id $LIB_NAME $LIB_OUTPUT_PATH
6+
cp $LIB_OUTPUT_PATH ./$LIB_NAME
7+
clang -shared -O2 -o libUltraSamplerIndirect.dyld ultra_sampler_indirect.cpp ./$LIB_NAME
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <errno.h>
5+
#include <sys/stat.h>
6+
#include <sys/types.h>
7+
#include <unistd.h>
8+
9+
extern "C" {
10+
void ultra_sampler_start();
11+
12+
13+
void set_temporary_tmpdir(const char* sub_dir_name, void (*func)()) {
14+
// Get the current TMPDIR
15+
const char* original_tmpdir = getenv("TMPDIR");
16+
if (!original_tmpdir) {
17+
return;
18+
}
19+
20+
size_t original_tmpdir_len = strlen(original_tmpdir);
21+
bool has_trailing_slash = original_tmpdir[original_tmpdir_len - 1] == '/';
22+
23+
// Construct the new TMPDIR path
24+
size_t new_tmpdir_len = original_tmpdir_len + strlen(sub_dir_name) + 1 + (has_trailing_slash ? 1 : 0); // +1 for '\0', and +1 for the trailing slash
25+
char* new_tmpdir = (char*)malloc(new_tmpdir_len);
26+
if (!new_tmpdir) {
27+
// perror("Failed to allocate memory");
28+
return;
29+
}
30+
31+
snprintf(new_tmpdir, new_tmpdir_len, has_trailing_slash ? "%s%s/" : "%s/%s/", original_tmpdir, sub_dir_name);
32+
33+
// Create the subdirectory if it doesn't exist
34+
if (mkdir(new_tmpdir, 0700) != 0 && errno != EEXIST) {
35+
//perror("Failed to create directory");
36+
free(new_tmpdir);
37+
return;
38+
}
39+
40+
// Set the TMPDIR environment variable to the new subdirectory
41+
if (setenv("TMPDIR", new_tmpdir, 1) != 0) {
42+
//perror("Failed to set TMPDIR");
43+
free(new_tmpdir);
44+
return;
45+
}
46+
free(new_tmpdir);
47+
48+
pid_t pid = getpid();
49+
printf("Current Process pid: %d tmpdir: %s\n", pid, getenv("TMPDIR"));
50+
51+
// Call the arbitrary function
52+
func();
53+
54+
// Restore the original TMPDIR
55+
setenv("TMPDIR", original_tmpdir, 1);
56+
}
57+
58+
__attribute__((visibility("default"),used))
59+
void ultra_sampler_boot()
60+
{
61+
set_temporary_tmpdir(".ultra", ultra_sampler_start);
62+
}
63+
64+
__attribute__((visibility("default"),used))
65+
__attribute__((section("__DATA,__mod_init_func"))) void (*ultra_sampler_boot_ptr)() = &ultra_sampler_boot;
66+
}

0 commit comments

Comments
 (0)