PythonSystemComponent_mac.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include <AzCore/PlatformDef.h>
  9. #include <AzCore/std/containers/unordered_set.h>
  10. #include <AzCore/IO/SystemFile.h>
  11. #include <AzCore/IO/Path/Path.h>
  12. #include <AzFramework/StringFunc/StringFunc.h>
  13. namespace Platform
  14. {
  15. bool InsertPythonLibraryPath(AZStd::unordered_set<AZStd::string>& paths, const char* pythonPackage, const char* engineRoot, const char* subPath)
  16. {
  17. // append lib path to Python paths
  18. AZ::IO::FixedMaxPath libPath = engineRoot;
  19. libPath /= AZ::IO::FixedMaxPathString::format(subPath, pythonPackage);
  20. libPath = libPath.LexicallyNormal();
  21. if (AZ::IO::SystemFile::Exists(libPath.c_str()))
  22. {
  23. paths.insert(libPath.c_str());
  24. return true;
  25. }
  26. AZ_Warning("python", false, "Python library path should exist! path:%s", libPath.c_str());
  27. return false;
  28. }
  29. bool InsertPythonBinaryLibraryPaths(AZStd::unordered_set<AZStd::string>& paths, const char* pythonPackage, const char* engineRoot)
  30. {
  31. // PY_VERSION_MAJOR_MINOR must be defined through the build scripts based on the current python package (see cmake/LYPython.cmake)
  32. #if !defined(PY_VERSION_MAJOR_MINOR)
  33. #error "PY_VERSION_MAJOR_MINOR is not defined"
  34. #endif
  35. // append lib path to Python paths
  36. bool succeeded = true;
  37. succeeded = succeeded && InsertPythonLibraryPath(paths, pythonPackage, engineRoot, "python/runtime/%s/Python.framework/Versions/" PY_VERSION_MAJOR_MINOR "/lib");
  38. // append lib-dynload path
  39. succeeded = succeeded && InsertPythonLibraryPath(paths, pythonPackage, engineRoot, "python/runtime/%s/Python.framework/Versions/" PY_VERSION_MAJOR_MINOR "/lib/python" PY_VERSION_MAJOR_MINOR "/lib-dynload");
  40. // append base path to dynamic link libraries
  41. succeeded = succeeded && InsertPythonLibraryPath(paths, pythonPackage, engineRoot, "python/runtime/%s/Python.framework/Versions/" PY_VERSION_MAJOR_MINOR "/lib/python" PY_VERSION_MAJOR_MINOR);
  42. // append path to site-packages
  43. succeeded = succeeded && InsertPythonLibraryPath(paths, pythonPackage, engineRoot, "python/runtime/%s/Python.framework/Versions/" PY_VERSION_MAJOR_MINOR "/lib/python" PY_VERSION_MAJOR_MINOR "/site-packages");
  44. return succeeded;
  45. }
  46. AZStd::string GetPythonHomePath(const char* pythonPackage, const char* engineRoot)
  47. {
  48. // append lib path to Python paths
  49. AZ::IO::FixedMaxPath libPath = engineRoot;
  50. libPath /= AZ::IO::FixedMaxPathString::format("python/runtime/%s/Python.framework/Versions/" PY_VERSION_MAJOR_MINOR, pythonPackage);
  51. libPath = libPath.LexicallyNormal();
  52. return libPath.String();
  53. }
  54. }