InitializePython.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Debug/Trace.h>
  10. #include <dlfcn.h>
  11. namespace EditorPythonBindings
  12. {
  13. // s_libPythonLibraryFile must match the library name listed in (O3DE Engine Root)/python/runtime/.../python-config.cmake
  14. // in the set(${MY}_LIBRARY_xxxx sections.
  15. const char* s_libPythonLibraryFile = "libpython3.10.so.1.0";
  16. class InitializePython
  17. {
  18. public:
  19. InitializePython()
  20. {
  21. m_libPythonLibraryFile = InitializePython::LoadModule(s_libPythonLibraryFile);
  22. }
  23. virtual ~InitializePython()
  24. {
  25. InitializePython::UnloadModule(m_libPythonLibraryFile);
  26. }
  27. private:
  28. static void* LoadModule(const char* moduleToLoad)
  29. {
  30. void* moduleHandle = dlopen(moduleToLoad, RTLD_NOW | RTLD_GLOBAL);
  31. if (!moduleHandle)
  32. {
  33. [[maybe_unused]] const char* loadError = dlerror();
  34. AZ_Error("EditorPythonBindings", false, "Unable to load python library %s for EditorPythonBindings: %s", moduleToLoad,
  35. loadError ? loadError : "Unknown Error");
  36. }
  37. return moduleHandle;
  38. }
  39. static void UnloadModule(void* moduleHandle)
  40. {
  41. if (moduleHandle)
  42. {
  43. dlclose(moduleHandle);
  44. }
  45. }
  46. void* m_libPythonLibraryFile;
  47. };
  48. } // namespace EditorPythonBindings