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.

974 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. #include "../Application/jucer_ProjucerAnalytics.h"
  24. //==============================================================================
  25. static var parseModuleDesc (const StringArray& lines)
  26. {
  27. DynamicObject* o = new DynamicObject();
  28. var result (o);
  29. for (auto line : lines)
  30. {
  31. line = trimCommentCharsFromStartOfLine (line);
  32. auto colon = line.indexOfChar (':');
  33. if (colon >= 0)
  34. {
  35. auto key = line.substring (0, colon).trim();
  36. auto value = line.substring (colon + 1).trim();
  37. o->setProperty (key, value);
  38. }
  39. }
  40. return result;
  41. }
  42. static var parseModuleDesc (const File& header)
  43. {
  44. StringArray lines;
  45. header.readLines (lines);
  46. for (int i = 0; i < lines.size(); ++i)
  47. {
  48. if (trimCommentCharsFromStartOfLine (lines[i]).startsWith ("BEGIN_JUCE_MODULE_DECLARATION"))
  49. {
  50. StringArray desc;
  51. for (int j = i + 1; j < lines.size(); ++j)
  52. {
  53. if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_MODULE_DECLARATION"))
  54. return parseModuleDesc (desc);
  55. desc.add (lines[j]);
  56. }
  57. break;
  58. }
  59. }
  60. return {};
  61. }
  62. ModuleDescription::ModuleDescription (const File& folder)
  63. : moduleFolder (folder),
  64. moduleInfo (parseModuleDesc (getHeader()))
  65. {
  66. }
  67. File ModuleDescription::getHeader() const
  68. {
  69. if (moduleFolder != File())
  70. {
  71. const char* extensions[] = { ".h", ".hpp", ".hxx" };
  72. for (auto e : extensions)
  73. {
  74. auto header = moduleFolder.getChildFile (moduleFolder.getFileName() + e);
  75. if (header.existsAsFile())
  76. return header;
  77. }
  78. }
  79. return {};
  80. }
  81. StringArray ModuleDescription::getDependencies() const
  82. {
  83. auto deps = StringArray::fromTokens (moduleInfo ["dependencies"].toString(), " \t;,", "\"'");
  84. deps.trim();
  85. deps.removeEmptyStrings();
  86. return deps;
  87. }
  88. //==============================================================================
  89. ModuleList::ModuleList()
  90. {
  91. }
  92. ModuleList::ModuleList (const ModuleList& other)
  93. {
  94. operator= (other);
  95. }
  96. ModuleList& ModuleList::operator= (const ModuleList& other)
  97. {
  98. modules.clear();
  99. modules.addCopiesOf (other.modules);
  100. return *this;
  101. }
  102. const ModuleDescription* ModuleList::getModuleWithID (const String& moduleID) const
  103. {
  104. for (auto* m : modules)
  105. if (m->getID() == moduleID)
  106. return m;
  107. return nullptr;
  108. }
  109. void ModuleList::sort()
  110. {
  111. std::sort (modules.begin(), modules.end(), [] (const ModuleDescription* m1, const ModuleDescription* m2)
  112. {
  113. return m1->getID().compareIgnoreCase (m2->getID()) < 0;
  114. });
  115. }
  116. StringArray ModuleList::getIDs() const
  117. {
  118. StringArray results;
  119. for (auto* m : modules)
  120. results.add (m->getID());
  121. results.sort (true);
  122. return results;
  123. }
  124. bool ModuleList::tryToAddModuleFromFolder (const File& path)
  125. {
  126. ModuleDescription m (path);
  127. if (m.isValid())
  128. {
  129. getPreviousModuleDirectories().addIfNotAlreadyThere (path.getParentDirectory());
  130. modules.add (new ModuleDescription (m));
  131. return true;
  132. }
  133. return false;
  134. }
  135. void ModuleList::addAllModulesInFolder (const File& path)
  136. {
  137. if (! tryToAddModuleFromFolder (path))
  138. {
  139. for (DirectoryIterator iter (path, false, "*", File::findDirectories); iter.next();)
  140. tryToAddModuleFromFolder (iter.getFile().getLinkedTarget());
  141. }
  142. }
  143. Array<File>& ModuleList::getPreviousModuleDirectories()
  144. {
  145. static Array<File> previous;
  146. return previous;
  147. }
  148. File ModuleList::tryToFindModulePathFromPrevious (const String& id)
  149. {
  150. for (auto& f : getPreviousModuleDirectories())
  151. {
  152. auto modulePath = f.getChildFile (id);
  153. if (ModuleDescription (modulePath).isValid())
  154. return modulePath;
  155. }
  156. return {};
  157. }
  158. //==============================================================================
  159. static File getModuleFolderFromPathIfItExists (const String& path, const String& moduleID)
  160. {
  161. if (path.isNotEmpty())
  162. {
  163. ModuleList list;
  164. list.addAllModulesInFolder (path);
  165. if (auto* desc = list.getModuleWithID (moduleID))
  166. return desc->getFolder();
  167. }
  168. return {};
  169. }
  170. static Array<File> getAllPossibleModulePathsFromExporters (Project& project, bool onlyThisOS)
  171. {
  172. StringArray paths;
  173. for (Project::ExporterIterator exporter (project); exporter.next();)
  174. {
  175. if (onlyThisOS && ! exporter->mayCompileOnCurrentOS())
  176. continue;
  177. auto& modules = project.getModules();
  178. auto n = modules.getNumModules();
  179. for (int i = 0; i < n; ++i)
  180. {
  181. auto id = modules.getModuleID (i);
  182. if (modules.shouldUseGlobalPath (id))
  183. continue;
  184. auto path = exporter->getPathForModuleString (id);
  185. if (path.isNotEmpty())
  186. paths.addIfNotAlreadyThere (path);
  187. }
  188. auto oldPath = exporter->getLegacyModulePath();
  189. if (oldPath.isNotEmpty())
  190. paths.addIfNotAlreadyThere (oldPath);
  191. }
  192. Array<File> files;
  193. for (auto& path : paths)
  194. {
  195. auto f = project.resolveFilename (path);
  196. if (f.isDirectory())
  197. {
  198. files.addIfNotAlreadyThere (f);
  199. if (f.getChildFile ("modules").isDirectory())
  200. files.addIfNotAlreadyThere (f.getChildFile ("modules"));
  201. }
  202. }
  203. return files;
  204. }
  205. void ModuleList::scanProjectExporterModulePaths (Project& project)
  206. {
  207. modules.clear();
  208. for (auto& m : getAllPossibleModulePathsFromExporters (project, false))
  209. addAllModulesInFolder (m);
  210. sort();
  211. }
  212. void ModuleList::scanGlobalJuceModulePath()
  213. {
  214. modules.clear();
  215. auto& settings = getAppSettings();
  216. auto path = settings.getStoredPath (Ids::defaultJuceModulePath).toString();
  217. if (path.isNotEmpty())
  218. addAllModulesInFolder ({ path });
  219. sort();
  220. }
  221. void ModuleList::scanGlobalUserModulePath()
  222. {
  223. modules.clear();
  224. auto paths = StringArray::fromTokens (getAppSettings().getStoredPath (Ids::defaultUserModulePath).toString(), ";", {});
  225. for (auto p : paths)
  226. {
  227. p = p.replace ("~", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  228. auto f = File::createFileWithoutCheckingPath (p.trim());
  229. if (f.exists())
  230. addAllModulesInFolder (f);
  231. }
  232. sort();
  233. }
  234. //==============================================================================
  235. LibraryModule::LibraryModule (const ModuleDescription& d)
  236. : moduleInfo (d)
  237. {
  238. }
  239. //==============================================================================
  240. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  241. {
  242. auto& project = projectSaver.project;
  243. auto& modules = project.getModules();
  244. auto id = getID();
  245. if (modules.shouldCopyModuleFilesLocally (id).getValue())
  246. {
  247. auto juceModuleFolder = moduleInfo.getFolder();
  248. auto localModuleFolder = project.getLocalModuleFolder (id);
  249. localModuleFolder.createDirectory();
  250. projectSaver.copyFolder (juceModuleFolder, localModuleFolder);
  251. }
  252. out << "#include <" << moduleInfo.moduleFolder.getFileName() << "/"
  253. << moduleInfo.getHeader().getFileName()
  254. << ">" << newLine;
  255. }
  256. //==============================================================================
  257. static void parseAndAddLibs (StringArray& libList, const String& libs)
  258. {
  259. libList.addTokens (libs, ", ", {});
  260. libList.trim();
  261. libList.sort (false);
  262. libList.removeDuplicates (false);
  263. }
  264. void LibraryModule::addSettingsForModuleToExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  265. {
  266. auto& project = exporter.getProject();
  267. auto moduleRelativePath = exporter.getModuleFolderRelativeToProject (getID());
  268. exporter.addToExtraSearchPaths (moduleRelativePath.getParentDirectory());
  269. String libDirPlatform;
  270. if (exporter.isLinux())
  271. libDirPlatform = "Linux";
  272. else if (exporter.isCodeBlocks() && exporter.isWindows())
  273. libDirPlatform = "MinGW";
  274. else
  275. libDirPlatform = exporter.getTargetFolder().getFileName();
  276. auto libSubdirPath = moduleRelativePath.toUnixStyle() + "/libs/" + libDirPlatform;
  277. auto moduleLibDir = File (project.getProjectFolder().getFullPathName() + "/" + libSubdirPath);
  278. if (moduleLibDir.exists())
  279. exporter.addToModuleLibPaths ({ libSubdirPath, moduleRelativePath.getRoot() });
  280. auto extraInternalSearchPaths = moduleInfo.getExtraSearchPaths().trim();
  281. if (extraInternalSearchPaths.isNotEmpty())
  282. {
  283. auto paths = StringArray::fromTokens (extraInternalSearchPaths, true);
  284. for (auto& path : paths)
  285. exporter.addToExtraSearchPaths (moduleRelativePath.getChildFile (path.unquoted()));
  286. }
  287. {
  288. auto extraDefs = moduleInfo.getPreprocessorDefs().trim();
  289. if (extraDefs.isNotEmpty())
  290. exporter.getExporterPreprocessorDefsValue() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  291. }
  292. {
  293. Array<File> compiled;
  294. auto& modules = project.getModules();
  295. auto id = getID();
  296. auto localModuleFolder = modules.shouldCopyModuleFilesLocally (id).getValue() ? project.getLocalModuleFolder (id)
  297. : moduleInfo.getFolder();
  298. findAndAddCompiledUnits (exporter, &projectSaver, compiled);
  299. if (modules.shouldShowAllModuleFilesInProject (id).getValue())
  300. addBrowseableCode (exporter, compiled, localModuleFolder);
  301. }
  302. if (exporter.isXcode())
  303. {
  304. auto& xcodeExporter = dynamic_cast<XcodeProjectExporter&> (exporter);
  305. if (project.isAUPluginHost())
  306. xcodeExporter.xcodeFrameworks.addTokens (xcodeExporter.isOSX() ? "AudioUnit CoreAudioKit" : "CoreAudioKit", false);
  307. auto frameworks = moduleInfo.moduleInfo [xcodeExporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString();
  308. xcodeExporter.xcodeFrameworks.addTokens (frameworks, ", ", {});
  309. parseAndAddLibs (xcodeExporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  310. }
  311. else if (exporter.isLinux())
  312. {
  313. parseAndAddLibs (exporter.linuxLibs, moduleInfo.moduleInfo ["linuxLibs"].toString());
  314. parseAndAddLibs (exporter.linuxPackages, moduleInfo.moduleInfo ["linuxPackages"].toString());
  315. }
  316. else if (exporter.isWindows())
  317. {
  318. if (exporter.isCodeBlocks())
  319. parseAndAddLibs (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  320. else
  321. parseAndAddLibs (exporter.windowsLibs, moduleInfo.moduleInfo ["windowsLibs"].toString());
  322. }
  323. else if (exporter.isAndroid())
  324. {
  325. parseAndAddLibs (exporter.androidLibs, moduleInfo.moduleInfo ["androidLibs"].toString());
  326. }
  327. }
  328. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  329. {
  330. auto header = moduleInfo.getHeader();
  331. jassert (header.exists());
  332. StringArray lines;
  333. header.readLines (lines);
  334. for (int i = 0; i < lines.size(); ++i)
  335. {
  336. auto line = lines[i].trim();
  337. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  338. {
  339. std::unique_ptr<Project::ConfigFlag> config (new Project::ConfigFlag());
  340. config->sourceModuleID = getID();
  341. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  342. if (config->symbol.length() > 2)
  343. {
  344. ++i;
  345. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  346. {
  347. if (lines[i].trim().isNotEmpty())
  348. config->description = config->description.trim() + " " + lines[i].trim();
  349. ++i;
  350. }
  351. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  352. config->value = project.getConfigFlag (config->symbol);
  353. i += 2;
  354. if (lines[i].contains ("#define " + config->symbol))
  355. {
  356. auto value = lines[i].fromFirstOccurrenceOf ("#define " + config->symbol, false, true).trim();
  357. config->value.setDefault (value == "0" ? false : true);
  358. }
  359. auto currentValue = config->value.get().toString();
  360. if (currentValue == "enabled") config->value = true;
  361. else if (currentValue == "disabled") config->value = false;
  362. flags.add (config.release());
  363. }
  364. }
  365. }
  366. }
  367. //==============================================================================
  368. struct FileSorter
  369. {
  370. static int compareElements (const File& f1, const File& f2)
  371. {
  372. return f1.getFileName().compareNatural (f2.getFileName());
  373. }
  374. };
  375. bool LibraryModule::CompileUnit::hasSuffix (const File& f, const char* suffix)
  376. {
  377. auto fileWithoutSuffix = f.getFileNameWithoutExtension() + ".";
  378. return fileWithoutSuffix.containsIgnoreCase (suffix + String ("."))
  379. || fileWithoutSuffix.containsIgnoreCase (suffix + String ("_"));
  380. }
  381. void LibraryModule::CompileUnit::writeInclude (MemoryOutputStream&) const
  382. {
  383. }
  384. bool LibraryModule::CompileUnit::isNeededForExporter (ProjectExporter& exporter) const
  385. {
  386. if ((hasSuffix (file, "_OSX") && ! exporter.isOSX())
  387. || (hasSuffix (file, "_iOS") && ! exporter.isiOS())
  388. || (hasSuffix (file, "_Windows") && ! exporter.isWindows())
  389. || (hasSuffix (file, "_Linux") && ! exporter.isLinux())
  390. || (hasSuffix (file, "_Android") && ! exporter.isAndroid()))
  391. return false;
  392. auto targetType = Project::getTargetTypeFromFilePath (file, false);
  393. if (targetType != ProjectType::Target::unspecified && ! exporter.shouldBuildTargetType (targetType))
  394. return false;
  395. return exporter.usesMMFiles() ? isCompiledForObjC
  396. : isCompiledForNonObjC;
  397. }
  398. String LibraryModule::CompileUnit::getFilenameForProxyFile() const
  399. {
  400. return "include_" + file.getFileName();
  401. }
  402. Array<LibraryModule::CompileUnit> LibraryModule::getAllCompileUnits (ProjectType::Target::Type forTarget) const
  403. {
  404. auto files = getFolder().findChildFiles (File::findFiles, false);
  405. FileSorter sorter;
  406. files.sort (sorter);
  407. Array<LibraryModule::CompileUnit> units;
  408. for (auto& file : files)
  409. {
  410. if (file.getFileName().startsWithIgnoreCase (getID())
  411. && file.hasFileExtension (sourceFileExtensions))
  412. {
  413. if (forTarget == ProjectType::Target::unspecified
  414. || forTarget == Project::getTargetTypeFromFilePath (file, true))
  415. {
  416. CompileUnit cu;
  417. cu.file = file;
  418. units.add (cu);
  419. }
  420. }
  421. }
  422. for (auto& cu : units)
  423. {
  424. cu.isCompiledForObjC = true;
  425. cu.isCompiledForNonObjC = ! cu.file.hasFileExtension ("mm;m");
  426. if (cu.isCompiledForNonObjC)
  427. if (files.contains (cu.file.withFileExtension ("mm")))
  428. cu.isCompiledForObjC = false;
  429. jassert (cu.isCompiledForObjC || cu.isCompiledForNonObjC);
  430. }
  431. return units;
  432. }
  433. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter,
  434. ProjectSaver* projectSaver,
  435. Array<File>& result,
  436. ProjectType::Target::Type forTarget) const
  437. {
  438. for (auto& cu : getAllCompileUnits (forTarget))
  439. {
  440. if (cu.isNeededForExporter (exporter))
  441. {
  442. auto localFile = exporter.getProject().getGeneratedCodeFolder()
  443. .getChildFile (cu.getFilenameForProxyFile());
  444. result.add (localFile);
  445. if (projectSaver != nullptr)
  446. projectSaver->addFileToGeneratedGroup (localFile);
  447. }
  448. }
  449. }
  450. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  451. {
  452. auto slash = path.indexOfChar (File::getSeparatorChar());
  453. if (slash >= 0)
  454. {
  455. auto topLevelGroup = path.substring (0, slash);
  456. auto remainingPath = path.substring (slash + 1);
  457. auto newGroup = group.getOrCreateSubGroup (topLevelGroup);
  458. addFileWithGroups (newGroup, file, remainingPath);
  459. }
  460. else
  461. {
  462. if (! group.containsChildForFile (file))
  463. group.addRelativeFile (file, -1, false);
  464. }
  465. }
  466. void LibraryModule::findBrowseableFiles (const File& folder, Array<File>& filesFound) const
  467. {
  468. Array<File> tempList;
  469. FileSorter sorter;
  470. DirectoryIterator iter (folder, true, "*", File::findFiles);
  471. bool isHiddenFile;
  472. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  473. if (! isHiddenFile && iter.getFile().hasFileExtension (browseableFileExtensions))
  474. tempList.addSorted (sorter, iter.getFile());
  475. filesFound.addArray (tempList);
  476. }
  477. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  478. {
  479. if (sourceFiles.isEmpty())
  480. findBrowseableFiles (localModuleFolder, sourceFiles);
  481. auto sourceGroup = Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID(), false);
  482. auto moduleFromProject = exporter.getModuleFolderRelativeToProject (getID());
  483. auto moduleHeader = moduleInfo.getHeader();
  484. for (auto& sourceFile : sourceFiles)
  485. {
  486. auto pathWithinModule = FileHelpers::getRelativePathFrom (sourceFile, localModuleFolder);
  487. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  488. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  489. if ((exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFile)) && sourceFile != moduleHeader)
  490. addFileWithGroups (sourceGroup,
  491. moduleFromProject.getChildFile (pathWithinModule),
  492. pathWithinModule);
  493. }
  494. sourceGroup.sortAlphabetically (true, true);
  495. sourceGroup.addFileAtIndex (moduleHeader, -1, false);
  496. exporter.getModulesGroup().state.appendChild (sourceGroup.state.createCopy(), nullptr);
  497. }
  498. //==============================================================================
  499. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  500. : project (p), state (s)
  501. {
  502. }
  503. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  504. {
  505. return ModuleDescription (findFolderForModule (moduleID));
  506. }
  507. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  508. {
  509. return state.getChildWithProperty (Ids::ID, moduleID).isValid();
  510. }
  511. bool EnabledModuleList::isAudioPluginModuleMissing() const
  512. {
  513. return project.getProjectType().isAudioPlugin()
  514. && ! isModuleEnabled ("juce_audio_plugin_client");
  515. }
  516. bool EnabledModuleList::shouldUseGlobalPath (const String& moduleID) const
  517. {
  518. return static_cast<bool> (state.getChildWithProperty (Ids::ID, moduleID)
  519. .getProperty (Ids::useGlobalPath));
  520. }
  521. Value EnabledModuleList::getShouldUseGlobalPathValue (const String& moduleID) const
  522. {
  523. return state.getChildWithProperty (Ids::ID, moduleID)
  524. .getPropertyAsValue (Ids::useGlobalPath, getUndoManager());
  525. }
  526. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  527. {
  528. return state.getChildWithProperty (Ids::ID, moduleID)
  529. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  530. }
  531. File EnabledModuleList::findFolderForModule (const String& moduleID)
  532. {
  533. if (shouldUseGlobalPath (moduleID))
  534. {
  535. if (isJUCEModule (moduleID))
  536. return File (getAppSettings().getStoredPath (Ids::defaultJuceModulePath).toString()).getChildFile (moduleID);
  537. {
  538. auto previous = ModuleList::tryToFindModulePathFromPrevious (moduleID);
  539. if (previous != File())
  540. return previous;
  541. }
  542. ModuleList list;
  543. list.scanGlobalUserModulePath();
  544. if (auto* desc = list.getModuleWithID (moduleID))
  545. return desc->getFolder();
  546. }
  547. else
  548. {
  549. for (auto p : getAllPossibleModulePathsFromExporters (project, true))
  550. {
  551. auto f = getModuleFolderFromPathIfItExists (p.getFullPathName(), moduleID);
  552. if (f != File())
  553. return f;
  554. }
  555. for (auto p : getAllPossibleModulePathsFromExporters (project, false))
  556. {
  557. auto f = getModuleFolderFromPathIfItExists (p.getFullPathName(), moduleID);
  558. if (f != File())
  559. return f;
  560. }
  561. }
  562. return {};
  563. }
  564. struct ModuleTreeSorter
  565. {
  566. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  567. {
  568. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  569. }
  570. };
  571. void EnabledModuleList::sortAlphabetically()
  572. {
  573. ModuleTreeSorter sorter;
  574. state.sort (sorter, getUndoManager(), false);
  575. }
  576. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  577. {
  578. return state.getChildWithProperty (Ids::ID, moduleID)
  579. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  580. }
  581. void EnabledModuleList::addModule (const File& moduleFolder, bool copyLocally, bool useGlobalPath, bool sendAnalyticsEvent)
  582. {
  583. ModuleDescription info (moduleFolder);
  584. if (info.isValid())
  585. {
  586. auto moduleID = info.getID();
  587. if (! isModuleEnabled (moduleID))
  588. {
  589. ValueTree module (Ids::MODULE);
  590. module.setProperty (Ids::ID, moduleID, nullptr);
  591. state.appendChild (module, getUndoManager());
  592. sortAlphabetically();
  593. shouldShowAllModuleFilesInProject (moduleID) = true;
  594. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  595. getShouldUseGlobalPathValue (moduleID) = useGlobalPath;
  596. RelativePath path (moduleFolder.getParentDirectory(),
  597. project.getProjectFolder(), RelativePath::projectFolder);
  598. for (Project::ExporterIterator exporter (project); exporter.next();)
  599. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  600. if (sendAnalyticsEvent)
  601. {
  602. StringPairArray data;
  603. data.set ("label", moduleID);
  604. Analytics::getInstance()->logEvent ("Module Added", data, ProjucerAnalyticsEvent::projectEvent);
  605. }
  606. }
  607. }
  608. }
  609. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  610. {
  611. for (auto 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. auto 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.getCppStandardString();
  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. int 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. int 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 (auto i = getNumModules(); --i >= 0;)
  685. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  686. }
  687. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  688. {
  689. File globalPath (getAppSettings().getStoredPath (Ids::defaultJuceModulePath).toString());
  690. if (globalPath != File())
  691. return globalPath;
  692. ModuleList available;
  693. available.scanProjectExporterModulePaths (project);
  694. for (auto i = available.modules.size(); --i >= 0;)
  695. {
  696. auto f = available.modules.getUnchecked(i)->getFolder();
  697. if (f.isDirectory())
  698. return f.getParentDirectory();
  699. }
  700. return File::getCurrentWorkingDirectory();
  701. }
  702. void EnabledModuleList::addModuleFromUserSelectedFile()
  703. {
  704. static auto lastLocation = findDefaultModulesFolder (project);
  705. FileChooser fc ("Select a module to add...", lastLocation, {});
  706. if (fc.browseForDirectory())
  707. {
  708. lastLocation = fc.getResult();
  709. addModuleOfferingToCopy (lastLocation, true);
  710. }
  711. }
  712. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  713. {
  714. ModuleList list;
  715. list.scanGlobalJuceModulePath();
  716. if (auto* info = list.getModuleWithID (moduleID))
  717. {
  718. addModule (info->moduleFolder, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath(), true);
  719. return;
  720. }
  721. list.scanGlobalUserModulePath();
  722. if (auto* info = list.getModuleWithID (moduleID))
  723. {
  724. addModule (info->moduleFolder, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath(), true);
  725. return;
  726. }
  727. list.scanProjectExporterModulePaths (project);
  728. if (auto* info = list.getModuleWithID (moduleID))
  729. addModule (info->moduleFolder, areMostModulesCopiedLocally(), false, true);
  730. else
  731. addModuleFromUserSelectedFile();
  732. }
  733. void EnabledModuleList::addModuleOfferingToCopy (const File& f, bool isFromUserSpecifiedFolder)
  734. {
  735. ModuleDescription m (f);
  736. if (! m.isValid())
  737. {
  738. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  739. "Add Module", "This wasn't a valid module folder!");
  740. return;
  741. }
  742. if (isModuleEnabled (m.getID()))
  743. {
  744. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  745. "Add Module", "The project already contains this module!");
  746. return;
  747. }
  748. addModule (m.moduleFolder, areMostModulesCopiedLocally(),
  749. isFromUserSpecifiedFolder ? false : areMostModulesUsingGlobalPath(),
  750. true);
  751. }
  752. bool isJUCEFolder (const File& f)
  753. {
  754. return isJUCEModulesFolder (f.getChildFile ("modules"));
  755. }
  756. bool isJUCEModulesFolder (const File& f)
  757. {
  758. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  759. }