-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathpython_runtime_loader.cpp
More file actions
250 lines (229 loc) · 11.1 KB
/
Copy pathpython_runtime_loader.cpp
File metadata and controls
250 lines (229 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//*****************************************************************************
// Copyright 2023-2026 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include "python_runtime_loader.hpp"
#include <cstdlib>
#include <string>
#include <vector>
#ifdef __linux__
#include <dlfcn.h>
using PythonLibraryHandle = void*;
#elif _WIN32
#include <windows.h>
using PythonLibraryHandle = HMODULE;
#endif
#include "../logging.hpp"
#include "../module.hpp"
#include "../python_runtime_version.hpp"
namespace ovms {
namespace {
using CreatePythonInterpreterModuleFn = Module* (*)();
using ValidatePythonEnvironmentFn = bool (*)(const char** errorMessage);
PythonLibraryHandle pythonRuntimeHandle = nullptr;
CreatePythonInterpreterModuleFn createPythonInterpreterModuleFn = nullptr;
ValidatePythonEnvironmentFn validatePythonEnvironmentFn = nullptr;
} // namespace
Module* ensurePythonRuntimeLoaded() {
if (createPythonInterpreterModuleFn != nullptr && validatePythonEnvironmentFn != nullptr) {
return createPythonInterpreterModuleFn();
}
const bool preferInProcessPythonRuntime = []() {
const char* value = std::getenv("OVMS_TEST_PYTHON_RUNTIME_INPROCESS");
return value != nullptr && std::string(value) == "1";
}();
if (preferInProcessPythonRuntime) {
#ifdef __linux__
createPythonInterpreterModuleFn = reinterpret_cast<CreatePythonInterpreterModuleFn>(dlsym(RTLD_DEFAULT, "OVMS_createPythonInterpreterModule"));
validatePythonEnvironmentFn = reinterpret_cast<ValidatePythonEnvironmentFn>(dlsym(RTLD_DEFAULT, "OVMS_validatePythonEnvironment"));
if (createPythonInterpreterModuleFn != nullptr && validatePythonEnvironmentFn != nullptr) {
const char* pythonRuntimeValidationError = nullptr;
if (!validatePythonEnvironmentFn(&pythonRuntimeValidationError)) {
SPDLOG_WARN("In-process python runtime environment validation failed. Details: {}",
pythonRuntimeValidationError != nullptr ? pythonRuntimeValidationError : "Unknown error");
createPythonInterpreterModuleFn = nullptr;
validatePythonEnvironmentFn = nullptr;
return nullptr;
}
SPDLOG_INFO("Python runtime entry points resolved from in-process symbols");
return createPythonInterpreterModuleFn();
}
#elif _WIN32
HMODULE currentProcess = GetModuleHandleA(nullptr);
if (currentProcess != nullptr) {
createPythonInterpreterModuleFn = reinterpret_cast<CreatePythonInterpreterModuleFn>(GetProcAddress(currentProcess, "OVMS_createPythonInterpreterModule"));
validatePythonEnvironmentFn = reinterpret_cast<ValidatePythonEnvironmentFn>(GetProcAddress(currentProcess, "OVMS_validatePythonEnvironment"));
}
if (createPythonInterpreterModuleFn != nullptr && validatePythonEnvironmentFn != nullptr) {
const char* pythonRuntimeValidationError = nullptr;
if (!validatePythonEnvironmentFn(&pythonRuntimeValidationError)) {
SPDLOG_WARN("In-process python runtime environment validation failed. Details: {}",
pythonRuntimeValidationError != nullptr ? pythonRuntimeValidationError : "Unknown error");
createPythonInterpreterModuleFn = nullptr;
validatePythonEnvironmentFn = nullptr;
return nullptr;
}
SPDLOG_INFO("Python runtime entry points resolved from in-process symbols");
return createPythonInterpreterModuleFn();
}
#endif
}
#ifdef __linux__
std::vector<std::string> candidates{
"libovmspython.so",
"./libovmspython.so",
"src/python/libovmspython.so",
"./src/python/libovmspython.so",
"bazel-bin/src/python/libovmspython.so",
"./bazel-bin/src/python/libovmspython.so"};
candidates = withAbiVersionedCandidates(candidates);
for (const auto& candidate : candidates) {
pythonRuntimeHandle = dlopen(candidate.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (pythonRuntimeHandle != nullptr) {
break;
}
}
if (pythonRuntimeHandle == nullptr) {
SPDLOG_WARN("Python runtime library libovmspython.so failed to load: {}", dlerror());
return nullptr;
}
createPythonInterpreterModuleFn = reinterpret_cast<CreatePythonInterpreterModuleFn>(dlsym(pythonRuntimeHandle, "OVMS_createPythonInterpreterModule"));
if (createPythonInterpreterModuleFn == nullptr) {
SPDLOG_WARN("Python runtime library libovmspython.so missing symbol OVMS_createPythonInterpreterModule: {}", dlerror());
dlclose(pythonRuntimeHandle);
pythonRuntimeHandle = nullptr;
return nullptr;
}
validatePythonEnvironmentFn = reinterpret_cast<ValidatePythonEnvironmentFn>(dlsym(pythonRuntimeHandle, "OVMS_validatePythonEnvironment"));
if (validatePythonEnvironmentFn == nullptr) {
SPDLOG_WARN("Python runtime library libovmspython.so missing symbol OVMS_validatePythonEnvironment: {}", dlerror());
createPythonInterpreterModuleFn = nullptr;
dlclose(pythonRuntimeHandle);
pythonRuntimeHandle = nullptr;
return nullptr;
}
#elif _WIN32
std::vector<std::string> candidates{
"libovmspython.dll",
".\\libovmspython.dll",
"src\\python\\libovmspython.dll",
".\\src\\python\\libovmspython.dll",
"bazel-bin\\src\\python\\libovmspython.dll",
".\\bazel-bin\\src\\python\\libovmspython.dll"};
char executablePath[MAX_PATH] = {0};
DWORD executablePathLength = GetModuleFileNameA(nullptr, executablePath, MAX_PATH);
if (executablePathLength > 0 && executablePathLength < MAX_PATH) {
std::string exePath(executablePath, executablePathLength);
std::string exeDir = ".";
size_t separatorPos = exePath.find_last_of("\\/");
if (separatorPos != std::string::npos) {
exeDir = exePath.substr(0, separatorPos);
}
std::vector<std::string> executableRelativeCandidates{
exeDir + "\\libovmspython.dll",
exeDir + "\\src\\python\\libovmspython.dll",
exeDir + "\\..\\src\\python\\libovmspython.dll",
};
std::string runfilesRoot = exePath + ".runfiles";
std::vector<std::string> runfilesCandidates{
runfilesRoot + "\\src\\python\\libovmspython.dll",
runfilesRoot + "\\_main\\src\\python\\libovmspython.dll",
runfilesRoot + "\\model_server\\src\\python\\libovmspython.dll",
};
candidates.insert(candidates.end(), executableRelativeCandidates.begin(), executableRelativeCandidates.end());
candidates.insert(candidates.end(), runfilesCandidates.begin(), runfilesCandidates.end());
}
candidates = withAbiVersionedCandidates(candidates);
std::string triedCandidates;
for (const auto& candidate : candidates) {
pythonRuntimeHandle = LoadLibraryA(candidate.c_str());
if (pythonRuntimeHandle != nullptr) {
break;
}
if (!triedCandidates.empty())
triedCandidates += ", ";
triedCandidates += candidate;
}
if (pythonRuntimeHandle == nullptr) {
DWORD error = GetLastError();
SPDLOG_WARN("Python runtime library libovmspython.dll failed to load. "
"Tried candidates: [{}]. "
"Ensure the OVMS package directory is on PATH and the correct ABI DLL exists next to ovms.exe. "
"Detected ABI tag: '{}'. Win32 error: {} ({})",
triedCandidates,
detectPythonAbiTag(),
error, std::system_category().message(error));
return nullptr;
}
createPythonInterpreterModuleFn = reinterpret_cast<CreatePythonInterpreterModuleFn>(GetProcAddress(pythonRuntimeHandle, "OVMS_createPythonInterpreterModule"));
if (createPythonInterpreterModuleFn == nullptr) {
DWORD error = GetLastError();
SPDLOG_WARN("Python runtime library libovmspython.dll missing symbol OVMS_createPythonInterpreterModule: {} ({})", error, std::system_category().message(error));
FreeLibrary(pythonRuntimeHandle);
pythonRuntimeHandle = nullptr;
return nullptr;
}
validatePythonEnvironmentFn = reinterpret_cast<ValidatePythonEnvironmentFn>(GetProcAddress(pythonRuntimeHandle, "OVMS_validatePythonEnvironment"));
if (validatePythonEnvironmentFn == nullptr) {
DWORD error = GetLastError();
SPDLOG_WARN("Python runtime library libovmspython.dll missing symbol OVMS_validatePythonEnvironment: {} ({})", error, std::system_category().message(error));
createPythonInterpreterModuleFn = nullptr;
FreeLibrary(pythonRuntimeHandle);
pythonRuntimeHandle = nullptr;
return nullptr;
}
#endif
const char* pythonRuntimeValidationError = nullptr;
if (!validatePythonEnvironmentFn(&pythonRuntimeValidationError)) {
const std::string abiTag = detectPythonAbiTag();
const char* pythonHome = std::getenv("PYTHONHOME");
const char* pythonPath = std::getenv("PYTHONPATH");
SPDLOG_WARN("Python runtime environment validation failed. "
"Detected ABI tag: '{}'. PYTHONHOME: '{}'. PYTHONPATH: '{}'. "
"Ensure the correct pyovms.pyd (e.g. python\\cp{}\\pyovms.pyd) is on PYTHONPATH "
"and that the Python installation directory (containing python{}.dll) is on PATH. "
"Details: {}",
abiTag.empty() ? "<none - will use unversioned fallback DLLs>" : abiTag,
pythonHome != nullptr ? pythonHome : "<not set>",
pythonPath != nullptr ? pythonPath : "<not set>",
abiTag.empty() ? "312" : abiTag,
abiTag.empty() ? "312" : abiTag,
pythonRuntimeValidationError != nullptr ? pythonRuntimeValidationError : "Unknown error");
createPythonInterpreterModuleFn = nullptr;
validatePythonEnvironmentFn = nullptr;
#ifdef __linux__
dlclose(pythonRuntimeHandle);
#elif _WIN32
FreeLibrary(pythonRuntimeHandle);
#endif
pythonRuntimeHandle = nullptr;
return nullptr;
}
SPDLOG_INFO("Python runtime library loaded successfully");
return createPythonInterpreterModuleFn();
}
void unloadPythonRuntime() {
createPythonInterpreterModuleFn = nullptr;
validatePythonEnvironmentFn = nullptr;
if (pythonRuntimeHandle == nullptr) {
return;
}
#ifdef __linux__
dlclose(pythonRuntimeHandle);
#elif _WIN32
FreeLibrary(pythonRuntimeHandle);
#endif
pythonRuntimeHandle = nullptr;
}
} // namespace ovms