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.

457 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. #include "jucer_Module.h"
  19. #include "jucer_ProjectType.h"
  20. #include "jucer_ProjectExporter.h"
  21. #include "jucer_ProjectSaver.h"
  22. #include "jucer_AudioPluginModule.h"
  23. //==============================================================================
  24. ModuleList::ModuleList()
  25. {
  26. rescan();
  27. }
  28. ModuleList& ModuleList::getInstance()
  29. {
  30. static ModuleList list;
  31. return list;
  32. }
  33. struct ModuleSorter
  34. {
  35. static int compareElements (const ModuleList::Module* m1, const ModuleList::Module* m2)
  36. {
  37. return m1->uid.compareIgnoreCase (m2->uid);
  38. }
  39. };
  40. void ModuleList::rescan()
  41. {
  42. modules.clear();
  43. moduleFolder = StoredSettings::getInstance()->getLastKnownJuceFolder().getChildFile ("modules");
  44. DirectoryIterator iter (moduleFolder, false, "*", File::findDirectories);
  45. while (iter.next())
  46. {
  47. const File moduleDef (iter.getFile().getChildFile ("juce_module_info"));
  48. if (moduleDef.exists())
  49. {
  50. ScopedPointer<LibraryModule> m (new LibraryModule (moduleDef));
  51. jassert (m->isValid());
  52. if (m->isValid())
  53. {
  54. Module* info = new Module();
  55. modules.add (info);
  56. info->uid = m->getID();
  57. info->name = m->moduleInfo ["name"];
  58. info->description = m->moduleInfo ["description"];
  59. info->file = moduleDef;
  60. }
  61. }
  62. }
  63. ModuleSorter sorter;
  64. modules.sort (sorter);
  65. }
  66. LibraryModule* ModuleList::Module::create() const
  67. {
  68. return new LibraryModule (file);
  69. }
  70. LibraryModule* ModuleList::loadModule (const String& uid) const
  71. {
  72. const Module* const m = findModuleInfo (uid);
  73. return m != nullptr ? m->create() : nullptr;
  74. }
  75. const ModuleList::Module* ModuleList::findModuleInfo (const String& uid) const
  76. {
  77. for (int i = modules.size(); --i >= 0;)
  78. if (modules.getUnchecked(i)->uid == uid)
  79. return modules.getUnchecked(i);
  80. return nullptr;
  81. }
  82. void ModuleList::getDependencies (const String& moduleID, StringArray& dependencies) const
  83. {
  84. ScopedPointer<LibraryModule> m (loadModule (moduleID));
  85. if (m != nullptr)
  86. {
  87. const var depsArray (m->moduleInfo ["dependencies"]);
  88. const Array<var>* const deps = depsArray.getArray();
  89. for (int i = 0; i < deps->size(); ++i)
  90. {
  91. const var& d = deps->getReference(i);
  92. String uid (d ["id"].toString());
  93. String version (d ["version"].toString());
  94. if (! dependencies.contains (uid, true))
  95. {
  96. dependencies.add (uid);
  97. getDependencies (uid, dependencies);
  98. }
  99. }
  100. }
  101. }
  102. void ModuleList::createDependencies (const String& moduleID, OwnedArray<LibraryModule>& modules) const
  103. {
  104. ScopedPointer<LibraryModule> m (loadModule (moduleID));
  105. if (m != nullptr)
  106. {
  107. var depsArray (m->moduleInfo ["dependencies"]);
  108. const Array<var>* const deps = depsArray.getArray();
  109. for (int i = 0; i < deps->size(); ++i)
  110. {
  111. const var& d = deps->getReference(i);
  112. String uid (d ["id"].toString());
  113. String version (d ["version"].toString());
  114. //xxx to do - also need to find version conflicts
  115. jassertfalse
  116. }
  117. }
  118. }
  119. //==============================================================================
  120. LibraryModule::LibraryModule (const File& file)
  121. : moduleInfo (JSON::parse (file)),
  122. moduleFile (file),
  123. moduleFolder (file.getParentDirectory())
  124. {
  125. jassert (isValid());
  126. }
  127. String LibraryModule::getID() const { return moduleInfo ["id"].toString(); };
  128. bool LibraryModule::isValid() const { return getID().isNotEmpty(); }
  129. bool LibraryModule::isPluginClient() const { return getID() == "juce_audio_plugin_client"; }
  130. bool LibraryModule::isAUPluginHost (const Project& project) const { return getID() == "juce_audio_processors" && project.isConfigFlagEnabled ("JUCE_PLUGINHOST_AU"); }
  131. bool LibraryModule::isVSTPluginHost (const Project& project) const { return getID() == "juce_audio_processors" && project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST"); }
  132. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  133. {
  134. File header (getInclude (getModuleTargetFolder (projectSaver)));
  135. StringArray paths, guards;
  136. createMultipleIncludes (projectSaver.getProject(), getPathToModuleFile (projectSaver, header),
  137. paths, guards);
  138. ProjectSaver::writeGuardedInclude (out, paths, guards);
  139. }
  140. void LibraryModule::prepareExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  141. {
  142. Project& project = exporter.getProject();
  143. File localFolder (getModuleTargetFolder (projectSaver));
  144. if (localFolder != moduleFolder && ! localFolder.isDirectory())
  145. moduleFolder.copyDirectoryTo (localFolder);
  146. {
  147. Array<File> compiled;
  148. findAndAddCompiledCode (exporter, projectSaver, localFolder, compiled);
  149. if (project.shouldShowAllModuleFilesInProject (getID()).getValue())
  150. addBrowsableCode (exporter, compiled, localFolder);
  151. }
  152. if (isVSTPluginHost (project))
  153. VSTHelpers::addVSTFolderToPath (exporter, exporter.extraSearchPaths);
  154. if (isAUPluginHost (project))
  155. exporter.xcodeFrameworks.addTokens ("AudioUnit CoreAudioKit", false);
  156. if (isPluginClient())
  157. {
  158. if (shouldBuildVST (project).getValue()) VSTHelpers::prepareExporter (exporter, projectSaver);
  159. if (shouldBuildRTAS (project).getValue()) RTASHelpers::prepareExporter (exporter, projectSaver, localFolder);
  160. if (shouldBuildAU (project).getValue()) AUHelpers::prepareExporter (exporter, projectSaver);
  161. }
  162. }
  163. void LibraryModule::createPropertyEditors (const ProjectExporter& exporter, Array <PropertyComponent*>& props) const
  164. {
  165. if (isVSTPluginHost (exporter.getProject()))
  166. VSTHelpers::createVSTPathEditor (exporter, props);
  167. if (isPluginClient())
  168. {
  169. if (shouldBuildVST (exporter.getProject()).getValue()) VSTHelpers::createPropertyEditors (exporter, props);
  170. if (shouldBuildRTAS (exporter.getProject()).getValue()) RTASHelpers::createPropertyEditors (exporter, props);
  171. }
  172. }
  173. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  174. {
  175. const File header (getInclude (moduleFolder));
  176. jassert (header.exists());
  177. StringArray lines;
  178. header.readLines (lines);
  179. for (int i = 0; i < lines.size(); ++i)
  180. {
  181. String line (lines[i].trim());
  182. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  183. {
  184. ScopedPointer <Project::ConfigFlag> config (new Project::ConfigFlag());
  185. config->sourceModuleID = getID();
  186. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  187. if (config->symbol.length() > 2)
  188. {
  189. ++i;
  190. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  191. {
  192. if (lines[i].trim().isNotEmpty())
  193. config->description = config->description.trim() + " " + lines[i].trim();
  194. ++i;
  195. }
  196. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  197. config->value.referTo (project.getConfigFlag (config->symbol));
  198. flags.add (config.release());
  199. }
  200. }
  201. }
  202. }
  203. File LibraryModule::getModuleTargetFolder (ProjectSaver& projectSaver) const
  204. {
  205. if (projectSaver.getProject().shouldCopyModuleFilesLocally (getID()).getValue())
  206. return projectSaver.getGeneratedCodeFolder().getChildFile (getID());
  207. return moduleFolder;
  208. }
  209. File LibraryModule::getInclude (const File& folder) const
  210. {
  211. return folder.getChildFile (moduleInfo ["include"]);
  212. }
  213. String LibraryModule::getPathToModuleFile (ProjectSaver& projectSaver, const File& file) const
  214. {
  215. return file.getRelativePathFrom (getModuleTargetFolder (projectSaver).getParentDirectory().getParentDirectory());
  216. }
  217. bool LibraryModule::fileTargetMatches (ProjectExporter& exporter, const String& target)
  218. {
  219. if (target.startsWithChar ('!'))
  220. return ! fileTargetMatches (exporter, target.substring (1).trim());
  221. if (target == "xcode") return exporter.isXcode();
  222. if (target == "msvc") return exporter.isVisualStudio();
  223. if (target == "linux") return exporter.isLinux();
  224. return true;
  225. }
  226. void LibraryModule::findWildcardMatches (const File& localModuleFolder, const String& wildcardPath, Array<File>& result) const
  227. {
  228. String path (wildcardPath.upToLastOccurrenceOf ("/", false, false));
  229. String wildCard (wildcardPath.fromLastOccurrenceOf ("/", false, false));
  230. Array<File> tempList;
  231. FileSorter sorter;
  232. DirectoryIterator iter (localModuleFolder.getChildFile (path), false, wildCard);
  233. while (iter.next())
  234. tempList.addSorted (sorter, iter.getFile());
  235. result.addArray (tempList);
  236. }
  237. void LibraryModule::addFileWithGroups (Project::Item& group, const File& file, const String& path) const
  238. {
  239. const int slash = path.indexOfChar ('/');
  240. if (slash >= 0)
  241. {
  242. const String topLevelGroup (path.substring (0, slash));
  243. const String remainingPath (path.substring (slash + 1));
  244. Project::Item newGroup (group.getOrCreateSubGroup (topLevelGroup));
  245. addFileWithGroups (newGroup, file, remainingPath);
  246. }
  247. else
  248. {
  249. if (! group.findItemForFile (file).isValid())
  250. group.addFileUnchecked (file, -1, false);
  251. }
  252. }
  253. void LibraryModule::findAndAddCompiledCode (ProjectExporter& exporter, ProjectSaver& projectSaver,
  254. const File& localModuleFolder, Array<File>& result) const
  255. {
  256. const var compileArray (moduleInfo ["compile"]); // careful to keep this alive while the array is in use!
  257. const Array<var>* const files = compileArray.getArray();
  258. if (files != nullptr)
  259. {
  260. for (int i = 0; i < files->size(); ++i)
  261. {
  262. const var& file = files->getReference(i);
  263. const String filename (file ["file"].toString());
  264. if (filename.isNotEmpty()
  265. && fileTargetMatches (exporter, file ["target"].toString()))
  266. {
  267. const File compiledFile (localModuleFolder.getChildFile (filename));
  268. result.add (compiledFile);
  269. Project::Item item (projectSaver.addFileToGeneratedGroup (compiledFile));
  270. if (file ["warnings"].toString().equalsIgnoreCase ("disabled"))
  271. item.getShouldInhibitWarningsValue() = true;
  272. if (file ["stdcall"])
  273. item.getShouldUseStdCallValue() = true;
  274. }
  275. }
  276. }
  277. }
  278. void LibraryModule::addBrowsableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  279. {
  280. if (sourceFiles.size() == 0)
  281. {
  282. const var filesArray (moduleInfo ["browse"]);
  283. const Array<var>* const files = filesArray.getArray();
  284. for (int i = 0; i < files->size(); ++i)
  285. findWildcardMatches (localModuleFolder, files->getReference(i), sourceFiles);
  286. }
  287. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID()));
  288. int i;
  289. for (i = 0; i < sourceFiles.size(); ++i)
  290. addFileWithGroups (sourceGroup, sourceFiles.getReference(i),
  291. sourceFiles.getReference(i).getRelativePathFrom (localModuleFolder));
  292. sourceGroup.addFile (localModuleFolder.getChildFile (moduleFile.getRelativePathFrom (moduleFolder)), -1, false);
  293. sourceGroup.addFile (getInclude (localModuleFolder), -1, false);
  294. for (i = 0; i < compiled.size(); ++i)
  295. addFileWithGroups (sourceGroup, compiled.getReference(i),
  296. compiled.getReference(i).getRelativePathFrom (localModuleFolder));
  297. exporter.getModulesGroup().getNode().addChild (sourceGroup.getNode().createCopy(), -1, nullptr);
  298. }
  299. void LibraryModule::writeSourceWrapper (OutputStream& out, Project& project, const String& pathFromJuceFolder)
  300. {
  301. const String appConfigFileName (project.getAppConfigFilename());
  302. ProjectSaver::writeAutoGenWarningComment (out);
  303. out << " This file pulls in a module's source code, and builds it using the settings" << newLine
  304. << " defined in " << appConfigFileName << "." << newLine
  305. << newLine
  306. << "*/"
  307. << newLine
  308. << newLine
  309. << "#define JUCE_WRAPPED_FILE 1" << newLine
  310. << newLine
  311. << CodeHelpers::createIncludeStatement (appConfigFileName) << newLine;
  312. writeInclude (project, out, pathFromJuceFolder);
  313. }
  314. void LibraryModule::createMultipleIncludes (Project& project, const String& pathFromLibraryFolder,
  315. StringArray& paths, StringArray& guards)
  316. {
  317. for (int i = project.getNumExporters(); --i >= 0;)
  318. {
  319. ScopedPointer <ProjectExporter> exporter (project.createExporter (i));
  320. if (exporter != nullptr)
  321. {
  322. paths.add (exporter->getIncludePathForFileInJuceFolder (pathFromLibraryFolder, project.getAppIncludeFile()));
  323. guards.add ("defined (" + exporter->getExporterIdentifierMacro() + ")");
  324. }
  325. }
  326. }
  327. void LibraryModule::writeInclude (Project& project, OutputStream& out, const String& pathFromJuceFolder)
  328. {
  329. StringArray paths, guards;
  330. createMultipleIncludes (project, pathFromJuceFolder, paths, guards);
  331. StringArray uniquePaths (paths);
  332. uniquePaths.removeDuplicates (false);
  333. if (uniquePaths.size() == 1)
  334. {
  335. out << "#include " << paths[0] << newLine;
  336. }
  337. else
  338. {
  339. int i = paths.size();
  340. for (; --i >= 0;)
  341. {
  342. for (int j = i; --j >= 0;)
  343. {
  344. if (paths[i] == paths[j] && guards[i] == guards[j])
  345. {
  346. paths.remove (i);
  347. guards.remove (i);
  348. }
  349. }
  350. }
  351. for (i = 0; i < paths.size(); ++i)
  352. {
  353. out << (i == 0 ? "#if " : "#elif ") << guards[i] << newLine
  354. << " #include " << paths[i] << newLine;
  355. }
  356. out << "#endif" << newLine;
  357. }
  358. }