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.

541 lines
23KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_PROJECTSAVER_JUCEHEADER__
  19. #define __JUCER_PROJECTSAVER_JUCEHEADER__
  20. //==============================================================================
  21. class ProjectSaver
  22. {
  23. public:
  24. ProjectSaver (Project& project_, const File& projectFile_)
  25. : project (project_), projectFile (projectFile_), resourceFile (project_)
  26. {
  27. }
  28. const String save()
  29. {
  30. const File oldFile (project.getFile());
  31. project.setFile (projectFile);
  32. switch (project.getJuceLinkageMode())
  33. {
  34. case Project::notLinkedToJuce:
  35. hasAppHeaderFile = ! project.isLibrary();
  36. hasAppConfigFile = false;
  37. numJuceSourceFiles = 0;
  38. break;
  39. case Project::useAmalgamatedJuce:
  40. case Project::useAmalgamatedJuceViaSingleTemplate:
  41. hasAppHeaderFile = true;
  42. hasAppConfigFile = true;
  43. numJuceSourceFiles = 1;
  44. break;
  45. case Project::useAmalgamatedJuceViaMultipleTemplates:
  46. hasAppHeaderFile = true;
  47. hasAppConfigFile = true;
  48. numJuceSourceFiles = project.getNumSeparateAmalgamatedFiles();
  49. break;
  50. case Project::useLinkedJuce:
  51. hasAppHeaderFile = true;
  52. hasAppConfigFile = true;
  53. numJuceSourceFiles = 0;
  54. break;
  55. default:
  56. jassertfalse;
  57. break;
  58. }
  59. hasResources = (resourceFile.getNumFiles() > 0);
  60. writeMainProjectFile();
  61. if (errors.size() == 0)
  62. writeJuceSourceWrappers();
  63. if (errors.size() == 0)
  64. writeProjects();
  65. if (errors.size() > 0)
  66. project.setFile (oldFile);
  67. return errors[0];
  68. }
  69. private:
  70. Project& project;
  71. const File& projectFile;
  72. ResourceFile resourceFile;
  73. StringArray errors;
  74. File appConfigFile, juceHeaderFile, binaryDataCpp, pluginCharacteristicsFile;
  75. bool hasAppHeaderFile, hasAppConfigFile, hasResources;
  76. int numJuceSourceFiles;
  77. void writeMainProjectFile()
  78. {
  79. ScopedPointer <XmlElement> xml (project.getProjectRoot().createXml());
  80. jassert (xml != 0);
  81. if (xml != 0)
  82. {
  83. #if JUCE_DEBUG
  84. {
  85. MemoryOutputStream mo;
  86. project.getProjectRoot().writeToStream (mo);
  87. MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
  88. ValueTree v = ValueTree::readFromStream (mi);
  89. ScopedPointer <XmlElement> xml2 (v.createXml());
  90. // This bit just tests that ValueTree save/load works reliably.. Let me know if this asserts for you!
  91. jassert (xml->isEquivalentTo (xml2, true));
  92. }
  93. #endif
  94. MemoryOutputStream mo;
  95. xml->writeToStream (mo, String::empty);
  96. if (! overwriteFileWithNewDataIfDifferent (projectFile, mo))
  97. errors.add ("Couldn't write to the target file!");
  98. }
  99. }
  100. void writeJucerComment (OutputStream& out)
  101. {
  102. out << "/*" << newLine << newLine
  103. << " IMPORTANT! This file is auto-generated by the Jucer each time you save your" << newLine
  104. << " project - if you alter its contents, your changes may be overwritten!" << newLine
  105. << newLine;
  106. }
  107. void writeAppConfig (OutputStream& out)
  108. {
  109. writeJucerComment (out);
  110. out << " If you want to change any of these values, use the Jucer to do so, rather than" << newLine
  111. << " editing this file directly!" << newLine
  112. << newLine
  113. << " Any commented-out settings will fall back to using the default values that" << newLine
  114. << " they are given in juce_Config.h" << newLine
  115. << newLine
  116. << "*/" << newLine << newLine;
  117. bool notActive = project.getJuceLinkageMode() == Project::useLinkedJuce
  118. || project.getJuceLinkageMode() == Project::notLinkedToJuce;
  119. if (notActive)
  120. out << "/* NOTE: These configs aren't available when you're linking to the juce library statically!" << newLine
  121. << " If you need to set a configuration that differs from the default, you'll need" << newLine
  122. << " to include the amalgamated Juce files." << newLine << newLine;
  123. OwnedArray <Project::JuceConfigFlag> flags;
  124. project.getJuceConfigFlags (flags);
  125. for (int i = 0; i < flags.size(); ++i)
  126. {
  127. const Project::JuceConfigFlag* const f = flags[i];
  128. int value = (int) f->value.getValue();
  129. if (value < 1 || value > 2)
  130. out << "//#define ";
  131. else
  132. out << "#define ";
  133. out << f->symbol;
  134. if (value == 1)
  135. out << " 1";
  136. else if (value == 2)
  137. out << " 0";
  138. out << newLine;
  139. }
  140. if (notActive)
  141. out << newLine << "*/" << newLine;
  142. }
  143. void writeSourceWrapper (OutputStream& out, int fileNumber)
  144. {
  145. writeJucerComment (out);
  146. out << " This file pulls in all the Juce source code, and builds it using the settings" << newLine
  147. << " defined in " << appConfigFile.getFileName() << "." << newLine
  148. << newLine
  149. << " If you want to change the method by which Juce is linked into your app, use the" << newLine
  150. << " Jucer to change it, rather than trying to edit this file directly." << newLine
  151. << newLine
  152. << "*/"
  153. << newLine << newLine
  154. << createIncludeStatement (appConfigFile, appConfigFile) << newLine;
  155. if (fileNumber == 0)
  156. writeInclude (out, project.isUsingFullyAmalgamatedFile() ? "juce_amalgamated.cpp"
  157. : "amalgamation/juce_amalgamated_template.cpp");
  158. else
  159. writeInclude (out, "amalgamation/juce_amalgamated" + String (fileNumber) + ".cpp");
  160. }
  161. void writeAppHeader (OutputStream& out)
  162. {
  163. writeJucerComment (out);
  164. out << " This is the header file that your files should include in order to get all the" << newLine
  165. << " Juce library headers. You should NOT include juce.h or juce_amalgamated.h directly in" << newLine
  166. << " your own source files, because that wouldn't pick up the correct Juce configuration" << newLine
  167. << " options for your app." << newLine
  168. << newLine
  169. << "*/" << newLine << newLine;
  170. String headerGuard ("__APPHEADERFILE_" + String::toHexString (juceHeaderFile.hashCode()).toUpperCase() + "__");
  171. out << "#ifndef " << headerGuard << newLine
  172. << "#define " << headerGuard << newLine << newLine;
  173. if (hasAppConfigFile)
  174. out << createIncludeStatement (appConfigFile, appConfigFile) << newLine;
  175. if (project.getJuceLinkageMode() != Project::notLinkedToJuce)
  176. {
  177. writeInclude (out, (project.isUsingSingleTemplateFile() || project.isUsingMultipleTemplateFiles())
  178. ? "juce_amalgamated.h" // could use "amalgamation/juce_amalgamated_template.h", but it's slower..
  179. : (project.isUsingFullyAmalgamatedFile()
  180. ? "juce_amalgamated.h"
  181. : "juce.h"));
  182. }
  183. if (binaryDataCpp.exists())
  184. out << createIncludeStatement (binaryDataCpp.withFileExtension (".h"), appConfigFile) << newLine;
  185. out << newLine
  186. << "namespace ProjectInfo" << newLine
  187. << "{" << newLine
  188. << " const char* const projectName = " << replaceCEscapeChars (project.getProjectName().toString()).quoted() << ";" << newLine
  189. << " const char* const versionString = " << replaceCEscapeChars (project.getVersion().toString()).quoted() << ";" << newLine
  190. << " const int versionNumber = " << createVersionCode (project.getVersion().toString()) << ";" << newLine
  191. << "}" << newLine
  192. << newLine
  193. << "#endif // " << headerGuard << newLine;
  194. }
  195. void writeInclude (OutputStream& out, const String& pathFromJuceFolder)
  196. {
  197. StringArray paths, guards;
  198. for (int i = project.getNumExporters(); --i >= 0;)
  199. {
  200. ScopedPointer <ProjectExporter> exporter (project.createExporter (i));
  201. if (exporter != 0)
  202. {
  203. const RelativePath juceFromProject (exporter->getJuceFolder().toString(), RelativePath::projectFolder);
  204. const RelativePath fileFromProject (juceFromProject.getChildFile (pathFromJuceFolder));
  205. const RelativePath fileFromHere (fileFromProject.rebased (project.getFile().getParentDirectory(),
  206. juceHeaderFile.getParentDirectory(), RelativePath::unknown));
  207. paths.add (fileFromHere.toUnixStyle());
  208. guards.add (exporter->getOSTestMacro());
  209. }
  210. }
  211. StringArray uniquePaths (paths);
  212. uniquePaths.removeDuplicates (false);
  213. if (uniquePaths.size() == 1)
  214. {
  215. out << "#include " << paths[0].quoted() << newLine;
  216. }
  217. else
  218. {
  219. int i = paths.size();
  220. for (; --i >= 0;)
  221. {
  222. for (int j = i; --j >= 0;)
  223. {
  224. if (paths[i] == paths[j] && guards[i] == guards[j])
  225. {
  226. paths.remove (i);
  227. guards.remove (i);
  228. }
  229. }
  230. }
  231. for (i = 0; i < paths.size(); ++i)
  232. {
  233. out << (i == 0 ? "#if " : "#elif ") << guards[i] << newLine
  234. << " #include " << paths[i].quoted() << newLine;
  235. }
  236. out << "#endif" << newLine;
  237. }
  238. }
  239. static int countMaxPluginChannels (const String& configString, bool isInput)
  240. {
  241. StringArray configs;
  242. configs.addTokens (configString, ", {}", String::empty);
  243. configs.trim();
  244. configs.removeEmptyStrings();
  245. jassert ((configs.size() & 1) == 0); // looks like a syntax error in the configs?
  246. int maxVal = 0;
  247. for (int i = (isInput ? 0 : 1); i < configs.size(); i += 2)
  248. maxVal = jmax (maxVal, configs[i].getIntValue());
  249. return maxVal;
  250. }
  251. static const String createVersionCode (const String& version)
  252. {
  253. StringArray configs;
  254. configs.addTokens (version, ",.", String::empty);
  255. configs.trim();
  256. configs.removeEmptyStrings();
  257. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  258. if (configs.size() >= 4)
  259. value = (value << 8) + configs[3].getIntValue();
  260. return "0x" + String::toHexString (value);
  261. }
  262. void writePluginCharacteristics (OutputStream& out)
  263. {
  264. String headerGuard ("__PLUGINCHARACTERISTICS_" + String::toHexString (pluginCharacteristicsFile.hashCode()).toUpperCase() + "__");
  265. writeJucerComment (out);
  266. out << " This header file contains configuration options for the plug-in. If you need to change any of" << newLine
  267. << " these, it'd be wise to do so using the Jucer, rather than editing this file directly..." << newLine
  268. << newLine
  269. << "*/" << newLine
  270. << newLine
  271. << "#ifndef " << headerGuard << newLine
  272. << "#define " << headerGuard << newLine
  273. << newLine
  274. << "#define JucePlugin_Build_VST " << ((bool) project.shouldBuildVST().getValue() ? 1 : 0) << newLine
  275. << "#define JucePlugin_Build_AU " << ((bool) project.shouldBuildAU().getValue() ? 1 : 0) << newLine
  276. << "#define JucePlugin_Build_RTAS " << ((bool) project.shouldBuildRTAS().getValue() ? 1 : 0) << newLine
  277. << newLine
  278. << "#define JucePlugin_Name " << project.getPluginName().toString().quoted() << newLine
  279. << "#define JucePlugin_Desc " << project.getPluginDesc().toString().quoted() << newLine
  280. << "#define JucePlugin_Manufacturer " << project.getPluginManufacturer().toString().quoted() << newLine
  281. << "#define JucePlugin_ManufacturerCode '" << project.getPluginManufacturerCode().toString().trim().substring (0, 4) << "'" << newLine
  282. << "#define JucePlugin_PluginCode '" << project.getPluginCode().toString().trim().substring (0, 4) << "'" << newLine
  283. << "#define JucePlugin_MaxNumInputChannels " << countMaxPluginChannels (project.getPluginChannelConfigs().toString(), true) << newLine
  284. << "#define JucePlugin_MaxNumOutputChannels " << countMaxPluginChannels (project.getPluginChannelConfigs().toString(), false) << newLine
  285. << "#define JucePlugin_PreferredChannelConfigurations " << project.getPluginChannelConfigs().toString() << newLine
  286. << "#define JucePlugin_IsSynth " << ((bool) project.getPluginIsSynth().getValue() ? 1 : 0) << newLine
  287. << "#define JucePlugin_WantsMidiInput " << ((bool) project.getPluginWantsMidiInput().getValue() ? 1 : 0) << newLine
  288. << "#define JucePlugin_ProducesMidiOutput " << ((bool) project.getPluginProducesMidiOut().getValue() ? 1 : 0) << newLine
  289. << "#define JucePlugin_SilenceInProducesSilenceOut " << ((bool) project.getPluginSilenceInProducesSilenceOut().getValue() ? 1 : 0) << newLine
  290. << "#define JucePlugin_TailLengthSeconds " << (double) project.getPluginTailLengthSeconds().getValue() << newLine
  291. << "#define JucePlugin_EditorRequiresKeyboardFocus " << ((bool) project.getPluginEditorNeedsKeyFocus().getValue() ? 1 : 0) << newLine
  292. << "#define JucePlugin_VersionCode " << createVersionCode (project.getVersion().toString()) << newLine
  293. << "#define JucePlugin_VersionString " << project.getVersion().toString().quoted() << newLine
  294. << "#define JucePlugin_VSTUniqueID JucePlugin_PluginCode" << newLine
  295. << "#define JucePlugin_VSTCategory " << ((bool) project.getPluginIsSynth().getValue() ? "kPlugCategSynth" : "kPlugCategEffect") << newLine
  296. << "#define JucePlugin_AUMainType " << ((bool) project.getPluginIsSynth().getValue() ? "kAudioUnitType_MusicDevice" : "kAudioUnitType_Effect") << newLine
  297. << "#define JucePlugin_AUSubType JucePlugin_PluginCode" << newLine
  298. << "#define JucePlugin_AUExportPrefix " << project.getPluginAUExportPrefix().toString() << newLine
  299. << "#define JucePlugin_AUExportPrefixQuoted " << project.getPluginAUExportPrefix().toString().quoted() << newLine
  300. << "#define JucePlugin_AUManufacturerCode JucePlugin_ManufacturerCode" << newLine
  301. << "#define JucePlugin_CFBundleIdentifier " << project.getBundleIdentifier().toString() << newLine
  302. << "#define JucePlugin_AUCocoaViewClassName " << project.getPluginAUCocoaViewClassName().toString() << newLine
  303. << "#define JucePlugin_RTASCategory " << ((bool) project.getPluginIsSynth().getValue() ? "ePlugInCategory_SWGenerators" : "ePlugInCategory_None") << newLine
  304. << "#define JucePlugin_RTASManufacturerCode JucePlugin_ManufacturerCode" << newLine
  305. << "#define JucePlugin_RTASProductId JucePlugin_PluginCode" << newLine
  306. << "#define JUCE_USE_VSTSDK_2_4 1" << newLine
  307. << newLine
  308. << "#endif // " << headerGuard << newLine;
  309. }
  310. bool replaceFileIfDifferent (const File& f, const MemoryOutputStream& newData)
  311. {
  312. if (! overwriteFileWithNewDataIfDifferent (f, newData))
  313. {
  314. errors.add ("Can't write to file: " + f.getFullPathName());
  315. return false;
  316. }
  317. return true;
  318. }
  319. void writeJuceSourceWrappers()
  320. {
  321. const File wrapperFolder (project.getWrapperFolder());
  322. appConfigFile = wrapperFolder.getChildFile (project.getAppConfigFilename());
  323. pluginCharacteristicsFile = wrapperFolder.getChildFile (project.getPluginCharacteristicsFilename());
  324. juceHeaderFile = project.getAppIncludeFile();
  325. binaryDataCpp = wrapperFolder.getChildFile ("BinaryData.cpp");
  326. if (resourceFile.getNumFiles() > 0)
  327. {
  328. if (! wrapperFolder.createDirectory())
  329. {
  330. errors.add ("Couldn't create folder: " + wrapperFolder.getFullPathName());
  331. return;
  332. }
  333. //resourceFile.setJuceHeaderToInclude (juceHeaderFile);
  334. resourceFile.setClassName ("BinaryData");
  335. if (! resourceFile.write (binaryDataCpp))
  336. errors.add ("Can't create binary resources file: " + binaryDataCpp.getFullPathName());
  337. }
  338. else
  339. {
  340. binaryDataCpp.deleteFile();
  341. binaryDataCpp.withFileExtension ("h").deleteFile();
  342. }
  343. if (project.isLibrary())
  344. return;
  345. if (! wrapperFolder.createDirectory())
  346. {
  347. errors.add ("Couldn't create folder: " + wrapperFolder.getFullPathName());
  348. return;
  349. }
  350. if (hasAppConfigFile)
  351. {
  352. MemoryOutputStream mem;
  353. writeAppConfig (mem);
  354. replaceFileIfDifferent (appConfigFile, mem);
  355. }
  356. else
  357. {
  358. appConfigFile.deleteFile();
  359. }
  360. if (project.isAudioPlugin())
  361. {
  362. MemoryOutputStream mem;
  363. writePluginCharacteristics (mem);
  364. replaceFileIfDifferent (pluginCharacteristicsFile, mem);
  365. }
  366. for (int i = 0; i <= project.getNumSeparateAmalgamatedFiles(); ++i)
  367. {
  368. const File sourceWrapperCpp (getSourceWrapperCpp (i));
  369. const File sourceWrapperMM (sourceWrapperCpp.withFileExtension (".mm"));
  370. if (numJuceSourceFiles > 0
  371. && ((i == 0 && numJuceSourceFiles == 1) || (i != 0 && numJuceSourceFiles > 1)))
  372. {
  373. MemoryOutputStream mem;
  374. writeSourceWrapper (mem, i);
  375. replaceFileIfDifferent (sourceWrapperCpp, mem);
  376. replaceFileIfDifferent (sourceWrapperMM, mem);
  377. }
  378. else
  379. {
  380. sourceWrapperMM.deleteFile();
  381. sourceWrapperCpp.deleteFile();
  382. }
  383. }
  384. if (hasAppHeaderFile)
  385. {
  386. MemoryOutputStream mem;
  387. writeAppHeader (mem);
  388. replaceFileIfDifferent (juceHeaderFile, mem);
  389. }
  390. else
  391. {
  392. juceHeaderFile.deleteFile();
  393. }
  394. }
  395. void writeProjects()
  396. {
  397. for (int i = project.getNumExporters(); --i >= 0;)
  398. {
  399. ScopedPointer <ProjectExporter> exporter (project.createExporter (i));
  400. std::cout << "Writing files for: " << exporter->getName() << std::endl;
  401. const File targetFolder (exporter->getTargetFolder());
  402. if (targetFolder.createDirectory())
  403. {
  404. if (hasAppConfigFile)
  405. exporter->juceWrapperFiles.add (RelativePath (appConfigFile, targetFolder, RelativePath::buildTargetFolder));
  406. if (hasAppHeaderFile)
  407. exporter->juceWrapperFiles.add (RelativePath (juceHeaderFile, targetFolder, RelativePath::buildTargetFolder));
  408. if (hasResources)
  409. {
  410. exporter->juceWrapperFiles.add (RelativePath (binaryDataCpp, targetFolder, RelativePath::buildTargetFolder));
  411. exporter->juceWrapperFiles.add (RelativePath (binaryDataCpp, targetFolder, RelativePath::buildTargetFolder)
  412. .withFileExtension (".h"));
  413. }
  414. if (numJuceSourceFiles > 0)
  415. {
  416. for (int i = 0; i <= project.getNumSeparateAmalgamatedFiles(); ++i)
  417. {
  418. const File sourceWrapperCpp (getSourceWrapperCpp (i));
  419. const File sourceWrapperMM (sourceWrapperCpp.withFileExtension (".mm"));
  420. if ((i == 0 && numJuceSourceFiles == 1) || (i != 0 && numJuceSourceFiles > 1))
  421. {
  422. if (exporter->usesMMFiles())
  423. exporter->juceWrapperFiles.add (RelativePath (sourceWrapperMM, targetFolder, RelativePath::buildTargetFolder));
  424. else
  425. exporter->juceWrapperFiles.add (RelativePath (sourceWrapperCpp, targetFolder, RelativePath::buildTargetFolder));
  426. }
  427. }
  428. }
  429. if (project.isAudioPlugin())
  430. exporter->juceWrapperFiles.add (RelativePath (pluginCharacteristicsFile, targetFolder, RelativePath::buildTargetFolder));
  431. String error = exporter->create();
  432. if (error.isNotEmpty())
  433. errors.add (error);
  434. }
  435. else
  436. {
  437. errors.add ("Can't create folder: " + exporter->getTargetFolder().getFullPathName());
  438. }
  439. }
  440. }
  441. const File getSourceWrapperCpp (int fileIndex) const
  442. {
  443. return project.getWrapperFolder().getChildFile (project.getJuceSourceFilenameRoot() + (fileIndex != 0 ? String (fileIndex) : String::empty))
  444. .withFileExtension (".cpp");
  445. }
  446. };
  447. #endif