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.

345 lines
11KB

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