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.

542 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_IsSynth " << ((bool) project.getPluginIsSynth().getValue() ? 1 : 0) << newLine
  290. << "#define JucePlugin_SilenceInProducesSilenceOut " << ((bool) project.getPluginSilenceInProducesSilenceOut().getValue() ? 1 : 0) << newLine
  291. << "#define JucePlugin_TailLengthSeconds " << (double) project.getPluginTailLengthSeconds().getValue() << newLine
  292. << "#define JucePlugin_EditorRequiresKeyboardFocus " << ((bool) project.getPluginEditorNeedsKeyFocus().getValue() ? 1 : 0) << newLine
  293. << "#define JucePlugin_VersionCode " << createVersionCode (project.getVersion().toString()) << newLine
  294. << "#define JucePlugin_VersionString " << project.getVersion().toString().quoted() << newLine
  295. << "#define JucePlugin_VSTUniqueID JucePlugin_PluginCode" << newLine
  296. << "#define JucePlugin_VSTCategory " << ((bool) project.getPluginIsSynth().getValue() ? "kPlugCategSynth" : "kPlugCategEffect") << newLine
  297. << "#define JucePlugin_AUMainType " << ((bool) project.getPluginIsSynth().getValue() ? "kAudioUnitType_MusicDevice" : "kAudioUnitType_Effect") << newLine
  298. << "#define JucePlugin_AUSubType JucePlugin_PluginCode" << newLine
  299. << "#define JucePlugin_AUExportPrefix " << project.getPluginAUExportPrefix().toString() << newLine
  300. << "#define JucePlugin_AUExportPrefixQuoted " << project.getPluginAUExportPrefix().toString().quoted() << newLine
  301. << "#define JucePlugin_AUManufacturerCode JucePlugin_ManufacturerCode" << newLine
  302. << "#define JucePlugin_CFBundleIdentifier " << project.getBundleIdentifier().toString() << newLine
  303. << "#define JucePlugin_AUCocoaViewClassName " << project.getPluginAUCocoaViewClassName().toString() << newLine
  304. << "#define JucePlugin_RTASCategory " << ((bool) project.getPluginIsSynth().getValue() ? "ePlugInCategory_SWGenerators" : "ePlugInCategory_None") << newLine
  305. << "#define JucePlugin_RTASManufacturerCode JucePlugin_ManufacturerCode" << newLine
  306. << "#define JucePlugin_RTASProductId JucePlugin_PluginCode" << newLine
  307. << "#define JUCE_USE_VSTSDK_2_4 1" << newLine
  308. << newLine
  309. << "#endif // " << headerGuard << newLine;
  310. }
  311. bool replaceFileIfDifferent (const File& f, const MemoryOutputStream& newData)
  312. {
  313. if (! overwriteFileWithNewDataIfDifferent (f, newData))
  314. {
  315. errors.add ("Can't write to file: " + f.getFullPathName());
  316. return false;
  317. }
  318. return true;
  319. }
  320. void writeJuceSourceWrappers()
  321. {
  322. const File wrapperFolder (project.getWrapperFolder());
  323. appConfigFile = wrapperFolder.getChildFile (project.getAppConfigFilename());
  324. pluginCharacteristicsFile = wrapperFolder.getChildFile (project.getPluginCharacteristicsFilename());
  325. juceHeaderFile = project.getAppIncludeFile();
  326. binaryDataCpp = wrapperFolder.getChildFile ("BinaryData.cpp");
  327. if (resourceFile.getNumFiles() > 0)
  328. {
  329. if (! wrapperFolder.createDirectory())
  330. {
  331. errors.add ("Couldn't create folder: " + wrapperFolder.getFullPathName());
  332. return;
  333. }
  334. //resourceFile.setJuceHeaderToInclude (juceHeaderFile);
  335. resourceFile.setClassName ("BinaryData");
  336. if (! resourceFile.write (binaryDataCpp))
  337. errors.add ("Can't create binary resources file: " + binaryDataCpp.getFullPathName());
  338. }
  339. else
  340. {
  341. binaryDataCpp.deleteFile();
  342. binaryDataCpp.withFileExtension ("h").deleteFile();
  343. }
  344. if (project.isLibrary())
  345. return;
  346. if (! wrapperFolder.createDirectory())
  347. {
  348. errors.add ("Couldn't create folder: " + wrapperFolder.getFullPathName());
  349. return;
  350. }
  351. if (hasAppConfigFile)
  352. {
  353. MemoryOutputStream mem;
  354. writeAppConfig (mem);
  355. replaceFileIfDifferent (appConfigFile, mem);
  356. }
  357. else
  358. {
  359. appConfigFile.deleteFile();
  360. }
  361. if (project.isAudioPlugin())
  362. {
  363. MemoryOutputStream mem;
  364. writePluginCharacteristics (mem);
  365. replaceFileIfDifferent (pluginCharacteristicsFile, mem);
  366. }
  367. for (int i = 0; i <= project.getNumSeparateAmalgamatedFiles(); ++i)
  368. {
  369. const File sourceWrapperCpp (getSourceWrapperCpp (i));
  370. const File sourceWrapperMM (sourceWrapperCpp.withFileExtension (".mm"));
  371. if (numJuceSourceFiles > 0
  372. && ((i == 0 && numJuceSourceFiles == 1) || (i != 0 && numJuceSourceFiles > 1)))
  373. {
  374. MemoryOutputStream mem;
  375. writeSourceWrapper (mem, i);
  376. replaceFileIfDifferent (sourceWrapperCpp, mem);
  377. replaceFileIfDifferent (sourceWrapperMM, mem);
  378. }
  379. else
  380. {
  381. sourceWrapperMM.deleteFile();
  382. sourceWrapperCpp.deleteFile();
  383. }
  384. }
  385. if (hasAppHeaderFile)
  386. {
  387. MemoryOutputStream mem;
  388. writeAppHeader (mem);
  389. replaceFileIfDifferent (juceHeaderFile, mem);
  390. }
  391. else
  392. {
  393. juceHeaderFile.deleteFile();
  394. }
  395. }
  396. void writeProjects()
  397. {
  398. for (int i = project.getNumExporters(); --i >= 0;)
  399. {
  400. ScopedPointer <ProjectExporter> exporter (project.createExporter (i));
  401. std::cout << "Writing files for: " << exporter->getName() << std::endl;
  402. const File targetFolder (exporter->getTargetFolder());
  403. if (targetFolder.createDirectory())
  404. {
  405. if (hasAppConfigFile)
  406. exporter->juceWrapperFiles.add (RelativePath (appConfigFile, targetFolder, RelativePath::buildTargetFolder));
  407. if (hasAppHeaderFile)
  408. exporter->juceWrapperFiles.add (RelativePath (juceHeaderFile, targetFolder, RelativePath::buildTargetFolder));
  409. if (hasResources)
  410. {
  411. exporter->juceWrapperFiles.add (RelativePath (binaryDataCpp, targetFolder, RelativePath::buildTargetFolder));
  412. exporter->juceWrapperFiles.add (RelativePath (binaryDataCpp, targetFolder, RelativePath::buildTargetFolder)
  413. .withFileExtension (".h"));
  414. }
  415. if (numJuceSourceFiles > 0)
  416. {
  417. for (int i = 0; i <= project.getNumSeparateAmalgamatedFiles(); ++i)
  418. {
  419. const File sourceWrapperCpp (getSourceWrapperCpp (i));
  420. const File sourceWrapperMM (sourceWrapperCpp.withFileExtension (".mm"));
  421. if ((i == 0 && numJuceSourceFiles == 1) || (i != 0 && numJuceSourceFiles > 1))
  422. {
  423. if (exporter->usesMMFiles())
  424. exporter->juceWrapperFiles.add (RelativePath (sourceWrapperMM, targetFolder, RelativePath::buildTargetFolder));
  425. else
  426. exporter->juceWrapperFiles.add (RelativePath (sourceWrapperCpp, targetFolder, RelativePath::buildTargetFolder));
  427. }
  428. }
  429. }
  430. if (project.isAudioPlugin())
  431. exporter->juceWrapperFiles.add (RelativePath (pluginCharacteristicsFile, targetFolder, RelativePath::buildTargetFolder));
  432. String error = exporter->create();
  433. if (error.isNotEmpty())
  434. errors.add (error);
  435. }
  436. else
  437. {
  438. errors.add ("Can't create folder: " + exporter->getTargetFolder().getFullPathName());
  439. }
  440. }
  441. }
  442. const File getSourceWrapperCpp (int fileIndex) const
  443. {
  444. return project.getWrapperFolder().getChildFile (project.getJuceSourceFilenameRoot() + (fileIndex != 0 ? String (fileIndex) : String::empty))
  445. .withFileExtension (".cpp");
  446. }
  447. };
  448. #endif