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 "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(HOST_OS_MACOS) | 6 #if defined(HOST_OS_MACOS) |
7 | 7 |
8 #include "bin/platform.h" | 8 #include "bin/platform.h" |
9 | 9 |
10 #include <CoreFoundation/CoreFoundation.h> | 10 #include <CoreFoundation/CoreFoundation.h> |
(...skipping 17 matching lines...) Expand all Loading... |
28 const char* Platform::executable_name_ = NULL; | 28 const char* Platform::executable_name_ = NULL; |
29 char* Platform::resolved_executable_name_ = NULL; | 29 char* Platform::resolved_executable_name_ = NULL; |
30 int Platform::script_index_ = 1; | 30 int Platform::script_index_ = 1; |
31 char** Platform::argv_ = NULL; | 31 char** Platform::argv_ = NULL; |
32 | 32 |
33 static void segv_handler(int signal, siginfo_t* siginfo, void* context) { | 33 static void segv_handler(int signal, siginfo_t* siginfo, void* context) { |
34 Dart_DumpNativeStackTrace(context); | 34 Dart_DumpNativeStackTrace(context); |
35 abort(); | 35 abort(); |
36 } | 36 } |
37 | 37 |
38 | |
39 bool Platform::Initialize() { | 38 bool Platform::Initialize() { |
40 // Turn off the signal handler for SIGPIPE as it causes the process | 39 // Turn off the signal handler for SIGPIPE as it causes the process |
41 // to terminate on writing to a closed pipe. Without the signal | 40 // to terminate on writing to a closed pipe. Without the signal |
42 // handler error EPIPE is set instead. | 41 // handler error EPIPE is set instead. |
43 struct sigaction act; | 42 struct sigaction act; |
44 bzero(&act, sizeof(act)); | 43 bzero(&act, sizeof(act)); |
45 act.sa_handler = SIG_IGN; | 44 act.sa_handler = SIG_IGN; |
46 if (sigaction(SIGPIPE, &act, 0) != 0) { | 45 if (sigaction(SIGPIPE, &act, 0) != 0) { |
47 perror("Setting signal handler failed"); | 46 perror("Setting signal handler failed"); |
48 return false; | 47 return false; |
(...skipping 12 matching lines...) Expand all Loading... |
61 perror("sigaction() failed."); | 60 perror("sigaction() failed."); |
62 return false; | 61 return false; |
63 } | 62 } |
64 if (sigaction(SIGTRAP, &act, NULL) != 0) { | 63 if (sigaction(SIGTRAP, &act, NULL) != 0) { |
65 perror("sigaction() failed."); | 64 perror("sigaction() failed."); |
66 return false; | 65 return false; |
67 } | 66 } |
68 return true; | 67 return true; |
69 } | 68 } |
70 | 69 |
71 | |
72 int Platform::NumberOfProcessors() { | 70 int Platform::NumberOfProcessors() { |
73 int32_t cpus = -1; | 71 int32_t cpus = -1; |
74 size_t cpus_length = sizeof(cpus); | 72 size_t cpus_length = sizeof(cpus); |
75 if (sysctlbyname("hw.logicalcpu", &cpus, &cpus_length, NULL, 0) == 0) { | 73 if (sysctlbyname("hw.logicalcpu", &cpus, &cpus_length, NULL, 0) == 0) { |
76 return cpus; | 74 return cpus; |
77 } else { | 75 } else { |
78 // Failed, fallback to using sysconf. | 76 // Failed, fallback to using sysconf. |
79 return sysconf(_SC_NPROCESSORS_ONLN); | 77 return sysconf(_SC_NPROCESSORS_ONLN); |
80 } | 78 } |
81 } | 79 } |
82 | 80 |
83 | |
84 const char* Platform::OperatingSystem() { | 81 const char* Platform::OperatingSystem() { |
85 #if HOST_OS_IOS | 82 #if HOST_OS_IOS |
86 return "ios"; | 83 return "ios"; |
87 #else | 84 #else |
88 return "macos"; | 85 return "macos"; |
89 #endif | 86 #endif |
90 } | 87 } |
91 | 88 |
92 | |
93 const char* Platform::LibraryPrefix() { | 89 const char* Platform::LibraryPrefix() { |
94 return "lib"; | 90 return "lib"; |
95 } | 91 } |
96 | 92 |
97 | |
98 const char* Platform::LibraryExtension() { | 93 const char* Platform::LibraryExtension() { |
99 return "dylib"; | 94 return "dylib"; |
100 } | 95 } |
101 | 96 |
102 | |
103 static const char* GetLocaleName() { | 97 static const char* GetLocaleName() { |
104 CFLocaleRef locale = CFLocaleCopyCurrent(); | 98 CFLocaleRef locale = CFLocaleCopyCurrent(); |
105 CFStringRef locale_string = CFLocaleGetIdentifier(locale); | 99 CFStringRef locale_string = CFLocaleGetIdentifier(locale); |
106 CFIndex len = CFStringGetLength(locale_string); | 100 CFIndex len = CFStringGetLength(locale_string); |
107 CFIndex max_len = | 101 CFIndex max_len = |
108 CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1; | 102 CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1; |
109 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len)); | 103 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len)); |
110 ASSERT(result != NULL); | 104 ASSERT(result != NULL); |
111 bool success = | 105 bool success = |
112 CFStringGetCString(locale_string, result, max_len, kCFStringEncodingUTF8); | 106 CFStringGetCString(locale_string, result, max_len, kCFStringEncodingUTF8); |
113 CFRelease(locale); | 107 CFRelease(locale); |
114 if (!success) { | 108 if (!success) { |
115 return NULL; | 109 return NULL; |
116 } | 110 } |
117 return result; | 111 return result; |
118 } | 112 } |
119 | 113 |
120 | |
121 static const char* GetPreferredLanguageName() { | 114 static const char* GetPreferredLanguageName() { |
122 CFArrayRef languages = CFLocaleCopyPreferredLanguages(); | 115 CFArrayRef languages = CFLocaleCopyPreferredLanguages(); |
123 CFIndex languages_length = CFArrayGetCount(languages); | 116 CFIndex languages_length = CFArrayGetCount(languages); |
124 if (languages_length < 1) { | 117 if (languages_length < 1) { |
125 CFRelease(languages); | 118 CFRelease(languages); |
126 return NULL; | 119 return NULL; |
127 } | 120 } |
128 CFTypeRef item = | 121 CFTypeRef item = |
129 reinterpret_cast<CFTypeRef>(CFArrayGetValueAtIndex(languages, 0)); | 122 reinterpret_cast<CFTypeRef>(CFArrayGetValueAtIndex(languages, 0)); |
130 CFTypeID item_type = CFGetTypeID(item); | 123 CFTypeID item_type = CFGetTypeID(item); |
131 ASSERT(item_type == CFStringGetTypeID()); | 124 ASSERT(item_type == CFStringGetTypeID()); |
132 CFStringRef language = reinterpret_cast<CFStringRef>(item); | 125 CFStringRef language = reinterpret_cast<CFStringRef>(item); |
133 CFIndex len = CFStringGetLength(language); | 126 CFIndex len = CFStringGetLength(language); |
134 CFIndex max_len = | 127 CFIndex max_len = |
135 CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1; | 128 CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1; |
136 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len)); | 129 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len)); |
137 ASSERT(result != NULL); | 130 ASSERT(result != NULL); |
138 bool success = | 131 bool success = |
139 CFStringGetCString(language, result, max_len, kCFStringEncodingUTF8); | 132 CFStringGetCString(language, result, max_len, kCFStringEncodingUTF8); |
140 CFRelease(languages); | 133 CFRelease(languages); |
141 if (!success) { | 134 if (!success) { |
142 return NULL; | 135 return NULL; |
143 } | 136 } |
144 return result; | 137 return result; |
145 } | 138 } |
146 | 139 |
147 | |
148 const char* Platform::LocaleName() { | 140 const char* Platform::LocaleName() { |
149 // First see if there is a preferred language. If not, return the | 141 // First see if there is a preferred language. If not, return the |
150 // current locale name. | 142 // current locale name. |
151 const char* preferred_language = GetPreferredLanguageName(); | 143 const char* preferred_language = GetPreferredLanguageName(); |
152 return (preferred_language != NULL) ? preferred_language : GetLocaleName(); | 144 return (preferred_language != NULL) ? preferred_language : GetLocaleName(); |
153 } | 145 } |
154 | 146 |
155 | |
156 bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) { | 147 bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) { |
157 return gethostname(buffer, buffer_length) == 0; | 148 return gethostname(buffer, buffer_length) == 0; |
158 } | 149 } |
159 | 150 |
160 | |
161 char** Platform::Environment(intptr_t* count) { | 151 char** Platform::Environment(intptr_t* count) { |
162 #if HOST_OS_IOS | 152 #if HOST_OS_IOS |
163 // TODO(zra,chinmaygarde): On iOS, environment variables are seldom used. Wire | 153 // TODO(zra,chinmaygarde): On iOS, environment variables are seldom used. Wire |
164 // this up if someone needs it. In the meantime, we return an empty array. | 154 // this up if someone needs it. In the meantime, we return an empty array. |
165 char** result; | 155 char** result; |
166 result = reinterpret_cast<char**>(Dart_ScopeAllocate(1 * sizeof(*result))); | 156 result = reinterpret_cast<char**>(Dart_ScopeAllocate(1 * sizeof(*result))); |
167 if (result == NULL) { | 157 if (result == NULL) { |
168 return NULL; | 158 return NULL; |
169 } | 159 } |
170 result[0] = NULL; | 160 result[0] = NULL; |
(...skipping 13 matching lines...) Expand all Loading... |
184 *count = i; | 174 *count = i; |
185 char** result; | 175 char** result; |
186 result = reinterpret_cast<char**>(Dart_ScopeAllocate(i * sizeof(*result))); | 176 result = reinterpret_cast<char**>(Dart_ScopeAllocate(i * sizeof(*result))); |
187 for (intptr_t current = 0; current < i; current++) { | 177 for (intptr_t current = 0; current < i; current++) { |
188 result[current] = environ[current]; | 178 result[current] = environ[current]; |
189 } | 179 } |
190 return result; | 180 return result; |
191 #endif | 181 #endif |
192 } | 182 } |
193 | 183 |
194 | |
195 const char* Platform::GetExecutableName() { | 184 const char* Platform::GetExecutableName() { |
196 return executable_name_; | 185 return executable_name_; |
197 } | 186 } |
198 | 187 |
199 | |
200 const char* Platform::ResolveExecutablePath() { | 188 const char* Platform::ResolveExecutablePath() { |
201 // Get the required length of the buffer. | 189 // Get the required length of the buffer. |
202 uint32_t path_size = 0; | 190 uint32_t path_size = 0; |
203 if (_NSGetExecutablePath(NULL, &path_size) == 0) { | 191 if (_NSGetExecutablePath(NULL, &path_size) == 0) { |
204 return NULL; | 192 return NULL; |
205 } | 193 } |
206 // Allocate buffer and get executable path. | 194 // Allocate buffer and get executable path. |
207 char* path = DartUtils::ScopedCString(path_size); | 195 char* path = DartUtils::ScopedCString(path_size); |
208 if (_NSGetExecutablePath(path, &path_size) != 0) { | 196 if (_NSGetExecutablePath(path, &path_size) != 0) { |
209 return NULL; | 197 return NULL; |
210 } | 198 } |
211 // Return the canonical path as the returned path might contain symlinks. | 199 // Return the canonical path as the returned path might contain symlinks. |
212 const char* canon_path = File::GetCanonicalPath(path); | 200 const char* canon_path = File::GetCanonicalPath(path); |
213 return canon_path; | 201 return canon_path; |
214 } | 202 } |
215 | 203 |
216 | |
217 void Platform::Exit(int exit_code) { | 204 void Platform::Exit(int exit_code) { |
218 exit(exit_code); | 205 exit(exit_code); |
219 } | 206 } |
220 | 207 |
221 } // namespace bin | 208 } // namespace bin |
222 } // namespace dart | 209 } // namespace dart |
223 | 210 |
224 #endif // defined(HOST_OS_MACOS) | 211 #endif // defined(HOST_OS_MACOS) |
OLD | NEW |