1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // system_utils.h: declaration of OS-specific utility functions
8 
9 #ifndef COMMON_SYSTEM_UTILS_H_
10 #define COMMON_SYSTEM_UTILS_H_
11 
12 #include "common/Optional.h"
13 #include "common/angleutils.h"
14 
15 #include <string>
16 
17 namespace angle
18 {
19 std::string GetExecutableName();
20 std::string GetExecutablePath();
21 std::string GetExecutableDirectory();
22 std::string GetHelperExecutableDir();
23 const char *GetSharedLibraryExtension();
24 const char *GetExecutableExtension();
25 char GetPathSeparator();
26 Optional<std::string> GetCWD();
27 bool SetCWD(const char *dirName);
28 bool SetEnvironmentVar(const char *variableName, const char *value);
29 bool UnsetEnvironmentVar(const char *variableName);
30 std::string GetEnvironmentVar(const char *variableName);
31 std::string GetEnvironmentVarOrUnCachedAndroidProperty(const char *variableName,
32                                                        const char *propertyName);
33 std::string GetEnvironmentVarOrAndroidProperty(const char *variableName, const char *propertyName);
34 const char *GetPathSeparatorForEnvironmentVar();
35 bool PrependPathToEnvironmentVar(const char *variableName, const char *path);
36 bool IsDirectory(const char *filename);
37 
38 // Get absolute time in seconds.  Use this function to get an absolute time with an unknown origin.
39 double GetCurrentTime();
40 
41 // Run an application and get the output.  Gets a nullptr-terminated set of args to execute the
42 // application with, and returns the stdout and stderr outputs as well as the exit code.
43 //
44 // Pass nullptr for stdoutOut/stderrOut if you don't need to capture. exitCodeOut is required.
45 //
46 // Returns false if it fails to actually execute the application.
47 bool RunApp(const std::vector<const char *> &args,
48             std::string *stdoutOut,
49             std::string *stderrOut,
50             int *exitCodeOut);
51 
52 class Library : angle::NonCopyable
53 {
54   public:
~Library()55     virtual ~Library() {}
56     virtual void *getSymbol(const char *symbolName) = 0;
57     virtual void *getNative() const                 = 0;
58 
59     template <typename FuncT>
getAs(const char * symbolName,FuncT * funcOut)60     void getAs(const char *symbolName, FuncT *funcOut)
61     {
62         *funcOut = reinterpret_cast<FuncT>(getSymbol(symbolName));
63     }
64 };
65 
66 // Use SYSTEM_DIR to bypass loading ANGLE libraries with the same name as system DLLS
67 // (e.g. opengl32.dll)
68 enum class SearchType
69 {
70     ApplicationDir,
71     SystemDir
72 };
73 
74 Library *OpenSharedLibrary(const char *libraryName, SearchType searchType);
75 Library *OpenSharedLibraryWithExtension(const char *libraryName);
76 
77 // Returns true if the process is currently being debugged.
78 bool IsDebuggerAttached();
79 
80 // Calls system APIs to break into the debugger.
81 void BreakDebugger();
82 }  // namespace angle
83 
84 #endif  // COMMON_SYSTEM_UTILS_H_
85