Skip to content

Commit

Permalink
[runtime] Implement computing and passing the NATIVE_DLL_SEARCH_DIREC…
Browse files Browse the repository at this point in the history
…TORIES runtime property. Fixes xamarin#10504.

This adds support to compute the NATIVE_DLL_SEARCH_DIRECTORIES value and pass
it to the runtime. It's the last property listed in xamarin#10504, so this fixes that
issue.

Fixes xamarin#10504
  • Loading branch information
rolfbjarne committed Jul 30, 2021
1 parent 39df6c5 commit 7ebdcc0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@
<_RuntimeConfigReservedProperties Include="PINVOKE_OVERRIDE" />
<_RuntimeConfigReservedProperties Include="ICU_DAT_FILE_PATH" />
<_RuntimeConfigReservedProperties Include="TRUSTED_PLATFORM_ASSEMBLIES" />
<_RuntimeConfigReservedProperties Include="NATIVE_DLL_SEARCH_DIRECTORIES" />
</ItemGroup>
<RuntimeConfigParserTask
Condition="'$(GenerateRuntimeConfigurationFiles)' == 'true'"
Expand Down
37 changes: 37 additions & 0 deletions runtime/runtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,39 @@ -(void) xamarinSetFlags: (enum XamarinGCHandleFlags) flags;
return rv;
}

char *
xamarin_compute_native_dll_search_directories ()
{
const char *bundle_path = xamarin_get_bundle_path ();

NSMutableArray<NSString *> *directories = [NSMutableArray array];

// Native libraries might be in the app bundle
[directories addObject: [NSString stringWithUTF8String: bundle_path]];
// They won't be in the runtimeidentifier-specific directory (because they get lipo'ed into a fat file instead)
// However, might also be in the Resources/lib directory
[directories addObject: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"lib"]];

// Missing:
// * The parent app bundle if launched from an app extension. This requires adding app extension tests, which we currently don't have.

// Remove the ones that don't exist
NSFileManager *manager = [NSFileManager defaultManager];
for (int i = (int) [directories count] - 1; i >= 0; i--) {
NSString *dir = [directories objectAtIndex: (NSUInteger) i];
BOOL isDirectory;
if ([manager fileExistsAtPath: dir isDirectory: &isDirectory] && isDirectory)
continue;

[directories removeObjectAtIndex: (NSUInteger) i];
}

// Join them all together with a colon separating them
NSString *joined = [directories componentsJoinedByString: @":"];
char *rv = xamarin_strdup_printf ("%s", [joined UTF8String]);
return rv;
}

void
xamarin_vm_initialize ()
{
Expand All @@ -2464,6 +2497,7 @@ -(void) xamarinSetFlags: (enum XamarinGCHandleFlags) flags;
}

char *trusted_platform_assemblies = xamarin_compute_trusted_platform_assemblies ();
char *native_dll_search_directories = xamarin_compute_native_dll_search_directories ();

// All the properties we pass here must also be listed in the _RuntimeConfigReservedProperties item group
// for the _CreateRuntimeConfiguration target in dotnet/targets/Xamarin.Shared.Sdk.targets.
Expand All @@ -2472,12 +2506,14 @@ -(void) xamarinSetFlags: (enum XamarinGCHandleFlags) flags;
"PINVOKE_OVERRIDE",
"ICU_DAT_FILE_PATH",
"TRUSTED_PLATFORM_ASSEMBLIES",
"NATIVE_DLL_SEARCH_DIRECTORIES",
};
const char *propertyValues[] = {
xamarin_get_bundle_path (),
pinvokeOverride,
icu_dat_file_path,
trusted_platform_assemblies,
native_dll_search_directories,
};
static_assert (sizeof (propertyKeys) == sizeof (propertyValues), "The number of keys and values must be the same.");

Expand All @@ -2486,6 +2522,7 @@ -(void) xamarinSetFlags: (enum XamarinGCHandleFlags) flags;
xamarin_free (pinvokeOverride);

xamarin_free (trusted_platform_assemblies);
xamarin_free (native_dll_search_directories);

if (!rv)
xamarin_assertion_message ("Failed to initialize the VM");
Expand Down

0 comments on commit 7ebdcc0

Please sign in to comment.