1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "SharedLibrary.hpp"
16
17#if defined(_WIN32)
18std::string getModuleDirectory()
19{
20 static int dummy_symbol = 0;
21
22 HMODULE module = NULL;
23 GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)&dummy_symbol, &module);
24
25 char filename[1024];
26 if(module && (GetModuleFileName(module, filename, sizeof(filename)) != 0))
27 {
28 std::string directory(filename);
29 return directory.substr(0, directory.find_last_of("\\/") + 1).c_str();
30 }
31 else
32 {
33 return "";
34 }
35}
36#else
37std::string getModuleDirectory()
38{
39 static int dummy_symbol = 0;
40
41 Dl_info dl_info;
42 if(dladdr(&dummy_symbol, &dl_info) != 0)
43 {
44 std::string directory(dl_info.dli_fname);
45 return directory.substr(0, directory.find_last_of("\\/") + 1).c_str();
46 }
47 else
48 {
49 return "";
50 }
51}
52#endif
53