The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "projucer_LiveCodeBuilderDLL.h"
  18. struct CompileEngineDLL : DeletedAtShutdown
  19. {
  20. CompileEngineDLL()
  21. {
  22. tryLoadDll();
  23. }
  24. ~CompileEngineDLL()
  25. {
  26. shutdown();
  27. }
  28. bool tryLoadDll()
  29. {
  30. // never load the dynamic lib multiple times
  31. if (! isLoaded())
  32. {
  33. File f = findDLLFile();
  34. if (f != File() && dll.open (f.getLinkedTarget().getFullPathName()))
  35. {
  36. #define INIT_LIVE_DLL_FN(name, returnType, params) name = (name##_type) dll.getFunction (#name);
  37. LIVE_DLL_FUNCTIONS (INIT_LIVE_DLL_FN);
  38. #undef INIT_LIVE_DLL_FN
  39. return true;
  40. }
  41. return false;
  42. }
  43. return true;
  44. }
  45. void initialise (CrashCallbackFunction crashFn, QuitCallbackFunction quitFn, bool setupSignals)
  46. {
  47. if (isLoaded())
  48. projucer_initialise (crashFn, quitFn, setPropertyCallback, getPropertyCallback, setupSignals);
  49. }
  50. void shutdown()
  51. {
  52. if (isLoaded())
  53. projucer_shutdown();
  54. }
  55. bool isLoaded() const
  56. {
  57. #define CHECK_LIVE_DLL_FN(name, returnType, params) if (name == nullptr) return false;
  58. LIVE_DLL_FUNCTIONS (CHECK_LIVE_DLL_FN);
  59. #undef CHECK_LIVE_DLL_FN
  60. return projucer_getVersion() == requiredVersion;
  61. }
  62. #define DECLARE_LIVE_DLL_FN(name, returnType, params) \
  63. typedef returnType (*name##_type) params; \
  64. name##_type name = nullptr;
  65. LIVE_DLL_FUNCTIONS (DECLARE_LIVE_DLL_FN)
  66. #undef DECLARE_LIVE_DLL_FN
  67. static String getDLLName()
  68. {
  69. #if JUCE_MAC
  70. return "JUCECompileEngine.dylib";
  71. #elif JUCE_LINUX
  72. return "JUCECompileEngine.so";
  73. #elif JUCE_WINDOWS
  74. return "JUCECompileEngine.dll";
  75. #else
  76. #error
  77. return "JUCECompileEngine.so";
  78. #endif
  79. }
  80. static File getVersionedUserAppSupportFolder()
  81. {
  82. File userAppData (File::getSpecialLocation (File::userApplicationDataDirectory));
  83. #if JUCE_MAC
  84. userAppData = userAppData.getChildFile ("Application Support");
  85. #endif
  86. return userAppData.getChildFile (String ("Projucer-") + ProjectInfo::versionString);
  87. }
  88. juce_DeclareSingleton (CompileEngineDLL, false)
  89. private:
  90. DynamicLibrary dll;
  91. enum { requiredVersion = 2 };
  92. static File findDLLFile()
  93. {
  94. File dllFile;
  95. if (tryFindDLLFileInAppFolder(dllFile))
  96. return dllFile;
  97. #if JUCE_MAC
  98. if (tryFindDLLFileInAppBundle(dllFile))
  99. return dllFile;
  100. #endif
  101. if (tryFindDLLFileInAppConfigFolder(dllFile))
  102. return dllFile;
  103. return {};
  104. }
  105. #if JUCE_MAC
  106. static bool tryFindDLLFileInAppBundle(File &outFile)
  107. {
  108. File currentAppFile (File::getSpecialLocation (File::currentApplicationFile));
  109. return tryFindDLLFileInFolder (currentAppFile.getChildFile ("Contents"), outFile);
  110. }
  111. #endif
  112. static bool tryFindDLLFileInAppFolder(File &outFile)
  113. {
  114. File currentAppFile (File::getSpecialLocation (File::currentApplicationFile));
  115. return tryFindDLLFileInFolder (currentAppFile.getParentDirectory(), outFile);
  116. }
  117. static bool tryFindDLLFileInAppConfigFolder(File &outFile)
  118. {
  119. File userAppDataFolder (getVersionedUserAppSupportFolder());
  120. return tryFindDLLFileInFolder (userAppDataFolder, outFile);
  121. }
  122. static bool tryFindDLLFileInFolder(File folder, File& outFile)
  123. {
  124. File file = folder.getChildFile (getDLLName());
  125. if (isDLLFile (file))
  126. {
  127. outFile = file;
  128. return true;
  129. }
  130. return false;
  131. }
  132. static bool isDLLFile (const File& f)
  133. {
  134. return f.getFileName().equalsIgnoreCase (getDLLName()) && f.exists();
  135. }
  136. static void setPropertyCallback (const char* key, const char* value)
  137. {
  138. if (String (key).isNotEmpty())
  139. getGlobalProperties().setValue (key, value);
  140. else
  141. jassertfalse;
  142. }
  143. static void getPropertyCallback (const char* key, char* value, size_t size)
  144. {
  145. jassert (getGlobalProperties().getValue (key).getNumBytesAsUTF8() < size);
  146. value[0] = 0;
  147. getGlobalProperties().getValue (key).copyToUTF8 (value, size);
  148. }
  149. static void crashCallback (const char*) {}
  150. static void quitCallback() {}
  151. };