OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "bin/extensions.h" | 5 #include "bin/extensions.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
10 #include "bin/file.h" | 10 #include "bin/file.h" |
11 #include "bin/platform.h" | 11 #include "bin/platform.h" |
| 12 #include "bin/utils.h" |
12 #include "include/dart_api.h" | 13 #include "include/dart_api.h" |
13 #include "platform/assert.h" | 14 #include "platform/assert.h" |
14 #include "platform/globals.h" | 15 #include "platform/globals.h" |
15 | 16 |
16 namespace dart { | 17 namespace dart { |
17 namespace bin { | 18 namespace bin { |
18 | 19 |
| 20 static char PathSeparator() { |
| 21 const char* sep = File::PathSeparator(); |
| 22 ASSERT(strlen(sep) == 1); |
| 23 return sep[0]; |
| 24 } |
| 25 |
| 26 |
| 27 void* Extensions::MakePathAndResolve(const char* dir, const char* name) { |
| 28 // First try to find the library with a suffix specifying the architecture. |
| 29 { |
| 30 const char* path_components[] = { |
| 31 dir, |
| 32 Platform::LibraryPrefix(), |
| 33 name, |
| 34 "-", |
| 35 Platform::HostArchitecture(), // arm |
| 36 ".", |
| 37 Platform::LibraryExtension(), // so |
| 38 NULL, |
| 39 }; |
| 40 const char* library_file = Concatenate(path_components); |
| 41 void* library_handle = LoadExtensionLibrary(library_file); |
| 42 if (library_handle != NULL) { |
| 43 return library_handle; |
| 44 } |
| 45 } |
| 46 |
| 47 // Fall back on a library name without the suffix. |
| 48 { |
| 49 const char* path_components[] = { |
| 50 dir, |
| 51 Platform::LibraryPrefix(), |
| 52 name, |
| 53 ".", |
| 54 Platform::LibraryExtension(), // so |
| 55 NULL, |
| 56 }; |
| 57 const char* library_file = Concatenate(path_components); |
| 58 return LoadExtensionLibrary(library_file); |
| 59 } |
| 60 } |
| 61 |
| 62 |
| 63 // IMPORTANT: In the absolute path case, do not extract the filename and search |
| 64 // for that by passing it to LoadExtensionLibrary. That can lead to confusion in |
| 65 // which the absolute path is wrong, and a different version of the library is |
| 66 // loaded from the standard location. |
| 67 void* Extensions::ResolveAbsPathExtension(const char* extension_path) { |
| 68 const char* last_slash = strrchr(extension_path, PathSeparator()) + 1; |
| 69 char* name = strdup(last_slash); |
| 70 char* dir = StringUtils::StrNDup(extension_path, last_slash - extension_path); |
| 71 void* library_handle = MakePathAndResolve(dir, name); |
| 72 free(dir); |
| 73 free(name); |
| 74 return library_handle; |
| 75 } |
| 76 |
| 77 |
| 78 void* Extensions::ResolveExtension(const char* extension_directory, |
| 79 const char* extension_name) { |
| 80 // If the path following dart-ext is an absolute path, then only look for the |
| 81 // library there. |
| 82 if (File::IsAbsolutePath(extension_name)) { |
| 83 return ResolveAbsPathExtension(extension_name); |
| 84 } |
| 85 |
| 86 // If the path following dart-ext is just a file name, first look next to |
| 87 // the importing Dart library. |
| 88 void* library_handle = MakePathAndResolve(extension_directory, |
| 89 extension_name); |
| 90 if (library_handle != NULL) { |
| 91 return library_handle; |
| 92 } |
| 93 |
| 94 // Then pass the library name down to the platform. E.g. dlopen will do its |
| 95 // own search in standard search locations. |
| 96 return MakePathAndResolve("", extension_name); |
| 97 } |
| 98 |
| 99 |
19 Dart_Handle Extensions::LoadExtension(const char* extension_directory, | 100 Dart_Handle Extensions::LoadExtension(const char* extension_directory, |
20 const char* extension_name, | 101 const char* extension_name, |
21 Dart_Handle parent_library) { | 102 Dart_Handle parent_library) { |
22 // For example on Linux: directory/libfoo-arm.so | 103 void* library_handle = ResolveExtension(extension_directory, extension_name); |
23 const char* library_strings[] = { | |
24 extension_directory, // directory/ | |
25 Platform::LibraryPrefix(), // lib | |
26 extension_name, // foo | |
27 "-", | |
28 Platform::HostArchitecture(), // arm | |
29 ".", | |
30 Platform::LibraryExtension(), // so | |
31 NULL, | |
32 }; | |
33 const char* library_file = Concatenate(library_strings); | |
34 void* library_handle = LoadExtensionLibrary(library_file); | |
35 if (library_handle == NULL) { | 104 if (library_handle == NULL) { |
36 // Fallback on a library file name that does not specify the host | 105 return GetError(); |
37 // architecture. For example on Linux: directory/libfoo.so | |
38 const char* fallback_library_strings[] = { | |
39 extension_directory, // directory/ | |
40 Platform::LibraryPrefix(), // lib | |
41 extension_name, // foo | |
42 ".", | |
43 Platform::LibraryExtension(), // so | |
44 NULL, | |
45 }; | |
46 const char* fallback_library_file = Concatenate(fallback_library_strings); | |
47 library_handle = LoadExtensionLibrary(fallback_library_file); | |
48 if (library_handle == NULL) { | |
49 return GetError(); | |
50 } | |
51 } | 106 } |
52 | 107 |
53 const char* strings[] = { extension_name, "_Init", NULL }; | 108 const char* extension = extension_name; |
| 109 if (File::IsAbsolutePath(extension_name)) { |
| 110 extension = strrchr(extension_name, PathSeparator()) + 1; |
| 111 } |
| 112 |
| 113 const char* strings[] = { extension, "_Init", NULL }; |
54 const char* init_function_name = Concatenate(strings); | 114 const char* init_function_name = Concatenate(strings); |
55 void* init_function = ResolveSymbol(library_handle, init_function_name); | 115 void* init_function = ResolveSymbol(library_handle, init_function_name); |
56 Dart_Handle result = GetError(); | 116 Dart_Handle result = GetError(); |
57 if (Dart_IsError(result)) { | 117 if (Dart_IsError(result)) { |
58 return result; | 118 return result; |
59 } | 119 } |
60 ASSERT(init_function != NULL); | 120 ASSERT(init_function != NULL); |
61 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); | 121 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
62 InitFunctionType fn = reinterpret_cast<InitFunctionType>(init_function); | 122 InitFunctionType fn = reinterpret_cast<InitFunctionType>(init_function); |
63 return (*fn)(parent_library); | 123 return (*fn)(parent_library); |
(...skipping 12 matching lines...) Expand all Loading... |
76 for (int i = 0; strings[i] != NULL; i++) { | 136 for (int i = 0; strings[i] != NULL; i++) { |
77 index += snprintf(result + index, size - index, "%s", strings[i]); | 137 index += snprintf(result + index, size - index, "%s", strings[i]); |
78 } | 138 } |
79 ASSERT(index == size - 1); | 139 ASSERT(index == size - 1); |
80 ASSERT(result[size - 1] == '\0'); | 140 ASSERT(result[size - 1] == '\0'); |
81 return result; | 141 return result; |
82 } | 142 } |
83 | 143 |
84 } // namespace bin | 144 } // namespace bin |
85 } // namespace dart | 145 } // namespace dart |
OLD | NEW |