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.

356 lines
11KB

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