InitializeEmbeddedPyside2.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 QtForPython
  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. const char* s_libPyside2LibraryFile = "libpyside2.abi3.so.5.15";
  17. const char* s_libShibokenLibraryFile = "libshiboken2.abi3.so.5.15";
  18. const char* s_libQt5TestLibraryFile = "libQt5Test.so.5";
  19. class InitializeEmbeddedPyside2
  20. {
  21. public:
  22. InitializeEmbeddedPyside2()
  23. {
  24. m_libPythonLibraryFile = InitializeEmbeddedPyside2::LoadModule(s_libPythonLibraryFile);
  25. m_libPyside2LibraryFile = InitializeEmbeddedPyside2::LoadModule(s_libPyside2LibraryFile);
  26. m_libShibokenLibraryFile = InitializeEmbeddedPyside2::LoadModule(s_libShibokenLibraryFile);
  27. m_libQt5TestLibraryFile = InitializeEmbeddedPyside2::LoadModule(s_libQt5TestLibraryFile);
  28. }
  29. virtual ~InitializeEmbeddedPyside2()
  30. {
  31. InitializeEmbeddedPyside2::UnloadModule(m_libQt5TestLibraryFile);
  32. InitializeEmbeddedPyside2::UnloadModule(m_libShibokenLibraryFile);
  33. InitializeEmbeddedPyside2::UnloadModule(m_libPyside2LibraryFile);
  34. InitializeEmbeddedPyside2::UnloadModule(m_libPythonLibraryFile);
  35. }
  36. private:
  37. static void* LoadModule(const char* moduleToLoad)
  38. {
  39. void* moduleHandle = dlopen(moduleToLoad, RTLD_NOW | RTLD_GLOBAL);
  40. if (!moduleHandle)
  41. {
  42. [[maybe_unused]] const char* loadError = dlerror();
  43. AZ_Error("QtForPython", false, "Unable to load python library %s for Pyside2: %s", moduleToLoad,
  44. loadError ? loadError : "Unknown Error");
  45. }
  46. return moduleHandle;
  47. }
  48. static void UnloadModule(void* moduleHandle)
  49. {
  50. if (moduleHandle)
  51. {
  52. dlclose(moduleHandle);
  53. }
  54. }
  55. void* m_libPythonLibraryFile;
  56. void* m_libPyside2LibraryFile;
  57. void* m_libShibokenLibraryFile;
  58. void* m_libQt5TestLibraryFile;
  59. };
  60. } // namespace QtForPython