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.

360 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace build_tools
  21. {
  22. void overwriteFileIfDifferentOrThrow (const File& file, const MemoryOutputStream& newData)
  23. {
  24. if (! overwriteFileWithNewDataIfDifferent (file, newData))
  25. throw SaveError (file);
  26. }
  27. void overwriteFileIfDifferentOrThrow (const File& file, const String& newData)
  28. {
  29. if (! overwriteFileWithNewDataIfDifferent (file, newData))
  30. throw SaveError (file);
  31. }
  32. String replacePreprocessorDefs (const StringPairArray& definitions, String sourceString)
  33. {
  34. for (int i = 0; i < definitions.size(); ++i)
  35. {
  36. const String key (definitions.getAllKeys()[i]);
  37. const String value (definitions.getAllValues()[i]);
  38. sourceString = sourceString.replace ("${" + key + "}", value);
  39. }
  40. return sourceString;
  41. }
  42. String getXcodePackageType (ProjectType::Target::Type type)
  43. {
  44. using Type = ProjectType::Target::Type;
  45. switch (type)
  46. {
  47. case Type::GUIApp:
  48. case Type::StandalonePlugIn:
  49. return "APPL";
  50. case Type::VSTPlugIn:
  51. case Type::VST3PlugIn:
  52. case Type::AudioUnitPlugIn:
  53. case Type::UnityPlugIn:
  54. return "BNDL";
  55. case Type::AudioUnitv3PlugIn:
  56. return "XPC!";
  57. case Type::AAXPlugIn:
  58. return "TDMw";
  59. case Type::ConsoleApp:
  60. case Type::StaticLibrary:
  61. case Type::DynamicLibrary:
  62. case Type::LV2PlugIn:
  63. case Type::LV2Helper:
  64. case Type::VST3Helper:
  65. case Type::SharedCodeTarget:
  66. case Type::AggregateTarget:
  67. case Type::unspecified:
  68. default:
  69. return {};
  70. }
  71. }
  72. String getXcodeBundleSignature (ProjectType::Target::Type type)
  73. {
  74. using Type = ProjectType::Target::Type;
  75. switch (type)
  76. {
  77. case Type::GUIApp:
  78. case Type::VSTPlugIn:
  79. case Type::VST3PlugIn:
  80. case Type::AudioUnitPlugIn:
  81. case Type::StandalonePlugIn:
  82. case Type::AudioUnitv3PlugIn:
  83. case Type::UnityPlugIn:
  84. return "????";
  85. case Type::AAXPlugIn:
  86. return "PTul";
  87. case Type::ConsoleApp:
  88. case Type::StaticLibrary:
  89. case Type::DynamicLibrary:
  90. case Type::LV2PlugIn:
  91. case Type::LV2Helper:
  92. case Type::VST3Helper:
  93. case Type::SharedCodeTarget:
  94. case Type::AggregateTarget:
  95. case Type::unspecified:
  96. default:
  97. return {};
  98. }
  99. }
  100. static unsigned int calculateHash (const String& s, const unsigned int hashMultiplier)
  101. {
  102. auto t = s.toUTF8();
  103. unsigned int hash = 0;
  104. while (*t != 0)
  105. hash = hashMultiplier * hash + (unsigned int) *t++;
  106. return hash;
  107. }
  108. static unsigned int findBestHashMultiplier (const StringArray& strings)
  109. {
  110. unsigned int v = 31;
  111. for (;;)
  112. {
  113. SortedSet<unsigned int> hashes;
  114. bool collision = false;
  115. for (int i = strings.size(); --i >= 0;)
  116. {
  117. auto hash = calculateHash (strings[i], v);
  118. if (hashes.contains (hash))
  119. {
  120. collision = true;
  121. break;
  122. }
  123. hashes.add (hash);
  124. }
  125. if (! collision)
  126. break;
  127. v += 2;
  128. }
  129. return v;
  130. }
  131. String makeValidIdentifier (String s, bool makeCamelCase, bool removeColons, bool allowTemplates, bool allowAsterisks)
  132. {
  133. if (s.isEmpty())
  134. return "unknown";
  135. if (removeColons)
  136. s = s.replaceCharacters (".,;:/@", "______");
  137. else
  138. s = s.replaceCharacters (".,;/@", "_____");
  139. for (int i = s.length(); --i > 0;)
  140. if (CharacterFunctions::isLetter (s[i])
  141. && CharacterFunctions::isLetter (s[i - 1])
  142. && CharacterFunctions::isUpperCase (s[i])
  143. && ! CharacterFunctions::isUpperCase (s[i - 1]))
  144. s = s.substring (0, i) + " " + s.substring (i);
  145. String allowedChars ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ 0123456789");
  146. if (allowTemplates)
  147. allowedChars += "<>";
  148. if (! removeColons)
  149. allowedChars += ":";
  150. if (allowAsterisks)
  151. allowedChars += "*";
  152. StringArray words;
  153. words.addTokens (s.retainCharacters (allowedChars), false);
  154. words.trim();
  155. auto n = words[0];
  156. if (makeCamelCase)
  157. n = n.toLowerCase();
  158. for (int i = 1; i < words.size(); ++i)
  159. {
  160. if (makeCamelCase && words[i].length() > 1)
  161. n << words[i].substring (0, 1).toUpperCase()
  162. << words[i].substring (1).toLowerCase();
  163. else
  164. n << words[i];
  165. }
  166. if (CharacterFunctions::isDigit (n[0]))
  167. n = "_" + n;
  168. if (isReservedKeyword (n))
  169. n << '_';
  170. return n;
  171. }
  172. String makeBinaryDataIdentifierName (const File& file)
  173. {
  174. return makeValidIdentifier (file.getFileName()
  175. .replaceCharacters (" .", "__")
  176. .retainCharacters ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"),
  177. false, true, false);
  178. }
  179. void writeDataAsCppLiteral (const MemoryBlock& mb, OutputStream& out,
  180. bool breakAtNewLines, bool allowStringBreaks)
  181. {
  182. const int maxCharsOnLine = 250;
  183. auto data = (const unsigned char*) mb.getData();
  184. int charsOnLine = 0;
  185. bool canUseStringLiteral = mb.getSize() < 32768; // MS compilers can't handle big string literals..
  186. if (canUseStringLiteral)
  187. {
  188. unsigned int numEscaped = 0;
  189. for (size_t i = 0; i < mb.getSize(); ++i)
  190. {
  191. auto num = (unsigned int) data[i];
  192. if (! ((num >= 32 && num < 127) || num == '\t' || num == '\r' || num == '\n'))
  193. {
  194. if (++numEscaped > mb.getSize() / 4)
  195. {
  196. canUseStringLiteral = false;
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. if (! canUseStringLiteral)
  203. {
  204. out << "{ ";
  205. for (size_t i = 0; i < mb.getSize(); ++i)
  206. {
  207. auto num = (int) (unsigned int) data[i];
  208. out << num << ',';
  209. charsOnLine += 2;
  210. if (num >= 10)
  211. {
  212. ++charsOnLine;
  213. if (num >= 100)
  214. ++charsOnLine;
  215. }
  216. if (charsOnLine >= maxCharsOnLine)
  217. {
  218. charsOnLine = 0;
  219. out << newLine;
  220. }
  221. }
  222. out << "0,0 };";
  223. }
  224. else
  225. {
  226. out << "\"";
  227. writeEscapeChars (out, (const char*) data, (int) mb.getSize(),
  228. maxCharsOnLine, breakAtNewLines, false, allowStringBreaks);
  229. out << "\";";
  230. }
  231. }
  232. void createStringMatcher (OutputStream& out, const String& utf8PointerVariable,
  233. const StringArray& strings, const StringArray& codeToExecute, const int indentLevel)
  234. {
  235. jassert (strings.size() == codeToExecute.size());
  236. auto indent = String::repeatedString (" ", indentLevel);
  237. auto hashMultiplier = findBestHashMultiplier (strings);
  238. out << indent << "unsigned int hash = 0;" << newLine
  239. << newLine
  240. << indent << "if (" << utf8PointerVariable << " != nullptr)" << newLine
  241. << indent << " while (*" << utf8PointerVariable << " != 0)" << newLine
  242. << indent << " hash = " << (int) hashMultiplier << " * hash + (unsigned int) *" << utf8PointerVariable << "++;" << newLine
  243. << newLine
  244. << indent << "switch (hash)" << newLine
  245. << indent << "{" << newLine;
  246. for (int i = 0; i < strings.size(); ++i)
  247. {
  248. out << indent << " case 0x" << hexString8Digits ((int) calculateHash (strings[i], hashMultiplier))
  249. << ": " << codeToExecute[i] << newLine;
  250. }
  251. out << indent << " default: break;" << newLine
  252. << indent << "}" << newLine << newLine;
  253. }
  254. String unixStylePath (const String& path) { return path.replaceCharacter ('\\', '/'); }
  255. String windowsStylePath (const String& path) { return path.replaceCharacter ('/', '\\'); }
  256. String currentOSStylePath (const String& path)
  257. {
  258. #if JUCE_WINDOWS
  259. return windowsStylePath (path);
  260. #else
  261. return unixStylePath (path);
  262. #endif
  263. }
  264. bool isAbsolutePath (const String& path)
  265. {
  266. return File::isAbsolutePath (path)
  267. || path.startsWithChar ('/') // (needed because File::isAbsolutePath will ignore forward-slashes on Windows)
  268. || path.startsWithChar ('$')
  269. || path.startsWithChar ('~')
  270. || (CharacterFunctions::isLetter (path[0]) && path[1] == ':')
  271. || path.startsWithIgnoreCase ("smb:");
  272. }
  273. String getRelativePathFrom (const File& file, const File& sourceFolder)
  274. {
  275. #if ! JUCE_WINDOWS
  276. // On a non-windows machine, we can't know if a drive-letter path may be relative or not.
  277. if (CharacterFunctions::isLetter (file.getFullPathName()[0]) && file.getFullPathName()[1] == ':')
  278. return file.getFullPathName();
  279. #endif
  280. return file.getRelativePathFrom (sourceFolder);
  281. }
  282. void writeStreamToFile (const File& file, const std::function<void (MemoryOutputStream&)>& writer)
  283. {
  284. MemoryOutputStream mo;
  285. writer (mo);
  286. overwriteFileIfDifferentOrThrow (file, mo);
  287. }
  288. }
  289. }