Skip to content

Commit 4432914

Browse files
committed
Add search paths for dependencies.
1 parent e11c445 commit 4432914

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

source/loader/source/loader_impl.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ static int loader_impl_dependencies_self_find(loader_impl impl, const char *key_
113113

114114
static int loader_impl_dependencies_load(loader_impl impl, const char *key_str, value *paths_array, size_t paths_size);
115115

116+
#if defined(WIN32) || defined(_WIN32)
117+
static void loader_impl_dependencies_search_paths(loader_impl impl, const loader_tag tag);
118+
#endif
119+
116120
static configuration loader_impl_initialize_configuration(const loader_tag tag);
117121

118122
static int loader_impl_initialize_registered(plugin_manager manager, plugin p);
@@ -370,6 +374,39 @@ int loader_impl_dependencies_load(loader_impl impl, const char *key_str, value *
370374
return 1;
371375
}
372376

377+
#if defined(WIN32) || defined(_WIN32)
378+
void loader_impl_dependencies_search_paths(loader_impl impl, const loader_tag tag)
379+
{
380+
/* Search paths have the following format and are only implemented for Windows:
381+
{
382+
"search_paths": ["C:\Program Files\ruby\bin\ruby_builtin_dlls"]
383+
}
384+
*/
385+
value search_paths_value = configuration_value_type(impl->config, "search_paths", TYPE_ARRAY);
386+
387+
/* Check if the loader has search paths and initialize them */
388+
if (search_paths_value != NULL)
389+
{
390+
size_t size = value_type_count(search_paths_value);
391+
value *search_paths_array = value_to_array(search_paths_value);
392+
size_t iterator;
393+
394+
for (iterator = 0; iterator < size; ++iterator)
395+
{
396+
if (value_type_id(search_paths_array[iterator]) == TYPE_STRING)
397+
{
398+
const char *key_str = value_to_string(search_paths_array[iterator]);
399+
400+
if (SetDllDirectoryA(key_str) == FALSE)
401+
{
402+
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register the DLL directory %s in loader '%s'; dependencies with other dependant DLLs may fail to load", key_str, tag);
403+
}
404+
}
405+
}
406+
}
407+
}
408+
#endif
409+
373410
int loader_impl_dependencies(loader_impl impl, detour d, const loader_tag tag)
374411
{
375412
/* Dependencies have the following format:
@@ -412,6 +449,10 @@ int loader_impl_dependencies(loader_impl impl, detour d, const loader_tag tag)
412449
/* Initialize the loader detour */
413450
impl->d = d;
414451

452+
#if defined(WIN32) || defined(_WIN32)
453+
loader_impl_dependencies_search_paths(impl, tag);
454+
#endif
455+
415456
/* Check if the loader has dependencies and load them */
416457
if (dependencies_value != NULL)
417458
{

source/loaders/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ macro(loader_configuration_begin TARGET)
8787
set(LOADER_CONFIGURATION_TARGET "${TARGET}")
8888
endmacro()
8989

90+
# Generate configuration with search paths for a loader
91+
#
92+
# "search_paths": [ "C:/Program Files/ruby/bin/ruby_builtin_dlls" ],
93+
#
94+
macro(loader_configuration_paths LIBRARY_PATHS_LIST)
95+
if(NOT "${LIBRARY_PATHS_LIST}" STREQUAL "")
96+
# Normalize the paths
97+
string(REPLACE "\\" "/" LIBRARY_PATHS "${LIBRARY_PATHS_LIST}")
98+
99+
# Convert to JSON
100+
string(REPLACE ";" "\", \"" LOADER_SEARCH_PATHS "\"${LIBRARY_PATHS}\"")
101+
else()
102+
set(LOADER_SEARCH_PATHS)
103+
endif()
104+
endmacro()
105+
90106
# Generate configuration with dependencies for a loader
91107
#
92108
# node_loader:

source/loaders/loader.json.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"search_paths": [ @LOADER_SEARCH_PATHS@ ],
23
"dependencies": {
34
@LOADER_DEPENDENCIES@
45
}

source/loaders/rb_loader/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,37 @@ else()
238238
set(Ruby_LIBRARY_INSTALL "${Ruby_LIBRARY_NAME_PATH}")
239239
endif()
240240

241+
# Define search paths
242+
set(Ruby_LIBRARY_SEARCH_PATHS_DEVELOPMENT "${Ruby_LIBRARY_SEARCH_PATHS}")
243+
244+
if(Ruby_LIBRARY_SEARCH_PATHS AND WIN32)
245+
set(Ruby_LIBRARY_SEARCH_PATHS_INSTALL "")
246+
247+
foreach(SEARCH_PATH IN LISTS Ruby_LIBRARY_SEARCH_PATHS)
248+
install(DIRECTORY
249+
"${SEARCH_PATH}"
250+
DESTINATION ${INSTALL_LIB}
251+
COMPONENT runtime
252+
)
253+
254+
get_filename_component(SEARCH_PATH_FOLDER_NAME "${SEARCH_PATH}" NAME)
255+
256+
list(APPEND Ruby_LIBRARY_SEARCH_PATHS_INSTALL "${CMAKE_INSTALL_PREFIX}/${INSTALL_LIB}/${SEARCH_PATH_FOLDER_NAME}")
257+
endforeach()
258+
endif()
259+
241260
#
242261
# Configuration
243262
#
244263

245264
# Development
246265
loader_configuration_begin(rb_loader)
266+
loader_configuration_paths("${Ruby_LIBRARY_SEARCH_PATHS_DEVELOPMENT}")
247267
loader_configuration_deps(ruby "${Ruby_LIBRARY_DEVELOPMENT}")
248268
loader_configuartion_end_development()
249269

250270
# Install
251271
loader_configuration_begin(rb_loader)
272+
loader_configuration_paths("${Ruby_LIBRARY_SEARCH_PATHS_INSTALL}")
252273
loader_configuration_deps(ruby "${Ruby_LIBRARY_INSTALL}")
253274
loader_configuartion_end_install()

tools/metacall-environment.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ function Set-Ruby {
199199
Write-Output "-DRuby_EXECUTABLE=""$RubyDir/bin/ruby.exe""" >> $EnvOpts
200200
Write-Output "-DRuby_LIBRARY=""$RubyDir/lib/x64-vcruntime140-ruby350.lib""" >> $EnvOpts
201201
Write-Output "-DRuby_LIBRARY_NAME=""$RubyDir/bin/x64-vcruntime140-ruby350.dll""" >> $EnvOpts
202+
Write-Output "-DRuby_LIBRARY_SEARCH_PATHS=""$RubyDir/bin/ruby_builtin_dlls""" >> $EnvOpts
202203
}
203204

204205
function Set-TypeScript {

0 commit comments

Comments
 (0)