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.

1009 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../Application/jucer_Headers.h"
  20. #include "jucer_Module.h"
  21. #include "../ProjectSaving/jucer_ProjectSaver.h"
  22. #include "../ProjectSaving/jucer_ProjectExport_XCode.h"
  23. //==============================================================================
  24. static String trimCommentCharsFromStartOfLine (const String& line)
  25. {
  26. return line.trimStart().trimCharactersAtStart ("*/").trimStart();
  27. }
  28. static var parseModuleDesc (const StringArray& lines)
  29. {
  30. DynamicObject* o = new DynamicObject();
  31. var result (o);
  32. for (int i = 0; i < lines.size(); ++i)
  33. {
  34. String line = trimCommentCharsFromStartOfLine (lines[i]);
  35. int colon = line.indexOfChar (':');
  36. if (colon >= 0)
  37. {
  38. String key = line.substring (0, colon).trim();
  39. String value = line.substring (colon + 1).trim();
  40. o->setProperty (key, value);
  41. }
  42. }
  43. return result;
  44. }
  45. static var parseModuleDesc (const File& header)
  46. {
  47. StringArray lines;
  48. header.readLines (lines);
  49. for (int i = 0; i < lines.size(); ++i)
  50. {
  51. if (trimCommentCharsFromStartOfLine (lines[i]).startsWith ("BEGIN_JUCE_MODULE_DECLARATION"))
  52. {
  53. StringArray desc;
  54. for (int j = i + 1; j < lines.size(); ++j)
  55. {
  56. if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_MODULE_DECLARATION"))
  57. return parseModuleDesc (desc);
  58. desc.add (lines[j]);
  59. }
  60. break;
  61. }
  62. }
  63. return {};
  64. }
  65. ModuleDescription::ModuleDescription (const File& folder)
  66. : moduleFolder (folder),
  67. moduleInfo (parseModuleDesc (getHeader()))
  68. {
  69. }
  70. File ModuleDescription::getHeader() const
  71. {
  72. if (moduleFolder != File())
  73. {
  74. const char* extensions[] = { ".h", ".hpp", ".hxx" };
  75. for (auto e : extensions)
  76. {
  77. File header (moduleFolder.getChildFile (moduleFolder.getFileName() + e));
  78. if (header.existsAsFile())
  79. return header;
  80. }
  81. }
  82. return {};
  83. }
  84. StringArray ModuleDescription::getDependencies() const
  85. {
  86. auto deps = StringArray::fromTokens (moduleInfo ["dependencies"].toString(), " \t;,", "\"'");
  87. deps.trim();
  88. deps.removeEmptyStrings();
  89. return deps;
  90. }
  91. //==============================================================================
  92. ModuleList::ModuleList()
  93. {
  94. }
  95. ModuleList::ModuleList (const ModuleList& other)
  96. {
  97. operator= (other);
  98. }
  99. ModuleList& ModuleList::operator= (const ModuleList& other)
  100. {
  101. modules.clear();
  102. modules.addCopiesOf (other.modules);
  103. return *this;
  104. }
  105. const ModuleDescription* ModuleList::getModuleWithID (const String& moduleID) const
  106. {
  107. for (auto* m : modules)
  108. if (m->getID() == moduleID)
  109. return m;
  110. return nullptr;
  111. }
  112. struct ModuleSorter
  113. {
  114. static int compareElements (const ModuleDescription* m1, const ModuleDescription* m2)
  115. {
  116. return m1->getID().compareIgnoreCase (m2->getID());
  117. }
  118. };
  119. void ModuleList::sort()
  120. {
  121. ModuleSorter sorter;
  122. modules.sort (sorter);
  123. }
  124. StringArray ModuleList::getIDs() const
  125. {
  126. StringArray results;
  127. for (auto* m : modules)
  128. results.add (m->getID());
  129. results.sort (true);
  130. return results;
  131. }
  132. Result ModuleList::tryToAddModuleFromFolder (const File& path)
  133. {
  134. ModuleDescription m (path);
  135. if (m.isValid())
  136. {
  137. modules.add (new ModuleDescription (m));
  138. return Result::ok();
  139. }
  140. return Result::fail (path.getFullPathName() + " is not a valid module");
  141. }
  142. Result ModuleList::addAllModulesInFolder (const File& path)
  143. {
  144. if (! tryToAddModuleFromFolder (path))
  145. {
  146. const int subfolders = 2;
  147. return addAllModulesInSubfoldersRecursively (path, subfolders);
  148. }
  149. return Result::ok();
  150. }
  151. Result ModuleList::addAllModulesInSubfoldersRecursively (const File& path, int depth)
  152. {
  153. if (depth > 0)
  154. {
  155. for (DirectoryIterator iter (path, false, "*", File::findDirectories); iter.next();)
  156. {
  157. auto childPath = iter.getFile().getLinkedTarget();
  158. if (! tryToAddModuleFromFolder (childPath))
  159. addAllModulesInSubfoldersRecursively (childPath, depth - 1);
  160. }
  161. }
  162. return Result::ok();
  163. }
  164. static Array<File> getAllPossibleModulePathsFromExporters (Project& project)
  165. {
  166. StringArray paths;
  167. for (Project::ExporterIterator exporter (project); exporter.next();)
  168. {
  169. auto& modules = project.getModules();
  170. auto n = modules.getNumModules();
  171. for (int i = 0; i < n; ++i)
  172. {
  173. auto id = modules.getModuleID (i);
  174. if (modules.shouldUseGlobalPath (id))
  175. continue;
  176. const auto path = exporter->getPathForModuleString (id);
  177. if (path.isNotEmpty())
  178. paths.addIfNotAlreadyThere (path);
  179. }
  180. String oldPath (exporter->getLegacyModulePath());
  181. if (oldPath.isNotEmpty())
  182. paths.addIfNotAlreadyThere (oldPath);
  183. }
  184. Array<File> files;
  185. for (auto& path : paths)
  186. {
  187. auto f = project.resolveFilename (path);
  188. if (f.isDirectory())
  189. {
  190. files.addIfNotAlreadyThere (f);
  191. if (f.getChildFile ("modules").isDirectory())
  192. files.addIfNotAlreadyThere (f.getChildFile ("modules"));
  193. }
  194. }
  195. return files;
  196. }
  197. Result ModuleList::scanProjectExporterModulePaths (Project& project)
  198. {
  199. modules.clear();
  200. Result result (Result::ok());
  201. for (auto& m : getAllPossibleModulePathsFromExporters (project))
  202. {
  203. result = addAllModulesInFolder (m);
  204. if (result.failed())
  205. break;
  206. }
  207. sort();
  208. return result;
  209. }
  210. void ModuleList::scanGlobalJuceModulePath()
  211. {
  212. modules.clear();
  213. auto& settings = getAppSettings();
  214. auto path = settings.getStoredPath (Ids::defaultJuceModulePath).toString();
  215. if (path.isNotEmpty())
  216. addAllModulesInFolder ({ path });
  217. sort();
  218. }
  219. void ModuleList::scanGlobalUserModulePath()
  220. {
  221. modules.clear();
  222. auto paths = StringArray::fromTokens (getAppSettings().getStoredPath (Ids::defaultUserModulePath).toString(), ";", {});
  223. for (auto p : paths)
  224. {
  225. auto f = File::createFileWithoutCheckingPath (p.trim());
  226. if (f.exists())
  227. addAllModulesInFolder (f);
  228. }
  229. sort();
  230. }
  231. //==============================================================================
  232. LibraryModule::LibraryModule (const ModuleDescription& d)
  233. : moduleInfo (d)
  234. {
  235. }
  236. //==============================================================================
  237. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  238. {
  239. Project& project = projectSaver.project;
  240. EnabledModuleList& modules = project.getModules();
  241. const String id (getID());
  242. if (modules.shouldCopyModuleFilesLocally (id).getValue())
  243. {
  244. const File juceModuleFolder (moduleInfo.getFolder());
  245. const File localModuleFolder (project.getLocalModuleFolder (id));
  246. localModuleFolder.createDirectory();
  247. projectSaver.copyFolder (juceModuleFolder, localModuleFolder);
  248. }
  249. out << "#include <" << moduleInfo.moduleFolder.getFileName() << "/"
  250. << moduleInfo.getHeader().getFileName()
  251. << ">" << newLine;
  252. }
  253. //==============================================================================
  254. static void parseAndAddLibs (StringArray& libList, const String& libs)
  255. {
  256. libList.addTokens (libs, ", ", {});
  257. libList.trim();
  258. libList.sort (false);
  259. libList.removeDuplicates (false);
  260. }
  261. void LibraryModule::addSettingsForModuleToExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  262. {
  263. auto& project = exporter.getProject();
  264. const auto moduleRelativePath = exporter.getModuleFolderRelativeToProject (getID());
  265. exporter.addToExtraSearchPaths (moduleRelativePath.getParentDirectory());
  266. String libDirPlatform;
  267. if (exporter.isLinux())
  268. libDirPlatform = "Linux";
  269. else if (exporter.isCodeBlocks() && exporter.isWindows())
  270. libDirPlatform = "MinGW";
  271. else
  272. libDirPlatform = exporter.getTargetFolder().getFileName();
  273. const auto libSubdirPath = String (moduleRelativePath.toUnixStyle() + "/libs/") + libDirPlatform;
  274. const auto moduleLibDir = File (project.getProjectFolder().getFullPathName() + "/" + libSubdirPath);
  275. if (moduleLibDir.exists())
  276. exporter.addToModuleLibPaths (RelativePath (libSubdirPath, moduleRelativePath.getRoot()));
  277. const auto extraInternalSearchPaths = moduleInfo.getExtraSearchPaths().trim();
  278. if (extraInternalSearchPaths.isNotEmpty())
  279. {
  280. StringArray paths;
  281. paths.addTokens (extraInternalSearchPaths, true);
  282. for (int i = 0; i < paths.size(); ++i)
  283. exporter.addToExtraSearchPaths (moduleRelativePath.getChildFile (paths.getReference(i)));
  284. }
  285. {
  286. const String extraDefs (moduleInfo.getPreprocessorDefs().trim());
  287. if (extraDefs.isNotEmpty())
  288. exporter.getExporterPreprocessorDefs() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  289. }
  290. {
  291. Array<File> compiled;
  292. auto& modules = project.getModules();
  293. auto id = getID();
  294. const File localModuleFolder = modules.shouldCopyModuleFilesLocally (id).getValue()
  295. ? project.getLocalModuleFolder (id)
  296. : moduleInfo.getFolder();
  297. findAndAddCompiledUnits (exporter, &projectSaver, compiled);
  298. if (modules.shouldShowAllModuleFilesInProject (id).getValue())
  299. addBrowseableCode (exporter, compiled, localModuleFolder);
  300. }
  301. if (exporter.isXcode())
  302. {
  303. auto& xcodeExporter = dynamic_cast<XCodeProjectExporter&> (exporter);
  304. if (project.isAUPluginHost())
  305. xcodeExporter.xcodeFrameworks.addTokens (xcodeExporter.isOSX() ? "AudioUnit CoreAudioKit" : "CoreAudioKit", false);
  306. const String frameworks (moduleInfo.moduleInfo [xcodeExporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString());
  307. xcodeExporter.xcodeFrameworks.addTokens (frameworks, ", ", {});
  308. parseAndAddLibs (xcodeExporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  309. }
  310. else if (exporter.isLinux())
  311. {
  312. parseAndAddLibs (exporter.linuxLibs, moduleInfo.moduleInfo ["linuxLibs"].toString());
  313. parseAndAddLibs (exporter.linuxPackages, moduleInfo.moduleInfo ["linuxPackages"].toString());
  314. }
  315. else if (exporter.isWindows())
  316. {
  317. if (exporter.isCodeBlocks())
  318. parseAndAddLibs (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  319. else
  320. parseAndAddLibs (exporter.windowsLibs, moduleInfo.moduleInfo ["windowsLibs"].toString());
  321. }
  322. else if (exporter.isAndroid())
  323. {
  324. parseAndAddLibs (exporter.androidLibs, moduleInfo.moduleInfo ["androidLibs"].toString());
  325. }
  326. }
  327. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  328. {
  329. const File header (moduleInfo.getHeader());
  330. jassert (header.exists());
  331. StringArray lines;
  332. header.readLines (lines);
  333. for (int i = 0; i < lines.size(); ++i)
  334. {
  335. String line (lines[i].trim());
  336. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  337. {
  338. ScopedPointer<Project::ConfigFlag> config (new Project::ConfigFlag());
  339. config->sourceModuleID = getID();
  340. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  341. if (config->symbol.length() > 2)
  342. {
  343. ++i;
  344. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  345. {
  346. if (lines[i].trim().isNotEmpty())
  347. config->description = config->description.trim() + " " + lines[i].trim();
  348. ++i;
  349. }
  350. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  351. config->value.referTo (project.getConfigFlag (config->symbol));
  352. flags.add (config.release());
  353. }
  354. }
  355. }
  356. }
  357. //==============================================================================
  358. struct FileSorter
  359. {
  360. static int compareElements (const File& f1, const File& f2)
  361. {
  362. return f1.getFileName().compareNatural (f2.getFileName());
  363. }
  364. };
  365. bool LibraryModule::CompileUnit::hasSuffix (const File& f, const char* suffix)
  366. {
  367. auto fileWithoutSuffix = f.getFileNameWithoutExtension() + ".";
  368. return fileWithoutSuffix.containsIgnoreCase (suffix + String ("."))
  369. || fileWithoutSuffix.containsIgnoreCase (suffix + String ("_"));
  370. }
  371. void LibraryModule::CompileUnit::writeInclude (MemoryOutputStream&) const
  372. {
  373. }
  374. bool LibraryModule::CompileUnit::isNeededForExporter (ProjectExporter& exporter) const
  375. {
  376. if ((hasSuffix (file, "_OSX") && ! exporter.isOSX())
  377. || (hasSuffix (file, "_iOS") && ! exporter.isiOS())
  378. || (hasSuffix (file, "_Windows") && ! exporter.isWindows())
  379. || (hasSuffix (file, "_Linux") && ! exporter.isLinux())
  380. || (hasSuffix (file, "_Android") && ! exporter.isAndroid()))
  381. return false;
  382. auto targetType = Project::getTargetTypeFromFilePath (file, false);
  383. if (targetType != ProjectType::Target::unspecified && ! exporter.shouldBuildTargetType (targetType))
  384. return false;
  385. return exporter.usesMMFiles() ? isCompiledForObjC
  386. : isCompiledForNonObjC;
  387. }
  388. String LibraryModule::CompileUnit::getFilenameForProxyFile() const
  389. {
  390. return "include_" + file.getFileName();
  391. }
  392. Array<LibraryModule::CompileUnit> LibraryModule::getAllCompileUnits (ProjectType::Target::Type forTarget) const
  393. {
  394. Array<File> files;
  395. getFolder().findChildFiles (files, File::findFiles, false);
  396. FileSorter sorter;
  397. files.sort (sorter);
  398. Array<LibraryModule::CompileUnit> units;
  399. for (auto& file : files)
  400. {
  401. if (file.getFileName().startsWithIgnoreCase (getID())
  402. && file.hasFileExtension (sourceFileExtensions))
  403. {
  404. if (forTarget == ProjectType::Target::unspecified
  405. || forTarget == Project::getTargetTypeFromFilePath (file, true))
  406. {
  407. CompileUnit cu;
  408. cu.file = file;
  409. units.add (cu);
  410. }
  411. }
  412. }
  413. for (auto& cu : units)
  414. {
  415. cu.isCompiledForObjC = true;
  416. cu.isCompiledForNonObjC = ! cu.file.hasFileExtension ("mm;m");
  417. if (cu.isCompiledForNonObjC)
  418. if (files.contains (cu.file.withFileExtension ("mm")))
  419. cu.isCompiledForObjC = false;
  420. jassert (cu.isCompiledForObjC || cu.isCompiledForNonObjC);
  421. }
  422. return units;
  423. }
  424. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter,
  425. ProjectSaver* projectSaver,
  426. Array<File>& result,
  427. ProjectType::Target::Type forTarget) const
  428. {
  429. for (auto& cu : getAllCompileUnits (forTarget))
  430. {
  431. if (cu.isNeededForExporter (exporter))
  432. {
  433. auto localFile = exporter.getProject().getGeneratedCodeFolder()
  434. .getChildFile (cu.getFilenameForProxyFile());
  435. result.add (localFile);
  436. if (projectSaver != nullptr)
  437. projectSaver->addFileToGeneratedGroup (localFile);
  438. }
  439. }
  440. }
  441. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  442. {
  443. const int slash = path.indexOfChar (File::separator);
  444. if (slash >= 0)
  445. {
  446. const String topLevelGroup (path.substring (0, slash));
  447. const String remainingPath (path.substring (slash + 1));
  448. Project::Item newGroup (group.getOrCreateSubGroup (topLevelGroup));
  449. addFileWithGroups (newGroup, file, remainingPath);
  450. }
  451. else
  452. {
  453. if (! group.containsChildForFile (file))
  454. group.addRelativeFile (file, -1, false);
  455. }
  456. }
  457. void LibraryModule::findBrowseableFiles (const File& folder, Array<File>& filesFound) const
  458. {
  459. Array<File> tempList;
  460. FileSorter sorter;
  461. DirectoryIterator iter (folder, true, "*", File::findFiles);
  462. bool isHiddenFile;
  463. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  464. if (! isHiddenFile && iter.getFile().hasFileExtension (browseableFileExtensions))
  465. tempList.addSorted (sorter, iter.getFile());
  466. filesFound.addArray (tempList);
  467. }
  468. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  469. {
  470. if (sourceFiles.isEmpty())
  471. findBrowseableFiles (localModuleFolder, sourceFiles);
  472. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID(), false));
  473. const RelativePath moduleFromProject (exporter.getModuleFolderRelativeToProject (getID()));
  474. auto moduleHeader = moduleInfo.getHeader();
  475. for (auto& sourceFile : sourceFiles)
  476. {
  477. auto pathWithinModule = FileHelpers::getRelativePathFrom (sourceFile, localModuleFolder);
  478. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  479. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  480. if ((exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFile)) && sourceFile != moduleHeader)
  481. addFileWithGroups (sourceGroup,
  482. moduleFromProject.getChildFile (pathWithinModule),
  483. pathWithinModule);
  484. }
  485. sourceGroup.sortAlphabetically (true, true);
  486. sourceGroup.addFileAtIndex (moduleHeader, -1, false);
  487. exporter.getModulesGroup().state.addChild (sourceGroup.state.createCopy(), -1, nullptr);
  488. }
  489. //==============================================================================
  490. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  491. : project (p), state (s)
  492. {
  493. }
  494. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  495. {
  496. return ModuleDescription (getModuleFolder (moduleID));
  497. }
  498. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  499. {
  500. return state.getChildWithProperty (Ids::ID, moduleID).isValid();
  501. }
  502. bool EnabledModuleList::isAudioPluginModuleMissing() const
  503. {
  504. return project.getProjectType().isAudioPlugin()
  505. && ! isModuleEnabled ("juce_audio_plugin_client");
  506. }
  507. bool EnabledModuleList::shouldUseGlobalPath (const String& moduleID) const
  508. {
  509. return static_cast<bool> (state.getChildWithProperty (Ids::ID, moduleID)
  510. .getProperty (Ids::useGlobalPath));
  511. }
  512. Value EnabledModuleList::getShouldUseGlobalPathValue (const String& moduleID) const
  513. {
  514. return state.getChildWithProperty (Ids::ID, moduleID)
  515. .getPropertyAsValue (Ids::useGlobalPath, getUndoManager());
  516. }
  517. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  518. {
  519. return state.getChildWithProperty (Ids::ID, moduleID)
  520. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  521. }
  522. File EnabledModuleList::getModuleFolderFromPathIfItExists (const String& path, const String& moduleID, const Project& project)
  523. {
  524. if (path.isNotEmpty())
  525. {
  526. auto moduleFolder = project.resolveFilename (path);
  527. if (moduleFolder.exists())
  528. {
  529. if (ModuleDescription (moduleFolder).isValid())
  530. return moduleFolder;
  531. auto f = moduleFolder.getChildFile (moduleID);
  532. if (ModuleDescription (f).isValid())
  533. return f;
  534. }
  535. }
  536. return {};
  537. }
  538. File EnabledModuleList::findUserModuleFolder (const String& possiblePaths, const String& moduleID)
  539. {
  540. auto paths = StringArray::fromTokens (possiblePaths, ";", {});
  541. for (auto p : paths)
  542. {
  543. auto f = File::createFileWithoutCheckingPath (p.trim());
  544. if (f.exists())
  545. {
  546. auto moduleFolder = getModuleFolderFromPathIfItExists (f.getFullPathName(), moduleID, project);
  547. if (moduleFolder != File())
  548. return moduleFolder;
  549. }
  550. }
  551. return {};
  552. }
  553. File EnabledModuleList::getModuleFolder (const String& moduleID)
  554. {
  555. if (shouldUseGlobalPath (moduleID))
  556. {
  557. if (isJuceModule (moduleID))
  558. return getModuleFolderFromPathIfItExists (getAppSettings().getStoredPath (Ids::defaultJuceModulePath).toString(), moduleID, project);
  559. return findUserModuleFolder (moduleID, getAppSettings().getStoredPath (Ids::defaultUserModulePath).toString());
  560. }
  561. auto paths = getAllPossibleModulePathsFromExporters (project);
  562. for (auto p : paths)
  563. {
  564. auto f = getModuleFolderFromPathIfItExists (p.getFullPathName(), moduleID, project);
  565. if (f != File())
  566. return f;
  567. }
  568. return {};
  569. }
  570. struct ModuleTreeSorter
  571. {
  572. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  573. {
  574. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  575. }
  576. };
  577. void EnabledModuleList::sortAlphabetically()
  578. {
  579. ModuleTreeSorter sorter;
  580. state.sort (sorter, getUndoManager(), false);
  581. }
  582. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  583. {
  584. return state.getChildWithProperty (Ids::ID, moduleID)
  585. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  586. }
  587. void EnabledModuleList::addModule (const File& moduleFolder, bool copyLocally, bool useGlobalPath)
  588. {
  589. ModuleDescription info (moduleFolder);
  590. if (info.isValid())
  591. {
  592. const String moduleID (info.getID());
  593. if (! isModuleEnabled (moduleID))
  594. {
  595. ValueTree module (Ids::MODULE);
  596. module.setProperty (Ids::ID, moduleID, nullptr);
  597. state.addChild (module, -1, getUndoManager());
  598. sortAlphabetically();
  599. shouldShowAllModuleFilesInProject (moduleID) = true;
  600. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  601. getShouldUseGlobalPathValue (moduleID) = useGlobalPath;
  602. RelativePath path (moduleFolder.getParentDirectory(),
  603. project.getProjectFolder(), RelativePath::projectFolder);
  604. for (Project::ExporterIterator exporter (project); exporter.next();)
  605. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  606. }
  607. }
  608. }
  609. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  610. {
  611. for (int i = state.getNumChildren(); --i >= 0;)
  612. if (state.getChild(i) [Ids::ID] == moduleID)
  613. state.removeChild (i, getUndoManager());
  614. for (Project::ExporterIterator exporter (project); exporter.next();)
  615. exporter->removePathForModule (moduleID);
  616. }
  617. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  618. {
  619. for (int i = 0; i < getNumModules(); ++i)
  620. modules.add (new LibraryModule (getModuleInfo (getModuleID (i))));
  621. }
  622. StringArray EnabledModuleList::getAllModules() const
  623. {
  624. StringArray moduleIDs;
  625. for (int i = 0; i < getNumModules(); ++i)
  626. moduleIDs.add (getModuleID (i));
  627. return moduleIDs;
  628. }
  629. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  630. {
  631. ModuleDescription info (project.getModules().getModuleInfo (moduleID));
  632. for (auto uid : info.getDependencies())
  633. {
  634. if (! dependencies.contains (uid, true))
  635. {
  636. dependencies.add (uid);
  637. getDependencies (project, uid, dependencies);
  638. }
  639. }
  640. }
  641. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  642. {
  643. StringArray dependencies, extraDepsNeeded;
  644. getDependencies (project, moduleID, dependencies);
  645. for (auto dep : dependencies)
  646. if (dep != moduleID && ! isModuleEnabled (dep))
  647. extraDepsNeeded.add (dep);
  648. return extraDepsNeeded;
  649. }
  650. bool EnabledModuleList::doesModuleHaveHigherCppStandardThanProject (const String& moduleID)
  651. {
  652. auto projectCppStandard = project.getCppStandardValue().toString();
  653. if (projectCppStandard == "latest")
  654. return false;
  655. auto moduleCppStandard = getModuleInfo (moduleID).getMinimumCppStandard();
  656. return (moduleCppStandard.getIntValue() > projectCppStandard.getIntValue());
  657. }
  658. bool EnabledModuleList::areMostModulesUsingGlobalPath() const
  659. {
  660. auto numYes = 0, numNo = 0;
  661. for (auto i = getNumModules(); --i >= 0;)
  662. {
  663. if (shouldUseGlobalPath (getModuleID (i)))
  664. ++numYes;
  665. else
  666. ++numNo;
  667. }
  668. return numYes > numNo;
  669. }
  670. bool EnabledModuleList::areMostModulesCopiedLocally() const
  671. {
  672. auto numYes = 0, numNo = 0;
  673. for (auto i = getNumModules(); --i >= 0;)
  674. {
  675. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  676. ++numYes;
  677. else
  678. ++numNo;
  679. }
  680. return numYes > numNo;
  681. }
  682. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  683. {
  684. for (int i = getNumModules(); --i >= 0;)
  685. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  686. }
  687. File EnabledModuleList::findGlobalModulesFolder()
  688. {
  689. auto& settings = getAppSettings();
  690. auto path = settings.getStoredPath (Ids::defaultJuceModulePath).toString();
  691. if (settings.isGlobalPathValid (File(), Ids::defaultJuceModulePath, path))
  692. return { path };
  693. return {};
  694. }
  695. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  696. {
  697. auto globalPath = findGlobalModulesFolder();
  698. if (globalPath != File())
  699. return globalPath;
  700. ModuleList available;
  701. available.scanProjectExporterModulePaths (project);
  702. for (int i = available.modules.size(); --i >= 0;)
  703. {
  704. File f (available.modules.getUnchecked(i)->getFolder());
  705. if (f.isDirectory())
  706. return f.getParentDirectory();
  707. }
  708. return File::getCurrentWorkingDirectory();
  709. }
  710. bool EnabledModuleList::isJuceModule (const String& moduleID)
  711. {
  712. static StringArray juceModuleIds =
  713. {
  714. "juce_audio_basics",
  715. "juce_audio_devices",
  716. "juce_audio_formats",
  717. "juce_audio_plugin_client",
  718. "juce_audio_processors",
  719. "juce_audio_utils",
  720. "juce_blocks_basics",
  721. "juce_box2d",
  722. "juce_core",
  723. "juce_cryptography",
  724. "juce_data_structures",
  725. "juce_dsp",
  726. "juce_events",
  727. "juce_graphics",
  728. "juce_gui_basics",
  729. "juce_gui_extra",
  730. "juce_opengl",
  731. "juce_osc",
  732. "juce_product_unlocking",
  733. "juce_video"
  734. };
  735. return juceModuleIds.contains (moduleID);
  736. }
  737. void EnabledModuleList::addModuleFromUserSelectedFile()
  738. {
  739. static File lastLocation (findDefaultModulesFolder (project));
  740. FileChooser fc ("Select a module to add...", lastLocation, String());
  741. if (fc.browseForDirectory())
  742. {
  743. lastLocation = fc.getResult();
  744. addModuleOfferingToCopy (lastLocation, true);
  745. }
  746. }
  747. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  748. {
  749. ModuleList list;
  750. list.scanGlobalJuceModulePath();
  751. if (auto* info = list.getModuleWithID (moduleID))
  752. {
  753. addModule (info->moduleFolder, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath());
  754. return;
  755. }
  756. list.scanGlobalUserModulePath();
  757. if (auto* info = list.getModuleWithID (moduleID))
  758. {
  759. addModule (info->moduleFolder, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath());
  760. return;
  761. }
  762. list.scanProjectExporterModulePaths (project);
  763. if (auto* info = list.getModuleWithID (moduleID))
  764. addModule (info->moduleFolder, areMostModulesCopiedLocally(), false);
  765. else
  766. addModuleFromUserSelectedFile();
  767. }
  768. void EnabledModuleList::addModuleOfferingToCopy (const File& f, bool isFromUserSpecifiedFolder)
  769. {
  770. ModuleDescription m (f);
  771. if (! m.isValid())
  772. {
  773. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  774. "Add Module", "This wasn't a valid module folder!");
  775. return;
  776. }
  777. if (isModuleEnabled (m.getID()))
  778. {
  779. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  780. "Add Module", "The project already contains this module!");
  781. return;
  782. }
  783. addModule (m.moduleFolder, areMostModulesCopiedLocally(), isFromUserSpecifiedFolder ? false
  784. : areMostModulesUsingGlobalPath());
  785. }
  786. bool isJuceFolder (const File& f)
  787. {
  788. return isJuceModulesFolder (f.getChildFile ("modules"));
  789. }
  790. bool isJuceModulesFolder (const File& f)
  791. {
  792. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  793. }