Skip to content

Commit

Permalink
Add script to build Ultra.Sampler NativeAOT library
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 7, 2024
1 parent a5bc97a commit abd3e73
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Ultra.Sampler/MacOS/NativeModuleEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System.Text;

namespace Ultra.Sampler.MacOS;

internal struct NativeModuleEvent
Expand All @@ -13,6 +15,6 @@ internal struct NativeModuleEvent

public override string ToString()
{
return $"{nameof(LoadAddress)}: 0x{LoadAddress:X8}, {nameof(Path)}: {Path}, {nameof(TimestampUtc)}: {TimestampUtc:O}";
return $"{nameof(LoadAddress)}: 0x{LoadAddress:X8}, {nameof(Path)}: {Encoding.UTF8.GetString(Path ?? [])}, {nameof(TimestampUtc)}: {TimestampUtc:O}";
}
}
5 changes: 5 additions & 0 deletions src/Ultra.Sampler/Ultra.Sampler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<PublishAot>true</PublishAot>
<AssemblyName>libUltraSampler</AssemblyName>
<EventSourceSupport>true</EventSourceSupport>
<_SuppressNativeLibEventSourceWarning>true</_SuppressNativeLibEventSourceWarning>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions src/Ultra.Sampler/build_native.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh
dotnet publish -c Release -r osx-arm64
LIB_NAME=libUltraSampler.dylib
LIB_OUTPUT_PATH=./bin/Release/net8.0/osx-arm64/publish/$LIB_NAME
install_name_tool -id $LIB_NAME $LIB_OUTPUT_PATH
cp $LIB_OUTPUT_PATH ./$LIB_NAME
clang -shared -O2 -o libUltraSamplerIndirect.dyld ultra_sampler_indirect.cpp ./$LIB_NAME
66 changes: 66 additions & 0 deletions src/Ultra.Sampler/ultra_sampler_indirect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

extern "C" {
void ultra_sampler_start();


void set_temporary_tmpdir(const char* sub_dir_name, void (*func)()) {
// Get the current TMPDIR
const char* original_tmpdir = getenv("TMPDIR");
if (!original_tmpdir) {
return;
}

size_t original_tmpdir_len = strlen(original_tmpdir);
bool has_trailing_slash = original_tmpdir[original_tmpdir_len - 1] == '/';

// Construct the new TMPDIR path
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
char* new_tmpdir = (char*)malloc(new_tmpdir_len);
if (!new_tmpdir) {
// perror("Failed to allocate memory");
return;
}

snprintf(new_tmpdir, new_tmpdir_len, has_trailing_slash ? "%s%s/" : "%s/%s/", original_tmpdir, sub_dir_name);

// Create the subdirectory if it doesn't exist
if (mkdir(new_tmpdir, 0700) != 0 && errno != EEXIST) {
//perror("Failed to create directory");
free(new_tmpdir);
return;
}

// Set the TMPDIR environment variable to the new subdirectory
if (setenv("TMPDIR", new_tmpdir, 1) != 0) {
//perror("Failed to set TMPDIR");
free(new_tmpdir);
return;
}
free(new_tmpdir);

pid_t pid = getpid();
printf("Current Process pid: %d tmpdir: %s\n", pid, getenv("TMPDIR"));

// Call the arbitrary function
func();

// Restore the original TMPDIR
setenv("TMPDIR", original_tmpdir, 1);
}

__attribute__((visibility("default"),used))
void ultra_sampler_boot()
{
set_temporary_tmpdir(".ultra", ultra_sampler_start);
}

__attribute__((visibility("default"),used))
__attribute__((section("__DATA,__mod_init_func"))) void (*ultra_sampler_boot_ptr)() = &ultra_sampler_boot;
}

0 comments on commit abd3e73

Please sign in to comment.