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
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_BIN_PLATFORM_H_
6#define RUNTIME_BIN_PLATFORM_H_
7
8#include "bin/builtin.h"
9#include "platform/globals.h"
10#include "platform/utils.h"
11
12#if defined(HOST_OS_MACOS)
13#include "bin/platform_macos.h"
14#endif // defined(HOST_OS_MACOS)
15
16namespace dart {
17namespace bin {
18
19class Platform {
20 public:
21 // Perform platform specific initialization.
22 static bool Initialize();
23
24 // Returns the number of processors on the machine.
25 static int NumberOfProcessors();
26
27 // Returns a string representing the operating system ("linux",
28 // "macos", "windows", or "android"). The returned string should not be
29 // deallocated by the caller.
30 static const char* OperatingSystem();
31
32 // Returns a string representing the version of the operating system. The
33 // format of the string is determined by the platform. The returned string
34 // should not be deallocated by the caller.
35 static const char* OperatingSystemVersion();
36
37 // Returns the architecture name of the processor the VM is running on
38 // (ia32, x64, arm, or arm64).
39 static const char* HostArchitecture() {
40#if defined(HOST_ARCH_ARM)
41 return "arm";
42#elif defined(HOST_ARCH_ARM64)
43 return "arm64";
44#elif defined(HOST_ARCH_IA32)
45 return "ia32";
46#elif defined(HOST_ARCH_X64)
47 return "x64";
48#else
49#error Architecture detection failed.
50#endif
51 }
52
53 static const char* LibraryPrefix();
54
55 // Returns a string representing the operating system's shared library
56 // extension (e.g. 'so', 'dll', ...). The returned string should not be
57 // deallocated by the caller.
58 static const char* LibraryExtension();
59
60 // Extracts the local hostname.
61 static bool LocalHostname(char* buffer, intptr_t buffer_length);
62
63 static const char* LocaleName();
64
65 // Extracts the environment variables for the current process. The array of
66 // strings is Dart_ScopeAllocated. The number of elements in the array is
67 // returned in the count argument.
68 static char** Environment(intptr_t* count);
69
70 static const char* ResolveExecutablePath();
71
72 // This has the same effect as calling ResolveExecutablePath except that
73 // Dart_ScopeAllocate is not called and that the result goes into the given
74 // parameters.
75 // WARNING: On Fuchsia it returns -1, i.e. doesn't work.
76 // Note that `result` should be pre-allocated with size `result_size`.
77 // The return-value is the length read into `result` or -1 on failure.
78 static intptr_t ResolveExecutablePathInto(char* result, size_t result_size);
79
80 // Stores the executable name.
81 static void SetExecutableName(const char* executable_name) {
82 executable_name_ = executable_name;
83 }
84 static const char* GetExecutableName();
85 static const char* GetResolvedExecutableName() {
86 if (resolved_executable_name_ == NULL) {
87 // Try to resolve the executable path using platform specific APIs.
88 const char* resolved_name = Platform::ResolveExecutablePath();
89 if (resolved_name != NULL) {
90 resolved_executable_name_ = Utils::StrDup(resolved_name);
91 }
92 }
93 return resolved_executable_name_;
94 }
95
96 // Stores and gets the flags passed to the executable.
97 static void SetExecutableArguments(int script_index, char** argv) {
98 script_index_ = script_index;
99 argv_ = argv;
100 }
101 static int GetScriptIndex() { return script_index_; }
102 static char** GetArgv() { return argv_; }
103
104 DART_NORETURN static void Exit(int exit_code);
105
106 static void SetCoreDumpResourceLimit(int value);
107
108 private:
109 // The path to the executable.
110 static const char* executable_name_;
111 // The path to the resolved executable.
112 static char* resolved_executable_name_;
113
114 static int script_index_;
115 static char** argv_; // VM flags are argv_[1 ... script_index_ - 1]
116
117 DISALLOW_ALLOCATION();
118 DISALLOW_IMPLICIT_CONSTRUCTORS(Platform);
119};
120
121} // namespace bin
122} // namespace dart
123
124#endif // RUNTIME_BIN_PLATFORM_H_
125