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.

351 lines
11KB

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